Merge "Size negotiation patch 3: Scope size negotiation enums" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / images / encoded-buffer-image-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/encoded-buffer-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memcpy
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/resources/resource-client.h>
28 #include <dali/integration-api/platform-abstraction.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34
35 namespace
36 {
37 TypeRegistration mType( typeid( Dali::EncodedBufferImage ), typeid( Dali::Image ), NULL );
38 } // unnamed namespace
39
40 EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImage,
41                                                const std::size_t encodedImageByteCount,
42                                                const ImageAttributes& attributes,
43                                                const ReleasePolicy releasePol )
44 {
45   DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
46   DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
47   DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
48   // SEGV early before we allocate anything if the caller passed in an invalid
49   // input buffer by reading both ends of it:
50   DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
51
52   EncodedBufferImagePtr image( new EncodedBufferImage( releasePol ) );
53   image->Initialize(); // Second stage initialization
54
55   // Replicate the functionality of ImageFactory::load() without the filesystem caching:
56   Dali::Integration::BitmapResourceType resourceType( attributes );
57   RequestBufferPtr buffer( new RequestBuffer );
58   buffer->GetVector().Resize( encodedImageByteCount );
59   // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
60   DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
61
62   memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
63
64   // Get image size from buffer
65   Vector2 size;
66   Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( buffer, attributes, size );
67   image->mWidth = (unsigned int) size.width;
68   image->mHeight = (unsigned int) size.height;
69
70   ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
71   ResourceTicketPtr ticket = resourceClient.DecodeResource( resourceType, buffer );
72   if( ticket )
73   {
74     DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket returned for image resource has to be an ImageTicket subclass.\n" );
75     ImageTicket * const imageTicket = static_cast<ImageTicket*>( ticket.Get() );
76
77     image->mTicket = imageTicket;
78     imageTicket->AddObserver( *image );
79   }
80
81   return image;
82 }
83
84 } // namespace Internal
85 } // namespace Dali