e0ab529d5fb4c1f8f9f094e3d5e3f92b19ee4e23
[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 #ifdef THORVG_SUPPORT
53   ,
54   mTvgPaint(nullptr)
55 #endif
56 {
57 }
58
59 DrawableUbuntu::~DrawableUbuntu()
60 {
61 #ifdef THORVG_SUPPORT
62   if(mTvgPaint && !mAdded)
63   {
64     delete mTvgPaint;
65   }
66 #endif
67 }
68
69 bool DrawableUbuntu::SetOpacity(float opacity)
70 {
71 #ifdef THORVG_SUPPORT
72   if(!mTvgPaint)
73   {
74     DALI_LOG_ERROR("Drawable is null [%p]\n", this);
75     return false;
76   }
77   if(mTvgPaint->opacity(round(opacity * 255.f)) != tvg::Result::Success)
78   {
79     DALI_LOG_ERROR("Set opacity fail [%p]\n", this);
80     return false;
81   }
82   SetChanged(true);
83   return true;
84 #else
85   return false;
86 #endif
87 }
88
89 float DrawableUbuntu::GetOpacity() const
90 {
91 #ifdef THORVG_SUPPORT
92   if(!mTvgPaint)
93   {
94     DALI_LOG_ERROR("Drawable is null [%p]\n", this);
95     return 0;
96   }
97   return (float)mTvgPaint->opacity() / 255.f;
98 #else
99   return 0;
100 #endif
101 }
102
103 bool DrawableUbuntu::Rotate(Degree degree)
104 {
105 #ifdef THORVG_SUPPORT
106   if(!mTvgPaint)
107   {
108     DALI_LOG_ERROR("Drawable is null\n");
109     return false;
110   }
111
112   if(mTvgPaint->rotate(degree.degree) != tvg::Result::Success)
113   {
114     DALI_LOG_ERROR("Rotate fail.\n");
115     return false;
116   }
117   SetChanged(true);
118   return true;
119 #else
120   return false;
121 #endif
122 }
123
124 bool DrawableUbuntu::Scale(float factor)
125 {
126 #ifdef THORVG_SUPPORT
127   if(!mTvgPaint)
128   {
129     DALI_LOG_ERROR("Drawable is null\n");
130     return false;
131   }
132
133   if(mTvgPaint->scale(factor) != tvg::Result::Success)
134   {
135     DALI_LOG_ERROR("Scale fail.\n");
136     return false;
137   }
138   SetChanged(true);
139   return true;
140 #else
141   return false;
142 #endif
143 }
144
145 bool DrawableUbuntu::Translate(Vector2 translate)
146 {
147 #ifdef THORVG_SUPPORT
148   if(!mTvgPaint)
149   {
150     DALI_LOG_ERROR("Drawable is null\n");
151     return false;
152   }
153
154   if(mTvgPaint->translate(translate.x, translate.y) != tvg::Result::Success)
155   {
156     DALI_LOG_ERROR("Translate fail.\n");
157     return false;
158   }
159   SetChanged(true);
160   return true;
161 #else
162   return false;
163 #endif
164 }
165
166 bool DrawableUbuntu::Transform(const Dali::Matrix3& matrix)
167 {
168 #ifdef THORVG_SUPPORT
169   if(!mTvgPaint)
170   {
171     DALI_LOG_ERROR("Drawable is null\n");
172     return false;
173   }
174
175   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]};
176
177   if(mTvgPaint->transform(tvgMatrix) != tvg::Result::Success)
178   {
179     DALI_LOG_ERROR("Transform fail.\n");
180     return false;
181   }
182   SetChanged(true);
183   return true;
184 #else
185   return false;
186 #endif
187 }
188
189 Rect<float> DrawableUbuntu::GetBoundingBox() const
190 {
191 #ifdef THORVG_SUPPORT
192   if(!mTvgPaint)
193   {
194     DALI_LOG_ERROR("Drawable is null\n");
195     return Rect<float>(0, 0, 0, 0);
196   }
197
198   float x, y, width, height;
199   x = y = width = height = 0;
200
201   if(mTvgPaint->bounds(&x, &y, &width, &height) != tvg::Result::Success)
202   {
203     DALI_LOG_ERROR("Get bounds fail.\n");
204     return Rect<float>(0, 0, 0, 0);
205   }
206   return Rect<float>(x, y, width, height);
207 #else
208   return Rect<float>(0, 0, 0, 0);
209 #endif
210 }
211
212 void DrawableUbuntu::SetAdded(bool added)
213 {
214   mAdded = !!added;
215 }
216
217 bool DrawableUbuntu::IsAdded() const
218 {
219   return mAdded;
220 }
221
222 void* DrawableUbuntu::GetObject() const
223 {
224 #ifdef THORVG_SUPPORT
225   return static_cast<void*>(mTvgPaint);
226 #else
227   return nullptr;
228 #endif
229 }
230
231 void DrawableUbuntu::SetObject(const void* object)
232 {
233 #ifdef THORVG_SUPPORT
234   if(object)
235   {
236     mTvgPaint = static_cast<tvg::Paint*>((void*)object);
237   }
238   else
239   {
240     if(mAdded)
241     {
242       mTvgPaint = nullptr;
243     }
244     if(mTvgPaint)
245     {
246       delete mTvgPaint;
247     }
248   }
249 #endif
250 }
251
252 void DrawableUbuntu::SetChanged(bool changed)
253 {
254   if(!mChanged && changed) Dali::Stage::GetCurrent().KeepRendering(0.0f);
255   mChanged = !!changed;
256 }
257
258 bool DrawableUbuntu::GetChanged() const
259 {
260   return mChanged;
261 }
262 } // namespace Adaptor
263
264 } // namespace Internal
265
266 } // namespace Dali