Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / internal / common / image-sampler.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/image-sampler.h>
20
21 // EXTERNAL INCLUDES
22 #include <iosfwd>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace ImageSampler
31 {
32
33 namespace
34 {
35
36 // Adjust these shift sizes if the FilterMode enum grows
37 const int MINIFY_BIT_SHIFT  = 0;    // Room for 16
38 const int MAGNIFY_BIT_SHIFT = 4;
39
40 const int MASK_MINIFY_FILTER    = 0x0000000F;
41 const int MASK_MAGNIFY_FILTER   = 0x000000F0;
42
43 const unsigned int FILTER_MODE_COUNT = 4;
44
45 FilterMode::Type FILTER_MODE_OPTIONS[ FILTER_MODE_COUNT ] =
46   { FilterMode::NONE,
47     FilterMode::DEFAULT,
48     FilterMode::NEAREST,
49     FilterMode::LINEAR };
50
51 } // namespace
52
53 /**
54  * Utility to store one of the FilterMode values.
55  * @param[out] options A bitmask used to store the FilterMode values.
56  * @param[in] factor The FilterMode value.
57  * @param[in] bitshift Used to shift to the correct part of options.
58  */
59 void StoreFilterMode( unsigned int& options, FilterMode::Type mode, int bitShift )
60 {
61   // Start shifting from 1 as 0 is the unassigned state
62   switch ( mode )
63   {
64     case FilterMode::NONE:
65     {
66       // Nothing to do
67       break;
68     }
69     case FilterMode::DEFAULT:
70     {
71       options |= ( 1u << bitShift );
72       break;
73     }
74     case FilterMode::NEAREST:
75     {
76       options |= ( 2u << bitShift );
77       break;
78     }
79     case FilterMode::LINEAR:
80     {
81       options |= ( 3u << bitShift );
82       break;
83     }
84   }
85 }
86
87 /**
88  * Utility to retrieve one of the FilterMode values.
89  * @param[in] options A bitmask of filter values.
90  * @param[in] mask The used to mask unwanted values.
91  * @param[in] bitshift Used to shift to the correct part of options.
92  * @return Return the filter mode.
93  */
94 FilterMode::Type RetrieveFilterMode( unsigned int options, int mask, int bitShift )
95 {
96   unsigned int index = options & mask;
97
98   index = ( index >> bitShift );    // Zero based index for array
99
100   DALI_ASSERT_DEBUG( index < FILTER_MODE_COUNT );
101
102   return FILTER_MODE_OPTIONS[ index ];
103 }
104
105 unsigned int PackBitfield( FilterMode::Type minify, FilterMode::Type magnify )
106 {
107   unsigned int bitfield = 0;
108   StoreFilterMode( bitfield, minify, MINIFY_BIT_SHIFT );
109   StoreFilterMode( bitfield, magnify, MAGNIFY_BIT_SHIFT );
110   return bitfield;
111 }
112
113 FilterMode::Type GetMinifyFilterMode( unsigned int bitfield )
114 {
115   return RetrieveFilterMode( bitfield, MASK_MINIFY_FILTER, MINIFY_BIT_SHIFT );
116 }
117
118 FilterMode::Type GetMagnifyFilterMode( unsigned int bitfield )
119 {
120   return RetrieveFilterMode( bitfield, MASK_MAGNIFY_FILTER, MAGNIFY_BIT_SHIFT );
121 }
122
123 } // namespace ImageSampler
124
125 } // namespace Internal
126
127 } // namespace Dali