Change VectorImage method 54/274254/2
authorHeeyong Song <heeyong.song@samsung.com>
Mon, 25 Apr 2022 09:39:49 +0000 (18:39 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Mon, 2 May 2022 02:56:31 +0000 (11:56 +0900)
Change-Id: Ia0136d682fb518cd2f1b1ab50f6b62a67c6a4681

dali/devel-api/adaptor-framework/vector-image-renderer.cpp
dali/devel-api/adaptor-framework/vector-image-renderer.h
dali/internal/vector-image/common/vector-image-renderer-impl.cpp
dali/internal/vector-image/common/vector-image-renderer-impl.h

index f557cad..bf69063 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -47,9 +47,9 @@ bool VectorImageRenderer::Load(const Vector<uint8_t>& data, float dpi)
   return GetImplementation(*this).Load(data, dpi);
 }
 
-bool VectorImageRenderer::Rasterize(Dali::Devel::PixelBuffer& buffer, float scale)
+Dali::Devel::PixelBuffer VectorImageRenderer::Rasterize(uint32_t width, uint32_t height)
 {
-  return GetImplementation(*this).Rasterize(buffer, scale);
+  return GetImplementation(*this).Rasterize(width, height);
 }
 
 void VectorImageRenderer::GetDefaultSize(uint32_t& width, uint32_t& height) const
index d2343d6..f11220d 100644 (file)
@@ -2,7 +2,7 @@
 #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.
@@ -86,13 +86,13 @@ public:
   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.
index 90f24f4..deec3c4 100644 (file)
@@ -46,6 +46,10 @@ Dali::BaseHandle Create()
 
 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()
@@ -60,9 +64,7 @@ VectorImageRendererPtr VectorImageRenderer::New()
 
 VectorImageRenderer::VectorImageRenderer()
 #ifdef THORVG_SUPPORT
-: mPicture(nullptr),
-  mDefaultWidth(0),
-  mDefaultHeight(0)
+: mPicture(nullptr)
 #else
 : mParsedImage(nullptr),
   mRasterizer(nullptr)
@@ -174,34 +176,63 @@ bool VectorImageRenderer::Load(const Vector<uint8_t>& data, float dpi)
     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);
 
@@ -209,42 +240,40 @@ bool VectorImageRenderer::Rasterize(Dali::Devel::PixelBuffer& buffer, float scal
   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
index f2cce44..ccc9301 100644 (file)
@@ -2,7 +2,7 @@
 #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.
@@ -64,7 +64,7 @@ public:
   /**
    * @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()
@@ -94,14 +94,14 @@ private:
 
 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