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-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 CanvasRendererTizen* CanvasRendererTizen::New(const Vector2& viewBox)
50 return new CanvasRendererTizen(viewBox);
53 CanvasRendererTizen::CanvasRendererTizen(const Vector2& viewBox)
54 : mPixelBuffer(nullptr),
66 CanvasRendererTizen::~CanvasRendererTizen()
69 for(DrawableVectorIterator it = mDrawables.begin(),
70 endIt = mDrawables.end();
74 Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
75 if(DALI_UNLIKELY(!drawable))
79 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
80 drawableImpl.SetObject(nullptr);
82 //Terminate ThorVG Engine
83 tvg::Initializer::term(tvg::CanvasEngine::Sw);
87 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
90 if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
92 DALI_LOG_ERROR("ThorVG engine initialize failed\n");
94 mTvgCanvas = tvg::SwCanvas::gen();
96 mSize = mViewBox = viewBox;
97 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
102 MakeTargetBuffer(mSize);
104 auto scene = tvg::Scene::gen();
105 mTvgRoot = scene.get();
106 mTvgCanvas->push(move(scene));
110 bool CanvasRendererTizen::Commit()
112 #ifdef THORVG_SUPPORT
113 bool changed = false;
115 for(DrawableVectorIterator it = mDrawables.begin(),
116 endIt = mDrawables.end();
120 Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
121 if(DALI_UNLIKELY(!drawable))
125 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
126 if(drawableImpl.GetChanged())
129 drawableImpl.SetChanged(false);
133 if(!changed && !mChanged)
139 if(!mPixelBuffer.GetBuffer())
141 MakeTargetBuffer(mSize);
146 if(mSize.width < 1.0f || mSize.height < 1.0f)
148 DALI_LOG_ERROR("Size is zero [%p]\n", this);
152 if(mViewBox != mSize)
154 auto scaleX = mSize.width / mViewBox.width;
155 auto scaleY = mSize.height / mViewBox.height;
156 mTvgRoot->scale(scaleX < scaleY ? scaleX : scaleY);
158 mTvgCanvas->update(mTvgRoot);
160 if(mTvgCanvas->draw() != tvg::Result::Success)
162 DALI_LOG_ERROR("ThorVG Draw fail [%p]\n", this);
171 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
176 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
178 #ifdef THORVG_SUPPORT
180 for(DrawableVectorIterator it = mDrawables.begin(),
181 endIt = mDrawables.end();
185 if((*it) == drawable)
193 DALI_LOG_ERROR("Already added [%p]\n", this);
197 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
198 tvg::Paint* pDrawable = static_cast<tvg::Paint*>(drawableImpl.GetObject());
201 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
204 if(mSize.width < 1.0f || mSize.height < 1.0f)
206 DALI_LOG_ERROR("Size is zero [%p]\n", this);
210 if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
212 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
216 drawableImpl.SetDrawableAdded(true);
217 mDrawables.push_back(drawable);
226 bool CanvasRendererTizen::SetSize(const Vector2& size)
228 if(size.width < 1.0f || size.height < 1.0f)
236 MakeTargetBuffer(size);
244 const Vector2& CanvasRendererTizen::GetSize()
249 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
251 #ifdef THORVG_SUPPORT
252 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
254 unsigned char* pBuffer;
255 pBuffer = mPixelBuffer.GetBuffer();
259 DALI_LOG_ERROR("Pixel buffer create to fail [%p]\n", this);
265 mTvgCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), size.width, size.width, size.height, tvg::SwCanvas::ABGR8888);
269 } // namespace Adaptor
271 } // namespace Internal