1 /* 2 * Copyright (C) 2019 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 com.android.systemui.glwallpaper; 18 19 import static android.opengl.GLES20.GL_FRAGMENT_SHADER; 20 import static android.opengl.GLES20.GL_VERTEX_SHADER; 21 import static android.opengl.GLES20.glAttachShader; 22 import static android.opengl.GLES20.glCompileShader; 23 import static android.opengl.GLES20.glCreateProgram; 24 import static android.opengl.GLES20.glCreateShader; 25 import static android.opengl.GLES20.glGetAttribLocation; 26 import static android.opengl.GLES20.glGetUniformLocation; 27 import static android.opengl.GLES20.glLinkProgram; 28 import static android.opengl.GLES20.glShaderSource; 29 import static android.opengl.GLES20.glUseProgram; 30 31 import android.content.Context; 32 import android.content.res.Resources; 33 import android.util.Log; 34 35 import java.io.BufferedReader; 36 import java.io.IOException; 37 import java.io.InputStreamReader; 38 39 /** 40 * This class takes charge of linking shader codes and then return a handle for OpenGL ES program. 41 */ 42 class ImageGLProgram { 43 private static final String TAG = ImageGLProgram.class.getSimpleName(); 44 45 private Context mContext; 46 private int mProgramHandle; 47 ImageGLProgram(Context context)48 ImageGLProgram(Context context) { 49 mContext = context.getApplicationContext(); 50 } 51 loadShaderProgram(int vertexId, int fragmentId)52 private int loadShaderProgram(int vertexId, int fragmentId) { 53 final String vertexSrc = getShaderResource(vertexId); 54 final String fragmentSrc = getShaderResource(fragmentId); 55 final int vertexHandle = getShaderHandle(GL_VERTEX_SHADER, vertexSrc); 56 final int fragmentHandle = getShaderHandle(GL_FRAGMENT_SHADER, fragmentSrc); 57 return getProgramHandle(vertexHandle, fragmentHandle); 58 } 59 getShaderResource(int shaderId)60 private String getShaderResource(int shaderId) { 61 Resources res = mContext.getResources(); 62 StringBuilder code = new StringBuilder(); 63 64 try (BufferedReader reader = new BufferedReader( 65 new InputStreamReader(res.openRawResource(shaderId)))) { 66 String nextLine; 67 while ((nextLine = reader.readLine()) != null) { 68 code.append(nextLine).append("\n"); 69 } 70 } catch (IOException | Resources.NotFoundException ex) { 71 Log.d(TAG, "Can not read the shader source", ex); 72 code = null; 73 } 74 75 return code == null ? "" : code.toString(); 76 } 77 getShaderHandle(int type, String src)78 private int getShaderHandle(int type, String src) { 79 final int shader = glCreateShader(type); 80 if (shader == 0) { 81 Log.d(TAG, "Create shader failed, type=" + type); 82 return 0; 83 } 84 glShaderSource(shader, src); 85 glCompileShader(shader); 86 return shader; 87 } 88 getProgramHandle(int vertexHandle, int fragmentHandle)89 private int getProgramHandle(int vertexHandle, int fragmentHandle) { 90 final int program = glCreateProgram(); 91 if (program == 0) { 92 Log.d(TAG, "Can not create OpenGL ES program"); 93 return 0; 94 } 95 96 glAttachShader(program, vertexHandle); 97 glAttachShader(program, fragmentHandle); 98 glLinkProgram(program); 99 return program; 100 } 101 useGLProgram(int vertexResId, int fragmentResId)102 boolean useGLProgram(int vertexResId, int fragmentResId) { 103 mProgramHandle = loadShaderProgram(vertexResId, fragmentResId); 104 glUseProgram(mProgramHandle); 105 return true; 106 } 107 getAttributeHandle(String name)108 int getAttributeHandle(String name) { 109 return glGetAttribLocation(mProgramHandle, name); 110 } 111 getUniformHandle(String name)112 int getUniformHandle(String name) { 113 return glGetUniformLocation(mProgramHandle, name); 114 } 115 } 116