blob: cf93157a99de6ec3c1772b784c52082acb314807 [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();
Jan Sebechlebsky4f74d392024-06-13 14:51:54 +020049 virtual ~EglTestPatternProgram();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010050
Jan Sebechlebsky58a7ac42024-06-03 16:41:03 +020051 bool draw(std::chrono::nanoseconds timestamp);
Jan Sebechlebsky288900f2024-05-24 14:47:54 +020052
53 private:
54 int mPositionHandle = -1;
55 int mTextureCoordHandle = -1;
56 int mCHandle = -1;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010057};
58
59// Shader program to draw texture.
60//
61// Shader stretches the texture over the viewport (if the texture is not same
62// aspect ratio as viewport, it will be deformed).
63//
64// TODO(b/301023410) Add support for translation / cropping.
65class EglTextureProgram : public EglProgram {
66 public:
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010067 enum class TextureFormat { RGBA, YUV };
68
69 EglTextureProgram(TextureFormat textureFormat = TextureFormat::YUV);
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +010070 virtual ~EglTextureProgram();
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010071
Jan Sebechlebsky99492e32023-12-20 09:49:45 +010072 // Draw texture over whole viewport, applying transformMatrix to texture
73 // coordinates.
74 //
75 // Transform matrix is 4x4 matrix represented in column-major order and is
76 // applied to texture coordinates in (s,t,0,1), s,t from <0,1> range prior to
77 // sampling:
78 //
79 // textureCoord = transformMatrix * vec4(s,t,0,1).xy
80 bool draw(GLuint textureId, const std::array<float, 16>& transformMatrix);
Jan Sebechlebskyca2d7972023-12-22 13:02:51 +010081
82 private:
83 int mPositionHandle = -1;
84 int mTextureCoordHandle = -1;
85 int mTransformMatrixHandle = -1;
86 int mTextureHandle = -1;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010087};
88
89} // namespace virtualcamera
90} // namespace companion
91} // namespace android
92
93#endif // ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H