CanvasRenderer: Add Drawable::SetClipPath() Api
[platform/core/uifw/dali-adaptor.git] / dali / internal / canvas-renderer / tizen / canvas-renderer-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/canvas-renderer-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 // 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/imaging/common/pixel-buffer-impl.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 namespace // unnamed namespace
38 {
39 // Type Registration
40 Dali::BaseHandle Create()
41 {
42   return Dali::BaseHandle();
43 }
44
45 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer), typeid(Dali::BaseHandle), Create);
46
47 } // unnamed namespace
48
49 CanvasRendererTizen* CanvasRendererTizen::New(const Vector2& viewBox)
50 {
51   return new CanvasRendererTizen(viewBox);
52 }
53
54 CanvasRendererTizen::CanvasRendererTizen(const Vector2& viewBox)
55 : mPixelBuffer(nullptr),
56 #ifdef THORVG_SUPPORT
57   mTvgCanvas(nullptr),
58   mTvgRoot(nullptr),
59 #endif
60   mSize(0, 0),
61   mViewBox(0, 0),
62   mChanged(false)
63 {
64   Initialize(viewBox);
65 }
66
67 CanvasRendererTizen::~CanvasRendererTizen()
68 {
69 #ifdef THORVG_SUPPORT
70   mDrawables.clear();
71
72   //Terminate ThorVG Engine
73   tvg::Initializer::term(tvg::CanvasEngine::Sw);
74 #endif
75 }
76
77 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
78 {
79 #ifdef THORVG_SUPPORT
80   if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
81   {
82     DALI_LOG_ERROR("ThorVG engine initialize failed\n");
83   }
84   mTvgCanvas = tvg::SwCanvas::gen();
85
86   mSize = mViewBox = viewBox;
87   if(viewBox.width < 1.0f || viewBox.height < 1.0f)
88   {
89     return;
90   }
91
92   MakeTargetBuffer(mSize);
93 #endif
94 }
95
96 bool CanvasRendererTizen::Commit()
97 {
98 #ifdef THORVG_SUPPORT
99   bool changed = false;
100
101   for(auto& it : mDrawables)
102   {
103     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(it);
104     if(drawableImpl.GetChanged())
105     {
106       changed = true;
107       drawableImpl.SetChanged(false);
108     }
109   }
110
111   if(!changed && !mChanged)
112   {
113     return false;
114   }
115   else
116   {
117     if(!mPixelBuffer.GetBuffer())
118     {
119       MakeTargetBuffer(mSize);
120       mChanged = false;
121     }
122   }
123
124   if(mSize.width < 1.0f || mSize.height < 1.0f)
125   {
126     DALI_LOG_ERROR("Size is zero [%p]\n", this);
127     return false;
128   }
129
130   if(mTvgCanvas->clear() != tvg::Result::Success)
131   {
132     DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
133     return false;
134   }
135
136   auto scene = tvg::Scene::gen();
137   mTvgRoot   = scene.get();
138   for(auto& it : mDrawables)
139   {
140     PushDrawableToGroup(it, mTvgRoot);
141   }
142
143   if(mTvgCanvas->push(move(scene)) != tvg::Result::Success)
144   {
145     DALI_LOG_ERROR("ThorVG canvas push fail [%p]\n", this);
146     return false;
147   }
148
149   if(mViewBox != mSize)
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   mTvgCanvas->update(mTvgRoot);
157
158   if(mTvgCanvas->draw() != tvg::Result::Success)
159   {
160     DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
161     return false;
162   }
163
164   mTvgCanvas->sync();
165
166   return true;
167 #else
168   return false;
169 #endif
170 }
171
172 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
173 {
174   return mPixelBuffer;
175 }
176
177 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
178 {
179 #ifdef THORVG_SUPPORT
180   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
181   if(drawableImpl.IsAdded())
182   {
183     DALI_LOG_ERROR("Already added [%p][%p]\n", this, &drawable);
184     return false;
185   }
186
187   if(mSize.width < 1.0f || mSize.height < 1.0f)
188   {
189     DALI_LOG_ERROR("Size is zero [%p]\n", this);
190     return false;
191   }
192
193   drawableImpl.SetAdded(true);
194   mDrawables.push_back(drawable);
195   mChanged = true;
196
197   return true;
198 #else
199   return false;
200 #endif
201 }
202
203 bool CanvasRendererTizen::SetSize(const Vector2& size)
204 {
205   if(size.width < 1.0f || size.height < 1.0f)
206   {
207     return false;
208   }
209
210   if(size != mSize)
211   {
212     mSize = size;
213     MakeTargetBuffer(size);
214   }
215
216   mChanged = true;
217
218   return true;
219 }
220
221 const Vector2& CanvasRendererTizen::GetSize()
222 {
223   return mSize;
224 }
225
226 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
227 {
228 #ifdef THORVG_SUPPORT
229   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
230
231   unsigned char* pBuffer;
232   pBuffer = mPixelBuffer.GetBuffer();
233
234   if(!pBuffer)
235   {
236     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
237     return;
238   }
239
240   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
241 #endif
242 }
243
244 #ifdef THORVG_SUPPORT
245 void CanvasRendererTizen::PushDrawableToGroup(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* parent)
246 {
247   Internal::Adaptor::Drawable& drawableImpl        = Dali::GetImplementation(drawable);
248   tvg::Paint*                  tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
249   if(!tvgDuplicatedObject)
250   {
251     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
252     return;
253   }
254   Drawable::Types type = drawableImpl.GetType();
255
256   if(type == Drawable::Types::DRAWABLE_GROUP)
257   {
258     Dali::CanvasRenderer::DrawableGroup& group             = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
259     Internal::Adaptor::DrawableGroup&    drawableGroupImpl = Dali::GetImplementation(group);
260     DrawableGroup::DrawableVector        drawables         = drawableGroupImpl.GetDrawables();
261     for(auto& it : drawables)
262     {
263       PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
264     }
265   }
266
267   Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
268   if(DALI_UNLIKELY(compositeDrawable))
269   {
270     Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
271     tvg::Paint*                  tvgCompositeObject    = static_cast<tvg::Paint*>(compositeDrawableImpl.GetObject());
272     if(tvgCompositeObject)
273     {
274       tvg::Paint*     tvgDuplicatedCompositeObject = tvgCompositeObject->duplicate();
275       Drawable::Types type                         = compositeDrawableImpl.GetType();
276
277       if(type == Drawable::Types::DRAWABLE_GROUP)
278       {
279         Dali::CanvasRenderer::DrawableGroup& compositeGroup             = static_cast<Dali::CanvasRenderer::DrawableGroup&>(compositeDrawable);
280         Internal::Adaptor::DrawableGroup&    compositeDrawableGroupImpl = Dali::GetImplementation(compositeGroup);
281         DrawableGroup::DrawableVector        compositeDrawables         = compositeDrawableGroupImpl.GetDrawables();
282         for(auto& it : compositeDrawables)
283         {
284           PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedCompositeObject));
285         }
286       }
287
288       if(tvgDuplicatedObject->composite(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedCompositeObject)), static_cast<tvg::CompositeMethod>(drawableImpl.GetCompositionType())) != tvg::Result::Success)
289       {
290         DALI_LOG_ERROR("Tvg composite fail [%p]\n", this);
291         return;
292       }
293     }
294   }
295
296   if(parent->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
297   {
298     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
299     return;
300   }
301 }
302 #endif
303
304 } // namespace Adaptor
305
306 } // namespace Internal
307
308 } // namespace Dali