am c807fa97: (-s ours) am c0636411: am 0870de9a: DO NOT MERGE Do not require signed...
[platform/upstream/VK-GL-CTS.git] / modules / egl / teglRenderCase.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program EGL Module
3  * ---------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Base class for rendering tests.
22  *//*--------------------------------------------------------------------*/
23
24 #include "teglRenderCase.hpp"
25
26 #include "teglSimpleConfigCase.hpp"
27
28 #include "egluNativeDisplay.hpp"
29 #include "egluNativeWindow.hpp"
30 #include "egluNativePixmap.hpp"
31 #include "egluUtil.hpp"
32 #include "egluUnique.hpp"
33
34 #include "eglwLibrary.hpp"
35 #include "eglwEnums.hpp"
36
37 #include "tcuRenderTarget.hpp"
38 #include "tcuTestLog.hpp"
39 #include "tcuCommandLine.hpp"
40
41 #include "deStringUtil.hpp"
42 #include "deUniquePtr.hpp"
43
44 #include <algorithm>
45 #include <iterator>
46 #include <memory>
47 #include <set>
48
49 namespace deqp
50 {
51 namespace egl
52 {
53
54 using std::string;
55 using std::vector;
56 using std::set;
57 using tcu::TestLog;
58 using namespace eglw;
59
60 static void postSurface (const Library& egl, EGLDisplay display, EGLSurface surface, EGLint typeBit)
61 {
62         if (typeBit == EGL_WINDOW_BIT)
63                 EGLU_CHECK_CALL(egl, swapBuffers(display, surface));
64         else if (typeBit == EGL_PIXMAP_BIT)
65                 EGLU_CHECK_CALL(egl, waitClient());
66         else if (typeBit == EGL_PBUFFER_BIT)
67                 EGLU_CHECK_CALL(egl, waitClient());
68         else
69                 DE_ASSERT(false);
70 }
71
72 // RenderCase
73
74 RenderCase::RenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint apiMask, EGLint surfaceTypeMask, const eglu::FilterList& filters)
75         : SimpleConfigCase      (eglTestCtx, name, description, filters)
76         , m_apiMask                     (apiMask)
77         , m_surfaceTypeMask     (surfaceTypeMask)
78 {
79 }
80
81 RenderCase::~RenderCase (void)
82 {
83 }
84
85 EGLint getBuildClientAPIMask (void)
86 {
87         EGLint apiMask = 0;
88
89         // Always supported regardless of flags - dynamically loaded
90         apiMask |= EGL_OPENGL_ES2_BIT;
91         apiMask |= EGL_OPENGL_ES3_BIT;
92         apiMask |= EGL_OPENGL_BIT;
93
94 #if defined(DEQP_SUPPORT_GLES1)
95         apiMask |= EGL_OPENGL_ES_BIT;
96 #endif
97
98 #if defined(DEQP_SUPPORT_VG)
99         apiMask |= EGL_OPENVG_BIT;
100 #endif
101
102         return apiMask;
103 }
104
105 static void checkBuildClientAPISupport (EGLint requiredAPIs)
106 {
107         const EGLint    builtClientAPIs         = getBuildClientAPIMask();
108
109         if ((requiredAPIs & builtClientAPIs) != requiredAPIs)
110                 TCU_THROW(InternalError, "Test case requires client API not supported in current build");
111 }
112
113 void RenderCase::executeForConfig (EGLDisplay display, EGLConfig config)
114 {
115         const Library&                                          egl                             = m_eglTestCtx.getLibrary();
116         tcu::TestLog&                                           log                             = m_testCtx.getLog();
117         const int                                                       width                   = 128;
118         const int                                                       height                  = 128;
119         const EGLint                                            configId                = eglu::getConfigID(egl, display, config);
120
121         const eglu::NativeDisplayFactory&       displayFactory  = m_eglTestCtx.getNativeDisplayFactory();
122         eglu::NativeDisplay&                            nativeDisplay   = m_eglTestCtx.getNativeDisplay();
123
124         bool                                                            isOk                    = true;
125         string                                                          failReason              = "";
126
127         if (m_surfaceTypeMask & EGL_WINDOW_BIT)
128         {
129                 tcu::ScopedLogSection(log,
130                                                           string("Config") + de::toString(configId) + "-Window",
131                                                           string("Config ID ") + de::toString(configId) + ", window surface");
132
133                 const eglu::NativeWindowFactory&        windowFactory   = eglu::selectNativeWindowFactory(displayFactory, m_testCtx.getCommandLine());
134
135                 try
136                 {
137                         const eglu::WindowParams                        params          (width, height, eglu::parseWindowVisibility(m_testCtx.getCommandLine()));
138                         de::UniquePtr<eglu::NativeWindow>       window          (windowFactory.createWindow(&nativeDisplay, display, config, DE_NULL, params));
139                         EGLSurface                                                      eglSurface      = createWindowSurface(nativeDisplay, *window, display, config, DE_NULL);
140                         eglu::UniqueSurface                                     surface         (egl, display, eglSurface);
141
142                         executeForSurface(display, *surface, Config(config, EGL_WINDOW_BIT, 0));
143                 }
144                 catch (const tcu::TestError& e)
145                 {
146                         log << e;
147                         isOk = false;
148                         failReason = e.what();
149                 }
150         }
151
152         if (m_surfaceTypeMask & EGL_PIXMAP_BIT)
153         {
154                 tcu::ScopedLogSection(log,
155                                                           string("Config") + de::toString(configId) + "-Pixmap",
156                                                           string("Config ID ") + de::toString(configId) + ", pixmap surface");
157
158                 const eglu::NativePixmapFactory&        pixmapFactory   = eglu::selectNativePixmapFactory(displayFactory, m_testCtx.getCommandLine());
159
160                 try
161                 {
162                         std::auto_ptr<eglu::NativePixmap>       pixmap          (pixmapFactory.createPixmap(&nativeDisplay, display, config, DE_NULL, width, height));
163                         EGLSurface                                                      eglSurface      = createPixmapSurface(nativeDisplay, *pixmap, display, config, DE_NULL);
164                         eglu::UniqueSurface                                     surface         (egl, display, eglSurface);
165
166                         executeForSurface(display, *surface, Config(config, EGL_PIXMAP_BIT, 0));
167                 }
168                 catch (const tcu::TestError& e)
169                 {
170                         log << e;
171                         isOk = false;
172                         failReason = e.what();
173                 }
174         }
175
176         if (m_surfaceTypeMask & EGL_PBUFFER_BIT)
177         {
178                 tcu::ScopedLogSection(log,
179                                                           string("Config") + de::toString(configId) + "-Pbuffer",
180                                                           string("Config ID ") + de::toString(configId) + ", pbuffer surface");
181                 try
182                 {
183                         const EGLint surfaceAttribs[] =
184                         {
185                                 EGL_WIDTH,      width,
186                                 EGL_HEIGHT,     height,
187                                 EGL_NONE
188                         };
189
190                         eglu::UniqueSurface surface(egl, display, egl.createPbufferSurface(display, config, surfaceAttribs));
191                         EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
192
193                         executeForSurface(display, *surface, Config(config, EGL_PBUFFER_BIT, 0));
194                 }
195                 catch (const tcu::TestError& e)
196                 {
197                         log << e;
198                         isOk = false;
199                         failReason = e.what();
200                 }
201         }
202
203         if (!isOk && m_testCtx.getTestResult() == QP_TEST_RESULT_PASS)
204                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, failReason.c_str());
205 }
206
207 // SingleContextRenderCase
208
209 SingleContextRenderCase::SingleContextRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint apiMask, EGLint surfaceTypeMask, const eglu::FilterList& filters)
210         : RenderCase(eglTestCtx, name, description, apiMask, surfaceTypeMask, filters)
211 {
212 }
213
214 SingleContextRenderCase::~SingleContextRenderCase (void)
215 {
216 }
217
218 void SingleContextRenderCase::executeForSurface (EGLDisplay display, EGLSurface surface, const Config& config)
219 {
220         const Library&          egl             = m_eglTestCtx.getLibrary();
221         const EGLint            apis[]  = { EGL_OPENGL_ES2_BIT, EGL_OPENGL_ES3_BIT_KHR, EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT };
222         tcu::TestLog&           log             = m_testCtx.getLog();
223
224         checkBuildClientAPISupport(m_apiMask);
225
226         for (int apiNdx = 0; apiNdx < DE_LENGTH_OF_ARRAY(apis); apiNdx++)
227         {
228                 EGLint apiBit = apis[apiNdx];
229
230                 if ((apiBit & m_apiMask) == 0)
231                         continue; // Skip this api.
232
233                 EGLint                  api             = EGL_NONE;
234                 const char*             apiName = DE_NULL;
235                 vector<EGLint>  contextAttribs;
236
237                 // Select api enum and build context attributes.
238                 switch (apiBit)
239                 {
240                         case EGL_OPENGL_ES2_BIT:
241                                 api             = EGL_OPENGL_ES_API;
242                                 apiName = "OpenGL ES 2.x";
243                                 contextAttribs.push_back(EGL_CONTEXT_CLIENT_VERSION);
244                                 contextAttribs.push_back(2);
245                                 break;
246
247                         case EGL_OPENGL_ES3_BIT_KHR:
248                                 api             = EGL_OPENGL_ES_API;
249                                 apiName = "OpenGL ES 3.x";
250                                 contextAttribs.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
251                                 contextAttribs.push_back(3);
252                                 break;
253
254                         case EGL_OPENGL_ES_BIT:
255                                 api             = EGL_OPENGL_ES_API;
256                                 apiName = "OpenGL ES 1.x";
257                                 contextAttribs.push_back(EGL_CONTEXT_CLIENT_VERSION);
258                                 contextAttribs.push_back(1);
259                                 break;
260
261                         case EGL_OPENVG_BIT:
262                                 api             = EGL_OPENVG_API;
263                                 apiName = "OpenVG";
264                                 break;
265
266                         default:
267                                 DE_ASSERT(DE_FALSE);
268                 }
269
270                 contextAttribs.push_back(EGL_NONE);
271
272                 log << TestLog::Message << apiName << TestLog::EndMessage;
273
274                 EGLU_CHECK_CALL(egl, bindAPI(api));
275
276                 eglu::UniqueContext     context (egl, display, egl.createContext(display, config.config, EGL_NO_CONTEXT, &contextAttribs[0]));
277
278                 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, *context));
279                 executeForContext(display, *context, surface, Config(config.config, config.surfaceTypeBit, apiBit));
280
281                 // Call SwapBuffers() / WaitClient() to finish rendering
282                 postSurface(egl, display, surface, config.surfaceTypeBit);
283         }
284
285         EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
286 }
287
288 // MultiContextRenderCase
289
290 MultiContextRenderCase::MultiContextRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint api, EGLint surfaceType, const eglu::FilterList& filters, int numContextsPerApi)
291         : RenderCase                    (eglTestCtx, name, description, api, surfaceType, filters)
292         , m_numContextsPerApi   (numContextsPerApi)
293 {
294 }
295
296 MultiContextRenderCase::~MultiContextRenderCase (void)
297 {
298 }
299
300 void MultiContextRenderCase::executeForSurface (EGLDisplay display, EGLSurface surface, const Config& config)
301 {
302         const Library&                                                  egl             = m_eglTestCtx.getLibrary();
303         vector<std::pair<EGLint, EGLContext> >  contexts;
304         contexts.reserve(3*m_numContextsPerApi); // 3 types of contexts at maximum.
305
306         checkBuildClientAPISupport(m_apiMask);
307
308         try
309         {
310                 // Create contexts that will participate in rendering.
311                 for (int ndx = 0; ndx < m_numContextsPerApi; ndx++)
312                 {
313                         if (m_apiMask & EGL_OPENGL_ES2_BIT)
314                         {
315                                 static const EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
316                                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
317                                 contexts.push_back(std::make_pair(EGL_OPENGL_ES2_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
318                         }
319
320                         if (m_apiMask & EGL_OPENGL_ES3_BIT_KHR)
321                         {
322                                 static const EGLint attribs[] = { EGL_CONTEXT_MAJOR_VERSION_KHR, 3, EGL_NONE };
323                                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
324                                 contexts.push_back(std::make_pair(EGL_OPENGL_ES3_BIT_KHR, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
325                         }
326
327                         if (m_apiMask & EGL_OPENGL_ES_BIT)
328                         {
329                                 static const EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
330                                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
331                                 contexts.push_back(std::make_pair(EGL_OPENGL_ES_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
332                         }
333
334                         if (m_apiMask & EGL_OPENVG_BIT)
335                         {
336                                 static const EGLint attribs[] = { EGL_NONE };
337                                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENVG_API));
338                                 contexts.push_back(std::make_pair(EGL_OPENVG_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
339                         }
340                 }
341
342                 EGLU_CHECK_MSG(egl, "eglCreateContext()");
343
344                 // Execute for contexts.
345                 executeForContexts(display, surface, Config(config.config, config.surfaceTypeBit, m_apiMask), contexts);
346
347                 EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
348         }
349         catch (...)
350         {
351                 // Make sure all contexts have been destroyed.
352                 for (vector<std::pair<EGLint, EGLContext> >::iterator i = contexts.begin(); i != contexts.end(); i++)
353                         egl.destroyContext(display, i->second);
354                 throw;
355         }
356
357         // Destroy contexts.
358         for (vector<std::pair<EGLint, EGLContext> >::iterator i = contexts.begin(); i != contexts.end(); i++)
359                 egl.destroyContext(display, i->second);
360 }
361
362 // Utilities
363
364 template <int Red, int Green, int Blue, int Alpha>
365 static bool colorBits (const eglu::CandidateConfig& c)
366 {
367         return c.redSize()              == Red          &&
368                    c.greenSize()        == Green        &&
369                    c.blueSize()         == Blue         &&
370                    c.alphaSize()        == Alpha;
371 }
372
373 template <int Red, int Green, int Blue, int Alpha>
374 static bool notColorBits (const eglu::CandidateConfig& c)
375 {
376         return c.redSize()              != Red          ||
377                    c.greenSize()        != Green        ||
378                    c.blueSize()         != Blue         ||
379                    c.alphaSize()        != Alpha;
380 }
381
382 template <deUint32 Type>
383 static bool surfaceType (const eglu::CandidateConfig& c)
384 {
385         return (c.surfaceType() & Type) == Type;
386 }
387
388 void getDefaultRenderFilterLists (vector<RenderFilterList>& filterLists, const eglu::FilterList& baseFilters)
389 {
390         static const struct
391         {
392                 const char*                     name;
393                 eglu::ConfigFilter      filter;
394         } s_colorRules[] =
395         {
396                 { "rgb565",             colorBits<5, 6, 5, 0>   },
397                 { "rgb888",             colorBits<8, 8, 8, 0>   },
398                 { "rgba4444",   colorBits<4, 4, 4, 4>   },
399                 { "rgba5551",   colorBits<5, 5, 5, 1>   },
400                 { "rgba8888",   colorBits<8, 8, 8, 8>   },
401         };
402
403         static const struct
404         {
405                 const char*                     name;
406                 EGLint                          bits;
407                 eglu::ConfigFilter      filter;
408         } s_surfaceRules[] =
409         {
410                 { "window",             EGL_WINDOW_BIT,         surfaceType<EGL_WINDOW_BIT>             },
411                 { "pixmap",             EGL_PIXMAP_BIT,         surfaceType<EGL_PIXMAP_BIT>,    },
412                 { "pbuffer",    EGL_PBUFFER_BIT,        surfaceType<EGL_PBUFFER_BIT>    }
413         };
414
415         for (int colorNdx = 0; colorNdx < DE_LENGTH_OF_ARRAY(s_colorRules); colorNdx++)
416         {
417                 for (int surfaceNdx = 0; surfaceNdx < DE_LENGTH_OF_ARRAY(s_surfaceRules); surfaceNdx++)
418                 {
419                         const string            name    = string(s_colorRules[colorNdx].name) + "_" + s_surfaceRules[surfaceNdx].name;
420                         RenderFilterList        filters (name.c_str(), "", s_surfaceRules[surfaceNdx].bits);
421
422                         filters << baseFilters
423                                         << s_colorRules[colorNdx].filter
424                                         << s_surfaceRules[surfaceNdx].filter;
425
426                         filterLists.push_back(filters);
427                 }
428         }
429
430         // Add other config ids to "other" set
431         {
432                 RenderFilterList        filters ("other", "", EGL_WINDOW_BIT|EGL_PIXMAP_BIT|EGL_PBUFFER_BIT);
433
434                 filters << baseFilters
435                                 << notColorBits<5, 6, 5, 0>
436                                 << notColorBits<8, 8, 8, 0>
437                                 << notColorBits<4, 4, 4, 4>
438                                 << notColorBits<5, 5, 5, 1>
439                                 << notColorBits<8, 8, 8, 8>;
440
441                 filterLists.push_back(filters);
442         }
443 }
444
445 } // egl
446 } // deqp