Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import android.opengl.GLES20; |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 14 | |
| 15 | import java.nio.ByteBuffer; |
| 16 | import java.nio.ByteOrder; |
| 17 | import java.nio.FloatBuffer; |
| 18 | |
| 19 | /** |
| 20 | * Some OpenGL static utility functions. |
| 21 | */ |
| 22 | public class GlUtil { |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 23 | private GlUtil() {} |
| 24 | |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame^] | 25 | public static class GlOutOfMemoryException extends RuntimeException { |
| 26 | public GlOutOfMemoryException(String msg) { |
| 27 | super(msg); |
| 28 | } |
| 29 | } |
| 30 | |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 31 | // Assert that no OpenGL ES 2.0 error has been raised. |
| 32 | public static void checkNoGLES2Error(String msg) { |
| 33 | int error = GLES20.glGetError(); |
| 34 | if (error != GLES20.GL_NO_ERROR) { |
Magnus Jedvert | ecae9cd | 2019-07-05 14:33:12 +0200 | [diff] [blame^] | 35 | throw error == GLES20.GL_OUT_OF_MEMORY |
| 36 | ? new GlOutOfMemoryException(msg) |
| 37 | : new RuntimeException(msg + ": GLES20 error: " + error); |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | public static FloatBuffer createFloatBuffer(float[] coords) { |
| 42 | // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. |
| 43 | ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4); |
| 44 | bb.order(ByteOrder.nativeOrder()); |
| 45 | FloatBuffer fb = bb.asFloatBuffer(); |
| 46 | fb.put(coords); |
| 47 | fb.position(0); |
| 48 | return fb; |
| 49 | } |
Magnus Jedvert | 1a591dd | 2015-09-02 14:43:03 +0200 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Generate texture with standard parameters. |
| 53 | */ |
| 54 | public static int generateTexture(int target) { |
| 55 | final int textureArray[] = new int[1]; |
| 56 | GLES20.glGenTextures(1, textureArray, 0); |
| 57 | final int textureId = textureArray[0]; |
| 58 | GLES20.glBindTexture(target, textureId); |
| 59 | GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); |
| 60 | GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); |
| 61 | GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); |
| 62 | GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); |
| 63 | checkNoGLES2Error("generateTexture"); |
| 64 | return textureId; |
| 65 | } |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 66 | } |