xref: /aosp_15_r20/external/deqp/modules/egl/teglRenderCase.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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::set;
55 using std::string;
56 using std::vector;
57 using tcu::TestLog;
58 using namespace eglw;
59 
postSurface(const Library & egl,EGLDisplay display,EGLSurface surface,EGLint typeBit)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 
RenderCase(EglTestContext & eglTestCtx,const char * name,const char * description,EGLint surfaceTypeMask,const eglu::FilterList & filters)74 RenderCase::RenderCase(EglTestContext &eglTestCtx, const char *name, const char *description, EGLint surfaceTypeMask,
75                        const eglu::FilterList &filters)
76     : SimpleConfigCase(eglTestCtx, name, description, filters)
77     , m_surfaceTypeMask(surfaceTypeMask)
78 {
79 }
80 
~RenderCase(void)81 RenderCase::~RenderCase(void)
82 {
83 }
84 
getBuildClientAPIMask(void)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 
checkBuildClientAPISupport(EGLint requiredAPIs)105 static void checkBuildClientAPISupport(EGLint requiredAPIs)
106 {
107     const EGLint builtClientAPIs = getBuildClientAPIMask();
108 
109 #if !defined(DEQP_SUPPORT_GLES1)
110     if (requiredAPIs & EGL_OPENGL_ES_BIT)
111         TCU_THROW(NotSupportedError, "Test case requires ES1.1 API not supported in current build");
112     else
113 #endif
114         if ((requiredAPIs & builtClientAPIs) != requiredAPIs)
115         TCU_THROW(InternalError, "Test case requires client API not supported in current build");
116 }
117 
executeForConfig(EGLDisplay display,EGLConfig config)118 void RenderCase::executeForConfig(EGLDisplay display, EGLConfig config)
119 {
120     const Library &egl        = m_eglTestCtx.getLibrary();
121     tcu::TestLog &log         = m_testCtx.getLog();
122     const int width           = 128;
123     const int height          = 128;
124     const EGLint configId     = eglu::getConfigID(egl, display, config);
125     const EGLint surfaceTypes = eglu::getConfigAttribInt(egl, display, config, EGL_SURFACE_TYPE);
126 
127     const eglu::NativeDisplayFactory &displayFactory = m_eglTestCtx.getNativeDisplayFactory();
128     eglu::NativeDisplay &nativeDisplay               = m_eglTestCtx.getNativeDisplay();
129 
130     bool isOk         = true;
131     string failReason = "";
132 
133     if (surfaceTypes & m_surfaceTypeMask & EGL_WINDOW_BIT)
134     {
135         tcu::ScopedLogSection(log, string("Config") + de::toString(configId) + "-Window",
136                               string("Config ID ") + de::toString(configId) + ", window surface");
137 
138         const eglu::NativeWindowFactory &windowFactory =
139             eglu::selectNativeWindowFactory(displayFactory, m_testCtx.getCommandLine());
140 
141         try
142         {
143             const eglu::WindowParams params(width, height, eglu::parseWindowVisibility(m_testCtx.getCommandLine()));
144             de::UniquePtr<eglu::NativeWindow> window(
145                 windowFactory.createWindow(&nativeDisplay, display, config, DE_NULL, params));
146             EGLSurface eglSurface = createWindowSurface(nativeDisplay, *window, display, config, DE_NULL);
147             eglu::UniqueSurface surface(egl, display, eglSurface);
148 
149             executeForSurface(display, *surface, Config(config, EGL_WINDOW_BIT, 0));
150         }
151         catch (const tcu::TestError &e)
152         {
153             log << e;
154             isOk       = false;
155             failReason = e.what();
156         }
157     }
158 
159     if (surfaceTypes & m_surfaceTypeMask & EGL_PIXMAP_BIT)
160     {
161         tcu::ScopedLogSection(log, string("Config") + de::toString(configId) + "-Pixmap",
162                               string("Config ID ") + de::toString(configId) + ", pixmap surface");
163 
164         const eglu::NativePixmapFactory &pixmapFactory =
165             eglu::selectNativePixmapFactory(displayFactory, m_testCtx.getCommandLine());
166 
167         try
168         {
169             de::UniquePtr<eglu::NativePixmap> pixmap(
170                 pixmapFactory.createPixmap(&nativeDisplay, display, config, DE_NULL, width, height));
171             EGLSurface eglSurface = createPixmapSurface(nativeDisplay, *pixmap, display, config, DE_NULL);
172             eglu::UniqueSurface surface(egl, display, eglSurface);
173 
174             executeForSurface(display, *surface, Config(config, EGL_PIXMAP_BIT, 0));
175         }
176         catch (const tcu::TestError &e)
177         {
178             log << e;
179             isOk       = false;
180             failReason = e.what();
181         }
182     }
183 
184     if (surfaceTypes & m_surfaceTypeMask & EGL_PBUFFER_BIT)
185     {
186         tcu::ScopedLogSection(log, string("Config") + de::toString(configId) + "-Pbuffer",
187                               string("Config ID ") + de::toString(configId) + ", pbuffer surface");
188         try
189         {
190             const EGLint surfaceAttribs[] = {EGL_WIDTH, width, EGL_HEIGHT, height, EGL_NONE};
191 
192             eglu::UniqueSurface surface(egl, display, egl.createPbufferSurface(display, config, surfaceAttribs));
193             EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
194 
195             executeForSurface(display, *surface, Config(config, EGL_PBUFFER_BIT, 0));
196         }
197         catch (const tcu::TestError &e)
198         {
199             log << e;
200             isOk       = false;
201             failReason = e.what();
202         }
203     }
204 
205     if (!isOk && m_testCtx.getTestResult() == QP_TEST_RESULT_PASS)
206         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, failReason.c_str());
207 }
208 
209 // SingleContextRenderCase
210 
SingleContextRenderCase(EglTestContext & eglTestCtx,const char * name,const char * description,EGLint apiMask,EGLint surfaceTypeMask,const eglu::FilterList & filters)211 SingleContextRenderCase::SingleContextRenderCase(EglTestContext &eglTestCtx, const char *name, const char *description,
212                                                  EGLint apiMask, EGLint surfaceTypeMask,
213                                                  const eglu::FilterList &filters)
214     : RenderCase(eglTestCtx, name, description, surfaceTypeMask, filters)
215     , m_apiMask(apiMask)
216 {
217 }
218 
~SingleContextRenderCase(void)219 SingleContextRenderCase::~SingleContextRenderCase(void)
220 {
221 }
222 
executeForSurface(EGLDisplay display,EGLSurface surface,const Config & config)223 void SingleContextRenderCase::executeForSurface(EGLDisplay display, EGLSurface surface, const Config &config)
224 {
225     const Library &egl         = m_eglTestCtx.getLibrary();
226     const EGLint apis[]        = {EGL_OPENGL_ES2_BIT, EGL_OPENGL_ES3_BIT_KHR, EGL_OPENGL_ES_BIT, EGL_OPENVG_BIT};
227     tcu::TestLog &log          = m_testCtx.getLog();
228     const EGLint configApiMask = eglu::getConfigAttribInt(egl, display, config.config, EGL_RENDERABLE_TYPE);
229 
230     checkBuildClientAPISupport(m_apiMask);
231 
232     for (int apiNdx = 0; apiNdx < DE_LENGTH_OF_ARRAY(apis); apiNdx++)
233     {
234         EGLint apiBit = apis[apiNdx];
235 
236         // Skip API if build or current config doesn't support it.
237         if ((apiBit & m_apiMask) == 0 || (apiBit & configApiMask) == 0)
238             continue;
239 
240         EGLint api          = EGL_NONE;
241         const char *apiName = DE_NULL;
242         vector<EGLint> contextAttribs;
243 
244         // Select api enum and build context attributes.
245         switch (apiBit)
246         {
247         case EGL_OPENGL_ES2_BIT:
248             api     = EGL_OPENGL_ES_API;
249             apiName = "OpenGL ES 2.x";
250             contextAttribs.push_back(EGL_CONTEXT_CLIENT_VERSION);
251             contextAttribs.push_back(2);
252             break;
253 
254         case EGL_OPENGL_ES3_BIT_KHR:
255             api     = EGL_OPENGL_ES_API;
256             apiName = "OpenGL ES 3.x";
257             contextAttribs.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
258             contextAttribs.push_back(3);
259             break;
260 
261         case EGL_OPENGL_ES_BIT:
262             api     = EGL_OPENGL_ES_API;
263             apiName = "OpenGL ES 1.x";
264             contextAttribs.push_back(EGL_CONTEXT_CLIENT_VERSION);
265             contextAttribs.push_back(1);
266             break;
267 
268         case EGL_OPENVG_BIT:
269             api     = EGL_OPENVG_API;
270             apiName = "OpenVG";
271             break;
272 
273         default:
274             DE_ASSERT(false);
275         }
276 
277         contextAttribs.push_back(EGL_NONE);
278 
279         log << TestLog::Message << apiName << TestLog::EndMessage;
280 
281         EGLU_CHECK_CALL(egl, bindAPI(api));
282 
283         eglu::UniqueContext context(egl, display,
284                                     egl.createContext(display, config.config, EGL_NO_CONTEXT, &contextAttribs[0]));
285 
286         EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, *context));
287         executeForContext(display, *context, surface, Config(config.config, config.surfaceTypeBit, apiBit));
288 
289         // Call SwapBuffers() / WaitClient() to finish rendering
290         postSurface(egl, display, surface, config.surfaceTypeBit);
291     }
292 
293     EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
294 }
295 
296 // MultiContextRenderCase
297 
MultiContextRenderCase(EglTestContext & eglTestCtx,const char * name,const char * description,EGLint api,EGLint surfaceType,const eglu::FilterList & filters,int numContextsPerApi)298 MultiContextRenderCase::MultiContextRenderCase(EglTestContext &eglTestCtx, const char *name, const char *description,
299                                                EGLint api, EGLint surfaceType, const eglu::FilterList &filters,
300                                                int numContextsPerApi)
301     : RenderCase(eglTestCtx, name, description, surfaceType, filters)
302     , m_numContextsPerApi(numContextsPerApi)
303     , m_apiMask(api)
304 {
305 }
306 
~MultiContextRenderCase(void)307 MultiContextRenderCase::~MultiContextRenderCase(void)
308 {
309 }
310 
executeForSurface(EGLDisplay display,EGLSurface surface,const Config & config)311 void MultiContextRenderCase::executeForSurface(EGLDisplay display, EGLSurface surface, const Config &config)
312 {
313     const Library &egl         = m_eglTestCtx.getLibrary();
314     const EGLint configApiMask = eglu::getConfigAttribInt(egl, display, config.config, EGL_RENDERABLE_TYPE);
315     vector<std::pair<EGLint, EGLContext>> contexts;
316     contexts.reserve(3 * m_numContextsPerApi); // 3 types of contexts at maximum.
317 
318     checkBuildClientAPISupport(m_apiMask);
319 
320     // ConfigFilter should make sure that config always supports all of the APIs.
321     TCU_CHECK_INTERNAL((configApiMask & m_apiMask) == m_apiMask);
322 
323     try
324     {
325         // Create contexts that will participate in rendering.
326         for (int ndx = 0; ndx < m_numContextsPerApi; ndx++)
327         {
328             if (m_apiMask & EGL_OPENGL_ES2_BIT)
329             {
330                 static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
331                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
332                 contexts.push_back(std::make_pair(
333                     EGL_OPENGL_ES2_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
334             }
335 
336             if (m_apiMask & EGL_OPENGL_ES3_BIT_KHR)
337             {
338                 static const EGLint attribs[] = {EGL_CONTEXT_MAJOR_VERSION_KHR, 3, EGL_NONE};
339                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
340                 contexts.push_back(std::make_pair(
341                     EGL_OPENGL_ES3_BIT_KHR, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
342             }
343 
344             if (m_apiMask & EGL_OPENGL_ES_BIT)
345             {
346                 static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE};
347                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
348                 contexts.push_back(std::make_pair(
349                     EGL_OPENGL_ES_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
350             }
351 
352             if (m_apiMask & EGL_OPENVG_BIT)
353             {
354                 static const EGLint attribs[] = {EGL_NONE};
355                 EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENVG_API));
356                 contexts.push_back(std::make_pair(
357                     EGL_OPENVG_BIT, egl.createContext(display, config.config, EGL_NO_CONTEXT, &attribs[0])));
358             }
359         }
360 
361         EGLU_CHECK_MSG(egl, "eglCreateContext()");
362 
363         // Execute for contexts.
364         executeForContexts(display, surface, Config(config.config, config.surfaceTypeBit, m_apiMask), contexts);
365 
366         EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
367     }
368     catch (...)
369     {
370         // Make sure all contexts have been destroyed.
371         for (vector<std::pair<EGLint, EGLContext>>::iterator i = contexts.begin(); i != contexts.end(); i++)
372             egl.destroyContext(display, i->second);
373         throw;
374     }
375 
376     // Destroy contexts.
377     for (vector<std::pair<EGLint, EGLContext>>::iterator i = contexts.begin(); i != contexts.end(); i++)
378         egl.destroyContext(display, i->second);
379 }
380 
381 // Utilities
382 
383 template <int Red, int Green, int Blue, int Alpha>
colorBits(const eglu::CandidateConfig & c)384 static bool colorBits(const eglu::CandidateConfig &c)
385 {
386     return c.redSize() == Red && c.greenSize() == Green && c.blueSize() == Blue && c.alphaSize() == Alpha;
387 }
388 
389 template <int Red, int Green, int Blue, int Alpha>
notColorBits(const eglu::CandidateConfig & c)390 static bool notColorBits(const eglu::CandidateConfig &c)
391 {
392     return c.redSize() != Red || c.greenSize() != Green || c.blueSize() != Blue || c.alphaSize() != Alpha;
393 }
394 
395 template <uint32_t Type>
surfaceType(const eglu::CandidateConfig & c)396 static bool surfaceType(const eglu::CandidateConfig &c)
397 {
398     return (c.surfaceType() & Type) == Type;
399 }
400 
isConformant(const eglu::CandidateConfig & c)401 static bool isConformant(const eglu::CandidateConfig &c)
402 {
403     return c.get(EGL_CONFIG_CAVEAT) != EGL_NON_CONFORMANT_CONFIG;
404 }
405 
notFloat(const eglu::CandidateConfig & c)406 static bool notFloat(const eglu::CandidateConfig &c)
407 {
408     return c.colorComponentType() != EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
409 }
410 
notYUV(const eglu::CandidateConfig & c)411 static bool notYUV(const eglu::CandidateConfig &c)
412 {
413     return c.colorBufferType() != EGL_YUV_BUFFER_EXT;
414 }
415 
getDefaultRenderFilterLists(vector<RenderFilterList> & filterLists,const eglu::FilterList & baseFilters)416 void getDefaultRenderFilterLists(vector<RenderFilterList> &filterLists, const eglu::FilterList &baseFilters)
417 {
418     static const struct
419     {
420         const char *name;
421         eglu::ConfigFilter filter;
422     } s_colorRules[] = {
423         {"rgb565", colorBits<5, 6, 5, 0>},   {"rgb888", colorBits<8, 8, 8, 0>},   {"rgba4444", colorBits<4, 4, 4, 4>},
424         {"rgba5551", colorBits<5, 5, 5, 1>}, {"rgba8888", colorBits<8, 8, 8, 8>},
425     };
426 
427     static const struct
428     {
429         const char *name;
430         EGLint bits;
431         eglu::ConfigFilter filter;
432     } s_surfaceRules[] = {{"window", EGL_WINDOW_BIT, surfaceType<EGL_WINDOW_BIT>},
433                           {
434                               "pixmap",
435                               EGL_PIXMAP_BIT,
436                               surfaceType<EGL_PIXMAP_BIT>,
437                           },
438                           {"pbuffer", EGL_PBUFFER_BIT, surfaceType<EGL_PBUFFER_BIT>}};
439 
440     for (int colorNdx = 0; colorNdx < DE_LENGTH_OF_ARRAY(s_colorRules); colorNdx++)
441     {
442         for (int surfaceNdx = 0; surfaceNdx < DE_LENGTH_OF_ARRAY(s_surfaceRules); surfaceNdx++)
443         {
444             const string name = string(s_colorRules[colorNdx].name) + "_" + s_surfaceRules[surfaceNdx].name;
445             RenderFilterList filters(name.c_str(), "", s_surfaceRules[surfaceNdx].bits);
446 
447             filters << baseFilters << s_colorRules[colorNdx].filter << s_surfaceRules[surfaceNdx].filter
448                     << isConformant;
449 
450             filterLists.push_back(filters);
451         }
452     }
453 
454     // Add other config ids to "other" set
455     {
456         RenderFilterList filters("other", "", EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT);
457 
458         filters
459             << baseFilters
460             << notColorBits<
461                    5, 6, 5,
462                    0> << notColorBits<8, 8, 8, 0> << notColorBits<4, 4, 4, 4> << notColorBits<5, 5, 5, 1> << notColorBits<8, 8, 8, 8> << isConformant
463             << notFloat << notYUV;
464 
465         filterLists.push_back(filters);
466     }
467 }
468 
469 } // namespace egl
470 } // namespace deqp
471