Merge "Fix SVACE error in animated-image-visual.cpp" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-data.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-toolkit/internal/visuals/npatch-data.h>
20
21 // INTERNAL HEADERS
22 #include <dali-toolkit/internal/visuals/rendering-addon.h>
23
24 // EXTERNAL HEADERS
25 #include <dali/integration-api/debug.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal
34 {
35
36 NPatchData::NPatchData()
37 : mId(INVALID_NPATCH_DATA_ID),
38   mUrl(),
39   mTextureSet(),
40   mHash(0),
41   mCroppedWidth(0),
42   mCroppedHeight(0),
43   mBorder(0, 0, 0, 0),
44   mLoadingState(LoadingState::LOADING),
45   mRenderingMap{nullptr}
46 {
47 }
48
49 NPatchData::~NPatchData()
50 {
51   // If there is an opacity map, it has to be destroyed using addon call
52   if( mRenderingMap )
53   {
54     RenderingAddOn::Get().DestroyNPatch( mRenderingMap );
55   }
56 }
57
58 void NPatchData::SetId(const NPatchDataId id)
59 {
60   mId = id;
61 }
62
63 NPatchData::NPatchDataId NPatchData::GetId() const
64 {
65   return mId;
66 }
67
68 void NPatchData::AddObserver(TextureUploadObserver* textureObserver)
69 {
70   mObserverList.PushBack( textureObserver );
71 }
72
73 void NPatchData::RemoveObserver(TextureUploadObserver* textureObserver)
74 {
75   for(uint32_t index = 0; index < mObserverList.Count(); ++index )
76   {
77     if(textureObserver == mObserverList[index])
78     {
79       mObserverList.Erase( mObserverList.begin() + index );
80       break;
81     }
82   }
83 }
84
85 uint32_t NPatchData::GetObserverCount() const
86 {
87   return mObserverList.Count();
88 }
89
90 void NPatchData::SetUrl(const std::string url)
91 {
92   mUrl = url;
93 }
94
95 std::string NPatchData::GetUrl() const
96 {
97   return mUrl;
98 }
99
100 void NPatchData::SetTextures(const TextureSet textureSet)
101 {
102   mTextureSet = textureSet;
103 }
104
105 TextureSet NPatchData::GetTextures() const
106 {
107   return mTextureSet;
108 }
109
110 void NPatchData::SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX)
111 {
112   mStretchPixelsX = stretchPixelsX;
113 }
114
115 void NPatchData::SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY)
116 {
117   mStretchPixelsY = stretchPixelsY;
118 }
119
120 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsX() const
121 {
122   return mStretchPixelsX;
123 }
124
125 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsY() const
126 {
127   return mStretchPixelsY;
128 }
129
130 void NPatchData::SetHash(std::size_t hash)
131 {
132   mHash = hash;
133 }
134
135 std::size_t NPatchData::GetHash() const
136 {
137   return mHash;
138 }
139
140 void NPatchData::SetCroppedWidth(uint32_t croppedWidth)
141 {
142   mCroppedWidth = croppedWidth;
143 }
144
145 void NPatchData::SetCroppedHeight(uint32_t croppedHeight)
146 {
147   mCroppedHeight = croppedHeight;
148 }
149
150 uint32_t NPatchData::GetCroppedWidth() const
151 {
152   return mCroppedWidth;
153 }
154
155 uint32_t NPatchData::GetCroppedHeight() const
156 {
157   return mCroppedHeight;
158 }
159
160 void NPatchData::SetBorder(const Rect<int> border)
161 {
162   mBorder = border;
163 }
164
165 Rect<int> NPatchData::GetBorder() const
166 {
167   return mBorder;
168 }
169
170 void NPatchData::SetPreMultiplyOnLoad(bool preMultiplyOnLoad)
171 {
172   mPreMultiplyOnLoad = preMultiplyOnLoad;
173 }
174
175 bool NPatchData::IsPreMultiplied() const
176 {
177   return mPreMultiplyOnLoad;
178 }
179
180 void NPatchData::SetLoadingState(const LoadingState loadingState)
181 {
182   mLoadingState = loadingState;
183 }
184
185 NPatchData::LoadingState NPatchData::GetLoadingState() const
186 {
187   return mLoadingState;
188 }
189
190 void* NPatchData::GetRenderingMap() const
191 {
192   return mRenderingMap;
193 }
194
195 void NPatchData::SetLoadedNPatchData( Devel::PixelBuffer& pixelBuffer, bool preMultiplied )
196 {
197   if( mBorder == Rect< int >( 0, 0, 0, 0 ) )
198   {
199     NPatchUtility::ParseBorders( pixelBuffer, mStretchPixelsX, mStretchPixelsY );
200
201     // Crop the image
202     pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
203   }
204   else
205   {
206     mStretchPixelsX.PushBack( Uint16Pair( mBorder.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( mBorder.right )) ? pixelBuffer.GetWidth() - mBorder.right : 0 ) ) );
207     mStretchPixelsY.PushBack( Uint16Pair( mBorder.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( mBorder.bottom )) ? pixelBuffer.GetHeight() - mBorder.bottom : 0 ) ) );
208   }
209
210   mCroppedWidth = pixelBuffer.GetWidth();
211   mCroppedHeight = pixelBuffer.GetHeight();
212
213   // Create opacity map
214   mRenderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, this) : nullptr;
215
216   PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
217
218   Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
219   texture.Upload( pixels );
220
221   mTextureSet = TextureSet::New();
222   mTextureSet.SetTexture( 0u, texture );
223
224   mPreMultiplyOnLoad = preMultiplied;
225
226   mLoadingState = LoadingState::LOAD_COMPLETE;
227 }
228
229 void NPatchData::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
230 {
231   if(loadSuccess)
232   {
233     SetLoadedNPatchData( pixelBuffer, preMultiplied );
234   }
235   else
236   {
237     mLoadingState = LoadingState::LOAD_FAILED;
238   }
239
240   for(uint32_t index = 0; index < mObserverList.Count(); ++index)
241   {
242     TextureUploadObserver* observer = mObserverList[index];
243     observer->UploadComplete(loadSuccess, TextureManager::INVALID_TEXTURE_ID, mTextureSet, false, Vector4(), preMultiplied);
244   }
245 }
246
247 } // namespace Internal
248
249 } // namespace Toolkit
250
251 } // namespace Dali