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