Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgLoader.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <string.h>
24
25 #include <atomic>
26 #include "tvgInlist.h"
27 #include "tvgLoader.h"
28 #include "tvgLock.h"
29
30 #ifdef THORVG_SVG_LOADER_SUPPORT
31 #include "tvgSvgLoader.h"
32 #endif
33
34 #ifdef THORVG_PNG_LOADER_SUPPORT
35 #include "tvgPngLoader.h"
36 #endif
37
38 #ifdef THORVG_TVG_LOADER_SUPPORT
39 #include "tvgTvgLoader.h"
40 #endif
41
42 #ifdef THORVG_JPG_LOADER_SUPPORT
43 #include "tvgJpgLoader.h"
44 #endif
45
46 #ifdef THORVG_WEBP_LOADER_SUPPORT
47 #include "tvgWebpLoader.h"
48 #endif
49
50 #ifdef THORVG_TTF_LOADER_SUPPORT
51 #include "tvgTtfLoader.h"
52 #endif
53
54 #ifdef THORVG_LOTTIE_LOADER_SUPPORT
55 #include "tvgLottieLoader.h"
56 #endif
57
58 #include "tvgRawLoader.h"
59
60
61 uintptr_t HASH_KEY(const char* data)
62 {
63 return reinterpret_cast<uintptr_t>(data);
64 }
65
66 /************************************************************************/
67 /* Internal Class Implementation */
68 /************************************************************************/
69
70 //TODO: remove it.
71 atomic<ColorSpace> ImageLoader::cs{ColorSpace::ARGB8888};
72
73 static Key _key;
74 static Inlist<LoadModule> _activeLoaders;
75
76
77 static LoadModule* _find(FileType type)
78 {
79 switch(type) {
80 case FileType::Png: {
81 #ifdef THORVG_PNG_LOADER_SUPPORT
82 return new PngLoader;
83 #endif
84 break;
85 }
86 case FileType::Jpg: {
87 #ifdef THORVG_JPG_LOADER_SUPPORT
88 return new JpgLoader;
89 #endif
90 break;
91 }
92 case FileType::Webp: {
93 #ifdef THORVG_WEBP_LOADER_SUPPORT
94 return new WebpLoader;
95 #endif
96 break;
97 }
98 case FileType::Tvg: {
99 #ifdef THORVG_TVG_LOADER_SUPPORT
100 return new TvgLoader;
101 #endif
102 break;
103 }
104 case FileType::Svg: {
105 #ifdef THORVG_SVG_LOADER_SUPPORT
106 return new SvgLoader;
107 #endif
108 break;
109 }
110 case FileType::Ttf: {
111 #ifdef THORVG_TTF_LOADER_SUPPORT
112 return new TtfLoader;
113 #endif
114 break;
115 }
116 case FileType::Lottie: {
117 #ifdef THORVG_LOTTIE_LOADER_SUPPORT
118 return new LottieLoader;
119 #endif
120 break;
121 }
122 case FileType::Raw: {
123 return new RawLoader;
124 break;
125 }
126 default: {
127 break;
128 }
129 }
130
131 #ifdef THORVG_LOG_ENABLED
132 const char *format;
133 switch(type) {
134 case FileType::Tvg: {
135 format = "TVG";
136 break;
137 }
138 case FileType::Svg: {
139 format = "SVG";
140 break;
141 }
142 case FileType::Ttf: {
143 format = "TTF";
144 break;
145 }
146 case FileType::Lottie: {
147 format = "lottie(json)";
148 break;
149 }
150 case FileType::Raw: {
151 format = "RAW";
152 break;
153 }
154 case FileType::Png: {
155 format = "PNG";
156 break;
157 }
158 case FileType::Jpg: {
159 format = "JPG";
160 break;
161 }
162 case FileType::Webp: {
163 format = "WEBP";
164 break;
165 }
166 default: {
167 format = "???";
168 break;
169 }
170 }
171 TVGLOG("RENDERER", "%s format is not supported", format);
172 #endif
173 return nullptr;
174 }
175
176
177 #ifdef THORVG_FILE_IO_SUPPORT
178 static LoadModule* _findByPath(const string& path)
179 {
180 auto ext = path.substr(path.find_last_of(".") + 1);
181 if (!ext.compare("tvg")) return _find(FileType::Tvg);
182 if (!ext.compare("svg")) return _find(FileType::Svg);
183 if (!ext.compare("json")) return _find(FileType::Lottie);
184 if (!ext.compare("png")) return _find(FileType::Png);
185 if (!ext.compare("jpg")) return _find(FileType::Jpg);
186 if (!ext.compare("webp")) return _find(FileType::Webp);
187 if (!ext.compare("ttf") || !ext.compare("ttc")) return _find(FileType::Ttf);
188 if (!ext.compare("otf") || !ext.compare("otc")) return _find(FileType::Ttf);
189 return nullptr;
190 }
191 #endif
192
193
194 static FileType _convert(const string& mimeType)
195 {
196 auto type = FileType::Unknown;
197
198 if (mimeType == "tvg") type = FileType::Tvg;
199 else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg;
200 else if (mimeType == "ttf" || mimeType == "otf") type = FileType::Ttf;
201 else if (mimeType == "lottie") type = FileType::Lottie;
202 else if (mimeType == "raw") type = FileType::Raw;
203 else if (mimeType == "png") type = FileType::Png;
204 else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg;
205 else if (mimeType == "webp") type = FileType::Webp;
206 else TVGLOG("RENDERER", "Given mimetype is unknown = \"%s\".", mimeType.c_str());
207
208 return type;
209 }
210
211
212 static LoadModule* _findByType(const string& mimeType)
213 {
214 return _find(_convert(mimeType));
215 }
216
217
218 static LoadModule* _findFromCache(const string& path)
219 {
220 ScopedLock lock(_key);
221
222 auto loader = _activeLoaders.head;
223
224 while (loader) {
225 if (loader->pathcache && !strcmp(loader->hashpath, path.c_str())) {
226 ++loader->sharing;
227 return loader;
228 }
229 loader = loader->next;
230 }
231 return nullptr;
232 }
233
234
235 static LoadModule* _findFromCache(const char* data, uint32_t size, const string& mimeType)
236 {
237 auto type = _convert(mimeType);
238 if (type == FileType::Unknown) return nullptr;
239
240 auto key = HASH_KEY(data);
241 ScopedLock lock(_key);
242
243 auto loader = _activeLoaders.head;
244 while (loader) {
245 if (loader->type == type && loader->hashkey == key) {
246 ++loader->sharing;
247 return loader;
248 }
249 loader = loader->next;
250 }
251 return nullptr;
252 }
253
254
255 /************************************************************************/
256 /* External Class Implementation */
257 /************************************************************************/
258
259
260 bool LoaderMgr::init()
261 {
262 return true;
263 }
264
265
266 bool LoaderMgr::term()
267 {
268 auto loader = _activeLoaders.head;
269
270 //clean up the remained font loaders which is globally used.
271 while (loader) {
272 if (loader->type != FileType::Ttf) {
273 loader = loader->next;
274 continue;
275 }
276 auto ret = loader->close();
277 auto tmp = loader;
278 loader = loader->next;
279 _activeLoaders.remove(tmp);
280 if (ret) delete(tmp);
281 }
282 return true;
283 }
284
285
286 bool LoaderMgr::retrieve(LoadModule* loader)
287 {
288 if (!loader) return false;
289
290 if (loader->close()) {
291 if (loader->cached()) {
292 _activeLoaders.remove(loader);
293 }
294 delete(loader);
295 }
296 return true;
297 }
298
299
300 LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
301 {
302 #ifdef THORVG_FILE_IO_SUPPORT
303 *invalid = false;
304
305 //TODO: svg & lottie is not sharable.
306 auto allowCache = true;
307 auto ext = path.substr(path.find_last_of(".") + 1);
308 if (!ext.compare("svg") || !ext.compare("json")) allowCache = false;
309
310 if (allowCache) {
311 if (auto loader = _findFromCache(path)) return loader;
312 }
313
314 if (auto loader = _findByPath(path)) {
315 if (loader->open(path)) {
316 if (allowCache) {
317 loader->hashpath = strdup(path.c_str());
318 loader->pathcache = true;
319 {
320 ScopedLock lock(_key);
321 _activeLoaders.back(loader);
322 }
323 }
324 return loader;
325 }
326 delete(loader);
327 }
328 //Unknown MimeType. Try with the candidates in the order
329 for (int i = 0; i < static_cast<int>(FileType::Raw); i++) {
330 if (auto loader = _find(static_cast<FileType>(i))) {
331 if (loader->open(path)) {
332 if (allowCache) {
333 loader->hashpath = strdup(path.c_str());
334 loader->pathcache = true;
335 {
336 ScopedLock lock(_key);
337 _activeLoaders.back(loader);
338 }
339 }
340 return loader;
341 }
342 delete(loader);
343 }
344 }
345 *invalid = true;
346 #endif
347 return nullptr;
348 }
349
350
351 bool LoaderMgr::retrieve(const string& path)
352 {
353 return retrieve(_findFromCache(path));
354 }
355
356
357 LoadModule* LoaderMgr::loader(const char* key)
358 {
359 auto loader = _activeLoaders.head;
360
361 while (loader) {
362 if (loader->pathcache && strstr(loader->hashpath, key)) {
363 ++loader->sharing;
364 return loader;
365 }
366 loader = loader->next;
367 }
368 return nullptr;
369 }
370
371
372 LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy)
373 {
374 //Note that users could use the same data pointer with the different content.
375 //Thus caching is only valid for shareable.
376 auto allowCache = !copy;
377
378 //TODO: lottie is not sharable.
379 if (allowCache) {
380 auto type = _convert(mimeType);
381 if (type == FileType::Lottie) allowCache = false;
382 }
383
384 if (allowCache) {
385 if (auto loader = _findFromCache(data, size, mimeType)) return loader;
386 }
387
388 //Try with the given MimeType
389 if (!mimeType.empty()) {
390 if (auto loader = _findByType(mimeType)) {
391 if (loader->open(data, size, copy)) {
392 if (allowCache) {
393 loader->hashkey = HASH_KEY(data);
394 ScopedLock lock(_key);
395 _activeLoaders.back(loader);
396 }
397 return loader;
398 } else {
399 TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported.", mimeType.c_str());
400 delete(loader);
401 }
402 }
403 }
404 //Unknown MimeType. Try with the candidates in the order
405 for (int i = 0; i < static_cast<int>(FileType::Raw); i++) {
406 auto loader = _find(static_cast<FileType>(i));
407 if (loader) {
408 if (loader->open(data, size, copy)) {
409 if (allowCache) {
410 loader->hashkey = HASH_KEY(data);
411 ScopedLock lock(_key);
412 _activeLoaders.back(loader);
413 }
414 return loader;
415 }
416 delete(loader);
417 }
418 }
419 return nullptr;
420 }
421
422
423 LoadModule* LoaderMgr::loader(const uint32_t *data, uint32_t w, uint32_t h, bool copy)
424 {
425 //Note that users could use the same data pointer with the different content.
426 //Thus caching is only valid for shareable.
427 if (!copy) {
428 //TODO: should we check premultiplied??
429 if (auto loader = _findFromCache((const char*)(data), w * h, "raw")) return loader;
430 }
431
432 //function is dedicated for raw images only
433 auto loader = new RawLoader;
434 if (loader->open(data, w, h, copy)) {
435 if (!copy) {
436 loader->hashkey = HASH_KEY((const char*)data);
437 ScopedLock lock(_key);
438 _activeLoaders.back(loader);
439 }
440 return loader;
441 }
442 delete(loader);
443 return nullptr;
444 }
445
446
447 //loads fonts from memory - loader is cached (regardless of copy value) in order to access it while setting font
448 LoadModule* LoaderMgr::loader(const char* name, const char* data, uint32_t size, TVG_UNUSED const string& mimeType, bool copy)
449 {
450 #ifdef THORVG_TTF_LOADER_SUPPORT
451 //TODO: add check for mimetype ?
452 if (auto loader = _findFromCache(name)) return loader;
453
454 //function is dedicated for ttf loader (the only supported font loader)
455 auto loader = new TtfLoader;
456 if (loader->open(data, size, copy)) {
457 loader->hashpath = strdup(name);
458 loader->pathcache = true;
459 ScopedLock lock(_key);
460 _activeLoaders.back(loader);
461 return loader;
462 }
463
464 TVGLOG("LOADER", "The font data \"%s\" could not be loaded.", name);
465 delete(loader);
466 #endif
467 return nullptr;
468 }