2 * Copyright (c) 2018 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>
33 using namespace Dali::Integration;
46 const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished";
49 BaseHandle CreateImage()
51 ImagePtr image = ResourceImage::New();
52 return Dali::Image(image.Get());
55 TypeRegistration mType( typeid( Dali::ResourceImage ), typeid( Dali::Image ), CreateImage );
57 Dali::SignalConnectorType signalConnector1( mType, SIGNAL_IMAGE_LOADING_FINISHED, &ResourceImage::DoConnectSignal );
61 ResourceImage::ResourceImage()
66 mLoadingState( Dali::ResourceLoading )
70 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
73 mAttributes(attributes),
75 mLoadingState( Dali::ResourceLoading )
79 ResourceImagePtr ResourceImage::New()
81 ResourceImagePtr image = new ResourceImage;
86 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
88 ResourceImagePtr image;
90 if( NinePatchImage::IsNinePatchUrl( url ) )
92 image = NinePatchImage::New( url );
96 image = new ResourceImage(url, attributes);
101 DALI_LOG_SET_OBJECT_STRING( image, url );
106 ResourceImage::~ResourceImage()
110 bool ResourceImage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
112 bool connected( true );
113 DALI_ASSERT_DEBUG( dynamic_cast<ResourceImage*>( object ) && "Failed to downcast from BaseObject to ResourceImage.\n" );
114 ResourceImage* image = static_cast<ResourceImage*>(object);
116 if( 0 == strcmp( signalName.c_str(), SIGNAL_IMAGE_LOADING_FINISHED ) )
118 image->LoadingFinishedSignal().Connect( tracker, functor );
122 // signalName does not match any signal
130 const ImageAttributes& ResourceImage::GetAttributes() const
135 const std::string& ResourceImage::GetUrl() const
140 void ResourceImage::Reload()
142 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
143 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
144 Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
145 mAttributes.GetScalingMode(),
146 mAttributes.GetFilterMode(),
147 mAttributes.GetOrientationCorrection() );
149 // Note, bitmap is only destroyed when the image is destroyed.
150 Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
153 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
154 unsigned width = bitmap->GetImageWidth();
155 unsigned height = bitmap->GetImageHeight();
158 Pixel::Format format = bitmap->GetPixelFormat();
159 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
161 //Upload data to the texture
162 uint32_t bufferSize = bitmap->GetBufferSize();
163 PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
164 static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
165 mTexture->Upload( pixelData );
167 mWidth = mAttributes.GetWidth();
173 mHeight = mAttributes.GetHeight();
179 mLoadingState = Dali::ResourceLoadingSucceeded;
184 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
185 mWidth = mHeight = 0u;
186 mLoadingState = Dali::ResourceLoadingFailed;
189 UploadedSignal().Emit( Dali::Image( this ) );
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( static_cast<float>( mWidth ), static_cast<float>( mHeight ) );
209 } // namespace Internal