a91bed6d119897d796c0df79f41293fc7808066e
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/resource-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
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>
31
32 using namespace Dali::Integration;
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 ResourceImage::ResourceImage()
41 : Image(),
42   mLoadingFinished(),
43   mAttributes(),
44   mUrl(),
45   mLoadingState( Dali::ResourceLoading )
46 {
47 }
48
49 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
50 : Image(),
51   mLoadingFinished(),
52   mAttributes(attributes),
53   mUrl(url),
54   mLoadingState( Dali::ResourceLoading )
55 {
56 }
57
58 ResourceImagePtr ResourceImage::New()
59 {
60   ResourceImagePtr image = new ResourceImage;
61   image->Initialize();
62   return image;
63 }
64
65 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
66 {
67   ResourceImagePtr image;
68   image = new ResourceImage(url, attributes);
69   image->Initialize();
70   image->Reload();
71
72   DALI_LOG_SET_OBJECT_STRING( image, url );
73
74   return image;
75 }
76
77 ResourceImage::~ResourceImage()
78 {
79 }
80
81 const ImageAttributes& ResourceImage::GetAttributes() const
82 {
83   return mAttributes;
84 }
85
86 const std::string& ResourceImage::GetUrl() const
87 {
88   return mUrl;
89 }
90
91 void ResourceImage::Reload()
92 {
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() );
99
100   // Note, bitmap is only destroyed when the image is destroyed.
101   Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
102   if( resource )
103   {
104     Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
105     unsigned width  = bitmap->GetImageWidth();
106     unsigned height = bitmap->GetImageHeight();
107
108     //Create texture
109     Pixel::Format format = bitmap->GetPixelFormat();
110     mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
111
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 );
117
118     mWidth = mAttributes.GetWidth();
119     if( mWidth == 0 )
120     {
121       mWidth = width;
122     }
123
124     mHeight = mAttributes.GetHeight();
125     if( mHeight == 0 )
126     {
127       mHeight = height;
128     }
129
130     mLoadingState = Dali::ResourceLoadingSucceeded;
131
132   }
133   else
134   {
135     mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
136     mWidth = mHeight = 0u;
137     mLoadingState = Dali::ResourceLoadingFailed;
138   }
139
140   mLoadingFinished.Emit( Dali::ResourceImage( this ) );
141 }
142
143 unsigned int ResourceImage::GetWidth() const
144 {
145   return mWidth;
146 }
147
148 unsigned int ResourceImage::GetHeight() const
149 {
150   return mHeight;
151 }
152
153 Vector2 ResourceImage::GetNaturalSize() const
154 {
155   return Vector2( static_cast<float>( mWidth ), static_cast<float>( mHeight ) );
156 }
157
158
159 } // namespace Internal
160
161 } // namespace Dali