1 /* 2 * libjingle 3 * Copyright 2015 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 package org.webrtc; 29 30 import android.opengl.GLES20; 31 32 import java.nio.ByteBuffer; 33 import java.nio.ByteOrder; 34 import java.nio.FloatBuffer; 35 36 /** 37 * Some OpenGL static utility functions. 38 */ 39 public class GlUtil { GlUtil()40 private GlUtil() {} 41 42 // Assert that no OpenGL ES 2.0 error has been raised. checkNoGLES2Error(String msg)43 public static void checkNoGLES2Error(String msg) { 44 int error = GLES20.glGetError(); 45 if (error != GLES20.GL_NO_ERROR) { 46 throw new RuntimeException(msg + ": GLES20 error: " + error); 47 } 48 } 49 createFloatBuffer(float[] coords)50 public static FloatBuffer createFloatBuffer(float[] coords) { 51 // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. 52 ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4); 53 bb.order(ByteOrder.nativeOrder()); 54 FloatBuffer fb = bb.asFloatBuffer(); 55 fb.put(coords); 56 fb.position(0); 57 return fb; 58 } 59 60 /** 61 * Generate texture with standard parameters. 62 */ generateTexture(int target)63 public static int generateTexture(int target) { 64 final int textureArray[] = new int[1]; 65 GLES20.glGenTextures(1, textureArray, 0); 66 final int textureId = textureArray[0]; 67 GLES20.glBindTexture(target, textureId); 68 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); 69 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 70 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); 71 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); 72 checkNoGLES2Error("generateTexture"); 73 return textureId; 74 } 75 } 76