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/tizen/canvas-renderer-impl-tizen.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 CanvasRendererTizen* CanvasRendererTizen::New(const Vector2& viewBox)
51 return new CanvasRendererTizen(viewBox);
54 CanvasRendererTizen::CanvasRendererTizen(const Vector2& viewBox)
55 : mPixelBuffer(nullptr),
67 CanvasRendererTizen::~CanvasRendererTizen()
70 //This clear function will do free all tvg::Paint object that added.
71 if(mTvgCanvas->clear() != tvg::Result::Success)
73 DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
78 //Terminate ThorVG Engine
79 tvg::Initializer::term(tvg::CanvasEngine::Sw);
83 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
86 if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
88 DALI_LOG_ERROR("ThorVG engine initialize failed\n");
90 mTvgCanvas = tvg::SwCanvas::gen();
92 mSize = mViewBox = viewBox;
93 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
98 MakeTargetBuffer(mSize);
102 bool CanvasRendererTizen::Commit()
104 #ifdef THORVG_SUPPORT
105 bool changed = false;
107 for(auto& it : mDrawables)
109 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(it);
110 if(drawableImpl.GetChanged())
113 drawableImpl.SetChanged(false);
117 if(!changed && !mChanged)
123 if(!mPixelBuffer.GetBuffer())
125 MakeTargetBuffer(mSize);
130 if(mSize.width < 1.0f || mSize.height < 1.0f)
132 DALI_LOG_ERROR("Size is zero [%p]\n", this);
136 if(mTvgCanvas->clear() != tvg::Result::Success)
138 DALI_LOG_ERROR("ThorVG canvas clear fail [%p]\n", this);
142 auto scene = tvg::Scene::gen();
143 mTvgRoot = scene.get();
144 if(mTvgCanvas->push(move(scene)) != tvg::Result::Success)
146 DALI_LOG_ERROR("ThorVG canvas push fail [%p]\n", this);
150 for(auto& it : mDrawables)
152 PushDrawableToParent(it, mTvgRoot);
155 if(mViewBox != mSize)
157 auto scaleX = mSize.width / mViewBox.width;
158 auto scaleY = mSize.height / mViewBox.height;
159 mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
161 mTvgCanvas->update(mTvgRoot);
163 if(mTvgCanvas->draw() != tvg::Result::Success)
165 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
177 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
182 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
184 #ifdef THORVG_SUPPORT
185 for(auto& it : mDrawables)
189 DALI_LOG_ERROR("Already added [%p]\n", this);
194 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
195 if(mSize.width < 1.0f || mSize.height < 1.0f)
197 DALI_LOG_ERROR("Size is zero [%p]\n", this);
201 drawableImpl.SetDrawableAdded(true);
202 mDrawables.push_back(drawable);
211 bool CanvasRendererTizen::SetSize(const Vector2& size)
213 if(size.width < 1.0f || size.height < 1.0f)
221 MakeTargetBuffer(size);
229 const Vector2& CanvasRendererTizen::GetSize()
234 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
236 #ifdef THORVG_SUPPORT
237 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
239 unsigned char* pBuffer;
240 pBuffer = mPixelBuffer.GetBuffer();
244 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
248 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
252 #ifdef THORVG_SUPPORT
253 void CanvasRendererTizen::PushDrawableToParent(Dali::CanvasRenderer::Drawable& drawable, tvg::Scene* parent)
255 Internal::Adaptor::Drawable& drawableImpl = Dali::GetImplementation(drawable);
256 tvg::Paint* tvgDuplicatedObject = static_cast<tvg::Paint*>(drawableImpl.GetObject())->duplicate();
257 if(!tvgDuplicatedObject)
259 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
262 Drawable::DrawableTypes type = drawableImpl.GetDrawableType();
264 if(type == Drawable::DrawableTypes::DRAWABLE_GROUP)
266 Dali::CanvasRenderer::DrawableGroup& group = static_cast<Dali::CanvasRenderer::DrawableGroup&>(drawable);
267 Internal::Adaptor::DrawableGroup& drawableGroupImpl = Dali::GetImplementation(group);
268 std::vector<Dali::CanvasRenderer::Drawable> drawables = drawableGroupImpl.GetDrawables();
269 for(auto& it : drawables)
271 PushDrawableToParent(it, static_cast<tvg::Scene*>(tvgDuplicatedObject));
275 if(parent->push(std::move(std::unique_ptr<tvg::Paint>(tvgDuplicatedObject))) != tvg::Result::Success)
277 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
283 } // namespace Adaptor
285 } // namespace Internal