Revert "[Tizen] Restore Uploaded signal for BufferImage and ResourceImage"
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.cpp
1 /*
2  * Copyright (c) 2018 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/images/nine-patch-image-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32
33 using namespace Dali::Integration;
34
35 namespace Dali
36 {
37
38 namespace Internal
39 {
40
41 ResourceImage::ResourceImage()
42 : Image(),
43   mLoadingFinished(),
44   mAttributes(),
45   mUrl(),
46   mLoadingState( Dali::ResourceLoading )
47 {
48 }
49
50 ResourceImage::ResourceImage( const std::string& url, const ImageAttributes& attributes)
51 : Image(),
52   mLoadingFinished(),
53   mAttributes(attributes),
54   mUrl(url),
55   mLoadingState( Dali::ResourceLoading )
56 {
57 }
58
59 ResourceImagePtr ResourceImage::New()
60 {
61   ResourceImagePtr image = new ResourceImage;
62   image->Initialize();
63   return image;
64 }
65
66 ResourceImagePtr ResourceImage::New( const std::string& url, const ImageAttributes& attributes )
67 {
68   ResourceImagePtr image;
69
70   if( NinePatchImage::IsNinePatchUrl( url ) )
71   {
72     image = NinePatchImage::New( url );
73   }
74   else
75   {
76     image = new ResourceImage(url, attributes);
77     image->Initialize();
78     image->Reload();
79   }
80
81   DALI_LOG_SET_OBJECT_STRING( image, url );
82
83   return image;
84 }
85
86 ResourceImage::~ResourceImage()
87 {
88 }
89
90 const ImageAttributes& ResourceImage::GetAttributes() const
91 {
92   return mAttributes;
93 }
94
95 const std::string& ResourceImage::GetUrl() const
96 {
97   return mUrl;
98 }
99
100 void ResourceImage::Reload()
101 {
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() );
108
109   // Note, bitmap is only destroyed when the image is destroyed.
110   Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, mUrl );
111   if( resource )
112   {
113     Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
114     unsigned width  = bitmap->GetImageWidth();
115     unsigned height = bitmap->GetImageHeight();
116
117     //Create texture
118     Pixel::Format format = bitmap->GetPixelFormat();
119     mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
120
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 );
126
127     mWidth = mAttributes.GetWidth();
128     if( mWidth == 0 )
129     {
130       mWidth = width;
131     }
132
133     mHeight = mAttributes.GetHeight();
134     if( mHeight == 0 )
135     {
136       mHeight = height;
137     }
138
139     mLoadingState = Dali::ResourceLoadingSucceeded;
140
141   }
142   else
143   {
144     mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
145     mWidth = mHeight = 0u;
146     mLoadingState = Dali::ResourceLoadingFailed;
147   }
148
149   mLoadingFinished.Emit( Dali::ResourceImage( this ) );
150 }
151
152 unsigned int ResourceImage::GetWidth() const
153 {
154   return mWidth;
155 }
156
157 unsigned int ResourceImage::GetHeight() const
158 {
159   return mHeight;
160 }
161
162 Vector2 ResourceImage::GetNaturalSize() const
163 {
164   return Vector2( static_cast<float>( mWidth ), static_cast<float>( mHeight ) );
165 }
166
167
168 } // namespace Internal
169
170 } // namespace Dali