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;
41 ResourceImage::ResourceImage()
46 mLoadingState( Dali::ResourceLoading )
50 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
53 mAttributes(attributes),
55 mLoadingState( Dali::ResourceLoading )
59 ResourceImagePtr ResourceImage::New()
61 ResourceImagePtr image = new ResourceImage;
66 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
68 ResourceImagePtr image;
70 if( NinePatchImage::IsNinePatchUrl( url ) )
72 image = NinePatchImage::New( url );
76 image = new ResourceImage(url, attributes);
81 DALI_LOG_SET_OBJECT_STRING( image, url );
86 ResourceImage::~ResourceImage()
90 const ImageAttributes& ResourceImage::GetAttributes() const
95 const std::string& ResourceImage::GetUrl() const
100 void ResourceImage::Reload()
102 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
103 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
104 Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
105 mAttributes.GetScalingMode(),
106 mAttributes.GetFilterMode(),
107 mAttributes.GetOrientationCorrection() );
109 // Note, bitmap is only destroyed when the image is destroyed.
110 Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
113 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
114 unsigned width = bitmap->GetImageWidth();
115 unsigned height = bitmap->GetImageHeight();
118 Pixel::Format format = bitmap->GetPixelFormat();
119 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
121 //Upload data to the texture
122 uint32_t bufferSize = bitmap->GetBufferSize();
123 PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
124 static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
125 mTexture->Upload( pixelData );
127 mWidth = mAttributes.GetWidth();
133 mHeight = mAttributes.GetHeight();
139 mLoadingState = Dali::ResourceLoadingSucceeded;
144 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
145 mWidth = mHeight = 0u;
146 mLoadingState = Dali::ResourceLoadingFailed;
149 UploadedSignal().Emit( Dali::Image( this ) );
150 mLoadingFinished.Emit( Dali::ResourceImage( this ) );
153 unsigned int ResourceImage::GetWidth() const
158 unsigned int ResourceImage::GetHeight() const
163 Vector2 ResourceImage::GetNaturalSize() const
165 return Vector2( static_cast<float>( mWidth ), static_cast<float>( mHeight ) );
169 } // namespace Internal