1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "OpenGLTestContext.h"
16
17 #include "Standalone.h"
18
19 namespace emugl {
20
21 static bool sDisplayNeedsInit = true;
22
getDisplay()23 EGLDisplay getDisplay() {
24 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
25
26 if (sDisplayNeedsInit) {
27 egl->eglUseOsEglApi(!shouldUseHostGpu());
28 }
29
30 EGLDisplay dpy = egl->eglGetDisplay(EGL_DEFAULT_DISPLAY);
31 EXPECT_TRUE(dpy != EGL_NO_DISPLAY);
32
33 if (sDisplayNeedsInit) {
34 EGLint eglMaj, eglMin;
35 EGLBoolean init_res = egl->eglInitialize(dpy, &eglMaj, &eglMin);
36 EXPECT_TRUE(init_res != EGL_FALSE);
37 sDisplayNeedsInit = false;
38 }
39
40 return dpy;
41 }
42
createConfig(EGLDisplay dpy,EGLint r,EGLint g,EGLint b,EGLint a,EGLint d,EGLint s,EGLint ms)43 EGLConfig createConfig(EGLDisplay dpy, EGLint r, EGLint g, EGLint b, EGLint a, EGLint d, EGLint s, EGLint ms) {
44 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
45 const EGLint configAttribs[] = {
46 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
47 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
48 EGL_RED_SIZE, r,
49 EGL_GREEN_SIZE, g,
50 EGL_BLUE_SIZE, b,
51 EGL_ALPHA_SIZE, a,
52 EGL_DEPTH_SIZE, d,
53 EGL_STENCIL_SIZE, s,
54 EGL_SAMPLES, ms,
55 EGL_NONE
56 };
57
58 EGLint nConfigs;
59 EGLConfig configOut;
60 EGLBoolean chooseConfigResult =
61 egl->eglChooseConfig(
62 dpy, configAttribs,
63 &configOut,
64 1,
65 &nConfigs);
66 EXPECT_TRUE(chooseConfigResult != EGL_FALSE);
67 EXPECT_TRUE(nConfigs > 0);
68 return configOut;
69 }
70
pbufferSurface(EGLDisplay dpy,::EGLConfig config,EGLint w,EGLint h)71 EGLSurface pbufferSurface(EGLDisplay dpy, ::EGLConfig config, EGLint w, EGLint h) {
72 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
73 const EGLint pbufferAttribs[] = {
74 EGL_WIDTH, w,
75 EGL_HEIGHT, h,
76 EGL_NONE,
77 };
78
79 EGLSurface pbuf =
80 egl->eglCreatePbufferSurface(
81 dpy, config, pbufferAttribs);
82
83 EXPECT_TRUE(pbuf != EGL_NO_SURFACE);
84 return pbuf;
85 }
86
createContext(EGLDisplay dpy,::EGLConfig config,EGLint maj,EGLint min)87 EGLContext createContext(EGLDisplay dpy, ::EGLConfig config, EGLint maj, EGLint min) {
88 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
89 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, maj, EGL_NONE };
90 EGLContext cxt = egl->eglCreateContext(dpy, config, EGL_NO_CONTEXT, contextAttribs);
91 EXPECT_TRUE(cxt != EGL_NO_CONTEXT);
92 return cxt;
93 }
94
destroyContext(EGLDisplay dpy,EGLContext cxt)95 void destroyContext(EGLDisplay dpy, EGLContext cxt) {
96 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
97 EGLBoolean destroyContextRes =
98 egl->eglDestroyContext(dpy, cxt);
99 EXPECT_TRUE(destroyContextRes != GL_FALSE);
100 }
101
destroySurface(EGLDisplay dpy,EGLSurface surface)102 void destroySurface(EGLDisplay dpy, EGLSurface surface) {
103 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
104 EGLBoolean destroySurfaceRes =
105 egl->eglDestroySurface(dpy, surface);
106 EXPECT_TRUE(destroySurfaceRes != GL_FALSE);
107 }
108
destroyDisplay(EGLDisplay dpy)109 void destroyDisplay(EGLDisplay dpy) {
110 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
111 EGLBoolean terminateRes =
112 egl->eglTerminate(dpy);
113 EXPECT_TRUE(terminateRes != GL_FALSE);
114 sDisplayNeedsInit = true;
115 }
116
SetUp()117 void GLTest::SetUp() {
118 // setupStandaloneLibrarySearchPaths();
119
120 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
121 gl = LazyLoadedGLESv2Dispatch::get();
122 EXPECT_TRUE(egl != nullptr);
123 EXPECT_TRUE(gl != nullptr);
124
125 m_display = getDisplay();
126 m_config = createConfig(m_display, 8, 8, 8, 8, 24, 8, 0);
127 m_surface = pbufferSurface(m_display, m_config, kTestSurfaceSize[0],
128 kTestSurfaceSize[1]);
129 egl->eglSetMaxGLESVersion(3);
130 m_context = createContext(m_display, m_config, 3, 0);
131 egl->eglMakeCurrent(m_display, m_surface, m_surface, m_context);
132 }
133
TearDown()134 void GLTest::TearDown() {
135 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
136 egl->eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
137 EGL_NO_CONTEXT);
138 destroyContext(m_display, m_context);
139 destroySurface(m_display, m_surface);
140 destroyDisplay(m_display);
141
142 EXPECT_EQ(EGL_SUCCESS, egl->eglGetError())
143 << "GLTest TearDown found EGL error";
144 }
145
146 } // namespace emugl
147