5b1328bd0551d47414b3418a053cd508ecdb46cc
[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(auto& it : mDrawables)
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 #endif
82 }
83
84 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
85 {
86 #ifdef THORVG_SUPPORT
87   if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
88   {
89     DALI_LOG_ERROR("ThorVG engine initialize failed\n");
90   }
91   mTvgCanvas = tvg::SwCanvas::gen();
92
93   mSize = mViewBox = viewBox;
94   if(viewBox.width < 1.0f || viewBox.height < 1.0f)
95   {
96     return;
97   }
98
99   MakeTargetBuffer(mSize);
100
101   auto scene = tvg::Scene::gen();
102   mTvgRoot   = scene.get();
103   mTvgCanvas->push(move(scene));
104 #endif
105 }
106
107 bool CanvasRendererTizen::Commit()
108 {
109 #ifdef THORVG_SUPPORT
110   bool changed = false;
111
112   for(auto& it : mDrawables)
113   {
114     Dali::CanvasRenderer::Drawable drawable = it.GetHandle();
115     if(DALI_UNLIKELY(!drawable))
116     {
117       continue;
118     }
119     Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
120     if(drawableImpl.GetChanged())
121     {
122       changed = true;
123       drawableImpl.SetChanged(false);
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
160   mTvgCanvas->sync();
161
162   return true;
163 #else
164   return false;
165 #endif
166 }
167
168 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
169 {
170   return mPixelBuffer;
171 }
172
173 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
174 {
175 #ifdef THORVG_SUPPORT
176   for(auto& it : mDrawables)
177   {
178     if(it.GetHandle() == drawable)
179     {
180       DALI_LOG_ERROR("Already added [%p]\n", this);
181       return false;
182     }
183   }
184
185   Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
186   tvg::Paint*                  pDrawable    = static_cast<tvg::Paint*>(drawableImpl.GetObject());
187   if(!pDrawable)
188   {
189     DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
190     return false;
191   }
192   if(mSize.width < 1.0f || mSize.height < 1.0f)
193   {
194     DALI_LOG_ERROR("Size is zero [%p]\n", this);
195     return false;
196   }
197
198   if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
199   {
200     DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
201     return false;
202   }
203
204   drawableImpl.SetAdded(true);
205   mDrawables.push_back(drawable);
206   mChanged = true;
207
208   return true;
209 #else
210   return false;
211 #endif
212 }
213
214 bool CanvasRendererTizen::SetSize(const Vector2& size)
215 {
216   if(size.width < 1.0f || size.height < 1.0f)
217   {
218     return false;
219   }
220
221   if(size != mSize)
222   {
223     mSize = size;
224     MakeTargetBuffer(size);
225   }
226
227   mChanged = true;
228
229   return true;
230 }
231
232 const Vector2& CanvasRendererTizen::GetSize()
233 {
234   return mSize;
235 }
236
237 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
238 {
239 #ifdef THORVG_SUPPORT
240   mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
241
242   unsigned char* pBuffer;
243   pBuffer = mPixelBuffer.GetBuffer();
244
245   if(!pBuffer)
246   {
247     DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
248     return;
249   }
250
251   mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
252 #endif
253 }
254
255 } // namespace Adaptor
256
257 } // namespace Internal
258
259 } // namespace Dali