Merge branch 'devel/master' into devel/graphics
[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 #ifdef THORVG_SUPPORT
56   mTvgCanvas(nullptr),
57   mTvgRoot(nullptr),
58 #endif
59   mSize(0, 0),
60   mViewBox(0, 0),
61   mChanged(false)
62 {
63   Initialize(viewBox);
64 }
65
66 CanvasRendererTizen::~CanvasRendererTizen()
67 {
68 #ifdef THORVG_SUPPORT
69   for(DrawableVectorIterator it    = mDrawables.begin(),
70                              endIt = mDrawables.end();
71       it != endIt;
72       ++it)
73   {
74     Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
75     if(DALI_UNLIKELY(!drawable))
76     {
77       continue;
78     }
79     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
80     drawableImpl.SetObject(nullptr);
81   }
82   //Terminate ThorVG Engine
83   tvg::Initializer::term(tvg::CanvasEngine::Sw);
84 #endif
85 }
86
87 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
88 {
89 #ifdef THORVG_SUPPORT
90   if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
91   {
92     DALI_LOG_ERROR("ThorVG engine initialize failed\n");
93   }
94   mTvgCanvas = tvg::SwCanvas::gen();
95
96   mSize = mViewBox = viewBox;
97   if(viewBox.width < 1.0f || viewBox.height < 1.0f)
98   {
99     return;
100   }
101
102   MakeTargetBuffer(mSize);
103
104   auto scene = tvg::Scene::gen();
105   mTvgRoot   = scene.get();
106   mTvgCanvas->push(move(scene));
107 #endif
108 }
109
110 bool CanvasRendererTizen::Commit()
111 {
112 #ifdef THORVG_SUPPORT
113   bool changed = false;
114
115   for(DrawableVectorIterator it    = mDrawables.begin(),
116                              endIt = mDrawables.end();
117       it != endIt;
118       ++it)
119   {
120     Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
121     if(DALI_UNLIKELY(!drawable))
122     {
123       continue;
124     }
125     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
126     if(drawableImpl.GetChanged())
127     {
128       changed = true;
129       drawableImpl.SetChanged(false);
130       break;
131     }
132   }
133
134   if(!changed && !mChanged)
135   {
136     return false;
137   }
138   else
139   {
140     if(!mPixelBuffer.GetBuffer())
141     {
142       MakeTargetBuffer(mSize);
143       mChanged = false;
144     }
145   }
146
147   if(mSize.width < 1.0f || mSize.height < 1.0f)
148   {
149     DALI_LOG_ERROR("Size is zero [%p]\n", this);
150     return false;
151   }
152
153   if(mViewBox != mSize)
154   {
155     auto scaleX = mSize.width / mViewBox.width;
156     auto scaleY = mSize.height / mViewBox.height;
157     mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
158   }
159   mTvgCanvas->update(mTvgRoot);
160
161   if(mTvgCanvas->draw() != tvg::Result::Success)
162   {
163     DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
164     return false;
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   bool exist = false;
181   for(DrawableVectorIterator it    = mDrawables.begin(),
182                              endIt = mDrawables.end();
183       it != endIt;
184       ++it)
185   {
186     if((*it) == drawable)
187     {
188       exist = true;
189       break;
190     }
191   }
192   if(exist)
193   {
194     DALI_LOG_ERROR("Already added [%p]\n", this);
195     return false;
196   }
197
198   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
199   tvg::Paint*                  pDrawable    = static_cast<tvg::Paint*>(drawableImpl.GetObject());
200   if(!pDrawable)
201   {
202     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
203     return false;
204   }
205   if(mSize.width < 1.0f || mSize.height < 1.0f)
206   {
207     DALI_LOG_ERROR("Size is zero [%p]\n", this);
208     return false;
209   }
210
211   if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
212   {
213     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
214     return false;
215   }
216
217   drawableImpl.SetDrawableAdded(true);
218   mDrawables.push_back(drawable);
219   mChanged = true;
220
221   return true;
222 #else
223   return false;
224 #endif
225 }
226
227 bool CanvasRendererTizen::SetSize(const Vector2& size)
228 {
229   if(size.width < 1.0f || size.height < 1.0f)
230   {
231     return false;
232   }
233
234   if(size != mSize)
235   {
236     mSize = size;
237     MakeTargetBuffer(size);
238   }
239
240   mChanged = true;
241
242   return true;
243 }
244
245 const Vector2& CanvasRendererTizen::GetSize()
246 {
247   return mSize;
248 }
249
250 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
251 {
252 #ifdef THORVG_SUPPORT
253   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
254
255   unsigned char* pBuffer;
256   pBuffer = mPixelBuffer.GetBuffer();
257
258   if(!pBuffer)
259   {
260     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
261     return;
262   }
263
264   mTvgCanvas->sync();
265
266   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
267 #endif
268 }
269
270 } // namespace Adaptor
271
272 } // namespace Internal
273
274 } // namespace Dali