| 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 <cstdarg> |
| 24 | #include "tvgScene.h" |
| 25 | |
| 26 | /************************************************************************/ |
| 27 | /* Internal Class Implementation */ |
| 28 | /************************************************************************/ |
| 29 | |
| 30 | Result Scene::Impl::resetEffects() |
| 31 | { |
| 32 | if (effects) { |
| 33 | for (auto e = effects->begin(); e < effects->end(); ++e) { |
| 34 | delete(*e); |
| 35 | } |
| 36 | delete(effects); |
| 37 | effects = nullptr; |
| 38 | } |
| 39 | return Result::Success; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /************************************************************************/ |
| 44 | /* External Class Implementation */ |
| 45 | /************************************************************************/ |
| 46 | |
| 47 | Scene::Scene() : pImpl(new Impl(this)) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | |
| 52 | Scene::~Scene() |
| 53 | { |
| 54 | delete(pImpl); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | unique_ptr<Scene> Scene::gen() noexcept |
| 59 | { |
| 60 | return unique_ptr<Scene>(new Scene); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | TVG_DEPRECATED uint32_t Scene::identifier() noexcept |
| 65 | { |
| 66 | return (uint32_t) Type::Scene; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | Type Scene::type() const noexcept |
| 71 | { |
| 72 | return Type::Scene; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | Result Scene::push(unique_ptr<Paint> paint) noexcept |
| 77 | { |
| 78 | auto p = paint.release(); |
| 79 | if (!p) return Result::MemoryCorruption; |
| 80 | P(p)->ref(); |
| 81 | |
| 82 | //Relocated the paint to the current scene space |
| 83 | P(p)->renderFlag |= RenderUpdateFlag::Transform; |
| 84 | |
| 85 | pImpl->paints.push_back(p); |
| 86 | |
| 87 | return Result::Success; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | Result Scene::reserve(TVG_UNUSED uint32_t size) noexcept |
| 92 | { |
| 93 | return Result::NonSupport; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | Result Scene::clear(bool free) noexcept |
| 98 | { |
| 99 | pImpl->clear(free); |
| 100 | |
| 101 | return Result::Success; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | list<Paint*>& Scene::paints() noexcept |
| 106 | { |
| 107 | return pImpl->paints; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | Result Scene::push(SceneEffect effect, ...) noexcept |
| 112 | { |
| 113 | if (effect == SceneEffect::ClearAll) return pImpl->resetEffects(); |
| 114 | |
| 115 | if (!pImpl->effects) pImpl->effects = new Array<RenderEffect*>; |
| 116 | |
| 117 | va_list args; |
| 118 | va_start(args, effect); |
| 119 | |
| 120 | RenderEffect* re = nullptr; |
| 121 | |
| 122 | switch (effect) { |
| 123 | case SceneEffect::GaussianBlur: { |
| 124 | re = RenderEffectGaussianBlur::gen(args); |
| 125 | break; |
| 126 | } |
| 127 | case SceneEffect::DropShadow: { |
| 128 | re = RenderEffectDropShadow::gen(args); |
| 129 | break; |
| 130 | } |
| 131 | case SceneEffect::Fill: { |
| 132 | re = RenderEffectFill::gen(args); |
| 133 | break; |
| 134 | } |
| 135 | case SceneEffect::Tint: { |
| 136 | re = RenderEffectTint::gen(args); |
| 137 | break; |
| 138 | } |
| 139 | case SceneEffect::Tritone: { |
| 140 | re = RenderEffectTritone::gen(args); |
| 141 | break; |
| 142 | } |
| 143 | default: break; |
| 144 | } |
| 145 | |
| 146 | if (!re) return Result::InvalidArguments; |
| 147 | |
| 148 | pImpl->effects->push(re); |
| 149 | |
| 150 | return Result::Success; |
| 151 | } |