GLUtils.java revision c8405b6d02306d4cba4a169a4c24d7791dbaa0b4
1/* 2 * Copyright (C) 2006 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 17package android.opengl; 18 19import javax.microedition.khronos.opengles.GL10; 20import android.graphics.Bitmap; 21 22/** 23 * 24 * Utility class to help bridging OpenGL ES and Android APIs. 25 * 26 */ 27 28public final class GLUtils { 29 30 /* 31 * We use a class initializer to allow the native code to cache some 32 * field offsets. 33 */ 34 static { 35 nativeClassInit(); 36 } 37 38 private GLUtils() { 39 } 40 41 /** 42 * return the internal format as defined by OpenGL ES of the supplied bitmap. 43 * @param bitmap 44 * @return the internal format of the bitmap. 45 */ 46 public static int getInternalFormat(Bitmap bitmap) { 47 if (bitmap == null) { 48 throw new NullPointerException("getInternalFormat can't be used with a null Bitmap"); 49 } 50 int result = native_getInternalFormat(bitmap); 51 if (result < 0) { 52 throw new IllegalArgumentException("Unknown internalformat"); 53 } 54 return result; 55 } 56 57 /** 58 * Return the type as defined by OpenGL ES of the supplied bitmap, if there 59 * is one. If the bitmap is stored in a compressed format, it may not have 60 * a valid OpenGL ES type. 61 * @throws IllegalArgumentException if the bitmap does not have a type. 62 * @param bitmap 63 * @return the OpenGL ES type of the bitmap. 64 */ 65 public static int getType(Bitmap bitmap) { 66 if (bitmap == null) { 67 throw new NullPointerException("getType can't be used with a null Bitmap"); 68 } 69 int result = native_getType(bitmap); 70 if (result < 0) { 71 throw new IllegalArgumentException("Unknown type"); 72 } 73 return result; 74 } 75 76 /** 77 * Calls glTexImage2D() on the current OpenGL context. If no context is 78 * current the behavior is the same as calling glTexImage2D() with no 79 * current context, that is, eglGetError() will return the appropriate 80 * error. 81 * Unlike glTexImage2D() bitmap cannot be null and will raise an exception 82 * in that case. 83 * All other parameters are identical to those used for glTexImage2D(). 84 * 85 * NOTE: this method doesn't change GL_UNPACK_ALIGNMENT, you must make 86 * sure to set it properly according to the supplied bitmap. 87 * 88 * Whether or not bitmap can have non power of two dimensions depends on 89 * the current OpenGL context. Always check glGetError() some time 90 * after calling this method, just like when using OpenGL directly. 91 * 92 * @param target 93 * @param level 94 * @param internalformat 95 * @param bitmap 96 * @param border 97 */ 98 public static void texImage2D(int target, int level, int internalformat, 99 Bitmap bitmap, int border) { 100 if (bitmap == null) { 101 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 102 } 103 if (native_texImage2D(target, level, internalformat, bitmap, -1, border)!=0) { 104 throw new IllegalArgumentException("invalid Bitmap format"); 105 } 106 } 107 108 /** 109 * A version of texImage2D() that takes an explicit type parameter 110 * as defined by the OpenGL ES specification. The actual type and 111 * internalformat of the bitmap must be compatible with the specified 112 * type and internalformat parameters. 113 * 114 * @param target 115 * @param level 116 * @param internalformat 117 * @param bitmap 118 * @param type 119 * @param border 120 */ 121 public static void texImage2D(int target, int level, int internalformat, 122 Bitmap bitmap, int type, int border) { 123 if (bitmap == null) { 124 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 125 } 126 if (native_texImage2D(target, level, internalformat, bitmap, type, border)!=0) { 127 throw new IllegalArgumentException("invalid Bitmap format"); 128 } 129 } 130 131 /** 132 * A version of texImage2D that determines the internalFormat and type 133 * automatically. 134 * 135 * @param target 136 * @param level 137 * @param bitmap 138 * @param border 139 */ 140 public static void texImage2D(int target, int level, Bitmap bitmap, 141 int border) { 142 if (bitmap == null) { 143 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 144 } 145 if (native_texImage2D(target, level, -1, bitmap, -1, border)!=0) { 146 throw new IllegalArgumentException("invalid Bitmap format"); 147 } 148 } 149 150 /** 151 * Calls glTexSubImage2D() on the current OpenGL context. If no context is 152 * current the behavior is the same as calling glTexSubImage2D() with no 153 * current context, that is, eglGetError() will return the appropriate 154 * error. 155 * Unlike glTexSubImage2D() bitmap cannot be null and will raise an exception 156 * in that case. 157 * All other parameters are identical to those used for glTexSubImage2D(). 158 * 159 * NOTE: this method doesn't change GL_UNPACK_ALIGNMENT, you must make 160 * sure to set it properly according to the supplied bitmap. 161 * 162 * Whether or not bitmap can have non power of two dimensions depends on 163 * the current OpenGL context. Always check glGetError() some time 164 * after calling this method, just like when using OpenGL directly. 165 * 166 * @param target 167 * @param level 168 * @param xoffset 169 * @param yoffset 170 * @param bitmap 171 */ 172 public static void texSubImage2D(int target, int level, int xoffset, int yoffset, 173 Bitmap bitmap) { 174 if (bitmap == null) { 175 throw new NullPointerException("texSubImage2D can't be used with a null Bitmap"); 176 } 177 int type = getType(bitmap); 178 if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, -1, type)!=0) { 179 throw new IllegalArgumentException("invalid Bitmap format"); 180 } 181 } 182 183 /** 184 * A version of texSubImage2D() that takes an explicit type parameter 185 * as defined by the OpenGL ES specification. 186 * 187 * @param target 188 * @param level 189 * @param xoffset 190 * @param yoffset 191 * @param bitmap 192 * @param type 193 */ 194 public static void texSubImage2D(int target, int level, int xoffset, int yoffset, 195 Bitmap bitmap, int format, int type) { 196 if (bitmap == null) { 197 throw new NullPointerException("texSubImage2D can't be used with a null Bitmap"); 198 } 199 if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, format, type)!=0) { 200 throw new IllegalArgumentException("invalid Bitmap format"); 201 } 202 } 203 204 native private static void nativeClassInit(); 205 206 native private static int native_getInternalFormat(Bitmap bitmap); 207 native private static int native_getType(Bitmap bitmap); 208 native private static int native_texImage2D(int target, int level, int internalformat, 209 Bitmap bitmap, int type, int border); 210 native private static int native_texSubImage2D(int target, int level, int xoffset, int yoffset, 211 Bitmap bitmap, int format, int type); 212} 213