2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/images/image-impl.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/object/type-registry.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/resources/resource-ticket.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/event/images/image-factory.h>
30 #include <dali/internal/event/images/nine-patch-image-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
33 using namespace Dali::Integration;
44 BaseHandle CreateImage()
46 ImagePtr image = Image::New();
47 return Dali::Image(image.Get());
50 TypeRegistration mType( typeid(Dali::Image), typeid(Dali::BaseHandle), CreateImage );
52 Dali::SignalConnectorType signalConnector1(mType, Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED, &Image::DoConnectSignal);
53 Dali::SignalConnectorType signalConnector2(mType, Dali::Image::SIGNAL_IMAGE_UPLOADED, &Image::DoConnectSignal);
57 Image::Image( LoadPolicy loadPol, ReleasePolicy releasePol )
58 : mImageFactory(ThreadLocalStorage::Get().GetImageFactory()),
63 mReleasePolicy(releasePol)
69 ImagePtr image = new Image;
74 ImagePtr Image::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
77 if( IsNinePatchFileName(filename) )
79 image = NinePatchImage::New( filename, attributes, loadPol, releasePol );
83 image = new Image( loadPol, releasePol );
86 // consider the requested size as natural size, 0 means we don't (yet) know it
87 image->mWidth = attributes.GetWidth();
88 image->mHeight = attributes.GetHeight();
89 image->mRequest = image->mImageFactory.RegisterRequest( filename, &attributes );
91 if( Dali::Image::Immediate == loadPol )
93 // Trigger loading of the image on a as soon as it can be done
94 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
95 image->mTicket->AddObserver( *image );
98 DALI_LOG_SET_OBJECT_STRING( image, filename );
103 ImagePtr Image::New( NativeImage& nativeImg )
105 ImagePtr image = new Image;
108 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
110 image->mWidth = nativeImg.GetWidth();
111 image->mHeight = nativeImg.GetHeight();
113 const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
114 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
115 image->mTicket = static_cast<ImageTicket*>(ticket.Get());
116 image->mTicket->AddObserver( *image );
125 mTicket->RemoveObserver( *this );
126 if( Stage::IsInstalled() )
128 mImageFactory.ReleaseTicket( mTicket.Get() );
132 if( Stage::IsInstalled() )
138 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
140 bool connected( true );
141 DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
142 Image* image = static_cast<Image*>(object);
144 if( Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
146 image->LoadingFinishedSignal().Connect( tracker, functor );
148 else if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
150 image->UploadedSignal().Connect( tracker, functor );
154 // signalName does not match any signal
161 ResourceId Image::GetResourceId() const
163 ResourceId ret = mTicket ? mTicket->GetId() : 0;
168 const Dali::ImageAttributes& Image::GetAttributes() const
172 return mImageFactory.GetActualAttributes( mTicket );
176 return mImageFactory.GetRequestAttributes( mRequest );
180 const std::string& Image::GetFilename() const
182 return mImageFactory.GetRequestPath( mRequest );
189 ResourceTicketPtr ticket = mImageFactory.Reload( mRequest.Get() );
190 SetTicket( ticket.Get() );
194 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
196 mLoadingFinishedV2.Emit( Dali::Image( this ) );
199 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
201 mLoadingFinishedV2.Emit( Dali::Image( this ) );
204 void Image::ResourceUploaded(const ResourceTicket& ticket)
206 mUploadedV2.Emit( Dali::Image( this ) );
209 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
214 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
219 unsigned int Image::GetWidth() const
221 // if width is 0, it means we've not yet loaded the image
225 mImageFactory.GetImageSize( mRequest, mTicket, size );
227 // The app will probably ask for the height immediately, so don't waste the synchronous file IO that ImageFactory may have just done:
228 DALI_ASSERT_DEBUG( 0 == mHeight || unsigned(size.height) == mHeight );
231 mHeight = size.height;
237 unsigned int Image::GetHeight() const
242 mImageFactory.GetImageSize( mRequest, mTicket, size );
243 mHeight = size.height;
244 DALI_ASSERT_DEBUG( 0 == mWidth || unsigned(size.width) == mWidth );
253 Vector2 Image::GetNaturalSize() const
255 Vector2 naturalSize(mWidth, mHeight);
256 if( 0u == mWidth || 0u == mHeight )
258 mImageFactory.GetImageSize( mRequest, mTicket, naturalSize );
259 mWidth = naturalSize.width;
260 mHeight = naturalSize.height;
265 void Image::Connect()
269 if( mConnectionCount == 1 )
271 // ticket was thrown away when related actors went offstage or image loading on demand
274 ResourceTicketPtr newTicket = mImageFactory.Load( mRequest.Get() );
275 SetTicket( newTicket.Get() );
280 void Image::Disconnect()
287 DALI_ASSERT_DEBUG( mConnectionCount > 0 );
289 if( mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused )
291 // release image memory when it's not visible anymore (decrease ref. count of texture)
296 void Image::Initialize()
301 void Image::SetTicket( ResourceTicket* ticket )
303 if( ticket == mTicket.Get() )
310 mTicket->RemoveObserver( *this );
311 mImageFactory.ReleaseTicket( mTicket.Get() );
316 mTicket.Reset( ticket );
317 mTicket->AddObserver( *this );
325 bool Image::IsNinePatchFileName( const std::string& filename )
329 std::string::const_reverse_iterator iter = filename.rbegin();
330 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
331 while(iter < filename.rend())
341 else if(!isalnum(*iter))
349 if( *iter == '#' || *iter == '9' )
365 state = DONE; // Stop testing characters
386 } // namespace Internal