GR32_RepaintOpt.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * Contributor(s):
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$I GR32.inc}
  36. uses
  37. {$IFDEF FPC}
  38. LCLIntf,
  39. {$ELSE}
  40. Types, Windows,
  41. {$ENDIF}
  42. Classes, SysUtils, GR32, GR32_Containers, GR32_Layers;
  43. type
  44. { TCustomRepaintOptimizer }
  45. TCustomRepaintOptimizer = class
  46. private
  47. FEnabled: Boolean;
  48. FLayerCollections: TList;
  49. FInvalidRects: TRectList;
  50. FBuffer: TBitmap32;
  51. protected
  52. function GetEnabled: Boolean; virtual;
  53. procedure SetEnabled(const Value: Boolean); virtual;
  54. property LayerCollections: TList read FLayerCollections write FLayerCollections;
  55. property Buffer: TBitmap32 read FBuffer write FBuffer;
  56. property InvalidRects: TRectList read FInvalidRects write FInvalidRects;
  57. // LayerCollection handler
  58. procedure LayerCollectionNotifyHandler(Sender: TLayerCollection;
  59. Action: TLayerListNotification; Layer: TCustomLayer; Index: Integer); virtual; abstract;
  60. public
  61. constructor Create(Buffer: TBitmap32; InvalidRects: TRectList); virtual;
  62. destructor Destroy; override;
  63. procedure RegisterLayerCollection(Layers: TLayerCollection); virtual;
  64. procedure UnregisterLayerCollection(Layers: TLayerCollection); virtual;
  65. procedure BeginPaint; virtual;
  66. procedure EndPaint; virtual;
  67. procedure BeginPaintBuffer; virtual;
  68. procedure EndPaintBuffer; virtual;
  69. procedure Reset; virtual; abstract;
  70. function UpdatesAvailable: Boolean; virtual; abstract;
  71. procedure PerformOptimization; virtual; abstract;
  72. // handlers
  73. procedure AreaUpdateHandler(Sender: TObject; const Area: TRect; const Info: Cardinal); virtual; abstract;
  74. procedure LayerUpdateHandler(Sender: TObject; Layer: TCustomLayer); virtual; abstract;
  75. procedure BufferResizedHandler(const NewWidth, NewHeight: Integer); virtual; abstract;
  76. property Enabled: Boolean read GetEnabled write SetEnabled;
  77. end;
  78. TCustomRepaintOptimizerClass = class of TCustomRepaintOptimizer;
  79. // differs from InflateRect in the way that it does also handle negative rects
  80. procedure InflateArea(var Area: TRect; Dx, Dy: Integer);
  81. implementation
  82. procedure InflateArea(var Area: TRect; Dx, Dy: Integer);
  83. begin
  84. if Area.Left > Area.Right then
  85. Dx := -Dx;
  86. if Area.Top > Area.Bottom then
  87. Dy := -Dy;
  88. Dec(Area.Left, Dx); Dec(Area.Top, Dy);
  89. Inc(Area.Right, Dx); Inc(Area.Bottom, Dy);
  90. end;
  91. type
  92. TLayerCollectionAccess = class(TLayerCollection);
  93. { TCustomRepaintManager }
  94. constructor TCustomRepaintOptimizer.Create(Buffer: TBitmap32; InvalidRects: TRectList);
  95. begin
  96. FLayerCollections := TList.Create;
  97. FInvalidRects := InvalidRects;
  98. FBuffer := Buffer;
  99. end;
  100. destructor TCustomRepaintOptimizer.Destroy;
  101. var
  102. I: Integer;
  103. begin
  104. for I := 0 to FLayerCollections.Count - 1 do
  105. UnregisterLayerCollection(TLayerCollection(FLayerCollections[I]));
  106. FLayerCollections.Free;
  107. inherited;
  108. end;
  109. function TCustomRepaintOptimizer.GetEnabled: Boolean;
  110. begin
  111. Result := FEnabled;
  112. end;
  113. procedure TCustomRepaintOptimizer.SetEnabled(const Value: Boolean);
  114. begin
  115. FEnabled := Value;
  116. end;
  117. procedure TCustomRepaintOptimizer.RegisterLayerCollection(Layers: TLayerCollection);
  118. begin
  119. if FLayerCollections.IndexOf(Layers) = -1 then
  120. begin
  121. FLayerCollections.Add(Layers);
  122. TLayerCollectionAccess(Layers).OnListNotify := LayerCollectionNotifyHandler;
  123. end;
  124. end;
  125. procedure TCustomRepaintOptimizer.UnregisterLayerCollection(Layers: TLayerCollection);
  126. begin
  127. TLayerCollectionAccess(Layers).OnListNotify := nil;
  128. FLayerCollections.Remove(Layers);
  129. end;
  130. procedure TCustomRepaintOptimizer.BeginPaint;
  131. begin
  132. // do nothing by default
  133. end;
  134. procedure TCustomRepaintOptimizer.EndPaint;
  135. begin
  136. // do nothing by default
  137. end;
  138. procedure TCustomRepaintOptimizer.BeginPaintBuffer;
  139. begin
  140. // do nothing by default
  141. end;
  142. procedure TCustomRepaintOptimizer.EndPaintBuffer;
  143. begin
  144. // do nothing by default
  145. end;
  146. end.