Update BitmapLoader to use PixelData 38/50438/12
authorXiangyin Ma <x1.ma@samsung.com>
Wed, 28 Oct 2015 17:29:55 +0000 (17:29 +0000)
committerXiangyin Ma <x1.ma@samsung.com>
Mon, 9 Nov 2015 10:48:05 +0000 (02:48 -0800)
Change-Id: I2785810d7eeb5c66c1c2de00429d5027826ef6f3

adaptors/common/bitmap-loader-impl.cpp
adaptors/common/bitmap-loader-impl.h
adaptors/devel-api/adaptor-framework/bitmap-loader.cpp
adaptors/devel-api/adaptor-framework/bitmap-loader.h
automated-tests/resources/gallery-small-1.jpg [new file with mode: 0644]
automated-tests/resources/icon-edit.png [new file with mode: 0644]
automated-tests/src/dali-adaptor/CMakeLists.txt
automated-tests/src/dali-adaptor/utc-Dali-BitmapLoader.cpp [new file with mode: 0644]

index 1e6c545..7af4351 100644 (file)
@@ -37,14 +37,13 @@ IntrusivePtr<BitmapLoader> BitmapLoader::New(const std::string& url,
 }
 
 BitmapLoader::BitmapLoader(const std::string& url,
-             ImageDimensions size,
-             FittingMode::Type fittingMode,
-             SamplingMode::Type samplingMode,
-             bool orientationCorrection)
+                           ImageDimensions size,
+                           FittingMode::Type fittingMode,
+                           SamplingMode::Type samplingMode,
+                           bool orientationCorrection)
 : mResourceType( size, fittingMode, samplingMode, orientationCorrection ),
-  mBitmap(NULL),
-  mUrl(url),
-  mIsLoaded( false )
+  mPixelData(),
+  mUrl(url)
 {
 }
 
@@ -56,53 +55,33 @@ void BitmapLoader::Load()
 {
   IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( mResourceType, mUrl );
 
-  mBitmap = static_cast<Integration::Bitmap*>(resource.Get());
-  mIsLoaded = true;
-}
-
-bool BitmapLoader::IsLoaded()
-{
-  return mIsLoaded;
-}
-
-unsigned char* BitmapLoader::GetPixelData() const
-{
-  if( mIsLoaded )
+  if( resource )
   {
-    return mBitmap->GetBuffer();
+    Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>(resource.Get());
+
+    // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
+    // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
+    mPixelData = PixelData::New( bitmap->GetBufferOwnership(),
+                                 bitmap->GetImageWidth(),
+                                 bitmap->GetImageHeight(),
+                                 bitmap->GetPixelFormat(),
+                                 PixelData::FREE);
   }
-
-  return NULL;
 }
 
-unsigned int BitmapLoader::GetImageHeight() const
+bool BitmapLoader::IsLoaded()
 {
-  if( mIsLoaded )
-  {
-    return mBitmap->GetImageHeight();
-  }
-
-  return 0u;
+  return mPixelData ? true : false ;
 }
 
-unsigned int BitmapLoader::GetImageWidth() const
+const std::string& BitmapLoader::GetUrl() const
 {
-  if( mIsLoaded )
-  {
-    return mBitmap->GetImageWidth();
-  }
-
-  return 0u;
+  return mUrl;
 }
 
-Pixel::Format BitmapLoader::GetPixelFormat() const
+PixelDataPtr BitmapLoader::GetPixelData() const
 {
-  if( mIsLoaded )
-  {
-    return mBitmap->GetPixelFormat();
-  }
-
-  return Pixel::RGBA8888;
+  return mPixelData;
 }
 
 } // namespace Internal
index 143fef1..cde518b 100644 (file)
@@ -74,34 +74,19 @@ public:
   bool IsLoaded();
 
   /**
-   * Get the raw pixel data.
-   * @return The pixel data. Use the GetHeight(), GetWidth(), GetStride() and GetPixelFormat() methods
-   * to decode the data.
+   * @copydoc Dali::BitmapLoader::GetUrl()
    */
-  unsigned char* GetPixelData() const;
+  const std::string& GetUrl() const;
 
   /**
-   * Get the buffer height in pixels
-   * @return the height of the buffer in pixels
+   * @copydoc Dali::BitmapLoader::GetPixelData
    */
