1 /* 2 * Copyright (C) 2011 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.effect.cts; 18 19 import android.graphics.Bitmap; 20 import android.opengl.GLUtils; 21 import android.opengl.GLES20; 22 23 import javax.microedition.khronos.egl.*; 24 import javax.microedition.khronos.opengles.*; 25 26 public class GLEnv { 27 28 private EGLContext mEGLContext; 29 private EGLSurface mEGLSurface; 30 private EGLDisplay mEGLDisplay; 31 private EGLConfig mEGLConfig; 32 33 private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; 34 private static final int EGL_OPENGL_ES2_BIT = 0x0004; 35 36 public GLEnv() { 37 EGL10 egl = (EGL10)EGLContext.getEGL(); 38 39 mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 40 checkForEGLErrors("eglGetDisplay"); 41 42 int[] version = new int[2]; 43 egl.eglInitialize(mEGLDisplay, version); 44 int[] configSpec = { 45 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT, 46 EGL10.EGL_RED_SIZE, 8, 47 EGL10.EGL_GREEN_SIZE, 8, 48 EGL10.EGL_BLUE_SIZE, 8, 49 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 50 EGL10.EGL_NONE 51 }; 52 EGLConfig[] configs = new EGLConfig[1]; 53 int[] num_config = new int[1]; 54 egl.eglChooseConfig(mEGLDisplay, configSpec, configs, 1, num_config); 55 checkForEGLErrors("eglChooseConfig"); 56 if (num_config[0] < 1) { 57 throw new RuntimeException("Could not find a suitable config for EGL context!"); 58 } 59 mEGLConfig = configs[0]; 60 61 int[] attribs = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; 62 mEGLContext = egl.eglCreateContext(mEGLDisplay, mEGLConfig, EGL10.EGL_NO_CONTEXT, attribs); 63 checkForEGLErrors("eglCreateContext"); 64 65 int[] surfaceSize = { EGL10.EGL_WIDTH, 1, EGL10.EGL_HEIGHT, 1, EGL10.EGL_NONE }; 66 mEGLSurface = egl.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceSize); 67 checkForEGLErrors("eglCreatePbufferSurface"); 68 } 69 70 public void makeCurrent() { 71 EGL10 egl = (EGL10)EGLContext.getEGL(); 72 egl.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext); 73 checkForEGLErrors("eglMakeCurrent"); 74 } 75 76 public int generateTexture() { 77 int textures[] = new int[1]; 78 GLES20.glGenTextures(1, textures, 0); 79 return textures[0]; 80 } 81 82 public int bitmapToTexture(Bitmap bitmap) { 83 int texId = generateTexture(); 84 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId); 85 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); 86 return texId; 87 } 88 89 public void releaseTexture(int texId) { 90 int[] textures = new int[1]; 91 textures[0] = texId; 92 GLES20.glDeleteTextures(1, textures, 0); 93 } 94 95 public void tearDown() { 96 EGL10 egl = (EGL10)EGLContext.getEGL(); 97 egl.eglDestroySurface(mEGLDisplay, mEGLSurface); 98 egl.eglDestroyContext(mEGLDisplay, mEGLContext); 99 } 100 101 public void checkForEGLErrors(String operation) { 102 EGL10 egl = (EGL10)EGLContext.getEGL(); 103 int error = egl.eglGetError(); 104 if (error != EGL10.EGL_SUCCESS) { 105 throw new RuntimeException("Operation '" + operation + "' caused EGL error: " + error); 106 } 107 } 108 109 } 110