License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / common / blending-options.cpp
1 /*
2  * Copyright (c) 2014 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 StoreBlendingFactor( unsigned int& options, BlendingFactor::Type factor, int bitShift )
50 {
51   switch ( factor )
52   {
53     case BlendingFactor::ZERO:
54       options |= ( 0u << bitShift );
55       break;
56
57     case BlendingFactor::ONE:
58       options |= ( 1u << bitShift );
59       break;
60
61     case BlendingFactor::SRC_COLOR:
62       options |= ( 2u << bitShift );
63       break;
64
65     case BlendingFactor::ONE_MINUS_SRC_COLOR:
66       options |= ( 3u << bitShift );
67       break;
68
69     case BlendingFactor::SRC_ALPHA:
70       options |= ( 4u << bitShift );
71       break;
72
73     case BlendingFactor::ONE_MINUS_SRC_ALPHA:
74       options |= ( 5u << bitShift );
75       break;
76
77     case BlendingFactor::DST_ALPHA:
78       options |= ( 6u << bitShift );
79       break;
80
81     case BlendingFactor::ONE_MINUS_DST_ALPHA:
82       options |= ( 7u << bitShift );
83       break;
84
85     case BlendingFactor::DST_COLOR:
86       options |= ( 8u << bitShift );
87       break;
88
89     case BlendingFactor::ONE_MINUS_DST_COLOR:
90       options |= ( 9u << bitShift );
91       break;
92
93     case BlendingFactor::SRC_ALPHA_SATURATE:
94       options |= ( 10u << bitShift );
95       break;
96
97     case BlendingFactor::CONSTANT_COLOR:
98       options |= ( 11u << bitShift );
99       break;
100
101     case BlendingFactor::ONE_MINUS_CONSTANT_COLOR:
102       options |= ( 12u << bitShift );
103       break;
104
105     case BlendingFactor::CONSTANT_ALPHA:
106       options |= ( 13u << bitShift );
107       break;
108
109     case BlendingFactor::ONE_MINUS_CONSTANT_ALPHA:
110       options |= ( 14u << bitShift );
111       break;
112
113     default:
114       DALI_ASSERT_ALWAYS( false && "Invalid BlendingFactor" );
115       break;
116   }
117 }
118
119 /**
120  * Utility to store one of the BlendEquation values.
121  * @param[out] options A bitmask used to store the BlendEquation values.
122  * @param[in] factor The BlendEquation value.
123  * @param[in] bitshift Used to shift to the correct part of options.
124  */
125 void StoreBlendingEquation( unsigned int& options, BlendingEquation::Type factor, int bitShift )
126 {
127   switch ( factor )
128   {
129     case BlendingEquation::ADD:
130       options |= ( 0u << bitShift );
131       break;
132
133     case BlendingEquation::SUBTRACT:
134       options |= ( 1u << bitShift );
135       break;
136
137     case BlendingEquation::REVERSE_SUBTRACT:
138       options |= ( 2u << bitShift );
139       break;
140
141     default:
142       DALI_ASSERT_ALWAYS( false && "Invalid BlendingEquation" );
143       break;
144   }
145 }
146
147 const unsigned int BLENDING_FACTOR_COUNT   = 15;
148 const unsigned int BLENDING_EQUATION_COUNT = 3;
149
150 BlendingFactor::Type BLENDING_FACTORS[ BLENDING_FACTOR_COUNT ] =
151   { BlendingFactor::ZERO,
152     BlendingFactor::ONE,
153     BlendingFactor::SRC_COLOR,
154     BlendingFactor::ONE_MINUS_SRC_COLOR,
155     BlendingFactor::SRC_ALPHA,
156     BlendingFactor::ONE_MINUS_SRC_ALPHA,
157     BlendingFactor::DST_ALPHA,
158     BlendingFactor::ONE_MINUS_DST_ALPHA,
159     BlendingFactor::DST_COLOR,
160     BlendingFactor::ONE_MINUS_DST_COLOR,
161     BlendingFactor::SRC_ALPHA_SATURATE,
162     BlendingFactor::CONSTANT_COLOR,
163     BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
164     BlendingFactor::CONSTANT_ALPHA,
165     BlendingFactor::ONE_MINUS_CONSTANT_ALPHA };
166
167 BlendingEquation::Type BLENDING_EQUATIONS[ BLENDING_EQUATION_COUNT ] =
168   { BlendingEquation::ADD,
169     BlendingEquation::SUBTRACT,
170     BlendingEquation::REVERSE_SUBTRACT };
171
172 /**
173  * Utility to retrieve one of the BlendFunc values.
174  * @param[in] options A bitmask of blending values.
175  * @param[in] mask The used to mask unwanted values.
176  * @param[in] bitshift Used to shift to the correct part of options.
177  * @return The blending factor.
178  */
179 BlendingFactor::Type RetrieveBlendingFactor( unsigned int options, int mask, int bitShift )
180 {
181   unsigned int index = options & mask;
182   index = index >> bitShift;
183
184   DALI_ASSERT_DEBUG( index < BLENDING_FACTOR_COUNT );
185
186   return BLENDING_FACTORS[ index ];
187 }
188
189 /**
190  * Utility to retrieve one of the BlendEquation values.
191  * @param[in] options A bitmask of blending values.
192  * @param[in] mask The used to mask unwanted values.
193  * @param[in] bitshift Used to shift to the correct part of options.
194  * @return The blending equation.
195  */
196 BlendingEquation::Type RetrieveBlendingEquation( unsigned int options, int mask, int bitShift )
197 {
198   unsigned int index = options & mask;
199   index = index >> bitShift;
200
201   DALI_ASSERT_DEBUG( index < BLENDING_EQUATION_COUNT );
202
203   return BLENDING_EQUATIONS[ index ];
204 }
205
206 } // unnamed namespace
207
208 namespace Dali
209 {
210
211 namespace Internal
212 {
213
214 BlendingOptions::BlendingOptions()
215 : mBitmask( 0u ),
216   mOptionalColor( NULL )
217 {
218   SetBlendFunc( DEFAULT_BLENDING_SRC_FACTOR_RGB,   DEFAULT_BLENDING_DEST_FACTOR_RGB,
219                 DEFAULT_BLENDING_SRC_FACTOR_ALPHA, DEFAULT_BLENDING_DEST_FACTOR_ALPHA );
220
221   SetBlendEquation( DEFAULT_BLENDING_EQUATION_RGB, DEFAULT_BLENDING_EQUATION_ALPHA );
222 }
223
224 BlendingOptions::~BlendingOptions()
225 {
226   delete mOptionalColor;
227 }
228
229 void BlendingOptions::SetBitmask( unsigned int bitmask )
230 {
231   mBitmask = bitmask;
232 }
233
234 unsigned int BlendingOptions::GetBitmask() const
235 {
236   return mBitmask;
237 }
238
239 void BlendingOptions::SetBlendFunc( BlendingFactor::Type srcFactorRgb,   BlendingFactor::Type destFactorRgb,
240                                     BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
241 {
242   mBitmask &= CLEAR_BLEND_FUNC_MASK; // Clear the BlendFunc values
243
244   StoreBlendingFactor( mBitmask, srcFactorRgb,    SHIFT_TO_SRC_FACTOR_RGB );
245   StoreBlendingFactor( mBitmask, destFactorRgb,   SHIFT_TO_DEST_FACTOR_RGB );
246   StoreBlendingFactor( mBitmask, srcFactorAlpha,  SHIFT_TO_SRC_FACTOR_ALPHA );
247   StoreBlendingFactor( mBitmask, destFactorAlpha, SHIFT_TO_DEST_FACTOR_ALPHA );
248 }
249
250 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorRgb() const
251 {
252   return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_RGB,  SHIFT_TO_SRC_FACTOR_RGB );
253 }
254
255 BlendingFactor::Type BlendingOptions::GetBlendDestFactorRgb() const
256 {
257   return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_RGB, SHIFT_TO_DEST_FACTOR_RGB );
258 }
259
260 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorAlpha() const
261 {
262   return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_ALPHA,  SHIFT_TO_SRC_FACTOR_ALPHA );
263 }
264
265 BlendingFactor::Type BlendingOptions::GetBlendDestFactorAlpha() const
266 {
267   return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_ALPHA, SHIFT_TO_DEST_FACTOR_ALPHA );
268 }
269
270 void BlendingOptions::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
271 {
272   mBitmask &= CLEAR_BLEND_EQUATION_MASK; // Clear the BlendEquation values
273
274   StoreBlendingEquation( mBitmask, equationRgb,   SHIFT_TO_EQUATION_RGB );
275   StoreBlendingEquation( mBitmask, equationAlpha, SHIFT_TO_EQUATION_ALPHA );
276 }
277
278 BlendingEquation::Type BlendingOptions::GetBlendEquationRgb() const
279 {
280   return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_RGB, SHIFT_TO_EQUATION_RGB );
281 }
282
283 BlendingEquation::Type BlendingOptions::GetBlendEquationAlpha() const
284 {
285   return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_ALPHA, SHIFT_TO_EQUATION_ALPHA );
286 }
287
288 bool BlendingOptions::SetBlendColor( const Vector4& color )
289 {
290   bool changed( false );
291
292   if( Vector4::ZERO == color )
293   {
294     if( mOptionalColor )
295     {
296       // Discard unnecessary vector
297       delete mOptionalColor;
298       mOptionalColor = NULL;
299
300       changed = true;
301     }
302   }
303   else if( !mOptionalColor )
304   {
305     // Lazy allocation when non-default is set
306     mOptionalColor = new Vector4( color );
307     changed = true;
308   }
309   else if( *mOptionalColor != color )
310   {
311     *mOptionalColor = color;
312     changed = true;
313   }
314
315   return changed;
316 }
317
318 const Vector4* BlendingOptions::GetBlendColor() const
319 {
320   return mOptionalColor;
321 }
322
323 } // namespace Internal
324
325 } // namespace Dali