| 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 "tvgPaint.h" |
| 24 | #include "tvgPicture.h" |
| 25 | |
| 26 | /************************************************************************/ |
| 27 | /* Internal Class Implementation */ |
| 28 | /************************************************************************/ |
| 29 | |
| 30 | RenderUpdateFlag Picture::Impl::load() |
| 31 | { |
| 32 | if (loader) { |
| 33 | if (paint) { |
| 34 | loader->sync(); |
| 35 | } else { |
| 36 | paint = loader->paint(); |
| 37 | if (paint) { |
| 38 | if (w != loader->w || h != loader->h) { |
| 39 | if (!resizing) { |
| 40 | w = loader->w; |
| 41 | h = loader->h; |
| 42 | } |
| 43 | loader->resize(paint, w, h); |
| 44 | resizing = false; |
| 45 | } |
| 46 | return RenderUpdateFlag::None; |
| 47 | } |
| 48 | } |
| 49 | if (!surface) { |
| 50 | if ((surface = loader->bitmap())) { |
| 51 | return RenderUpdateFlag::Image; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return RenderUpdateFlag::None; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void Picture::Impl::queryComposition(uint8_t opacity) |
| 60 | { |
| 61 | cFlag = CompositionFlag::Invalid; |
| 62 | |
| 63 | //In this case, paint(scene) would try composition itself. |
| 64 | if (opacity < 255) return; |
| 65 | |
| 66 | //Composition test |
| 67 | const Paint* target; |
| 68 | auto method = picture->composite(&target); |
| 69 | if (!target || method == tvg::CompositeMethod::ClipPath) return; |
| 70 | if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) return; |
| 71 | |
| 72 | cFlag = CompositionFlag::Opacity; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | bool Picture::Impl::render(RenderMethod* renderer) |
| 77 | { |
| 78 | bool ret = false; |
| 79 | renderer->blend(PP(picture)->blendMethod); |
| 80 | |
| 81 | if (surface) return renderer->renderImage(rd); |
| 82 | else if (paint) { |
| 83 | RenderCompositor* cmp = nullptr; |
| 84 | if (cFlag) { |
| 85 | cmp = renderer->target(bounds(renderer), renderer->colorSpace(), static_cast<CompositionFlag>(cFlag)); |
| 86 | renderer->beginComposite(cmp, CompositeMethod::None, 255); |
| 87 | } |
| 88 | ret = paint->pImpl->render(renderer); |
| 89 | if (cmp) renderer->endComposite(cmp); |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bool Picture::Impl::size(float w, float h) |
| 96 | { |
| 97 | this->w = w; |
| 98 | this->h = h; |
| 99 | resizing = true; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | RenderRegion Picture::Impl::bounds(RenderMethod* renderer) |
| 105 | { |
| 106 | if (rd) return renderer->region(rd); |
| 107 | if (paint) return paint->pImpl->bounds(renderer); |
| 108 | return {0, 0, 0, 0}; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | Result Picture::Impl::load(ImageLoader* loader) |
| 113 | { |
| 114 | //Same resource has been loaded. |
| 115 | if (this->loader == loader) { |
| 116 | this->loader->sharing--; //make it sure the reference counting. |
| 117 | return Result::Success; |
| 118 | } else if (this->loader) { |
| 119 | LoaderMgr::retrieve(this->loader); |
| 120 | } |
| 121 | |
| 122 | this->loader = loader; |
| 123 | |
| 124 | if (!loader->read()) return Result::Unknown; |
| 125 | |
| 126 | this->w = loader->w; |
| 127 | this->h = loader->h; |
| 128 | |
| 129 | return Result::Success; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | /************************************************************************/ |
| 135 | /* External Class Implementation */ |
| 136 | /************************************************************************/ |
| 137 | |
| 138 | Picture::Picture() : pImpl(new Impl(this)) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | |
| 143 | Picture::~Picture() |
| 144 | { |
| 145 | delete(pImpl); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | unique_ptr<Picture> Picture::gen() noexcept |
| 150 | { |
| 151 | return unique_ptr<Picture>(new Picture); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | TVG_DEPRECATED uint32_t Picture::identifier() noexcept |
| 156 | { |
| 157 | return (uint32_t) Type::Picture; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | Type Picture::type() const noexcept |
| 162 | { |
| 163 | return Type::Picture; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | Result Picture::load(const std::string& path) noexcept |
| 168 | { |
| 169 | #ifdef THORVG_FILE_IO_SUPPORT |
| 170 | if (path.empty()) return Result::InvalidArguments; |
| 171 | |
| 172 | return pImpl->load(path); |
| 173 | #else |
| 174 | TVGLOG("RENDERER", "FILE IO is disabled!"); |
| 175 | return Result::NonSupport; |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | |
| 180 | Result Picture::load(const char* data, uint32_t size, const string& mimeType, bool copy) noexcept |
| 181 | { |
| 182 | if (!data || size <= 0) return Result::InvalidArguments; |
| 183 | |
| 184 | return pImpl->load(data, size, mimeType, copy); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | TVG_DEPRECATED Result Picture::load(const char* data, uint32_t size, bool copy) noexcept |
| 189 | { |
| 190 | return load(data, size, "", copy); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | Result Picture::load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept |
| 195 | { |
| 196 | if (!data || w <= 0 || h <= 0) return Result::InvalidArguments; |
| 197 | |
| 198 | return pImpl->load(data, w, h, copy); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | Result Picture::size(float w, float h) noexcept |
| 203 | { |
| 204 | if (pImpl->size(w, h)) return Result::Success; |
| 205 | return Result::InsufficientCondition; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | Result Picture::size(float* w, float* h) const noexcept |
| 210 | { |
| 211 | if (!pImpl->loader) return Result::InsufficientCondition; |
| 212 | if (w) *w = pImpl->w; |
| 213 | if (h) *h = pImpl->h; |
| 214 | return Result::Success; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | const Paint* Picture::paint(uint32_t id) noexcept |
| 219 | { |
| 220 | struct Value |
| 221 | { |
| 222 | uint32_t id; |
| 223 | const Paint* ret; |
| 224 | } value = {id, nullptr}; |
| 225 | |
| 226 | auto cb = [](const tvg::Paint* paint, void* data) -> bool |
| 227 | { |
| 228 | auto p = static_cast<Value*>(data); |
| 229 | if (p->id == paint->id) { |
| 230 | p->ret = paint; |
| 231 | return false; |
| 232 | } |
| 233 | return true; |
| 234 | }; |
| 235 | |
| 236 | tvg::Accessor::gen()->set(this, cb, &value); |
| 237 | return value.ret; |
| 238 | } |