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