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