xref: /aosp_15_r20/external/angle/util/EGLWindow.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #ifndef UTIL_EGLWINDOW_H_
8 #define UTIL_EGLWINDOW_H_
9 
10 #include <stdint.h>
11 #include <list>
12 #include <memory>
13 #include <string>
14 
15 #include "common/Optional.h"
16 #include "common/PackedEnums.h"
17 #include "common/angleutils.h"
18 #include "util/EGLPlatformParameters.h"
19 #include "util/util_export.h"
20 #include "util/util_gl.h"
21 
22 class OSWindow;
23 
24 namespace angle
25 {
26 class Library;
27 struct PlatformMethods;
28 using GenericProc = void (*)();
29 }  // namespace angle
30 
31 struct ANGLE_UTIL_EXPORT ConfigParameters
32 {
33     ConfigParameters();
34     ~ConfigParameters();
35 
36     void reset();
37 
38     // Surface and Context parameters.
39     int redBits;
40     int greenBits;
41     int blueBits;
42     int alphaBits;
43     int depthBits;
44     int stencilBits;
45 
46     Optional<bool> webGLCompatibility;
47     Optional<bool> robustResourceInit;
48 
49     // EGLWindow-specific.
50     EGLenum componentType;
51     bool multisample;
52     bool debug;
53     bool noError;
54     Optional<bool> extensionsEnabled;
55     bool bindGeneratesResource;
56     bool clientArraysEnabled;
57     bool robustAccess;
58     bool mutableRenderBuffer;
59     EGLint samples;
60     Optional<bool> contextProgramCacheEnabled;
61     EGLenum resetStrategy;
62     EGLenum colorSpace;
63     EGLint swapInterval;
64 };
65 
66 using GLWindowContext = struct GLWindowHandleContext_T *;
67 
68 enum class GLWindowResult
69 {
70     NoError,
71     NoColorspaceSupport,
72     NoMutableRenderBufferSupport,
73     Error,
74 };
75 
76 class ANGLE_UTIL_EXPORT GLWindowBase : angle::NonCopyable
77 {
78   public:
79     static void Delete(GLWindowBase **window);
80 
81     using Image        = void *;
82     using ClientBuffer = void *;
83     using Enum         = unsigned int;
84     using Attrib       = intptr_t;
85     using AttribKHR    = khronos_int32_t;
86     using Boolean      = unsigned int;
87     using Surface      = void *;
88     using Sync         = void *;
89     using Display      = void *;
90 
91     // It should also be possible to set multisample and floating point framebuffers.
getClientMajorVersion()92     EGLint getClientMajorVersion() const { return mClientMajorVersion; }
getClientMinorVersion()93     EGLint getClientMinorVersion() const { return mClientMinorVersion; }
94 
95     virtual bool initializeGL(OSWindow *osWindow,
96                               angle::Library *glWindowingLibrary,
97                               angle::GLESDriverType driverType,
98                               const EGLPlatformParameters &platformParams,
99                               const ConfigParameters &configParams) = 0;
100 
101     virtual GLWindowResult initializeGLWithResult(OSWindow *osWindow,
102                                                   angle::Library *glWindowingLibrary,
103                                                   angle::GLESDriverType driverType,
104                                                   const EGLPlatformParameters &platformParams,
105                                                   const ConfigParameters &configParams) = 0;
106 
107     virtual bool isGLInitialized() const                        = 0;
108     virtual void swap()                                         = 0;
109     virtual void destroyGL()                                    = 0;
110     virtual bool makeCurrent()                                  = 0;
111     virtual bool hasError() const                               = 0;
112     virtual bool setSwapInterval(EGLint swapInterval)           = 0;
113     virtual angle::GenericProc getProcAddress(const char *name) = 0;
114     // EGLContext and HGLRC (WGL) are both "handles", which are implemented as pointers.
115     // Use void* here and let the underlying implementation handle interpreting the type correctly.
116     virtual GLWindowContext getCurrentContextGeneric()                  = 0;
117     virtual GLWindowContext createContextGeneric(GLWindowContext share) = 0;
118     virtual bool makeCurrentGeneric(GLWindowContext context)            = 0;
119     virtual Image createImage(GLWindowContext context,
120                               Enum target,
121                               ClientBuffer buffer,
122                               const Attrib *attrib_list)                = 0;
123     virtual Image createImageKHR(GLWindowContext context,
124                                  Enum target,
125                                  ClientBuffer buffer,
126                                  const AttribKHR *attrib_list)          = 0;
127     virtual EGLBoolean destroyImage(Image image)                        = 0;
128     virtual EGLBoolean destroyImageKHR(Image image)                     = 0;
129 
130     virtual Sync createSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list)        = 0;
131     virtual Sync createSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)        = 0;
132     virtual EGLBoolean destroySync(EGLDisplay dpy, Sync sync)                                  = 0;
133     virtual EGLBoolean destroySyncKHR(EGLDisplay dpy, Sync sync)                               = 0;
134     virtual EGLint clientWaitSync(EGLDisplay dpy, Sync sync, EGLint flags, EGLTimeKHR timeout) = 0;
135     virtual EGLint clientWaitSyncKHR(EGLDisplay dpy,
136                                      Sync sync,
137                                      EGLint flags,
138                                      EGLTimeKHR timeout)                                       = 0;
139 
140     virtual EGLint getEGLError()                                    = 0;
141     virtual Display getCurrentDisplay()                             = 0;
142     virtual Surface createPbufferSurface(const EGLint *attrib_list) = 0;
143     virtual EGLBoolean destroySurface(Surface surface)              = 0;
144 
145     virtual EGLBoolean bindTexImage(EGLSurface surface, EGLint buffer)    = 0;
146     virtual EGLBoolean releaseTexImage(EGLSurface surface, EGLint buffer) = 0;
147 
148     virtual bool makeCurrent(EGLSurface draw, EGLSurface read, EGLContext context) = 0;
149 
isMultisample()150     bool isMultisample() const { return mConfigParams.multisample; }
isDebugEnabled()151     bool isDebugEnabled() const { return mConfigParams.debug; }
152 
getPlatformMethods()153     const angle::PlatformMethods *getPlatformMethods() const { return mPlatform.platformMethods; }
154 
getPlatform()155     const EGLPlatformParameters &getPlatform() const { return mPlatform; }
getConfigParams()156     const ConfigParameters &getConfigParams() const { return mConfigParams; }
157 
isFeatureEnabled(angle::Feature feature)158     virtual bool isFeatureEnabled(angle::Feature feature) { return false; }
159 
160   protected:
161     GLWindowBase(EGLint glesMajorVersion, EGLint glesMinorVersion);
162     virtual ~GLWindowBase();
163 
164     EGLint mClientMajorVersion;
165     EGLint mClientMinorVersion;
166     EGLPlatformParameters mPlatform;
167     ConfigParameters mConfigParams;
168 };
169 
170 enum class ANGLEFeatureStatus
171 {
172     Enabled,
173     Disabled,
174     Unknown,
175 };
176 using ANGLEFeatureArray = angle::PackedEnumMap<angle::Feature, ANGLEFeatureStatus>;
177 
178 class ANGLE_UTIL_EXPORT EGLWindow : public GLWindowBase
179 {
180   public:
181     static EGLWindow *New(EGLint glesMajorVersion, EGLint glesMinorVersion);
182     static void Delete(EGLWindow **window);
183 
184     static EGLBoolean FindEGLConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *config);
185 
186     EGLConfig getConfig() const;
187     EGLDisplay getDisplay() const;
188     EGLSurface getSurface() const;
189     EGLContext getContext() const;
190 
191     bool isContextVersion(EGLint glesMajorVersion, EGLint glesMinorVersion) const;
192 
193     // Internally initializes the Display, Surface and Context.
194     bool initializeGL(OSWindow *osWindow,
195                       angle::Library *glWindowingLibrary,
196                       angle::GLESDriverType driverType,
197                       const EGLPlatformParameters &platformParams,
198                       const ConfigParameters &configParams) override;
199 
200     GLWindowResult initializeGLWithResult(OSWindow *osWindow,
201                                           angle::Library *glWindowingLibrary,
202                                           angle::GLESDriverType driverType,
203                                           const EGLPlatformParameters &platformParams,
204                                           const ConfigParameters &configParams) override;
205 
206     bool isGLInitialized() const override;
207     void swap() override;
208     void destroyGL() override;
209     bool makeCurrent() override;
210     bool hasError() const override;
211     bool setSwapInterval(EGLint swapInterval) override;
212     angle::GenericProc getProcAddress(const char *name) override;
213     // Initializes EGL resources.
214     GLWindowContext getCurrentContextGeneric() override;
215     GLWindowContext createContextGeneric(GLWindowContext share) override;
216     bool makeCurrentGeneric(GLWindowContext context) override;
217 
218     // Only initializes the Display.
219     bool initializeDisplay(OSWindow *osWindow,
220                            angle::Library *glWindowingLibrary,
221                            angle::GLESDriverType driverType,
222                            const EGLPlatformParameters &params);
223 
224     // Only initializes the Surface.
225     GLWindowResult initializeSurface(OSWindow *osWindow,
226                                      angle::Library *glWindowingLibrary,
227                                      const ConfigParameters &params);
228 
229     // Create an EGL context with this window's configuration
230     EGLContext createContext(EGLContext share, EGLint *extraAttributes);
231     // Make the EGL context current
232     bool makeCurrent(EGLContext context);
233 
234     Image createImage(GLWindowContext context,
235                       Enum target,
236                       ClientBuffer buffer,
237                       const Attrib *attrib_list) override;
238     Image createImageKHR(GLWindowContext context,
239                          Enum target,
240                          ClientBuffer buffer,
241                          const AttribKHR *attrib_list) override;
242 
243     EGLBoolean destroyImage(Image image) override;
244     EGLBoolean destroyImageKHR(Image image) override;
245 
246     Sync createSync(EGLDisplay dpy, EGLenum type, const EGLAttrib *attrib_list) override;
247     Sync createSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list) override;
248     EGLBoolean destroySync(EGLDisplay dpy, Sync sync) override;
249     EGLBoolean destroySyncKHR(EGLDisplay dpy, Sync sync) override;
250     EGLint clientWaitSync(EGLDisplay dpy, Sync sync, EGLint flags, EGLTimeKHR timeout) override;
251     EGLint clientWaitSyncKHR(EGLDisplay dpy, Sync sync, EGLint flags, EGLTimeKHR timeout) override;
252 
253     EGLint getEGLError() override;
254     Display getCurrentDisplay() override;
255     Surface createPbufferSurface(const EGLint *attrib_list) override;
256     EGLBoolean destroySurface(Surface surface) override;
257 
258     EGLBoolean bindTexImage(EGLSurface surface, EGLint buffer) override;
259     EGLBoolean releaseTexImage(EGLSurface surface, EGLint buffer) override;
260     bool makeCurrent(EGLSurface draw, EGLSurface read, EGLContext context) override;
261 
262     // Only initializes the Context.
263     bool initializeContext();
264 
265     void destroySurface();
266     void destroyContext();
267 
isDisplayInitialized()268     bool isDisplayInitialized() const { return mDisplay != EGL_NO_DISPLAY; }
269 
270     // Get the status of features and cache them in mFeatures.
271     void queryFeatures();
272     // Return whether a feature is enabled.  Features that don't exist in the backend have Unknown
273     // status, and are considered disabled for the purposes of this function.
274     bool isFeatureEnabled(angle::Feature feature) override;
275 
276   private:
277     EGLWindow(EGLint glesMajorVersion, EGLint glesMinorVersion);
278     ~EGLWindow() override;
279 
280     EGLConfig mConfig;
281     EGLDisplay mDisplay;
282     EGLSurface mSurface;
283     EGLContext mContext;
284 
285     EGLint mEGLMajorVersion;
286     EGLint mEGLMinorVersion;
287 
288     ANGLEFeatureArray mFeatures;
289 };
290 
291 #endif  // UTIL_EGLWINDOW_H_
292