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 )
77 if( IsNinePatchFileName(filename) )
79 image = NinePatchImage::New( filename, attributes, loadPol, releasePol );
83 image = new Image( loadPol, releasePol );
86 // if the attributes have a size, use that as natural size, otherwise get the size
87 const unsigned int width = attributes.GetWidth();
88 const unsigned int height = attributes.GetHeight();
89 // if one is zero, then there is some scaling calculation so we have to ask the loading logic what it will do
90 if( width > 0 && height > 0 )
92 image->mWidth = width;
93 image->mHeight = height;
94 image->mRequest = image->mImageFactory.RegisterRequest( filename, &attributes );
98 // TODO do this query only if/when its needed (if someone is calling GetSize)
99 Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
101 platformAbstraction.GetClosestImageSize( filename, attributes, closestSize );
102 image->mWidth = closestSize.width;
103 image->mHeight = closestSize.height;
104 // need a new request object as we will request for the closest size
105 Dali::ImageAttributes newAttributes( attributes );
106 newAttributes.SetSize( closestSize );
107 image->mRequest = image->mImageFactory.RegisterRequest( filename, &newAttributes );
110 if( Dali::Image::Immediate == loadPol )
112 // Trigger loading of the image on a seperate resource thread as soon as it
114 image->mTicket = image->mImageFactory.Load( image->mRequest.Get() );
115 image->mTicket->AddObserver( *image );
118 DALI_LOG_SET_OBJECT_STRING( image, filename );
123 ImagePtr Image::New( NativeImage& nativeImg, LoadPolicy loadPol, ReleasePolicy releasePol )
125 ImagePtr image = new Image;
128 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
130 image->mWidth = nativeImg.GetWidth();
131 image->mHeight = nativeImg.GetHeight();
133 const ResourceTicketPtr& ticket = resourceClient.AddNativeImage( nativeImg );
134 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
135 image->mTicket = static_cast<ImageTicket*>(ticket.Get());
136 image->mTicket->AddObserver( *image );
145 mTicket->RemoveObserver( *this );
146 if( Stage::IsInstalled() )
148 mImageFactory.ReleaseTicket( mTicket.Get() );
152 if( Stage::IsInstalled() )
158 bool Image::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
160 bool connected( true );
161 DALI_ASSERT_DEBUG( dynamic_cast<Image*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
162 Image* image = static_cast<Image*>(object);
164 if( Dali::Image::SIGNAL_IMAGE_LOADING_FINISHED == signalName )
166 image->LoadingFinishedSignal().Connect( tracker, functor );
168 else if(Dali::Image::SIGNAL_IMAGE_UPLOADED == signalName)
170 image->UploadedSignal().Connect( tracker, functor );
174 // signalName does not match any signal
181 ResourceId Image::GetResourceId() const
183 ResourceId ret = mTicket ? mTicket->GetId() : 0;
188 const Dali::ImageAttributes& Image::GetAttributes() const
192 return mImageFactory.GetActualAttributes( mTicket->GetId() );
196 return mImageFactory.GetRequestAttributes( mRequest.Get() );
200 const std::string& Image::GetFilename() const
202 return mImageFactory.GetRequestPath( mRequest.Get() );
209 ResourceTicketPtr ticket = mImageFactory.Reload( mRequest.Get() );
210 SetTicket( ticket.Get() );
214 void Image::ResourceLoadingFailed(const ResourceTicket& ticket)
216 mLoadingFinishedV2.Emit( Dali::Image( this ) );
219 void Image::ResourceLoadingSucceeded(const ResourceTicket& ticket)
221 mLoadingFinishedV2.Emit( Dali::Image( this ) );
224 void Image::ResourceUploaded(const ResourceTicket& ticket)
226 mUploadedV2.Emit( Dali::Image( this ) );
229 void Image::ResourceSavingSucceeded( const ResourceTicket& ticket )
234 void Image::ResourceSavingFailed( const ResourceTicket& ticket )
239 unsigned int Image::GetWidth() const
244 unsigned int Image::GetHeight() const
249 Vector2 Image::GetNaturalSize() const
251 Vector2 naturalSize(mWidth, mHeight);
255 void Image::Connect()
259 if( mConnectionCount == 1 )
261 // ticket was thrown away when related actors went offstage or image loading on demand
264 ResourceTicketPtr newTicket = mImageFactory.Load( mRequest.Get() );
265 SetTicket( newTicket.Get() );
270 void Image::Disconnect()
277 DALI_ASSERT_DEBUG( mConnectionCount > 0 );
279 if( mConnectionCount == 0 && mReleasePolicy == Dali::Image::Unused )
281 // release image memory when it's not visible anymore (decrease ref. count of texture)
286 void Image::Initialize()
291 void Image::SetTicket( ResourceTicket* ticket )
293 if( ticket == mTicket.Get() )
300 mTicket->RemoveObserver( *this );
301 mImageFactory.ReleaseTicket( mTicket.Get() );
306 mTicket.Reset( ticket );
307 mTicket->AddObserver( *this );
315 bool Image::IsNinePatchFileName( std::string filename )
319 std::string::const_reverse_iterator iter = filename.rbegin();
320 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
321 while(iter < filename.rend())
331 else if(!isalnum(*iter))
339 if( *iter == '#' || *iter == '9' )
355 state = DONE; // Stop testing characters
376 } // namespace Internal