1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 #include <GLES2/gl2.h>
23 #include <GLES2/gl2ext.h>
24 #include <string>
25 #include <vector>
26 #include <sstream>
27 #include <algorithm>
28
29 static EGLConfig eglConf;
30 static EGLSurface eglSurface;
31 static EGLContext eglCtx;
32 static EGLDisplay eglDisp;
33
setupEGL(int w,int h)34 void setupEGL(int w, int h) {
35 const EGLint confAttr[] = {
36 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
37 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
38 EGL_RED_SIZE, 8,
39 EGL_GREEN_SIZE, 8,
40 EGL_BLUE_SIZE, 8,
41 EGL_ALPHA_SIZE, 8,
42 EGL_DEPTH_SIZE, 16,
43 EGL_NONE
44 };
45
46 const EGLint ctxAttr[] = {
47 EGL_CONTEXT_CLIENT_VERSION, 2,
48 EGL_NONE
49 };
50
51 const EGLint surfaceAttr[] = {
52 EGL_WIDTH, w,
53 EGL_HEIGHT, h,
54 EGL_NONE
55 };
56
57 EGLint eglMajVers, eglMinVers;
58 EGLint numConfigs;
59
60 eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
61 eglInitialize(eglDisp, &eglMajVers, &eglMinVers);
62
63 eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs);
64
65 eglCtx = eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr);
66
67 eglSurface = eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr);
68
69 eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx);
70 }
71
shutdownEGL()72 void shutdownEGL() {
73 eglMakeCurrent(eglDisp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
74 eglDestroyContext(eglDisp, eglCtx);
75 eglDestroySurface(eglDisp, eglSurface);
76 eglTerminate(eglDisp);
77
78 eglDisp = EGL_NO_DISPLAY;
79 eglSurface = EGL_NO_SURFACE;
80 eglCtx = EGL_NO_CONTEXT;
81 }
82
83 /**
84 * The following OpenGL extensions are required:
85 * GL_EXT_color_buffer_half_float
86 * GL_EXT_shader_framebuffer_fetch
87 */
TEST(glExtensions,glExtensions)88 TEST(glExtensions, glExtensions) {
89 std::vector<std::string> neededExts {"GL_EXT_color_buffer_half_float",
90 "GL_EXT_shader_framebuffer_fetch"};
91
92 setupEGL(64,64);
93
94 std::string extString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)));
95
96 std::istringstream iss(extString);
97 //split by space
98 std::vector<std::string> availableExts(std::istream_iterator<std::string>{iss},
99 std::istream_iterator<std::string>());
100
101 for (auto& ext : neededExts) {
102 if (std::find(availableExts.begin(), availableExts.end(), ext) == availableExts.end())
103 ADD_FAILURE() << "Could not find the GL extension: " << ext;
104 }
105
106 shutdownEGL();
107 }
108
109 /**
110 * The following EGL extensions are required:
111 * EGL_ANDROID_get_frame_timestamps
112 * EGL_ANDROID_presentation_time
113 * EGL_KHR_fence_sync
114 */
TEST(glExtensions,eglExtensions)115 TEST(glExtensions, eglExtensions) {
116 std::vector<std::string> neededExts {"EGL_ANDROID_get_frame_timestamps",
117 "EGL_ANDROID_presentation_time",
118 "EGL_KHR_fence_sync"};
119
120 setupEGL(64,64);
121
122 std::string extString(eglQueryString(eglDisp, EGL_EXTENSIONS));
123
124 std::istringstream iss(extString);
125 //split by space
126 std::vector<std::string> availableExts(std::istream_iterator<std::string>{iss},
127 std::istream_iterator<std::string>());
128
129 for (auto& ext : neededExts) {
130 if (std::find(availableExts.begin(), availableExts.end(), ext) == availableExts.end())
131 ADD_FAILURE() << "Could not find the EGL extension: " << ext;
132 }
133
134 shutdownEGL();
135 }
136