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 androidx.media.filterfw;
18 
19 import android.graphics.Bitmap;
20 import android.opengl.GLES20;
21 import android.opengl.GLUtils;
22 import android.os.Looper;
23 
24 import java.nio.ByteBuffer;
25 import java.nio.ByteOrder;
26 
27 /**
28  * TODO: Make this package-private as RenderTarget and TextureSource should suffice as public
29  * facing OpenGL utilities.
30  * @hide
31  */
32 public class GLToolbox {
33 
textureNone()34     public static int textureNone() {
35         return 0;
36     }
37 
isTexture(int texId)38     public static boolean isTexture(int texId) {
39         return GLES20.glIsTexture(texId);
40     }
41 
deleteTexture(int texId)42     public static void deleteTexture(int texId) {
43         int[] textures = new int[] { texId };
44         assertNonUiThread("glDeleteTextures");
45         GLES20.glDeleteTextures(1, textures, 0);
46         checkGlError("glDeleteTextures");
47     }
48 
deleteFbo(int fboId)49     public static void deleteFbo(int fboId) {
50         int[] fbos = new int[] { fboId };
51         assertNonUiThread("glDeleteFramebuffers");
52         GLES20.glDeleteFramebuffers(1, fbos, 0);
53         checkGlError("glDeleteFramebuffers");
54     }
55 
generateTexture()56     public static int generateTexture() {
57         int[] textures = new int[1];
58         GLES20.glGenTextures(1, textures, 0);
59         checkGlError("glGenTextures");
60         return textures[0];
61     }
62 
generateFbo()63     public static int generateFbo() {
64         int[] fbos = new int[1];
65         GLES20.glGenFramebuffers(1, fbos, 0);
66         checkGlError("glGenFramebuffers");
67         return fbos[0];
68     }
69 
readFbo(int fboId, ByteBuffer pixels, int width, int height)70     public static void readFbo(int fboId, ByteBuffer pixels, int width, int height) {
71         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
72         GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixels);
73         checkGlError("glReadPixels");
74     }
75 
readTarget(RenderTarget target, ByteBuffer pixels, int width, int height)76     public static void readTarget(RenderTarget target, ByteBuffer pixels, int width, int height) {
77         target.focus();
78         GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixels);
79         checkGlError("glReadPixels");
80     }
81 
attachedTexture(int fboId)82     public static int attachedTexture(int fboId) {
83         int[] params = new int[1];
84         GLES20.glGetFramebufferAttachmentParameteriv(
85             GLES20.GL_FRAMEBUFFER,
86             GLES20.GL_COLOR_ATTACHMENT0,
87             GLES20.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
88             params, 0);
89         checkGlError("glGetFramebufferAttachmentParameteriv");
90         return params[0];
91     }
92 
attachTextureToFbo(int texId, int fboId)93     public static void attachTextureToFbo(int texId, int fboId) {
94         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
95         GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
96                                       GLES20.GL_COLOR_ATTACHMENT0,
97                                       GLES20.GL_TEXTURE_2D,
98                                       texId,
99                                       0);
100         checkGlError("glFramebufferTexture2D");
101     }
102 
allocateTexturePixels(int texId, int target, int width, int height)103     public static void allocateTexturePixels(int texId, int target, int width, int height) {
104         setTexturePixels(texId, target, (ByteBuffer)null, width, height);
105     }
106 
setTexturePixels(int texId, int target, Bitmap bitmap)107     public static void setTexturePixels(int texId, int target, Bitmap bitmap) {
108         GLES20.glBindTexture(target, texId);
109         GLUtils.texImage2D(target, 0, bitmap, 0);
110         checkGlError("glTexImage2D");
111         setDefaultTexParams();
112     }
113 
setTexturePixels(int texId, int target, ByteBuffer pixels, int width, int height)114     public static void setTexturePixels(int texId, int target, ByteBuffer pixels,
115                                         int width, int height) {
116         GLES20.glBindTexture(target, texId);
117 
118         // For some devices, "pixels" being null causes system error.
119         if (pixels == null) {
120             pixels = ByteBuffer.allocateDirect(width * height * 4);
121         }
122         GLES20.glTexImage2D(target, 0, GLES20.GL_RGBA, width, height, 0,
123                             GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixels);
124         checkGlError("glTexImage2D");
125         setDefaultTexParams();
126     }
127 
setDefaultTexParams()128     public static void setDefaultTexParams() {
129         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
130                                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
131         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
132                                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
133         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
134                                GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
135         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
136                                GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
137         checkGlError("glTexParameteri");
138     }
139 
vboNone()140     public static int vboNone() {
141         return 0;
142     }
143 
generateVbo()144     public static int generateVbo() {
145         int[] vbos = new int[1];
146         GLES20.glGenBuffers(1, vbos, 0);
147         checkGlError("glGenBuffers");
148         return vbos[0];
149     }
150 
setVboData(int vboId, ByteBuffer data)151     public static void setVboData(int vboId, ByteBuffer data) {
152         GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
153         GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, data.remaining(), data, GLES20.GL_STATIC_DRAW);
154         checkGlError("glBufferData");
155     }
156 
setVboFloats(int vboId, float[] values)157     public static void setVboFloats(int vboId, float[] values) {
158         int len = values.length * 4;
159         ByteBuffer buffer = ByteBuffer.allocateDirect(len).order(ByteOrder.nativeOrder());
160         setVboData(vboId, buffer);
161     }
162 
isVbo(int vboId)163     public static boolean isVbo(int vboId) {
164         return GLES20.glIsBuffer(vboId);
165     }
166 
deleteVbo(int vboId)167     public static void deleteVbo(int vboId) {
168         int[] buffers = new int[] { vboId };
169         GLES20.glDeleteBuffers(1, buffers, 0);
170         checkGlError("glDeleteBuffers");
171     }
172 
checkGlError(String operation)173     public static void checkGlError(String operation) {
174         int error;
175         while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
176             throw new RuntimeException("GL Operation '" + operation + "' caused error "
177                 + Integer.toHexString(error) + "!");
178         }
179     }
180 
181     /**
182      * Make sure we are not operating in the UI thread.
183      *
184      * It is often tricky to track down bugs that happen when issuing GL commands in the UI thread.
185      * This is especially true when releasing GL resources. Often this will cause errors much later
186      * on. Therefore we make sure we do not do these dangerous operations on the UI thread.
187      */
assertNonUiThread(String operation)188     private static void assertNonUiThread(String operation) {
189         if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
190             throw new RuntimeException("Attempting to perform GL operation '" + operation
191                     + "' on UI thread!");
192         }
193     }
194 }
195