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())
197 if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
199 const Dali::CanvasRenderer::DrawableGroup& group = static_cast<const Dali::CanvasRenderer::DrawableGroup&>(drawable);
200 const Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
201 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
202 for(auto& it : drawables)
204 if(HaveDrawablesChanged(it))
214 void CanvasRendererUbuntu::UpdateDrawablesChanged(Dali::CanvasRenderer::Drawable& drawable, bool changed)
216 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
217 drawableImpl.SetChanged(changed);
219 if(drawableImpl.GetType() == Drawable::Types::DRAWABLE_GROUP)
221 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
222 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
223 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
224 for(auto& it : drawables)
226 UpdateDrawablesChanged(it, changed);
232 bool CanvasRendererUbuntu::IsCanvasChanged() const
234 #ifdef THORVG_SUPPORT
240 for(auto& it : mDrawables)
242 if(HaveDrawablesChanged(it))
251 bool CanvasRendererUbuntu::Rasterize()
253 #ifdef THORVG_SUPPORT
254 Mutex::ScopedLock lock(mMutex);
256 if(mTvgCanvas->draw() != tvg::Result::Success)
258 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
270 bool CanvasRendererUbuntu::RemoveDrawable(Dali::CanvasRenderer::Drawable& drawable)
272 #ifdef THORVG_SUPPORT
273 DrawableGroup::DrawableVector::iterator it = std::find(mDrawables.begin(), mDrawables.end(), drawable);
274 if(it != mDrawables.end())
276 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
277 drawableImpl.SetAdded(false);
279 mDrawables.erase(it);
289 bool CanvasRendererUbuntu::SetSize(const Vector2& size)
291 if(size.width < 1.0f || size.height < 1.0f)
305 const Vector2& CanvasRendererUbuntu::GetSize()
310 bool CanvasRendererUbuntu::SetViewBox(const Vector2& viewBox)
312 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
317 if(viewBox != mViewBox)
326 const Vector2& CanvasRendererUbuntu::GetViewBox()
331 void CanvasRendererUbuntu::MakeTargetBuffer(const Vector2& size)
333 #ifdef THORVG_SUPPORT
334 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
336 unsigned char* pBuffer;
337 pBuffer = mPixelBuffer.GetBuffer();
341 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
345 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
349 #ifdef THORVG_SUPPORT
350 void CanvasRendererUbuntu::PushDrawableToGroup(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* group)
352 Internal::Adaptor::Drawable& drawableImpl = Dali::GetImplementation(drawable);
353 tvg::Paint* tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
354 if(!tvgDuplicatedObject)
356 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
359 Drawable::Types type = drawableImpl.GetType();
361 if(type == Drawable::Types::DRAWABLE_GROUP)
363 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
364 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
365 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
366 for(auto& it : drawables)
368 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
372 Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
373 if(DALI_UNLIKELY(compositeDrawable))
375 Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
376 tvg::Paint* tvgCompositeObject = static_cast<tvg::Paint*>(compositeDrawableImpl.GetObject());
377 if(tvgCompositeObject)
379 tvg::Paint* tvgDuplicatedCompositeObject = tvgCompositeObject->duplicate();
380 Drawable::Types type = compositeDrawableImpl.GetType();
382 if(type == Drawable::Types::DRAWABLE_GROUP)
384 Dali::CanvasRenderer::DrawableGroup& compositeGroup = static_cast<Dali::CanvasRenderer::DrawableGroup&>(compositeDrawable);
385 Internal::Adaptor::DrawableGroup& compositeDrawableGroupImpl = Dali::GetImplementation(compositeGroup);
386 DrawableGroup::DrawableVector compositeDrawables = compositeDrawableGroupImpl.GetDrawables();
387 for(auto& it : compositeDrawables)
389 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedCompositeObject));
393 if(tvgDuplicatedObject->composite(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedCompositeObject)), static_cast<tvg::CompositeMethod>(drawableImpl.GetCompositionType())) != tvg::Result::Success)
395 DALI_LOG_ERROR("Tvg composite fail [%p]\n", this);
401 if(group->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
403 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
409 } // namespace Adaptor
411 } // namespace Internal