[dali_1.0.2] Merge branch 'tizen'
[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 #include <iostream>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 namespace ImageSampler
30 {
31
32 namespace
33 {
34
35 // Adjust these shift sizes if the FilterMode enum grows
36 const int MINIFY_BIT_SHIFT  = 0;    // Room for 16
37 const int MAGNIFY_BIT_SHIFT = 4;
38
39 const int MASK_MINIFY_FILTER    = 0x0000000F;
40 const int MASK_MAGNIFY_FILTER   = 0x000000F0;
41
42 const unsigned int FILTER_MODE_COUNT = 3;
43
44 FilterMode::Type FILTER_MODE_OPTIONS[ FILTER_MODE_COUNT ] =
45   { FilterMode::DEFAULT,
46     FilterMode::NEAREST,
47     FilterMode::LINEAR };
48
49 // These are the default sampling options - must match what is in GL
50 const FilterMode::Type DEFAULT_MINIFY = FilterMode::LINEAR;
51 const FilterMode::Type DEFAULT_MAGNIFY = FilterMode::LINEAR;
52
53 } // namespace
54
55 /**
56  * Utility to store one of the FilterMode values.
57  * @param[out] options A bitmask used to store the FilterMode values.
58  * @param[in] factor The FilterMode value.
59  * @param[in] bitshift Used to shift to the correct part of options.
60  */
61 void StoreFilterMode( unsigned int& options, FilterMode::Type mode, int bitShift )
62 {
63   // Start shifting from 1 as 0 is the unassigned state
64   switch ( mode )
65   {
66     case FilterMode::DEFAULT:
67     {
68       options |= ( 1u << bitShift );
69       break;
70     }
71     case FilterMode::NEAREST:
72     {
73       options |= ( 2u << bitShift );
74       break;
75     }
76     case FilterMode::LINEAR:
77     {
78       options |= ( 3u << bitShift );
79       break;
80     }
81   }
82 }
83
84 /**
85  * Utility to retrieve one of the FilterMode values.
86  * @param[in] options A bitmask of filter values.
87  * @param[in] mask The used to mask unwanted values.
88  * @param[in] bitshift Used to shift to the correct part of options.
89  * @return The filter mode.
90  */
91 bool RetrieveFilterMode( unsigned int options, int mask, int bitShift, FilterMode::Type& filterModeOut )
92 {
93   unsigned int index = options & mask;
94
95   if( index != 0 )
96   {
97     index = ( index >> bitShift ) - 1;    // Zero based index for array
98
99     DALI_ASSERT_DEBUG( index < FILTER_MODE_COUNT );
100
101     filterModeOut = FILTER_MODE_OPTIONS[ index ];
102     return true;
103   }
104
105   return false;
106 }
107
108 unsigned int DefaultOptions()
109 {
110   // Only initialise min filter as mag filter will use the system default
111   unsigned int bitfield = 0;
112   StoreFilterMode( bitfield, DEFAULT_MINIFY, MINIFY_BIT_SHIFT );
113   return bitfield;
114 }
115
116 bool IsMinifyAssigned( unsigned int bitfield )
117 {
118   FilterMode::Type filterModeDummy = FilterMode::NEAREST;
119   return RetrieveFilterMode( bitfield, MASK_MINIFY_FILTER, MINIFY_BIT_SHIFT, filterModeDummy );
120 }
121
122 bool IsMagnifyAssigned( unsigned int bitfield )
123 {
124   FilterMode::Type filterModeDummy = FilterMode::NEAREST;
125   return RetrieveFilterMode( bitfield, MASK_MAGNIFY_FILTER, MAGNIFY_BIT_SHIFT, filterModeDummy );
126 }
127
128 unsigned int PackBitfield( FilterMode::Type minify, FilterMode::Type magnify )
129 {
130   unsigned int bitfield = 0;
131   StoreFilterMode( bitfield, minify, MINIFY_BIT_SHIFT );
132   StoreFilterMode( bitfield, magnify, MAGNIFY_BIT_SHIFT );
133   return bitfield;
134 }
135
136 FilterMode::Type GetMinifyFilterMode( unsigned int bitfield )
137 {
138   FilterMode::Type filterMode = FilterMode::NEAREST;
139   RetrieveFilterMode( bitfield, MASK_MINIFY_FILTER, MINIFY_BIT_SHIFT, filterMode );
140   return filterMode;
141 }
142
143 FilterMode::Type GetMagnifyFilterMode( unsigned int bitfield )
144 {
145   FilterMode::Type filterMode = FilterMode::NEAREST;
146   RetrieveFilterMode( bitfield, MASK_MAGNIFY_FILTER, MAGNIFY_BIT_SHIFT, filterMode );
147   return filterMode;
148 }
149
150 } // namespace ImageSampler
151
152 } // namespace Internal
153
154 } // namespace Dali