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