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),
64 CanvasRendererTizen::~CanvasRendererTizen()
66 for(DrawableVectorIterator it = mDrawables.begin(),
67 endIt = mDrawables.end();
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);
83 void CanvasRendererTizen::Initialize(const Vector2& viewBox)
85 if(tvg::Initializer::init(tvg::CanvasEngine::Sw, 0 /*threads*/) != tvg::Result::Success)
87 DALI_LOG_ERROR("ThorVG engine initialize failed\n");
89 mTvgCanvas = tvg::SwCanvas::gen();
91 mSize = mViewBox = viewBox;
92 if(viewBox.width < 1.0f || viewBox.height < 1.0f)
97 MakeTargetBuffer(mSize);
99 auto scene = tvg::Scene::gen();
100 mTvgRoot = scene.get();
101 mTvgCanvas->push(move(scene));
104 bool CanvasRendererTizen::Commit()
106 bool changed = false;
108 for(DrawableVectorIterator it = mDrawables.begin(),
109 endIt = mDrawables.end();
113 Dali::CanvasRenderer::Drawable drawable = (*it).GetHandle();
114 if(DALI_UNLIKELY(!drawable))
118 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
119 if(drawableImpl.GetChanged())
122 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);
162 Devel::PixelBuffer CanvasRendererTizen::GetPixelBuffer()
167 bool CanvasRendererTizen::AddDrawable(Dali::CanvasRenderer::Drawable& drawable)
170 for(DrawableVectorIterator it = mDrawables.begin(),
171 endIt = mDrawables.end();
175 if((*it) == drawable)
183 DALI_LOG_ERROR("Already added [%p]\n", this);
187 Internal::Adaptor::Drawable& drawableImpl = GetImplementation(drawable);
188 tvg::Paint* pDrawable = static_cast<tvg::Paint*>(drawableImpl.GetObject());
191 DALI_LOG_ERROR("Invalid drawable object [%p]\n", this);
194 if(mSize.width < 1.0f || mSize.height < 1.0f)
196 DALI_LOG_ERROR("Size is zero [%p]\n", this);
200 if(mTvgRoot->push(std::unique_ptr<tvg::Paint>(pDrawable)) != tvg::Result::Success)
202 DALI_LOG_ERROR("Tvg push fail [%p]\n", this);
206 drawableImpl.SetDrawableAdded(true);
207 mDrawables.push_back(drawable);
213 bool CanvasRendererTizen::SetSize(const Vector2& size)
215 if(size.width < 1.0f || size.height < 1.0f)
223 MakeTargetBuffer(size);
231 const Vector2& CanvasRendererTizen::GetSize()
236 void CanvasRendererTizen::MakeTargetBuffer(const Vector2& size)
238 mPixelBuffer = Devel::PixelBuffer::New(size.width, size.height, Dali::Pixel::RGBA8888);
240 unsigned char* pBuffer;
241 pBuffer = mPixelBuffer.GetBuffer();
245 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);
254 } // namespace Adaptor
256 } // namespace Internal