CanvasRenderer: Move sync() after draw()
[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     }
131   }
132
133   if(!changed && !mChanged)
134   {
135     return false;
136   }
137   else
138   {
139     if(!mPixelBuffer.GetBuffer())
140     {
141       MakeTargetBuffer(mSize);
142       mChanged = false;
143     }
144   }
145
146   if(mSize.width < 1.0f || mSize.height < 1.0f)
147   {
148     DALI_LOG_ERROR("Size is zero [%p]\n", this);
149     return false;
150   }
151
152   if(mViewBox != mSize)
153   {
154     auto scaleX = mSize.width / mViewBox.width;
155     auto scaleY = mSize.height / mViewBox.height;
156     mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
157   }
158   mTvgCanvas->update(mTvgRoot);
159
160   if(mTvgCanvas->draw() != tvg::Result::Success)
161   {
162     DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
163     return false;
164   }
165
166   mTvgCanvas->sync();
167
168   return true;
169 #else
170   return false;
171 #endif
172 }
173
174 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
175 {
176   return mPixelBuffer;
177 }
178
179 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
180 {
181 #ifdef THORVG_SUPPORT
182   bool exist = false;
183   for(DrawableVectorIterator it    = mDrawables.begin(),
184                              endIt = mDrawables.end();
185       it != endIt;
186       ++it)
187   {
188     if((*it) == drawable)
189     {
190       exist = true;
191       break;
192     }
193   }
194   if(exist)
195   {
196     DALI_LOG_ERROR("Already added [%p]\n", this);
197     return false;
198   }
199
200   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
201   tvg::Paint*                  pDrawable    = static_cast<tvg::Paint*>(drawableImpl.GetObject());
202   if(!pDrawable)
203   {
204     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
205     return false;
206   }
207   if(mSize.width < 1.0f || mSize.height < 1.0f)
208   {
209     DALI_LOG_ERROR("Size is zero [%p]\n", this);
210     return false;
211   }
212
213   if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
214   {
215     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
216     return false;
217   }
218
219   drawableImpl.SetDrawableAdded(true);
220   mDrawables.push_back(drawable);
221   mChanged = true;
222
223   return true;
224 #else
225   return false;
226 #endif
227 }
228
229 bool CanvasRendererTizen::SetSize(const Vector2& size)
230 {
231   if(size.width < 1.0f || size.height < 1.0f)
232   {
233     return false;
234   }
235
236   if(size != mSize)
237   {
238     mSize = size;
239     MakeTargetBuffer(size);
240   }
241
242   mChanged = true;
243
244   return true;
245 }
246
247 const Vector2& CanvasRendererTizen::GetSize()
248 {
249   return mSize;
250 }
251
252 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
253 {
254 #ifdef THORVG_SUPPORT
255   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
256
257   unsigned char* pBuffer;
258   pBuffer = mPixelBuffer.GetBuffer();
259
260   if(!pBuffer)
261   {
262     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
263     return;
264   }
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