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 #include "testutils.h"
29 
30 static EGLConfig eglConf;
31 static EGLSurface eglSurface;
32 static EGLContext eglCtx;
33 static EGLDisplay eglDisp;
34 
setupEGL(int w,int h)35 void setupEGL(int w, int h) {
36     const EGLint confAttr[] = {
37             EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
38             EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
39             EGL_RED_SIZE,   8,
40             EGL_GREEN_SIZE, 8,
41             EGL_BLUE_SIZE,  8,
42             EGL_ALPHA_SIZE, 8,
43             EGL_DEPTH_SIZE, 16,
44             EGL_NONE
45     };
46 
47     const EGLint ctxAttr[] = {
48             EGL_CONTEXT_CLIENT_VERSION, 2,
49             EGL_NONE
50     };
51 
52     const EGLint surfaceAttr[] = {
53              EGL_WIDTH, w,
54              EGL_HEIGHT, h,
55              EGL_NONE
56     };
57 
58     EGLint eglMajVers, eglMinVers;
59     EGLint numConfigs;
60 
61     eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
62     eglInitialize(eglDisp, &eglMajVers, &eglMinVers);
63 
64     eglChooseConfig(eglDisp, confAttr, &eglConf, 1, &numConfigs);
65 
66     eglCtx = eglCreateContext(eglDisp, eglConf, EGL_NO_CONTEXT, ctxAttr);
67 
68     eglSurface = eglCreatePbufferSurface(eglDisp, eglConf, surfaceAttr);
69 
70     eglMakeCurrent(eglDisp, eglSurface, eglSurface, eglCtx);
71 }
72 
shutdownEGL()73 void shutdownEGL() {
74     eglMakeCurrent(eglDisp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
75     eglDestroyContext(eglDisp, eglCtx);
76     eglDestroySurface(eglDisp, eglSurface);
77     eglTerminate(eglDisp);
78 
79     eglDisp = EGL_NO_DISPLAY;
80     eglSurface = EGL_NO_SURFACE;
81     eglCtx = EGL_NO_CONTEXT;
82 }
83 
84 /**
85  * The following OpenGL extensions are required:
86  *     GL_EXT_color_buffer_half_float
87  *     GL_EXT_shader_framebuffer_fetch
88  */
TEST(glExtensions,glExtensions)89 TEST(glExtensions, glExtensions) {
90     ASSUME_GAMECORE_CERTIFIED();
91 
92     std::vector<std::string> neededExts {"GL_EXT_color_buffer_half_float",
93                                          "GL_EXT_shader_framebuffer_fetch"};
94 
95     setupEGL(64,64);
96 
97     std::string extString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)));
98 
99     std::istringstream iss(extString);
100     //split by space
101     std::vector<std::string> availableExts(std::istream_iterator<std::string>{iss},
102                                            std::istream_iterator<std::string>());
103 
104     for (auto& ext : neededExts) {
105         if (std::find(availableExts.begin(), availableExts.end(), ext) == availableExts.end())
106             ADD_FAILURE() << "Could not find the GL extension: " << ext;
107     }
108 
109     shutdownEGL();
110 }
111 
112 /**
113  * The following EGL extensions are required:
114  *     EGL_ANDROID_get_frame_timestamps
115  *     EGL_ANDROID_presentation_time
116  *     EGL_KHR_fence_sync
117  */
TEST(glExtensions,eglExtensions)118 TEST(glExtensions, eglExtensions) {
119     ASSUME_GAMECORE_CERTIFIED();
120 
121     std::vector<std::string> neededExts {"EGL_ANDROID_get_frame_timestamps",
122                                          "EGL_ANDROID_presentation_time",
123                                          "EGL_KHR_fence_sync"};
124 
125     setupEGL(64,64);
126 
127     std::string extString(eglQueryString(eglDisp, EGL_EXTENSIONS));
128 
129     std::istringstream iss(extString);
130     //split by space
131     std::vector<std::string> availableExts(std::istream_iterator<std::string>{iss},
132                                            std::istream_iterator<std::string>());
133 
134     for (auto& ext : neededExts) {
135         if (std::find(availableExts.begin(), availableExts.end(), ext) == availableExts.end())
136             ADD_FAILURE() << "Could not find the EGL extension: " << ext;
137     }
138 
139     shutdownEGL();
140 }
141