1 /*
2 * Copyright (C) 2011 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 #ifndef EGL_OS_API_H
17 #define EGL_OS_API_H
18
19 #include "base/Compiler.h"
20
21 #include <EGL/egl.h>
22
23 #include <memory>
24
25 #define PBUFFER_MAX_WIDTH 32767
26 #define PBUFFER_MAX_HEIGHT 32767
27 #define PBUFFER_MAX_PIXELS (PBUFFER_MAX_WIDTH * PBUFFER_MAX_HEIGHT)
28
29 class GlLibrary;
30
31 namespace EglOS {
32
33 // This header contains declaration used to abstract the underlying
34 // desktop GL library (or equivalent) that is being used by our EGL
35 // and GLES translation libraries.
36
37 // Use EglOS::Engine::getHostInstance() to retrieve an instance of the
38 // EglOS::Engine interface that matches the host display system.
39 //
40 // Alternate renderers (e.g. software-based Mesa) can also implement
41 // their own engine.
42
43 // Base class used to wrap various GL Surface types.
44 class Surface {
45 public:
46 typedef enum {
47 WINDOW = 0,
48 PBUFFER = 1,
49 } SurfaceType;
50
Surface(SurfaceType type)51 explicit Surface(SurfaceType type) : mType(type) {}
52
type()53 SurfaceType type() const { return mType; }
54
55 protected:
56 SurfaceType mType;
57 };
58
59 // An interface class for engine-specific implementation of a GL context.
60 class Context {
61 public:
mCoreProfile(coreProfile)62 Context(bool coreProfile = false) : mCoreProfile(coreProfile) {}
isCoreProfile()63 bool isCoreProfile() const {
64 return mCoreProfile;
65 }
66 protected:
67 ~Context() = default;
68 private:
69 bool mCoreProfile = false;
70 };
71
72 // Base class used to wrap engine-specific pixel format descriptors.
73 class PixelFormat {
74 public:
PixelFormat()75 PixelFormat() {}
76
~PixelFormat()77 virtual ~PixelFormat() {}
78
79 virtual PixelFormat* clone() = 0;
80 };
81
82 // Small structure used to describe the properties of an engine-specific
83 // config.
84 struct ConfigInfo {
85 EGLint red_size;
86 EGLint green_size;
87 EGLint blue_size;
88 EGLint alpha_size;
89 EGLenum caveat;
90 EGLint depth_size;
91 EGLint frame_buffer_level;
92 EGLint max_pbuffer_width;
93 EGLint max_pbuffer_height;
94 EGLint max_pbuffer_size;
95 EGLBoolean native_renderable;
96 EGLint renderable_type;
97 EGLint native_visual_id;
98 EGLint native_visual_type;
99 EGLint samples_per_pixel;
100 EGLint stencil_size;
101 EGLint surface_type;
102 EGLenum transparent_type;
103 EGLint trans_red_val;
104 EGLint trans_green_val;
105 EGLint trans_blue_val;
106 EGLBoolean recordable_android;
107 PixelFormat* frmt;
108 };
109
110 // A callback function type used with Display::queryConfig() to report to the
111 // caller a new host EGLConfig.
112 // |opaque| is an opaque value passed to queryConfig().
113 // All other parameters are config attributes.
114 // Note that ownership of |frmt| is transfered to the callback.
115 typedef void (AddConfigCallback)(void* opaque, const ConfigInfo* configInfo);
116
117 // Pbuffer description.
118 // |width| and |height| are its dimensions.
119 // |largest| is set to ask the largest pixek buffer (see GLX_LARGEST_PBUFFER).
120 // |format| is one of EGL_TEXTURE_RGB or EGL_TEXTURE_RGBA
121 // |target| is one of EGL_TEXTURE_2D or EGL_NO_TEXTURE.
122 // |hasMipmap| is true if the Pbuffer has mipmaps.
123 struct PbufferInfo {
124 EGLint width;
125 EGLint height;
126 EGLint largest;
127 EGLint format;
128 EGLint target;
129 EGLint hasMipmap;
130 };
131
132 enum class GlesVersion {
133 ES2 = 0,
134 ES30 = 1,
135 ES31 = 2,
136 ES32 = 3,
137 };
138
calcMaxESVersionFromCoreVersion(int coreMajor,int coreMinor)139 inline GlesVersion calcMaxESVersionFromCoreVersion(int coreMajor, int coreMinor) {
140 switch (coreMajor) {
141 case 3:
142 return coreMinor > 1 ? EglOS::GlesVersion::ES30 : EglOS::GlesVersion::ES2;
143 case 4:
144 // 4.3 core has all the entry points we need, but we want 4.5 core for
145 // ARB_ES31_compatibility to avoid shader translation (for now. TODO:
146 // translate ESSL 310 to 4.3 shaders)
147 return coreMinor > 4 ? EglOS::GlesVersion::ES31 : EglOS::GlesVersion::ES30;
148 default:
149 return EglOS::GlesVersion::ES2;
150 }
151 }
152
153 // A class to model the engine-specific implementation of a GL display
154 // connection.
155 class Display {
156 public:
157 Display() = default;
~Display()158 virtual ~Display() {}
159
160 virtual GlesVersion getMaxGlesVersion() = 0;
161
162 virtual void queryConfigs(int renderableType,
163 AddConfigCallback* addConfigFunc,
164 void* addConfigOpaque) = 0;
165
166 virtual bool isValidNativeWin(Surface* win) = 0;
167 virtual bool isValidNativeWin(EGLNativeWindowType win) = 0;
168
169 virtual bool checkWindowPixelFormatMatch(EGLNativeWindowType win,
170 const PixelFormat* pixelFormat,
171 unsigned int* width,
172 unsigned int* height) = 0;
173
174 virtual std::shared_ptr<Context> createContext(
175 EGLint profileMask,
176 const PixelFormat* pixelFormat,
177 Context* sharedContext) = 0;
178
179 virtual Surface* createPbufferSurface(
180 const PixelFormat* pixelFormat, const PbufferInfo* info) = 0;
181
182 virtual bool releasePbuffer(Surface* pb) = 0;
183
184 virtual bool makeCurrent(Surface* read,
185 Surface* draw,
186 Context* context) = 0;
187
188 virtual void swapBuffers(Surface* srfc) = 0;
189
190 DISALLOW_COPY_AND_ASSIGN(Display);
191 };
192
193 // An interface class to model a specific underlying GL graphics subsystem
194 // or engine. Use getHost() to retrieve the implementation for the current
195 // host.
196 class Engine {
197 public:
198 Engine() = default;
~Engine()199 virtual ~Engine() {}
200
201 // Return a Display instance to the default display / window.
202 virtual Display* getDefaultDisplay() = 0;
203
204 // Return to engine-specific implementation of GlLibrary.
205 virtual GlLibrary* getGlLibrary() = 0;
206
207 // Return to engine-specific implementation of eglGetProcAddress.
208 virtual void* eglGetProcAddress(const char*) = 0;
209
210 // Create a new window surface. |wnd| is a host-specific window handle
211 // (e.g. a Windows HWND). A software renderer would always return NULL
212 // here.
213 virtual Surface* createWindowSurface(PixelFormat* cfg,
214 EGLNativeWindowType wnd) = 0;
215
216 // Retrieve the implementation for the current host. This can be called
217 // multiple times, and will initialize the engine on first call.
218 static Engine* getHostInstance();
219 };
220
221 // getEgl2EglHostInstance returns a host instance that is used to mount
222 // EGL/GLES translator on top of another EGL/GLES library
223 Engine* getEgl2EglHostInstance();
224
225 } // namespace EglOS
226
227 #endif // EGL_OS_API_H
228