GLUtils.java revision 5f89f510f327228d4ba2261aff7e8faa7d3715aa
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 (bitmap.isRecycled()) {
146            throw new IllegalArgumentException("bitmap is recycled");
147        }
148        if (native_texImage2D(target, level, -1, bitmap, -1, border)!=0) {
149            throw new IllegalArgumentException("invalid Bitmap format");
150        }
151    }
152
153    /**
154     * Calls glTexSubImage2D() on the current OpenGL context. If no context is
155     * current the behavior is the same as calling glTexSubImage2D() with  no
156     * current context, that is, eglGetError() will return the appropriate
157     * error.
158     * Unlike glTexSubImage2D() bitmap cannot be null and will raise an exception
159     * in that case.
160     * All other parameters are identical to those used for glTexSubImage2D().
161     *
162     * NOTE: this method doesn't change GL_UNPACK_ALIGNMENT, you must make
163     * sure to set it properly according to the supplied bitmap.
164     *
165     * Whether or not bitmap can have non power of two dimensions depends on
166     * the current OpenGL context. Always check glGetError() some time
167     * after calling this method, just like when using OpenGL directly.
168     *
169     * @param target
170     * @param level
171     * @param xoffset
172     * @param yoffset
173     * @param bitmap
174     */
175    public static void texSubImage2D(int target, int level, int xoffset, int yoffset,
176            Bitmap bitmap) {
177        if (bitmap == null) {
178            throw new NullPointerException("texSubImage2D can't be used with a null Bitmap");
179        }
180        int type = getType(bitmap);
181        if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, -1, type)!=0) {
182            throw new IllegalArgumentException("invalid Bitmap format");
183        }
184    }
185
186    /**
187     * A version of texSubImage2D() that takes an explicit type parameter
188     * as defined by the OpenGL ES specification.
189     *
190     * @param target
191     * @param level
192     * @param xoffset
193     * @param yoffset
194     * @param bitmap
195     * @param type
196     */
197    public static void texSubImage2D(int target, int level, int xoffset, int yoffset,
198            Bitmap bitmap, int format, int type) {
199        if (bitmap == null) {
200            throw new NullPointerException("texSubImage2D can't be used with a null Bitmap");
201        }
202        if (native_texSubImage2D(target, level, xoffset, yoffset, bitmap, format, type)!=0) {
203            throw new IllegalArgumentException("invalid Bitmap format");
204        }
205    }
206
207    native private static void nativeClassInit();
208
209    native private static int native_getInternalFormat(Bitmap bitmap);
210    native private static int native_getType(Bitmap bitmap);
211    native private static int native_texImage2D(int target, int level, int internalformat,
212            Bitmap bitmap, int type, int border);
213    native private static int native_texSubImage2D(int target, int level, int xoffset, int yoffset,
214            Bitmap bitmap, int format, int type);
215}
216