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/common/stage-impl.h>
31 using namespace Dali::Integration;
39 Image::Image( LoadPolicy loadPol, ReleasePolicy releasePol )
43 mReleasePolicy(releasePol),
45 mImageFactory(ThreadLocalStorage::Get().GetImageFactory())
54 Image* Image::New( const std::string& filename, LoadPolicy loadPol, ReleasePolicy releasePol )
56 Image* image = new Image( loadPol, releasePol );
58 image->mRequest = image->mImageFactory.RegisterRequest( filename, NULL );
60 if( ! filename.empty() )
63 Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().LoadImageMetadata( filename, size );
64 image->mWidth = (int)size.width;
65 image->mHeight = (int)size.height;
68 if( Dali::Image::OnDemand == loadPol )
70 // load image on demand,
71 // ask for ticket on connect
75 // load image immediately
76 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
77 image->mTicket->AddObserver( *image );
80 DALI_LOG_SET_OBJECT_STRING( image, filename );
85 Image* Image::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
87 Image* image = new Image( loadPol, releasePol );
89 if( ! filename.empty() )
91 if( attributes.GetWidth() == 0 && attributes.GetHeight() == 0 )
93 // Only get actual file size if a requested size is not present.
95 Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().LoadImageMetadata( filename, size );
96 image->mWidth = (int)size.width;
97 image->mHeight = (int)size.height;
99 // @TODO: else, determine closest size to requested size
102 image->mRequest = image->mImageFactory.RegisterRequest( filename, &attributes );
104 // Lazily load image data later, only when it is needed to draw something:
105 if( Dali::Image::OnDemand == loadPol )
107 // Ask for ticket later, only on connect.
111 // Trigger loading of the image over on a resource thread eagerly as soon as it
113 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
114 image->mTicket->AddObserver( *image );
117 DALI_LOG_SET_OBJECT_STRING( image, filename );
122 Image* Image::New( NativeImage& nativeImg, LoadPolicy loadPol, ReleasePolicy releasePol )
124 Image* image = new Image;
125 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
128 image->mWidth = nativeImg.GetWidth();
129 image->mHeight = nativeImg.GetHeight();
131 const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
132 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
133 image->mTicket = static_cast<ImageTicket*>(ticket.Get());
134 image->mTicket->AddObserver( *image );
143 mTicket->RemoveObserver( *this );
144 if( Stage::IsInstalled() )
146 mImageFactory.ReleaseTicket( mTicket.Get() );
151 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
153 bool connected( true );
154 DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
155 Image* image = static_cast<Image*>(object);
157 if( Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
159 image->LoadingFinishedSignal().Connect( tracker, functor );
161 else if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
163 image->UploadedSignal().Connect( tracker, functor );
167 // signalName does not match any signal
174 ResourceId Image::GetResourceId() const
176 ResourceId ret = mTicket ? mTicket->GetId() : 0;
181 const Dali::ImageAttributes& Image::GetAttributes() const
185 return mImageFactory.GetActualAttributes( mTicket->GetId() );
189 return mImageFactory.GetRequestAttributes( mRequest.Get() );
193 const std::string& Image::GetFilename() const
195 return mImageFactory.GetRequestPath( mRequest.Get() );
202 ResourceTicketPtr ticket = mImageFactory.Reload( mRequest.Get() );
203 SetTicket( ticket.Get() );
207 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
209 mLoadingFinishedV2.Emit( Dali::Image( this ) );
212 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
214 // Update size with actual loaded size
215 const ImageTicket* imageTicket = static_cast<const ImageTicket*>(&ticket);
216 mWidth = imageTicket->GetWidth();
217 mHeight = imageTicket->GetHeight();
218 mLoadingFinishedV2.Emit( Dali::Image( this ) );
221 void Image::ResourceUploaded(const ResourceTicket& ticket)
223 mUploadedV2.Emit( Dali::Image( this ) );
226 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
231 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
236 unsigned int Image::GetWidth() const
240 const ImageAttributes& attr = mImageFactory.GetActualAttributes( mTicket->GetId() );
241 return attr.GetWidth();
245 const ImageAttributes& attr = mImageFactory.GetRequestAttributes( mRequest.Get() );
246 return attr.GetWidth();
254 unsigned int Image::GetHeight() const
258 const ImageAttributes& attr = mImageFactory.GetActualAttributes( mTicket->GetId() );
259 return attr.GetHeight();
263 const ImageAttributes& attr = mImageFactory.GetRequestAttributes( mRequest.Get() );
264 return attr.GetHeight();
272 void Image::Connect()
276 if( mConnectionCount == 1 )
278 // ticket was thrown away when related actors went offstage or image loading on demand
281 ResourceTicketPtr newTicket = mImageFactory.Load( mRequest.Get() );
282 SetTicket( newTicket.Get() );
287 void Image::Disconnect()
294 DALI_ASSERT_DEBUG( mConnectionCount > 0 );
296 if( mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused )
298 // release image memory when it's not visible anymore (decrease ref. count of texture)
303 void Image::SetTicket( ResourceTicket* ticket )
305 if( ticket == mTicket.Get() )
312 mTicket->RemoveObserver( *this );
313 mImageFactory.ReleaseTicket( mTicket.Get() );
318 mTicket.Reset( ticket );
319 mTicket->AddObserver( *this );
327 } // namespace Internal