2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/common/blending-options.h>
20 namespace // unnamed namespace
25 const int MASK_SRC_FACTOR_RGB = 0x0000000F;
26 const int MASK_SRC_FACTOR_ALPHA = 0x000000F0;
27 const int MASK_DEST_FACTOR_RGB = 0x00000F00;
28 const int MASK_DEST_FACTOR_ALPHA = 0x0000F000;
29 const int MASK_EQUATION_RGB = 0x000F0000;
30 const int MASK_EQUATION_ALPHA = 0x00F00000;
32 const int SHIFT_TO_SRC_FACTOR_RGB = 0;
33 const int SHIFT_TO_SRC_FACTOR_ALPHA = 4;
34 const int SHIFT_TO_DEST_FACTOR_RGB = 8;
35 const int SHIFT_TO_DEST_FACTOR_ALPHA = 12;
36 const int SHIFT_TO_EQUATION_RGB = 16;
37 const int SHIFT_TO_EQUATION_ALPHA = 20;
39 static unsigned int CLEAR_BLEND_FUNC_MASK = 0xFFFF0000; // Bottom 16 bits cleared
40 static unsigned int CLEAR_BLEND_EQUATION_MASK = 0xFF00FFFF; // 8 bits cleared
43 * Utility to store one of the BlendFunc values.
44 * @param[out] options A bitmask used to store the BlendFunc values.
45 * @param[in] factor The BlendFunc value.
46 * @param[in] bitshift Used to shift to the correct part of options.
48 void StoreBlendingFactor( unsigned int& options, BlendingFactor::Type factor, int bitShift )
52 case BlendingFactor::ZERO:
53 options |= ( 0u << bitShift );
56 case BlendingFactor::ONE:
57 options |= ( 1u << bitShift );
60 case BlendingFactor::SRC_COLOR:
61 options |= ( 2u << bitShift );
64 case BlendingFactor::ONE_MINUS_SRC_COLOR:
65 options |= ( 3u << bitShift );
68 case BlendingFactor::SRC_ALPHA:
69 options |= ( 4u << bitShift );
72 case BlendingFactor::ONE_MINUS_SRC_ALPHA:
73 options |= ( 5u << bitShift );
76 case BlendingFactor::DST_ALPHA:
77 options |= ( 6u << bitShift );
80 case BlendingFactor::ONE_MINUS_DST_ALPHA:
81 options |= ( 7u << bitShift );
84 case BlendingFactor::DST_COLOR:
85 options |= ( 8u << bitShift );
88 case BlendingFactor::ONE_MINUS_DST_COLOR:
89 options |= ( 9u << bitShift );
92 case BlendingFactor::SRC_ALPHA_SATURATE:
93 options |= ( 10u << bitShift );
96 case BlendingFactor::CONSTANT_COLOR:
97 options |= ( 11u << bitShift );
100 case BlendingFactor::ONE_MINUS_CONSTANT_COLOR:
101 options |= ( 12u << bitShift );
104 case BlendingFactor::CONSTANT_ALPHA:
105 options |= ( 13u << bitShift );
108 case BlendingFactor::ONE_MINUS_CONSTANT_ALPHA:
109 options |= ( 14u << bitShift );
113 DALI_ASSERT_ALWAYS( false && "Invalid BlendingFactor" );
119 * Utility to store one of the BlendEquation values.
120 * @param[out] options A bitmask used to store the BlendEquation values.
121 * @param[in] factor The BlendEquation value.
122 * @param[in] bitshift Used to shift to the correct part of options.
124 void StoreBlendingEquation( unsigned int& options, BlendingEquation::Type factor, int bitShift )
128 case BlendingEquation::ADD:
129 options |= ( 0u << bitShift );
132 case BlendingEquation::SUBTRACT:
133 options |= ( 1u << bitShift );
136 case BlendingEquation::REVERSE_SUBTRACT:
137 options |= ( 2u << bitShift );
141 DALI_ASSERT_ALWAYS( false && "Invalid BlendingEquation" );
146 const unsigned int BLENDING_FACTOR_COUNT = 15;
147 const unsigned int BLENDING_EQUATION_COUNT = 3;
149 BlendingFactor::Type BLENDING_FACTORS[ BLENDING_FACTOR_COUNT ] =
150 { BlendingFactor::ZERO,
152 BlendingFactor::SRC_COLOR,
153 BlendingFactor::ONE_MINUS_SRC_COLOR,
154 BlendingFactor::SRC_ALPHA,
155 BlendingFactor::ONE_MINUS_SRC_ALPHA,
156 BlendingFactor::DST_ALPHA,
157 BlendingFactor::ONE_MINUS_DST_ALPHA,
158 BlendingFactor::DST_COLOR,
159 BlendingFactor::ONE_MINUS_DST_COLOR,
160 BlendingFactor::SRC_ALPHA_SATURATE,
161 BlendingFactor::CONSTANT_COLOR,
162 BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
163 BlendingFactor::CONSTANT_ALPHA,
164 BlendingFactor::ONE_MINUS_CONSTANT_ALPHA };
166 BlendingEquation::Type BLENDING_EQUATIONS[ BLENDING_EQUATION_COUNT ] =
167 { BlendingEquation::ADD,
168 BlendingEquation::SUBTRACT,
169 BlendingEquation::REVERSE_SUBTRACT };
172 * Utility to retrieve one of the BlendFunc values.
173 * @param[in] options A bitmask of blending values.
174 * @param[in] mask The used to mask unwanted values.
175 * @param[in] bitshift Used to shift to the correct part of options.
176 * @return The blending factor.
178 BlendingFactor::Type RetrieveBlendingFactor( unsigned int options, int mask, int bitShift )
180 unsigned int index = options & mask;
181 index = index >> bitShift;
183 DALI_ASSERT_DEBUG( index < BLENDING_FACTOR_COUNT );
185 return BLENDING_FACTORS[ index ];
189 * Utility to retrieve one of the BlendEquation values.
190 * @param[in] options A bitmask of blending values.
191 * @param[in] mask The used to mask unwanted values.
192 * @param[in] bitshift Used to shift to the correct part of options.
193 * @return The blending equation.
195 BlendingEquation::Type RetrieveBlendingEquation( unsigned int options, int mask, int bitShift )
197 unsigned int index = options & mask;
198 index = index >> bitShift;
200 DALI_ASSERT_DEBUG( index < BLENDING_EQUATION_COUNT );
202 return BLENDING_EQUATIONS[ index ];
205 } // unnamed namespace
213 BlendingOptions::BlendingOptions()
215 mOptionalColor( NULL )
217 SetBlendFunc( DEFAULT_BLENDING_SRC_FACTOR_RGB, DEFAULT_BLENDING_DEST_FACTOR_RGB,
218 DEFAULT_BLENDING_SRC_FACTOR_ALPHA, DEFAULT_BLENDING_DEST_FACTOR_ALPHA );
220 SetBlendEquation( DEFAULT_BLENDING_EQUATION_RGB, DEFAULT_BLENDING_EQUATION_ALPHA );
223 BlendingOptions::~BlendingOptions()
225 delete mOptionalColor;
228 void BlendingOptions::SetBitmask( unsigned int bitmask )
233 unsigned int BlendingOptions::GetBitmask() const
238 void BlendingOptions::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
239 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
241 mBitmask &= CLEAR_BLEND_FUNC_MASK; // Clear the BlendFunc values
243 StoreBlendingFactor( mBitmask, srcFactorRgb, SHIFT_TO_SRC_FACTOR_RGB );
244 StoreBlendingFactor( mBitmask, destFactorRgb, SHIFT_TO_DEST_FACTOR_RGB );
245 StoreBlendingFactor( mBitmask, srcFactorAlpha, SHIFT_TO_SRC_FACTOR_ALPHA );
246 StoreBlendingFactor( mBitmask, destFactorAlpha, SHIFT_TO_DEST_FACTOR_ALPHA );
249 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorRgb() const
251 return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_RGB, SHIFT_TO_SRC_FACTOR_RGB );
254 BlendingFactor::Type BlendingOptions::GetBlendDestFactorRgb() const
256 return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_RGB, SHIFT_TO_DEST_FACTOR_RGB );
259 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorAlpha() const
261 return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_ALPHA, SHIFT_TO_SRC_FACTOR_ALPHA );
264 BlendingFactor::Type BlendingOptions::GetBlendDestFactorAlpha() const
266 return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_ALPHA, SHIFT_TO_DEST_FACTOR_ALPHA );
269 void BlendingOptions::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
271 mBitmask &= CLEAR_BLEND_EQUATION_MASK; // Clear the BlendEquation values
273 StoreBlendingEquation( mBitmask, equationRgb, SHIFT_TO_EQUATION_RGB );
274 StoreBlendingEquation( mBitmask, equationAlpha, SHIFT_TO_EQUATION_ALPHA );
277 BlendingEquation::Type BlendingOptions::GetBlendEquationRgb() const
279 return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_RGB, SHIFT_TO_EQUATION_RGB );
282 BlendingEquation::Type BlendingOptions::GetBlendEquationAlpha() const
284 return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_ALPHA, SHIFT_TO_EQUATION_ALPHA );
287 bool BlendingOptions::SetBlendColor( const Vector4& color )
289 bool changed( false );
291 if( Vector4::ZERO == color )
295 // Discard unnecessary vector
296 delete mOptionalColor;
297 mOptionalColor = NULL;
302 else if( !mOptionalColor )
304 // Lazy allocation when non-default is set
305 mOptionalColor = new Vector4( color );
308 else if( *mOptionalColor != color )
310 *mOptionalColor = color;
317 const Vector4* BlendingOptions::GetBlendColor() const
319 return mOptionalColor;
322 } // namespace Internal