CanvasRenderer: Refactoring to pass rasterized buffer
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / ubuntu / canvas-renderer-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/canvas-renderer-impl-ubuntu.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
27 #include <dali/internal/canvas-renderer/common/drawable-group-impl.h>
28 #include <dali/internal/canvas-renderer/common/drawable-impl.h>
29 #include <dali/internal/canvas-renderer/common/gradient-impl.h>
30 #include <dali/internal/canvas-renderer/common/shape-impl.h>
31 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace Adaptor
38 {
39 namespace // unnamed namespace
40 {
41 // Type Registration
42 Dali::BaseHandle Create()
43 {
44   return Dali::BaseHandle();
45 }
46
47 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer), typeid(Dali::BaseHandle), Create);
48
49 } // unnamed namespace
50
51 CanvasRendererUbuntu* CanvasRendererUbuntu::New(const Vector2& viewBox)
52 {
53   return new CanvasRendererUbuntu(viewBox);
54 }
55
56 CanvasRendererUbuntu::CanvasRendererUbuntu(const Vector2& viewBox)
57 :
58 #ifdef THORVG_SUPPORT
59   mPixelBuffer(nullptr),
60   mRasterizedTexture(),
61   mMutex(),
62   mTvgCanvas(nullptr),
63   mTvgRoot(nullptr),
64 #endif
65   mSize(Vector2::ZERO),
66   mViewBox(Vector2::ZERO),
67   mChanged(false)
68 {
69   Initialize(viewBox);
70 }
71
72 CanvasRendererUbuntu::~CanvasRendererUbuntu()
73 {
74 #ifdef THORVG_SUPPORT
75   mDrawables.clear();
76
77   //Terminate ThorVG Engine
78   tvg::Initializer::term(tvg::CanvasEngine::Sw);
79 #endif
80 }
81
82 void CanvasRendererUbuntu::Initialize(const Vector2& viewBox)
83 {
84 #ifdef THORVG_SUPPORT
85   if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
86   {
87     DALI_LOG_ERROR("ThorVG engine initialize failed\n");
88   }
89   mTvgCanvas = tvg::SwCanvas::gen();
90
91   mSize = mViewBox = viewBox;
92   if(viewBox.width < 1.0f || viewBox.height < 1.0f)
93   {
94     return;
95   }
96
97   MakeTargetBuffer(mSize);
98 #endif
99 }
100
101 bool CanvasRendererUbuntu::Commit()
102 {
103 #ifdef THORVG_SUPPORT
104   Mutex::ScopedLock lock(mMutex);
105
106   if(mSize.width < 1.0f || mSize.height < 1.0f)
107   {
108     DALI_LOG_ERROR("Size is zero [%p]\n", this);
109     return false;
110   }
111
112   bool changed = false;
113
114   for(auto& it : mDrawables)
115   {
116     if(HaveDrawablesChanged(it))
117     {
118       UpdateDrawablesChanged(it, false);
119       changed = true;
120     }
121   }
122
123   if(!changed && !mChanged)
124   {
125     return false;
126   }
127   else
128   {
129     if(!mPixelBuffer || !mPixelBuffer.GetBuffer())
130     {
131       MakeTargetBuffer(mSize);
132       mChanged = false;
133     }
134   }
135
136   if(mTvgCanvas->clear() != tvg::Result::Success)
137   {
138     DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
139     return false;
140   }
141
142   auto scene = tvg::Scene::gen();
143   mTvgRoot   = scene.get();
144   for(auto& it : mDrawables)
145   {
146     PushDrawableToGroup(it, mTvgRoot);
147   }
148
149   if(mViewBox != mSize && mViewBox.width != 0 && mViewBox.height != 0)
150   {
151     auto scaleX = mSize.width / mViewBox.width;
152     auto scaleY = mSize.height / mViewBox.height;
153     mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
154   }
155
156   if(mTvgCanvas->push(move(scene)) != tvg::Result::Success)
157   {
158     DALI_LOG_ERROR("ThorVG canvas push fail [%p]\n", this);
159     return false;
160   }
161
162   return true;
163 #else
164   return false;
165 #endif
166 }
167
168 Dali::Texture CanvasRendererUbuntu::GetRasterizedTexture()
169 {
170 #ifdef THORVG_SUPPORT
171   if(mPixelBuffer)
172   {
173     auto width  = mPixelBuffer.GetWidth();
174     auto height = mPixelBuffer.GetHeight();
175     if(width <= 0 || height <= 0)
176     {
177       return Dali::Texture();
178     }
179
180     Dali::PixelData pixelData = Devel::PixelBuffer::Convert(mPixelBuffer);
181
182     if(!mRasterizedTexture || mRasterizedTexture.GetWidth() != width || mRasterizedTexture.GetHeight() != height)
183     {
184       mRasterizedTexture = Dali::Texture::New(Dali::TextureType::TEXTURE_2D, Dali::Pixel::BGRA8888, width, height);
185     }
186
187     mRasterizedTexture.Upload(pixelData);
188   }
189   return mRasterizedTexture;
190 #else
191   return Dali::Texture();
192 #endif
193 }
194
195 bool CanvasRendererUbuntu::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
196 {
197 #ifdef THORVG_SUPPORT
198   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
199   if(drawableImpl.IsAdded())
200   {
201     DALI_LOG_ERROR("Already added [%p][%p]\n", this, &drawable);
202     return false;
203   }
204
205   drawableImpl.SetAdded(true);
206   mDrawables.push_back(drawable);
207   mChanged = true;
208
209   return true;
210 #else
211   return false;
212 #endif
213 }
214
215 #ifdef THORVG_SUPPORT
216 bool CanvasRendererUbuntu::HaveDrawablesChanged(const Dali::CanvasRenderer::Drawable& drawable) const
217 {
218   const Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
219   if(drawableImpl.GetChanged())
220   {
221     return true;
222   }
223   Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
224   if(DALI_UNLIKELY(compositeDrawable))
225   {
226     Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
227     if(compositeDrawableImpl.GetChanged())
228     {
229       return true;
230     }
231   }
232
233   if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
234   {
235     const Dali::CanvasRenderer::DrawableGroup& group             = static_cast<const Dali::CanvasRenderer::DrawableGroup&>(drawable);
236     const Internal::Adaptor::DrawableGroup&    drawableGroupImpl = Dali::GetImplementation(group);
237     DrawableGroup::DrawableVector              drawables         = drawableGroupImpl.GetDrawables();
238     for(auto& it : drawables)
239     {
240       if(HaveDrawablesChanged(it))
241       {
242         return true;
243       }
244     }
245   }
246   else if(drawableImpl.GetType() == Drawable::Types::SHAPE)
247   {
248     const Dali::CanvasRenderer::Shape& shape        = static_cast<const Dali::CanvasRenderer::Shape&>(drawable);
249     Dali::CanvasRenderer::Gradient     fillGradient = shape.GetFillGradient();
250     if(DALI_UNLIKELY(fillGradient))
251     {
252       Internal::Adaptor::Gradient& fillGradientImpl = Dali::GetImplementation(fillGradient);
253       if(fillGradientImpl.GetChanged())
254       {
255         return true;
256       }
257     }
258
259     Dali::CanvasRenderer::Gradient strokeGradient = shape.GetStrokeGradient();
260     if(DALI_UNLIKELY(strokeGradient))
261     {
262       Internal::Adaptor::Gradient& strokeGradientImpl = Dali::GetImplementation(strokeGradient);
263       if(strokeGradientImpl.GetChanged())
264       {
265         return true;
266       }
267     }
268   }
269
270   return false;
271 }
272
273 void CanvasRendererUbuntu::UpdateDrawablesChanged(Dali::CanvasRenderer::Drawable& drawable, bool changed)
274 {
275   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
276   drawableImpl.SetChanged(changed);
277
278   Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
279   if(DALI_UNLIKELY(compositeDrawable))
280   {
281     Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
282     compositeDrawableImpl.SetChanged(changed);
283   }
284
285   if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
286   {
287     Dali::CanvasRenderer::DrawableGroup& group             = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
288     Internal::Adaptor::DrawableGroup&    drawableGroupImpl = Dali::GetImplementation(group);
289     DrawableGroup::DrawableVector        drawables         = drawableGroupImpl.GetDrawables();
290     for(auto& it : drawables)
291     {
292       UpdateDrawablesChanged(it, changed);
293     }
294   }
295   else if(drawableImpl.GetType() == Drawable::Types::SHAPE)
296   {
297     Dali::CanvasRenderer::Shape&   shape        = static_cast<Dali::CanvasRenderer::Shape&>(drawable);
298     Dali::CanvasRenderer::Gradient fillGradient = shape.GetFillGradient();
299     if(DALI_UNLIKELY(fillGradient))
300     {
301       Internal::Adaptor::Gradient& fillGradientImpl = Dali::GetImplementation(fillGradient);
302       fillGradientImpl.SetChanged(changed);
303     }
304
305     Dali::CanvasRenderer::Gradient strokeGradient = shape.GetStrokeGradient();
306     if(DALI_UNLIKELY(strokeGradient))
307     {
308       Internal::Adaptor::Gradient& strokeGradientImpl = Dali::GetImplementation(strokeGradient);
309       strokeGradientImpl.SetChanged(changed);
310     }
311   }
312 }
313 #endif
314
315 bool CanvasRendererUbuntu::IsCanvasChanged() const
316 {
317 #ifdef THORVG_SUPPORT
318   if(mChanged)
319   {
320     return true;
321   }
322
323   for(auto& it : mDrawables)
324   {
325     if(HaveDrawablesChanged(it))
326     {
327       return true;
328     }
329   }
330 #endif
331   return false;
332 }
333
334 bool CanvasRendererUbuntu::Rasterize()
335 {
336 #ifdef THORVG_SUPPORT
337   Mutex::ScopedLock lock(mMutex);
338
339   if(mTvgCanvas->draw() != tvg::Result::Success)
340   {
341     DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
342     return false;
343   }
344
345   mTvgCanvas->sync();
346
347   return true;
348 #else
349   return false;
350 #endif
351 }
352
353 bool CanvasRendererUbuntu::RemoveDrawable(Dali::CanvasRenderer::Drawable& drawable)
354 {
355 #ifdef THORVG_SUPPORT
356   DrawableGroup::DrawableVector::iterator it = std::find(mDrawables.begin(), mDrawables.end(), drawable);
357   if(it != mDrawables.end())
358   {
359     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
360     drawableImpl.SetAdded(false);
361
362     mDrawables.erase(it);
363     mChanged = true;
364
365     return true;
366   }
367
368 #endif
369   return false;
370 }
371
372 bool CanvasRendererUbuntu::RemoveAllDrawables()
373 {
374 #ifdef THORVG_SUPPORT
375   for(auto& it : mDrawables)
376   {
377     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(it);
378     drawableImpl.SetAdded(false);
379   }
380
381   mDrawables.clear();
382   mChanged = true;
383
384   return true;
385 #else
386   return false;
387 #endif
388 }
389
390 bool CanvasRendererUbuntu::SetSize(Vector2 size)
391 {
392   if(size.width < 1.0f || size.height < 1.0f)
393   {
394     return false;
395   }
396
397   if(size != mSize)
398   {
399     mSize    = size;
400     mChanged = true;
401   }
402
403   return true;
404 }
405
406 Vector2 CanvasRendererUbuntu::GetSize() const
407 {
408   return mSize;
409 }
410
411 bool CanvasRendererUbuntu::SetViewBox(const Vector2& viewBox)
412 {
413   if(viewBox.width < 1.0f || viewBox.height < 1.0f)
414   {
415     return false;
416   }
417
418   if(viewBox != mViewBox)
419   {
420     mViewBox = viewBox;
421     mChanged = true;
422   }
423
424   return true;
425 }
426
427 const Vector2& CanvasRendererUbuntu::GetViewBox()
428 {
429   return mViewBox;
430 }
431
432 void CanvasRendererUbuntu::MakeTargetBuffer(const Vector2& size)
433 {
434 #ifdef THORVG_SUPPORT
435   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::BGRA8888);
436
437   unsigned char* pBuffer;
438   pBuffer = mPixelBuffer.GetBuffer();
439
440   if(!pBuffer)
441   {
442     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
443     return;
444   }
445
446   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ARGB8888);
447 #endif
448 }
449
450 #ifdef THORVG_SUPPORT
451 void CanvasRendererUbuntu::PushDrawableToGroup(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* group)
452 {
453   Internal::Adaptor::Drawable& drawableImpl        = Dali::GetImplementation(drawable);
454   tvg::Paint*                  tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
455   if(!tvgDuplicatedObject)
456   {
457     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
458     return;
459   }
460   Drawable::Types type = drawableImpl.GetType();
461
462   if(type == Drawable::Types::DRAWABLE_GROUP)
463   {
464     Dali::CanvasRenderer::DrawableGroup& group             = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
465     Internal::Adaptor::DrawableGroup&    drawableGroupImpl = Dali::GetImplementation(group);
466     DrawableGroup::DrawableVector        drawables         = drawableGroupImpl.GetDrawables();
467     for(auto& it : drawables)
468     {
469       PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
470     }
471   }
472   else if(type == Drawable::Types::SHAPE)
473   {
474     //FillGradient
475     Dali::CanvasRenderer::Shape&   shape        = static_cast<Dali::CanvasRenderer::Shape&>(drawable);
476     Dali::CanvasRenderer::Gradient fillGradient = shape.GetFillGradient();
477     if(DALI_UNLIKELY(fillGradient))
478     {
479       Internal::Adaptor::Gradient& fillGradientImpl          = Dali::GetImplementation(fillGradient);
480       tvg::Fill*                   tvgDuplicatedFillGradient = static_cast<tvg::Fill*>(fillGradientImpl.GetObject())->duplicate();
481       if(!tvgDuplicatedFillGradient)
482       {
483         DALI_LOG_ERROR("Invalid gradient object [%p]\n", this);
484         return;
485       }
486       if(static_cast<tvg::Shape*>(tvgDuplicatedObject)->fill(std::unique_ptr<tvg::Fill>(tvgDuplicatedFillGradient)) != tvg::Result::Success)
487       {
488         DALI_LOG_ERROR("Tvg gradient set fail [%p]\n", this);
489         return;
490       }
491     }
492
493     //StrokeGradient
494     Dali::CanvasRenderer::Gradient strokeGradient = shape.GetStrokeGradient();
495     if(DALI_UNLIKELY(strokeGradient))
496     {
497       Internal::Adaptor::Gradient& strokeGradientImpl          = Dali::GetImplementation(strokeGradient);
498       tvg::Fill*                   tvgDuplicatedStrokeGradient = static_cast<tvg::Fill*>(strokeGradientImpl.GetObject())->duplicate();
499       if(!tvgDuplicatedStrokeGradient)
500       {
501         DALI_LOG_ERROR("Invalid gradient object [%p]\n", this);
502         return;
503       }
504       if(static_cast<tvg::Shape*>(tvgDuplicatedObject)->stroke(std::unique_ptr<tvg::Fill>(tvgDuplicatedStrokeGradient)) != tvg::Result::Success)
505       {
506         DALI_LOG_ERROR("Tvg gradient set fail [%p]\n", this);
507         return;
508       }
509     }
510   }
511
512   Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
513   if(DALI_UNLIKELY(compositeDrawable))
514   {
515     Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
516     tvg::Paint*                  tvgCompositeObject    = static_cast<tvg::Paint*>(compositeDrawableImpl.GetObject());
517     if(tvgCompositeObject)
518     {
519       tvg::Paint*     tvgDuplicatedCompositeObject = tvgCompositeObject->duplicate();
520       Drawable::Types type                         = compositeDrawableImpl.GetType();
521
522       if(type == Drawable::Types::DRAWABLE_GROUP)
523       {
524         Dali::CanvasRenderer::DrawableGroup& compositeGroup             = static_cast<Dali::CanvasRenderer::DrawableGroup&>(compositeDrawable);
525         Internal::Adaptor::DrawableGroup&    compositeDrawableGroupImpl = Dali::GetImplementation(compositeGroup);
526         DrawableGroup::DrawableVector        compositeDrawables         = compositeDrawableGroupImpl.GetDrawables();
527         for(auto& it : compositeDrawables)
528         {
529           PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedCompositeObject));
530         }
531       }
532
533       if(tvgDuplicatedObject->composite(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedCompositeObject)), static_cast<tvg::CompositeMethod>(drawableImpl.GetCompositionType())) != tvg::Result::Success)
534       {
535         DALI_LOG_ERROR("Tvg composite fail [%p]\n", this);
536         return;
537       }
538     }
539   }
540
541   if(group->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
542   {
543     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
544     return;
545   }
546 }
547 #endif
548
549 } // namespace Adaptor
550
551 } // namespace Internal
552
553 } // namespace Dali