CanvasRenderer:: Add Picture class
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / tizen / drawable-impl-tizen.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/tizen/drawable-impl-tizen.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 DrawableTizen* DrawableTizen::New()
45 {
46   return new DrawableTizen();
47 }
48
49 DrawableTizen::DrawableTizen()
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 DrawableTizen::~DrawableTizen()
63 {
64 #ifdef THORVG_SUPPORT
65   if(mTvgPaint)
66   {
67     delete mTvgPaint;
68   }
69 #endif
70 }
71
72 bool DrawableTizen::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 DrawableTizen::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 DrawableTizen::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 DrawableTizen::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 DrawableTizen::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 DrawableTizen::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> DrawableTizen::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 DrawableTizen::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 bool DrawableTizen::SetMask(Dali::CanvasRenderer::Drawable& mask, Dali::CanvasRenderer::Drawable::MaskType type)
243 {
244 #ifdef THORVG_SUPPORT
245   if(!mTvgPaint)
246   {
247     DALI_LOG_ERROR("Drawable is null\n");
248     return false;
249   }
250
251   Internal::Adaptor::Drawable& drawableImpl = Dali::GetImplementation(mask);
252   if(drawableImpl.IsAdded())
253   {
254     DALI_LOG_ERROR("Already used [%p][%p]\n", this, &mask);
255     return false;
256   }
257
258   drawableImpl.SetAdded(true);
259   mCompositionDrawable = mask;
260   switch(type)
261   {
262     case Dali::CanvasRenderer::Drawable::MaskType::ALPHA:
263     {
264       mCompositionType = Drawable::CompositionType::ALPHA_MASK;
265       break;
266     }
267     case Dali::CanvasRenderer::Drawable::MaskType::ALPHA_INVERSE:
268     {
269       mCompositionType = Drawable::CompositionType::ALPHA_MASK_INVERSE;
270       break;
271     }
272     default:
273     {
274       mCompositionType = Drawable::CompositionType::ALPHA_MASK;
275       break;
276     }
277   }
278   Drawable::SetChanged(true);
279
280   return true;
281 #else
282   return false;
283 #endif
284 }
285
286 Dali::CanvasRenderer::Drawable DrawableTizen::GetCompositionDrawable() const
287 {
288   return mCompositionDrawable;
289 }
290
291 Drawable::CompositionType DrawableTizen::GetCompositionType() const
292 {
293   return mCompositionType;
294 }
295
296 void DrawableTizen::SetAdded(bool added)
297 {
298   mAdded = !!added;
299 }
300
301 bool DrawableTizen::IsAdded() const
302 {
303   return mAdded;
304 }
305
306 void* DrawableTizen::GetObject() const
307 {
308 #ifdef THORVG_SUPPORT
309   return static_cast<void*>(mTvgPaint);
310 #else
311   return nullptr;
312 #endif
313 }
314
315 void DrawableTizen::SetObject(const void* object)
316 {
317 #ifdef THORVG_SUPPORT
318   if(object)
319   {
320     mTvgPaint = static_cast<tvg::Paint*>((void*)object);
321   }
322 #endif
323 }
324
325 void DrawableTizen::SetChanged(bool changed)
326 {
327   if(!mChanged && changed) Dali::Stage::GetCurrent().KeepRendering(0.0f);
328   mChanged = !!changed;
329 }
330
331 bool DrawableTizen::GetChanged() const
332 {
333   return mChanged;
334 }
335
336 void DrawableTizen::SetType(Drawable::Types type)
337 {
338   mType = type;
339 }
340
341 Drawable::Types DrawableTizen::GetType() const
342 {
343   return mType;
344 }
345 } // namespace Adaptor
346
347 } // namespace Internal
348
349 } // namespace Dali