Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2007, 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 | |
| 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> |
| 27 | #include <GLES/gl.h> |
| 28 | #include <GLES/glext.h> |
| 29 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | #include <utils/SortedVector.h> |
| 32 | #include <utils/threads.h> |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 33 | #include <utils/String8.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 34 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 35 | #include "egldefs.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 36 | #include "hooks.h" |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | namespace android { |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | class egl_object_t; |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 43 | class egl_context_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | class egl_connection_t; |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 48 | class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 49 | static egl_display_t sDisplay[NUM_DISPLAYS]; |
| 50 | EGLDisplay getDisplay(EGLNativeDisplayType display); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 51 | void loseCurrentImpl(egl_context_t * cur_c); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 52 | |
| 53 | public: |
| 54 | enum { |
| 55 | NOT_INITIALIZED = 0, |
| 56 | INITIALIZED = 1, |
| 57 | TERMINATED = 2 |
| 58 | }; |
| 59 | |
| 60 | egl_display_t(); |
| 61 | ~egl_display_t(); |
| 62 | |
| 63 | EGLBoolean initialize(EGLint *major, EGLint *minor); |
| 64 | EGLBoolean terminate(); |
| 65 | |
| 66 | // add object to this display's list |
| 67 | void addObject(egl_object_t* object); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 68 | // remove object from this display's list |
| 69 | void removeObject(egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 70 | // 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] | 71 | bool getObject(egl_object_t* object) const; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 72 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 73 | // These notifications allow the display to keep track of how many window |
| 74 | // surfaces exist, which it uses to decide whether to hibernate the |
| 75 | // underlying EGL implementation. They can be called by any thread without |
| 76 | // holding a lock, but must be called via egl_display_ptr to ensure |
| 77 | // proper hibernate/wakeup sequencing. If a surface destruction triggers |
| 78 | // hibernation, hibernation will be delayed at least until the calling |
| 79 | // thread's egl_display_ptr is destroyed. |
| 80 | void onWindowSurfaceCreated(); |
| 81 | void onWindowSurfaceDestroyed(); |
| 82 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 83 | static egl_display_t* get(EGLDisplay dpy); |
| 84 | static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp); |
| 85 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 86 | EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c, |
| 87 | EGLSurface draw, EGLSurface read, EGLContext ctx, |
| 88 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx); |
| 89 | static void loseCurrent(egl_context_t * cur_c); |
| 90 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 91 | inline bool isReady() const { return (refs > 0); } |
| 92 | inline bool isValid() const { return magic == '_dpy'; } |
| 93 | inline bool isAlive() const { return isValid(); } |
| 94 | |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 95 | char const * getVendorString() const { return mVendorString.string(); } |
| 96 | char const * getVersionString() const { return mVersionString.string(); } |
| 97 | char const * getClientApiString() const { return mClientApiString.string(); } |
| 98 | char const * getExtensionString() const { return mExtensionString.string(); } |
| 99 | |
Romain Guy | 4725e2c | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 100 | inline uint32_t getRefsCount() const { return refs; } |
| 101 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 102 | struct strings_t { |
| 103 | char const * vendor; |
| 104 | char const * version; |
| 105 | char const * clientApi; |
| 106 | char const * extensions; |
| 107 | }; |
| 108 | |
| 109 | struct DisplayImpl { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 110 | DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 111 | EGLDisplay dpy; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 112 | EGLint state; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 113 | strings_t queryString; |
| 114 | }; |
| 115 | |
| 116 | private: |
| 117 | uint32_t magic; |
| 118 | |
| 119 | public: |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 120 | DisplayImpl disp; |
Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 121 | bool finishOnSwap; // property: debug.egl.finish |
| 122 | bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 123 | |
| 124 | private: |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 125 | friend class egl_display_ptr; |
| 126 | bool enter(); |
| 127 | void leave(); |
| 128 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 129 | uint32_t refs; |
| 130 | mutable Mutex lock; |
| 131 | SortedVector<egl_object_t*> objects; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 132 | String8 mVendorString; |
| 133 | String8 mVersionString; |
| 134 | String8 mClientApiString; |
| 135 | String8 mExtensionString; |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 136 | int32_t mWakeCount; |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 137 | bool mHibernating; |
| 138 | bool mAttemptHibernation; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | // ---------------------------------------------------------------------------- |
| 142 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 143 | // An egl_display_ptr is a kind of smart pointer for egl_display_t objects. |
| 144 | // It doesn't refcount the egl_display_t, but does ensure that the underlying |
| 145 | // EGL implementation is "awake" (not hibernating) and ready for use as long |
| 146 | // as the egl_display_ptr exists. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 147 | class egl_display_ptr { |
| 148 | public: |
| 149 | explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) { |
| 150 | if (mDpy) { |
| 151 | if (CC_UNLIKELY(!mDpy->enter())) { |
| 152 | mDpy = NULL; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // We only really need a C++11 move constructor, not a copy constructor. |
| 158 | // A move constructor would save an enter()/leave() pair on every EGL API |
| 159 | // 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] | 160 | // can't use a move constructor until those are cleaned up. |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 161 | // |
| 162 | // egl_display_ptr(egl_display_ptr&& other) { |
| 163 | // mDpy = other.mDpy; |
| 164 | // other.mDpy = NULL; |
| 165 | // } |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 166 | // |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 167 | egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) { |
| 168 | if (mDpy) { |
| 169 | mDpy->enter(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | ~egl_display_ptr() { |
| 174 | if (mDpy) { |
| 175 | mDpy->leave(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | const egl_display_t* operator->() const { return mDpy; } |
| 180 | egl_display_t* operator->() { return mDpy; } |
| 181 | |
| 182 | const egl_display_t* get() const { return mDpy; } |
| 183 | egl_display_t* get() { return mDpy; } |
| 184 | |
| 185 | operator bool() const { return mDpy != NULL; } |
| 186 | |
| 187 | private: |
| 188 | egl_display_t* mDpy; |
| 189 | |
| 190 | // non-assignable |
| 191 | egl_display_ptr& operator=(const egl_display_ptr&); |
| 192 | }; |
| 193 | |
| 194 | // ---------------------------------------------------------------------------- |
| 195 | |
| 196 | inline egl_display_ptr get_display(EGLDisplay dpy) { |
| 197 | return egl_display_ptr(egl_display_t::get(dpy)); |
| 198 | } |
| 199 | |
| 200 | // Does not ensure EGL is unhibernated. Use with caution: calls into the |
| 201 | // underlying EGL implementation are not safe. |
| 202 | inline egl_display_t* get_display_nowake(EGLDisplay dpy) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 203 | return egl_display_t::get(dpy); |
| 204 | } |
| 205 | |
| 206 | // ---------------------------------------------------------------------------- |
| 207 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 208 | egl_display_ptr validate_display(EGLDisplay dpy); |
| 209 | egl_display_ptr validate_display_connection(EGLDisplay dpy, |
| 210 | egl_connection_t*& cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 211 | EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx); |
| 212 | EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface); |
| 213 | |
| 214 | // ---------------------------------------------------------------------------- |
| 215 | }; // namespace android |
| 216 | // ---------------------------------------------------------------------------- |
| 217 | |
| 218 | #endif // ANDROID_EGL_DISPLAY_H |