-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.ImgLoadQueue.pas
305 lines (281 loc) · 8.18 KB
/
Apus.Engine.ImgLoadQueue.pas
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Image loading queue for multithreading preload of images
// PNG/JPG/TGA/DDS/PVR only
// JPEG with external RAW alpha channel NOT SUPPORTED!
//
// Copyright (C) 2019 Ivan Polyacov, Apus Software ([email protected])
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
unit Apus.Engine.ImgLoadQueue;
interface
uses Apus.Common, Apus.Images;
type
// What happens when requested image is queued, but not ready
TQueueRequestMode=(
qrmWait, // block and wait until image is ready (requested image get highest priority)
qrmReturnSource, // if file is loaded but not yet unpacked - return its content in imageSource and abort task
qrmAbort, // abort task
qrmIgnore); // do nothing, return nothing
threadvar
imageSource:ByteArray;
// Queue file for loading, name must be with extension
procedure QueueFileLoad(fname:string);
// Starts one loading thread and 1..4 unpacking threads
procedure StartLoadingThreads(unpackThreadsNum:integer=2);
// Get raw image from queue if exists or nil elsewere
// Waits if image is queued, but not yet processed
function GetImageFromQueue(fname:string;mode:TQueueRequestMode=qrmWait):TRawImage;
implementation
uses Apus.CrossPlatform, SysUtils, Classes, Apus.GfxFormats;
// Queue
type
TLoadQueueStatus=(lqsNone, // No entry
lqsWaiting, // Load operation queued
lqsLoading, // File is loading
lqsLoaded, // Loading done, waiting for unpack
lqsUnpacking, // Processing by an unpacking thread
lqsReady, // All done! Final RAW image data is ready
lqsError); // Failed to load the image
PQueueEntry=^TQueueEntry;
TQueueEntry=record
status:TLoadQueueStatus;
fname:string;
srcData:ByteArray;
img:TRawImage;
format:TImageFileType;
info:TImageFileInfo;
timeLoaded,timeUnpacked:int64;
next:PQueueEntry;
end;
var
cSect:TMyCriticalSection;
firstItem,lastItem:PQueueEntry;
// Threads
type
TLoadingThread=class(TThread)
procedure Execute; override;
end;
TUnpackThread=class(TThread)
procedure Execute; override;
end;
var
// This thread load image files
loadingThread:TLoadingThread;
// These threads are unpacking compressed images (JPG/PNG)
unpackThreads:array[1..4] of TUnpackThread;
maxUnpackTime:integer;
function GetImageFromQueue(fname:string;mode:TQueueRequestMode=qrmWait):TRawImage;
var
item,prev:PQueueEntry;
found:boolean;
begin
result:=nil;
if firstItem=nil then exit;
cSect.Enter;
try
item:=firstItem;
prev:=nil;
found:=false;
while item<>nil do begin
if (item.status<>lqsNone) and SameText(fname,item.fname) then begin
found:=true;
break;
end;
prev:=item;
item:=item.next;
end;
if not found then exit;
if item.status=lqsReady then exit(item.img); // success
case mode of
qrmIgnore:exit;
qrmAbort:begin
item.status:=lqsNone;
exit;
end;
qrmReturnSource:
if (item.status in [lqsLoaded,lqsUnpacking]) then begin
if item.status=lqsLoaded then item.status:=lqsNone;
imageSource:=item.srcData;
exit;
end;
qrmWait:begin
LogMessage('Waiting for %s, status %d',[fname,ord(item.status)]);
// try to handle this earlier -> move on top
if (prev<>nil) and (item.status in [lqsWaiting,lqsLoaded]) then begin
prev.next:=item.next;
item.next:=firstItem;
MemoryBarrier;
firstItem:=item;
end;
end;
end;
finally
cSect.Leave;
end;
// Wait for result
while not (item.status in [lqsNone,lqsReady,lqsError]) do sleep(0);
if item.status=lqsReady then result:=item.img;
end;
procedure QueueFileLoad(fname:string);
var
item:PQueueEntry;
begin
fname:=FileName(fname);
cSect.Enter;
try
New(item);
ZeroMem(item^,sizeof(TQueueEntry));
item.status:=lqsWaiting;
item.fname:=fname;
if lastItem<>nil then lastItem.next:=item;
MemoryBarrier;
lastItem:=item;
if firstItem=nil then firstItem:=item;
finally
cSect.Leave;
end;
end;
procedure LockImgQueue;
begin
cSect.Enter;
end;
procedure UnlockImgQueue;
begin
cSect.Leave;
end;
procedure StartLoadingThreads(unpackThreadsNum:integer=2);
var
i:integer;
begin
loadingThread:=TLoadingThread.Create(false);
for i:=1 to min2(unpackThreadsNum,high(unpackThreads)) do
unpackThreads[i]:=TUnpackThread.Create(false);
end;
{ TLoadingThread }
procedure TLoadingThread.Execute;
var
i,n:integer;
item,start:PQueueEntry;
procedure Restart;
begin
// Wait until queue not empty
while firstItem=nil do sleep(10);
start:=firstItem;
item:=start;
end;
begin
RegisterThread('QLoading');
try
Restart;
repeat
while (item<>nil) and (item.status<>lqsWaiting) do item:=item.next;
if item=nil then begin
Sleep(10);
Restart;
continue;
end;
if item.status=lqsWaiting then
with item^ do begin
LogMessage('Preloading '+fname);
try
srcData:=LoadFileAsBytes(fname);
timeLoaded:=MyTickCount;
except
on e:Exception do ForceLogMessage('Loader error; '+ExceptionMsg(e));
end;
if length(srcData)<30 then begin
ForceLogMessage('Failed to load file: '+fname);
status:=lqsError;
end;
format:=CheckImageFormat(srcData);
info:=imgInfo;
status:=lqsLoaded; // ready for processing
end;
if firstItem<>start then begin
Restart;
continue;
end;
item:=item.next;
until terminated;
except
on e:Exception do ErrorMessage('Error in LoadingThread: '+ExceptionMsg(e));
end;
UnregisterThread;
end;
{ TUnpackThread }
procedure TUnpackThread.Execute;
var
item:PQueueEntry;
t:int64;
begin
RegisterThread('QUnpack');
try
repeat
sleep(1); // Never wait inside CS!
item:=firstItem;
// Find the first waiting entry
cSect.Enter;
try
while (item<>nil) and (item.status<>lqsLoaded) do item:=item.next;
if item<>nil then
item.status:=lqsUnpacking;
finally
cSect.Leave;
end;
if item<>nil then begin
// Unpack image
t:=MyTickCount;
with item^ do
try
img:=nil;
if format=ifTGA then LoadTGA(srcData,img,true) else
if format=ifJPEG then LoadJPEG(srcData,img) else
if format=ifPNG then LoadPNG(srcData,img) else
if format=ifPVR then LoadPVR(srcData,img,true) else
if format=ifDDS then LoadDDS(srcData,img,true) else begin
ForceLogMessage('Image format not supported for async load: '+fname);
Setlength(srcData,0);
status:=lqsError;
continue;
end;
LogMessage('Preloaded: '+fname+', time='+IntToStr(MyTickCount-t));
Setlength(srcData,0);
status:=lqsReady;
timeUnpacked:=MyTickCount;
maxUnpackTime:=max2(maxUnpackTime,timeUnpacked-timeLoaded);
sleep(0);
except
on e:exception do begin
ForceLogMessage('Error unpacking '+fname+': '+ExceptionMsg(e));
Setlength(srcData,0);
status:=lqsError;
end;
end;
end;
until terminated;
except
on e:Exception do ErrorMessage('Error in UnpackingThread: '+ExceptionMsg(e));
end;
UnregisterThread;
end;
procedure TerminateThreads;
var
i:integer;
begin
ForceLogMessage('Terminating ILQ threads');
if (loadingThread<>nil) and not loadingThread.Terminated then begin
loadingThread.Terminate;
loadingThread.WaitFor;
end;
for i:=1 to high(unpackThreads) do
if (unpackThreads[i]<>nil) and not unpackThreads[i].Terminated then begin
unpackThreads[i].Terminate;
unpackThreads[i].WaitFor;
end;
LogMessage('ILQ threads terminated. Max unpack time was '+inttostr(maxUnpackTime));
end;
initialization
InitCritSect(cSect,'ImgloadQueue');
finalization
TerminateThreads;
DeleteCritSect(cSect);
end.