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