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 | |
| 21 | #include <ctype.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include <EGL/egl.h> |
| 26 | #include <EGL/eglext.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 27 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 28 | #include <cutils/compiler.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 29 | #include <utils/SortedVector.h> |
| 30 | #include <utils/threads.h> |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 31 | #include <utils/String8.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 33 | #include "egldefs.h" |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 34 | #include "../hooks.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | namespace android { |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | class egl_object_t; |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 41 | class egl_context_t; |
Bernhard Rosenkränzer | 9f42591 | 2014-11-17 21:12:15 +0100 | [diff] [blame] | 42 | struct egl_connection_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 46 | class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 47 | static egl_display_t sDisplay[NUM_DISPLAYS]; |
| 48 | EGLDisplay getDisplay(EGLNativeDisplayType display); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 49 | void loseCurrentImpl(egl_context_t * cur_c); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | enum { |
| 53 | NOT_INITIALIZED = 0, |
| 54 | INITIALIZED = 1, |
| 55 | TERMINATED = 2 |
| 56 | }; |
| 57 | |
| 58 | egl_display_t(); |
| 59 | ~egl_display_t(); |
| 60 | |
| 61 | EGLBoolean initialize(EGLint *major, EGLint *minor); |
| 62 | EGLBoolean terminate(); |
| 63 | |
| 64 | // add object to this display's list |
| 65 | void addObject(egl_object_t* object); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 66 | // remove object from this display's list |
| 67 | void removeObject(egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 68 | // 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] | 69 | bool getObject(egl_object_t* object) const; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 70 | |
Pablo Ceballos | 541de49 | 2016-04-09 15:56:12 -0700 | [diff] [blame] | 71 | // add context to this display's list |
| 72 | void addContext(egl_context_t* context); |
| 73 | // remove context from this display's list |
| 74 | void removeContext(egl_context_t* context); |
| 75 | // search for surface on all contexts and remove the references |
| 76 | void removeSurface(EGLSurface surface) const; |
| 77 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 78 | // These notifications allow the display to keep track of how many window |
| 79 | // surfaces exist, which it uses to decide whether to hibernate the |
| 80 | // underlying EGL implementation. They can be called by any thread without |
| 81 | // holding a lock, but must be called via egl_display_ptr to ensure |
| 82 | // proper hibernate/wakeup sequencing. If a surface destruction triggers |
| 83 | // hibernation, hibernation will be delayed at least until the calling |
| 84 | // thread's egl_display_ptr is destroyed. |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 85 | void onWindowSurfaceCreated() { |
| 86 | mHibernation.incWakeCount(HibernationMachine::STRONG); |
| 87 | } |
| 88 | void onWindowSurfaceDestroyed() { |
| 89 | mHibernation.decWakeCount(HibernationMachine::STRONG); |
| 90 | } |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 91 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 92 | static egl_display_t* get(EGLDisplay dpy); |
| 93 | static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp); |
| 94 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 95 | EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c, |
| 96 | EGLSurface draw, EGLSurface read, EGLContext ctx, |
| 97 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx); |
| 98 | static void loseCurrent(egl_context_t * cur_c); |
| 99 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 100 | inline bool isReady() const { return (refs > 0); } |
| 101 | inline bool isValid() const { return magic == '_dpy'; } |
| 102 | inline bool isAlive() const { return isValid(); } |
| 103 | |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 104 | char const * getVendorString() const { return mVendorString.string(); } |
| 105 | char const * getVersionString() const { return mVersionString.string(); } |
| 106 | char const * getClientApiString() const { return mClientApiString.string(); } |
| 107 | char const * getExtensionString() const { return mExtensionString.string(); } |
| 108 | |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 109 | bool haveExtension(const char* name, size_t nameLen = 0) const; |
| 110 | |
Romain Guy | 4725e2c | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 111 | inline uint32_t getRefsCount() const { return refs; } |
| 112 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 113 | struct strings_t { |
| 114 | char const * vendor; |
| 115 | char const * version; |
| 116 | char const * clientApi; |
| 117 | char const * extensions; |
| 118 | }; |
| 119 | |
| 120 | struct DisplayImpl { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 121 | DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 122 | EGLDisplay dpy; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 123 | EGLint state; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 124 | strings_t queryString; |
| 125 | }; |
| 126 | |
| 127 | private: |
| 128 | uint32_t magic; |
| 129 | |
| 130 | public: |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 131 | DisplayImpl disp; |
Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 132 | bool finishOnSwap; // property: debug.egl.finish |
| 133 | bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 134 | |
| 135 | private: |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 136 | friend class egl_display_ptr; |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 137 | bool enter() { return mHibernation.incWakeCount(HibernationMachine::WEAK); } |
| 138 | void leave() { return mHibernation.decWakeCount(HibernationMachine::WEAK); } |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 139 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 140 | uint32_t refs; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 141 | bool eglIsInitialized; |
| 142 | mutable Mutex lock, refLock; |
| 143 | mutable Condition refCond; |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 144 | SortedVector<egl_object_t*> objects; |
Pablo Ceballos | 541de49 | 2016-04-09 15:56:12 -0700 | [diff] [blame] | 145 | SortedVector<egl_context_t*> contexts; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 146 | String8 mVendorString; |
| 147 | String8 mVersionString; |
| 148 | String8 mClientApiString; |
| 149 | String8 mExtensionString; |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 150 | |
| 151 | // HibernationMachine uses its own internal mutex to protect its own data. |
| 152 | // The owning egl_display_t's lock may be but is not required to be held |
| 153 | // when calling HibernationMachine methods. As a result, nothing in this |
| 154 | // class may call back up to egl_display_t directly or indirectly. |
| 155 | class HibernationMachine { |
| 156 | public: |
| 157 | // STRONG refs cancel (inc) or initiate (dec) a hibernation attempt |
| 158 | // the next time the wakecount reaches zero. WEAK refs don't affect |
| 159 | // whether a hibernation attempt will be made. Use STRONG refs only |
| 160 | // for infrequent/heavy changes that are likely to indicate the |
| 161 | // EGLDisplay is entering or leaving a long-term idle state. |
| 162 | enum WakeRefStrength { |
| 163 | WEAK = 0, |
| 164 | STRONG = 1, |
| 165 | }; |
| 166 | |
| 167 | HibernationMachine(): mWakeCount(0), mHibernating(false), |
Jesse Hall | 201f3b2 | 2012-05-04 14:52:40 -0700 | [diff] [blame] | 168 | mAttemptHibernation(false), mDpyValid(false), |
| 169 | #if BOARD_ALLOW_EGL_HIBERNATION |
| 170 | mAllowHibernation(true) |
| 171 | #else |
| 172 | mAllowHibernation(false) |
| 173 | #endif |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 174 | {} |
| 175 | ~HibernationMachine() {} |
| 176 | |
| 177 | bool incWakeCount(WakeRefStrength strenth); |
| 178 | void decWakeCount(WakeRefStrength strenth); |
| 179 | |
| 180 | void setDisplayValid(bool valid); |
| 181 | |
| 182 | private: |
Jesse Hall | 201f3b2 | 2012-05-04 14:52:40 -0700 | [diff] [blame] | 183 | Mutex mLock; |
| 184 | int32_t mWakeCount; |
| 185 | bool mHibernating; |
| 186 | bool mAttemptHibernation; |
| 187 | bool mDpyValid; |
| 188 | const bool mAllowHibernation; |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 189 | }; |
| 190 | HibernationMachine mHibernation; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 195 | // An egl_display_ptr is a kind of smart pointer for egl_display_t objects. |
| 196 | // It doesn't refcount the egl_display_t, but does ensure that the underlying |
| 197 | // EGL implementation is "awake" (not hibernating) and ready for use as long |
| 198 | // as the egl_display_ptr exists. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 199 | class egl_display_ptr { |
| 200 | public: |
| 201 | explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) { |
| 202 | if (mDpy) { |
| 203 | if (CC_UNLIKELY(!mDpy->enter())) { |
| 204 | mDpy = NULL; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // We only really need a C++11 move constructor, not a copy constructor. |
| 210 | // A move constructor would save an enter()/leave() pair on every EGL API |
| 211 | // 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] | 212 | // can't use a move constructor until those are cleaned up. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 213 | // |
| 214 | // egl_display_ptr(egl_display_ptr&& other) { |
| 215 | // mDpy = other.mDpy; |
| 216 | // other.mDpy = NULL; |
| 217 | // } |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 218 | // |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 219 | egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) { |
| 220 | if (mDpy) { |
| 221 | mDpy->enter(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | ~egl_display_ptr() { |
| 226 | if (mDpy) { |
| 227 | mDpy->leave(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | const egl_display_t* operator->() const { return mDpy; } |
| 232 | egl_display_t* operator->() { return mDpy; } |
| 233 | |
| 234 | const egl_display_t* get() const { return mDpy; } |
| 235 | egl_display_t* get() { return mDpy; } |
| 236 | |
| 237 | operator bool() const { return mDpy != NULL; } |
| 238 | |
| 239 | private: |
| 240 | egl_display_t* mDpy; |
| 241 | |
| 242 | // non-assignable |
| 243 | egl_display_ptr& operator=(const egl_display_ptr&); |
| 244 | }; |
| 245 | |
| 246 | // ---------------------------------------------------------------------------- |
| 247 | |
| 248 | inline egl_display_ptr get_display(EGLDisplay dpy) { |
| 249 | return egl_display_ptr(egl_display_t::get(dpy)); |
| 250 | } |
| 251 | |
| 252 | // Does not ensure EGL is unhibernated. Use with caution: calls into the |
| 253 | // underlying EGL implementation are not safe. |
| 254 | inline egl_display_t* get_display_nowake(EGLDisplay dpy) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 255 | return egl_display_t::get(dpy); |
| 256 | } |
| 257 | |
| 258 | // ---------------------------------------------------------------------------- |
| 259 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 260 | egl_display_ptr validate_display(EGLDisplay dpy); |
| 261 | egl_display_ptr validate_display_connection(EGLDisplay dpy, |
| 262 | egl_connection_t*& cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 263 | EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx); |
| 264 | EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface); |
| 265 | |
| 266 | // ---------------------------------------------------------------------------- |
| 267 | }; // namespace android |
| 268 | // ---------------------------------------------------------------------------- |
| 269 | |
| 270 | #endif // ANDROID_EGL_DISPLAY_H |