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