sw_engine: concrete shape rendering sequence.
[platform/core/graphics/tizenvg.git] / src / lib / tvgShapeNode.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 #ifndef _TVG_SHAPE_NODE_CPP_
18 #define _TVG_SHAPE_NODE_CPP_
19
20 #include "tvgCommon.h"
21 #include "tvgShapePath.h"
22
23 /************************************************************************/
24 /* Internal Class Implementation                                        */
25 /************************************************************************/
26
27 struct ShapeFill
28 {
29 };
30
31
32 struct ShapeStroke
33 {
34 };
35
36
37 struct ShapeTransform
38 {
39     float e[4*4];
40 };
41
42
43 struct ShapeNode::Impl
44 {
45     ShapeTransform *transform = nullptr;
46     ShapeFill *fill = nullptr;
47     ShapeStroke *stroke = nullptr;
48     ShapePath *path = nullptr;
49     uint8_t color[4] = {0, 0, 0, 0};    //r, g, b, a
50     void *edata = nullptr;              //engine data
51
52     Impl() : path(new ShapePath)
53     {
54     }
55
56     ~Impl()
57     {
58         if (path) delete(path);
59         if (stroke) delete(stroke);
60         if (fill) delete(fill);
61         if (transform) delete(transform);
62     }
63 };
64
65
66 /************************************************************************/
67 /* External Class Implementation                                        */
68 /************************************************************************/
69
70 ShapeNode :: ShapeNode() : pImpl(make_unique<Impl>())
71 {
72 }
73
74
75 ShapeNode :: ~ShapeNode()
76 {
77     cout << "ShapeNode(" << this << ") destroyed!" << endl;
78 }
79
80
81 unique_ptr<ShapeNode> ShapeNode::gen() noexcept
82 {
83     return unique_ptr<ShapeNode>(new ShapeNode);
84 }
85
86
87 void* ShapeNode::engine() noexcept
88 {
89     auto impl = pImpl.get();
90     assert(impl);
91     return impl->edata;
92 }
93
94
95 int ShapeNode::update(RenderMethod* engine) noexcept
96 {
97     auto impl = pImpl.get();
98     assert(impl);
99
100     impl->edata = engine->prepare(*this, impl->edata, RenderMethod::UpdateFlag::All);
101     if (impl->edata) return 0;
102     return - 1;
103 }
104
105
106 int ShapeNode::clear() noexcept
107 {
108     auto impl = pImpl.get();
109     assert(impl);
110
111     return impl->path->clear();
112 }
113
114
115 int ShapeNode::pathCommands(const PathCommand** cmds) const noexcept
116 {
117     auto impl = pImpl.get();
118     assert(impl && cmds);
119
120     *cmds = impl->path->cmds;
121
122     return impl->path->cmdCnt;
123 }
124
125
126 int ShapeNode::pathCoords(const Point** pts) const noexcept
127 {
128     auto impl = pImpl.get();
129     assert(impl && pts);
130
131     *pts = impl->path->pts;
132
133     return impl->path->ptsCnt;
134 }
135
136
137 int ShapeNode::appendCircle(float cx, float cy, float radius) noexcept
138 {
139     return 0;
140 }
141
142
143 int ShapeNode::appendRect(float x, float y, float w, float h, float radius) noexcept
144 {
145     auto impl = pImpl.get();
146     assert(impl);
147
148     //clamping radius by minimum size
149     auto min = (w < h ? w : h) * 0.5f;
150     if (radius > min) radius = min;
151
152     //rectangle
153     if (radius == 0) {
154         impl->path->reserve(5, 4);
155         impl->path->moveTo(x, y);
156         impl->path->lineTo(x + w, y);
157         impl->path->lineTo(x + w, y + h);
158         impl->path->lineTo(x, y + h);
159         impl->path->close();
160     //circle
161     } else if (w == h && radius * 2 == w) {
162         appendCircle(x + (w * 0.5f), y + (h * 0.5f), radius);
163     } else {
164         //...
165     }
166
167     return 0;
168 }
169
170
171 int ShapeNode::fill(size_t r, size_t g, size_t b, size_t a) noexcept
172 {
173     auto impl = pImpl.get();
174     assert(impl);
175
176     impl->color[0] = r;
177     impl->color[1] = g;
178     impl->color[2] = b;
179     impl->color[3] = a;
180
181     return 0;
182 }
183
184
185 int ShapeNode::fill(size_t* r, size_t* g, size_t* b, size_t* a) const noexcept
186 {
187     auto impl = pImpl.get();
188     assert(impl);
189
190     if (r) *r = impl->color[0];
191     if (g) *g = impl->color[1];
192     if (b) *b = impl->color[2];
193     if (a) *a = impl->color[3];
194
195     return 0;
196 }
197
198 #endif //_TVG_SHAPE_NODE_CPP_