2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/images/image-impl.h>
21 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/platform-abstraction.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/internal/event/resources/resource-ticket.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/resources/resource-client.h>
28 #include <dali/internal/event/images/image-factory.h>
29 #include <dali/internal/event/images/nine-patch-image-impl.h>
30 #include <dali/internal/event/common/stage-impl.h>
32 using namespace Dali::Integration;
40 Image::Image( LoadPolicy loadPol, ReleasePolicy releasePol )
44 mReleasePolicy(releasePol),
46 mImageFactory(ThreadLocalStorage::Get().GetImageFactory())
55 Image* Image::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
57 if( IsNinePatchFileName(filename) )
59 NinePatchImage* image = new NinePatchImage( filename, attributes, loadPol, releasePol );
64 Image* image = new Image( loadPol, releasePol );
66 if( ! filename.empty() )
70 Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( filename, attributes, closestSize );
71 image->mWidth = closestSize.width;
72 image->mHeight = closestSize.height;
75 image->mRequest = image->mImageFactory.RegisterRequest( filename, &attributes );
77 if( Dali::Image::Immediate == loadPol )
79 // Trigger loading of the image on a seperate resource thread as soon as it
81 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
82 image->mTicket->AddObserver( *image );
84 // else lazily load image data later, only when it is needed to draw something:
86 DALI_LOG_SET_OBJECT_STRING( image, filename );
92 Image* Image::New( NativeImage& nativeImg, LoadPolicy loadPol, ReleasePolicy releasePol )
94 Image* image = new Image;
95 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
98 image->mWidth = nativeImg.GetWidth();
99 image->mHeight = nativeImg.GetHeight();
101 const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
102 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
103 image->mTicket = static_cast<ImageTicket*>(ticket.Get());
104 image->mTicket->AddObserver( *image );
113 mTicket->RemoveObserver( *this );
114 if( Stage::IsInstalled() )
116 mImageFactory.ReleaseTicket( mTicket.Get() );
121 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
123 bool connected( true );
124 DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
125 Image* image = static_cast<Image*>(object);
127 if( Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
129 image->LoadingFinishedSignal().Connect( tracker, functor );
131 else if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
133 image->UploadedSignal().Connect( tracker, functor );
137 // signalName does not match any signal
144 ResourceId Image::GetResourceId() const
146 ResourceId ret = mTicket ? mTicket->GetId() : 0;
151 const Dali::ImageAttributes& Image::GetAttributes() const
155 return mImageFactory.GetActualAttributes( mTicket->GetId() );
159 return mImageFactory.GetRequestAttributes( mRequest.Get() );
163 const std::string& Image::GetFilename() const
165 return mImageFactory.GetRequestPath( mRequest.Get() );
172 ResourceTicketPtr ticket = mImageFactory.Reload( mRequest.Get() );
173 SetTicket( ticket.Get() );
177 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
179 mLoadingFinishedV2.Emit( Dali::Image( this ) );
182 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
184 // Update size with actual loaded size
185 const ImageTicket* imageTicket = static_cast<const ImageTicket*>(&ticket);
186 mWidth = imageTicket->GetWidth();
187 mHeight = imageTicket->GetHeight();
188 mLoadingFinishedV2.Emit( Dali::Image( this ) );
191 void Image::ResourceUploaded(const ResourceTicket& ticket)
193 mUploadedV2.Emit( Dali::Image( this ) );
196 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
201 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
206 unsigned int Image::GetWidth() const
208 // Width has already been calculated - just return that
212 unsigned int Image::GetHeight() const
214 // Height has already been calculated - just return that
218 Vector2 Image::GetNaturalSize() const
220 return Vector2( mWidth, mHeight );
223 void Image::Connect()
227 if( mConnectionCount == 1 )
229 // ticket was thrown away when related actors went offstage or image loading on demand
232 ResourceTicketPtr newTicket = mImageFactory.Load( mRequest.Get() );
233 SetTicket( newTicket.Get() );
238 void Image::Disconnect()
245 DALI_ASSERT_DEBUG( mConnectionCount > 0 );
247 if( mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused )
249 // release image memory when it's not visible anymore (decrease ref. count of texture)
254 void Image::SetTicket( ResourceTicket* ticket )
256 if( ticket == mTicket.Get() )
263 mTicket->RemoveObserver( *this );
264 mImageFactory.ReleaseTicket( mTicket.Get() );
269 mTicket.Reset( ticket );
270 mTicket->AddObserver( *this );
278 bool Image::IsNinePatchFileName( std::string filename )
282 std::string::const_reverse_iterator iter = filename.rbegin();
283 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
284 while(iter < filename.rend())
294 else if(!isalnum(*iter))
302 if( *iter == '#' || *iter == '9' )
318 state = DONE; // Stop testing characters
339 } // namespace Internal