File: patternbitmap.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (237 lines) | stat: -rw-r--r-- 6,365 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{%MainUnit ../graphics.pp}
{******************************************************************************
                          TPatternBitmapCache
 ******************************************************************************

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}

{
  Delphi does not expose any of the internal structures that deal
  with the management or byproducts of calls to AllocPatternBitmap,
  especially the caching mechanisme and the cache are not exposed.
  So all class definitions for this are in the implementation section.
}


type
  { TPatternBitmap }

  TPatternBitmap = class
  private
    FBitmap: TBitmap;
  public
    ColorBG: TColor;
    ColorFG: TColor;
    constructor Create(AColorBG, AColorFG: TColor);
    destructor Destroy; override;
    function GetBitmap: TBitmap;
  end;

  { TPatternBitmapCache }

  TPatternBitmapCache = class
  private
    FLock: TCriticalSection;
    FList: TAvlTree;
    function InternalCompare(Tree: TAvlTree; Data1, Data2: Pointer): integer;
  protected
    procedure Lock;
    procedure UnLock;
  public
    constructor Create;
    destructor Destroy; override;
    function Add(ABitmap: TPatternBitmap): TPatternBitmap;
    function FindBitmap(AColorBG, AColorFG: TColor): TPatternBitmap;
    function Count: Integer;
  end;

var
  PatternBitmapCache: TPatternBitmapCache;

type
  TPatternRec = record
    ColorBG, ColorFG: TColor;
  end;
  PPatternRec = ^TPatternRec;

function CompareColors(C1, C2: TColor): Integer;
begin
  if (C1 = C2) then
    Result := 0
  else
    if (C1 < C2) then
      Result := -1
    else Result := +1;
end;

function AllocPatternBitmap(colorBG, colorFG: TColor): TBitmap;
var
  x,y: Integer;
  PatternBmp: TPatternBitmap;
begin
  Result := nil;
  if not Assigned(PatternBitmapCache) then
    Exit;
  PatternBmp := PatternBitmapCache.FindBitmap(colorBG, colorFG);
  if not Assigned(PatternBmp) then
  begin
    {$ifdef debugpatternbitmap}
    debugln('AllocPatternBitmap: FindBitmap = nil');
    {$endif}
    PatternBmp := TPatternBitmap.Create(colorBG, colorFG);
    PatternBitmapCache.Add(PatternBmp);
  end;
  {$ifdef debugpatternbitmap}
  debugln(['AllocPatternBitmap: FindBitmap = ',Pointer(PatternBmp)]);
  {$endif}
  Result := PatternBmp.GetBitmap;
end;

constructor TPatternBitmap.Create(AColorBG, AColorFG: TColor);
var
  x, y: Integer;
begin
  ColorBG := AColorBG and $00FFFFFF; //don't use systemcolors
  ColorFG := AColorFG and $00FFFFFF;
  FBitmap := TBitmap.Create;
  FBitmap.Width := 8;
  FBitmap.Height := 8;
  FBitmap.Canvas.Brush.Style := bsSolid;
  FBitmap.Canvas.Brush.Color := colorBG;
  FBitmap.Canvas.Rectangle(0,0,7,7);
  for y := 0 to 7 do
    for x := 0 to 7 do
    begin
      if ((not Odd(x)) and (not Odd(y))) xor ((Odd(x)) and (Odd(y))) then
        FBitmap.Canvas.Pixels[x,y] := colorFG;
    end;
end;

destructor TPatternBitmap.Destroy;
begin
  {$ifdef debugpatternbitmap}
  debugln(['TPatternBitmap.Destroy: Freeing FBitmap: ',Pointer(FBitmap)]);
  {$endif}
  FBitmap.Clear;
  FBitmap.Free;
  inherited Destroy;
end;

function TPatternBitmap.GetBitmap: TBitmap;
begin
  Result := FBitmap;
end;

{ TPatternBitmapCache }


