2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/canvas-renderer/ubuntu/canvas-renderer-impl-ubuntu.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
26 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
27 #include <dali/internal/canvas-renderer/common/drawable-group-impl.h>
28 #include <dali/internal/canvas-renderer/common/drawable-impl.h>
29 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
37 namespace // unnamed namespace
40 Dali::BaseHandle Create()
42 return Dali::BaseHandle();
45 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer), typeid(Dali::BaseHandle), Create);
47 } // unnamed namespace
49 CanvasRendererUbuntu* CanvasRendererUbuntu::New(const Vector2& viewBox)
51 return new CanvasRendererUbuntu(viewBox);
54 CanvasRendererUbuntu::CanvasRendererUbuntu(const Vector2& viewBox)
55 : mPixelBuffer(nullptr),
61 mViewBox(Vector2::ZERO),
67 CanvasRendererUbuntu::~CanvasRendererUbuntu()
72 //Terminate ThorVG Engine
73 tvg::Initializer::term(tvg::CanvasEngine::Sw);
77 void CanvasRendererUbuntu::Initialize(const Vector2& viewBox)
80 if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
82 DALI_LOG_ERROR("ThorVG engine initialize failed\n");
84 mTvgCanvas = tvg::SwCanvas::gen();
86 mSize = mViewBox = viewBox;
87 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
92 MakeTargetBuffer(mSize);
96 bool CanvasRendererUbuntu::Commit()
99 Mutex::ScopedLock lock(mMutex);
101 if(mSize.width < 1.0f || mSize.height < 1.0f)
103 DALI_LOG_ERROR("Size is zero [%p]\n", this);
107 bool changed = false;
109 for(auto& it : mDrawables)
111 if(HaveDrawablesChanged(it))
113 UpdateDrawablesChanged(it, false);
118 if(!changed && !mChanged)
124 if(!mPixelBuffer || !mPixelBuffer.GetBuffer())
126 MakeTargetBuffer(mSize);
131 if(mTvgCanvas->clear() != tvg::Result::Success)
133 DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
137 auto scene = tvg::Scene::gen();
138 mTvgRoot = scene.get();
139 for(auto& it : mDrawables)
141 PushDrawableToGroup(it, mTvgRoot);
144 if(mViewBox != mSize && mViewBox.width != 0 && mViewBox.height != 0)
146 auto scaleX = mSize.width / mViewBox.width;
147 auto scaleY = mSize.height / mViewBox.height;
148 mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
151 if(mTvgCanvas->push(move(scene)) != tvg::Result::Success)
153 DALI_LOG_ERROR("ThorVG canvas push fail [%p]\n", this);
163 Devel::PixelBuffer CanvasRendererUbuntu::GetPixelBuffer()
168 bool CanvasRendererUbuntu::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
170 #ifdef THORVG_SUPPORT
171 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
172 if(drawableImpl.IsAdded())
174 DALI_LOG_ERROR("Already added [%p][%p]\n", this, &drawable);
178 drawableImpl.SetAdded(true);
179 mDrawables.push_back(drawable);
188 #ifdef THORVG_SUPPORT
189 bool CanvasRendererUbuntu::HaveDrawablesChanged(const Dali::CanvasRenderer::Drawable& drawable) const
191 const Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
192 if(drawableImpl.GetChanged())
196 Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
197 if(DALI_UNLIKELY(compositeDrawable))
199 Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
200 if(compositeDrawableImpl.GetChanged())
206 if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
208 const Dali::CanvasRenderer::DrawableGroup& group = static_cast<const Dali::CanvasRenderer::DrawableGroup&>(drawable);
209 const Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
210 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
211 for(auto& it : drawables)
213 if(HaveDrawablesChanged(it))
223 void CanvasRendererUbuntu::UpdateDrawablesChanged(Dali::CanvasRenderer::Drawable& drawable, bool changed)
225 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
226 drawableImpl.SetChanged(changed);
228 Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
229 if(DALI_UNLIKELY(compositeDrawable))
231 Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
232 compositeDrawableImpl.SetChanged(changed);
235 if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
237 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
238 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
239 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
240 for(auto& it : drawables)
242 UpdateDrawablesChanged(it, changed);
248 bool CanvasRendererUbuntu::IsCanvasChanged() const
250 #ifdef THORVG_SUPPORT
256 for(auto& it : mDrawables)
258 if(HaveDrawablesChanged(it))
267 bool CanvasRendererUbuntu::Rasterize()
269 #ifdef THORVG_SUPPORT
270 Mutex::ScopedLock lock(mMutex);
272 if(mTvgCanvas->draw() != tvg::Result::Success)
274 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
286 bool CanvasRendererUbuntu::RemoveDrawable(Dali::CanvasRenderer::Drawable& drawable)
288 #ifdef THORVG_SUPPORT
289 DrawableGroup::DrawableVector::iterator it = std::find(mDrawables.begin(), mDrawables.end(), drawable);
290 if(it != mDrawables.end())
292 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
293 drawableImpl.SetAdded(false);
295 mDrawables.erase(it);
305 bool CanvasRendererUbuntu::RemoveAllDrawables()
307 #ifdef THORVG_SUPPORT
308 for(auto& it : mDrawables)
310 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(it);
311 drawableImpl.SetAdded(false);
323 bool CanvasRendererUbuntu::SetSize(Vector2 size)
325 if(size.width < 1.0f || size.height < 1.0f)
339 Vector2 CanvasRendererUbuntu::GetSize() const
344 bool CanvasRendererUbuntu::SetViewBox(const Vector2& viewBox)
346 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
351 if(viewBox != mViewBox)
360 const Vector2& CanvasRendererUbuntu::GetViewBox()
365 void CanvasRendererUbuntu::MakeTargetBuffer(const Vector2& size)
367 #ifdef THORVG_SUPPORT
368 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
370 unsigned char* pBuffer;
371 pBuffer = mPixelBuffer.GetBuffer();
375 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
379 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
383 #ifdef THORVG_SUPPORT
384 void CanvasRendererUbuntu::PushDrawableToGroup(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* group)
386 Internal::Adaptor::Drawable& drawableImpl = Dali::GetImplementation(drawable);
387 tvg::Paint* tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
388 if(!tvgDuplicatedObject)
390 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
393 Drawable::Types type = drawableImpl.GetType();
395 if(type == Drawable::Types::DRAWABLE_GROUP)
397 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
398 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
399 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
400 for(auto& it : drawables)
402 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
406 Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
407 if(DALI_UNLIKELY(compositeDrawable))
409 Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
410 tvg::Paint* tvgCompositeObject = static_cast<tvg::Paint*>(compositeDrawableImpl.GetObject());
411 if(tvgCompositeObject)
413 tvg::Paint* tvgDuplicatedCompositeObject = tvgCompositeObject->duplicate();
414 Drawable::Types type = compositeDrawableImpl.GetType();
416 if(type == Drawable::Types::DRAWABLE_GROUP)
418 Dali::CanvasRenderer::DrawableGroup& compositeGroup = static_cast<Dali::CanvasRenderer::DrawableGroup&>(compositeDrawable);
419 Internal::Adaptor::DrawableGroup& compositeDrawableGroupImpl = Dali::GetImplementation(compositeGroup);
420 DrawableGroup::DrawableVector compositeDrawables = compositeDrawableGroupImpl.GetDrawables();
421 for(auto& it : compositeDrawables)
423 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedCompositeObject));
427 if(tvgDuplicatedObject->composite(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedCompositeObject)), static_cast<tvg::CompositeMethod>(drawableImpl.GetCompositionType())) != tvg::Result::Success)
429 DALI_LOG_ERROR("Tvg composite fail [%p]\n", this);
435 if(group->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
437 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
443 } // namespace Adaptor
445 } // namespace Internal