example: just renamed the svg file.
[platform/core/graphics/tizenvg.git] / src / lib / tvgInitializer.cpp
1 /*
2  * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. 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 #include "tvgCommon.h"
23 #include "tvgTaskScheduler.h"
24 #include "tvgLoader.h"
25
26 #ifdef _WIN32
27     #include <cstring>
28 #endif
29
30 #ifdef THORVG_SW_RASTER_SUPPORT
31     #include "tvgSwRenderer.h"
32 #endif
33
34 #ifdef THORVG_GL_RASTER_SUPPORT
35     #include "tvgGlRenderer.h"
36 #endif
37
38
39 /************************************************************************/
40 /* Internal Class Implementation                                        */
41 /************************************************************************/
42
43 static int _initCnt = 0;
44 static uint16_t _version = 0;
45
46
47 static bool _buildVersionInfo()
48 {
49     auto SRC = THORVG_VERSION_STRING;   //ex) 0.3.99
50     auto p = SRC;
51     const char* x;
52
53     char major[3];
54     x = strchr(p, '.');
55     if (!x) return false;
56     memcpy(major, p, x - p);
57     major[x - p] = '\0';
58     p = x + 1;
59
60     char minor[3];
61     x = strchr(p, '.');
62     if (!x) return false;
63     memcpy(minor, p, x - p);
64     minor[x - p] = '\0';
65     p = x + 1;
66
67     char micro[3];
68     x = SRC + strlen(THORVG_VERSION_STRING);
69     memcpy(micro, p, x - p);
70     micro[x - p] = '\0';
71
72     char sum[7];
73     snprintf(sum, sizeof(sum), "%s%s%s", major, minor, micro);
74
75     _version = atoi(sum);
76
77     return true;
78 }
79
80
81 /************************************************************************/
82 /* External Class Implementation                                        */
83 /************************************************************************/
84
85 Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept
86 {
87     auto nonSupport = true;
88
89     if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
90         #ifdef THORVG_SW_RASTER_SUPPORT
91             if (!SwRenderer::init(threads)) return Result::FailedAllocation;
92             nonSupport = false;
93         #endif
94     } else if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Gl)) {
95         #ifdef THORVG_GL_RASTER_SUPPORT
96             if (!GlRenderer::init(threads)) return Result::FailedAllocation;
97             nonSupport = false;
98         #endif
99     } else {
100         return Result::InvalidArguments;
101     }
102
103     if (nonSupport) return Result::NonSupport;
104
105     if (_initCnt++ > 0) return Result::Success;
106
107     if (!_buildVersionInfo()) return Result::Unknown;
108
109     if (!LoaderMgr::init()) return Result::Unknown;
110
111     TaskScheduler::init(threads);
112
113     return Result::Success;
114 }
115
116
117 Result Initializer::term(CanvasEngine engine) noexcept
118 {
119     if (_initCnt == 0) return Result::InsufficientCondition;
120
121     auto nonSupport = true;
122
123     if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Sw)) {
124         #ifdef THORVG_SW_RASTER_SUPPORT
125             if (!SwRenderer::term()) return Result::InsufficientCondition;
126             nonSupport = false;
127         #endif
128     } else if (static_cast<uint32_t>(engine) & static_cast<uint32_t>(CanvasEngine::Gl)) {
129         #ifdef THORVG_GL_RASTER_SUPPORT
130             if (!GlRenderer::term()) return Result::InsufficientCondition;
131             nonSupport = false;
132         #endif
133     } else {
134         return Result::InvalidArguments;
135     }
136
137     if (nonSupport) return Result::NonSupport;
138
139     if (--_initCnt > 0) return Result::Success;
140
141     TaskScheduler::term();
142
143     if (!LoaderMgr::term()) return Result::Unknown;
144
145     return Result::Success;
146 }
147
148
149 uint16_t THORVG_VERSION_NUMBER()
150 {
151     return _version;
152 }