-  unsigned int GetImageHeight() const;
-
-  /**
-   * Get the buffer width in pixels
-   * @return the width of the buffer in pixels
-   */
-  unsigned int GetImageWidth() const;
-
-  /**
-   * Get the pixel format of the loaded bitmap.
-   */
-  Pixel::Format GetPixelFormat() const;
+  PixelDataPtr GetPixelData() const;
 
 private:
   Integration::BitmapResourceType mResourceType;
-  Integration::BitmapPtr mBitmap;
+  PixelDataPtr mPixelData;
   const std::string mUrl;
-  bool mIsLoaded;
 };
 
 } // Internal
index dabfbd1..6f25a81 100644 (file)
@@ -70,24 +70,14 @@ bool BitmapLoader::IsLoaded()
   return GetImplementation(*this).IsLoaded();
 }
 
-unsigned char* BitmapLoader::GetPixelData() const
+std::string BitmapLoader::GetUrl() const
 {
-  return GetImplementation(*this).GetPixelData();
-}
-
-unsigned int BitmapLoader::GetImageHeight() const
-{
-  return GetImplementation(*this).GetImageHeight();
+  return GetImplementation(*this).GetUrl();
 }
 
-unsigned int BitmapLoader::GetImageWidth() const
+PixelDataPtr BitmapLoader::GetPixelData() const
 {
-  return GetImplementation(*this).GetImageWidth();
-}
-
-Pixel::Format BitmapLoader::GetPixelFormat() const
-{
-  return GetImplementation(*this).GetPixelFormat();
+  return GetImplementation(*this).GetPixelData();
 }
 
 } // namespace Dali
index af5ac92..9dba69a 100644 (file)
@@ -23,6 +23,7 @@
 #include <dali/public-api/images/pixel.h>
 #include <dali/public-api/images/image-operations.h>
 #include <dali/public-api/object/base-handle.h>
