2 * Copyright (c) 2020 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/common/stage-impl.h>
32 using namespace Dali::Integration;
40 ResourceImage::ResourceImage()
45 mLoadingState( Dali::ResourceLoading )
49 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
52 mAttributes(attributes),
54 mLoadingState( Dali::ResourceLoading )
58 ResourceImagePtr ResourceImage::New()
60 ResourceImagePtr image = new ResourceImage;
65 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
67 ResourceImagePtr image;
68 image = new ResourceImage(url, attributes);
72 DALI_LOG_SET_OBJECT_STRING( image, url );
77 ResourceImage::~ResourceImage()
81 const ImageAttributes& ResourceImage::GetAttributes() const
86 const std::string& ResourceImage::GetUrl() const
91 void ResourceImage::Reload()
93 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
94 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
95 Integration::BitmapResourceType resourceType( ImageDimensions(mAttributes.GetWidth(), mAttributes.GetHeight()),
96 mAttributes.GetScalingMode(),
97 mAttributes.GetFilterMode(),
98 mAttributes.GetOrientationCorrection() );
100 // Note, bitmap is only destroyed when the image is destroyed.
101 Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
104 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
105 unsigned width = bitmap->GetImageWidth();
106 unsigned height = bitmap->GetImageHeight();
109 Pixel::Format format = bitmap->GetPixelFormat();
110 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
112 //Upload data to the texture
113 uint32_t bufferSize = bitmap->GetBufferSize();
114 PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
115 static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
116 mTexture->Upload( pixelData );
118 mWidth = mAttributes.GetWidth();
124 mHeight = mAttributes.GetHeight();
130 mLoadingState = Dali::ResourceLoadingSucceeded;
135 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
136 mWidth = mHeight = 0u;
137 mLoadingState = Dali::ResourceLoadingFailed;
140 mLoadingFinished.Emit( Dali::ResourceImage( this ) );
143 unsigned int ResourceImage::GetWidth() const
148 unsigned int ResourceImage::GetHeight() const
153 Vector2 ResourceImage::GetNaturalSize() const
155 return Vector2( static_cast<float>( mWidth ), static_cast<float>( mHeight ) );
159 } // namespace Internal