Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgCanvas.h

raw symbols SCIP
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 #ifndef _TVG_CANVAS_H_
24 #define _TVG_CANVAS_H_
25
26 #include "tvgPaint.h"
27
28
29 enum Status : uint8_t {Synced = 0, Updating, Drawing, Damaged};
30
31 struct Canvas::Impl
32 {
33 list<Paint*> paints;
34 RenderMethod* renderer;
35 RenderRegion vport = {0, 0, INT32_MAX, INT32_MAX};
36 Status status = Status::Synced;
37
38 Impl(RenderMethod* pRenderer) : renderer(pRenderer)
39 {
40 renderer->ref();
41 }
42
43 ~Impl()
44 {
45 //make it sure any deferred jobs
46 renderer->sync();
47 renderer->clear();
48
49 clearPaints();
50
51 if (renderer->unref() == 0) delete(renderer);
52 }
53
54 void clearPaints()
55 {
56 for (auto paint : paints) {
57 if (P(paint)->unref() == 0) delete(paint);
58 }
59 paints.clear();
60 }
61
62 Result push(unique_ptr<Paint> paint)
63 {
64 //You cannot push paints during rendering.
65 if (status == Status::Drawing) return Result::InsufficientCondition;
66
67 auto p = paint.release();
68 if (!p) return Result::MemoryCorruption;
69 PP(p)->ref();
70 paints.push_back(p);
71
72 return update(p, true);
73 }
74
75 Result clear(bool free)
76 {
77 //Clear render target before drawing
78 if (!renderer->clear()) return Result::InsufficientCondition;
79
80 //Free paints
81 if (free) clearPaints();
82
83 status = Status::Synced;
84
85 return Result::Success;
86 }
87
88 Result update(Paint* paint, bool force)
89 {
90 if (paints.empty() || status == Status::Drawing) return Result::InsufficientCondition;
91
92 Array<RenderData> clips;
93 auto flag = RenderUpdateFlag::None;
94 if (status == Status::Damaged || force) flag = RenderUpdateFlag::All;
95
96 auto m = Matrix{1, 0, 0, 0, 1, 0, 0, 0, 1};
97
98 if (paint) {
99 paint->pImpl->update(renderer, m, clips, 255, flag);
100 } else {
101 for (auto paint : paints) {
102 paint->pImpl->update(renderer, m, clips, 255, flag);
103 }
104 }
105 status = Status::Updating;
106 return Result::Success;
107 }
108
109 Result draw()
110 {
111 if (status == Status::Damaged) update(nullptr, false);
112 if (status == Status::Drawing || paints.empty() || !renderer->preRender()) return Result::InsufficientCondition;
113
114 bool rendered = false;
115 for (auto paint : paints) {
116 if (paint->pImpl->render(renderer)) rendered = true;
117 }
118
119 if (!rendered || !renderer->postRender()) return Result::InsufficientCondition;
120
121 status = Status::Drawing;
122 return Result::Success;
123 }
124
125 Result sync()
126 {
127 if (status == Status::Synced || status == Status::Damaged) return Result::InsufficientCondition;
128
129 if (renderer->sync()) {
130 status = Status::Synced;
131 return Result::Success;
132 }
133
134 return Result::Unknown;
135 }
136
137 Result viewport(int32_t x, int32_t y, int32_t w, int32_t h)
138 {
139 if (status != Status::Damaged && status != Status::Synced) return Result::InsufficientCondition;
140
141 RenderRegion val = {x, y, w, h};
142 //intersect if the target buffer is already set.
143 auto surface = renderer->mainSurface();
144 if (surface && surface->w > 0 && surface->h > 0) {
145 val.intersect({0, 0, (int32_t)surface->w, (int32_t)surface->h});
146 }
147 if (vport == val) return Result::Success;
148 renderer->viewport(val);
149 vport = val;
150 status = Status::Damaged;
151 return Result::Success;
152 }
153 };
154
155 #endif /* _TVG_CANVAS_H_ */