Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgPaint.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_PAINT_H_
24 #define _TVG_PAINT_H_
25
26 #include "tvgRender.h"
27 #include "tvgMath.h"
28
29 namespace tvg
30 {
31 enum ContextFlag : uint8_t {Default = 0, FastTrack = 1};
32
33 struct Iterator
34 {
35 virtual ~Iterator() {}
36 virtual const Paint* next() = 0;
37 virtual uint32_t count() = 0;
38 virtual void begin() = 0;
39 };
40
41 struct Composite
42 {
43 Paint* target;
44 Paint* source;
45 CompositeMethod method;
46 };
47
48 struct Paint::Impl
49 {
50 Paint* paint = nullptr;
51 Composite* compData = nullptr;
52 Paint* clipper = nullptr;
53 RenderMethod* renderer = nullptr;
54 struct {
55 Matrix m; //input matrix
56 Matrix cm; //multipled parents matrix
57 float degree; //rotation degree
58 float scale; //scale factor
59 bool overriding; //user transform?
60
61 void update()
62 {
63 if (overriding) return;
64 m.e11 = 1.0f;
65 m.e12 = 0.0f;
66 m.e21 = 0.0f;
67 m.e22 = 1.0f;
68 m.e31 = 0.0f;
69 m.e32 = 0.0f;
70 m.e33 = 1.0f;
71 tvg::scale(&m, scale, scale);
72 tvg::rotate(&m, degree);
73 }
74 } tr;
75 RenderUpdateFlag renderFlag = RenderUpdateFlag::None;
76 BlendMethod blendMethod;
77 uint16_t refCnt = 0; //reference count
78 uint8_t ctxFlag;
79 uint8_t opacity;
80
81 Impl(Paint* pnt) : paint(pnt)
82 {
83 reset();
84 }
85
86 ~Impl()
87 {
88 if (compData) {
89 if (P(compData->target)->unref() == 0) delete(compData->target);
90 free(compData);
91 }
92 if (clipper && P(clipper)->unref() == 0) delete(clipper);
93 if (renderer && (renderer->unref() == 0)) delete(renderer);
94 }
95
96 uint8_t ref()
97 {
98 return ++refCnt;
99 }
100
101 uint8_t unref()
102 {
103 if (refCnt == 0) TVGERR("RENDERER", "Corrupted reference count!");
104 return --refCnt;
105 }
106
107 bool transform(const Matrix& m)
108 {
109 if (&tr.m != &m) tr.m = m;
110 tr.overriding = true;
111 renderFlag |= RenderUpdateFlag::Transform;
112
113 return true;
114 }
115
116 Matrix& transform(bool origin = false)
117 {
118 //update transform
119 if (renderFlag & RenderUpdateFlag::Transform) tr.update();
120 if (origin) return tr.cm;
121 return tr.m;
122 }
123
124 void clip(Paint* clp)
125 {
126 if (this->clipper) {
127 P(this->clipper)->unref();
128 if (this->clipper != clp && P(this->clipper)->refCnt == 0) {
129 delete(this->clipper);
130 }
131 }
132 this->clipper = clp;
133 if (!clp) return;
134
135 P(clipper)->ref();
136 }
137
138 bool composite(Paint* source, Paint* target, CompositeMethod method)
139 {
140 //Invalid case
141 if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false;
142
143 if (compData) {
144 P(compData->target)->unref();
145 if ((compData->target != target) && P(compData->target)->refCnt == 0) {
146 delete(compData->target);
147 }
148 //Reset scenario
149 if (!target && method == CompositeMethod::None) {
150 free(compData);
151 compData = nullptr;
152 return true;
153 }
154 } else {
155 if (!target && method == CompositeMethod::None) return true;
156 compData = static_cast<Composite*>(calloc(1, sizeof(Composite)));
157 }
158 P(target)->ref();
159 compData->target = target;
160 compData->source = source;
161 compData->method = method;
162 return true;
163 }
164
165 RenderRegion bounds(RenderMethod* renderer) const;
166 Iterator* iterator();
167 bool rotate(float degree);
168 bool scale(float factor);
169 bool translate(float x, float y);
170 bool bounds(float* x, float* y, float* w, float* h, bool transformed, bool stroking, bool origin = false);
171 RenderData update(RenderMethod* renderer, const Matrix& pm, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper = false);
172 bool render(RenderMethod* renderer);
173 Paint* duplicate(Paint* ret = nullptr);
174 void reset();
175 };
176 }
177
178 #endif //_TVG_PAINT_H_