GR32_RepaintOpt.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. unit GR32_RepaintOpt;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Repaint Optimizer Extension for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Andre Beckedorf - metaException
  26. * [email protected]
  27. *
  28. * Portions created by the Initial Developer are Copyright (C) 2005-2009
  29. * the Initial Developer. All Rights Reserved.
  30. *
  31. * ***** END LICENSE BLOCK ***** *)
  32. interface
  33. {$include GR32.inc}
  34. uses
  35. {$IFDEF FPC}
  36. LCLIntf,
  37. {$ENDIF}
  38. Generics.Defaults,
  39. Generics.Collections,
  40. Classes, SysUtils,
  41. GR32,
  42. GR32_Containers,
  43. GR32_Layers;
  44. {$if defined(FPC) or (CompilerVersion < 35.0)}
  45. type
  46. TNoRefCountObject = TSingletonImplementation;
  47. {$ifend}
  48. type
  49. { TCustomRepaintOptimizer }
  50. TCustomRepaintOptimizer = class(TNoRefCountObject)
  51. private type
  52. TLayerCollectionList = TList<TLayerCollection>;
  53. private
  54. FEnabled: Boolean;
  55. FLayerCollections: TLayerCollectionList;
  56. FInvalidRects: TRectList;
  57. FBuffer: TBitmap32;
  58. protected
  59. function GetEnabled: Boolean; virtual;
  60. procedure SetEnabled(const Value: Boolean); virtual;
  61. property LayerCollections: TLayerCollectionList read FLayerCollections;
  62. property Buffer: TBitmap32 read FBuffer write FBuffer;
  63. property InvalidRects: TRectList read FInvalidRects write FInvalidRects;
  64. public
  65. constructor Create(Buffer: TBitmap32; InvalidRects: TRectList); virtual;
  66. destructor Destroy; override;
  67. procedure RegisterLayerCollection(Layers: TLayerCollection); virtual;
  68. procedure UnregisterLayerCollection(Layers: TLayerCollection); virtual;
  69. procedure BeginPaint; virtual;
  70. procedure EndPaint; virtual;
  71. procedure BeginPaintBuffer; virtual;
  72. procedure EndPaintBuffer; virtual;
  73. procedure Reset; virtual; abstract;
  74. function UpdatesAvailable: Boolean; virtual; abstract;
  75. procedure PerformOptimization; virtual; abstract;
  76. // handlers
  77. procedure BufferResizedHandler(const NewWidth, NewHeight: Integer); virtual; abstract;
  78. property Enabled: Boolean read GetEnabled write SetEnabled;
  79. end;
  80. TCustomRepaintOptimizerClass = class of TCustomRepaintOptimizer;
  81. // differs from InflateRect in the way that it does also handle negative rects
  82. procedure InflateArea(var Area: TRect; Dx, Dy: Integer);
  83. implementation
  84. procedure InflateArea(var Area: TRect; Dx, Dy: Integer);
  85. begin
  86. if Area.Left > Area.Right then
  87. Dx := -Dx;
  88. if Area.Top > Area.Bottom then
  89. Dy := -Dy;
  90. Dec(Area.Left, Dx); Dec(Area.Top, Dy);
  91. Inc(Area.Right, Dx); Inc(Area.Bottom, Dy);
  92. end;
  93. { TCustomRepaintManager }
  94. constructor TCustomRepaintOptimizer.Create(Buffer: TBitmap32; InvalidRects: TRectList);
  95. begin
  96. inherited Create;
  97. FInvalidRects := InvalidRects;
  98. FBuffer := Buffer;
  99. end;
  100. destructor TCustomRepaintOptimizer.Destroy;
  101. var
  102. i: Integer;
  103. begin
  104. if (FLayerCollections <> nil) then
  105. for i := FLayerCollections.Count-1 downto 0 do
  106. UnregisterLayerCollection(FLayerCollections[i]);
  107. FLayerCollections.Free;
  108. inherited;
  109. end;
  110. function TCustomRepaintOptimizer.GetEnabled: Boolean;
  111. begin
  112. Result := FEnabled;
  113. end;
  114. procedure TCustomRepaintOptimizer.SetEnabled(const Value: Boolean);
  115. begin
  116. FEnabled := Value;
  117. end;
  118. procedure TCustomRepaintOptimizer.RegisterLayerCollection(Layers: TLayerCollection);
  119. begin
  120. if (FLayerCollections = nil) then
  121. FLayerCollections := TLayerCollectionList.Create;
  122. if not FLayerCollections.Contains(Layers) then
  123. begin
  124. FLayerCollections.Add(Layers);
  125. Layers.Subscribe(Self);
  126. end;
  127. end;
  128. procedure TCustomRepaintOptimizer.UnregisterLayerCollection(Layers: TLayerCollection);
  129. begin
  130. if (FLayerCollections <> nil) and FLayerCollections.Contains(Layers) then
  131. begin
  132. Layers.Unsubscribe(Self);
  133. FLayerCollections.Remove(Layers);
  134. end;
  135. end;
  136. procedure TCustomRepaintOptimizer.BeginPaint;
  137. begin
  138. // do nothing by default
  139. end;
  140. procedure TCustomRepaintOptimizer.EndPaint;
  141. begin
  142. // do nothing by default
  143. end;
  144. procedure TCustomRepaintOptimizer.BeginPaintBuffer;
  145. begin
  146. // do nothing by default
  147. end;
  148. procedure TCustomRepaintOptimizer.EndPaintBuffer;
  149. begin
  150. // do nothing by default
  151. end;
  152. end.