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 "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #include "tvgCommon.h"
27 #include "tvgTaskScheduler.h"
28 #include "tvgLoader.h"
29 
30 #ifdef _WIN32
31     #include <cstring>
32 #endif
33 
34 #ifdef THORVG_SW_RASTER_SUPPORT
35     #include "tvgSwRenderer.h"
36 #endif
37 
38 #ifdef THORVG_GL_RASTER_SUPPORT
39     #include "tvgGlRenderer.h"
40 #endif
41 
42 #ifdef THORVG_WG_RASTER_SUPPORT
43     #include "tvgWgRenderer.h"
44 #endif
45 
46 
47 /************************************************************************/
48 /* Internal Class Implementation                                        */
49 /************************************************************************/
50 
51 static int _initCnt = 0;
52 static uint16_t _version = 0;
53 
54 //enum class operation helper
operator &(CanvasEngine a,CanvasEngine b)55 static constexpr bool operator &(CanvasEngine a, CanvasEngine b)
56 {
57     return int(a) & int(b);
58 }
59 
_buildVersionInfo(uint32_t * major,uint32_t * minor,uint32_t * micro)60 static bool _buildVersionInfo(uint32_t* major, uint32_t* minor, uint32_t* micro)
61 {
62     auto VER = THORVG_VERSION_STRING;
63     auto p = VER;
64     const char* x;
65 
66     if (!(x = strchr(p, '.'))) return false;
67     uint32_t majorVal = atoi(p);
68     p = x + 1;
69 
70     if (!(x = strchr(p, '.'))) return false;
71     uint32_t minorVal = atoi(p);
72     p = x + 1;
73 
74     uint32_t microVal = atoi(p);
75 
76     char sum[7];
77     snprintf(sum, sizeof(sum), "%d%02d%02d", majorVal, minorVal, microVal);
78     _version = atoi(sum);
79 
80     if (major) *major = majorVal;
81     if (minor) *minor = minorVal;
82     if (micro) *micro = microVal;
83 
84     return true;
85 }
86 
87 
88 /************************************************************************/
89 /* External Class Implementation                                        */
90 /************************************************************************/
91 
init(CanvasEngine engine,uint32_t threads)92 Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
93 {
94     auto nonSupport = true;
95     if (static_cast<int>(engine) == 0) return Result::InvalidArguments;
96 
97     if (engine & CanvasEngine::Sw) {
98         #ifdef THORVG_SW_RASTER_SUPPORT
99             if (!SwRenderer::init(threads)) return Result::FailedAllocation;
100             nonSupport = false;
101         #endif
102     }
103 
104     if (engine & CanvasEngine::Gl) {
105         #ifdef THORVG_GL_RASTER_SUPPORT
106             if (!GlRenderer::init(threads)) return Result::FailedAllocation;
107             nonSupport = false;
108         #endif
109     }
110 
111     if (engine & CanvasEngine::Wg) {
112         #ifdef THORVG_WG_RASTER_SUPPORT
113             if (!WgRenderer::init(threads)) return Result::FailedAllocation;
114             nonSupport = false;
115         #endif
116     }
117 
118     if (nonSupport) return Result::NonSupport;
119 
120     if (_initCnt++ > 0) return Result::Success;
121 
122     if (!_buildVersionInfo(nullptr, nullptr, nullptr)) return Result::Unknown;
123 
124     if (!LoaderMgr::init()) return Result::Unknown;
125 
126     TaskScheduler::init(threads);
127 
128     return Result::Success;
129 }
130 
131 
term(CanvasEngine engine)132 Result Initializer::term(CanvasEngine engine) noexcept
133 {
134     if (_initCnt == 0) return Result::InsufficientCondition;
135 
136     auto nonSupport = true;
137     if (static_cast<int>(engine) == 0) return Result::InvalidArguments;
138 
139     if (engine & CanvasEngine::Sw) {
140         #ifdef THORVG_SW_RASTER_SUPPORT
141             if (!SwRenderer::term()) return Result::InsufficientCondition;
142             nonSupport = false;
143         #endif
144     }
145 
146     if (engine & CanvasEngine::Gl) {
147         #ifdef THORVG_GL_RASTER_SUPPORT
148             if (!GlRenderer::term()) return Result::InsufficientCondition;
149             nonSupport = false;
150         #endif
151     }
152 
153     if (engine & CanvasEngine::Wg) {
154         #ifdef THORVG_WG_RASTER_SUPPORT
155             if (!WgRenderer::term()) return Result::InsufficientCondition;
156             nonSupport = false;
157         #endif
158     }
159 
160     if (nonSupport) return Result::NonSupport;
161 
162     if (--_initCnt > 0) return Result::Success;
163 
164     TaskScheduler::term();
165 
166     if (!LoaderMgr::term()) return Result::Unknown;
167 
168     return Result::Success;
169 }
170 
171 
version(uint32_t * major,uint32_t * minor,uint32_t * micro)172 const char* Initializer::version(uint32_t* major, uint32_t* minor, uint32_t* micro) noexcept
173 {
174     if ((!major && ! minor && !micro) || _buildVersionInfo(major, minor, micro)) return THORVG_VERSION_STRING;
175     return nullptr;
176 }
177 
178 
THORVG_VERSION_NUMBER()179 uint16_t THORVG_VERSION_NUMBER()
180 {
181     return _version;
182 }
183 
184 #endif /* LV_USE_THORVG_INTERNAL */
185 
186