Merge "use modern construct 'nullptr' instead of 'NULL' or '0'" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / common / blending-options.cpp
1 /*
2  * Copyright (c) 2017 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/internal/common/blending-options.h>
20
21 namespace // unnamed namespace
22 {
23
24 using namespace Dali;
25
26 const int MASK_SRC_FACTOR_RGB    = 0x0000000F;
27 const int MASK_SRC_FACTOR_ALPHA  = 0x000000F0;
28 const int MASK_DEST_FACTOR_RGB   = 0x00000F00;
29 const int MASK_DEST_FACTOR_ALPHA = 0x0000F000;
30 const int MASK_EQUATION_RGB      = 0x000F0000;
31 const int MASK_EQUATION_ALPHA    = 0x00F00000;
32
33 const int SHIFT_TO_SRC_FACTOR_RGB    =  0;
34 const int SHIFT_TO_SRC_FACTOR_ALPHA  =  4;
35 const int SHIFT_TO_DEST_FACTOR_RGB   =  8;
36 const int SHIFT_TO_DEST_FACTOR_ALPHA = 12;
37 const int SHIFT_TO_EQUATION_RGB      = 16;
38 const int SHIFT_TO_EQUATION_ALPHA    = 20;
39
40 static unsigned int CLEAR_BLEND_FUNC_MASK     = 0xFFFF0000; // Bottom 16 bits cleared
41 static unsigned int CLEAR_BLEND_EQUATION_MASK = 0xFF00FFFF; // 8 bits cleared
42
43 /**
44  * Utility to store one of the BlendFunc values.
45  * @param[out] options A bitmask used to store the BlendFunc values.
46  * @param[in] factor The BlendFunc value.
47  * @param[in] bitshift Used to shift to the correct part of options.
48  */
49 void StoreBlendFactor( unsigned int& options, BlendFactor::Type factor, int bitShift )
50 {
51   switch( factor )
52   {
53     case BlendFactor::ZERO:
54     {
55       options |= ( 0u << bitShift );
56       break;
57     }
58
59     case BlendFactor::ONE:
60     {
61       options |= ( 1u << bitShift );
62       break;
63     }
64
65     case BlendFactor::SRC_COLOR:
66     {
67       options |= ( 2u << bitShift );
68       break;
69     }
70
71     case BlendFactor::ONE_MINUS_SRC_COLOR:
72     {
73       options |= ( 3u << bitShift );
74       break;
75     }
76
77     case BlendFactor::SRC_ALPHA:
78     {
79       options |= ( 4u << bitShift );
80       break;
81     }
82
83     case BlendFactor::ONE_MINUS_SRC_ALPHA:
84     {
85       options |= ( 5u << bitShift );
86       break;
87     }
88
89     case BlendFactor::DST_ALPHA:
90     {
91       options |= ( 6u << bitShift );
92       break;
93     }
94
95     case BlendFactor::ONE_MINUS_DST_ALPHA:
96     {
97       options |= ( 7u << bitShift );
98       break;
99     }
100
101     case BlendFactor::DST_COLOR:
102     {
103       options |= ( 8u << bitShift );
104       break;
105     }
106
107     case BlendFactor::ONE_MINUS_DST_COLOR:
108     {
109       options |= ( 9u << bitShift );
110       break;
111     }
112
113     case BlendFactor::SRC_ALPHA_SATURATE:
114     {
115       options |= ( 10u << bitShift );
116       break;
117     }
118
119     case BlendFactor::CONSTANT_COLOR:
120     {
121       options |= ( 11u << bitShift );
122       break;
123     }
124
125     case BlendFactor::ONE_MINUS_CONSTANT_COLOR:
126     {
127       options |= ( 12u << bitShift );
128       break;
129     }
130
131     case BlendFactor::CONSTANT_ALPHA:
132     {
133       options |= ( 13u << bitShift );
134       break;
135     }
136
137     case BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
138     {
139       options |= ( 14u << bitShift );
140       break;
141     }
142   }
143 }
144
145 /**
146  * Utility to store one of the BlendEquation values.
147  * @param[out] options A bitmask used to store the BlendEquation values.
148  * @param[in] factor The BlendEquation value.
149  * @param[in] bitshift Used to shift to the correct part of options.
150  */
151 void StoreBlendEquation( unsigned int& options, BlendEquation::Type factor, int bitShift )
152 {
153   switch ( factor )
154   {
155     case BlendEquation::ADD:
156     {
157       options |= ( 0u << bitShift );
158       break;
159     }
160
161     case BlendEquation::SUBTRACT:
162     {
163       options |= ( 1u << bitShift );
164       break;
165     }
166
167     case BlendEquation::REVERSE_SUBTRACT:
168     {
169       options |= ( 2u << bitShift );
170       break;
171     }
172   }
173 }
174
175 const unsigned int BLENDING_FACTOR_COUNT   = 15;
176 const unsigned int BLENDING_EQUATION_COUNT = 3;
177
178 BlendFactor::Type BLENDING_FACTORS[ BLENDING_FACTOR_COUNT ] =
179   { BlendFactor::ZERO,
180     BlendFactor::ONE,
181     BlendFactor::SRC_COLOR,
182     BlendFactor::ONE_MINUS_SRC_COLOR,
183     BlendFactor::SRC_ALPHA,
184     BlendFactor::ONE_MINUS_SRC_ALPHA,
185     BlendFactor::DST_ALPHA,
186     BlendFactor::ONE_MINUS_DST_ALPHA,
187     BlendFactor::DST_COLOR,
188     BlendFactor::ONE_MINUS_DST_COLOR,
189     BlendFactor::SRC_ALPHA_SATURATE,
190     BlendFactor::CONSTANT_COLOR,
191     BlendFactor::ONE_MINUS_CONSTANT_COLOR,
192     BlendFactor::CONSTANT_ALPHA,
193     BlendFactor::ONE_MINUS_CONSTANT_ALPHA };
194
195 BlendEquation::Type BLENDING_EQUATIONS[ BLENDING_EQUATION_COUNT ] =
196   { BlendEquation::ADD,
197     BlendEquation::SUBTRACT,
198     BlendEquation::REVERSE_SUBTRACT };
199
200 /**
201  * Utility to retrieve one of the BlendFunc values.
202  * @param[in] options A bitmask of blending values.
203  * @param[in] mask The used to mask unwanted values.
204  * @param[in] bitshift Used to shift to the correct part of options.
205  * @return The blending factor.
206  */
207 BlendFactor::Type RetrieveBlendFactor( unsigned int options, int mask, int bitShift )
208 {
209   unsigned int index = options & mask;
210   index = index >> bitShift;
211
212   DALI_ASSERT_DEBUG( index < BLENDING_FACTOR_COUNT );
213
214   return BLENDING_FACTORS[ index ];
215 }
216
217 /**
218  * Utility to retrieve one of the BlendEquation values.
219  * @param[in] options A bitmask of blending values.
220  * @param[in] mask The used to mask unwanted values.
221  * @param[in] bitshift Used to shift to the correct part of options.
222  * @return The blending equation.
223  */
224 BlendEquation::Type RetrieveBlendEquation( unsigned int options, int mask, int bitShift )
225 {
226   unsigned int index = options & mask;
227   index = index >> bitShift;
228
229   DALI_ASSERT_DEBUG( index < BLENDING_EQUATION_COUNT );
230
231   return BLENDING_EQUATIONS[ index ];
232 }
233
234 } // unnamed namespace
235
236 namespace Dali
237 {
238
239 namespace Internal
240 {
241
242 BlendingOptions::BlendingOptions()
243 : mBitmask( 0u ),
244   mBlendColor( nullptr )
245 {
246   SetBlendFunc( BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA,
247                 BlendFactor::ONE,       BlendFactor::ONE_MINUS_SRC_ALPHA );
248
249   SetBlendEquation( BlendEquation::ADD, BlendEquation::ADD );
250 }
251
252 BlendingOptions::~BlendingOptions()
253 {
254 }
255
256 void BlendingOptions::SetBitmask( unsigned int bitmask )
257 {
258   mBitmask = bitmask;
259 }
260
261 unsigned int BlendingOptions::GetBitmask() const
262 {
263   return mBitmask;
264 }
265
266 void BlendingOptions::SetBlendFunc( BlendFactor::Type srcFactorRgb,   BlendFactor::Type destFactorRgb,
267                                     BlendFactor::Type srcFactorAlpha, BlendFactor::Type destFactorAlpha )
268 {
269   mBitmask &= CLEAR_BLEND_FUNC_MASK; // Clear the BlendFunc values
270
271   StoreBlendFactor( mBitmask, srcFactorRgb,    SHIFT_TO_SRC_FACTOR_RGB );
272   StoreBlendFactor( mBitmask, destFactorRgb,   SHIFT_TO_DEST_FACTOR_RGB );
273   StoreBlendFactor( mBitmask, srcFactorAlpha,  SHIFT_TO_SRC_FACTOR_ALPHA );
274   StoreBlendFactor( mBitmask, destFactorAlpha, SHIFT_TO_DEST_FACTOR_ALPHA );
275 }
276
277 BlendFactor::Type BlendingOptions::GetBlendSrcFactorRgb() const
278 {
279   return RetrieveBlendFactor( mBitmask, MASK_SRC_FACTOR_RGB,  SHIFT_TO_SRC_FACTOR_RGB );
280 }
281
282 BlendFactor::Type BlendingOptions::GetBlendDestFactorRgb() const
283 {
284   return RetrieveBlendFactor( mBitmask, MASK_DEST_FACTOR_RGB, SHIFT_TO_DEST_FACTOR_RGB );
285 }
286
287 BlendFactor::Type BlendingOptions::GetBlendSrcFactorAlpha() const
288 {
289   return RetrieveBlendFactor( mBitmask, MASK_SRC_FACTOR_ALPHA,  SHIFT_TO_SRC_FACTOR_ALPHA );
290 }
291
292 BlendFactor::Type BlendingOptions::GetBlendDestFactorAlpha() const
293 {
294   return RetrieveBlendFactor( mBitmask, MASK_DEST_FACTOR_ALPHA, SHIFT_TO_DEST_FACTOR_ALPHA );
295 }
296
297 void BlendingOptions::SetBlendEquation( BlendEquation::Type equationRgb, BlendEquation::Type equationAlpha )
298 {
299   mBitmask &= CLEAR_BLEND_EQUATION_MASK; // Clear the BlendEquation values
300
301   StoreBlendEquation( mBitmask, equationRgb,   SHIFT_TO_EQUATION_RGB );
302   StoreBlendEquation( mBitmask, equationAlpha, SHIFT_TO_EQUATION_ALPHA );
303 }
304
305 BlendEquation::Type BlendingOptions::GetBlendEquationRgb() const
306 {
307   return RetrieveBlendEquation( mBitmask, MASK_EQUATION_RGB, SHIFT_TO_EQUATION_RGB );
308 }
309
310 BlendEquation::Type BlendingOptions::GetBlendEquationAlpha() const
311 {
312   return RetrieveBlendEquation( mBitmask, MASK_EQUATION_ALPHA, SHIFT_TO_EQUATION_ALPHA );
313 }
314
315 void BlendingOptions::SetBlendColor( const Vector4& color )
316 {
317   if( Color::TRANSPARENT == color )
318   {
319     mBlendColor = nullptr;
320   }
321   else
322   {
323     if( mBlendColor )
324     {
325       *mBlendColor = color;
326     }
327     else
328     {
329       // Lazy allocation when non-default is set
330       mBlendColor = new Vector4( color );
331     }
332   }
333 }
334
335 const Vector4* BlendingOptions::GetBlendColor() const
336 {
337   return mBlendColor.Get();
338 }
339
340 } // namespace Internal
341
342 } // namespace Dali