7c8dd2ad56a386efe290ffcbfe534daf035e07e9
[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 #endif
72 }
73
74 bool ShapeTizen::AddRect(Rect<float> rect, Vector2 roundedCorner)
75 {
76 #ifdef THORVG_SUPPORT
77   if(!mTvgShape)
78   {
79     DALI_LOG_ERROR("Shape is null [%p]\n", this);
80     return false;
81   }
82   if(static_cast<tvg::Shape*>(mTvgShape)->appendRect(rect.x, rect.y, rect.width, rect.height, roundedCorner.x, roundedCorner.y) != tvg::Result::Success)
83   {
84     DALI_LOG_ERROR("Add Rect Fail [%p]\n", this);
85     return false;
86   }
87   Drawable::SetChanged(true);
88   return true;
89 #else
90   return false;
91 #endif
92 }
93
94 bool ShapeTizen::AddCircle(Vector2 center, Vector2 radius)
95 {
96 #ifdef THORVG_SUPPORT
97   if(!mTvgShape)
98   {
99     DALI_LOG_ERROR("Shape is null [%p]\n", this);
100     return false;
101   }
102   if(static_cast<tvg::Shape*>(mTvgShape)->appendCircle(center.x, center.y, radius.x, radius.y) != tvg::Result::Success)
103   {
104     DALI_LOG_ERROR("Add Circle Fail [%p]\n", this);
105     return false;
106   }
107   Drawable::SetChanged(true);
108   return true;
109 #else
110   return false;
111 #endif
112 }
113
114 bool ShapeTizen::AddArc(Vector2 center, float radius, float startAngle, float sweep, bool pie)
115 {
116 #ifdef THORVG_SUPPORT
117   if(!mTvgShape)
118   {
119     DALI_LOG_ERROR("Shape is null\n");
120     return false;
121   }
122
123   if(static_cast<tvg::Shape*>(mTvgShape)->appendArc(center.x, center.y, radius, startAngle, sweep, pie) != tvg::Result::Success)
124   {
125     DALI_LOG_ERROR("Add arc fail.\n");
126     return false;
127   }
128   Drawable::SetChanged(true);
129   return true;
130 #else
131   return false;
132 #endif
133 }
134
135 bool ShapeTizen::AddMoveTo(Vector2 point)
136 {
137 #ifdef THORVG_SUPPORT
138   if(!mTvgShape)
139   {
140     DALI_LOG_ERROR("Shape is null\n");
141     return false;
142   }
143
144   if(static_cast<tvg::Shape*>(mTvgShape)->moveTo(point.x, point.y) != tvg::Result::Success)
145   {
146     DALI_LOG_ERROR("AddMoveTo() fail.\n");
147     return false;
148   }
149   Drawable::SetChanged(true);
150   return true;
151 #else
152   return false;
153 #endif
154 }
155
156 bool ShapeTizen::AddLineTo(Vector2 line)
157 {
158 #ifdef THORVG_SUPPORT
159   if(!mTvgShape)
160   {
161     DALI_LOG_ERROR("Shape is null\n");
162     return false;
163   }
164
165   if(static_cast<tvg::Shape*>(mTvgShape)->lineTo(line.x, line.y) != tvg::Result::Success)
166   {
167     DALI_LOG_ERROR("AddLineTo() fail.\n");
168     return false;
169   }
170   Drawable::SetChanged(true);
171   return true;
172 #else
173   return false;
174 #endif
175 }
176
177 bool ShapeTizen::AddCubicTo(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
178 {
179 #ifdef THORVG_SUPPORT
180   if(!mTvgShape)
181   {
182     DALI_LOG_ERROR("Shape is null\n");
183     return false;
184   }
185
186   if(static_cast<tvg::Shape*>(mTvgShape)->cubicTo(controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y) != tvg::Result::Success)
187   {
188     DALI_LOG_ERROR("cubicTo() fail.\n");
189     return false;
190   }
191   Drawable::SetChanged(true);
192   return true;
193 #else
194   return false;
195 #endif
196 }
197
198 bool ShapeTizen::Close()
199 {
200 #ifdef THORVG_SUPPORT
201   if(!mTvgShape)
202   {
203     DALI_LOG_ERROR("Shape is null\n");
204     return false;
205   }
206
207   if(static_cast<tvg::Shape*>(mTvgShape)->close() != tvg::Result::Success)
208   {
209     DALI_LOG_ERROR("close() fail.\n");
210     return false;
211   }
212   Drawable::SetChanged(true);
213   return true;
214 #else
215   return false;
216 #endif
217 }
218
219 bool ShapeTizen::ResetPath()
220 {
221 #ifdef THORVG_SUPPORT
222   if(!mTvgShape)
223   {
224     DALI_LOG_ERROR("Shape is null\n");
225     return false;
226   }
227
228   if(static_cast<tvg::Shape*>(mTvgShape)->reset() != tvg::Result::Success)
229   {
230     DALI_LOG_ERROR("reset() fail.\n");
231     return false;
232   }
233   Drawable::SetChanged(true);
234   return true;
235 #else
236   return false;
237 #endif
238 }
239
240 bool ShapeTizen::SetFillColor(Vector4 color)
241 {
242 #ifdef THORVG_SUPPORT
243   if(!mTvgShape)
244   {
245     DALI_LOG_ERROR("Shape is null [%p]\n", this);
246     return false;
247   }
248
249   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)
250   {
251     DALI_LOG_ERROR("SetFillColor fail [%p]\n", this);
252     return false;
253   }
254   Drawable::SetChanged(true);
255   return true;
256 #else
257   return false;
258 #endif
259 }
260
261 Vector4 ShapeTizen::GetFillColor() const
262 {
263 #ifdef THORVG_SUPPORT
264   if(!mTvgShape)
265   {
266     DALI_LOG_ERROR("Shape is null [%p]\n", this);
267     return Vector4(0, 0, 0, 0);
268   }
269   uint8_t r, g, b, a;
270
271   if(static_cast<tvg::Shape*>(mTvgShape)->fillColor(&r, &g, &b, &a) != tvg::Result::Success)
272   {
273     DALI_LOG_ERROR("GetFillColor fail [%p]\n", this);
274     return Vector4(0, 0, 0, 0);
275   }
276   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
277 #else
278   return Vector4::ZERO;
279 #endif
280 }
281
282 bool ShapeTizen::SetFillRule(Dali::CanvasRenderer::Shape::FillRule rule)
283 {
284 #ifdef THORVG_SUPPORT
285   if(!mTvgShape)
286   {
287     DALI_LOG_ERROR("Shape is null\n");
288     return false;
289   }
290   if(static_cast<tvg::Shape*>(mTvgShape)->fill(static_cast<tvg::FillRule>(rule)) != tvg::Result::Success)
291   {
292     DALI_LOG_ERROR("SetFillRule fail.\n");
293     return false;
294   }
295   Drawable::SetChanged(true);
296   return true;
297 #else
298   return false;
299 #endif
300 }
301
302 Dali::CanvasRenderer::Shape::FillRule ShapeTizen::GetFillRule() const
303 {
304 #ifdef THORVG_SUPPORT
305   if(!mTvgShape)
306   {
307     DALI_LOG_ERROR("Shape is null\n");
308     return Dali::CanvasRenderer::Shape::FillRule::WINDING;
309   }
310
311   tvg::FillRule rule = static_cast<tvg::Shape*>(mTvgShape)->fillRule();
312
313   return static_cast<Dali::CanvasRenderer::Shape::FillRule>(rule);
314 #endif
315   return Dali::CanvasRenderer::Shape::FillRule::WINDING;
316 }
317
318 bool ShapeTizen::SetStrokeWidth(float width)
319 {
320 #ifdef THORVG_SUPPORT
321   if(!mTvgShape)
322   {
323     DALI_LOG_ERROR("Shape is null\n");
324     return false;
325   }
326
327   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(width) != tvg::Result::Success)
328   {
329     DALI_LOG_ERROR("SetStrokeWidth fail.\n");
330     return false;
331   }
332   Drawable::SetChanged(true);
333   return true;
334 #else
335   return false;
336 #endif
337 }
338
339 float ShapeTizen::GetStrokeWidth() const
340 {
341 #ifdef THORVG_SUPPORT
342   if(!mTvgShape)
343   {
344     DALI_LOG_ERROR("Shape is null\n");
345     return false;
346   }
347   return static_cast<tvg::Shape*>(mTvgShape)->strokeWidth();
348 #else
349   return false;
350 #endif
351 }
352
353 bool ShapeTizen::SetStrokeColor(Vector4 color)
354 {
355 #ifdef THORVG_SUPPORT
356   if(!mTvgShape)
357   {
358     DALI_LOG_ERROR("Shape is null\n");
359     return false;
360   }
361
362   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)
363   {
364     DALI_LOG_ERROR("SetStrokeColor fail.\n");
365     return false;
366   }
367   Drawable::SetChanged(true);
368   return true;
369 #else
370   return false;
371 #endif
372 }
373
374 Vector4 ShapeTizen::GetStrokeColor() const
375 {
376 #ifdef THORVG_SUPPORT
377   if(!mTvgShape)
378   {
379     DALI_LOG_ERROR("Shape is null\n");
380     return Vector4(0, 0, 0, 0);
381   }
382
383   uint8_t r, g, b, a;
384
385   if(static_cast<tvg::Shape*>(mTvgShape)->strokeColor(&r, &g, &b, &a) != tvg::Result::Success)
386   {
387     DALI_LOG_ERROR("GetStrokeColor fail.\n");
388     return Vector4(0, 0, 0, 0);
389   }
390   return Vector4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
391 #else
392   return Vector4(0, 0, 0, 0);
393 #endif
394 }
395
396 bool ShapeTizen::SetStrokeDash(const Dali::Vector<float> dashPattern)
397 {
398 #ifdef THORVG_SUPPORT
399   if(!mTvgShape)
400   {
401     DALI_LOG_ERROR("Shape is null\n");
402     return false;
403   }
404
405   float* tvgDashPattern = (float*)alloca(sizeof(float) * dashPattern.Count());
406
407   for(unsigned int i = 0u; i < dashPattern.Count(); ++i)
408   {
409     tvgDashPattern[i] = dashPattern[i];
410   }
411
412   if(static_cast<tvg::Shape*>(mTvgShape)->stroke(tvgDashPattern, dashPattern.Count()) != tvg::Result::Success)
413   {
414     DALI_LOG_ERROR("SetStrokeDash fail.\n");
415     return false;
416   }
417   Drawable::SetChanged(true);
418   return true;
419 #else
420   return false;
421 #endif
422 }
423
424 Dali::Vector<float> ShapeTizen::GetStrokeDash() const
425 {
426 #ifdef THORVG_SUPPORT
427   if(!mTvgShape)
428   {
429     DALI_LOG_ERROR("Shape is null\n");
430     return Vector<float>();
431   }
432   const float* tvgDashPattern = nullptr;
433   uint32_t     count          = 0;
434
435   count = static_cast<tvg::Shape*>(mTvgShape)->strokeDash(&tvgDashPattern);
436   if(!tvgDashPattern || count <= 0)
437   {
438     DALI_LOG_ERROR("GetStrokeDash() fail.\n");
439     return Vector<float>();
440   }
441
442   Dali::Vector<float> dashPattern;
443
444   dashPattern.Reserve(count);
445
446   for(unsigned int i = 0u; i < count; ++i)
447   {
448     dashPattern.PushBack(tvgDashPattern[i]);
449   }
450   return dashPattern;
451 #else
452   return Vector<float>();
453 #endif
454 }
455
456 bool ShapeTizen::SetStrokeCap(Dali::CanvasRenderer::Shape::StrokeCap cap)
457 {
458 #ifdef THORVG_SUPPORT
459   if(!mTvgShape)
460   {
461     DALI_LOG_ERROR("Shape is null\n");
462     return false;
463   }
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(!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(!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(!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