2 * Copyright (c) 2023 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-toolkit/internal/visuals/npatch-data.h>
22 #include <dali-toolkit/internal/visuals/rendering-addon.h>
25 #include <dali/integration-api/debug.h>
33 NPatchData::NPatchData()
34 : mId(INVALID_NPATCH_DATA_ID),
41 mLoadingState(LoadingState::NOT_STARTED),
42 mRenderingMap{nullptr},
43 mPreMultiplyOnLoad(false),
44 mObserverNotifying(false)
48 NPatchData::~NPatchData()
50 // If there is an opacity map, it has to be destroyed using addon call
53 RenderingAddOn::Get().DestroyNPatch(mRenderingMap);
55 mObserverList.Clear();
56 mQueuedObservers.Clear();
59 void NPatchData::SetId(const NPatchDataId id)
64 NPatchData::NPatchDataId NPatchData::GetId() const
69 void NPatchData::AddObserver(TextureUploadObserver* textureObserver)
71 if(mObserverNotifying)
73 // Do not add it into observer list during observer notifying.
74 mQueuedObservers.PushBack(textureObserver);
78 mObserverList.PushBack(textureObserver);
80 textureObserver->DestructionSignal().Connect(this, &NPatchData::ObserverDestroyed);
83 void NPatchData::RemoveObserver(TextureUploadObserver* textureObserver)
85 for(uint32_t index = 0; index < mObserverList.Count(); ++index)
87 if(textureObserver == mObserverList[index])
89 textureObserver->DestructionSignal().Disconnect(this, &NPatchData::ObserverDestroyed);
90 mObserverList.Erase(mObserverList.begin() + index);
96 uint32_t NPatchData::GetObserverCount() const
98 return mObserverList.Count();
101 void NPatchData::SetUrl(const VisualUrl& url)
106 VisualUrl NPatchData::GetUrl() const
111 void NPatchData::SetTextures(const TextureSet textureSet)
113 mTextureSet = textureSet;
116 TextureSet NPatchData::GetTextures() const
121 void NPatchData::SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX)
123 mStretchPixelsX = stretchPixelsX;
126 void NPatchData::SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY)
128 mStretchPixelsY = stretchPixelsY;
131 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsX() const
133 return mStretchPixelsX;
136 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsY() const
138 return mStretchPixelsY;
141 void NPatchData::SetHash(std::size_t hash)
146 std::size_t NPatchData::GetHash() const
151 void NPatchData::SetCroppedWidth(uint32_t croppedWidth)
153 mCroppedWidth = croppedWidth;
156 void NPatchData::SetCroppedHeight(uint32_t croppedHeight)
158 mCroppedHeight = croppedHeight;
161 uint32_t NPatchData::GetCroppedWidth() const
163 return mCroppedWidth;
166 uint32_t NPatchData::GetCroppedHeight() const
168 return mCroppedHeight;
171 void NPatchData::SetBorder(const Rect<int> border)
176 Rect<int> NPatchData::GetBorder() const
181 void NPatchData::SetPreMultiplyOnLoad(bool preMultiplyOnLoad)
183 mPreMultiplyOnLoad = preMultiplyOnLoad;
186 bool NPatchData::IsPreMultiplied() const
188 return mPreMultiplyOnLoad;
191 void NPatchData::SetLoadingState(const LoadingState loadingState)
193 mLoadingState = loadingState;
196 NPatchData::LoadingState NPatchData::GetLoadingState() const
198 return mLoadingState;
201 void* NPatchData::GetRenderingMap() const
203 return mRenderingMap;
206 void NPatchData::SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied)
208 if(mBorder == Rect<int>(0, 0, 0, 0))
210 NPatchUtility::ParseBorders(pixelBuffer, mStretchPixelsX, mStretchPixelsY);
213 pixelBuffer.Crop(1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2);
217 mStretchPixelsX.PushBack(Uint16Pair(mBorder.left, ((pixelBuffer.GetWidth() >= static_cast<unsigned int>(mBorder.right)) ? pixelBuffer.GetWidth() - mBorder.right : 0)));
218 mStretchPixelsY.PushBack(Uint16Pair(mBorder.top, ((pixelBuffer.GetHeight() >= static_cast<unsigned int>(mBorder.bottom)) ? pixelBuffer.GetHeight() - mBorder.bottom : 0)));
221 mCroppedWidth = pixelBuffer.GetWidth();
222 mCroppedHeight = pixelBuffer.GetHeight();
224 // Create opacity map
225 mRenderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, this) : nullptr;
227 PixelData pixels = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
229 Texture texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
230 texture.Upload(pixels);
232 mTextureSet = TextureSet::New();
233 mTextureSet.SetTexture(0u, texture);
235 mPreMultiplyOnLoad = preMultiplied;
237 mLoadingState = LoadingState::LOAD_COMPLETE;
240 void NPatchData::NotifyObserver(TextureUploadObserver* observer, const bool& loadSuccess)
242 observer->LoadComplete(
244 TextureUploadObserver::TextureInformation(
245 TextureUploadObserver::ReturnType::TEXTURE,
246 static_cast<TextureManager::TextureId>(mId), ///< Note : until end of NPatchLoader::Load, npatch-visual don't know the id of data.
249 mPreMultiplyOnLoad));
252 void NPatchData::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
256 if(mLoadingState != LoadingState::LOAD_COMPLETE)
258 // If mLoadingState is LOAD_FAILED, just re-set (It can be happened when sync loading is failed, but async loading is succeeded).
259 SetLoadedNPatchData(textureInformation.pixelBuffer, textureInformation.preMultiplied);
264 if(mLoadingState == LoadingState::LOADING)
266 mLoadingState = LoadingState::LOAD_FAILED;
268 // If mLoadingState is already LOAD_COMPLETE, we can use uploaded texture (It can be happened when sync loading is succeeded, but async loading is failed).
269 else if(mLoadingState == LoadingState::LOAD_COMPLETE)
275 mObserverNotifying = true;
277 // Reverse observer list that we can pop_back the observer.
278 std::reverse(mObserverList.Begin(), mObserverList.End());
280 while(mObserverList.Count() > 0u)
282 TextureUploadObserver* observer = *(mObserverList.End() - 1u);
283 mObserverList.Erase(mObserverList.End() - 1u);
285 observer->DestructionSignal().Disconnect(this, &NPatchData::ObserverDestroyed);
287 NotifyObserver(observer, loadSuccess);
290 mObserverNotifying = false;
292 // Swap observer list what we queued during notify observer.
293 // If mQueuedObserver is not empty, it mean mLoadingState was LOAD_FAILED, and we try to re-load for this data.
294 // (If mLoadingState was LOAD_COMPLETE, NotifyObserver will be called directly. @todo : Should we fix this logic, matched with texture manager?)
295 // So LoadComplete will be called.
296 mObserverList.Swap(mQueuedObservers);
299 void NPatchData::ObserverDestroyed(TextureUploadObserver* observer)
301 for(auto iter = mObserverList.Begin(); iter != mObserverList.End();)
303 if(observer == (*iter))
305 iter = mObserverList.Erase(iter);
312 if(mObserverNotifying)
314 for(auto iter = mQueuedObservers.Begin(); iter != mQueuedObservers.End();)
316 if(observer == (*iter))
318 iter = mQueuedObservers.Erase(iter);
328 } // namespace Internal
330 } // namespace Toolkit