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