Implements CanvasRenderer that can draw Vector Primitives using ThorVG
[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-impl.h>
28 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace Adaptor
35 {
36 namespace // unnamed namespace
37 {
38 // Type Registration
39 Dali::BaseHandle Create()
40 {
41   return Dali::BaseHandle();
42 }
43
44 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer), typeid(Dali::BaseHandle), Create);
45
46 } // unnamed namespace
47
48 CanvasRendererTizen* CanvasRendererTizen::New(const Vector2& viewBox)
49 {
50   return new CanvasRendererTizen(viewBox);
51 }
52
53 CanvasRendererTizen::CanvasRendererTizen(const Vector2& viewBox)
54 : mPixelBuffer(nullptr),
55   mTvgCanvas(nullptr),
56   mTvgRoot(nullptr),
57   mSize(0, 0),
58   mViewBox(0, 0),
59   mChanged(false)
60 {
61   Initialize(viewBox);
62 }
63
64 CanvasRendererTizen::~CanvasRendererTizen()
65 {
66   for(DrawableVectorIterator it    = mDrawables.begin(),
67                              endIt = mDrawables.end();
68       it != endIt;
69       ++it)
70   {
71     Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
72     if(DALI_UNLIKELY(!drawable))
73     {
74       continue;
75     }
76     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
77     drawableImpl.SetObject(nullptr);
78   }
79   //Terminate ThorVG Engine
80   tvg::Initializer::term(tvg::CanvasEngine::Sw);
81 }
82
83 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
84 {
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
99   auto scene = tvg::Scene::gen();
100   mTvgRoot   = scene.get();
101   mTvgCanvas->push(move(scene));
102 }
103
104 bool CanvasRendererTizen::Commit()
105 {
106   bool changed = false;
107
108   for(DrawableVectorIterator it    = mDrawables.begin(),
109                              endIt = mDrawables.end();
110       it != endIt;
111       ++it)
112   {
113     Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
114     if(DALI_UNLIKELY(!drawable))
115     {
116       continue;
117     }
118     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
119     if(drawableImpl.GetChanged())
120     {
121       changed = true;
122       drawableImpl.SetChanged(false);
123       break;
124     }
125   }
126
127   if(!changed && !mChanged)
128   {
129     return false;
130   }
131   else
132   {
133     if(!mPixelBuffer.GetBuffer())
134     {
135       MakeTargetBuffer(mSize);
136       mChanged = false;
137     }
138   }
139
140   if(mSize.width < 1.0f || mSize.height < 1.0f)
141   {
142     DALI_LOG_ERROR("Size is zero [%p]\n", this);
143     return false;
144   }
145
146   if(mViewBox != mSize)
147   {
148     auto scaleX = mSize.width / mViewBox.width;
149     auto scaleY = mSize.height / mViewBox.height;
150     mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
151   }
152   mTvgCanvas->update(mTvgRoot);
153
154   if(mTvgCanvas->draw() != tvg::Result::Success)
155   {
156     DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
157     return false;
158   }
159   return true;
160 }
161
162 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
163 {
164   return mPixelBuffer;
165 }
166
167 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
168 {
169   bool exist = false;
170   for(DrawableVectorIterator it    = mDrawables.begin(),
171                              endIt = mDrawables.end();
172       it != endIt;
173       ++it)
174   {
175     if((*it) == drawable)
176     {
177       exist = true;
178       break;
179     }
180   }
181   if(exist)
182   {
183     DALI_LOG_ERROR("Already added [%p]\n", this);
184     return false;
185   }
186
187   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
188   tvg::Paint*                  pDrawable    = static_cast<tvg::Paint*>(drawableImpl.GetObject());
189   if(!pDrawable)
190   {
191     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
192     return false;
193   }
194   if(mSize.width < 1.0f || mSize.height < 1.0f)
195   {
196     DALI_LOG_ERROR("Size is zero [%p]\n", this);
197     return false;
198   }
199
200   if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
201   {
202     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
203     return false;
204   }
205
206   drawableImpl.SetDrawableAdded(true);
207   mDrawables.push_back(drawable);
208   mChanged = true;
209
210   return true;
211 }
212
213 bool CanvasRendererTizen::SetSize(const Vector2& size)
214 {
215   if(size.width < 1.0f || size.height < 1.0f)
216   {
217     return false;
218   }
219
220   if(size != mSize)
221   {
222     mSize = size;
223     MakeTargetBuffer(size);
224   }
225
226   mChanged = true;
227
228   return true;
229 }
230
231 const Vector2& CanvasRendererTizen::GetSize()
232 {
233   return mSize;
234 }
235
236 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
237 {
238   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
239
240   unsigned char* pBuffer;
241   pBuffer = mPixelBuffer.GetBuffer();
242
243   if(!pBuffer)
244   {
245     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
246     return;
247   }
248
249   mTvgCanvas->sync();
250
251   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
252 }
253
254 } // namespace Adaptor
255
256 } // namespace Internal
257
258 } // namespace Dali