common paint: code refactoring
[platform/core/graphics/tizenvg.git] / src / lib / tvgPaint.h
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. 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 #ifndef _TVG_PAINT_H_
23 #define _TVG_PAINT_H_
24
25 #include "tvgRender.h"
26
27
28 namespace tvg
29 {
30     struct Iterator
31     {
32         virtual ~Iterator() {}
33         virtual const Paint* next() = 0;
34         virtual uint32_t count() = 0;
35         virtual void begin() = 0;
36     };
37
38     struct StrategyMethod
39     {
40         virtual ~StrategyMethod() {}
41
42         virtual bool dispose(RenderMethod& renderer) = 0;
43         virtual void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag) = 0;   //Return engine data if it has.
44         virtual bool render(RenderMethod& renderer) = 0;
45         virtual bool bounds(float* x, float* y, float* w, float* h) = 0;
46         virtual RenderRegion bounds(RenderMethod& renderer) const = 0;
47         virtual Paint* duplicate() = 0;
48         virtual Iterator* iterator() = 0;
49     };
50
51     struct Paint::Impl
52     {
53         StrategyMethod* smethod = nullptr;
54         RenderTransform *rTransform = nullptr;
55         uint32_t renderFlag = RenderUpdateFlag::None;
56         Paint* cmpTarget = nullptr;
57         CompositeMethod cmpMethod = CompositeMethod::None;
58         uint8_t opacity = 255;
59
60         ~Impl() {
61             if (cmpTarget) delete(cmpTarget);
62             if (smethod) delete(smethod);
63             if (rTransform) delete(rTransform);
64         }
65
66         void method(StrategyMethod* method)
67         {
68             smethod = method;
69         }
70
71         bool transform(const Matrix& m)
72         {
73             if (!rTransform) {
74                 rTransform = new RenderTransform();
75                 if (!rTransform) return false;
76             }
77             rTransform->override(m);
78             renderFlag |= RenderUpdateFlag::Transform;
79
80             return true;
81         }
82
83         Matrix* transform()
84         {
85             if (rTransform) {
86                 rTransform->update();
87                 return &rTransform->m;
88             }
89             return nullptr;
90         }
91
92         RenderRegion bounds(RenderMethod& renderer) const
93         {
94             return smethod->bounds(renderer);
95         }
96
97         bool dispose(RenderMethod& renderer)
98         {
99             if (cmpTarget) cmpTarget->pImpl->dispose(renderer);
100             return smethod->dispose(renderer);
101         }
102
103         Iterator* iterator()
104         {
105             return smethod->iterator();
106         }
107
108         bool composite(Paint* target, CompositeMethod method)
109         {
110             if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false;
111             if (cmpTarget) delete(cmpTarget);
112             cmpTarget = target;
113             cmpMethod = method;
114             return true;
115         }
116
117         bool rotate(float degree);
118         bool scale(float factor);
119         bool translate(float x, float y);
120         bool bounds(float* x, float* y, float* w, float* h, bool transformed);
121         void* update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, uint32_t pFlag);
122         bool render(RenderMethod& renderer);
123         Paint* duplicate();
124     };
125
126
127     template<class T>
128     struct PaintMethod : StrategyMethod
129     {
130         T* inst = nullptr;
131
132         PaintMethod(T* _inst) : inst(_inst) {}
133         ~PaintMethod() {}
134
135         bool bounds(float* x, float* y, float* w, float* h) override
136         {
137             return inst->bounds(x, y, w, h);
138         }
139
140         RenderRegion bounds(RenderMethod& renderer) const override
141         {
142             return inst->bounds(renderer);
143         }
144
145         bool dispose(RenderMethod& renderer) override
146         {
147             return inst->dispose(renderer);
148         }
149
150         void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag renderFlag) override
151         {
152             return inst->update(renderer, transform, opacity, clips, renderFlag);
153         }
154
155         bool render(RenderMethod& renderer) override
156         {
157             return inst->render(renderer);
158         }
159
160         Paint* duplicate() override
161         {
162             return inst->duplicate();
163         }
164
165         Iterator* iterator() override
166         {
167             return inst->iterator();
168         }
169     };
170 }
171
172 #endif //_TVG_PAINT_H_