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