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 "host-common/GraphicsAgentFactory.h"
18 #include "host-common/testing/MockGraphicsAgentFactory.h"
19 #include "Standalone.h"
20
21 namespace gfxstream {
22 namespace gl {
23
24 static bool sDisplayNeedsInit = true;
25
getDisplay()26 EGLDisplay getDisplay() {
27 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
28
29 if (sDisplayNeedsInit) {
30 egl->eglUseOsEglApi(!shouldUseHostGpu(), EGL_FALSE);
31 }
32
33 EGLDisplay dpy = egl->eglGetDisplay(EGL_DEFAULT_DISPLAY);
34 EXPECT_TRUE(dpy != EGL_NO_DISPLAY);
35
36 if (sDisplayNeedsInit) {
37 EGLint eglMaj, eglMin;
38 EGLBoolean init_res = egl->eglInitialize(dpy, &eglMaj, &eglMin);
39 EXPECT_TRUE(init_res != EGL_FALSE);
40 sDisplayNeedsInit = false;
41 }
42
43 return dpy;
44 }
45
createConfig(EGLDisplay dpy,EGLint r,EGLint g,EGLint b,EGLint a,EGLint d,EGLint s,EGLint ms)46 EGLConfig createConfig(EGLDisplay dpy, EGLint r, EGLint g, EGLint b, EGLint a, EGLint d, EGLint s, EGLint ms) {
47 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
48 const EGLint configAttribs[] = {
49 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
50 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
51 EGL_RED_SIZE, r,
52 EGL_GREEN_SIZE, g,
53 EGL_BLUE_SIZE, b,
54 EGL_ALPHA_SIZE, a,
55 EGL_DEPTH_SIZE, d,
56 EGL_STENCIL_SIZE, s,
57 EGL_SAMPLES, ms,
58 EGL_NONE
59 };
60
61 EGLint nConfigs;
62 EGLConfig configOut;
63 EGLBoolean chooseConfigResult =
64 egl->eglChooseConfig(
65 dpy, configAttribs,
66 &configOut,
67 1,
68 &nConfigs);
69 EXPECT_TRUE(chooseConfigResult != EGL_FALSE);
70 EXPECT_TRUE(nConfigs > 0);
71 return configOut;
72 }
73
pbufferSurface(EGLDisplay dpy,::EGLConfig config,EGLint w,EGLint h)74 EGLSurface pbufferSurface(EGLDisplay dpy, ::EGLConfig config, EGLint w, EGLint h) {
75 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
76 const EGLint pbufferAttribs[] = {
77 EGL_WIDTH, w,
78 EGL_HEIGHT, h,
79 EGL_NONE,
80 };
81
82 EGLSurface pbuf =
83 egl->eglCreatePbufferSurface(
84 dpy, config, pbufferAttribs);
85
86 EXPECT_TRUE(pbuf != EGL_NO_SURFACE);
87 return pbuf;
88 }
89
createContext(EGLDisplay dpy,::EGLConfig config,EGLint maj,EGLint min)90 EGLContext createContext(EGLDisplay dpy, ::EGLConfig config, EGLint maj, EGLint min) {
91 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
92 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, maj, EGL_NONE };
93 EGLContext cxt = egl->eglCreateContext(dpy, config, EGL_NO_CONTEXT, contextAttribs);
94 EXPECT_TRUE(cxt != EGL_NO_CONTEXT);
95 return cxt;
96 }
97
destroyContext(EGLDisplay dpy,EGLContext cxt)98 void destroyContext(EGLDisplay dpy, EGLContext cxt) {
99 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
100 EGLBoolean destroyContextRes =
101 egl->eglDestroyContext(dpy, cxt);
102 EXPECT_TRUE(destroyContextRes != GL_FALSE);
103 }
104
destroySurface(EGLDisplay dpy,EGLSurface surface)105 void destroySurface(EGLDisplay dpy, EGLSurface surface) {
106 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
107 EGLBoolean destroySurfaceRes =
108 egl->eglDestroySurface(dpy, surface);
109 EXPECT_TRUE(destroySurfaceRes != GL_FALSE);
110 }
111
destroyDisplay(EGLDisplay dpy)112 void destroyDisplay(EGLDisplay dpy) {
113 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
114 EGLBoolean terminateRes =
115 egl->eglTerminate(dpy);
116 EXPECT_TRUE(terminateRes != GL_FALSE);
117 sDisplayNeedsInit = true;
118 }
119
120 // static
SetUpTestSuite()121 void GLTest::SetUpTestSuite() {
122 android::emulation::injectGraphicsAgents(android::emulation::MockGraphicsAgentFactory());
123 }
124
SetUp()125 void GLTest::SetUp() {
126 // setupStandaloneLibrarySearchPaths();
127
128 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
129 gl = LazyLoadedGLESv2Dispatch::get();
130 gles1 = LazyLoadedGLESv1Dispatch::get();
131 EXPECT_TRUE(egl != nullptr);
132 EXPECT_TRUE(gl != nullptr);
133 EXPECT_TRUE(gles1 != nullptr);
134
135 m_display = getDisplay();
136 m_config = createConfig(m_display, 8, 8, 8, 8, 24, 8, 0);
137 m_surface = pbufferSurface(m_display, m_config, kTestSurfaceSize[0],
138 kTestSurfaceSize[1]);
139 egl->eglSetMaxGLESVersion(3);
140 m_context = createContext(m_display, m_config, 3, 0);
141 egl->eglMakeCurrent(m_display, m_surface, m_surface, m_context);
142 }
143
TearDown()144 void GLTest::TearDown() {
145 const EGLDispatch* egl = LazyLoadedEGLDispatch::get();
146 egl->eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
147 EGL_NO_CONTEXT);
148 destroyContext(m_display, m_context);
149 destroySurface(m_display, m_surface);
150 destroyDisplay(m_display);
151
152 EXPECT_EQ(EGL_SUCCESS, egl->eglGetError())
153 << "GLTest TearDown found EGL error";
154 }
155
156 } // namespace gl
157 } // namespace gfxstream
158