Merge "[Tizen] Prepare for Tizen 4.0 Build" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.cpp
1 /*
2  * Copyright (c) 2016 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/resource-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/event/images/nine-patch-image-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/resources/resource-client.h>
33
34 using namespace Dali::Integration;
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44
45 // Signals
46
47 const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
48
49
50 BaseHandle CreateImage()
51 {
52   ImagePtr image = ResourceImage::New();
53   return Dali::Image(image.Get());
54 }
55
56 TypeRegistration mType( typeid( Dali::ResourceImage ), typeid( Dali::Image ), CreateImage );
57
58 Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal );
59
60 }
61
62 ResourceImage::ResourceImage()
63 : Image(),
64   mLoadingFinished(),
65   mAttributes(),
66   mUrl(),
67   mLoadingState( Dali::ResourceLoading )
68 {
69 }
70
71 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
72 : Image(),
73   mLoadingFinished(),
74   mAttributes(attributes),
75   mUrl(url),
76   mLoadingState( Dali::ResourceLoading )
77 {
78 }
79
80 ResourceImagePtr ResourceImage::New()
81 {
82   ResourceImagePtr image = new ResourceImage;
83   image->Initialize();
84   return image;
85 }
86
87 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
88 {
89   ResourceImagePtr image;
90
91   if( NinePatchImage::IsNinePatchUrl( url ) )
92   {
93     image = NinePatchImage::New( url );
94   }
95   else
96   {
97     image = new ResourceImage(url, attributes);
98     image->Initialize();
99     image->Reload();
100   }
101
102   DALI_LOG_SET_OBJECT_STRING( image, url );
103
104   return image;
105 }
106
107 ResourceImage::~ResourceImage()
108 {
109 }
110
111 bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
112 {
113   bool connected( true );
114   DALI_ASSERT_DEBUG( dynamic_cast<ResourceImage*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
115   ResourceImage* image = static_cast<ResourceImage*>(object);
116
117   if( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
118   {
119     image->LoadingFinishedSignal().Connect( tracker, functor );
120   }
121   else
122   {
123     // signalName does not match any signal
124     connected = false;
125   }
126
127   return connected;
128 }
129
130
131 const ImageAttributes& ResourceImage::GetAttributes() const
132 {
133   return mAttributes;
134 }
135
136 const std::string& ResourceImage::GetUrl() const
137 {
138   return mUrl;
139 }
140
141 void ResourceImage::Reload()
142 {
143   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
144   Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
145   Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
146                                                 mAttributes.GetScalingMode(),
147                                                 mAttributes.GetFilterMode(),
148                                                 mAttributes.GetOrientationCorrection() );
149
150   // Note, bitmap is only destroyed when the image is destroyed.
151   Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously( resourceType, mUrl );
152   if( resource )
153   {
154     Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
155     unsigned width  = bitmap->GetImageWidth();
156     unsigned height = bitmap->GetImageHeight();
157
158     //Create texture
159     Pixel::Format format = bitmap->GetPixelFormat();
160     mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
161
162     //Upload data to the texture
163     size_t bufferSize = bitmap->GetBufferSize();
164     PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
165                                              static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
166     mTexture->Upload( pixelData );
167
168     mWidth = mAttributes.GetWidth();
169     if( mWidth == 0 )
170     {
171       mWidth = width;
172     }
173
174     mHeight = mAttributes.GetHeight();
175     if( mHeight == 0 )
176     {
177       mHeight = height;
178     }
179
180     mLoadingState = Dali::ResourceLoadingSucceeded;
181
182   }
183   else
184   {
185     mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
186     mWidth = mHeight = 0u;
187     mLoadingState = Dali::ResourceLoadingFailed;
188   }
189
190   mLoadingFinished.Emit( Dali::ResourceImage( this ) );
191 }
192
193 unsigned int ResourceImage::GetWidth() const
194 {
195   return mWidth;
196 }
197
198 unsigned int ResourceImage::GetHeight() const
199 {
200   return mHeight;
201 }
202
203 Vector2 ResourceImage::GetNaturalSize() const
204 {
205   return Vector2(mWidth, mHeight);
206 }
207
208
209 } // namespace Internal
210
211 } // namespace Dali