Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / android / java / src / org / chromium / chromoting / ShaderHelper.java
blob9c05aacf518aab38ea0b74ce3d7f964be65c8df8
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chromoting;
7 import android.opengl.GLES20;
9 import org.chromium.base.Log;
11 /**
12 * Helper class for working with OpenGL shaders and programs.
14 public class ShaderHelper {
15 private static final String TAG = "cr.ShaderHelper";
17 /**
18 * Compile a shader.
20 * @param shaderType The shader type.
21 * @param shaderSource The shader source code.
22 * @return An OpenGL handle to the shader.
24 public static int compileShader(int shaderType, String shaderSource) {
25 int shaderHandle = GLES20.glCreateShader(shaderType);
27 if (shaderHandle != 0) {
28 // Pass in the shader source.
29 GLES20.glShaderSource(shaderHandle, shaderSource);
31 // Compile the shader.
32 GLES20.glCompileShader(shaderHandle);
34 // Get the compilation status.
35 int[] compileStatus = new int[1];
36 GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
38 // If the compilation failed, delete the shader.
39 if (compileStatus[0] == 0) {
40 Log.e(TAG, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shaderHandle));
41 GLES20.glDeleteShader(shaderHandle);
42 shaderHandle = 0;
46 if (shaderHandle == 0) {
47 // TODO(shichengfeng): Handle Exception gracefully.
48 throw new RuntimeException("Error creating shader.");
51 return shaderHandle;
54 /**
55 * Compile and link a program.
57 * @param vertexShaderHandle An OpenGL handle to an already-compiled vertex shader.
58 * @param fragmentShaderHandle An OpenGL handle to an already-compiled fragment shader.
59 * @param attributes Attributes that need to be bound to the program.
60 * @return An OpenGL handle to the program.
62 public static int createAndLinkProgram(int vertexShaderHandle,
63 int fragmentShaderHandle, String[] attributes) {
64 int programHandle = GLES20.glCreateProgram();
66 if (programHandle != 0) {
67 // Bind the vertex shader to the program.
68 GLES20.glAttachShader(programHandle, vertexShaderHandle);
70 // Bind the fragment shader to the program.
71 GLES20.glAttachShader(programHandle, fragmentShaderHandle);
73 // Bind attributes
74 if (attributes != null) {
75 int size = attributes.length;
76 for (int i = 0; i < size; i++) {
77 GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
81 // Link the two shaders together into a program.
82 GLES20.glLinkProgram(programHandle);
84 // Get the link status.
85 int[] linkStatus = new int[1];
86 GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
88 // If the link failed, delete the program.
89 if (linkStatus[0] == 0) {
90 Log.e(TAG, "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
91 GLES20.glDeleteProgram(programHandle);
92 programHandle = 0;
96 if (programHandle == 0) {
97 // TODO(shichengfeng): Handle Exception gracefully.
98 throw new RuntimeException("Error creating program.");
101 return programHandle;