#define DALI_VECTOR_IMAGE_RENDERER_H
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
bool Load(const Vector<uint8_t>& data, float dpi);
/**
- * @brief Rasterizes the content to the target buffer synchronously.
+ * @brief Rasterizes the content to the pixel buffer synchronously.
*
- * @param[in] buffer The target buffer
- * @param[in] scale The target image scale factor
- * @return True if the rendering succeeds, false otherwise.
+ * @param[in] width The pixel buffer width
+ * @param[in] height The pixel buffer height
+ * @return The handle to the rasterized PixelBuffer object or an empty handle in case failed.
*/
- bool Rasterize(Dali::Devel::PixelBuffer& buffer, float scale);
+ Dali::Devel::PixelBuffer Rasterize(uint32_t width, uint32_t height);
/**
* @brief Gets the default size of the file.
Dali::TypeRegistration type(typeid(Dali::VectorImageRenderer), typeid(Dali::BaseHandle), Create);
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gVectorImageLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_IMAGE");
+#endif
+
} // unnamed namespace
VectorImageRendererPtr VectorImageRenderer::New()
VectorImageRenderer::VectorImageRenderer()
#ifdef THORVG_SUPPORT
-: mPicture(nullptr),
- mDefaultWidth(0),
- mDefaultHeight(0)
+: mPicture(nullptr)
#else
: mParsedImage(nullptr),
mRasterizer(nullptr)
DALI_LOG_ERROR("VectorImageRenderer::Load: nsvgParse failed\n");
return false;
}
+
+ mDefaultWidth = mParsedImage->width;
+ mDefaultHeight = mParsedImage->height;
+
return true;
#endif
}
-bool VectorImageRenderer::Rasterize(Dali::Devel::PixelBuffer& buffer, float scale)
+Dali::Devel::PixelBuffer VectorImageRenderer::Rasterize(uint32_t width, uint32_t height)
{
+ if(width == 0)
+ {
+ if(mDefaultWidth == 0)
+ {
+ DALI_LOG_ERROR("Invalid size [%d, %d]\n", width, height);
+ return Devel::PixelBuffer();
+ }
+ else
+ {
+ width = mDefaultWidth;
+ }
+ }
+
+ if(height == 0)
+ {
+ if(mDefaultHeight == 0)
+ {
+ DALI_LOG_ERROR("Invalid size [%d, %d]\n", width, height);
+ return Devel::PixelBuffer();
+ }
+ else
+ {
+ height = mDefaultHeight;
+ }
+ }
+
#ifdef THORVG_SUPPORT
if(!mSwCanvas || !mPicture)
{
DALI_LOG_ERROR("VectorImageRenderer::Rasterize: either Canvas[%p] or Picture[%p] is invalid [%p]\n", mSwCanvas.get(), mPicture, this);
- return false;
+ return Devel::PixelBuffer();
}
+ Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(width, height, Dali::Pixel::RGBA8888);
+
mSwCanvas->clear(false);
- auto pBuffer = buffer.GetBuffer();
+ auto pBuffer = pixelBuffer.GetBuffer();
if(!pBuffer)
{
DALI_LOG_ERROR("VectorImageRenderer::Rasterize: pixel buffer is null [%p]\n", this);
- return false;
+ return Devel::PixelBuffer();
}
- auto width = buffer.GetWidth();
- auto height = buffer.GetHeight();
-
mSwCanvas->target(reinterpret_cast<uint32_t*>(pBuffer), width, width, height, tvg::SwCanvas::ABGR8888);
- DALI_LOG_RELEASE_INFO("VectorImageRenderer::Rasterize: Buffer[%p] size[%d x %d]! [%p]\n", pBuffer, width, height, this);
+ DALI_LOG_INFO(gVectorImageLogFilter, Debug::Verbose, "Buffer[%p] size[%d x %d]! [%p]\n", pBuffer, width, height, this);
mPicture->size(width, height);
if(mSwCanvas->push(std::unique_ptr<tvg::Picture>(mPicture)) != tvg::Result::Success)
{
DALI_LOG_ERROR("VectorImageRenderer::Rasterize: Picture push fail [%p]\n", this);
- return false;
+ return Devel::PixelBuffer();
}
auto ret = mSwCanvas->draw();
if(ret != tvg::Result::Success)
{
DALI_LOG_ERROR("VectorImageRenderer::Rasterize: Draw fail %d [%p]\n", static_cast<int>(ret), this);
- return false;
+ return Devel::PixelBuffer();
}
mSwCanvas->sync();
- return true;
+ return pixelBuffer;
#else
if(mParsedImage != nullptr)
{
- int stride = buffer.GetWidth() * Pixel::GetBytesPerPixel(buffer.GetPixelFormat());
- nsvgRasterize(mRasterizer, mParsedImage, 0.0f, 0.0f, scale, buffer.GetBuffer(), buffer.GetWidth(), buffer.GetHeight(), stride);
- return true;
+ Devel::PixelBuffer pixelBuffer = Devel::PixelBuffer::New(width, height, Dali::Pixel::RGBA8888);
+
+ float scaleX = static_cast<float>(width) / (mDefaultWidth > 0 ? static_cast<float>(mDefaultWidth) : 1.0f);
+ float scaleY = static_cast<float>(height) / (mDefaultHeight > 0 ? static_cast<float>(mDefaultHeight) : 1.0f);
+ float scale = scaleX < scaleY ? scaleX : scaleY;
+ int stride = pixelBuffer.GetWidth() * Pixel::GetBytesPerPixel(Dali::Pixel::RGBA8888);
+
+ nsvgRasterize(mRasterizer, mParsedImage, 0.0f, 0.0f, scale, pixelBuffer.GetBuffer(), width, height, stride);
+ return pixelBuffer;
}
- return false;
+ return Devel::PixelBuffer();
#endif
}
void VectorImageRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
{
-#ifdef THORVG_SUPPORT
width = mDefaultWidth;
height = mDefaultHeight;
-#else
- if(mParsedImage)
- {
- width = mParsedImage->width;
- height = mParsedImage->height;
- }
-#endif
}
} // namespace Adaptor
#define DALI_INTERNAL_VECTOR_IMAGE_RENDERER_IMPL_H
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* @copydoc Dali::VectorImageRenderer::Rasterize()
*/
- bool Rasterize(Dali::Devel::PixelBuffer& buffer, float scale);
+ Dali::Devel::PixelBuffer Rasterize(uint32_t width, uint32_t height);
/**
* @copydoc Dali::VectorImageRenderer::GetDefaultSize()
private:
#ifdef THORVG_SUPPORT
- std::unique_ptr< tvg::SwCanvas > mSwCanvas;
- tvg::Picture* mPicture; ///< The pointer to the picture
- uint32_t mDefaultWidth; ///< The width of the surface
- uint32_t mDefaultHeight; ///< The height of the surface
+ std::unique_ptr<tvg::SwCanvas> mSwCanvas{nullptr};
+ tvg::Picture* mPicture{nullptr}; ///< The pointer to the picture
#else
- NSVGimage* mParsedImage;
- NSVGrasterizer* mRasterizer;
+ NSVGimage* mParsedImage{nullptr};
+ NSVGrasterizer* mRasterizer{nullptr};
#endif
+ uint32_t mDefaultWidth{0}; ///< The default width of the file
+ uint32_t mDefaultHeight{0}; ///< The default height of the file
};
} // namespace Adaptor