Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_EGL_DISPLAY_H |
| 18 | #define ANDROID_EGL_DISPLAY_H |
| 19 | |
| 20 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 22 | #include <stddef.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 23 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 24 | #include <condition_variable> |
| 25 | #include <mutex> |
| 26 | #include <string> |
| 27 | #include <unordered_set> |
| 28 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 29 | #include <EGL/egl.h> |
| 30 | #include <EGL/eglext.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 32 | #include <cutils/compiler.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 34 | #include "egldefs.h" |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 35 | #include "../hooks.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | namespace android { |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | class egl_object_t; |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 42 | class egl_context_t; |
Bernhard Rosenkränzer | 9f42591 | 2014-11-17 21:12:15 +0100 | [diff] [blame] | 43 | struct egl_connection_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | |
Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 45 | bool findExtension(const char* exts, const char* name, size_t nameLen = 0); |
| 46 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 47 | // ---------------------------------------------------------------------------- |
| 48 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 49 | class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 50 | static egl_display_t sDisplay[NUM_DISPLAYS]; |
| 51 | EGLDisplay getDisplay(EGLNativeDisplayType display); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 52 | void loseCurrentImpl(egl_context_t * cur_c); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 53 | |
| 54 | public: |
| 55 | enum { |
| 56 | NOT_INITIALIZED = 0, |
| 57 | INITIALIZED = 1, |
| 58 | TERMINATED = 2 |
| 59 | }; |
| 60 | |
| 61 | egl_display_t(); |
| 62 | ~egl_display_t(); |
| 63 | |
| 64 | EGLBoolean initialize(EGLint *major, EGLint *minor); |
| 65 | EGLBoolean terminate(); |
| 66 | |
| 67 | // add object to this display's list |
| 68 | void addObject(egl_object_t* object); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 69 | // remove object from this display's list |
| 70 | void removeObject(egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 71 | // add reference to this object. returns true if this is a valid object. |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 72 | bool getObject(egl_object_t* object) const; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 73 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 74 | static egl_display_t* get(EGLDisplay dpy); |
| 75 | static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp); |
| 76 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 77 | EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c, |
| 78 | EGLSurface draw, EGLSurface read, EGLContext ctx, |
| 79 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx); |
| 80 | static void loseCurrent(egl_context_t * cur_c); |
| 81 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 82 | inline bool isReady() const { return (refs > 0); } |
| 83 | inline bool isValid() const { return magic == '_dpy'; } |
| 84 | inline bool isAlive() const { return isValid(); } |
| 85 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 86 | char const * getVendorString() const { return mVendorString.c_str(); } |
| 87 | char const * getVersionString() const { return mVersionString.c_str(); } |
| 88 | char const * getClientApiString() const { return mClientApiString.c_str(); } |
| 89 | char const * getExtensionString() const { return mExtensionString.c_str(); } |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 90 | |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 91 | bool haveExtension(const char* name, size_t nameLen = 0) const; |
| 92 | |
Romain Guy | 4725e2c | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 93 | inline uint32_t getRefsCount() const { return refs; } |
| 94 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 95 | struct strings_t { |
| 96 | char const * vendor; |
| 97 | char const * version; |
| 98 | char const * clientApi; |
| 99 | char const * extensions; |
| 100 | }; |
| 101 | |
| 102 | struct DisplayImpl { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 103 | DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 104 | EGLDisplay dpy; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 105 | EGLint state; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 106 | strings_t queryString; |
| 107 | }; |
| 108 | |
| 109 | private: |
| 110 | uint32_t magic; |
| 111 | |
| 112 | public: |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 113 | DisplayImpl disp; |
Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 114 | bool finishOnSwap; // property: debug.egl.finish |
| 115 | bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion |
Krzysztof Kosiński | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 116 | bool hasColorSpaceSupport; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 117 | |
| 118 | private: |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 119 | friend class egl_display_ptr; |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 120 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 121 | uint32_t refs; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 122 | bool eglIsInitialized; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 123 | mutable std::mutex lock; |
| 124 | mutable std::mutex refLock; |
| 125 | mutable std::condition_variable refCond; |
| 126 | std::unordered_set<egl_object_t*> objects; |
| 127 | std::string mVendorString; |
| 128 | std::string mVersionString; |
| 129 | std::string mClientApiString; |
| 130 | std::string mExtensionString; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 135 | // An egl_display_ptr is a kind of smart pointer for egl_display_t objects. |
| 136 | // It doesn't refcount the egl_display_t, but does ensure that the underlying |
| 137 | // EGL implementation is "awake" (not hibernating) and ready for use as long |
| 138 | // as the egl_display_ptr exists. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 139 | class egl_display_ptr { |
| 140 | public: |
Dan Willemsen | e9cbb65 | 2016-10-11 12:46:38 -0700 | [diff] [blame] | 141 | explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {} |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 142 | |
| 143 | // We only really need a C++11 move constructor, not a copy constructor. |
| 144 | // A move constructor would save an enter()/leave() pair on every EGL API |
| 145 | // call. But enabling -std=c++0x causes lots of errors elsewhere, so I |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 146 | // can't use a move constructor until those are cleaned up. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 147 | // |
| 148 | // egl_display_ptr(egl_display_ptr&& other) { |
| 149 | // mDpy = other.mDpy; |
| 150 | // other.mDpy = NULL; |
| 151 | // } |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 152 | // |
Dan Willemsen | e9cbb65 | 2016-10-11 12:46:38 -0700 | [diff] [blame] | 153 | egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {} |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 154 | |
Dan Willemsen | e9cbb65 | 2016-10-11 12:46:38 -0700 | [diff] [blame] | 155 | ~egl_display_ptr() {} |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 156 | |
| 157 | const egl_display_t* operator->() const { return mDpy; } |
| 158 | egl_display_t* operator->() { return mDpy; } |
| 159 | |
| 160 | const egl_display_t* get() const { return mDpy; } |
| 161 | egl_display_t* get() { return mDpy; } |
| 162 | |
| 163 | operator bool() const { return mDpy != NULL; } |
| 164 | |
| 165 | private: |
| 166 | egl_display_t* mDpy; |
| 167 | |
| 168 | // non-assignable |
| 169 | egl_display_ptr& operator=(const egl_display_ptr&); |
| 170 | }; |
| 171 | |
| 172 | // ---------------------------------------------------------------------------- |
| 173 | |
| 174 | inline egl_display_ptr get_display(EGLDisplay dpy) { |
| 175 | return egl_display_ptr(egl_display_t::get(dpy)); |
| 176 | } |
| 177 | |
| 178 | // Does not ensure EGL is unhibernated. Use with caution: calls into the |
| 179 | // underlying EGL implementation are not safe. |
| 180 | inline egl_display_t* get_display_nowake(EGLDisplay dpy) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 181 | return egl_display_t::get(dpy); |
| 182 | } |
| 183 | |
| 184 | // ---------------------------------------------------------------------------- |
| 185 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 186 | egl_display_ptr validate_display(EGLDisplay dpy); |
| 187 | egl_display_ptr validate_display_connection(EGLDisplay dpy, |
| 188 | egl_connection_t*& cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 189 | EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx); |
| 190 | EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface); |
| 191 | |
| 192 | // ---------------------------------------------------------------------------- |
| 193 | }; // namespace android |
| 194 | // ---------------------------------------------------------------------------- |
| 195 | |
| 196 | #endif // ANDROID_EGL_DISPLAY_H |