| 1 |
{%MainUnit castleglimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2019-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Part of CastleGLImages unit: TDrawableImageCache. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{$endif read_interface} |
| 22 |
|
| 23 |
{$ifdef read_implementation} |
| 24 |
|
| 25 |
type |
| 26 |
{ Cache of TDrawableImage, one instance for one URL. |
| 27 |
This is only internally used by TCastleImagePersistent now, |
| 28 |
not visible from the outside. } |
| 29 |
TDrawableImageCache = class |
| 30 |
strict private |
| 31 |
type |
| 32 |
TCachedDrawableImage = class |
| 33 |
References: Cardinal; |
| 34 |
Url: String; |
| 35 |
DrawableImage: TDrawableImage; |
| 36 |
end; |
| 37 |
TCachedDrawableImageList = {$ifdef FPC}specialize{$endif} TObjectList<TCachedDrawableImage>; |
| 38 |
var |
| 39 |
Cached: TCachedDrawableImageList; |
| 40 |
public |
| 41 |
Log: Boolean; |
| 42 |
constructor Create; |
| 43 |
destructor Destroy; override; |
| 44 |
|
| 45 |
{ Load new image, possibly only increasing reference to cache. |
| 46 |
|
| 47 |
TODO: ASmoothScaling is not used as a key to select different cache, |
| 48 |
instead it's just the initial value in case we need to create |
| 49 |
the image. |
| 50 |
In the long run, we should probably have different cache entries |
| 51 |
for different ASmoothScaling versions. } |
| 52 |
function LoadDrawableImage(const Url: String; |
| 53 |
const ASmoothScaling: Boolean): TDrawableImage; |
| 54 |
procedure FreeDrawableImage(var DrawableImage: TDrawableImage); |
| 55 |
end; |
| 56 |
|
| 57 |
function DrawableImageCache: TDrawableImageCache; forward; |
| 58 |
|
| 59 |
{ ---------------------------------------------------------------------------- } |
| 60 |
|
| 61 |
constructor TDrawableImageCache.Create; |
| 62 |
begin |
| 63 |
inherited; |
| 64 |
Cached := TCachedDrawableImageList.Create; |
| 65 |
end; |
| 66 |
|
| 67 |
destructor TDrawableImageCache.Destroy; |
| 68 |
begin |
| 69 |
if Cached <> nil then |
| 70 |
begin |
| 71 |
Assert(Cached.Count = 0, 'Some references to TDrawableImage still exist when freeing TDrawableImageCache'); |
| 72 |
FreeAndNil(Cached); |
| 73 |
end; |
| 74 |
inherited; |
| 75 |
end; |
| 76 |
|
| 77 |
function TDrawableImageCache.LoadDrawableImage(const Url: String; |
| 78 |
const ASmoothScaling: Boolean): TDrawableImage; |
| 79 |
var |
| 80 |
C: TCachedDrawableImage; |
| 81 |
begin |
| 82 |
for C in Cached do |
| 83 |
begin |
| 84 |
if C.Url = Url then |
| 85 |
begin |
| 86 |
Inc(C.References); |
| 87 |
if Log then |
| 88 |
WritelnLog('DrawableImageCache', '++ : TDrawableImage %s : %d', [Url, C.References]); |
| 89 |
Exit(C.DrawableImage); |
| 90 |
end; |
| 91 |
end; |
| 92 |
|
| 93 |
Result := TDrawableImage.Create(Url); |
| 94 |
|
| 95 |
C := TCachedDrawableImage.Create; |
| 96 |
C.References := 1; |
| 97 |
C.Url := Url; |
| 98 |
C.DrawableImage := Result; |
| 99 |
Cached.Add(C); |
| 100 |
|
| 101 |
if Log then |
| 102 |
WritelnLog('DrawableImageCache', '++ : TDrawableImage %s : %d', [Url, C.References]); |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TDrawableImageCache.FreeDrawableImage(var DrawableImage: TDrawableImage); |
| 106 |
var |
| 107 |
I: Integer; |
| 108 |
C: TCachedDrawableImage; |
| 109 |
begin |
| 110 |
if DrawableImage = nil then Exit; |
| 111 |
|
| 112 |
for I := 0 to Cached.Count - 1 do |
| 113 |
begin |
| 114 |
C := Cached[I]; |
| 115 |
if C.DrawableImage = DrawableImage then |
| 116 |
begin |
| 117 |
if Log then |
| 118 |
WritelnLog('DrawableImageCache', '-- : TDrawableImage %s : %d', [C.Url, C.References - 1]); |
| 119 |
DrawableImage := nil; |
| 120 |
if C.References = 1 then |
| 121 |
begin |
| 122 |
FreeAndNil(C.DrawableImage); |
| 123 |
Cached.Delete(I); |
| 124 |
end else |
| 125 |
Dec(C.References); |
| 126 |
Exit; |
| 127 |
end; |
| 128 |
end; |
| 129 |
|
| 130 |
raise EInternalError.CreateFmt('FreeDrawableImage: no reference found for image %s', |
| 131 |
[PointerToStr(DrawableImage)]); |
| 132 |
end; |
| 133 |
|
| 134 |
var |
| 135 |
FDrawableImageCache: TDrawableImageCache; |
| 136 |
|
| 137 |
function DrawableImageCache: TDrawableImageCache; |
| 138 |
begin |
| 139 |
if FDrawableImageCache = nil then |
| 140 |
begin |
| 141 |
FDrawableImageCache := TDrawableImageCache.Create; |
| 142 |
// FDrawableImageCache.Log := true; // for debug |
| 143 |
end; |
| 144 |
Result := FDrawableImageCache; |
| 145 |
end; |
| 146 |
|
| 147 |
{$endif read_implementation} |