CanvasRenderer: Refactoring tvgObject management
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / tizen / shape-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/shape-impl-tizen.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 namespace // unnamed namespace
32 {
33 // Type Registration
34 Dali::BaseHandle Create()
35 {
36   return Dali::BaseHandle();
37 }
38
39 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer::Shape), typeid(Dali::BaseHandle), Create);
40
41 } // unnamed namespace
42
43 ShapeTizen* ShapeTizen::New()
44 {
45   return new ShapeTizen();
46 }
47
48 ShapeTizen::ShapeTizen()
49 #ifdef THORVG_SUPPORT
50 : mTvgShape(nullptr)
51 #endif
52 {
53   Initialize();
54 }
55
56 ShapeTizen::~ShapeTizen()
57 {
58 }
59
60 void ShapeTizen::Initialize()
61 {
62 #ifdef THORVG_SUPPORT
63   mTvgShape = tvg::Shape::gen().release();
64   if(!mTvgShape)
65   {
66     DALI_LOG_ERROR("Shape is null [%p]\n", this);
67   }
68
69   Drawable::Create();
70   Drawable::SetObject(static_cast<void*>(mTvgShape));
71   Drawable::SetType(Drawable::Types::SHAPE);
72 #endif
73 }
74
75 bool ShapeTizen::AddRect(Rect<float> rect, Vector2 roundedCorner)
76 {
77 #ifdef THORVG_SUPPORT
78   if(!Drawable::GetObject() || !mTvgShape)
79   {
80     DALI_LOG_ERROR("Shape is null [%p]\n", this);
81     return false;
82   }
83   if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
84   {
85     DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
86     return false;
87   }
88   Drawable::SetChanged(true);
89   return true;
90 #else
91   return false;
92 #endif
93 }
94
95 bool ShapeTizen::AddCircle(Vector2 center, Vector2 radius)
96 {
97 #ifdef THORVG_SUPPORT
98   if(!Drawable::GetObject() || !mTvgShape)
99   {
100     DALI_LOG_ERROR("Shape is null [%p]\n", this);
101     return false;
102   }
103   if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
104   {
105     DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
106     return false;
107   }
108   Drawable::SetChanged(true);
109   return true;
110 #else
111   return false;
112 #endif
113 }
114
115 bool ShapeTizen::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
116 {
117 #ifdef THORVG_SUPPORT
118   if(!Drawable::GetObject() || !mTvgShape)
119   {
120     DALI_LOG_ERROR("Shape is null\n");
121     return false;
122   }
123
124   if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
125   {
126     DALI_LOG_ERROR("Add arc fail.\n");
127     return false;
128   }
129   Drawable::SetChanged(true);
130   return true;
131 #else
132   return false;
133 #endif
134 }
135
136 bool ShapeTizen::AddMoveTo(Vector2 point)
137 {
138 #ifdef THORVG_SUPPORT
139   if(!Drawable::GetObject() || !mTvgShape)
140   {
141     DALI_LOG_ERROR("Shape is null\n");
142     return false;
143   }
144
145   if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
146   {
147     DALI_LOG_ERROR("AddMoveTo() fail.\n");
148     return false;
149   }
150   Drawable::SetChanged(true);
151   return true;
152 #else
153   return false;
154 #endif
155 }
156
157 bool ShapeTizen::AddLineTo(Vector2 line)
158 {
159 #ifdef THORVG_SUPPORT
160   if(!Drawable::GetObject() || !mTvgShape)
161   {
162     DALI_LOG_ERROR("Shape is null\n");
163     return false;
164   }
165
166   if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
167   {
168     DALI_LOG_ERROR("AddLineTo() fail.\n");
169     return false;
170   }
171   Drawable::SetChanged(true);
172   return true;
173 #else
174   return false;
175 #endif
176 }
177
178 bool ShapeTizen::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
179 {
180 #ifdef THORVG_SUPPORT
181   if(!Drawable::GetObject() || !mTvgShape)
182   {
183     DALI_LOG_ERROR("Shape is null\n");
184     return false;
185   }
186
187   if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
188   {
189     DALI_LOG_ERROR("cubicTo() fail.\n");
190     return false;
191   }
192   Drawable::SetChanged(true);
193   return true;
194 #else
195   return false;
196 #endif
197 }
198
199 bool ShapeTizen::Close()
200 {
201 #ifdef THORVG_SUPPORT
202   if(!Drawable::GetObject() || !mTvgShape)
203   {
204     DALI_LOG_ERROR("Shape is null\n");
205     return false;
206   }
207
208   if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
209   {
210     DALI_LOG_ERROR("close() fail.\n");
211     return false;
212   }
213   Drawable::SetChanged(true);
214   return true;
215 #else
216   return false;
217 #endif
218 }
219
220 bool ShapeTizen::ResetPath()
221 {
222 #ifdef THORVG_SUPPORT
223   if(!Drawable::GetObject() || !mTvgShape)
224   {
225     DALI_LOG_ERROR("Shape is null\n");
226     return false;
227   }
228
229   if(static_cast<tvg::Shape*>(mTvgShape)->reset() != tvg::Result::Success)
230   {
231     DALI_LOG_ERROR("reset() fail.\n");
232     return false;
233   }
234   Drawable::SetChanged(true);
235   return true;
236 #else
237   return false;
238 #endif
239 }
240
241 bool ShapeTizen::SetFillColor(Vector4 color)
242 {
243 #ifdef THORVG_SUPPORT
244   if(!Drawable::GetObject() || !mTvgShape)
245   {
246     DALI_LOG_ERROR("Shape is null [%p]\n", this);
247     return false;
248   }
249
250   if(static_cast<tvg::Shape*>(mTvgShape)->fill((uint8_t)(color.r * 255.f), (uint8_t)(color.g * 255.f), (uint8_t)(color.b * 255.f), (uint8_t)(color.a * 255.f)) != tvg::Result::Success)
251   {
252     DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
253     return false;
254   }
255   Drawable::SetChanged(true);
256   return true;
257 #else
258   return false;
259 #endif
260 }
261
262 Vector4 ShapeTizen::GetFillColor() const
263 {
264 #ifdef THORVG_SUPPORT
265   if(!Drawable::GetObject() || !mTvgShape)
266   {
267     DALI_LOG_ERROR("Shape is null [%p]\n", this);
268     return Vector4(0, 0, 0, 0);
269   }
270   uint8_t r, g, b, a;
271
272   if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
273   {
274     DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
275     return Vector4(0, 0, 0, 0);
276   }
277   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
278 #else
279   return Vector4::ZERO;
280 #endif
281 }
282
283 bool ShapeTizen::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
284 {
285 #ifdef THORVG_SUPPORT
286   if(!Drawable::GetObject() || !mTvgShape)
287   {
288     DALI_LOG_ERROR("Shape is null\n");
289     return false;
290   }
291   if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
292   {
293     DALI_LOG_ERROR("SetFillRule fail.\n");
294     return false;
295   }
296   Drawable::SetChanged(true);
297   return true;
298 #else
299   return false;
300 #endif
301 }
302
303 Dali::CanvasRenderer::Shape::FillRule ShapeTizen::GetFillRule() const
304 {
305 #ifdef THORVG_SUPPORT
306   if(!Drawable::GetObject() || !mTvgShape)
307   {
308     DALI_LOG_ERROR("Shape is null\n");
309     return Dali::CanvasRenderer::Shape::FillRule::WINDING;
310   }
311
312   tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
313
314   return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
315 #endif
316   return Dali::CanvasRenderer::Shape::FillRule::WINDING;
317 }
318
319 bool ShapeTizen::SetStrokeWidth(float width)
320 {
321 #ifdef THORVG_SUPPORT
322   if(!Drawable::GetObject() || !mTvgShape)
323   {
324     DALI_LOG_ERROR("Shape is null\n");
325     return false;
326   }
327
328   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(width) != tvg::Result::Success)
329   {
330     DALI_LOG_ERROR("SetStrokeWidth fail.\n");
331     return false;
332   }
333   Drawable::SetChanged(true);
334   return true;
335 #else
336   return false;
337 #endif
338 }
339
340 float ShapeTizen::GetStrokeWidth() const
341 {
342 #ifdef THORVG_SUPPORT
343   if(!Drawable::GetObject() || !mTvgShape)
344   {
345     DALI_LOG_ERROR("Shape is null\n");
346     return false;
347   }
348   return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
349 #else
350   return false;
351 #endif
352 }
353
354 bool ShapeTizen::SetStrokeColor(Vector4 color)
355 {
356 #ifdef THORVG_SUPPORT
357   if(!Drawable::GetObject() || !mTvgShape)
358   {
359     DALI_LOG_ERROR("Shape is null\n");
360     return false;
361   }
362
363   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a * 255.f) != tvg::Result::Success)
364   {
365     DALI_LOG_ERROR("SetStrokeColor fail.\n");
366     return false;
367   }
368   Drawable::SetChanged(true);
369   return true;
370 #else
371   return false;
372 #endif
373 }
374
375 Vector4 ShapeTizen::GetStrokeColor() const
376 {
377 #ifdef THORVG_SUPPORT
378   if(!Drawable::GetObject() || !mTvgShape)
379   {
380     DALI_LOG_ERROR("Shape is null\n");
381     return Vector4(0, 0, 0, 0);
382   }
383
384   uint8_t r, g, b, a;
385
386   if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
387   {
388     DALI_LOG_ERROR("GetStrokeColor fail.\n");
389     return Vector4(0, 0, 0, 0);
390   }
391   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
392 #else
393   return Vector4(0, 0, 0, 0);
394 #endif
395 }
396
397 bool ShapeTizen::SetStrokeDash(const Dali::Vector<float> dashPattern)
398 {
399 #ifdef THORVG_SUPPORT
400   if(!Drawable::GetObject() || !mTvgShape)
401   {
402     DALI_LOG_ERROR("Shape is null\n");
403     return false;
404   }
405
406   float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
407
408   for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
409   {
410     tvgDashPattern[i] = dashPattern[i];
411   }
412
413   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
414   {
415     DALI_LOG_ERROR("SetStrokeDash fail.\n");
416     return false;
417   }
418   Drawable::SetChanged(true);
419   return true;
420 #else
421   return false;
422 #endif
423 }
424
425 Dali::Vector<float> ShapeTizen::GetStrokeDash() const
426 {
427 #ifdef THORVG_SUPPORT
428   if(!Drawable::GetObject() || !mTvgShape)
429   {
430     DALI_LOG_ERROR("Shape is null\n");
431     return Vector<float>();
432   }
433   const float* tvgDashPattern = nullptr;
434   uint32_t     count          = 0;
435
436   count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
437   if(!tvgDashPattern || count <= 0)
438   {
439     DALI_LOG_ERROR("GetStrokeDash() fail.\n");
440     return Vector<float>();
441   }
442
443   Dali::Vector<float> dashPattern;
444
445   dashPattern.Reserve(count);
446
447   for(unsigned int i = 0u; i < count; ++i)
448   {
449     dashPattern.PushBack(tvgDashPattern[i]);
450   }
451   return dashPattern;
452 #else
453   return Vector<float>();
454 #endif
455 }
456
457 bool ShapeTizen::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
458 {
459 #ifdef THORVG_SUPPORT
460   if(!Drawable::GetObject() || !mTvgShape)
461   {
462     DALI_LOG_ERROR("Shape is null\n");
463     return false;
464   }
465   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeCap>(cap)) != tvg::Result::Success)
466   {
467     DALI_LOG_ERROR("SetStrokeCap fail.\n");
468     return false;
469   }
470   Drawable::SetChanged(true);
471   return true;
472 #else
473   return false;
474 #endif
475 }
476
477 Dali::CanvasRenderer::Shape::StrokeCap ShapeTizen::GetStrokeCap() const
478 {
479 #ifdef THORVG_SUPPORT
480   if(!Drawable::GetObject() || !mTvgShape)
481   {
482     DALI_LOG_ERROR("Shape is null\n");
483     return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
484   }
485
486   tvg::StrokeCap cap = static_cast<tvg::Shape*>(mTvgShape)->strokeCap();
487
488   return static_cast<Dali::CanvasRenderer::Shape::StrokeCap>(cap);
489 #endif
490   return Dali::CanvasRenderer::Shape::StrokeCap::SQUARE;
491 }
492
493 bool ShapeTizen::SetStrokeJoin(Dali::CanvasRenderer::Shape::StrokeJoin join)
494 {
495 #ifdef THORVG_SUPPORT
496   if(!Drawable::GetObject() || !mTvgShape)
497   {
498     DALI_LOG_ERROR("Shape is null\n");
499     return false;
500   }
501
502   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(static_cast<tvg::StrokeJoin>(join)) != tvg::Result::Success)
503   {
504     DALI_LOG_ERROR("SetStrokejoin fail.\n");
505     return false;
506   }
507   Drawable::SetChanged(true);
508   return true;
509 #else
510   return false;
511 #endif
512 }
513
514 Dali::CanvasRenderer::Shape::StrokeJoin ShapeTizen::GetStrokeJoin() const
515 {
516 #ifdef THORVG_SUPPORT
517   if(!Drawable::GetObject() || !mTvgShape)
518   {
519     DALI_LOG_ERROR("Shape is null\n");
520     return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
521   }
522
523   tvg::StrokeJoin join = static_cast<tvg::Shape*>(mTvgShape)->strokeJoin();
524
525   return static_cast<Dali::CanvasRenderer::Shape::StrokeJoin>(join);
526 #endif
527   return Dali::CanvasRenderer::Shape::StrokeJoin::BEVEL;
528 }
529
530 } // namespace Adaptor
531
532 } // namespace Internal
533
534 } // namespace Dali