Merge "Test harness sync" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-gl-abstraction.cpp
1 /*
2  * Copyright (c) 2023 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 #include "test-gl-abstraction.h"
19 #include "test-trace-call-stack.h"
20
21 static const bool TRACE{false};
22
23 uint32_t GetGLDataTypeSize(GLenum type)
24 {
25   // There are many more types than what are covered here, but
26   // they are not supported in dali.
27   switch(type)
28   {
29     case GL_FLOAT: // "float", 1 float, 4 bytes
30       return 4;
31     case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
32       return 8;
33     case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
34       return 12;
35     case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
36       return 16;
37     case GL_INT: // "int", 1 integer, 4 bytes
38       return 4;
39     case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
40       return 16;
41     case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
42       return 36;
43     case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
44       return 64;
45     default:
46       return 0;
47   }
48 }
49
50 namespace Dali
51 {
52 TestGlAbstraction::TestGlAbstraction()
53 : mBufferTrace(TRACE, std::string("gl")),
54   mCullFaceTrace(TRACE, "gl"),
55   mEnableDisableTrace(TRACE, "gl"),
56   mShaderTrace(TRACE, "gl"),
57   mTextureTrace(TRACE, std::string("gl")),
58   mTexParameterTrace(TRACE, "gl"),
59   mDrawTrace(TRACE, "gl"),
60   mDepthFunctionTrace(TRACE, "gl"),
61   mStencilFunctionTrace(TRACE, "gl"),
62   mScissorTrace(TRACE, "gl"),
63   mSetUniformTrace(TRACE, "Uniform "),
64   mViewportTrace(TRACE, "gl")
65 {
66   Initialize();
67 }
68
69 TestGlAbstraction::~TestGlAbstraction()
70 {
71 }
72
73 void TestGlAbstraction::Initialize()
74 {
75   mCurrentProgram                         = 0;
76   mCompileStatus                          = GL_TRUE;
77   mLinkStatus                             = GL_TRUE;
78   mGetErrorResult                         = 0;
79   mGetStringResult                        = NULL;
80   mIsBufferResult                         = 0;
81   mIsEnabledResult                        = 0;
82   mIsFramebufferResult                    = 0;
83   mIsProgramResult                        = 0;
84   mIsRenderbufferResult                   = 0;
85   mIsShaderResult                         = 0;
86   mIsTextureResult                        = 0;
87   mActiveTextureUnit                      = 0;
88   mCheckFramebufferStatusResult           = 0;
89   mFramebufferStatus                      = 0;
90   mFramebufferDepthAttached               = 0;
91   mFramebufferStencilAttached             = 0;
92   mFramebufferDepthStencilAttached        = 0;
93   mFramebufferColorAttachmentCount        = 0;
94   mFrameBufferColorStatus                 = 0;
95   mFramebufferDepthAttachmentCount        = 0;
96   mFramebufferStencilAttachmentCount      = 0;
97   mFramebufferDepthStencilAttachmentCount = 0;
98   mNumBinaryFormats                       = 0;
99   mBinaryFormats                          = 0;
100   mProgramBinaryLength                    = 0;
101
102   mVertexAttribArrayChanged = false;
103   mGetProgramBinaryCalled   = false;
104
105   mLastShaderCompiled = 0;
106   mLastClearBitMask   = 0;
107   mLastClearColor     = Color::TRANSPARENT;
108   mClearCount         = 0;
109
110   mLastBlendEquationRgb   = 0;
111   mLastBlendEquationAlpha = 0;
112   mLastBlendFuncSrcRgb    = 0;
113   mLastBlendFuncDstRgb    = 0;
114   mLastBlendFuncSrcAlpha  = 0;
115   mLastBlendFuncDstAlpha  = 0;
116   mLastAutoTextureIdUsed  = 0;
117   mNumGeneratedTextures   = 0;
118   mLastShaderIdUsed       = 0;
119   mLastProgramIdUsed      = 0;
120   mLastUniformIdUsed      = 0;
121   mLastDepthMask          = false;
122
123   mUniforms.clear();
124   mProgramUniforms1i.clear();
125   mProgramUniforms1f.clear();
126   mProgramUniforms2f.clear();
127   mProgramUniforms3f.clear();
128   mProgramUniforms4f.clear();
129
130   mAttribLocs  = {"aPosition", "aTexCoord"};
131   mAttribTypes = {GL_FLOAT, GL_FLOAT};
132
133   mCullFaceTrace.Reset();
134   mDepthFunctionTrace.Reset();
135   mEnableDisableTrace.Reset();
136   mShaderTrace.Reset();
137   mStencilFunctionTrace.Reset();
138   mScissorTrace.Reset();
139   mTextureTrace.Reset();
140   mTexParameterTrace.Reset();
141   mDrawTrace.Reset();
142
143   for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
144   {
145     mVertexAttribArrayState[i] = false;
146   }
147
148   mActiveUniforms = std::vector<ActiveUniform>{
149     {"uRendererColor", GL_FLOAT, 1},
150     {"uCustom", GL_FLOAT_VEC3, 1},
151     {"uCustom3", GL_FLOAT_VEC3, 1},
152     {"uFadeColor", GL_FLOAT_VEC4, 1},
153     {"uUniform1", GL_FLOAT_VEC4, 1},
154     {"uUniform2", GL_FLOAT_VEC4, 1},
155     {"uUniform3", GL_FLOAT_VEC4, 1},
156     {"uFadeProgress", GL_FLOAT, 1},
157     {"uANormalMatrix", GL_FLOAT_MAT3, 1},
158     {"sEffect", GL_SAMPLER_2D, 1},
159     {"sTexture", GL_SAMPLER_2D, 1},
160     {"sTextureRect", GL_SAMPLER_2D, 1},
161     {"sGloss", GL_SAMPLER_2D, 1},
162     {"uColor", GL_FLOAT_VEC4, 1},
163     {"uActorColor", GL_FLOAT_VEC4, 1},
164     {"uModelMatrix", GL_FLOAT_MAT4, 1},
165     {"uModelView", GL_FLOAT_MAT4, 1},
166     {"uMvpMatrix", GL_FLOAT_MAT4, 1},
167     {"uNormalMatrix", GL_FLOAT_MAT4, 1},
168     {"uProjection", GL_FLOAT_MAT4, 1},
169     {"uSize", GL_FLOAT_VEC3, 1},
170     {"uViewMatrix", GL_FLOAT_MAT4, 1},
171     {"uLightCameraProjectionMatrix", GL_FLOAT_MAT4, 1},
172     {"uLightCameraViewMatrix", GL_FLOAT_MAT4, 1}};
173
174   int offset = 0;
175   for(uint32_t i = 0; i < mActiveUniforms.size(); ++i)
176   {
177     mActiveUniforms[i].offset = offset;
178     offset += mActiveUniforms[i].size * GetGLDataTypeSize(mActiveUniforms[i].type);
179   }
180   // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE UNIFORMS IN test-graphics-reflection.cpp
181 }
182
183 void TestGlAbstraction::PreRender()
184 {
185 }
186
187 void TestGlAbstraction::PostRender()
188 {
189 }
190
191 bool TestGlAbstraction::IsSurfacelessContextSupported() const
192 {
193   return true;
194 }
195
196 bool TestGlAbstraction::IsAdvancedBlendEquationSupported()
197 {
198   return true;
199 }
200
201 bool TestGlAbstraction::IsMultisampledRenderToTextureSupported()
202 {
203   return true;
204 }
205
206 bool TestGlAbstraction::IsBlendEquationSupported(DevelBlendEquation::Type blendEquation)
207 {
208   return true;
209 }
210
211 std::string TestGlAbstraction::GetShaderVersionPrefix()
212 {
213   return std::string("");
214 }
215
216 std::string TestGlAbstraction::GetVertexShaderPrefix()
217 {
218   return std::string("");
219 }
220
221 std::string TestGlAbstraction::GetFragmentShaderPrefix()
222 {
223   return std::string("");
224 }
225
226 bool TestGlAbstraction::TextureRequiresConverting(const GLenum imageGlFormat, const GLenum textureGlFormat, const bool isSubImage) const
227 {
228   return ((imageGlFormat == GL_RGB) && (textureGlFormat == GL_RGBA));
229 }
230
231 void TestGlAbstraction::SetActiveUniforms(const std::vector<ActiveUniform>& uniforms)
232 {
233   mActiveUniforms = uniforms;
234   int offset      = 0;
235   for(uint32_t i = 0; i < uniforms.size(); ++i)
236   {
237     mActiveUniforms[i].offset = offset;
238     offset += mActiveUniforms[i].size * GetGLDataTypeSize(mActiveUniforms[i].type);
239   }
240 }
241
242 } // namespace Dali
243
244 bool BlendEnabled(const Dali::TraceCallStack& callStack)
245 {
246   std::stringstream out;
247   out << GL_BLEND;
248   bool blendEnabled = callStack.FindMethodAndParams("Enable", out.str());
249   return blendEnabled;
250 }
251
252 bool BlendDisabled(const Dali::TraceCallStack& callStack)
253 {
254   std::stringstream out;
255   out << GL_BLEND;
256   bool blendEnabled = callStack.FindMethodAndParams("Disable", out.str());
257   return blendEnabled;
258 }