| 1 | /* |
| 2 | * Copyright (c) 2020 - 2023 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 | |
| 40 | /************************************************************************/ |
| 41 | /* Internal Class Implementation */ |
| 42 | /************************************************************************/ |
| 43 | |
| 44 | static int _initCnt = 0; |
| 45 | static uint16_t _version = 0; |
| 46 | |
| 47 | //enum class operation helper |
| 48 | static constexpr bool operator &(CanvasEngine a, CanvasEngine b) |
| 49 | { |
| 50 | return int(a) & int(b); |
| 51 | } |
| 52 | |
| 53 | static bool _buildVersionInfo() |
| 54 | { |
| 55 | auto SRC = THORVG_VERSION_STRING; //ex) 0.3.99 |
| 56 | auto p = SRC; |
| 57 | const char* x; |
| 58 | |
| 59 | char major[3]; |
| 60 | x = strchr(p, '.'); |
| 61 | if (!x) return false; |
| 62 | memcpy(major, p, x - p); |
| 63 | major[x - p] = '\0'; |
| 64 | p = x + 1; |
| 65 | |
| 66 | char minor[3]; |
| 67 | x = strchr(p, '.'); |
| 68 | if (!x) return false; |
| 69 | memcpy(minor, p, x - p); |
| 70 | minor[x - p] = '\0'; |
| 71 | p = x + 1; |
| 72 | |
| 73 | char micro[3]; |
| 74 | x = SRC + strlen(THORVG_VERSION_STRING); |
| 75 | memcpy(micro, p, x - p); |
| 76 | micro[x - p] = '\0'; |
| 77 | |
| 78 | char sum[7]; |
| 79 | snprintf(sum, sizeof(sum), "%s%s%s" , major, minor, micro); |
| 80 | |
| 81 | _version = atoi(sum); |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /************************************************************************/ |
| 88 | /* External Class Implementation */ |
| 89 | /************************************************************************/ |
| 90 | |
| 91 | Result Initializer::init(CanvasEngine engine, uint32_t threads) noexcept |
| 92 | { |
| 93 | auto nonSupport = true; |
| 94 | |
| 95 | if (engine & CanvasEngine::Sw) { |
| 96 | #ifdef THORVG_SW_RASTER_SUPPORT |
| 97 | if (!SwRenderer::init(threads)) return Result::FailedAllocation; |
| 98 | nonSupport = false; |
| 99 | #endif |
| 100 | } else if (engine & CanvasEngine::Gl) { |
| 101 | #ifdef THORVG_GL_RASTER_SUPPORT |
| 102 | if (!GlRenderer::init(threads)) return Result::FailedAllocation; |
| 103 | nonSupport = false; |
| 104 | #endif |
| 105 | } else { |
| 106 | return Result::InvalidArguments; |
| 107 | } |
| 108 | |
| 109 | if (nonSupport) return Result::NonSupport; |
| 110 | |
| 111 | if (_initCnt++ > 0) return Result::Success; |
| 112 | |
| 113 | if (!_buildVersionInfo()) return Result::Unknown; |
| 114 | |
| 115 | if (!LoaderMgr::init()) return Result::Unknown; |
| 116 | |
| 117 | TaskScheduler::init(threads); |
| 118 | |
| 119 | return Result::Success; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | Result Initializer::term(CanvasEngine engine) noexcept |
| 124 | { |
| 125 | if (_initCnt == 0) return Result::InsufficientCondition; |
| 126 | |
| 127 | auto nonSupport = true; |
| 128 | |
| 129 | if (engine & CanvasEngine::Sw) { |
| 130 | #ifdef THORVG_SW_RASTER_SUPPORT |
| 131 | if (!SwRenderer::term()) return Result::InsufficientCondition; |
| 132 | nonSupport = false; |
| 133 | #endif |
| 134 | } else if (engine & CanvasEngine::Gl) { |
| 135 | #ifdef THORVG_GL_RASTER_SUPPORT |
| 136 | if (!GlRenderer::term()) return Result::InsufficientCondition; |
| 137 | nonSupport = false; |
| 138 | #endif |
| 139 | } else { |
| 140 | return Result::InvalidArguments; |
| 141 | } |
| 142 | |
| 143 | if (nonSupport) return Result::NonSupport; |
| 144 | |
| 145 | if (--_initCnt > 0) return Result::Success; |
| 146 | |
| 147 | TaskScheduler::term(); |
| 148 | |
| 149 | if (!LoaderMgr::term()) return Result::Unknown; |
| 150 | |
| 151 | return Result::Success; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | uint16_t THORVG_VERSION_NUMBER() |
| 156 | { |
| 157 | return _version; |
| 158 | } |
| 159 | |