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/platform-abstraction.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/event/resources/resource-ticket.h>
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/resources/resource-client.h>
30 #include <dali/internal/event/images/image-factory.h>
31 #include <dali/internal/event/images/nine-patch-image-impl.h>
32 #include <dali/internal/event/common/stage-impl.h>
34 using namespace Dali::Integration;
45 BaseHandle CreateImage()
47 ImagePtr image = Image::New();
48 return Dali::Image(image.Get());
51 TypeRegistration mType( typeid(Dali::Image), typeid(Dali::BaseHandle), CreateImage );
53 Dali::SignalConnectorType signalConnector1(mType, Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED, &Image::DoConnectSignal);
54 Dali::SignalConnectorType signalConnector2(mType, Dali::Image::SIGNAL_IMAGE_UPLOADED, &Image::DoConnectSignal);
57 Image::Image( LoadPolicy loadPol, ReleasePolicy releasePol )
61 mReleasePolicy(releasePol),
63 mImageFactory(ThreadLocalStorage::Get().GetImageFactory())
69 ImagePtr image = new Image;
74 ImagePtr Image::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
76 if( IsNinePatchFileName(filename) )
78 NinePatchImagePtr image = NinePatchImage::New( filename, attributes, loadPol, releasePol );
83 ImagePtr image = new Image( loadPol, releasePol );
86 if( ! filename.empty() )
90 Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( filename, attributes, closestSize );
91 image->mWidth = closestSize.width;
92 image->mHeight = closestSize.height;
95 image->mRequest = image->mImageFactory.RegisterRequest( filename, &attributes );
97 if( Dali::Image::Immediate == loadPol )
99 // Trigger loading of the image on a seperate resource thread as soon as it
101 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
102 image->mTicket->AddObserver( *image );
105 // else lazily load image data later, only when it is needed to draw something:
107 DALI_LOG_SET_OBJECT_STRING( image, filename );
113 ImagePtr Image::New( NativeImage& nativeImg, LoadPolicy loadPol, ReleasePolicy releasePol )
115 ImagePtr image = new Image;
118 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
120 image->mWidth = nativeImg.GetWidth();
121 image->mHeight = nativeImg.GetHeight();
123 const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
124 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
125 image->mTicket = static_cast<ImageTicket*>(ticket.Get());
126 image->mTicket->AddObserver( *image );
135 mTicket->RemoveObserver( *this );
136 if( Stage::IsInstalled() )
138 mImageFactory.ReleaseTicket( mTicket.Get() );
142 if( Stage::IsInstalled() )
148 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
150 bool connected( true );
151 DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
152 Image* image = static_cast<Image*>(object);
154 if( Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
156 image->LoadingFinishedSignal().Connect( tracker, functor );
158 else if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
160 image->UploadedSignal().Connect( tracker, functor );
164 // signalName does not match any signal
171 ResourceId Image::GetResourceId() const
173 ResourceId ret = mTicket ? mTicket->GetId() : 0;
178 const Dali::ImageAttributes& Image::GetAttributes() const
182 return mImageFactory.GetActualAttributes( mTicket->GetId() );
186 return mImageFactory.GetRequestAttributes( mRequest.Get() );
190 const std::string& Image::GetFilename() const
192 return mImageFactory.GetRequestPath( mRequest.Get() );
199 ResourceTicketPtr ticket = mImageFactory.Reload( mRequest.Get() );
200 SetTicket( ticket.Get() );
204 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
206 mLoadingFinishedV2.Emit( Dali::Image( this ) );
209 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
211 // Update size with actual loaded size
212 const ImageTicket* imageTicket = static_cast<const ImageTicket*>(&ticket);
213 mWidth = imageTicket->GetWidth();
214 mHeight = imageTicket->GetHeight();
215 mLoadingFinishedV2.Emit( Dali::Image( this ) );
218 void Image::ResourceUploaded(const ResourceTicket& ticket)
220 mUploadedV2.Emit( Dali::Image( this ) );
223 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
228 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
233 unsigned int Image::GetWidth() const
235 // Width has already been calculated - just return that
239 unsigned int Image::GetHeight() const
241 // Height has already been calculated - just return that
245 Vector2 Image::GetNaturalSize() const
247 return Vector2( mWidth, mHeight );
250 void Image::Connect()
254 if( mConnectionCount == 1 )
256 // ticket was thrown away when related actors went offstage or image loading on demand
259 ResourceTicketPtr newTicket = mImageFactory.Load( mRequest.Get() );
260 SetTicket( newTicket.Get() );
265 void Image::Disconnect()
272 DALI_ASSERT_DEBUG( mConnectionCount > 0 );
274 if( mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused )
276 // release image memory when it's not visible anymore (decrease ref. count of texture)
281 void Image::Initialize()
286 void Image::SetTicket( ResourceTicket* ticket )
288 if( ticket == mTicket.Get() )
295 mTicket->RemoveObserver( *this );
296 mImageFactory.ReleaseTicket( mTicket.Get() );
301 mTicket.Reset( ticket );
302 mTicket->AddObserver( *this );
310 bool Image::IsNinePatchFileName( std::string filename )
314 std::string::const_reverse_iterator iter = filename.rbegin();
315 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
316 while(iter < filename.rend())
326 else if(!isalnum(*iter))
334 if( *iter == '#' || *iter == '9' )
350 state = DONE; // Stop testing characters
371 } // namespace Internal