Add post processor
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-sampler.cpp
1 /*
2  * Copyright (c) 2021 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 #include "test-graphics-sampler.h"
18
19 namespace Dali
20 {
21 std::map<uint32_t, GLint> TestGraphicsSampler::mParamCache;
22
23 TestGraphicsSampler::TestGraphicsSampler(TestGlAbstraction& glAbstraction, const Graphics::SamplerCreateInfo& createInfo)
24 : mGlAbstraction(glAbstraction),
25   mCreateInfo(createInfo)
26 {
27 }
28
29 GLint TestGraphicsSampler::FilterModeToGL(Graphics::SamplerFilter filterMode)
30 {
31   switch(filterMode)
32   {
33     case Graphics::SamplerFilter::NEAREST:
34     {
35       return GL_NEAREST;
36     }
37     case Graphics::SamplerFilter::LINEAR:
38     {
39       return GL_LINEAR;
40     }
41   }
42   return GL_LINEAR;
43 }
44
45 GLint TestGraphicsSampler::FilterModeToGL(Graphics::SamplerFilter filterMode, Graphics::SamplerMipmapMode mipmapMode)
46 {
47   if(filterMode == Graphics::SamplerFilter::NEAREST)
48   {
49     switch(mipmapMode)
50     {
51       case Graphics::SamplerMipmapMode::NONE:
52         return GL_NEAREST;
53       case Graphics::SamplerMipmapMode::NEAREST:
54         return GL_NEAREST_MIPMAP_NEAREST;
55       case Graphics::SamplerMipmapMode::LINEAR:
56         return GL_NEAREST_MIPMAP_LINEAR;
57     }
58   }
59   else
60   {
61     switch(mipmapMode)
62     {
63       case Graphics::SamplerMipmapMode::NONE:
64         return GL_LINEAR;
65       case Graphics::SamplerMipmapMode::NEAREST:
66         return GL_LINEAR_MIPMAP_NEAREST;
67       case Graphics::SamplerMipmapMode::LINEAR:
68         return GL_LINEAR_MIPMAP_LINEAR;
69     }
70   }
71   return GL_LINEAR;
72 }
73
74 /**
75  * @brief Convert from a WrapMode to its corresponding GL enumeration
76  * @param[in] wrapMode The wrap mode
77  * @param[in] defaultWrapMode The mode to use if WrapMode is Default
78  * @return The equivalent GL wrap mode
79  */
80 GLint TestGraphicsSampler::WrapModeToGL(Graphics::SamplerAddressMode wrapMode)
81 {
82   switch(wrapMode)
83   {
84     case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
85     {
86       return GL_CLAMP_TO_EDGE;
87     }
88     case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
89     {
90       return GL_CLAMP_TO_EDGE;
91     }
92     case Graphics::SamplerAddressMode::REPEAT:
93     {
94       return GL_REPEAT;
95     }
96     case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
97     {
98       return GL_MIRRORED_REPEAT;
99     }
100     case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
101     {
102       return GL_MIRRORED_REPEAT;
103     }
104   }
105   return GL_REPEAT;
106 }
107
108 void TestGraphicsSampler::Apply(GLuint target)
109 {
110   SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MIN_FILTER, FilterModeToGL(mCreateInfo.minFilter, mCreateInfo.mipMapMode));
111   SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MAG_FILTER, FilterModeToGL(mCreateInfo.magFilter));
112   SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_S, WrapModeToGL(mCreateInfo.addressModeU));
113   SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_T, WrapModeToGL(mCreateInfo.addressModeV));
114   if(target == GL_TEXTURE_CUBE_MAP)
115   {
116     TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_R, WrapModeToGL(mCreateInfo.addressModeW));
117   }
118 }
119
120 uint32_t TestGraphicsSampler::GetTexParamHash(TestGlAbstraction& glAbstraction, GLuint target, GLenum pname)
121 {
122   uint32_t targetFlags = 0;
123   switch(target)
124   {
125     case GL_TEXTURE_2D:
126       targetFlags = 0x01;
127       break;
128     case GL_TEXTURE_CUBE_MAP:
129       targetFlags = 0x02;
130       break;
131     default:
132       targetFlags = 0x03;
133       break;
134   }
135   switch(pname)
136   {
137     case GL_TEXTURE_WRAP_S:
138       targetFlags |= (0x01) << 2;
139       break;
140     case GL_TEXTURE_WRAP_T:
141       targetFlags |= (0x02) << 2;
142       break;
143     case GL_TEXTURE_WRAP_R:
144       targetFlags |= (0x03) << 2;
145       break;
146     case GL_TEXTURE_MAG_FILTER:
147       targetFlags |= (0x04) << 2;
148       break;
149     case GL_TEXTURE_MIN_FILTER:
150       targetFlags |= (0x05) << 2;
151       break;
152     default:
153       targetFlags |= (0x07) << 2;
154       break;
155   }
156   auto& textures = glAbstraction.GetBoundTextures(glAbstraction.GetActiveTextureUnit());
157   targetFlags |= (textures.back() << 5);
158
159   return targetFlags;
160 }
161
162 void TestGraphicsSampler::SetTexParameter(TestGlAbstraction& glAbstraction, GLuint target, GLenum pname, GLint value)
163 {
164   // Works on the currently active texture
165
166   uint32_t hash = GetTexParamHash(glAbstraction, target, pname);
167
168   if(mParamCache.find(hash) != mParamCache.end())
169   {
170     if(mParamCache[hash] != value)
171     {
172       mParamCache[hash] = value;
173       glAbstraction.TexParameteri(target, pname, value);
174     }
175   }
176   else
177   {
178     mParamCache[hash] = value;
179     glAbstraction.TexParameteri(target, pname, value);
180   }
181 }
182
183 } // namespace Dali