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