2 * Copyright (c) 2020 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>
36 NPatchData::NPatchData()
37 : mId(INVALID_NPATCH_DATA_ID),
44 mLoadingState(LoadingState::LOADING),
45 mPreMultiplyOnLoad(false),
46 mRenderingMap{nullptr}
50 NPatchData::~NPatchData()
52 // If there is an opacity map, it has to be destroyed using addon call
55 RenderingAddOn::Get().DestroyNPatch( mRenderingMap );
59 void NPatchData::SetId(const NPatchDataId id)
64 NPatchData::NPatchDataId NPatchData::GetId() const
69 void NPatchData::AddObserver(TextureUploadObserver* textureObserver)
71 mObserverList.PushBack( textureObserver );
74 void NPatchData::RemoveObserver(TextureUploadObserver* textureObserver)
76 for(uint32_t index = 0; index < mObserverList.Count(); ++index )
78 if(textureObserver == mObserverList[index])
80 mObserverList.Erase( mObserverList.begin() + index );
86 uint32_t NPatchData::GetObserverCount() const
88 return mObserverList.Count();
91 void NPatchData::SetUrl(const std::string url)
96 std::string NPatchData::GetUrl() const
101 void NPatchData::SetTextures(const TextureSet textureSet)
103 mTextureSet = textureSet;
106 TextureSet NPatchData::GetTextures() const
111 void NPatchData::SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX)
113 mStretchPixelsX = stretchPixelsX;
116 void NPatchData::SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY)
118 mStretchPixelsY = stretchPixelsY;
121 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsX() const
123 return mStretchPixelsX;
126 NPatchUtility::StretchRanges NPatchData::GetStretchPixelsY() const
128 return mStretchPixelsY;
131 void NPatchData::SetHash(std::size_t hash)
136 std::size_t NPatchData::GetHash() const
141 void NPatchData::SetCroppedWidth(uint32_t croppedWidth)
143 mCroppedWidth = croppedWidth;
146 void NPatchData::SetCroppedHeight(uint32_t croppedHeight)
148 mCroppedHeight = croppedHeight;
151 uint32_t NPatchData::GetCroppedWidth() const
153 return mCroppedWidth;
156 uint32_t NPatchData::GetCroppedHeight() const
158 return mCroppedHeight;
161 void NPatchData::SetBorder(const Rect<int> border)
166 Rect<int> NPatchData::GetBorder() const
171 void NPatchData::SetPreMultiplyOnLoad(bool preMultiplyOnLoad)
173 mPreMultiplyOnLoad = preMultiplyOnLoad;
176 bool NPatchData::IsPreMultiplied() const
178 return mPreMultiplyOnLoad;
181 void NPatchData::SetLoadingState(const LoadingState loadingState)
183 mLoadingState = loadingState;
186 NPatchData::LoadingState NPatchData::GetLoadingState() const
188 return mLoadingState;
191 void* NPatchData::GetRenderingMap() const
193 return mRenderingMap;
196 void NPatchData::SetLoadedNPatchData( Devel::PixelBuffer& pixelBuffer, bool preMultiplied )
198 if( mBorder == Rect< int >( 0, 0, 0, 0 ) )
200 NPatchUtility::ParseBorders( pixelBuffer, mStretchPixelsX, mStretchPixelsY );
203 pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
207 mStretchPixelsX.PushBack( Uint16Pair( mBorder.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( mBorder.right )) ? pixelBuffer.GetWidth() - mBorder.right : 0 ) ) );
208 mStretchPixelsY.PushBack( Uint16Pair( mBorder.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( mBorder.bottom )) ? pixelBuffer.GetHeight() - mBorder.bottom : 0 ) ) );
211 mCroppedWidth = pixelBuffer.GetWidth();
212 mCroppedHeight = pixelBuffer.GetHeight();
214 // Create opacity map
215 mRenderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, this) : nullptr;
217 PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
219 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
220 texture.Upload( pixels );
222 mTextureSet = TextureSet::New();
223 mTextureSet.SetTexture( 0u, texture );
225 mPreMultiplyOnLoad = preMultiplied;
227 mLoadingState = LoadingState::LOAD_COMPLETE;
230 void NPatchData::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
234 SetLoadedNPatchData( pixelBuffer, preMultiplied );
238 mLoadingState = LoadingState::LOAD_FAILED;
241 for(uint32_t index = 0; index < mObserverList.Count(); ++index)
243 TextureUploadObserver* observer = mObserverList[index];
244 observer->UploadComplete(loadSuccess, TextureManager::INVALID_TEXTURE_ID, mTextureSet, false, Vector4(), preMultiplied);
248 } // namespace Internal
250 } // namespace Toolkit