xref: /aosp_15_r20/external/deqp/framework/egl/egluGLContextFactory.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker  * drawElements Quality Program Tester Core
3*35238bceSAndroid Build Coastguard Worker  * ----------------------------------------
4*35238bceSAndroid Build Coastguard Worker  *
5*35238bceSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker  *
7*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker  *
11*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker  *
13*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker  *
19*35238bceSAndroid Build Coastguard Worker  *//*!
20*35238bceSAndroid Build Coastguard Worker  * \file
21*35238bceSAndroid Build Coastguard Worker  * \brief GL context factory using EGL.
22*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "egluGLContextFactory.hpp"
25*35238bceSAndroid Build Coastguard Worker 
26*35238bceSAndroid Build Coastguard Worker #include "tcuRenderTarget.hpp"
27*35238bceSAndroid Build Coastguard Worker #include "tcuPlatform.hpp"
28*35238bceSAndroid Build Coastguard Worker #include "tcuCommandLine.hpp"
29*35238bceSAndroid Build Coastguard Worker 
30*35238bceSAndroid Build Coastguard Worker #include "gluDefs.hpp"
31*35238bceSAndroid Build Coastguard Worker 
32*35238bceSAndroid Build Coastguard Worker #include "egluDefs.hpp"
33*35238bceSAndroid Build Coastguard Worker #include "egluUtil.hpp"
34*35238bceSAndroid Build Coastguard Worker #include "egluGLUtil.hpp"
35*35238bceSAndroid Build Coastguard Worker #include "egluNativeWindow.hpp"
36*35238bceSAndroid Build Coastguard Worker #include "egluNativePixmap.hpp"
37*35238bceSAndroid Build Coastguard Worker #include "egluStrUtil.hpp"
38*35238bceSAndroid Build Coastguard Worker 
39*35238bceSAndroid Build Coastguard Worker #include "eglwLibrary.hpp"
40*35238bceSAndroid Build Coastguard Worker #include "eglwEnums.hpp"
41*35238bceSAndroid Build Coastguard Worker 
42*35238bceSAndroid Build Coastguard Worker #include "glwInitFunctions.hpp"
43*35238bceSAndroid Build Coastguard Worker #include "glwInitES20Direct.hpp"
44*35238bceSAndroid Build Coastguard Worker #include "glwInitES30Direct.hpp"
45*35238bceSAndroid Build Coastguard Worker #include "glwInitES31Direct.hpp"
46*35238bceSAndroid Build Coastguard Worker #include "glwInitES32Direct.hpp"
47*35238bceSAndroid Build Coastguard Worker 
48*35238bceSAndroid Build Coastguard Worker #include "deDynamicLibrary.hpp"
49*35238bceSAndroid Build Coastguard Worker #include "deSTLUtil.hpp"
50*35238bceSAndroid Build Coastguard Worker #include "deSharedPtr.hpp"
51*35238bceSAndroid Build Coastguard Worker 
52*35238bceSAndroid Build Coastguard Worker #include <string>
53*35238bceSAndroid Build Coastguard Worker #include <string>
54*35238bceSAndroid Build Coastguard Worker #include <sstream>
55*35238bceSAndroid Build Coastguard Worker 
56*35238bceSAndroid Build Coastguard Worker using std::string;
57*35238bceSAndroid Build Coastguard Worker using std::vector;
58*35238bceSAndroid Build Coastguard Worker 
59*35238bceSAndroid Build Coastguard Worker // \todo [2014-03-12 pyry] Use command line arguments for libraries?
60*35238bceSAndroid Build Coastguard Worker 
61*35238bceSAndroid Build Coastguard Worker // Default library names
62*35238bceSAndroid Build Coastguard Worker #if !defined(DEQP_GLES2_LIBRARY_PATH)
63*35238bceSAndroid Build Coastguard Worker #if (DE_OS == DE_OS_WIN32)
64*35238bceSAndroid Build Coastguard Worker #define DEQP_GLES2_LIBRARY_PATH "libGLESv2.dll"
65*35238bceSAndroid Build Coastguard Worker #else
66*35238bceSAndroid Build Coastguard Worker #define DEQP_GLES2_LIBRARY_PATH "libGLESv2.so"
67*35238bceSAndroid Build Coastguard Worker #endif
68*35238bceSAndroid Build Coastguard Worker #endif
69*35238bceSAndroid Build Coastguard Worker 
70*35238bceSAndroid Build Coastguard Worker #if !defined(DEQP_GLES3_LIBRARY_PATH)
71*35238bceSAndroid Build Coastguard Worker #define DEQP_GLES3_LIBRARY_PATH DEQP_GLES2_LIBRARY_PATH
72*35238bceSAndroid Build Coastguard Worker #endif
73*35238bceSAndroid Build Coastguard Worker 
74*35238bceSAndroid Build Coastguard Worker #if !defined(DEQP_OPENGL_LIBRARY_PATH)
75*35238bceSAndroid Build Coastguard Worker #if (DE_OS == DE_OS_WIN32)
76*35238bceSAndroid Build Coastguard Worker #define DEQP_OPENGL_LIBRARY_PATH "opengl32.dll"
77*35238bceSAndroid Build Coastguard Worker #else
78*35238bceSAndroid Build Coastguard Worker #define DEQP_OPENGL_LIBRARY_PATH "libGL.so"
79*35238bceSAndroid Build Coastguard Worker #endif
80*35238bceSAndroid Build Coastguard Worker #endif
81*35238bceSAndroid Build Coastguard Worker 
82*35238bceSAndroid Build Coastguard Worker namespace eglu
83*35238bceSAndroid Build Coastguard Worker {
84*35238bceSAndroid Build Coastguard Worker 
85*35238bceSAndroid Build Coastguard Worker using namespace eglw;
86*35238bceSAndroid Build Coastguard Worker 
87*35238bceSAndroid Build Coastguard Worker namespace
88*35238bceSAndroid Build Coastguard Worker {
89*35238bceSAndroid Build Coastguard Worker 
90*35238bceSAndroid Build Coastguard Worker enum
91*35238bceSAndroid Build Coastguard Worker {
92*35238bceSAndroid Build Coastguard Worker     DEFAULT_OFFSCREEN_WIDTH  = 512,
93*35238bceSAndroid Build Coastguard Worker     DEFAULT_OFFSCREEN_HEIGHT = 512
94*35238bceSAndroid Build Coastguard Worker };
95*35238bceSAndroid Build Coastguard Worker 
96*35238bceSAndroid Build Coastguard Worker class GetProcFuncLoader : public glw::FunctionLoader
97*35238bceSAndroid Build Coastguard Worker {
98*35238bceSAndroid Build Coastguard Worker public:
GetProcFuncLoader(const Library & egl)99*35238bceSAndroid Build Coastguard Worker     GetProcFuncLoader(const Library &egl) : m_egl(egl)
100*35238bceSAndroid Build Coastguard Worker     {
101*35238bceSAndroid Build Coastguard Worker     }
102*35238bceSAndroid Build Coastguard Worker 
get(const char * name) const103*35238bceSAndroid Build Coastguard Worker     glw::GenericFuncType get(const char *name) const
104*35238bceSAndroid Build Coastguard Worker     {
105*35238bceSAndroid Build Coastguard Worker         return (glw::GenericFuncType)m_egl.getProcAddress(name);
106*35238bceSAndroid Build Coastguard Worker     }
107*35238bceSAndroid Build Coastguard Worker 
108*35238bceSAndroid Build Coastguard Worker protected:
109*35238bceSAndroid Build Coastguard Worker     const Library &m_egl;
110*35238bceSAndroid Build Coastguard Worker };
111*35238bceSAndroid Build Coastguard Worker 
112*35238bceSAndroid Build Coastguard Worker class DynamicFuncLoader : public glw::FunctionLoader
113*35238bceSAndroid Build Coastguard Worker {
114*35238bceSAndroid Build Coastguard Worker public:
DynamicFuncLoader(de::DynamicLibrary * library)115*35238bceSAndroid Build Coastguard Worker     DynamicFuncLoader(de::DynamicLibrary *library) : m_library(library)
116*35238bceSAndroid Build Coastguard Worker     {
117*35238bceSAndroid Build Coastguard Worker     }
118*35238bceSAndroid Build Coastguard Worker 
get(const char * name) const119*35238bceSAndroid Build Coastguard Worker     glw::GenericFuncType get(const char *name) const
120*35238bceSAndroid Build Coastguard Worker     {
121*35238bceSAndroid Build Coastguard Worker         return (glw::GenericFuncType)m_library->getFunction(name);
122*35238bceSAndroid Build Coastguard Worker     }
123*35238bceSAndroid Build Coastguard Worker 
124*35238bceSAndroid Build Coastguard Worker private:
125*35238bceSAndroid Build Coastguard Worker     de::DynamicLibrary *m_library;
126*35238bceSAndroid Build Coastguard Worker };
127*35238bceSAndroid Build Coastguard Worker 
128*35238bceSAndroid Build Coastguard Worker class RenderContext : public GLRenderContext
129*35238bceSAndroid Build Coastguard Worker {
130*35238bceSAndroid Build Coastguard Worker public:
131*35238bceSAndroid Build Coastguard Worker     RenderContext(const NativeDisplayFactory *displayFactory, const NativeWindowFactory *windowFactory,
132*35238bceSAndroid Build Coastguard Worker                   const NativePixmapFactory *pixmapFactory, const glu::RenderConfig &config,
133*35238bceSAndroid Build Coastguard Worker                   const glu::RenderContext *sharedContext = DE_NULL);
134*35238bceSAndroid Build Coastguard Worker     virtual ~RenderContext(void);
135*35238bceSAndroid Build Coastguard Worker 
getType(void) const136*35238bceSAndroid Build Coastguard Worker     virtual glu::ContextType getType(void) const
137*35238bceSAndroid Build Coastguard Worker     {
138*35238bceSAndroid Build Coastguard Worker         return m_renderConfig.type;
139*35238bceSAndroid Build Coastguard Worker     }
getFunctions(void) const140*35238bceSAndroid Build Coastguard Worker     virtual const glw::Functions &getFunctions(void) const
141*35238bceSAndroid Build Coastguard Worker     {
142*35238bceSAndroid Build Coastguard Worker         return m_glFunctions;
143*35238bceSAndroid Build Coastguard Worker     }
getRenderTarget(void) const144*35238bceSAndroid Build Coastguard Worker     virtual const tcu::RenderTarget &getRenderTarget(void) const
145*35238bceSAndroid Build Coastguard Worker     {
146*35238bceSAndroid Build Coastguard Worker         return m_glRenderTarget;
147*35238bceSAndroid Build Coastguard Worker     }
148*35238bceSAndroid Build Coastguard Worker     virtual void postIterate(void);
149*35238bceSAndroid Build Coastguard Worker 
getEGLDisplay(void) const150*35238bceSAndroid Build Coastguard Worker     virtual EGLDisplay getEGLDisplay(void) const
151*35238bceSAndroid Build Coastguard Worker     {
152*35238bceSAndroid Build Coastguard Worker         return m_eglDisplay;
153*35238bceSAndroid Build Coastguard Worker     }
getEGLContext(void) const154*35238bceSAndroid Build Coastguard Worker     virtual EGLContext getEGLContext(void) const
155*35238bceSAndroid Build Coastguard Worker     {
156*35238bceSAndroid Build Coastguard Worker         return m_eglContext;
157*35238bceSAndroid Build Coastguard Worker     }
getEGLConfig(void) const158*35238bceSAndroid Build Coastguard Worker     virtual EGLConfig getEGLConfig(void) const
159*35238bceSAndroid Build Coastguard Worker     {
160*35238bceSAndroid Build Coastguard Worker         return m_eglConfig;
161*35238bceSAndroid Build Coastguard Worker     }
getLibrary(void) const162*35238bceSAndroid Build Coastguard Worker     virtual const eglw::Library &getLibrary(void) const
163*35238bceSAndroid Build Coastguard Worker     {
164*35238bceSAndroid Build Coastguard Worker         return m_display->getLibrary();
165*35238bceSAndroid Build Coastguard Worker     }
166*35238bceSAndroid Build Coastguard Worker 
167*35238bceSAndroid Build Coastguard Worker     virtual eglw::GenericFuncType getProcAddress(const char *name) const;
168*35238bceSAndroid Build Coastguard Worker 
169*35238bceSAndroid Build Coastguard Worker     virtual void makeCurrent(void);
170*35238bceSAndroid Build Coastguard Worker 
171*35238bceSAndroid Build Coastguard Worker private:
172*35238bceSAndroid Build Coastguard Worker     void create(const NativeDisplayFactory *displayFactory, const NativeWindowFactory *windowFactory,
173*35238bceSAndroid Build Coastguard Worker                 const NativePixmapFactory *pixmapFactory, const glu::RenderConfig &config,
174*35238bceSAndroid Build Coastguard Worker                 const glu::RenderContext *sharedContext);
175*35238bceSAndroid Build Coastguard Worker     void destroy(void);
176*35238bceSAndroid Build Coastguard Worker 
177*35238bceSAndroid Build Coastguard Worker     const glu::RenderConfig m_renderConfig;
178*35238bceSAndroid Build Coastguard Worker     const NativeWindowFactory *const m_nativeWindowFactory; // Stored in case window must be re-created
179*35238bceSAndroid Build Coastguard Worker 
180*35238bceSAndroid Build Coastguard Worker     de::SharedPtr<NativeDisplay> m_display;
181*35238bceSAndroid Build Coastguard Worker     NativeWindow *m_window;
182*35238bceSAndroid Build Coastguard Worker     NativePixmap *m_pixmap;
183*35238bceSAndroid Build Coastguard Worker 
184*35238bceSAndroid Build Coastguard Worker     EGLDisplay m_eglDisplay;
185*35238bceSAndroid Build Coastguard Worker     EGLConfig m_eglConfig;
186*35238bceSAndroid Build Coastguard Worker     EGLSurface m_eglSurface;
187*35238bceSAndroid Build Coastguard Worker     EGLContext m_eglContext;
188*35238bceSAndroid Build Coastguard Worker     EGLContext m_eglSharedContext;
189*35238bceSAndroid Build Coastguard Worker 
190*35238bceSAndroid Build Coastguard Worker     tcu::RenderTarget m_glRenderTarget;
191*35238bceSAndroid Build Coastguard Worker     de::DynamicLibrary *m_dynamicGLLibrary;
192*35238bceSAndroid Build Coastguard Worker     glw::Functions m_glFunctions;
193*35238bceSAndroid Build Coastguard Worker };
194*35238bceSAndroid Build Coastguard Worker 
RenderContext(const NativeDisplayFactory * displayFactory,const NativeWindowFactory * windowFactory,const NativePixmapFactory * pixmapFactory,const glu::RenderConfig & config,const glu::RenderContext * sharedContext)195*35238bceSAndroid Build Coastguard Worker RenderContext::RenderContext(const NativeDisplayFactory *displayFactory, const NativeWindowFactory *windowFactory,
196*35238bceSAndroid Build Coastguard Worker                              const NativePixmapFactory *pixmapFactory, const glu::RenderConfig &config,
197*35238bceSAndroid Build Coastguard Worker                              const glu::RenderContext *sharedContext)
198*35238bceSAndroid Build Coastguard Worker     : m_renderConfig(config)
199*35238bceSAndroid Build Coastguard Worker     , m_nativeWindowFactory(windowFactory)
200*35238bceSAndroid Build Coastguard Worker     , m_display(DE_NULL)
201*35238bceSAndroid Build Coastguard Worker     , m_window(DE_NULL)
202*35238bceSAndroid Build Coastguard Worker     , m_pixmap(DE_NULL)
203*35238bceSAndroid Build Coastguard Worker 
204*35238bceSAndroid Build Coastguard Worker     , m_eglDisplay(EGL_NO_DISPLAY)
205*35238bceSAndroid Build Coastguard Worker     , m_eglSurface(EGL_NO_SURFACE)
206*35238bceSAndroid Build Coastguard Worker     , m_eglContext(EGL_NO_CONTEXT)
207*35238bceSAndroid Build Coastguard Worker     , m_eglSharedContext(EGL_NO_CONTEXT)
208*35238bceSAndroid Build Coastguard Worker 
209*35238bceSAndroid Build Coastguard Worker     , m_dynamicGLLibrary(DE_NULL)
210*35238bceSAndroid Build Coastguard Worker {
211*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(displayFactory);
212*35238bceSAndroid Build Coastguard Worker 
213*35238bceSAndroid Build Coastguard Worker     try
214*35238bceSAndroid Build Coastguard Worker     {
215*35238bceSAndroid Build Coastguard Worker         create(displayFactory, windowFactory, pixmapFactory, config, sharedContext);
216*35238bceSAndroid Build Coastguard Worker     }
217*35238bceSAndroid Build Coastguard Worker     catch (...)
218*35238bceSAndroid Build Coastguard Worker     {
219*35238bceSAndroid Build Coastguard Worker         destroy();
220*35238bceSAndroid Build Coastguard Worker         throw;
221*35238bceSAndroid Build Coastguard Worker     }
222*35238bceSAndroid Build Coastguard Worker }
223*35238bceSAndroid Build Coastguard Worker 
~RenderContext(void)224*35238bceSAndroid Build Coastguard Worker RenderContext::~RenderContext(void)
225*35238bceSAndroid Build Coastguard Worker {
226*35238bceSAndroid Build Coastguard Worker     try
227*35238bceSAndroid Build Coastguard Worker     {
228*35238bceSAndroid Build Coastguard Worker         destroy();
229*35238bceSAndroid Build Coastguard Worker     }
230*35238bceSAndroid Build Coastguard Worker     catch (...)
231*35238bceSAndroid Build Coastguard Worker     {
232*35238bceSAndroid Build Coastguard Worker         // destroy() calls EGL functions that are checked and may throw exceptions
233*35238bceSAndroid Build Coastguard Worker     }
234*35238bceSAndroid Build Coastguard Worker 
235*35238bceSAndroid Build Coastguard Worker     delete m_window;
236*35238bceSAndroid Build Coastguard Worker     delete m_pixmap;
237*35238bceSAndroid Build Coastguard Worker     delete m_dynamicGLLibrary;
238*35238bceSAndroid Build Coastguard Worker }
239*35238bceSAndroid Build Coastguard Worker 
getNativeWindowVisibility(glu::RenderConfig::Visibility visibility)240*35238bceSAndroid Build Coastguard Worker static WindowParams::Visibility getNativeWindowVisibility(glu::RenderConfig::Visibility visibility)
241*35238bceSAndroid Build Coastguard Worker {
242*35238bceSAndroid Build Coastguard Worker     using glu::RenderConfig;
243*35238bceSAndroid Build Coastguard Worker 
244*35238bceSAndroid Build Coastguard Worker     switch (visibility)
245*35238bceSAndroid Build Coastguard Worker     {
246*35238bceSAndroid Build Coastguard Worker     case RenderConfig::VISIBILITY_HIDDEN:
247*35238bceSAndroid Build Coastguard Worker         return WindowParams::VISIBILITY_HIDDEN;
248*35238bceSAndroid Build Coastguard Worker     case RenderConfig::VISIBILITY_VISIBLE:
249*35238bceSAndroid Build Coastguard Worker         return WindowParams::VISIBILITY_VISIBLE;
250*35238bceSAndroid Build Coastguard Worker     case RenderConfig::VISIBILITY_FULLSCREEN:
251*35238bceSAndroid Build Coastguard Worker         return WindowParams::VISIBILITY_FULLSCREEN;
252*35238bceSAndroid Build Coastguard Worker     default:
253*35238bceSAndroid Build Coastguard Worker         DE_ASSERT((int)visibility == RenderConfig::DONT_CARE);
254*35238bceSAndroid Build Coastguard Worker         return WindowParams::VISIBILITY_DONT_CARE;
255*35238bceSAndroid Build Coastguard Worker     }
256*35238bceSAndroid Build Coastguard Worker }
257*35238bceSAndroid Build Coastguard Worker 
258*35238bceSAndroid Build Coastguard Worker typedef std::pair<NativeWindow *, EGLSurface> WindowSurfacePair;
259*35238bceSAndroid Build Coastguard Worker typedef std::pair<NativePixmap *, EGLSurface> PixmapSurfacePair;
260*35238bceSAndroid Build Coastguard Worker 
createWindow(NativeDisplay * nativeDisplay,const NativeWindowFactory * windowFactory,EGLDisplay eglDisplay,EGLConfig eglConfig,const glu::RenderConfig & config)261*35238bceSAndroid Build Coastguard Worker WindowSurfacePair createWindow(NativeDisplay *nativeDisplay, const NativeWindowFactory *windowFactory,
262*35238bceSAndroid Build Coastguard Worker                                EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig &config)
263*35238bceSAndroid Build Coastguard Worker {
264*35238bceSAndroid Build Coastguard Worker     const int width  = (config.width == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.width);
265*35238bceSAndroid Build Coastguard Worker     const int height = (config.height == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.height);
266*35238bceSAndroid Build Coastguard Worker     const WindowParams::Visibility visibility = getNativeWindowVisibility(config.windowVisibility);
267*35238bceSAndroid Build Coastguard Worker     NativeWindow *nativeWindow                = DE_NULL;
268*35238bceSAndroid Build Coastguard Worker     EGLSurface surface                        = EGL_NO_SURFACE;
269*35238bceSAndroid Build Coastguard Worker     const EGLAttrib attribList[]              = {EGL_NONE};
270*35238bceSAndroid Build Coastguard Worker 
271*35238bceSAndroid Build Coastguard Worker     nativeWindow = windowFactory->createWindow(nativeDisplay, eglDisplay, eglConfig, &attribList[0],
272*35238bceSAndroid Build Coastguard Worker                                                WindowParams(width, height, visibility));
273*35238bceSAndroid Build Coastguard Worker 
274*35238bceSAndroid Build Coastguard Worker     try
275*35238bceSAndroid Build Coastguard Worker     {
276*35238bceSAndroid Build Coastguard Worker         surface = eglu::createWindowSurface(*nativeDisplay, *nativeWindow, eglDisplay, eglConfig, attribList);
277*35238bceSAndroid Build Coastguard Worker     }
278*35238bceSAndroid Build Coastguard Worker     catch (...)
279*35238bceSAndroid Build Coastguard Worker     {
280*35238bceSAndroid Build Coastguard Worker         delete nativeWindow;
281*35238bceSAndroid Build Coastguard Worker         throw;
282*35238bceSAndroid Build Coastguard Worker     }
283*35238bceSAndroid Build Coastguard Worker 
284*35238bceSAndroid Build Coastguard Worker     return WindowSurfacePair(nativeWindow, surface);
285*35238bceSAndroid Build Coastguard Worker }
286*35238bceSAndroid Build Coastguard Worker 
createPixmap(NativeDisplay * nativeDisplay,const NativePixmapFactory * pixmapFactory,EGLDisplay eglDisplay,EGLConfig eglConfig,const glu::RenderConfig & config)287*35238bceSAndroid Build Coastguard Worker PixmapSurfacePair createPixmap(NativeDisplay *nativeDisplay, const NativePixmapFactory *pixmapFactory,
288*35238bceSAndroid Build Coastguard Worker                                EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig &config)
289*35238bceSAndroid Build Coastguard Worker {
290*35238bceSAndroid Build Coastguard Worker     const int width  = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
291*35238bceSAndroid Build Coastguard Worker     const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
292*35238bceSAndroid Build Coastguard Worker     NativePixmap *nativePixmap   = DE_NULL;
293*35238bceSAndroid Build Coastguard Worker     EGLSurface surface           = EGL_NO_SURFACE;
294*35238bceSAndroid Build Coastguard Worker     const EGLAttrib attribList[] = {EGL_NONE};
295*35238bceSAndroid Build Coastguard Worker 
296*35238bceSAndroid Build Coastguard Worker     nativePixmap = pixmapFactory->createPixmap(nativeDisplay, eglDisplay, eglConfig, &attribList[0], width, height);
297*35238bceSAndroid Build Coastguard Worker 
298*35238bceSAndroid Build Coastguard Worker     try
299*35238bceSAndroid Build Coastguard Worker     {
300*35238bceSAndroid Build Coastguard Worker         surface = eglu::createPixmapSurface(*nativeDisplay, *nativePixmap, eglDisplay, eglConfig, attribList);
301*35238bceSAndroid Build Coastguard Worker     }
302*35238bceSAndroid Build Coastguard Worker     catch (...)
303*35238bceSAndroid Build Coastguard Worker     {
304*35238bceSAndroid Build Coastguard Worker         delete nativePixmap;
305*35238bceSAndroid Build Coastguard Worker         throw;
306*35238bceSAndroid Build Coastguard Worker     }
307*35238bceSAndroid Build Coastguard Worker 
308*35238bceSAndroid Build Coastguard Worker     return PixmapSurfacePair(nativePixmap, surface);
309*35238bceSAndroid Build Coastguard Worker }
310*35238bceSAndroid Build Coastguard Worker 
createPBuffer(const Library & egl,EGLDisplay display,EGLConfig eglConfig,const glu::RenderConfig & config)311*35238bceSAndroid Build Coastguard Worker EGLSurface createPBuffer(const Library &egl, EGLDisplay display, EGLConfig eglConfig, const glu::RenderConfig &config)
312*35238bceSAndroid Build Coastguard Worker {
313*35238bceSAndroid Build Coastguard Worker     const int width  = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
314*35238bceSAndroid Build Coastguard Worker     const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
315*35238bceSAndroid Build Coastguard Worker     EGLSurface surface;
316*35238bceSAndroid Build Coastguard Worker     const EGLint attribList[] = {EGL_WIDTH, width, EGL_HEIGHT, height, EGL_NONE};
317*35238bceSAndroid Build Coastguard Worker 
318*35238bceSAndroid Build Coastguard Worker     surface = egl.createPbufferSurface(display, eglConfig, &(attribList[0]));
319*35238bceSAndroid Build Coastguard Worker     EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
320*35238bceSAndroid Build Coastguard Worker 
321*35238bceSAndroid Build Coastguard Worker     return surface;
322*35238bceSAndroid Build Coastguard Worker }
323*35238bceSAndroid Build Coastguard Worker 
makeCurrent(void)324*35238bceSAndroid Build Coastguard Worker void RenderContext::makeCurrent(void)
325*35238bceSAndroid Build Coastguard Worker {
326*35238bceSAndroid Build Coastguard Worker     const Library &egl = m_display->getLibrary();
327*35238bceSAndroid Build Coastguard Worker 
328*35238bceSAndroid Build Coastguard Worker     EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
329*35238bceSAndroid Build Coastguard Worker }
330*35238bceSAndroid Build Coastguard Worker 
getProcAddress(const char * name) const331*35238bceSAndroid Build Coastguard Worker glw::GenericFuncType RenderContext::getProcAddress(const char *name) const
332*35238bceSAndroid Build Coastguard Worker {
333*35238bceSAndroid Build Coastguard Worker     return (glw::GenericFuncType)m_display->getLibrary().getProcAddress(name);
334*35238bceSAndroid Build Coastguard Worker }
335*35238bceSAndroid Build Coastguard Worker 
create(const NativeDisplayFactory * displayFactory,const NativeWindowFactory * windowFactory,const NativePixmapFactory * pixmapFactory,const glu::RenderConfig & config,const glu::RenderContext * sharedContext)336*35238bceSAndroid Build Coastguard Worker void RenderContext::create(const NativeDisplayFactory *displayFactory, const NativeWindowFactory *windowFactory,
337*35238bceSAndroid Build Coastguard Worker                            const NativePixmapFactory *pixmapFactory, const glu::RenderConfig &config,
338*35238bceSAndroid Build Coastguard Worker                            const glu::RenderContext *sharedContext)
339*35238bceSAndroid Build Coastguard Worker {
340*35238bceSAndroid Build Coastguard Worker     glu::RenderConfig::SurfaceType surfaceType = config.surfaceType;
341*35238bceSAndroid Build Coastguard Worker 
342*35238bceSAndroid Build Coastguard Worker     DE_ASSERT(displayFactory);
343*35238bceSAndroid Build Coastguard Worker 
344*35238bceSAndroid Build Coastguard Worker     if (DE_NULL == sharedContext)
345*35238bceSAndroid Build Coastguard Worker         m_display = de::SharedPtr<NativeDisplay>(displayFactory->createDisplay());
346*35238bceSAndroid Build Coastguard Worker     else
347*35238bceSAndroid Build Coastguard Worker     {
348*35238bceSAndroid Build Coastguard Worker         const RenderContext *context = dynamic_cast<const RenderContext *>(sharedContext);
349*35238bceSAndroid Build Coastguard Worker         m_eglSharedContext           = context->m_eglContext;
350*35238bceSAndroid Build Coastguard Worker         m_display                    = context->m_display;
351*35238bceSAndroid Build Coastguard Worker     }
352*35238bceSAndroid Build Coastguard Worker 
353*35238bceSAndroid Build Coastguard Worker     m_eglDisplay       = eglu::getDisplay(*m_display);
354*35238bceSAndroid Build Coastguard Worker     const Library &egl = m_display->getLibrary();
355*35238bceSAndroid Build Coastguard Worker 
356*35238bceSAndroid Build Coastguard Worker     {
357*35238bceSAndroid Build Coastguard Worker         EGLint major = 0;
358*35238bceSAndroid Build Coastguard Worker         EGLint minor = 0;
359*35238bceSAndroid Build Coastguard Worker         EGLU_CHECK_CALL(egl, initialize(m_eglDisplay, &major, &minor));
360*35238bceSAndroid Build Coastguard Worker     }
361*35238bceSAndroid Build Coastguard Worker 
362*35238bceSAndroid Build Coastguard Worker     m_eglConfig = chooseConfig(egl, m_eglDisplay, config);
363*35238bceSAndroid Build Coastguard Worker 
364*35238bceSAndroid Build Coastguard Worker     if (surfaceType == glu::RenderConfig::SURFACETYPE_DONT_CARE)
365*35238bceSAndroid Build Coastguard Worker     {
366*35238bceSAndroid Build Coastguard Worker         // Choose based on what selected configuration supports
367*35238bceSAndroid Build Coastguard Worker         const EGLint supportedTypes = eglu::getConfigAttribInt(egl, m_eglDisplay, m_eglConfig, EGL_SURFACE_TYPE);
368*35238bceSAndroid Build Coastguard Worker 
369*35238bceSAndroid Build Coastguard Worker         if ((supportedTypes & EGL_WINDOW_BIT) != 0)
370*35238bceSAndroid Build Coastguard Worker             surfaceType = glu::RenderConfig::SURFACETYPE_WINDOW;
371*35238bceSAndroid Build Coastguard Worker         else if ((supportedTypes & EGL_PBUFFER_BIT) != 0)
372*35238bceSAndroid Build Coastguard Worker             surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC;
373*35238bceSAndroid Build Coastguard Worker         else if ((supportedTypes & EGL_PIXMAP_BIT) != 0)
374*35238bceSAndroid Build Coastguard Worker             surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE;
375*35238bceSAndroid Build Coastguard Worker         else
376*35238bceSAndroid Build Coastguard Worker             throw tcu::NotSupportedError("Selected EGL config doesn't support any surface types", DE_NULL, __FILE__,
377*35238bceSAndroid Build Coastguard Worker                                          __LINE__);
378*35238bceSAndroid Build Coastguard Worker     }
379*35238bceSAndroid Build Coastguard Worker 
380*35238bceSAndroid Build Coastguard Worker     switch (surfaceType)
381*35238bceSAndroid Build Coastguard Worker     {
382*35238bceSAndroid Build Coastguard Worker     case glu::RenderConfig::SURFACETYPE_WINDOW:
383*35238bceSAndroid Build Coastguard Worker     {
384*35238bceSAndroid Build Coastguard Worker         if (windowFactory)
385*35238bceSAndroid Build Coastguard Worker         {
386*35238bceSAndroid Build Coastguard Worker             const WindowSurfacePair windowSurface =
387*35238bceSAndroid Build Coastguard Worker                 createWindow(m_display.get(), windowFactory, m_eglDisplay, m_eglConfig, config);
388*35238bceSAndroid Build Coastguard Worker             m_window     = windowSurface.first;
389*35238bceSAndroid Build Coastguard Worker             m_eglSurface = windowSurface.second;
390*35238bceSAndroid Build Coastguard Worker         }
391*35238bceSAndroid Build Coastguard Worker         else
392*35238bceSAndroid Build Coastguard Worker             throw tcu::NotSupportedError("EGL platform doesn't support windows", DE_NULL, __FILE__, __LINE__);
393*35238bceSAndroid Build Coastguard Worker         break;
394*35238bceSAndroid Build Coastguard Worker     }
395*35238bceSAndroid Build Coastguard Worker 
396*35238bceSAndroid Build Coastguard Worker     case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
397*35238bceSAndroid Build Coastguard Worker     {
398*35238bceSAndroid Build Coastguard Worker         if (pixmapFactory)
399*35238bceSAndroid Build Coastguard Worker         {
400*35238bceSAndroid Build Coastguard Worker             const PixmapSurfacePair pixmapSurface =
401*35238bceSAndroid Build Coastguard Worker                 createPixmap(m_display.get(), pixmapFactory, m_eglDisplay, m_eglConfig, config);
402*35238bceSAndroid Build Coastguard Worker             m_pixmap     = pixmapSurface.first;
403*35238bceSAndroid Build Coastguard Worker             m_eglSurface = pixmapSurface.second;
404*35238bceSAndroid Build Coastguard Worker         }
405*35238bceSAndroid Build Coastguard Worker         else
406*35238bceSAndroid Build Coastguard Worker             throw tcu::NotSupportedError("EGL platform doesn't support pixmaps", DE_NULL, __FILE__, __LINE__);
407*35238bceSAndroid Build Coastguard Worker         break;
408*35238bceSAndroid Build Coastguard Worker     }
409*35238bceSAndroid Build Coastguard Worker 
410*35238bceSAndroid Build Coastguard Worker     case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
411*35238bceSAndroid Build Coastguard Worker         m_eglSurface = createPBuffer(egl, m_eglDisplay, m_eglConfig, config);
412*35238bceSAndroid Build Coastguard Worker         break;
413*35238bceSAndroid Build Coastguard Worker 
414*35238bceSAndroid Build Coastguard Worker     default:
415*35238bceSAndroid Build Coastguard Worker         throw tcu::InternalError("Invalid surface type");
416*35238bceSAndroid Build Coastguard Worker     }
417*35238bceSAndroid Build Coastguard Worker 
418*35238bceSAndroid Build Coastguard Worker     m_eglContext = createGLContext(egl, m_eglDisplay, m_eglConfig, config.type, m_eglSharedContext,
419*35238bceSAndroid Build Coastguard Worker                                    config.resetNotificationStrategy);
420*35238bceSAndroid Build Coastguard Worker 
421*35238bceSAndroid Build Coastguard Worker     EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
422*35238bceSAndroid Build Coastguard Worker 
423*35238bceSAndroid Build Coastguard Worker     // Init core functions
424*35238bceSAndroid Build Coastguard Worker 
425*35238bceSAndroid Build Coastguard Worker     if (hasExtension(egl, m_eglDisplay, "EGL_KHR_get_all_proc_addresses"))
426*35238bceSAndroid Build Coastguard Worker     {
427*35238bceSAndroid Build Coastguard Worker         // Use eglGetProcAddress() for core functions
428*35238bceSAndroid Build Coastguard Worker         GetProcFuncLoader funcLoader(egl);
429*35238bceSAndroid Build Coastguard Worker         glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
430*35238bceSAndroid Build Coastguard Worker     }
431*35238bceSAndroid Build Coastguard Worker #if defined(DEQP_GLES2_DIRECT_LINK)
432*35238bceSAndroid Build Coastguard Worker     else if (config.type.getAPI() == glu::ApiType::es(2, 0))
433*35238bceSAndroid Build Coastguard Worker     {
434*35238bceSAndroid Build Coastguard Worker         glw::initES20Direct(&m_glFunctions);
435*35238bceSAndroid Build Coastguard Worker     }
436*35238bceSAndroid Build Coastguard Worker #endif
437*35238bceSAndroid Build Coastguard Worker #if defined(DEQP_GLES3_DIRECT_LINK)
438*35238bceSAndroid Build Coastguard Worker     else if (config.type.getAPI() == glu::ApiType::es(3, 0))
439*35238bceSAndroid Build Coastguard Worker     {
440*35238bceSAndroid Build Coastguard Worker         glw::initES30Direct(&m_glFunctions);
441*35238bceSAndroid Build Coastguard Worker     }
442*35238bceSAndroid Build Coastguard Worker #endif
443*35238bceSAndroid Build Coastguard Worker #if defined(DEQP_GLES31_DIRECT_LINK)
444*35238bceSAndroid Build Coastguard Worker     else if (config.type.getAPI() == glu::ApiType::es(3, 1))
445*35238bceSAndroid Build Coastguard Worker     {
446*35238bceSAndroid Build Coastguard Worker         glw::initES31Direct(&m_glFunctions);
447*35238bceSAndroid Build Coastguard Worker     }
448*35238bceSAndroid Build Coastguard Worker #endif
449*35238bceSAndroid Build Coastguard Worker #if defined(DEQP_GLES32_DIRECT_LINK)
450*35238bceSAndroid Build Coastguard Worker     else if (config.type.getAPI() == glu::ApiType::es(3, 2))
451*35238bceSAndroid Build Coastguard Worker     {
452*35238bceSAndroid Build Coastguard Worker         glw::initES32Direct(&m_glFunctions);
453*35238bceSAndroid Build Coastguard Worker     }
454*35238bceSAndroid Build Coastguard Worker #endif
455*35238bceSAndroid Build Coastguard Worker     else
456*35238bceSAndroid Build Coastguard Worker     {
457*35238bceSAndroid Build Coastguard Worker         const char *libraryPath = DE_NULL;
458*35238bceSAndroid Build Coastguard Worker 
459*35238bceSAndroid Build Coastguard Worker         if (glu::isContextTypeES(config.type))
460*35238bceSAndroid Build Coastguard Worker         {
461*35238bceSAndroid Build Coastguard Worker             if (config.type.getMinorVersion() <= 2)
462*35238bceSAndroid Build Coastguard Worker                 libraryPath = DEQP_GLES2_LIBRARY_PATH;
463*35238bceSAndroid Build Coastguard Worker             else
464*35238bceSAndroid Build Coastguard Worker                 libraryPath = DEQP_GLES3_LIBRARY_PATH;
465*35238bceSAndroid Build Coastguard Worker         }
466*35238bceSAndroid Build Coastguard Worker         else
467*35238bceSAndroid Build Coastguard Worker             libraryPath = DEQP_OPENGL_LIBRARY_PATH;
468*35238bceSAndroid Build Coastguard Worker 
469*35238bceSAndroid Build Coastguard Worker         m_dynamicGLLibrary = new de::DynamicLibrary(libraryPath);
470*35238bceSAndroid Build Coastguard Worker 
471*35238bceSAndroid Build Coastguard Worker         DynamicFuncLoader funcLoader(m_dynamicGLLibrary);
472*35238bceSAndroid Build Coastguard Worker         glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
473*35238bceSAndroid Build Coastguard Worker     }
474*35238bceSAndroid Build Coastguard Worker 
475*35238bceSAndroid Build Coastguard Worker     // Init extension functions
476*35238bceSAndroid Build Coastguard Worker     {
477*35238bceSAndroid Build Coastguard Worker         GetProcFuncLoader extLoader(egl);
478*35238bceSAndroid Build Coastguard Worker         glu::initExtensionFunctions(&m_glFunctions, &extLoader, config.type.getAPI());
479*35238bceSAndroid Build Coastguard Worker     }
480*35238bceSAndroid Build Coastguard Worker 
481*35238bceSAndroid Build Coastguard Worker     {
482*35238bceSAndroid Build Coastguard Worker         EGLint width, height, depthBits, stencilBits, numSamples;
483*35238bceSAndroid Build Coastguard Worker         tcu::PixelFormat pixelFmt;
484*35238bceSAndroid Build Coastguard Worker 
485*35238bceSAndroid Build Coastguard Worker         egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width);
486*35238bceSAndroid Build Coastguard Worker         egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height);
487*35238bceSAndroid Build Coastguard Worker 
488*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_RED_SIZE, &pixelFmt.redBits);
489*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_GREEN_SIZE, &pixelFmt.greenBits);
490*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_BLUE_SIZE, &pixelFmt.blueBits);
491*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_ALPHA_SIZE, &pixelFmt.alphaBits);
492*35238bceSAndroid Build Coastguard Worker 
493*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_DEPTH_SIZE, &depthBits);
494*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_STENCIL_SIZE, &stencilBits);
495*35238bceSAndroid Build Coastguard Worker         egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_SAMPLES, &numSamples);
496*35238bceSAndroid Build Coastguard Worker 
497*35238bceSAndroid Build Coastguard Worker         EGLU_CHECK_MSG(egl, "Failed to query config attributes");
498*35238bceSAndroid Build Coastguard Worker 
499*35238bceSAndroid Build Coastguard Worker         m_glRenderTarget = tcu::RenderTarget(width, height, pixelFmt, depthBits, stencilBits, numSamples);
500*35238bceSAndroid Build Coastguard Worker     }
501*35238bceSAndroid Build Coastguard Worker 
502*35238bceSAndroid Build Coastguard Worker     egl.swapInterval(m_eglDisplay, 0);
503*35238bceSAndroid Build Coastguard Worker }
504*35238bceSAndroid Build Coastguard Worker 
destroy(void)505*35238bceSAndroid Build Coastguard Worker void RenderContext::destroy(void)
506*35238bceSAndroid Build Coastguard Worker {
507*35238bceSAndroid Build Coastguard Worker     if (m_eglDisplay != EGL_NO_DISPLAY)
508*35238bceSAndroid Build Coastguard Worker     {
509*35238bceSAndroid Build Coastguard Worker         const Library &egl = m_display->getLibrary();
510*35238bceSAndroid Build Coastguard Worker 
511*35238bceSAndroid Build Coastguard Worker         EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
512*35238bceSAndroid Build Coastguard Worker 
513*35238bceSAndroid Build Coastguard Worker         if (m_eglSurface != EGL_NO_SURFACE)
514*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
515*35238bceSAndroid Build Coastguard Worker 
516*35238bceSAndroid Build Coastguard Worker         if (m_eglContext != EGL_NO_CONTEXT)
517*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_CALL(egl, destroyContext(m_eglDisplay, m_eglContext));
518*35238bceSAndroid Build Coastguard Worker 
519*35238bceSAndroid Build Coastguard Worker         if (m_eglSharedContext == EGL_NO_CONTEXT)
520*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_CALL(egl, terminate(m_eglDisplay));
521*35238bceSAndroid Build Coastguard Worker 
522*35238bceSAndroid Build Coastguard Worker         m_eglDisplay = EGL_NO_DISPLAY;
523*35238bceSAndroid Build Coastguard Worker         m_eglSurface = EGL_NO_SURFACE;
524*35238bceSAndroid Build Coastguard Worker         m_eglContext = EGL_NO_CONTEXT;
525*35238bceSAndroid Build Coastguard Worker     }
526*35238bceSAndroid Build Coastguard Worker 
527*35238bceSAndroid Build Coastguard Worker     delete m_window;
528*35238bceSAndroid Build Coastguard Worker     delete m_pixmap;
529*35238bceSAndroid Build Coastguard Worker     delete m_dynamicGLLibrary;
530*35238bceSAndroid Build Coastguard Worker 
531*35238bceSAndroid Build Coastguard Worker     m_window           = DE_NULL;
532*35238bceSAndroid Build Coastguard Worker     m_pixmap           = DE_NULL;
533*35238bceSAndroid Build Coastguard Worker     m_dynamicGLLibrary = DE_NULL;
534*35238bceSAndroid Build Coastguard Worker }
535*35238bceSAndroid Build Coastguard Worker 
postIterate(void)536*35238bceSAndroid Build Coastguard Worker void RenderContext::postIterate(void)
537*35238bceSAndroid Build Coastguard Worker {
538*35238bceSAndroid Build Coastguard Worker     const Library &egl = m_display->getLibrary();
539*35238bceSAndroid Build Coastguard Worker 
540*35238bceSAndroid Build Coastguard Worker     if (m_window)
541*35238bceSAndroid Build Coastguard Worker     {
542*35238bceSAndroid Build Coastguard Worker         EGLBoolean swapOk    = egl.swapBuffers(m_eglDisplay, m_eglSurface);
543*35238bceSAndroid Build Coastguard Worker         EGLint error         = egl.getError();
544*35238bceSAndroid Build Coastguard Worker         const bool badWindow = error == EGL_BAD_SURFACE || error == EGL_BAD_NATIVE_WINDOW;
545*35238bceSAndroid Build Coastguard Worker 
546*35238bceSAndroid Build Coastguard Worker         if (!swapOk && !badWindow)
547*35238bceSAndroid Build Coastguard Worker             throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
548*35238bceSAndroid Build Coastguard Worker 
549*35238bceSAndroid Build Coastguard Worker         try
550*35238bceSAndroid Build Coastguard Worker         {
551*35238bceSAndroid Build Coastguard Worker             m_window->processEvents();
552*35238bceSAndroid Build Coastguard Worker         }
553*35238bceSAndroid Build Coastguard Worker         catch (const WindowDestroyedError &)
554*35238bceSAndroid Build Coastguard Worker         {
555*35238bceSAndroid Build Coastguard Worker             tcu::print("Warning: Window destroyed, recreating...\n");
556*35238bceSAndroid Build Coastguard Worker 
557*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
558*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
559*35238bceSAndroid Build Coastguard Worker             m_eglSurface = EGL_NO_SURFACE;
560*35238bceSAndroid Build Coastguard Worker 
561*35238bceSAndroid Build Coastguard Worker             delete m_window;
562*35238bceSAndroid Build Coastguard Worker             m_window = DE_NULL;
563*35238bceSAndroid Build Coastguard Worker 
564*35238bceSAndroid Build Coastguard Worker             try
565*35238bceSAndroid Build Coastguard Worker             {
566*35238bceSAndroid Build Coastguard Worker                 WindowSurfacePair windowSurface =
567*35238bceSAndroid Build Coastguard Worker                     createWindow(m_display.get(), m_nativeWindowFactory, m_eglDisplay, m_eglConfig, m_renderConfig);
568*35238bceSAndroid Build Coastguard Worker                 m_window     = windowSurface.first;
569*35238bceSAndroid Build Coastguard Worker                 m_eglSurface = windowSurface.second;
570*35238bceSAndroid Build Coastguard Worker 
571*35238bceSAndroid Build Coastguard Worker                 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
572*35238bceSAndroid Build Coastguard Worker 
573*35238bceSAndroid Build Coastguard Worker                 swapOk = EGL_TRUE;
574*35238bceSAndroid Build Coastguard Worker                 error  = EGL_SUCCESS;
575*35238bceSAndroid Build Coastguard Worker             }
576*35238bceSAndroid Build Coastguard Worker             catch (const std::exception &e)
577*35238bceSAndroid Build Coastguard Worker             {
578*35238bceSAndroid Build Coastguard Worker                 if (m_eglSurface)
579*35238bceSAndroid Build Coastguard Worker                 {
580*35238bceSAndroid Build Coastguard Worker                     egl.makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
581*35238bceSAndroid Build Coastguard Worker                     egl.destroySurface(m_eglDisplay, m_eglSurface);
582*35238bceSAndroid Build Coastguard Worker                     m_eglSurface = EGL_NO_SURFACE;
583*35238bceSAndroid Build Coastguard Worker                 }
584*35238bceSAndroid Build Coastguard Worker 
585*35238bceSAndroid Build Coastguard Worker                 delete m_window;
586*35238bceSAndroid Build Coastguard Worker                 m_window = DE_NULL;
587*35238bceSAndroid Build Coastguard Worker 
588*35238bceSAndroid Build Coastguard Worker                 throw tcu::ResourceError(string("Failed to re-create window: ") + e.what());
589*35238bceSAndroid Build Coastguard Worker             }
590*35238bceSAndroid Build Coastguard Worker         }
591*35238bceSAndroid Build Coastguard Worker 
592*35238bceSAndroid Build Coastguard Worker         if (!swapOk)
593*35238bceSAndroid Build Coastguard Worker         {
594*35238bceSAndroid Build Coastguard Worker             DE_ASSERT(badWindow);
595*35238bceSAndroid Build Coastguard Worker             throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
596*35238bceSAndroid Build Coastguard Worker         }
597*35238bceSAndroid Build Coastguard Worker 
598*35238bceSAndroid Build Coastguard Worker         // Refresh dimensions
599*35238bceSAndroid Build Coastguard Worker         {
600*35238bceSAndroid Build Coastguard Worker             int newWidth  = 0;
601*35238bceSAndroid Build Coastguard Worker             int newHeight = 0;
602*35238bceSAndroid Build Coastguard Worker 
603*35238bceSAndroid Build Coastguard Worker             egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &newWidth);
604*35238bceSAndroid Build Coastguard Worker             egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &newHeight);
605*35238bceSAndroid Build Coastguard Worker             EGLU_CHECK_MSG(egl, "Failed to query window size");
606*35238bceSAndroid Build Coastguard Worker 
607*35238bceSAndroid Build Coastguard Worker             if (newWidth != m_glRenderTarget.getWidth() || newHeight != m_glRenderTarget.getHeight())
608*35238bceSAndroid Build Coastguard Worker             {
609*35238bceSAndroid Build Coastguard Worker                 tcu::print("Warning: Window size changed (%dx%d -> %dx%d), test results might be invalid!\n",
610*35238bceSAndroid Build Coastguard Worker                            m_glRenderTarget.getWidth(), m_glRenderTarget.getHeight(), newWidth, newHeight);
611*35238bceSAndroid Build Coastguard Worker 
612*35238bceSAndroid Build Coastguard Worker                 m_glRenderTarget = tcu::RenderTarget(newWidth, newHeight, m_glRenderTarget.getPixelFormat(),
613*35238bceSAndroid Build Coastguard Worker                                                      m_glRenderTarget.getDepthBits(), m_glRenderTarget.getStencilBits(),
614*35238bceSAndroid Build Coastguard Worker                                                      m_glRenderTarget.getNumSamples());
615*35238bceSAndroid Build Coastguard Worker             }
616*35238bceSAndroid Build Coastguard Worker         }
617*35238bceSAndroid Build Coastguard Worker     }
618*35238bceSAndroid Build Coastguard Worker     else
619*35238bceSAndroid Build Coastguard Worker         m_glFunctions.flush();
620*35238bceSAndroid Build Coastguard Worker }
621*35238bceSAndroid Build Coastguard Worker 
622*35238bceSAndroid Build Coastguard Worker } // namespace
623*35238bceSAndroid Build Coastguard Worker 
GLContextFactory(const NativeDisplayFactoryRegistry & displayFactoryRegistry)624*35238bceSAndroid Build Coastguard Worker GLContextFactory::GLContextFactory(const NativeDisplayFactoryRegistry &displayFactoryRegistry)
625*35238bceSAndroid Build Coastguard Worker     : glu::ContextFactory("egl", "EGL OpenGL Context")
626*35238bceSAndroid Build Coastguard Worker     , m_displayFactoryRegistry(displayFactoryRegistry)
627*35238bceSAndroid Build Coastguard Worker {
628*35238bceSAndroid Build Coastguard Worker }
629*35238bceSAndroid Build Coastguard Worker 
createContext(const glu::RenderConfig & config,const tcu::CommandLine & cmdLine,const glu::RenderContext * sharedContext) const630*35238bceSAndroid Build Coastguard Worker glu::RenderContext *GLContextFactory::createContext(const glu::RenderConfig &config, const tcu::CommandLine &cmdLine,
631*35238bceSAndroid Build Coastguard Worker                                                     const glu::RenderContext *sharedContext) const
632*35238bceSAndroid Build Coastguard Worker {
633*35238bceSAndroid Build Coastguard Worker     const NativeDisplayFactory &displayFactory = selectNativeDisplayFactory(m_displayFactoryRegistry, cmdLine);
634*35238bceSAndroid Build Coastguard Worker 
635*35238bceSAndroid Build Coastguard Worker     const NativeWindowFactory *windowFactory;
636*35238bceSAndroid Build Coastguard Worker     const NativePixmapFactory *pixmapFactory;
637*35238bceSAndroid Build Coastguard Worker 
638*35238bceSAndroid Build Coastguard Worker     try
639*35238bceSAndroid Build Coastguard Worker     {
640*35238bceSAndroid Build Coastguard Worker         windowFactory = &selectNativeWindowFactory(displayFactory, cmdLine);
641*35238bceSAndroid Build Coastguard Worker     }
642*35238bceSAndroid Build Coastguard Worker     catch (const tcu::NotSupportedError &)
643*35238bceSAndroid Build Coastguard Worker     {
644*35238bceSAndroid Build Coastguard Worker         windowFactory = DE_NULL;
645*35238bceSAndroid Build Coastguard Worker     }
646*35238bceSAndroid Build Coastguard Worker 
647*35238bceSAndroid Build Coastguard Worker     try
648*35238bceSAndroid Build Coastguard Worker     {
649*35238bceSAndroid Build Coastguard Worker         pixmapFactory = &selectNativePixmapFactory(displayFactory, cmdLine);
650*35238bceSAndroid Build Coastguard Worker     }
651*35238bceSAndroid Build Coastguard Worker     catch (const tcu::NotSupportedError &)
652*35238bceSAndroid Build Coastguard Worker     {
653*35238bceSAndroid Build Coastguard Worker         pixmapFactory = DE_NULL;
654*35238bceSAndroid Build Coastguard Worker     }
655*35238bceSAndroid Build Coastguard Worker 
656*35238bceSAndroid Build Coastguard Worker     return new RenderContext(&displayFactory, windowFactory, pixmapFactory, config, sharedContext);
657*35238bceSAndroid Build Coastguard Worker }
658*35238bceSAndroid Build Coastguard Worker 
659*35238bceSAndroid Build Coastguard Worker } // namespace eglu
660