GLUtils.java revision 70a1875635cf988b9962fbe2325ea73fd346858c
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 if (bitmap.isRecycled()) { 51 throw new IllegalArgumentException("bitmap is recycled"); 52 } 53 int result = native_getInternalFormat(bitmap); 54 if (result < 0) { 55 throw new IllegalArgumentException("Unknown internalformat"); 56 } 57 return result; 58 } 59 60 /** 61 * Return the type as defined by OpenGL ES of the supplied bitmap, if there 62 * is one. If the bitmap is stored in a compressed format, it may not have 63 * a valid OpenGL ES type. 64 * @throws IllegalArgumentException if the bitmap does not have a type. 65 * @param bitmap 66 * @return the OpenGL ES type of the bitmap. 67 */ 68 public static int getType(Bitmap bitmap) { 69 if (bitmap == null) { 70 throw new NullPointerException("getType can't be used with a null Bitmap"); 71 } 72 if (bitmap.isRecycled()) { 73 throw new IllegalArgumentException("bitmap is recycled"); 74 } 75 int result = native_getType(bitmap); 76 if (result < 0) { 77 throw new IllegalArgumentException("Unknown type"); 78 } 79 return result; 80 } 81 82 /** 83 * Calls glTexImage2D() on the current OpenGL context. If no context is 84 * current the behavior is the same as calling glTexImage2D() with no 85 * current context, that is, eglGetError() will return the appropriate 86 * error. 87 * Unlike glTexImage2D() bitmap cannot be null and will raise an exception 88 * in that case. 89 * All other parameters are identical to those used for glTexImage2D(). 90 * 91 * NOTE: this method doesn't change GL_UNPACK_ALIGNMENT, you must make 92 * sure to set it properly according to the supplied bitmap. 93 * 94 * Whether or not bitmap can have non power of two dimensions depends on 95 * the current OpenGL context. Always check glGetError() some time 96 * after calling this method, just like when using OpenGL directly. 97 * 98 * @param target 99 * @param level 100 * @param internalformat 101 * @param bitmap 102 * @param border 103 */ 104 public static void texImage2D(int target, int level, int internalformat, 105 Bitmap bitmap, int border) { 106 if (bitmap == null) { 107 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 108 } 109 if (bitmap.isRecycled()) { 110 throw new IllegalArgumentException("bitmap is recycled"); 111 } 112 if (native_texImage2D(target, level, internalformat, bitmap, -1, border)!=0) { 113 throw new IllegalArgumentException("invalid Bitmap format"); 114 } 115 } 116 117 /** 118 * A version of texImage2D() that takes an explicit type parameter 119 * as defined by the OpenGL ES specification. The actual type and 120 * internalformat of the bitmap must be compatible with the specified 121 * type and internalformat parameters. 122 * 123 * @param target 124 * @param level 125 * @param internalformat 126 * @param bitmap 127 * @param type 128 * @param border 129 */ 130 public static void texImage2D(int target, int level, int internalformat, 131 Bitmap bitmap, int type, int border) { 132 if (bitmap == null) { 133 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 134 } 135 if (bitmap.isRecycled()) { 136 throw new IllegalArgumentException("bitmap is recycled"); 137 } 138 if (native_texImage2D(target, level, internalformat, bitmap, type, border)!=0) { 139 throw new IllegalArgumentException("invalid Bitmap format"); 140 } 141 } 142 143 /** 144 * A version of texImage2D that determines the internalFormat and type 145 * automatically. 146 * 147 * @param target 148 * @param level 149 * @param bitmap 150 * @param border 151 */ 152 public static void texImage2D(int target, int level, Bitmap bitmap, 153 int border) { 154 if (bitmap == null) { 155 throw new NullPointerException("texImage2D can't be used with a null Bitmap"); 156 } 157 if (bitmap.isRecycled()) { 158 throw new IllegalArgumentException("bitmap is recycled"); 159 } 160 if (native_texImage2D(target, level, -1, bitmap, -1, border)!=0) { 161 throw new IllegalArgumentException("invalid Bitmap format"); 162 } 163 } 164 165 /** 166 * Calls glTexSubImage2D() on the current OpenGL context. If no context is 167 * current the behavior is the same as calling glTexSubImage2D() with no 168 * current context, that is, eglGetError() will return the appropriate 169 * error. 170 * Unlike glTexSubImage2D() bitmap cannot be null and will raise an exception 171 * in that case. 172 * All other parameters are identical to those used for glTexSubImage2D(). 173 * 174 * NOTE: this method doesn't change GL_UNPACK_ALIGNMENT, you must make 175 * sure to set it properly according to the supplied bitmap. 176 * 177 * Whether or not bitmap can have non power of two dimensions depends on 178 * the current OpenGL context. Always check glGetError() some time 179 * after calling this method, just like when using OpenGL directly. 180 * 181 * @param target 182 * @param level 183 * @param xoffset 184 * @param yoffset 185 * @param bitmap 186 */ 187 public static void texSubImage2D(int target, int level, int xoffset, int yoffset, 188 Bitmap bitmap) { 189 if (bitmap == null) { 190 throw new NullPointerException("texSubImage2D can't be used with a null Bitmap"); 191 } 192 if (bitmap.isRecycled()) { 193 throw new IllegalArgumentException("bitmap is recycled"); 194 } 195 int type = getType(bitmap); 196 if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, -1, type)!=0) { 197 throw new IllegalArgumentException("invalid Bitmap format"); 198 } 199 } 200 201 /** 202 * A version of texSubImage2D() that takes an explicit type parameter 203 * as defined by the OpenGL ES specification. 204 * 205 * @param target 206 * @param level 207 * @param xoffset 208 * @param yoffset 209 * @param bitmap 210 * @param type 211 */ 212 public static void texSubImage2D(int target, int level, int xoffset, int yoffset, 213 Bitmap bitmap, int format, int type) { 214 if (bitmap == null) { 215 throw new NullPointerException("texSubImage2D can't be used with a null Bitmap"); 216 } 217 if (bitmap.isRecycled()) { 218 throw new IllegalArgumentException("bitmap is recycled"); 219 } 220 if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, format, type)!=0) { 221 throw new IllegalArgumentException("invalid Bitmap format"); 222 } 223 } 224 225 native private static void nativeClassInit(); 226 227 native private static int native_getInternalFormat(Bitmap bitmap); 228 native private static int native_getType(Bitmap bitmap); 229 native private static int native_texImage2D(int target, int level, int internalformat, 230 Bitmap bitmap, int type, int border); 231 native private static int native_texSubImage2D(int target, int level, int xoffset, int yoffset, 232 Bitmap bitmap, int format, int type); 233} 234