blob: e117314d7175a0362a2256859e41dc7d1574daee [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
17#ifndef ANDROID_EGL_DISPLAY_H
18#define ANDROID_EGL_DISPLAY_H
19
20
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdint.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080022#include <stddef.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070023
Mathias Agopian65421432017-03-08 11:49:05 -080024#include <condition_variable>
25#include <mutex>
26#include <string>
27#include <unordered_set>
28
Mathias Agopian518ec112011-05-13 16:21:08 -070029#include <EGL/egl.h>
30#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070031
Jesse Hallb29e5e82012-04-04 16:53:42 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033
Mathias Agopian1cadb252011-05-23 17:26:14 -070034#include "egldefs.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070035#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070036
37// ----------------------------------------------------------------------------
38namespace android {
39// ----------------------------------------------------------------------------
40
41class egl_object_t;
Mathias Agopianfb87e542012-01-30 18:20:52 -080042class egl_context_t;
Bernhard Rosenkränzer9f425912014-11-17 21:12:15 +010043struct egl_connection_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070045bool findExtension(const char* exts, const char* name, size_t nameLen = 0);
Krzysztof Kosińskidbccd222019-05-16 14:10:25 -070046bool needsAndroidPEglMitigation();
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070047
Mathias Agopian518ec112011-05-13 16:21:08 -070048// ----------------------------------------------------------------------------
49
Jamie Gennis98c63832011-11-07 17:03:54 -080050class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
Mathias Agopian518ec112011-05-13 16:21:08 -070051 static egl_display_t sDisplay[NUM_DISPLAYS];
52 EGLDisplay getDisplay(EGLNativeDisplayType display);
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -060053 EGLDisplay getPlatformDisplay(EGLNativeDisplayType display, const EGLAttrib* attrib_list);
Mathias Agopiana4b2c042012-02-03 15:24:51 -080054 void loseCurrentImpl(egl_context_t * cur_c);
Mathias Agopian518ec112011-05-13 16:21:08 -070055
56public:
57 enum {
58 NOT_INITIALIZED = 0,
59 INITIALIZED = 1,
60 TERMINATED = 2
61 };
62
63 egl_display_t();
64 ~egl_display_t();
65
66 EGLBoolean initialize(EGLint *major, EGLint *minor);
67 EGLBoolean terminate();
68
69 // add object to this display's list
70 void addObject(egl_object_t* object);
Mathias Agopian5b287a62011-05-16 18:58:55 -070071 // remove object from this display's list
72 void removeObject(egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070073 // add reference to this object. returns true if this is a valid object.
Mathias Agopianf0480de2011-11-13 20:50:07 -080074 bool getObject(egl_object_t* object) const;
Mathias Agopian518ec112011-05-13 16:21:08 -070075
Mathias Agopian518ec112011-05-13 16:21:08 -070076 static egl_display_t* get(EGLDisplay dpy);
Courtney Goeltzenleuchter4adf75b2018-10-11 13:09:40 -060077 static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp, const EGLAttrib* attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -070078
Mathias Agopianfb87e542012-01-30 18:20:52 -080079 EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
80 EGLSurface draw, EGLSurface read, EGLContext ctx,
81 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
82 static void loseCurrent(egl_context_t * cur_c);
83
Mathias Agopian518ec112011-05-13 16:21:08 -070084 inline bool isReady() const { return (refs > 0); }
85 inline bool isValid() const { return magic == '_dpy'; }
86 inline bool isAlive() const { return isValid(); }
87
Mathias Agopian65421432017-03-08 11:49:05 -080088 char const * getVendorString() const { return mVendorString.c_str(); }
89 char const * getVersionString() const { return mVersionString.c_str(); }
90 char const * getClientApiString() const { return mClientApiString.c_str(); }
91 char const * getExtensionString() const { return mExtensionString.c_str(); }
Mathias Agopian4b9511c2011-11-13 23:52:47 -080092
Jesse Hallc2e41222013-08-08 13:40:22 -070093 bool haveExtension(const char* name, size_t nameLen = 0) const;
94
Romain Guy4725e2c2011-11-09 20:10:18 -080095 inline uint32_t getRefsCount() const { return refs; }
96
Mathias Agopian518ec112011-05-13 16:21:08 -070097 struct strings_t {
98 char const * vendor;
99 char const * version;
100 char const * clientApi;
101 char const * extensions;
102 };
103
104 struct DisplayImpl {
Mathias Agopian7773c432012-02-13 20:06:08 -0800105 DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
Mathias Agopian518ec112011-05-13 16:21:08 -0700106 EGLDisplay dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700107 EGLint state;
Mathias Agopian518ec112011-05-13 16:21:08 -0700108 strings_t queryString;
109 };
110
111private:
112 uint32_t magic;
113
114public:
Mathias Agopianada798b2012-02-13 17:09:30 -0800115 DisplayImpl disp;
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700116 bool finishOnSwap; // property: debug.egl.finish
117 bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700118 bool hasColorSpaceSupport;
Mathias Agopian518ec112011-05-13 16:21:08 -0700119
120private:
Jesse Hallb29e5e82012-04-04 16:53:42 -0700121 friend class egl_display_ptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700122
Mathias Agopianf0480de2011-11-13 20:50:07 -0800123 uint32_t refs;
Michael Lentine54466bc2015-01-27 09:01:03 -0800124 bool eglIsInitialized;
Mathias Agopian65421432017-03-08 11:49:05 -0800125 mutable std::mutex lock;
126 mutable std::mutex refLock;
127 mutable std::condition_variable refCond;
128 std::unordered_set<egl_object_t*> objects;
129 std::string mVendorString;
130 std::string mVersionString;
131 std::string mClientApiString;
132 std::string mExtensionString;
Mathias Agopian518ec112011-05-13 16:21:08 -0700133};
134
135// ----------------------------------------------------------------------------
136
Jesse Hall25838592012-04-05 15:53:28 -0700137// An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
138// It doesn't refcount the egl_display_t, but does ensure that the underlying
139// EGL implementation is "awake" (not hibernating) and ready for use as long
140// as the egl_display_ptr exists.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700141class egl_display_ptr {
142public:
Dan Willemsene9cbb652016-10-11 12:46:38 -0700143 explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700144
145 // We only really need a C++11 move constructor, not a copy constructor.
146 // A move constructor would save an enter()/leave() pair on every EGL API
147 // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
Jesse Hall25838592012-04-05 15:53:28 -0700148 // can't use a move constructor until those are cleaned up.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700149 //
150 // egl_display_ptr(egl_display_ptr&& other) {
151 // mDpy = other.mDpy;
152 // other.mDpy = NULL;
153 // }
Jesse Hall25838592012-04-05 15:53:28 -0700154 //
Dan Willemsene9cbb652016-10-11 12:46:38 -0700155 egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700156
Dan Willemsene9cbb652016-10-11 12:46:38 -0700157 ~egl_display_ptr() {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700158
159 const egl_display_t* operator->() const { return mDpy; }
160 egl_display_t* operator->() { return mDpy; }
161
162 const egl_display_t* get() const { return mDpy; }
163 egl_display_t* get() { return mDpy; }
164
Yi Kong48a6cd22018-07-18 10:07:09 -0700165 operator bool() const { return mDpy != nullptr; }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700166
167private:
168 egl_display_t* mDpy;
169
170 // non-assignable
171 egl_display_ptr& operator=(const egl_display_ptr&);
172};
173
174// ----------------------------------------------------------------------------
175
176inline egl_display_ptr get_display(EGLDisplay dpy) {
177 return egl_display_ptr(egl_display_t::get(dpy));
178}
179
180// Does not ensure EGL is unhibernated. Use with caution: calls into the
181// underlying EGL implementation are not safe.
182inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700183 return egl_display_t::get(dpy);
184}
185
186// ----------------------------------------------------------------------------
187
Jesse Hallb29e5e82012-04-04 16:53:42 -0700188egl_display_ptr validate_display(EGLDisplay dpy);
189egl_display_ptr validate_display_connection(EGLDisplay dpy,
190 egl_connection_t*& cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700191EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
192EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
193
194// ----------------------------------------------------------------------------
195}; // namespace android
196// ----------------------------------------------------------------------------
197
198#endif // ANDROID_EGL_DISPLAY_H