function TPatternBitmapCache.InternalCompare(Tree: TAvlTree; Data1, Data2: Pointer): integer;
var
  Bmp1: TPatternBitmap absolute Data1;
  Bmp2: TPatternBitmap absolute Data2;
begin
  Result := CompareColors(Bmp1.ColorBG, Bmp2.ColorBG);
  if (Result = 0) then
    Result := CompareColors(Bmp1.ColorFG, Bmp2.ColorFG);
  {$ifdef debugpatternbitmap}
  debugln(['TPatternBitmapCache.InternalCompare: Result = ',Result]);
  {$endif}
end;

procedure TPatternBitmapCache.Lock;
begin
  FLock.Enter;
end;

procedure TPatternBitmapCache.UnLock;
begin
  FLock.Leave;
end;

constructor TPatternBitmapCache.Create;
begin
  {$ifdef debugpatternbitmap}
  debugln(['TPatternBitmapCache.Create']);
  {$endif}
  FLock := TCriticalSection.Create;
  FList := TAvlTree.CreateObjectCompare(@InternalCompare);
end;

destructor TPatternBitmapCache.Destroy;
begin
  {$ifdef debugpatternbitmap}
    debugln(['TPatternBitmapCache.Destroy: Assigned(WidgetSet) = ',Assigned(WidgetSet),
           ', FList.Count = ',FList.Count]);
  {$endif}
  FList.FreeAndClear;
  FList.Free;
  FLock.Free;
  inherited Destroy;
end;

function TPatternBitmapCache.Add(ABitmap: TPatternBitmap): TPatternBitmap;
begin
  Lock;
  try
    if (FindBitmap(ABitmap.ColorBG, ABitmap.ColorFG) <> nil) then
      RaiseGDBException(ClassName+'.Add: ABitmap added twice');
    {$ifdef debugpatternbitmap}
    debugln('TPatternBitmapCache.Add: FindBitmap = nil');
    {$endif}
    Result := TPatternBitmap((FList.Add(ABitmap)).Data);
    {$ifdef debugpatternbitmap}
    debugln(['  Result = ',Pointer(Result)]);
    {$endif}
    if (FList.Count mod 1000 = 0) then debugln([Self.ClassName,'.Add: Added ',FList.Count,' items.']);
    if (FindBitmap(ABitmap.ColorBG, ABitmap.ColorFG) = nil) then
    begin
      {$IFNDEF DisableChecks}
      DebugLn(['TPatternBitmapCache fatal error: cannot retrieve added bitmap: ', Pointer(Result)]);
      {$ENDIF}
      RaiseGDBException(ClassName+' fatal error: cannot retrieve added bitmap');
    end;
  finally
    UnLock;
  end;
end;

function InternalCompareKeyWithData(Key, Data: Pointer): Integer;
var
  PatternRec: PPatternRec absolute Key;
  Bmp: TPatternBitmap absolute Data;
begin
  Result := CompareColors(PatternRec^.ColorBG, Bmp.ColorBG);
  if (Result = 0) then
  begin
    Result := CompareColors(PatternRec^.ColorFG, Bmp.ColorFG);
  end;
end;

function TPatternBitmapCache.FindBitmap(AColorBG, AColorFG: TColor): TPatternBitmap;
var
  PatternRec: TPatternRec;
  Res: TAvlTreeNode;
begin
  Lock;
  Result := nil;
  AColorBG := AColorBG and $00FFFFFF; //do not use systemcolors
  AColorFG := AColorFG and $00FFFFFF; //do not use systemcolors
  PatternRec.ColorBG := AColorBG;
  PatternRec.ColorFG := AColorFG;
  Res := FList.FindKey(@PatternRec, @InternalCompareKeyWithData);
  if (Res <> nil) then
    Result := TPatternBitmap(Res.Data);
  UnLock;
end;

function TPatternBitmapCache.Count: Integer;
begin
  Result := FList.Count;
end;