blob: 510fd338c63d5b66d6f2393b4816e937f9fca7e8 [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
71 in vec4 aPosition;
72 in vec2 aTextureCoord;
73 out vec2 vTextureCoord;
74 void main() {
75 gl_Position = aPosition;
76 vTextureCoord = aTextureCoord;
77 })";
78
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010079constexpr char kExternalYuvTextureFragmentShader[] = R"(#version 300 es
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010080 #extension GL_OES_EGL_image_external_essl3 : require
81 #extension GL_EXT_YUV_target : require
82 precision mediump float;
83 in vec2 vTextureCoord;
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010084 layout (yuv) out vec4 fragColor;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010085 uniform __samplerExternal2DY2YEXT uTexture;
86 void main() {
87 fragColor = texture(uTexture, vTextureCoord);
88 })";
89
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010090constexpr char kExternalRgbaTextureFragmentShader[] = R"(#version 300 es
91 #extension GL_OES_EGL_image_external : require
92 #extension GL_EXT_YUV_target : require
93 precision mediump float;
94 in vec2 vTextureCoord;
95 layout (yuv) out vec4 fragColor;
96 uniform samplerExternalOES uTexture;
97 void main() {
98 vec4 rgbaColor = texture(uTexture, vTextureCoord);
99 fragColor = vec4(rgb_2_yuv(rgbaColor.xyz, itu_601_full_range), 0.0);
100 })";
101
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100102constexpr int kCoordsPerVertex = 3;
103constexpr std::array<float, 12> kSquareCoords{-1.f, 1.0f, 0.0f, // top left
104 -1.f, -1.f, 0.0f, // bottom left
105 1.0f, -1.f, 0.0f, // bottom right
106 1.0f, 1.0f, 0.0f}; // top right
107
108constexpr std::array<float, 8> kTextureCoords{0.0f, 1.0f, // top left
109 0.0f, 0.0f, // bottom left
110 1.0f, 0.0f, // bottom right
111 1.0f, 1.0f}; // top right
112
113constexpr std::array<uint8_t, 6> kDrawOrder{0, 1, 2, 0, 2, 3};
114
115GLuint compileShader(GLenum shaderType, const char* src) {
116 GLuint shader = glCreateShader(shaderType);
117 if (shader == 0) {
118 ALOGE("glCreateShader(shaderType=%x) error: %#x",
119 static_cast<unsigned int>(shaderType), glGetError());
120 return 0;
121 }
122
123 glShaderSource(shader, 1, &src, NULL);
124 glCompileShader(shader);
125
126 GLint compiled = 0;
127 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
128 if (!compiled) {
129 ALOGE("Compile of shader type %d failed", shaderType);
130 GLint infoLen = 0;
131 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
132 if (infoLen) {
133 char* buf = new char[infoLen];
134 if (buf) {
135 glGetShaderInfoLog(shader, infoLen, NULL, buf);
136 ALOGE("Compile log: %s", buf);
137 delete[] buf;
138 }
139 }
140 glDeleteShader(shader);
141 return 0;
142 }
143 return shader;
144}
145
146} // namespace
147
148EglProgram::~EglProgram() {
149 if (mProgram) {
150 glDeleteProgram(mProgram);
151 }
152}
153
154bool EglProgram::initialize(const char* vertexShaderSrc,
155 const char* fragmentShaderSrc) {
156 GLuint vertexShaderId = compileShader(GL_VERTEX_SHADER, vertexShaderSrc);
157 if (checkEglError("compileShader(vertex)")) {
158 return false;
159 }
160 GLuint fragmentShaderId = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSrc);
161 if (checkEglError("compileShader(fragment)")) {
162 return false;
163 }
164
165 GLuint programId = glCreateProgram();
166
167 glAttachShader(programId, vertexShaderId);
168 glAttachShader(programId, fragmentShaderId);
169 glLinkProgram(programId);
170
171 GLint linkStatus = GL_FALSE;
172 glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
173 if (linkStatus != GL_TRUE) {
174 ALOGE("glLinkProgram failed");
175 GLint bufLength = 0;
176 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &bufLength);
177 if (bufLength) {
178 char* buf = new char[bufLength];
179 if (buf) {
180 glGetProgramInfoLog(programId, bufLength, NULL, buf);
181 ALOGE("Link log: %s", buf);
182 delete[] buf;
183 }
184 }
185 glDeleteProgram(programId);
186 return false;
187 }
188
189 mProgram = programId;
190
191 mIsInitialized = true;
192 return mIsInitialized;
193}
194
195bool EglProgram::isInitialized() const {
196 return mIsInitialized;
197}
198
199EglTestPatternProgram::EglTestPatternProgram() {
200 if (initialize(kIdentityVertexShader, kJuliaFractalFragmentShader)) {
201 ALOGV("Successfully initialized EGL shaders for test pattern program.");
202 } else {
203 ALOGE("Test pattern EGL shader program initialization failed.");
204 }
205}
206
207bool EglTestPatternProgram::draw(int width, int height, int frameNumber) {
208 glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
209 checkEglError("glViewport");
210
211 // Load compiled shader.
212 glUseProgram(mProgram);
213 checkEglError("glUseProgram");
214
215 // Compute point in complex plane corresponding to fractal for this frame number.
216 float time = float(frameNumber) / 120.0f;
217 const std::complex<float> c(std::sin(time) * 0.78f, std::cos(time) * 0.78f);
218
219 // Pass uniform values to the shader.
220 int resolutionHandle = glGetUniformLocation(mProgram, "uResolution");
221 checkEglError("glGetUniformLocation -> uResolution");
222 glUniform2f(resolutionHandle, static_cast<float>(width),
223 static_cast<float>(height));
224 checkEglError("glUniform2f -> uResolution");
225
226 // Pass "C" constant value determining the Julia set to the shader.
227 int cHandle = glGetUniformLocation(mProgram, "uC");
228 glUniform2f(cHandle, c.imag(), c.real());
229
230 // Pass chroma value to the shader.
231 int uvHandle = glGetUniformLocation(mProgram, "uUV");
232 glUniform2f(uvHandle, (c.imag() + 1.f) / 2.f, (c.real() + 1.f) / 2.f);
233
234 // Pass vertex array to draw.
235 int positionHandle = glGetAttribLocation(mProgram, "vPosition");
236 glEnableVertexAttribArray(positionHandle);
237
238 // Prepare the triangle coordinate data.
239 glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
240 kSquareCoords.size(), kSquareCoords.data());
241
242 // Draw triangle strip forming a square filling the viewport.
243 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
244 kDrawOrder.data());
245 if (checkEglError("glDrawElements")) {
246 return false;
247 }
248
249 return true;
250}
251
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100252EglTextureProgram::EglTextureProgram(const TextureFormat textureFormat) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100253 if (!isGlExtensionSupported(kGlExtYuvTarget)) {
254 ALOGE(
255 "Cannot initialize external texture program due to missing "
256 "GL_EXT_YUV_target extension");
257 return;
258 }
259
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100260 const char* fragmentShaderSrc = textureFormat == TextureFormat::YUV
261 ? kExternalYuvTextureFragmentShader
262 : kExternalRgbaTextureFragmentShader;
263 if (initialize(kExternalTextureVertexShader, fragmentShaderSrc)) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100264 ALOGV("Successfully initialized EGL shaders for external texture program.");
265 } else {
266 ALOGE("External texture EGL shader program initialization failed.");
267 }
268}
269
270bool EglTextureProgram::draw(GLuint textureId) {
271 // Load compiled shader.
272 glUseProgram(mProgram);
273 if (checkEglError("glUseProgram")) {
274 return false;
275 }
276
277 // Pass vertex array to the shader.
278 int positionHandle = glGetAttribLocation(mProgram, "aPosition");
279 glEnableVertexAttribArray(positionHandle);
280 glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
281 kSquareCoords.size(), kSquareCoords.data());
282
283 // Pass texture coordinates corresponding to vertex array to the shader.
284 int textureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord");
285 glEnableVertexAttribArray(textureCoordHandle);
286 glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, false,
287 kTextureCoords.size(), kTextureCoords.data());
288
289 // Configure texture for the shader.
290 int textureHandle = glGetUniformLocation(mProgram, "uTexture");
291 glActiveTexture(GL_TEXTURE0);
292 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId);
293 glUniform1i(textureHandle, 0);
294
295 // Draw triangle strip forming a square filling the viewport.
296 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
297 kDrawOrder.data());
298 if (checkEglError("glDrawElements")) {
299 return false;
300 }
301
302 return true;
303}
304
305} // namespace virtualcamera
306} // namespace companion
307} // namespace android