1 /*
2  * Copyright (C) 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 package com.android.angleintegrationtest.common;
18 
19 import static javax.microedition.khronos.egl.EGL10.EGL_STENCIL_SIZE;
20 
21 import android.annotation.TargetApi;
22 import android.opengl.EGL14;
23 import android.opengl.GLES20;
24 import android.os.Build.VERSION_CODES;
25 import android.util.Log;
26 
27 import javax.microedition.khronos.egl.EGL10;
28 import javax.microedition.khronos.egl.EGLConfig;
29 import javax.microedition.khronos.egl.EGLContext;
30 import javax.microedition.khronos.egl.EGLDisplay;
31 import javax.microedition.khronos.egl.EGLSurface;
32 
33 /**
34  * GLES View Class to get access to GLES20.glGetString()
35  */
36 
37 @TargetApi(VERSION_CODES.GINGERBREAD)
38 public class GlesView {
39 
40     private static final EGL10 EGL = (EGL10) EGLContext.getEGL();
41     private String mRenderer = "";
42 
43     private final String TAG = this.getClass().getSimpleName();
44 
GlesView()45     public GlesView() {
46         createEGL();
47     }
48 
49     @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
createEGL()50     private void createEGL() {
51         int[] version = new int[2];
52         EGLDisplay display = EGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
53         EGL.eglInitialize(display, version);
54 
55         int[] numConfigs = new int[1];
56         EGL.eglGetConfigs(display, null, 0, numConfigs);
57         EGLConfig[] allConfigs = new EGLConfig[numConfigs[0]];
58         EGL.eglGetConfigs(display, allConfigs, numConfigs[0], numConfigs);
59 
60         int[] configAttrib =
61             new int[]{
62                 EGL10.EGL_RENDERABLE_TYPE,
63                 EGL14.EGL_OPENGL_ES2_BIT,
64                 EGL10.EGL_SURFACE_TYPE,
65                 EGL10.EGL_WINDOW_BIT,
66                 EGL10.EGL_RED_SIZE,
67                 8,
68                 EGL10.EGL_GREEN_SIZE,
69                 8,
70                 EGL10.EGL_BLUE_SIZE,
71                 8,
72                 EGL10.EGL_ALPHA_SIZE,
73                 8,
74                 EGL10.EGL_DEPTH_SIZE,
75                 16,
76                 EGL_STENCIL_SIZE,
77                 0,
78                 EGL10.EGL_NONE
79             };
80 
81         EGLConfig[] selectedConfig = new EGLConfig[1];
82         EGL.eglChooseConfig(display, configAttrib, selectedConfig, 1, numConfigs);
83         EGLConfig eglConfig;
84         if (selectedConfig[0] != null) {
85             eglConfig = selectedConfig[0];
86             Log.i(TAG, "Found matching EGL config");
87         } else {
88             Log.e(TAG, "Could not find matching EGL config");
89             throw new RuntimeException("No Matching EGL Config Found");
90         }
91 
92         EGLSurface surface = EGL.eglCreatePbufferSurface(display, eglConfig, null);
93         int[] contextAttribs = new int[]{EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
94         EGLContext context = EGL
95             .eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, contextAttribs);
96         EGL.eglMakeCurrent(display, surface, surface, context);
97 
98         Log.i(TAG, "CTS ANGLE Test :: GLES GL_VENDOR   : " + GLES20.glGetString(GLES20.GL_VENDOR));
99         Log.i(TAG, "CTS ANGLE Test :: GLES GL_VERSION  : " + GLES20.glGetString(GLES20.GL_VERSION));
100         Log.i(TAG,
101             "CTS ANGLE Test :: GLES GL_RENDERER : " + GLES20.glGetString(GLES20.GL_RENDERER));
102         mRenderer = GLES20.glGetString(GLES20.GL_RENDERER);
103     }
104 
getRenderer()105     public String getRenderer() {
106         return mRenderer;
107     }
108 
validateDeveloperOption(boolean angleEnabled)109     public boolean validateDeveloperOption(boolean angleEnabled)  {
110         if (angleEnabled) {
111             if (!mRenderer.toLowerCase().contains("angle")) {
112                 return false;
113             }
114         } else {
115             if (mRenderer.toLowerCase().contains("angle")) {
116                 return false;
117             }
118         }
119 
120         return true;
121     }
122 }
123