1 /*
2  * Copyright (C) 2021 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 <optional>
20 
21 #include <GLES/gl.h>
22 #include <GLES2/gl2.h>
23 #include <GLES2/gl2ext.h>
24 #include <GLES3/gl3.h>
25 #include <GLES3/gl3ext.h>
26 
27 #include "GlesFuncs.h"
28 #include "Egl.h"
29 #include "Lib.h"
30 
31 namespace gfxstream {
32 
33 #define CHECK_GL_ERROR()                                                        \
34     do {                                                                        \
35         if (GLenum error = gles->glGetError(); error != GL_NO_ERROR) {          \
36         LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":" << __PRETTY_FUNCTION__ \
37                    << " found error: " << error;                                \
38         }                                                                       \
39     } while (0);
40 
41 class Gles {
42   public:
43     static gfxstream::expected<Gles, std::string> Load();
44     static gfxstream::expected<Gles, std::string> LoadFromEgl(Egl* egl);
45 
46     Gles(const Gles&) = delete;
47     Gles& operator=(const Gles&) = delete;
48 
49     Gles(Gles&&) = default;
50     Gles& operator=(Gles&&) = default;
51 
52     std::optional<GLuint> CreateShader(GLenum shader_type,
53                                        const std::string& shader_source);
54 
55     std::optional<GLuint> CreateProgram(const std::string& vert_shader_source,
56                                         const std::string& frag_shader_source);
57 
58     #define DECLARE_GLES_FUNCTION_MEMBER_POINTER(return_type, function_name, \
59                                                  signature, args)            \
60         return_type(*function_name) signature = nullptr;
61 
62     FOR_EACH_GLES_FUNCTION(DECLARE_GLES_FUNCTION_MEMBER_POINTER);
63 
64   private:
65     Gles() = default;
66 
67     void Init();
68 
69     Lib lib_;
70 };
71 
72 }  // namespace gfxstream
73