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