1 /* 2 * Copyright (C) 2013 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 android.opengl.cts; 18 19 import android.app.Activity; 20 import android.opengl.EGL14; 21 import android.opengl.EGLConfig; 22 import android.opengl.EGLContext; 23 import android.opengl.EGLDisplay; 24 import android.opengl.GLES20; 25 import android.test.ActivityInstrumentationTestCase2; 26 27 /** 28 * Tests using EGL contexts. 29 */ 30 public class EglContextTest extends ActivityInstrumentationTestCase2<Activity> { 31 32 public EglContextTest() { 33 super(Activity.class); 34 } 35 36 /** 37 * Tests creating then releasing an EGL context. 38 */ 39 public void testCreateAndReleaseContext() { 40 EGLDisplay eglDisplay = null; 41 EGLContext eglContext = null; 42 try { 43 eglDisplay = createEglDisplay(); 44 eglContext = createEglContext(eglDisplay); 45 destroyEglContext(eglDisplay, eglContext); 46 eglDisplay = null; 47 eglContext = null; 48 } finally { 49 if (eglDisplay != null) { 50 if (eglContext != null) { 51 EGL14.eglDestroyContext(eglDisplay, eglContext); 52 } 53 54 EGL14.eglTerminate(eglDisplay); 55 } 56 } 57 } 58 59 /** 60 * Returns an initialized default display. 61 */ 62 private static EGLDisplay createEglDisplay() { 63 EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); 64 if (eglDisplay == EGL14.EGL_NO_DISPLAY) { 65 throw new IllegalStateException("no EGL display"); 66 } 67 68 int[] major = new int[1]; 69 int[] minor = new int[1]; 70 if (!EGL14.eglInitialize(eglDisplay, major, 0, minor, 0)) { 71 throw new IllegalStateException("error in eglInitialize"); 72 } 73 checkGlError(); 74 75 return eglDisplay; 76 } 77 78 /** 79 * Returns a new GL context for the specified {@code eglDisplay}. 80 */ 81 private static EGLContext createEglContext(EGLDisplay eglDisplay) { 82 int[] contextAttributes = { EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE }; 83 EGLContext eglContext = EGL14.eglCreateContext(eglDisplay, 84 getEglConfig(eglDisplay), EGL14.EGL_NO_CONTEXT, contextAttributes, 0); 85 checkGlError(); 86 87 return eglContext; 88 } 89 90 /** 91 * Destroys the GL context identifier by {@code eglDisplay} and {@code eglContext}. 92 */ 93 private static void destroyEglContext(EGLDisplay eglDisplay, EGLContext eglContext) { 94 EGL14.eglMakeCurrent(eglDisplay, 95 EGL14.EGL_NO_SURFACE, 96 EGL14.EGL_NO_SURFACE, 97 EGL14.EGL_NO_CONTEXT); 98 int error = EGL14.eglGetError(); 99 if (error != EGL14.EGL_SUCCESS) { 100 throw new RuntimeException("error releasing context: " + error); 101 } 102 103 EGL14.eglDestroyContext(eglDisplay, eglContext); 104 error = EGL14.eglGetError(); 105 if (error != EGL14.EGL_SUCCESS) { 106 throw new RuntimeException("error destroying context: " + error); 107 } 108 109 EGL14.eglReleaseThread(); 110 error = EGL14.eglGetError(); 111 if (error != EGL14.EGL_SUCCESS) { 112 throw new RuntimeException("error releasing thread: " + error); 113 } 114 115 EGL14.eglTerminate(eglDisplay); 116 error = EGL14.eglGetError(); 117 if (error != EGL14.EGL_SUCCESS) { 118 throw new RuntimeException("error terminating display: " + error); 119 } 120 } 121 122 private static EGLConfig getEglConfig(EGLDisplay eglDisplay) { 123 // Get an EGLConfig. 124 final int EGL_OPENGL_ES2_BIT = 4; 125 final int RED_SIZE = 8; 126 final int GREEN_SIZE = 8; 127 final int BLUE_SIZE = 8; 128 final int ALPHA_SIZE = 8; 129 final int DEPTH_SIZE = 0; 130 final int STENCIL_SIZE = 0; 131 final int[] DEFAULT_CONFIGURATION = new int[] { 132 EGL14.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 133 EGL14.EGL_RED_SIZE, RED_SIZE, 134 EGL14.EGL_GREEN_SIZE, GREEN_SIZE, 135 EGL14.EGL_BLUE_SIZE, BLUE_SIZE, 136 EGL14.EGL_ALPHA_SIZE, ALPHA_SIZE, 137 EGL14.EGL_DEPTH_SIZE, DEPTH_SIZE, 138 EGL14.EGL_STENCIL_SIZE, STENCIL_SIZE, 139 EGL14.EGL_NONE}; 140 141 int[] configsCount = new int[1]; 142 EGLConfig[] eglConfigs = new EGLConfig[1]; 143 if (!EGL14.eglChooseConfig( 144 eglDisplay, DEFAULT_CONFIGURATION, 0, eglConfigs, 0, 1, configsCount, 0)) { 145 throw new RuntimeException("eglChooseConfig failed"); 146 } 147 return eglConfigs[0]; 148 } 149 150 /** 151 * Checks for a GL error using {@link GLES20#glGetError()}. 152 * 153 * @throws RuntimeException if there is a GL error 154 */ 155 private static void checkGlError() { 156 int errorCode; 157 if ((errorCode = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { 158 throw new RuntimeException("gl error: " + Integer.toHexString(errorCode)); 159 } 160 } 161 162 } 163