2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/common/blending-options.h>
21 namespace // unnamed namespace
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;
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;
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
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.
49 void StoreBlendingFactor( unsigned int& options, BlendingFactor::Type factor, int bitShift )
53 case BlendingFactor::ZERO:
54 options |= ( 0u << bitShift );
57 case BlendingFactor::ONE:
58 options |= ( 1u << bitShift );
61 case BlendingFactor::SRC_COLOR:
62 options |= ( 2u << bitShift );
65 case BlendingFactor::ONE_MINUS_SRC_COLOR:
66 options |= ( 3u << bitShift );
69 case BlendingFactor::SRC_ALPHA:
70 options |= ( 4u << bitShift );
73 case BlendingFactor::ONE_MINUS_SRC_ALPHA:
74 options |= ( 5u << bitShift );
77 case BlendingFactor::DST_ALPHA:
78 options |= ( 6u << bitShift );
81 case BlendingFactor::ONE_MINUS_DST_ALPHA:
82 options |= ( 7u << bitShift );
85 case BlendingFactor::DST_COLOR:
86 options |= ( 8u << bitShift );
89 case BlendingFactor::ONE_MINUS_DST_COLOR:
90 options |= ( 9u << bitShift );
93 case BlendingFactor::SRC_ALPHA_SATURATE:
94 options |= ( 10u << bitShift );
97 case BlendingFactor::CONSTANT_COLOR:
98 options |= ( 11u << bitShift );
101 case BlendingFactor::ONE_MINUS_CONSTANT_COLOR:
102 options |= ( 12u << bitShift );
105 case BlendingFactor::CONSTANT_ALPHA:
106 options |= ( 13u << bitShift );
109 case BlendingFactor::ONE_MINUS_CONSTANT_ALPHA:
110 options |= ( 14u << bitShift );
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.
121 void StoreBlendingEquation( unsigned int& options, BlendingEquation::Type factor, int bitShift )
125 case BlendingEquation::ADD:
126 options |= ( 0u << bitShift );
129 case BlendingEquation::SUBTRACT:
130 options |= ( 1u << bitShift );
133 case BlendingEquation::REVERSE_SUBTRACT:
134 options |= ( 2u << bitShift );
139 const unsigned int BLENDING_FACTOR_COUNT = 15;
140 const unsigned int BLENDING_EQUATION_COUNT = 3;
142 BlendingFactor::Type BLENDING_FACTORS[ BLENDING_FACTOR_COUNT ] =
143 { BlendingFactor::ZERO,
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 };
159 BlendingEquation::Type BLENDING_EQUATIONS[ BLENDING_EQUATION_COUNT ] =
160 { BlendingEquation::ADD,
161 BlendingEquation::SUBTRACT,
162 BlendingEquation::REVERSE_SUBTRACT };
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.
171 BlendingFactor::Type RetrieveBlendingFactor( unsigned int options, int mask, int bitShift )
173 unsigned int index = options & mask;
174 index = index >> bitShift;
176 DALI_ASSERT_DEBUG( index < BLENDING_FACTOR_COUNT );
178 return BLENDING_FACTORS[ index ];
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.
188 BlendingEquation::Type RetrieveBlendingEquation( unsigned int options, int mask, int bitShift )
190 unsigned int index = options & mask;
191 index = index >> bitShift;
193 DALI_ASSERT_DEBUG( index < BLENDING_EQUATION_COUNT );
195 return BLENDING_EQUATIONS[ index ];
198 } // unnamed namespace
206 BlendingOptions::BlendingOptions()
208 mOptionalColor( NULL )
210 SetBlendFunc( DEFAULT_BLENDING_SRC_FACTOR_RGB, DEFAULT_BLENDING_DEST_FACTOR_RGB,
211 DEFAULT_BLENDING_SRC_FACTOR_ALPHA, DEFAULT_BLENDING_DEST_FACTOR_ALPHA );
213 SetBlendEquation( DEFAULT_BLENDING_EQUATION_RGB, DEFAULT_BLENDING_EQUATION_ALPHA );
216 BlendingOptions::~BlendingOptions()
218 delete mOptionalColor;
221 void BlendingOptions::SetBitmask( unsigned int bitmask )
226 unsigned int BlendingOptions::GetBitmask() const
231 void BlendingOptions::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
232 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
234 mBitmask &= CLEAR_BLEND_FUNC_MASK; // Clear the BlendFunc values
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 );
242 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorRgb() const
244 return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_RGB, SHIFT_TO_SRC_FACTOR_RGB );
247 BlendingFactor::Type BlendingOptions::GetBlendDestFactorRgb() const
249 return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_RGB, SHIFT_TO_DEST_FACTOR_RGB );
252 BlendingFactor::Type BlendingOptions::GetBlendSrcFactorAlpha() const
254 return RetrieveBlendingFactor( mBitmask, MASK_SRC_FACTOR_ALPHA, SHIFT_TO_SRC_FACTOR_ALPHA );
257 BlendingFactor::Type BlendingOptions::GetBlendDestFactorAlpha() const
259 return RetrieveBlendingFactor( mBitmask, MASK_DEST_FACTOR_ALPHA, SHIFT_TO_DEST_FACTOR_ALPHA );
262 void BlendingOptions::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
264 mBitmask &= CLEAR_BLEND_EQUATION_MASK; // Clear the BlendEquation values
266 StoreBlendingEquation( mBitmask, equationRgb, SHIFT_TO_EQUATION_RGB );
267 StoreBlendingEquation( mBitmask, equationAlpha, SHIFT_TO_EQUATION_ALPHA );
270 BlendingEquation::Type BlendingOptions::GetBlendEquationRgb() const
272 return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_RGB, SHIFT_TO_EQUATION_RGB );
275 BlendingEquation::Type BlendingOptions::GetBlendEquationAlpha() const
277 return RetrieveBlendingEquation( mBitmask, MASK_EQUATION_ALPHA, SHIFT_TO_EQUATION_ALPHA );
280 bool BlendingOptions::SetBlendColor( const Vector4& color )
282 bool changed( false );
284 if( Vector4::ZERO == color )
288 // Discard unnecessary vector
289 delete mOptionalColor;
290 mOptionalColor = NULL;
295 else if( !mOptionalColor )
297 // Lazy allocation when non-default is set
298 mOptionalColor = new Vector4( color );
301 else if( *mOptionalColor != color )
303 *mOptionalColor = color;
310 const Vector4* BlendingOptions::GetBlendColor() const
312 return mOptionalColor;
315 } // namespace Internal