Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 20 | #include <array> |
Jan Sebechlebsky | 58a7ac4 | 2024-06-03 16:41:03 +0200 | [diff] [blame] | 21 | #include <chrono> |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 22 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 23 | #include "GLES/gl.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace companion { |
| 27 | namespace virtualcamera { |
| 28 | |
| 29 | // Base class for EGL Shader programs representation. |
| 30 | class 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. |
| 46 | class EglTestPatternProgram : public EglProgram { |
| 47 | public: |
| 48 | EglTestPatternProgram(); |
| 49 | |
Jan Sebechlebsky | 58a7ac4 | 2024-06-03 16:41:03 +0200 | [diff] [blame] | 50 | bool draw(std::chrono::nanoseconds timestamp); |
Jan Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | int mPositionHandle = -1; |
| 54 | int mTextureCoordHandle = -1; |
| 55 | int mCHandle = -1; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 56 | }; |
| 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. |
| 64 | class EglTextureProgram : public EglProgram { |
| 65 | public: |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 66 | enum class TextureFormat { RGBA, YUV }; |
| 67 | |
| 68 | EglTextureProgram(TextureFormat textureFormat = TextureFormat::YUV); |
Jan Sebechlebsky | ca2d797 | 2023-12-22 13:02:51 +0100 | [diff] [blame] | 69 | virtual ~EglTextureProgram(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 70 | |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 71 | // 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 Sebechlebsky | ca2d797 | 2023-12-22 13:02:51 +0100 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | int mPositionHandle = -1; |
| 83 | int mTextureCoordHandle = -1; |
| 84 | int mTransformMatrixHandle = -1; |
| 85 | int mTextureHandle = -1; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace virtualcamera |
| 89 | } // namespace companion |
| 90 | } // namespace android |
| 91 | |
| 92 | #endif // ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H |