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 #pragma once
18 
19 #include <EGL/egl.h>
20 #include <EGL/eglext.h>
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace camera {
27 namespace provider {
28 namespace implementation {
29 namespace abc3d {
30 
31 struct CantCopyAssign {
32     CantCopyAssign() = default;
33     CantCopyAssign(const CantCopyAssign&) = delete;
34     CantCopyAssign& operator=(const CantCopyAssign&) = delete;
35 };
36 
37 struct CantCopyAssignMove : CantCopyAssign {
38     CantCopyAssignMove() = default;
39     CantCopyAssignMove(CantCopyAssignMove&&) = delete;
40     CantCopyAssign& operator=(CantCopyAssign&&) = delete;
41 };
42 
43 struct AutoImageKHR : CantCopyAssign {
44     AutoImageKHR(EGLDisplay, EGLImageKHR);
45     AutoImageKHR(AutoImageKHR&&) noexcept;
46     AutoImageKHR& operator=(AutoImageKHR&&) noexcept;
47     ~AutoImageKHR();
48 
okAutoImageKHR49     bool ok() const { return mEglImage != EGL_NO_IMAGE_KHR; }
getAutoImageKHR50     EGLImageKHR get() const { return mEglImage; }
51 
52 private:
53     EGLDisplay mEglDisplay;
54     EGLImageKHR mEglImage;
55 };
56 
57 struct EglCurrentContext : CantCopyAssign {
58     EglCurrentContext() = default;
59     EglCurrentContext(EGLDisplay);
60     EglCurrentContext(EglCurrentContext&&) noexcept;
61     EglCurrentContext& operator=(EglCurrentContext&&) noexcept;
62     ~EglCurrentContext();
63 
okEglCurrentContext64     bool ok() const { return mEglDisplay != EGL_NO_DISPLAY; }
65 
66 private:
67     EGLDisplay mEglDisplay = EGL_NO_DISPLAY;
68 };
69 
70 struct EglContext : CantCopyAssign {
71     EglContext() = default;
72     EglContext(EglContext&&) noexcept;
73     EglContext& operator=(EglContext&&) noexcept;
74     ~EglContext();
75 
76     EglCurrentContext init();
77     EglCurrentContext getCurrentContext();
getDisplayEglContext78     EGLDisplay getDisplay() const { return mEglDisplay; }
79     void clear();
80 
81 private:
82     EGLDisplay mEglDisplay = EGL_NO_DISPLAY;
83     EGLContext mEglContext = EGL_NO_CONTEXT;
84     EGLSurface mEglSurface = EGL_NO_SURFACE;
85 };
86 
87 struct AutoTexture : CantCopyAssign {
88     AutoTexture() = default;
89     AutoTexture(GLenum target);
90     AutoTexture(GLenum target,
91                 GLint internalformat,
92                 GLsizei width,
93                 GLsizei height,
94                 GLenum format,
95                 GLenum type,
96                 const void * data);
97     AutoTexture(AutoTexture&&) noexcept;
98     AutoTexture& operator=(AutoTexture&&) noexcept;
99     ~AutoTexture();
100 
okAutoTexture101     GLuint ok() const { return mTex != 0; }
getAutoTexture102     GLuint get() const { return mTex; }
103     void clear();
104 
105 private:
106     GLuint mTex = 0;
107 };
108 
109 struct AutoFrameBuffer : CantCopyAssignMove {
110     AutoFrameBuffer();
111     ~AutoFrameBuffer();
112 
okAutoFrameBuffer113     GLuint ok() const { return mFBO != 0; }
getAutoFrameBuffer114     GLuint get() const { return mFBO; }
115 
116 private:
117     GLuint mFBO = 0;
118 };
119 
120 struct AutoShader : CantCopyAssign {
121     AutoShader() = default;
122     AutoShader(AutoShader&&) noexcept;
123     AutoShader& operator=(AutoShader&&) noexcept;
124     ~AutoShader();
125 
126     GLuint compile(GLenum type, const char* text);
getAutoShader127     GLuint get() const { return mShader; }
128 
129 private:
130     GLuint mShader = 0;
131 };
132 
133 struct AutoProgram : CantCopyAssign {
134     AutoProgram() = default;
135     AutoProgram(AutoProgram&&) noexcept;
136     AutoProgram& operator=(AutoProgram&&) noexcept;
137     ~AutoProgram();
138 
139     bool link(GLuint vertexShader, GLuint fragmentShader);
140     GLint getAttribLocation(const char* name) const;
141     GLint getUniformLocation(const char* name) const;
142     void clear();
okAutoProgram143     bool ok() const { return mProgram != 0; }
getAutoProgram144     GLuint get() const { return mProgram; }
145 
146 private:
147     GLuint mProgram = 0;
148 };
149 
150 void frustum(float m44[], double left, double right,
151              double bottom, double top, double nearVal, double farVal);
152 void lookAtXyzRot(float m44[], const float eye3[], const float rot3[]);
153 void mulM44(float m44[], const float lhs44[], const float rhs44[]);
154 
155 }  // namespace abc3d
156 }  // namespace implementation
157 }  // namespace provider
158 }  // namespace camera
159 }  // namespace hardware
160 }  // namespace android
161