Debian Codegraph @

src/scene/x3d/x3dnodes_x3dnodescache.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2018 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 {$ifdef read_interface}
18
19 { Cache for X3D nodes.
20 Descends from TTexturesVideosCache, so can also cache textures and movies. }
21 TX3DNodesCache = class(TTexturesVideosCache)
22 strict private
23 type
24 TCachedNode = class
25 private
26 URL: String;
27 References: Cardinal;
28 Node: TX3DRootNode;
29 end;
30 TCachedNodeDictionary = {$ifdef FPC}specialize{$endif} TObjectDictionary<String, TCachedNode>;
31 var
32 CachedNodes: TCachedNodeDictionary;
33 function FindCachedNode(const Node: TX3DRootNode): TCachedNode;
34 public
35 Log: Boolean;
36
37 constructor Create;
38 destructor Destroy; override;
39
40 { Load model, just like @link(X3DLoad.LoadNode) but with a cache.
41 URL must be absolute (not relative).
42
43 Note that this should not be used if you plan to modify the model graph
44 (for example by VRML/X3D events). In such case, the cache should not
45 be used, as it would make all the model instances shared.
46 For example, if you inline the same model multiple times, you could not
47 modify one instance independent from another. }
48 function LoadNode(const URL: String): TX3DRootNode;
49
50 { Is the given URL cached. }
51 function IsCached(const URL: String): Boolean;
52
53 { If the given URL is cached, return a DeepCopy of the node from cache.
54 Otherwise returns @nil.
55 This makes sense if you possibly already have this model in the cache,
56 and now you want to load it again, *and* modify the X3D nodes tree afterwards. }
57 function TryCopyNode(const URL: String): TX3DRootNode;
58
59 { Unload previously loaded here model.
60 Node may be @nil (then it's ignored), or something loaded by
61 LoadNode (then it's released and changed to @nil). }
62 procedure FreeNode(var Node: TX3DRootNode);
63
64 function Empty: boolean; override;
65 end;
66
67 {$endif read_interface}
68
69 {$ifdef read_implementation}
70
71 { TX3DNodesCache ------------------------------------------------------------ }
72
73 var
74 CurrentlyLoading: TCastleStringList;
75
76 constructor TX3DNodesCache.Create;
77 begin
78 inherited;
79 CachedNodes := TCachedNodeDictionary.Create([doOwnsValues]);
80 end;
81
82 destructor TX3DNodesCache.Destroy;
83 begin
84 if CachedNodes <> nil then
85 begin
86 Assert(CachedNodes.Count = 0, 'Some references to nodes still exist when freeing TX3DNodesCache');
87 FreeAndNil(CachedNodes);
88 end;
89 inherited;
90 end;
91
92 function TX3DNodesCache.TryCopyNode(const URL: String): TX3DRootNode;
93 var
94 C: TCachedNode;
95 begin
96 if CachedNodes.TryGetValue(URL, C) then
97 Result := C.Node.DeepCopy as TX3DRootNode
98 else
99 Result := nil;
100 end;
101
102 function TX3DNodesCache.LoadNode(const URL: String): TX3DRootNode;
103 var
104 Index: Integer;
105 C: TCachedNode;
106 begin
107 if CachedNodes.TryGetValue(URL, C) then
108 begin
109 Inc(C.References);
110
111 if Log then
112 WritelnLog('++', 'Node %s : %d', [UriDisplay(URL), C.References]);
113
114 Exit(C.Node);
115 end;
116
117 { Add URL to CurrentlyLoading, detecting an infinite loop,
118 see https://sourceforge.net/p/castle-engine/tickets/11/ }
119 if CurrentlyLoading.IndexOf(URL) <> -1 then
120 begin
121 raise EX3DError.CreateFmt('Model references itself (through EXTERNPROTO or Inline), cannot load: %s',
122 [URL]);
123 end;
124 CurrentlyLoading.Add(URL);
125
126 { Initialize Result first, before calling CachedNodes.Add.
127 That's because in case LoadNode raises exception,
128 we don't want to add image to cache (because caller would have
129 no way to call FreeNode later). }
130
131 Result := X3DLoad.LoadNode(URL);
132
133 { Remove URL from CurrentlyLoading }
134 Index := CurrentlyLoading.IndexOf(URL);
135 Assert(Index <> -1);
136 CurrentlyLoading.Delete(Index);
137
138 C := TCachedNode.Create;
139 CachedNodes.Add(URL, C);
140 C.References := 1;
141 C.URL := URL;
142 C.Node := Result;
143
144 if Log then
145 WritelnLog('++', 'Node %s : %d', [UriDisplay(URL), 1]);
146 end;
147
148 function TX3DNodesCache.IsCached(const URL: String): Boolean;
149 begin
150 Result := CachedNodes.ContainsKey(URL);
151 end;
152
153 function TX3DNodesCache.FindCachedNode(const Node: TX3DRootNode): TCachedNode;
154 begin
155 for Result in CachedNodes.Values do
156 if Result.Node = Node then
157 Exit;
158 Result := nil;
159 end;
160
161 procedure TX3DNodesCache.FreeNode(var Node: TX3DRootNode);
162 var
163 URL: String;
164 C: TCachedNode;
165 begin
166 if Node = nil then Exit;
167
168 C := FindCachedNode(Node);
169 if C = nil then
170 raise EInternalError.CreateFmt('FreeNode: no reference found for node %s',
171 [PointerToStr(Node)]);
172
173 if Log then
174 WritelnLog('--', 'Node %s : %d', [UriDisplay(C.URL), C.References - 1]);
175
176 Node := nil;
177
178 if C.References = 1 then
179 begin
180 URL := C.URL;
181
182 { Note that deleting a node may cause recursive FreeNode call that may also remove
183 something, and shift our indexes.
184 Testcase when it's needed (TODO link not working anymore, find this):
185 http://www.web3d.org/x3d/content/examples/Basic/CAD/_pages/page02.html
186 Current code works regardless of this. }
187 FreeAndNil(C.Node);
188
189 CachedNodes.Remove(URL);
190 CheckEmpty;
191 end else
192 Dec(C.References);
193 end;
194
195 function TX3DNodesCache.Empty: boolean;
196 begin
197 Result := (inherited Empty) and (CachedNodes.Count = 0);
198 end;
199
200 {$endif read_implementation}