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