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