CanvasRenderer: Refactoring tvgObject management
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / ubuntu / drawable-impl-ubuntu.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali/internal/canvas-renderer/ubuntu/drawable-impl-ubuntu.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace Adaptor
31 {
32 namespace // unnamed namespace
33 {
34 // Type Registration
35 Dali::BaseHandle Create()
36 {
37   return Dali::BaseHandle();
38 }
39
40 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer::Drawable), typeid(Dali::BaseHandle), Create);
41
42 } // unnamed namespace
43
44 DrawableUbuntu* DrawableUbuntu::New()
45 {
46   return new DrawableUbuntu();
47 }
48
49 DrawableUbuntu::DrawableUbuntu()
50 : mAdded(false),
51   mChanged(false),
52   mType(Drawable::Types::NONE)
53 #ifdef THORVG_SUPPORT
54   ,
55   mTvgPaint(nullptr)
56 #endif
57 {
58 }
59
60 DrawableUbuntu::~DrawableUbuntu()
61 {
62 #ifdef THORVG_SUPPORT
63   if(mTvgPaint)
64   {
65     delete mTvgPaint;
66   }
67 #endif
68 }
69
70 bool DrawableUbuntu::SetOpacity(float opacity)
71 {
72 #ifdef THORVG_SUPPORT
73   if(!mTvgPaint)
74   {
75     DALI_LOG_ERROR("Drawable is null [%p]\n", this);
76     return false;
77   }
78   if(mTvgPaint->opacity(round(opacity * 255.f)) != tvg::Result::Success)
79   {
80     DALI_LOG_ERROR("Set opacity fail [%p]\n", this);
81     return false;
82   }
83   SetChanged(true);
84   return true;
85 #else
86   return false;
87 #endif
88 }
89
90 float DrawableUbuntu::GetOpacity() const
91 {
92 #ifdef THORVG_SUPPORT
93   if(!mTvgPaint)
94   {
95     DALI_LOG_ERROR("Drawable is null [%p]\n", this);
96     return 0;
97   }
98   return (float)mTvgPaint->opacity() / 255.f;
99 #else
100   return 0;
101 #endif
102 }
103
104 bool DrawableUbuntu::Rotate(Degree degree)
105 {
106 #ifdef THORVG_SUPPORT
107   if(!mTvgPaint)
108   {
109     DALI_LOG_ERROR("Drawable is null\n");
110     return false;
111   }
112
113   if(mTvgPaint->rotate(degree.degree) != tvg::Result::Success)
114   {
115     DALI_LOG_ERROR("Rotate fail.\n");
116     return false;
117   }
118   SetChanged(true);
119   return true;
120 #else
121   return false;
122 #endif
123 }
124
125 bool DrawableUbuntu::Scale(float factor)
126 {
127 #ifdef THORVG_SUPPORT
128   if(!mTvgPaint)
129   {
130     DALI_LOG_ERROR("Drawable is null\n");
131     return false;
132   }
133
134   if(mTvgPaint->scale(factor) != tvg::Result::Success)
135   {
136     DALI_LOG_ERROR("Scale fail.\n");
137     return false;
138   }
139   SetChanged(true);
140   return true;
141 #else
142   return false;
143 #endif
144 }
145
146 bool DrawableUbuntu::Translate(Vector2 translate)
147 {
148 #ifdef THORVG_SUPPORT
149   if(!mTvgPaint)
150   {
151     DALI_LOG_ERROR("Drawable is null\n");
152     return false;
153   }
154
155   if(mTvgPaint->translate(translate.x, translate.y) != tvg::Result::Success)
156   {
157     DALI_LOG_ERROR("Translate fail.\n");
158     return false;
159   }
160   SetChanged(true);
161   return true;
162 #else
163   return false;
164 #endif
165 }
166
167 bool DrawableUbuntu::Transform(const Dali::Matrix3& matrix)
168 {
169 #ifdef THORVG_SUPPORT
170   if(!mTvgPaint)
171   {
172     DALI_LOG_ERROR("Drawable is null\n");
173     return false;
174   }
175
176   tvg::Matrix tvgMatrix = {matrix.AsFloat()[0], matrix.AsFloat()[1], matrix.AsFloat()[2], matrix.AsFloat()[3], matrix.AsFloat()[4], matrix.AsFloat()[5], matrix.AsFloat()[6], matrix.AsFloat()[7], matrix.AsFloat()[8]};
177
178   if(mTvgPaint->transform(tvgMatrix) != tvg::Result::Success)
179   {
180     DALI_LOG_ERROR("Transform fail.\n");
181     return false;
182   }
183   SetChanged(true);
184   return true;
185 #else
186   return false;
187 #endif
188 }
189
190 Rect<float> DrawableUbuntu::GetBoundingBox() const
191 {
192 #ifdef THORVG_SUPPORT
193   if(!mTvgPaint)
194   {
195     DALI_LOG_ERROR("Drawable is null\n");
196     return Rect<float>(0, 0, 0, 0);
197   }
198
199   float x, y, width, height;
200   x = y = width = height = 0;
201
202   if(mTvgPaint->bounds(&x, &y, &width, &height) != tvg::Result::Success)
203   {
204     DALI_LOG_ERROR("Get bounds fail.\n");
205     return Rect<float>(0, 0, 0, 0);
206   }
207   return Rect<float>(x, y, width, height);
208 #else
209   return Rect<float>(0, 0, 0, 0);
210 #endif
211 }
212
213 void DrawableUbuntu::SetAdded(bool added)
214 {
215   mAdded = !!added;
216 }
217
218 bool DrawableUbuntu::IsAdded() const
219 {
220   return mAdded;
221 }
222
223 void* DrawableUbuntu::GetObject() const
224 {
225 #ifdef THORVG_SUPPORT
226   return static_cast<void*>(mTvgPaint);
227 #else
228   return nullptr;
229 #endif
230 }
231
232 void DrawableUbuntu::SetObject(const void* object)
233 {
234 #ifdef THORVG_SUPPORT
235   if(object)
236   {
237     mTvgPaint = static_cast<tvg::Paint*>((void*)object);
238   }
239 #endif
240 }
241
242 void DrawableUbuntu::SetChanged(bool changed)
243 {
244   if(!mChanged && changed) Dali::Stage::GetCurrent().KeepRendering(0.0f);
245   mChanged = !!changed;
246 }
247
248 bool DrawableUbuntu::GetChanged() const
249 {
250   return mChanged;
251 }
252
253 void DrawableUbuntu::SetType(Drawable::Types type)
254 {
255   mType = type;
256 }
257
258 Drawable::Types DrawableUbuntu::GetType() const
259 {
260   return mType;
261 }
262 } // namespace Adaptor
263
264 } // namespace Internal
265
266 } // namespace Dali