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     }
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   return true;
166 #else
167   return false;
168 #endif
169 }
170
171 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
172 {
173   return mPixelBuffer;
174 }
175
176 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
177 {
178 #ifdef THORVG_SUPPORT
179   bool exist = false;
180   for(DrawableVectorIterator it    = mDrawables.begin(),
181                              endIt = mDrawables.end();
182       it != endIt;
183       ++it)
184   {
185     if((*it) == drawable)
186     {
187       exist = true;
188       break;
189     }
190   }
191   if(exist)
192   {
193     DALI_LOG_ERROR("Already added [%p]\n", this);
194     return false;
195   }
196
197   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
198   tvg::Paint*                  pDrawable    = static_cast<tvg::Paint*>(drawableImpl.GetObject());
199   if(!pDrawable)
200   {
201     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
202     return false;
203   }
204   if(mSize.width < 1.0f || mSize.height < 1.0f)
205   {
206     DALI_LOG_ERROR("Size is zero [%p]\n", this);
207     return false;
208   }
209
210   if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
211   {
212     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
213     return false;
214   }
215
216   drawableImpl.SetDrawableAdded(true);
217   mDrawables.push_back(drawable);
218   mChanged = true;
219
220   return true;
221 #else
222   return false;
223 #endif
224 }
225
226 bool CanvasRendererTizen::SetSize(const Vector2& size)
227 {
228   if(size.width < 1.0f || size.height < 1.0f)
229   {
230     return false;
231   }
232
233   if(size != mSize)
234   {
235     mSize = size;
236     MakeTargetBuffer(size);
237   }
238
239   mChanged = true;
240
241   return true;
242 }
243
244 const Vector2& CanvasRendererTizen::GetSize()
245 {
246   return mSize;
247 }
248
249 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
250 {
251 #ifdef THORVG_SUPPORT
252   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
253
254   unsigned char* pBuffer;
255   pBuffer = mPixelBuffer.GetBuffer();
256
257   if(!pBuffer)
258   {
259     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
260     return;
261   }
262
263   mTvgCanvas->sync();
264
265   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
266 #endif
267 }
268
269 } // namespace Adaptor
270
271 } // namespace Internal
272
273 } // namespace Dali