2 * Copyright (c) 2021 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::LOADING),
42 mPreMultiplyOnLoad(false),
43 mRenderingMap{nullptr}
47 NPatchData::~NPatchData()
49 // If there is an opacity map, it has to be destroyed using addon call
52 RenderingAddOn::Get().DestroyNPatch(mRenderingMap);
56 void NPatchData::SetId(const NPatchDataId id)
61 NPatchData::NPatchDataId NPatchData::GetId() const
66 void NPatchData::AddObserver(TextureUploadObserver* textureObserver)
68 mObserverList.PushBack(textureObserver);
71 void NPatchData::RemoveObserver(TextureUploadObserver* textureObserver)
73 for(uint32_t index = 0; index < mObserverList.Count(); ++index)
75 if(textureObserver == mObserverList[index])
77 mObserverList.Erase(mObserverList.begin() + index);
83 uint32_t NPatchData::GetObserverCount() const
85 return mObserverList.Count();
88 void NPatchData::SetUrl(const std::string url)
93 std::string NPatchData::GetUrl() const
98 void NPatchData::SetTextures(const TextureSet textureSet)
100 mTextureSet = textureSet;
103 TextureSet NPatchData::GetTextures() const
108 void NPatchData::SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX)
110 mStretchPixelsX = stretchPixelsX;
113 void NPatchData::SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY)
115 mStretchPixelsY = stretchPixelsY;
118 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsX() const
120 return mStretchPixelsX;
123 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsY() const
125 return mStretchPixelsY;
128 void NPatchData::SetHash(std::size_t hash)
133 std::size_t NPatchData::GetHash() const
138 void NPatchData::SetCroppedWidth(uint32_t croppedWidth)
140 mCroppedWidth = croppedWidth;
143 void NPatchData::SetCroppedHeight(uint32_t croppedHeight)
145 mCroppedHeight = croppedHeight;
148 uint32_t NPatchData::GetCroppedWidth() const
150 return mCroppedWidth;
153 uint32_t NPatchData::GetCroppedHeight() const
155 return mCroppedHeight;
158 void NPatchData::SetBorder(const Rect<int> border)
163 Rect<int> NPatchData::GetBorder() const
168 void NPatchData::SetPreMultiplyOnLoad(bool preMultiplyOnLoad)
170 mPreMultiplyOnLoad = preMultiplyOnLoad;
173 bool NPatchData::IsPreMultiplied() const
175 return mPreMultiplyOnLoad;
178 void NPatchData::SetLoadingState(const LoadingState loadingState)
180 mLoadingState = loadingState;
183 NPatchData::LoadingState NPatchData::GetLoadingState() const
185 return mLoadingState;
188 void* NPatchData::GetRenderingMap() const
190 return mRenderingMap;
193 void NPatchData::SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied)
195 if(mBorder == Rect<int>(0, 0, 0, 0))
197 NPatchUtility::ParseBorders(pixelBuffer, mStretchPixelsX, mStretchPixelsY);
200 pixelBuffer.Crop(1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2);
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)));
208 mCroppedWidth = pixelBuffer.GetWidth();
209 mCroppedHeight = pixelBuffer.GetHeight();
211 // Create opacity map
212 mRenderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, this) : nullptr;
214 PixelData pixels = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
216 Texture texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
217 texture.Upload(pixels);
219 mTextureSet = TextureSet::New();
220 mTextureSet.SetTexture(0u, texture);
222 mPreMultiplyOnLoad = preMultiplied;
224 mLoadingState = LoadingState::LOAD_COMPLETE;
227 void NPatchData::LoadComplete(bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied)
231 SetLoadedNPatchData(pixelBuffer, preMultiplied);
235 mLoadingState = LoadingState::LOAD_FAILED;
238 for(uint32_t index = 0; index < mObserverList.Count(); ++index)
240 TextureUploadObserver* observer = mObserverList[index];
241 observer->UploadComplete(loadSuccess, TextureManager::INVALID_TEXTURE_ID, mTextureSet, false, Vector4(), preMultiplied);
245 } // namespace Internal
247 } // namespace Toolkit