2 * Copyright (c) 2016 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/resource-image-impl.h>
22 #include <cstring> // for strcmp
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/event/images/nine-patch-image-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/resources/resource-client.h>
34 using namespace Dali::Integration;
47 const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
50 BaseHandle CreateImage()
52 ImagePtr image = ResourceImage::New();
53 return Dali::Image(image.Get());
56 TypeRegistration mType( typeid( Dali::ResourceImage ), typeid( Dali::Image ), CreateImage );
58 Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal );
62 ResourceImage::ResourceImage()
67 mLoadingState( Dali::ResourceLoading )
71 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
74 mAttributes(attributes),
76 mLoadingState( Dali::ResourceLoading )
80 ResourceImagePtr ResourceImage::New()
82 ResourceImagePtr image = new ResourceImage;
87 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
89 ResourceImagePtr image;
91 if( NinePatchImage::IsNinePatchUrl( url ) )
93 image = NinePatchImage::New( url );
97 image = new ResourceImage(url, attributes);
102 DALI_LOG_SET_OBJECT_STRING( image, url );
107 ResourceImage::~ResourceImage()
111 bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
113 bool connected( true );
114 DALI_ASSERT_DEBUG( dynamic_cast<ResourceImage*>( object ) && "Resource ticket not ImageTicket subclass for image resource.\n" );
115 ResourceImage* image = static_cast<ResourceImage*>(object);
117 if( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
119 image->LoadingFinishedSignal().Connect( tracker, functor );
123 // signalName does not match any signal
131 const ImageAttributes& ResourceImage::GetAttributes() const
136 const std::string& ResourceImage::GetUrl() const
141 void ResourceImage::Reload()
143 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
144 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
145 Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
146 mAttributes.GetScalingMode(),
147 mAttributes.GetFilterMode(),
148 mAttributes.GetOrientationCorrection() );
150 // Note, bitmap is only destroyed when the image is destroyed.
151 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously( resourceType, mUrl );
154 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
155 unsigned width = bitmap->GetImageWidth();
156 unsigned height = bitmap->GetImageHeight();
159 Pixel::Format format = bitmap->GetPixelFormat();
160 mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
162 //Upload data to the texture
163 size_t bufferSize = bitmap->GetBufferSize();
164 PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
165 static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
166 mTexture->Upload( pixelData );
168 mWidth = mAttributes.GetWidth();
174 mHeight = mAttributes.GetHeight();
180 mLoadingState = Dali::ResourceLoadingSucceeded;
185 mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
186 mWidth = mHeight = 0u;
187 mLoadingState = Dali::ResourceLoadingFailed;
190 mLoadingFinished.Emit( Dali::ResourceImage( this ) );
193 unsigned int ResourceImage::GetWidth() const
198 unsigned int ResourceImage::GetHeight() const
203 Vector2 ResourceImage::GetNaturalSize() const
205 return Vector2(mWidth, mHeight);
209 } // namespace Internal