blob: a88308dfc562fd833b97b9fa16dbeddf1962a973 [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#ifndef ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H
18#define ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H
19
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010020#include <array>
Jan Sebechlebsky58a7ac42024-06-03 16:41:03 +020021#include <chrono>
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010022
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010023#include "GLES/gl.h"
24
25namespace android {
26namespace companion {
27namespace virtualcamera {
28
29// Base class for EGL Shader programs representation.
30class EglProgram {
31 public:
32 virtual ~EglProgram();
33
34 // Returns whether the EGL Program was successfully compiled and linked.
35 bool isInitialized() const;
36
37 protected:
38 // Compile & link program from the vertex & fragment shader source.
39 bool initialize(const char* vertexShaderSrc, const char* fragmentShaderSrc);
40 GLuint mProgram;
41 // Whether the EGL Program was successfully compiled and linked.
42 bool mIsInitialized = false;
43};
44
45// Shader program to draw Julia Set test pattern.
46class EglTestPatternProgram : public EglProgram {
47 public:
48 EglTestPatternProgram();
49
Jan Sebechlebsky58a7ac42024-06-03 16:41:03 +020050 bool draw(std::chrono::nanoseconds timestamp);
Jan Sebechlebsky288900f2024-05-24 14:47:54 +020051
52 private:
53 int mPositionHandle = -1;
54 int mTextureCoordHandle = -1;
55 int mCHandle = -1;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010056};
57
58// Shader program to draw texture.
59//
60// Shader stretches the texture over the viewport (if the texture is not same
61// aspect ratio as viewport, it will be deformed).
62//
63// TODO(b/301023410) Add support for translation / cropping.
64class EglTextureProgram : public EglProgram {
65 public:
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010066 enum class TextureFormat { RGBA, YUV };
67
68 EglTextureProgram(TextureFormat textureFormat = TextureFormat::YUV);
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +010069 virtual ~EglTextureProgram();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010070
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010071 // Draw texture over whole viewport, applying transformMatrix to texture
72 // coordinates.
73 //
74 // Transform matrix is 4x4 matrix represented in column-major order and is
75 // applied to texture coordinates in (s,t,0,1), s,t from <0,1> range prior to
76 // sampling:
77 //
78 // textureCoord = transformMatrix * vec4(s,t,0,1).xy
79 bool draw(GLuint textureId, const std::array<float, 16>& transformMatrix);
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +010080
81 private:
82 int mPositionHandle = -1;
83 int mTextureCoordHandle = -1;
84 int mTransformMatrixHandle = -1;
85 int mTextureHandle = -1;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010086};
87
88} // namespace virtualcamera
89} // namespace companion
90} // namespace android
91
92#endif // ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H