xref: /aosp_15_r20/cts/tests/tests/opengl/src/android/opengl/cts/RendererOneColorBufferTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2012 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 
18 package android.opengl.cts;
19 
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 import java.nio.FloatBuffer;
23 import java.nio.IntBuffer;
24 import java.nio.ShortBuffer;
25 import java.util.concurrent.CountDownLatch;
26 import javax.microedition.khronos.egl.EGLConfig;
27 import javax.microedition.khronos.opengles.GL10;
28 
29 import android.content.Context;
30 import android.opengl.GLES20;
31 import android.opengl.GLSurfaceView;
32 import android.opengl.GLU;
33 import android.util.Log;
34 
35 public class RendererOneColorBufferTest extends RendererBase {
36     private int mProgramObject;
37     private int mWidth;
38     private int mHeight;
39     private FloatBuffer mVertices;
40     private ShortBuffer mIndexBuffer;
41 
42     private static String TAG = "RendererOneColorBufferTest";
43 
44     // Our vertices.
45     private float mVerticesData[] = {
46            -0.5f,  0.5f, 0.0f,  // 0, Top Left
47            -0.5f, -0.5f, 0.0f,  // 1, Bottom Left
48             0.5f, -0.5f, 0.0f,  // 2, Bottom Right
49             0.5f,  0.5f, 0.0f,  // 3, Top Right
50     };
51 
52     private float[] mVertexColor = {
53             1.0f, 0.0f, 0.0f, 1.0f,
54             1.0f, 0.0f, 0.0f, 1.0f,
55             1.0f, 0.0f, 0.0f, 1.0f,
56             1.0f, 0.0f, 0.0f, 1.0f
57     };
58 
59     // The order we like to connect them.
60     private short[] mIndices = { 0, 1, 2, 0, 2, 3 };
61     private FloatBuffer mColor;
62 
63 
RendererOneColorBufferTest(Context context, CountDownLatch latch)64     public RendererOneColorBufferTest(Context context, CountDownLatch latch) {
65         super(latch);
66         mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4)
67                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
68         mVertices.put(mVerticesData).position(0);
69 
70         ByteBuffer ibb = ByteBuffer.allocateDirect(mIndices.length * 2);
71         ibb.order(ByteOrder.nativeOrder());
72         mIndexBuffer = ibb.asShortBuffer();
73         mIndexBuffer.put(mIndices);
74         mIndexBuffer.position(0);
75 
76         mColor = ByteBuffer.allocateDirect(mVertexColor.length*4).
77                 order(ByteOrder.nativeOrder()).asFloatBuffer();
78         mColor.put(mVertexColor).position(0);
79     }
80 
RendererOneColorBufferTest(Context context, float[] colors, CountDownLatch latch)81     public RendererOneColorBufferTest(Context context, float[] colors, CountDownLatch latch) {
82         super(latch);
83         mVertexColor = colors;
84         mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4)
85                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
86         mVertices.put(mVerticesData).position(0);
87 
88         ByteBuffer ibb = ByteBuffer.allocateDirect(mIndices.length * 2);
89         ibb.order(ByteOrder.nativeOrder());
90         mIndexBuffer = ibb.asShortBuffer();
91         mIndexBuffer.put(mIndices);
92         mIndexBuffer.position(0);
93 
94         mColor = ByteBuffer.allocateDirect(mVertexColor.length*4).
95                 order(ByteOrder.nativeOrder()).asFloatBuffer();
96         mColor.put(mVertexColor).position(0);
97     }
98 
LoadShader(int type, String shaderSrc)99     private int LoadShader(int type, String shaderSrc) {
100         int shader;
101         int[] compiled = new int[1];
102 
103         // Create the shader object
104         shader = GLES20.glCreateShader(type);
105 
106         if (shader == 0)
107             return 0;
108 
109         // Load the shader source
110         GLES20.glShaderSource(shader, shaderSrc);
111 
112         // Compile the shader
113         GLES20.glCompileShader(shader);
114 
115         // Check the compile status
116         GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
117 
118         if (compiled[0] == 0) {
119             Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
120             GLES20.glDeleteShader(shader);
121             return 0;
122         }
123         return shader;
124     }
125 
126 
onSurfaceCreated(GL10 glUnused, EGLConfig config)127     public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
128         String vShaderStr =
129               "attribute vec4 vPosition;    \n"
130             + "attribute vec4 vColor;       \n"
131             + "varying vec4 varyColor;      \n"
132             + "void main()                  \n"
133             + "{                            \n"
134             + "   gl_Position = vPosition;  \n"
135             + "   varyColor = vColor;       \n"
136             + "}                            \n";
137 
138         String fShaderStr =
139             "precision mediump float;       \n"
140             + "varying vec4 varyColor;      \n"
141             + "void main()                  \n"
142             + "{                            \n"
143             + "  gl_FragColor = varyColor;  \n"
144             + "}                            \n";
145 
146         int vertexShader;
147         int fragmentShader;
148         int programObject;
149         int[] linked = new int[1];
150 
151         // Load the vertex/fragment shaders
152         vertexShader = LoadShader(GLES20.GL_VERTEX_SHADER, vShaderStr);
153         fragmentShader = LoadShader(GLES20.GL_FRAGMENT_SHADER, fShaderStr);
154 
155         // Create the program object
156         programObject = GLES20.glCreateProgram();
157 
158         if (programObject == 0)
159             return;
160 
161         GLES20.glAttachShader(programObject, vertexShader);
162         GLES20.glAttachShader(programObject, fragmentShader);
163 
164         // Bind vPosition to attribute 0
165         GLES20.glBindAttribLocation(programObject, 0, "vPosition");
166         GLES20.glBindAttribLocation(programObject, 1, "vColor");
167 
168         // Link the program
169         GLES20.glLinkProgram(programObject);
170 
171         // Check the link status
172         GLES20.glGetProgramiv(programObject, GLES20.GL_LINK_STATUS, linked, 0);
173 
174         if (linked[0] == 0)
175         {
176             Log.e(TAG, "Error linking program:");
177             Log.e(TAG, GLES20.glGetProgramInfoLog(programObject));
178             GLES20.glDeleteProgram(programObject);
179             return;
180         }
181 
182         // Store the program object
183         mProgramObject = programObject;
184 
185         GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
186     }
187 
doOnDrawFrame(GL10 glUnused)188     public void doOnDrawFrame(GL10 glUnused)
189     {
190         // Clear the color buffer
191         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
192 
193         // Use the program object
194         GLES20.glUseProgram(mProgramObject);
195 
196         // Load the vertex data
197         GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, mVertices);
198         GLES20.glEnableVertexAttribArray(0);
199 
200         int mColorHandle = GLES20.glGetAttribLocation(mProgramObject, "vColor");
201         GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false, 0, mColor);
202         GLES20.glEnableVertexAttribArray(1);
203 
204         GLES20.glDrawElements(GLES20.GL_TRIANGLES, mIndices.length,
205                 GLES20.GL_UNSIGNED_SHORT, mIndexBuffer);
206 
207         int x = 1;
208         int y =1;
209         IntBuffer   pinbuffer   = IntBuffer.allocate(1*1*4);
210 
211         GLES20.glReadPixels(mWidth/2, mHeight/2, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
212                 pinbuffer);
213         int []      pin         = pinbuffer.array();
214         int pixel = pin[0];
215         float a = (pixel >> 24) & 0xFF;
216         float b = (pixel >> 16) & 0xFF;
217         float g = (pixel >> 8) & 0xFF;
218         float r = pixel & 0xFF;
219         Log.i(TAG, "w " + mWidth + " h " + mHeight + " rgba" + r + " " + g + " " + b + " " + a);
220         mColorOne[0] = r;
221         mColorOne[1] = g;
222         mColorOne[2] = b;
223         mColorOne[3] = a;
224     }
225 
getActualRGBA()226     public float[] getActualRGBA() {
227         return this.mColorOne;
228     }
229 
onSurfaceChanged(GL10 glUnused, int width, int height)230     public void onSurfaceChanged(GL10 glUnused, int width, int height) {
231         Log.i(TAG, "onSurfaceChanged " + width + " " + height);
232         mWidth = width;
233         mHeight = height;
234         // Set the viewport
235         GLES20.glViewport(0, 0, mWidth, mHeight);
236     }
237 }
238