+#include <dali/devel-api/images/pixel-data.h>
 
 namespace Dali
 {
@@ -101,28 +102,20 @@ public:
   bool IsLoaded();
 
   /**
-   * Get the raw pixel data.
-   * @return The pixel data. Use the GetHeight(), GetWidth(), GetStride() and GetPixelFormat() methods
-   * to decode the data.
-   */
-  unsigned char* GetPixelData() const;
-
-  /**
-   * Get the buffer height in pixels
-   * @return the height of the buffer in pixels
-   */
-  unsigned int GetImageHeight() const;
-
-  /**
-   * Get the buffer width in pixels
-   * @return the width of the buffer in pixels
+   * @brief Returns the URL of the image.
+   *
+   * @return The URL of the image file.
    */
-  unsigned int GetImageWidth() const;
+  std::string GetUrl() const;
 
   /**
-   * Get the pixel format of the loaded bitmap.
+   * @brief Get the pixel data.
+   *
+   * The returned pixel data is still valid after the BitmapLoader been destroyed.
+   *
+   * @return The pixel data.
    */
-  Pixel::Format GetPixelFormat() const;
+  PixelDataPtr GetPixelData() const;
 
 public: // Not intended for application developers
 
diff --git a/automated-tests/resources/gallery-small-1.jpg b/automated-tests/resources/gallery-small-1.jpg
new file mode 100644 (file)
index 0000000..9292310
Binary files /dev/null and b/automated-tests/resources/gallery-small-1.jpg differ
diff --git a/automated-tests/resources/icon-edit.png b/automated-tests/resources/icon-edit.png
new file mode 100644 (file)
index 0000000..ce3e327
Binary files /dev/null and b/automated-tests/resources/icon-edit.png differ
index f711d28..1591037 100644 (file)
@@ -13,6 +13,7 @@ SET(TC_SOURCES
     utc-Dali-TtsPlayer.cpp
     utc-Dali-Application.cpp
     utc-Dali-FileLoader.cpp
+    utc-Dali-BitmapLoader.cpp
 )
 
 LIST(APPEND TC_SOURCES
diff --git a/automated-tests/src/dali-adaptor/utc-Dali-BitmapLoader.cpp b/automated-tests/src/dali-adaptor/utc-Dali-BitmapLoader.cpp
new file mode 100644 (file)
index 0000000..46ae347
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2015 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdlib.h>
+#include <dali/dali.h>
+#include <dali-test-suite-utils.h>
+#include <dali/devel-api/adaptor-framework/bitmap-loader.h>
+
+using namespace Dali;
+
+namespace
+{
+// resolution: 34*34, pixel format: RGBA8888
+static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
+// resolution: 128*128, pixel format: RGB888
+static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
+
+// this is image is not exist, for negative test
+static const char* gImageNonExist = "non-exist.jpg";
+}
+
+void utc_dali_bitmap_loader_startup(void)
+{
+  test_return_value = TET_UNDEF;
+}
+
+void utc_dali_bitmap_loader_cleanup(void)
+{
+  test_return_value = TET_PASS;
+}
+
+int UtcDaliBitmapLoaderNew(void)
+{
+  TestApplication application;
+
+  // invoke default handle constructor
+  BitmapLoader loader;
+
+  DALI_TEST_CHECK( !loader );
+
+  // initialise handle
+  loader = BitmapLoader::New( gImage_34_RGBA );
+
+  DALI_TEST_CHECK( loader );
+  END_TEST;
+}
+
+int UtcDaliBitmapLoaderCopyConstructor(void)
+{
+  TestApplication application;
+
+  BitmapLoader loader = BitmapLoader::New( gImage_34_RGBA);
+  BitmapLoader loaderCopy(loader);
+
+  DALI_TEST_EQUALS( (bool)loaderCopy, true, TEST_LOCATION );
+  END_TEST;
+}
+
+int UtcDaliBitmapLoaderAssignmentOperator(void)
+{
+  TestApplication application;
+
+  BitmapLoader loader = BitmapLoader::New( gImage_34_RGBA );
+
+  BitmapLoader loader2;
+  DALI_TEST_EQUALS( (bool)loader2, false, TEST_LOCATION );
+
+  loader2 = loader;
+  DALI_TEST_EQUALS( (bool)loader2, true, TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliBitmapLoaderGetUrl(void)
+{
+  TestApplication application;
+
+  BitmapLoader loader = BitmapLoader::New( gImage_34_RGBA );
+  DALI_TEST_CHECK( loader.GetUrl() == gImage_34_RGBA );
+
+  END_TEST;
+}
+
+
+int UtcDaliBitmapLoaderLoadP(void)
+{
+  TestApplication application;
+
+  BitmapLoader loader1 = BitmapLoader::New( gImage_34_RGBA );
+  DALI_TEST_CHECK( ! loader1.IsLoaded() );
+  loader1.Load();
+  DALI_TEST_CHECK( loader1.IsLoaded() );
+  PixelDataPtr pixelData1 = loader1.GetPixelData();
+  DALI_TEST_CHECK( pixelData1 );
+  DALI_TEST_CHECK( pixelData1->GetWidth() == 34u );
+  DALI_TEST_CHECK( pixelData1->GetHeight() == 34u );
+  DALI_TEST_CHECK( pixelData1->GetPixelFormat() == Pixel::RGBA8888 );
+
+  BitmapLoader loader2 = BitmapLoader::New( gImage_128_RGB );
+  DALI_TEST_CHECK( ! loader2.IsLoaded() );
+  loader2.Load();
+  DALI_TEST_CHECK( loader2.IsLoaded() );
+  PixelDataPtr pixelData2 = loader2.GetPixelData();
+  DALI_TEST_CHECK( pixelData2 );
+  DALI_TEST_CHECK( pixelData2->GetWidth() == 128u );
+  DALI_TEST_CHECK( pixelData2->GetHeight() == 128u );
+  DALI_TEST_CHECK( pixelData2->GetPixelFormat() == Pixel::RGB888 );
+
+  END_TEST;
+}
+
+int UtcDaliBitmapLoaderLoadN(void)
+{
+  TestApplication application;
+
+  BitmapLoader loader = BitmapLoader::New( gImageNonExist );
+  DALI_TEST_CHECK( ! loader.IsLoaded() );
+  loader.Load();
+
+  // cannot load image that is not exist
+  DALI_TEST_CHECK( ! loader.IsLoaded() );
+  PixelDataPtr pixelData = loader.GetPixelData();
+  DALI_TEST_CHECK( !pixelData);
+
+  END_TEST;
+}