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),
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()
101 for(auto& it : mDrawables)
103 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(it);
104 if(drawableImpl.GetChanged())
107 drawableImpl.SetChanged(false);
111 if(!changed && !mChanged)
117 if(!mPixelBuffer.GetBuffer())
119 MakeTargetBuffer(mSize);
124 if(mSize.width < 1.0f || mSize.height < 1.0f)
126 DALI_LOG_ERROR("Size is zero [%p]\n", this);
130 if(mTvgCanvas->clear() != tvg::Result::Success)
132 DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
136 auto scene = tvg::Scene::gen();
137 mTvgRoot = scene.get();
138 for(auto& it : mDrawables)
140 PushDrawableToGroup(it, mTvgRoot);
143 if(mTvgCanvas->push(move(scene)) != tvg::Result::Success)
145 DALI_LOG_ERROR("ThorVG canvas push fail [%p]\n", this);
149 if(mViewBox != mSize)
151 auto scaleX = mSize.width / mViewBox.width;
152 auto scaleY = mSize.height / mViewBox.height;
153 mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
156 mTvgCanvas->update(mTvgRoot);
158 if(mTvgCanvas->draw() != tvg::Result::Success)
160 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
172 Devel::PixelBuffer CanvasRendererUbuntu::GetPixelBuffer()
177 bool CanvasRendererUbuntu::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
179 #ifdef THORVG_SUPPORT
180 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
181 if(drawableImpl.IsAdded())
183 DALI_LOG_ERROR("Already added [%p][%p]\n", this, &drawable);
187 if(mSize.width < 1.0f || mSize.height < 1.0f)
189 DALI_LOG_ERROR("Size is zero [%p]\n", this);
193 drawableImpl.SetAdded(true);
194 mDrawables.push_back(drawable);
203 bool CanvasRendererUbuntu::SetSize(const Vector2& size)
205 if(size.width < 1.0f || size.height < 1.0f)
213 MakeTargetBuffer(size);
221 const Vector2& CanvasRendererUbuntu::GetSize()
226 void CanvasRendererUbuntu::MakeTargetBuffer(const Vector2& size)
228 #ifdef THORVG_SUPPORT
229 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
231 unsigned char* pBuffer;
232 pBuffer = mPixelBuffer.GetBuffer();
236 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
240 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
244 #ifdef THORVG_SUPPORT
245 void CanvasRendererUbuntu::PushDrawableToGroup(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* group)
247 Internal::Adaptor::Drawable& drawableImpl = Dali::GetImplementation(drawable);
248 tvg::Paint* tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
249 if(!tvgDuplicatedObject)
251 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
254 Drawable::Types type = drawableImpl.GetType();
256 if(type == Drawable::Types::DRAWABLE_GROUP)
258 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
259 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
260 DrawableGroup::DrawableVector drawables = drawableGroupImpl.GetDrawables();
261 for(auto& it : drawables)
263 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
267 Dali::CanvasRenderer::Drawable compositeDrawable = drawableImpl.GetCompositionDrawable();
268 if(DALI_UNLIKELY(compositeDrawable))
270 Internal::Adaptor::Drawable& compositeDrawableImpl = Dali::GetImplementation(compositeDrawable);
271 tvg::Paint* tvgCompositeObject = static_cast<tvg::Paint*>(compositeDrawableImpl.GetObject());
272 if(tvgCompositeObject)
274 tvg::Paint* tvgDuplicatedCompositeObject = tvgCompositeObject->duplicate();
275 Drawable::Types type = compositeDrawableImpl.GetType();
277 if(type == Drawable::Types::DRAWABLE_GROUP)
279 Dali::CanvasRenderer::DrawableGroup& compositeGroup = static_cast<Dali::CanvasRenderer::DrawableGroup&>(compositeDrawable);
280 Internal::Adaptor::DrawableGroup& compositeDrawableGroupImpl = Dali::GetImplementation(compositeGroup);
281 DrawableGroup::DrawableVector compositeDrawables = compositeDrawableGroupImpl.GetDrawables();
282 for(auto& it : compositeDrawables)
284 PushDrawableToGroup(it, static_cast<tvg::Scene*>(tvgDuplicatedCompositeObject));
288 if(tvgDuplicatedObject->composite(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedCompositeObject)), static_cast<tvg::CompositeMethod>(drawableImpl.GetCompositionType())) != tvg::Result::Success)
290 DALI_LOG_ERROR("Tvg composite fail [%p]\n", this);
296 if(group->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
298 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
304 } // namespace Adaptor
306 } // namespace Internal