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 "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25
26 #include "tvgCanvas.h"
27 #include "tvgLoadModule.h"
28
29 #ifdef THORVG_SW_RASTER_SUPPORT
30 #include "tvgSwRenderer.h"
31 #else
32 class SwRenderer : public RenderMethod
33 {
34 //Non Supported. Dummy Class */
35 };
36 #endif
37
38 /************************************************************************/
39 /* Internal Class Implementation */
40 /************************************************************************/
41
42 struct SwCanvas::Impl
43 {
44 };
45
46
47 /************************************************************************/
48 /* External Class Implementation */
49 /************************************************************************/
50
51 #ifdef THORVG_SW_RASTER_SUPPORT
SwCanvas()52 SwCanvas::SwCanvas() : Canvas(SwRenderer::gen()), pImpl(nullptr)
53 #else
54 SwCanvas::SwCanvas() : Canvas(nullptr), pImpl(nullptr)
55 #endif
56 {
57 }
58
59
~SwCanvas()60 SwCanvas::~SwCanvas()
61 {
62 delete(pImpl);
63 }
64
65
mempool(MempoolPolicy policy)66 Result SwCanvas::mempool(MempoolPolicy policy) noexcept
67 {
68 #ifdef THORVG_SW_RASTER_SUPPORT
69 //We know renderer type, avoid dynamic_cast for performance.
70 auto renderer = static_cast<SwRenderer*>(Canvas::pImpl->renderer);
71 if (!renderer) return Result::MemoryCorruption;
72
73 //It can't change the policy during the running.
74 if (!Canvas::pImpl->paints.empty()) return Result::InsufficientCondition;
75
76 if (policy == MempoolPolicy::Individual) renderer->mempool(false);
77 else renderer->mempool(true);
78
79 return Result::Success;
80 #endif
81 return Result::NonSupport;
82 }
83
84
target(uint32_t * buffer,uint32_t stride,uint32_t w,uint32_t h,Colorspace cs)85 Result SwCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, Colorspace cs) noexcept
86 {
87 #ifdef THORVG_SW_RASTER_SUPPORT
88 if (Canvas::pImpl->status != Status::Damaged && Canvas::pImpl->status != Status::Synced) {
89 return Result::InsufficientCondition;
90 }
91
92 //We know renderer type, avoid dynamic_cast for performance.
93 auto renderer = static_cast<SwRenderer*>(Canvas::pImpl->renderer);
94 if (!renderer) return Result::MemoryCorruption;
95
96 if (!renderer->target(buffer, stride, w, h, static_cast<ColorSpace>(cs))) return Result::InvalidArguments;
97 Canvas::pImpl->vport = {0, 0, (int32_t)w, (int32_t)h};
98 renderer->viewport(Canvas::pImpl->vport);
99
100 //FIXME: The value must be associated with an individual canvas instance.
101 ImageLoader::cs = static_cast<ColorSpace>(cs);
102
103 //Paints must be updated again with this new target.
104 Canvas::pImpl->status = Status::Damaged;
105
106 return Result::Success;
107 #endif
108 return Result::NonSupport;
109 }
110
111
gen()112 unique_ptr<SwCanvas> SwCanvas::gen() noexcept
113 {
114 #ifdef THORVG_SW_RASTER_SUPPORT
115 if (SwRenderer::init() <= 0) return nullptr;
116 return unique_ptr<SwCanvas>(new SwCanvas);
117 #endif
118 return nullptr;
119 }
120
121 #endif /* LV_USE_THORVG_INTERNAL */
122
123