[Tizen] Revert "Remove TypeRegistration from deprecated Image classes"
[platform/core/uifw/dali-core.git] / dali / internal / event / images / encoded-buffer-image-impl.cpp
index 51ad81b..d6d5de0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
@@ -23,8 +23,8 @@
 
 // INTERNAL INCLUDES
 #include <dali/public-api/object/type-registry.h>
+#include <dali/devel-api/common/ref-counted-dali-vector.h>
 #include <dali/internal/event/common/thread-local-storage.h>
-#include <dali/internal/event/resources/resource-client.h>
 #include <dali/integration-api/platform-abstraction.h>
 
 namespace Dali
@@ -34,13 +34,20 @@ namespace Internal
 
 namespace
 {
+
 TypeRegistration mType( typeid( Dali::EncodedBufferImage ), typeid( Dali::Image ), NULL );
+
+/** Raw bytes of a resource laid out exactly as it would be in a file, but in memory. */
+typedef Dali::RefCountedVector<uint8_t> RequestBuffer;
+/** Counting smart pointer for managing a buffer of raw bytes. */
+typedef IntrusivePtr<RequestBuffer>     RequestBufferPtr;
+
 } // unnamed namespace
 
 EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImage,
-                                               const std::size_t encodedImageByteCount,
-                                               const ImageAttributes& attributes,
-                                               const ReleasePolicy releasePol )
+                                               std::size_t encodedImageByteCount,
+                                               ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
+                                               bool orientationCorrection )
 {
   DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
   DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
@@ -49,11 +56,10 @@ EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImag
   // input buffer by reading both ends of it:
   DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
 
-  EncodedBufferImagePtr image( new EncodedBufferImage( releasePol ) );
+  EncodedBufferImagePtr image( new EncodedBufferImage() );
   image->Initialize(); // Second stage initialization
 
-  // Replicate the functionality of ImageFactory::load() without the filesystem caching:
-  Dali::Integration::BitmapResourceType resourceType( attributes );
+  Dali::Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
   RequestBufferPtr buffer( new RequestBuffer );
   buffer->GetVector().Resize( encodedImageByteCount );
   // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
@@ -62,20 +68,45 @@ EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImag
   memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
 
   // Get image size from buffer
-  Vector2 size;
-  Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( buffer, attributes, size );
-  image->mWidth = (unsigned int) size.width;
-  image->mHeight = (unsigned int) size.height;
-
-  ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
-  ResourceTicketPtr ticket = resourceClient.DecodeResource( resourceType, buffer );
-  if( ticket )
+  Dali::Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
+  const ImageDimensions expectedSize = platformAbstraction.GetClosestImageSize( buffer, size, fittingMode, samplingMode, orientationCorrection );
+  image->mWidth = static_cast<unsigned int>( expectedSize.GetWidth() );
+  image->mHeight = static_cast<unsigned int>( expectedSize.GetHeight() );
+
+  // Load the image synchronously
+  Integration::BitmapPtr bitmap = platformAbstraction.DecodeBuffer( resourceType, &(buffer->GetVector()[0]), encodedImageByteCount );
+
+  if( bitmap )
   {
-    DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket returned for image resource has to be an ImageTicket subclass.\n" );
-    ImageTicket * const imageTicket = static_cast<ImageTicket*>( ticket.Get() );
+    unsigned width  = bitmap->GetImageWidth();
+    unsigned height = bitmap->GetImageHeight();
 
-    image->mTicket = imageTicket;
-    imageTicket->AddObserver( *image );
+    //Create texture
+    Pixel::Format format = bitmap->GetPixelFormat();
+    image->mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
+
+    //Upload data to the texture
+    uint32_t bufferSize = bitmap->GetBufferSize();
+    PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
+                                             static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
+    image->mTexture->Upload( pixelData );
+
+    image->mWidth = size.GetWidth();
+    if( image->mWidth == 0 )
+    {
+      image->mWidth = width;
+    }
+
+    image->mHeight = size.GetHeight();
+    if( image->mHeight == 0 )
+    {
+      image->mHeight = height;
+    }
+  }
+  else
+  {
+    image->mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
+    image->mWidth = image->mHeight = 0u;
   }
 
   return image;