blob: 7554a67e830744814f2c019e6a5e4beccee21907 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
2 * Copyright (C) 2023 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// #define LOG_NDEBUG 0
18#define LOG_TAG "EglProgram"
19#include "EglProgram.h"
20
21#include <array>
22#include <complex>
23
24#include "EglUtil.h"
25#include "GLES/gl.h"
26#include "GLES2/gl2.h"
27#include "GLES2/gl2ext.h"
28#include "log/log.h"
29
30namespace android {
31namespace companion {
32namespace virtualcamera {
33
34namespace {
35
36constexpr char kGlExtYuvTarget[] = "GL_EXT_YUV_target";
37
38constexpr char kIdentityVertexShader[] = R"(
39 attribute vec4 vPosition;
40 void main() {
41 gl_Position = vPosition;
42 })";
43
44constexpr char kJuliaFractalFragmentShader[] = R"(
45 precision mediump float;
46 uniform vec2 uResolution;
47 uniform vec2 uC;
48 uniform vec2 uUV;
49 const float kIter = 64.0;
50
51 vec2 imSq(vec2 n){
52 return vec2(pow(n.x,2.0)-pow(n.y,2.0), 2.0*n.x*n.y);
53 }
54
55 float julia(vec2 n, vec2 c) {
56 vec2 z = n;
57 for (float i=0.0;i<kIter; i+=1.0) {
58 z = imSq(z) + c;
59 if (length(z) > 2.0) return i/kIter;
60 }
61 return kIter;
62 }
63
64 void main() {
65 vec2 uv = vec2(gl_FragCoord.x / uResolution.x - 0.5, gl_FragCoord.y / uResolution.y - 0.5);
66 float juliaVal = julia(uv * 4.0, uC);
67 gl_FragColor = vec4( juliaVal,uUV.x,uUV.y,0.0);
68 })";
69
70constexpr char kExternalTextureVertexShader[] = R"(#version 300 es
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010071 uniform mat4 aTextureTransformMatrix; // Transform matrix given by surface texture.
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010072 in vec4 aPosition;
73 in vec2 aTextureCoord;
74 out vec2 vTextureCoord;
75 void main() {
76 gl_Position = aPosition;
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010077 vTextureCoord = (aTextureTransformMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010078 })";
79
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010080constexpr char kExternalYuvTextureFragmentShader[] = R"(#version 300 es
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010081 #extension GL_OES_EGL_image_external_essl3 : require
82 #extension GL_EXT_YUV_target : require
83 precision mediump float;
84 in vec2 vTextureCoord;
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010085 layout (yuv) out vec4 fragColor;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010086 uniform __samplerExternal2DY2YEXT uTexture;
87 void main() {
88 fragColor = texture(uTexture, vTextureCoord);
89 })";
90
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010091constexpr char kExternalRgbaTextureFragmentShader[] = R"(#version 300 es
92 #extension GL_OES_EGL_image_external : require
93 #extension GL_EXT_YUV_target : require
94 precision mediump float;
95 in vec2 vTextureCoord;
96 layout (yuv) out vec4 fragColor;
97 uniform samplerExternalOES uTexture;
98 void main() {
99 vec4 rgbaColor = texture(uTexture, vTextureCoord);
100 fragColor = vec4(rgb_2_yuv(rgbaColor.xyz, itu_601_full_range), 0.0);
101 })";
102
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100103constexpr int kCoordsPerVertex = 3;
Jan Sebechlebsky99492e32023-12-20 09:49:45 +0100104
105constexpr std::array<float, 12> kSquareCoords{
106 -1.f, -1.0f, 0.0f, // top left
107 -1.f, 1.f, 0.0f, // bottom left
108 1.0f, 1.f, 0.0f, // bottom right
109 1.0f, -1.0f, 0.0f}; // top right
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100110
111constexpr std::array<float, 8> kTextureCoords{0.0f, 1.0f, // top left
112 0.0f, 0.0f, // bottom left
113 1.0f, 0.0f, // bottom right
114 1.0f, 1.0f}; // top right
115
116constexpr std::array<uint8_t, 6> kDrawOrder{0, 1, 2, 0, 2, 3};
117
118GLuint compileShader(GLenum shaderType, const char* src) {
119 GLuint shader = glCreateShader(shaderType);
120 if (shader == 0) {
121 ALOGE("glCreateShader(shaderType=%x) error: %#x",
122 static_cast<unsigned int>(shaderType), glGetError());
123 return 0;
124 }
125
126 glShaderSource(shader, 1, &src, NULL);
127 glCompileShader(shader);
128
129 GLint compiled = 0;
130 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
131 if (!compiled) {
132 ALOGE("Compile of shader type %d failed", shaderType);
133 GLint infoLen = 0;
134 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
135 if (infoLen) {
136 char* buf = new char[infoLen];
137 if (buf) {
138 glGetShaderInfoLog(shader, infoLen, NULL, buf);
139 ALOGE("Compile log: %s", buf);
140 delete[] buf;
141 }
142 }
143 glDeleteShader(shader);
144 return 0;
145 }
146 return shader;
147}
148
149} // namespace
150
151EglProgram::~EglProgram() {
152 if (mProgram) {
153 glDeleteProgram(mProgram);
154 }
155}
156
157bool EglProgram::initialize(const char* vertexShaderSrc,
158 const char* fragmentShaderSrc) {
159 GLuint vertexShaderId = compileShader(GL_VERTEX_SHADER, vertexShaderSrc);
160 if (checkEglError("compileShader(vertex)")) {
161 return false;
162 }
163 GLuint fragmentShaderId = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSrc);
164 if (checkEglError("compileShader(fragment)")) {
165 return false;
166 }
167
168 GLuint programId = glCreateProgram();
169
170 glAttachShader(programId, vertexShaderId);
171 glAttachShader(programId, fragmentShaderId);
172 glLinkProgram(programId);
173
174 GLint linkStatus = GL_FALSE;
175 glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
176 if (linkStatus != GL_TRUE) {
177 ALOGE("glLinkProgram failed");
178 GLint bufLength = 0;
179 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &bufLength);
180 if (bufLength) {
181 char* buf = new char[bufLength];
182 if (buf) {
183 glGetProgramInfoLog(programId, bufLength, NULL, buf);
184 ALOGE("Link log: %s", buf);
185 delete[] buf;
186 }
187 }
188 glDeleteProgram(programId);
189 return false;
190 }
191
192 mProgram = programId;
193
194 mIsInitialized = true;
195 return mIsInitialized;
196}
197
198bool EglProgram::isInitialized() const {
199 return mIsInitialized;
200}
201
202EglTestPatternProgram::EglTestPatternProgram() {
203 if (initialize(kIdentityVertexShader, kJuliaFractalFragmentShader)) {
204 ALOGV("Successfully initialized EGL shaders for test pattern program.");
205 } else {
206 ALOGE("Test pattern EGL shader program initialization failed.");
207 }
208}
209
210bool EglTestPatternProgram::draw(int width, int height, int frameNumber) {
211 glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
212 checkEglError("glViewport");
213
214 // Load compiled shader.
215 glUseProgram(mProgram);
216 checkEglError("glUseProgram");
217
218 // Compute point in complex plane corresponding to fractal for this frame number.
219 float time = float(frameNumber) / 120.0f;
220 const std::complex<float> c(std::sin(time) * 0.78f, std::cos(time) * 0.78f);
221
222 // Pass uniform values to the shader.
223 int resolutionHandle = glGetUniformLocation(mProgram, "uResolution");
224 checkEglError("glGetUniformLocation -> uResolution");
225 glUniform2f(resolutionHandle, static_cast<float>(width),
226 static_cast<float>(height));
227 checkEglError("glUniform2f -> uResolution");
228
229 // Pass "C" constant value determining the Julia set to the shader.
230 int cHandle = glGetUniformLocation(mProgram, "uC");
231 glUniform2f(cHandle, c.imag(), c.real());
232
233 // Pass chroma value to the shader.
234 int uvHandle = glGetUniformLocation(mProgram, "uUV");
235 glUniform2f(uvHandle, (c.imag() + 1.f) / 2.f, (c.real() + 1.f) / 2.f);
236
237 // Pass vertex array to draw.
238 int positionHandle = glGetAttribLocation(mProgram, "vPosition");
239 glEnableVertexAttribArray(positionHandle);
240
241 // Prepare the triangle coordinate data.
242 glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
243 kSquareCoords.size(), kSquareCoords.data());
244
245 // Draw triangle strip forming a square filling the viewport.
246 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
247 kDrawOrder.data());
248 if (checkEglError("glDrawElements")) {
249 return false;
250 }
251
252 return true;
253}
254
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100255EglTextureProgram::EglTextureProgram(const TextureFormat textureFormat) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100256 if (!isGlExtensionSupported(kGlExtYuvTarget)) {
257 ALOGE(
258 "Cannot initialize external texture program due to missing "
259 "GL_EXT_YUV_target extension");
260 return;
261 }
262
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100263 const char* fragmentShaderSrc = textureFormat == TextureFormat::YUV
264 ? kExternalYuvTextureFragmentShader
265 : kExternalRgbaTextureFragmentShader;
266 if (initialize(kExternalTextureVertexShader, fragmentShaderSrc)) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100267 ALOGV("Successfully initialized EGL shaders for external texture program.");
268 } else {
269 ALOGE("External texture EGL shader program initialization failed.");
270 }
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +0100271
272 // Lookup and cache handles to uniforms & attributes.
273 mPositionHandle = glGetAttribLocation(mProgram, "aPosition");
274 mTextureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord");
275 mTransformMatrixHandle =
276 glGetUniformLocation(mProgram, "aTextureTransformMatrix");
277 mTextureHandle = glGetUniformLocation(mProgram, "uTexture");
278
279 // Pass vertex array to the shader.
280 glEnableVertexAttribArray(mPositionHandle);
281 glVertexAttribPointer(mPositionHandle, kCoordsPerVertex, GL_FLOAT, false,
282 kSquareCoords.size(), kSquareCoords.data());
283
284 // Pass texture coordinates corresponding to vertex array to the shader.
285 glEnableVertexAttribArray(mTextureCoordHandle);
286 glVertexAttribPointer(mTextureCoordHandle, 2, GL_FLOAT, false,
287 kTextureCoords.size(), kTextureCoords.data());
288}
289
290EglTextureProgram::~EglTextureProgram() {
291 if (mPositionHandle != -1) {
292 glDisableVertexAttribArray(mPositionHandle);
293 }
294 if (mTextureCoordHandle != -1) {
295 glDisableVertexAttribArray(mTextureCoordHandle);
296 }
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100297}
298
Jan Sebechlebsky99492e32023-12-20 09:49:45 +0100299bool EglTextureProgram::draw(GLuint textureId,
300 const std::array<float, 16>& transformMatrix) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100301 // Load compiled shader.
302 glUseProgram(mProgram);
303 if (checkEglError("glUseProgram")) {
304 return false;
305 }
306
Jan Sebechlebsky99492e32023-12-20 09:49:45 +0100307 // Pass transformation matrix for the texture coordinates.
Jan Sebechlebsky7a309b22023-12-27 15:25:57 +0100308 glUniformMatrix4fv(mTransformMatrixHandle, 1, /*transpose=*/GL_FALSE,
Jan Sebechlebsky99492e32023-12-20 09:49:45 +0100309 transformMatrix.data());
310
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100311 // Configure texture for the shader.
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100312 glActiveTexture(GL_TEXTURE0);
313 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId);
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +0100314 glUniform1i(mTextureHandle, 0);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100315
316 // Draw triangle strip forming a square filling the viewport.
317 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
318 kDrawOrder.data());
319 if (checkEglError("glDrawElements")) {
320 return false;
321 }
322
323 return true;
324}
325
326} // namespace virtualcamera
327} // namespace companion
328} // namespace android