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-impl.h>
28 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
36 namespace // unnamed namespace
39 Dali::BaseHandle Create()
41 return Dali::BaseHandle();
44 Dali::TypeRegistration type(typeid(Dali::CanvasRenderer), typeid(Dali::BaseHandle), Create);
46 } // unnamed namespace
48 CanvasRendererUbuntu* CanvasRendererUbuntu::New(const Vector2& viewBox)
50 return new CanvasRendererUbuntu(viewBox);
53 CanvasRendererUbuntu::CanvasRendererUbuntu(const Vector2& viewBox)
54 : mPixelBuffer(nullptr),
66 CanvasRendererUbuntu::~CanvasRendererUbuntu()
69 for(auto& it : mDrawables)
71 Dali::CanvasRenderer::Drawable drawable = it.GetHandle();
72 if(DALI_UNLIKELY(!drawable))
76 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
77 drawableImpl.SetObject(nullptr);
79 //Terminate ThorVG Engine
80 tvg::Initializer::term(tvg::CanvasEngine::Sw);
84 void CanvasRendererUbuntu::Initialize(const Vector2& viewBox)
87 if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
89 DALI_LOG_ERROR("ThorVG engine initialize failed\n");
91 mTvgCanvas = tvg::SwCanvas::gen();
93 mSize = mViewBox = viewBox;
94 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
99 MakeTargetBuffer(mSize);
101 auto scene = tvg::Scene::gen();
102 mTvgRoot = scene.get();
103 mTvgCanvas->push(move(scene));
107 bool CanvasRendererUbuntu::Commit()
109 #ifdef THORVG_SUPPORT
110 bool changed = false;
112 for(auto& it : mDrawables)
114 Dali::CanvasRenderer::Drawable drawable = it.GetHandle();
115 if(DALI_UNLIKELY(!drawable))
119 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
120 if(drawableImpl.GetChanged())
123 drawableImpl.SetChanged(false);
127 if(!changed && !mChanged)
133 if(!mPixelBuffer.GetBuffer())
135 MakeTargetBuffer(mSize);
140 if(mSize.width < 1.0f || mSize.height < 1.0f)
142 DALI_LOG_ERROR("Size is zero [%p]\n", this);
146 if(mViewBox != mSize)
148 auto scaleX = mSize.width / mViewBox.width;
149 auto scaleY = mSize.height / mViewBox.height;
150 mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
152 mTvgCanvas->update(mTvgRoot);
154 if(mTvgCanvas->draw() != tvg::Result::Success)
156 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
168 Devel::PixelBuffer CanvasRendererUbuntu::GetPixelBuffer()
173 bool CanvasRendererUbuntu::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
175 #ifdef THORVG_SUPPORT
176 for(auto& it : mDrawables)
178 if(it.GetHandle() == drawable)
180 DALI_LOG_ERROR("Already added [%p]\n", this);
185 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
186 tvg::Paint* pDrawable = static_cast<tvg::Paint*>(drawableImpl.GetObject());
189 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
192 if(mSize.width < 1.0f || mSize.height < 1.0f)
194 DALI_LOG_ERROR("Size is zero [%p]\n", this);
198 if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
200 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
204 drawableImpl.SetDrawableAdded(true);
205 mDrawables.push_back(drawable);
214 bool CanvasRendererUbuntu::SetSize(const Vector2& size)
216 if(size.width < 1.0f || size.height < 1.0f)
224 MakeTargetBuffer(size);
232 const Vector2& CanvasRendererUbuntu::GetSize()
237 void CanvasRendererUbuntu::MakeTargetBuffer(const Vector2& size)
239 #ifdef THORVG_SUPPORT
240 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
242 unsigned char* pBuffer;
243 pBuffer = mPixelBuffer.GetBuffer();
247 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
251 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
255 } // namespace Adaptor
257 } // namespace Internal