Merge "Remove dead code" into tizen
[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 }
114
115 /**
116  * Utility to store one of the BlendEquation values.
117  * @param[out] options A bitmask used to store the BlendEquation values.
118  * @param[in] factor The BlendEquation value.
119  * @param[in] bitshift Used to shift to the correct part of options.
120  */
121 void StoreBlendingEquation( unsigned int& options, BlendingEquation::Type factor, int bitShift )
122 {
123   switch ( factor )
124   {
125     case BlendingEquation::ADD:
126       options |= ( 0u << bitShift );
127       break;
128
129     case BlendingEquation::SUBTRACT:
130       options |= ( 1u << bitShift );
131       break;
132
133     case BlendingEquation::REVERSE_SUBTRACT:
134       options |= ( 2u << bitShift );
135       break;
136   }
137 }
138
139 const unsigned int BLENDING_FACTOR_COUNT   = 15;
140 const unsigned int BLENDING_EQUATION_COUNT = 3;
141
142 BlendingFactor::Type BLENDING_FACTORS[ BLENDING_FACTOR_COUNT ] =
143   { BlendingFactor::ZERO,
144     BlendingFactor::ONE,
145     BlendingFactor::SRC_COLOR,
146     BlendingFactor::ONE_MINUS_SRC_COLOR,
147     BlendingFactor::SRC_ALPHA,
148     BlendingFactor::ONE_MINUS_SRC_ALPHA,
149     BlendingFactor::DST_ALPHA,
150     BlendingFactor::ONE_MINUS_DST_ALPHA,
151     BlendingFactor::DST_COLOR,
152     BlendingFactor::ONE_MINUS_DST_COLOR,
153     BlendingFactor::SRC_ALPHA_SATURATE,
154     BlendingFactor::CONSTANT_COLOR,
155     BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
156     BlendingFactor::CONSTANT_ALPHA,
157     BlendingFactor::ONE_MINUS_CONSTANT_ALPHA };
158
159 BlendingEquation::Type BLENDING_EQUATIONS[ BLENDING_EQUATION_COUNT ] =
160   { BlendingEquation::ADD,
161     BlendingEquation::SUBTRACT,
162     BlendingEquation::REVERSE_SUBTRACT };
163
164 /**
165  * Utility to retrieve one of the BlendFunc values.
166  * @param[in] options A bitmask of blending values.
167  * @param[in] mask The used to mask unwanted values.
168  * @param[in] bitshift Used to shift to the correct part of options.
169  * @return The blending factor.
170  */
171 BlendingFactor::Type RetrieveBlendingFactor( unsigned int options, int mask, int bitShift )
172 {
173   unsigned int index = options & mask;
174   index = index >> bitShift;
175
176   DALI_ASSERT_DEBUG( index < BLENDING_FACTOR_COUNT );
177
178   return BLENDING_FACTORS[ index ];
179 }
180
181 /**
182  * Utility to retrieve one of the BlendEquation values.
183  * @param[in] options A bitmask of blending values.
184  * @param[in] mask The used to mask unwanted values.
185  * @param[in] bitshift Used to shift to the correct part of options.
186  * @return The blending equation.
187  */
188 BlendingEquation::Type RetrieveBlendingEquation( unsigned int options, int mask, int bitShift )
189 {
190   unsigned int index = options & mask;
191   index = index >> bitShift;
192
193   DALI_ASSERT_DEBUG( index < BLENDING_EQUATION_COUNT );
194
195   return BLENDING_EQUATIONS[ index ];
196 }
197
198 } // unnamed namespace
199
200 namespace Dali
201 {
202
203 namespace Internal
204 {
205
206 BlendingOptions::BlendingOptions()
207 : mBitmask( 0u ),
208   mOptionalColor( NULL )
209 {
210   SetBlendFunc( DEFAULT_BLENDING_SRC_FACTOR_RGB,   DEFAULT_BLENDING_DEST_FACTOR_RGB,
211                 DEFAULT_BLENDING_SRC_FACTOR_ALPHA, DEFAULT_BLENDING_DEST_FACTOR_ALPHA );
212
213   SetBlendEquation( DEFAULT_BLENDING_EQUATION_RGB, DEFAULT_BLENDING_EQUATION_ALPHA );
214 }
215
216 BlendingOptions::~BlendingOptions()
217 {
218   delete mOptionalColor;
219 }
220
221 void BlendingOptions::SetBitmask( unsigned int bitmask )
222 {
223   mBitmask = bitmask;
224 }
225
226 unsigned int BlendingOptions::GetBitmask() const
227 {
228   return mBitmask;
229 }
230
231 void BlendingOptions::SetBlendFunc( BlendingFactor::Type srcFactorRgb,   BlendingFactor::Type destFactorRgb,
232                                     BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
233 {
234   mBitmask &= CLEAR_BLEND_FUNC_MASK; // Clear the BlendFunc values
235
236   StoreBlendingFactor( mBitmask, srcFactorRgb,    SHIFT_TO_SRC_FACTOR_RGB );
237   StoreBlendingFactor( mBitmask, destFactorRgb,   SHIFT_TO_DEST_FACTOR_RGB );
238   StoreBlendingFactor( mBitmask, srcFactorAlpha,  SHIFT_TO_SRC_FACTOR_ALPHA );
239   StoreBlendingFactor( mBitmask, destFactorAlpha, SHIFT_TO_DEST_FACTOR_ALPHA );
240 }
241
242 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorRgb() const
243 {
244   return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_RGB,  SHIFT_TO_SRC_FACTOR_RGB );
245 }
246
247 BlendingFactor::Type BlendingOptions::GetBlendDestFactorRgb() const
248 {
249   return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_RGB, SHIFT_TO_DEST_FACTOR_RGB );
250 }
251
252 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorAlpha() const
253 {
254   return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_ALPHA,  SHIFT_TO_SRC_FACTOR_ALPHA );
255 }
256
257 BlendingFactor::Type BlendingOptions::GetBlendDestFactorAlpha() const
258 {
259   return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_ALPHA, SHIFT_TO_DEST_FACTOR_ALPHA );
260 }
261
262 void BlendingOptions::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
263 {
264   mBitmask &= CLEAR_BLEND_EQUATION_MASK; // Clear the BlendEquation values
265
266   StoreBlendingEquation( mBitmask, equationRgb,   SHIFT_TO_EQUATION_RGB );
267   StoreBlendingEquation( mBitmask, equationAlpha, SHIFT_TO_EQUATION_ALPHA );
268 }
269
270 BlendingEquation::Type BlendingOptions::GetBlendEquationRgb() const
271 {
272   return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_RGB, SHIFT_TO_EQUATION_RGB );
273 }
274
275 BlendingEquation::Type BlendingOptions::GetBlendEquationAlpha() const
276 {
277   return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_ALPHA, SHIFT_TO_EQUATION_ALPHA );
278 }
279
280 bool BlendingOptions::SetBlendColor( const Vector4& color )
281 {
282   bool changed( false );
283
284   if( Vector4::ZERO == color )
285   {
286     if( mOptionalColor )
287     {
288       // Discard unnecessary vector
289       delete mOptionalColor;
290       mOptionalColor = NULL;
291
292       changed = true;
293     }
294   }
295   else if( !mOptionalColor )
296   {
297     // Lazy allocation when non-default is set
298     mOptionalColor = new Vector4( color );
299     changed = true;
300   }
301   else if( *mOptionalColor != color )
302   {
303     *mOptionalColor = color;
304     changed = true;
305   }
306
307   return changed;
308 }
309
310 const Vector4* BlendingOptions::GetBlendColor() const
311 {
312   return mOptionalColor;
313 }
314
315 } // namespace Internal
316
317 } // namespace Dali