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