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 20 #include <array> 21 #include <chrono> 22 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 virtual ~EglTestPatternProgram(); 50 51 bool draw(std::chrono::nanoseconds timestamp); 52 53 private: 54 int mPositionHandle = -1; 55 int mTextureCoordHandle = -1; 56 int mCHandle = -1; 57 }; 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. 65 class EglTextureProgram : public EglProgram { 66 public: 67 enum class TextureFormat { RGBA, YUV }; 68 69 EglTextureProgram(TextureFormat textureFormat = TextureFormat::YUV); 70 virtual ~EglTextureProgram(); 71 72 // 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); 81 82 private: 83 int mPositionHandle = -1; 84 int mTextureCoordHandle = -1; 85 int mTransformMatrixHandle = -1; 86 int mTextureHandle = -1; 87 }; 88 89 } // namespace virtualcamera 90 } // namespace companion 91 } // namespace android 92 93 #endif // ANDROID_COMPANION_VIRTUALCAMERA_EGLPROGRAM_H 94