egl_display.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #ifndef ANDROID_EGL_DISPLAY_H
  17. #define ANDROID_EGL_DISPLAY_H
  18. #include <ctype.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <EGL/egl.h>
  22. #include <EGL/eglext.h>
  23. #include <cutils/compiler.h>
  24. #include <utils/SortedVector.h>
  25. #include <utils/threads.h>
  26. #include <utils/String8.h>
  27. #include "egldefs.h"
  28. #include "../hooks.h"
  29. // ----------------------------------------------------------------------------
  30. namespace android {
  31. // ----------------------------------------------------------------------------
  32. class egl_object_t;
  33. class egl_context_t;
  34. struct egl_connection_t;
  35. // ----------------------------------------------------------------------------
  36. class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
  37. static egl_display_t sDisplay[NUM_DISPLAYS];
  38. EGLDisplay getDisplay(EGLNativeDisplayType display);
  39. void loseCurrentImpl(egl_context_t * cur_c);
  40. public:
  41. enum {
  42. NOT_INITIALIZED = 0,
  43. INITIALIZED = 1,
  44. TERMINATED = 2
  45. };
  46. egl_display_t();
  47. ~egl_display_t();
  48. EGLBoolean initialize(EGLint *major, EGLint *minor);
  49. EGLBoolean terminate();
  50. // add object to this display's list
  51. void addObject(egl_object_t* object);
  52. // remove object from this display's list
  53. void removeObject(egl_object_t* object);
  54. // add reference to this object. returns true if this is a valid object.
  55. bool getObject(egl_object_t* object) const;
  56. // These notifications allow the display to keep track of how many window
  57. // surfaces exist, which it uses to decide whether to hibernate the
  58. // underlying EGL implementation. They can be called by any thread without
  59. // holding a lock, but must be called via egl_display_ptr to ensure
  60. // proper hibernate/wakeup sequencing. If a surface destruction triggers
  61. // hibernation, hibernation will be delayed at least until the calling
  62. // thread's egl_display_ptr is destroyed.
  63. void onWindowSurfaceCreated() {
  64. mHibernation.incWakeCount(HibernationMachine::STRONG);
  65. }
  66. void onWindowSurfaceDestroyed() {
  67. mHibernation.decWakeCount(HibernationMachine::STRONG);
  68. }
  69. static egl_display_t* get(EGLDisplay dpy);
  70. static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
  71. EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
  72. EGLSurface draw, EGLSurface read, EGLContext ctx,
  73. EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
  74. static void loseCurrent(egl_context_t * cur_c);
  75. inline bool isReady() const { return (refs > 0); }
  76. inline bool isValid() const { return magic == '_dpy'; }
  77. inline bool isAlive() const { return isValid(); }
  78. char const * getVendorString() const { return mVendorString.string(); }
  79. char const * getVersionString() const { return mVersionString.string(); }
  80. char const * getClientApiString() const { return mClientApiString.string(); }
  81. char const * getExtensionString() const { return mExtensionString.string(); }
  82. bool haveExtension(const char* name, size_t nameLen = 0) const;
  83. inline uint32_t getRefsCount() const { return refs; }
  84. struct strings_t {
  85. char const * vendor;
  86. char const * version;
  87. char const * clientApi;
  88. char const * extensions;
  89. };
  90. struct DisplayImpl {
  91. DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
  92. EGLDisplay dpy;
  93. EGLint state;
  94. strings_t queryString;
  95. };
  96. private:
  97. uint32_t magic;
  98. public:
  99. DisplayImpl disp;
  100. bool finishOnSwap; // property: debug.egl.finish
  101. bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
  102. private:
  103. friend class egl_display_ptr;
  104. bool enter() { return mHibernation.incWakeCount(HibernationMachine::WEAK); }
  105. void leave() { return mHibernation.decWakeCount(HibernationMachine::WEAK); }
  106. uint32_t refs;
  107. bool eglIsInitialized;
  108. mutable Mutex lock, refLock;
  109. mutable Condition refCond;
  110. SortedVector<egl_object_t*> objects;
  111. String8 mVendorString;
  112. String8 mVersionString;
  113. String8 mClientApiString;
  114. String8 mExtensionString;
  115. // HibernationMachine uses its own internal mutex to protect its own data.
  116. // The owning egl_display_t's lock may be but is not required to be held
  117. // when calling HibernationMachine methods. As a result, nothing in this
  118. // class may call back up to egl_display_t directly or indirectly.
  119. class HibernationMachine {
  120. public:
  121. // STRONG refs cancel (inc) or initiate (dec) a hibernation attempt
  122. // the next time the wakecount reaches zero. WEAK refs don't affect
  123. // whether a hibernation attempt will be made. Use STRONG refs only
  124. // for infrequent/heavy changes that are likely to indicate the
  125. // EGLDisplay is entering or leaving a long-term idle state.
  126. enum WakeRefStrength {
  127. WEAK = 0,
  128. STRONG = 1,
  129. };
  130. HibernationMachine(): mWakeCount(0), mHibernating(false),
  131. mAttemptHibernation(false), mDpyValid(false),
  132. #if BOARD_ALLOW_EGL_HIBERNATION
  133. mAllowHibernation(true)
  134. #else
  135. mAllowHibernation(false)
  136. #endif
  137. {}
  138. ~HibernationMachine() {}
  139. bool incWakeCount(WakeRefStrength strenth);
  140. void decWakeCount(WakeRefStrength strenth);
  141. void setDisplayValid(bool valid);
  142. private:
  143. Mutex mLock;
  144. int32_t mWakeCount;
  145. bool mHibernating;
  146. bool mAttemptHibernation;
  147. bool mDpyValid;
  148. const bool mAllowHibernation;
  149. };
  150. HibernationMachine mHibernation;
  151. };
  152. // ----------------------------------------------------------------------------
  153. // An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
  154. // It doesn't refcount the egl_display_t, but does ensure that the underlying
  155. // EGL implementation is "awake" (not hibernating) and ready for use as long
  156. // as the egl_display_ptr exists.
  157. class egl_display_ptr {
  158. public:
  159. explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
  160. if (mDpy) {
  161. if (CC_UNLIKELY(!mDpy->enter())) {
  162. mDpy = NULL;
  163. }
  164. }
  165. }
  166. // We only really need a C++11 move constructor, not a copy constructor.
  167. // A move constructor would save an enter()/leave() pair on every EGL API
  168. // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
  169. // can't use a move constructor until those are cleaned up.
  170. //
  171. // egl_display_ptr(egl_display_ptr&& other) {
  172. // mDpy = other.mDpy;
  173. // other.mDpy = NULL;
  174. // }
  175. //
  176. egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
  177. if (mDpy) {
  178. mDpy->enter();
  179. }
  180. }
  181. ~egl_display_ptr() {
  182. if (mDpy) {
  183. mDpy->leave();
  184. }
  185. }
  186. const egl_display_t* operator->() const { return mDpy; }
  187. egl_display_t* operator->() { return mDpy; }
  188. const egl_display_t* get() const { return mDpy; }
  189. egl_display_t* get() { return mDpy; }
  190. operator bool() const { return mDpy != NULL; }
  191. private:
  192. egl_display_t* mDpy;
  193. // non-assignable
  194. egl_display_ptr& operator=(const egl_display_ptr&);
  195. };
  196. // ----------------------------------------------------------------------------
  197. inline egl_display_ptr get_display(EGLDisplay dpy) {
  198. return egl_display_ptr(egl_display_t::get(dpy));
  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) {
  203. return egl_display_t::get(dpy);
  204. }
  205. // ----------------------------------------------------------------------------
  206. egl_display_ptr validate_display(EGLDisplay dpy);
  207. egl_display_ptr validate_display_connection(EGLDisplay dpy,
  208. egl_connection_t*& cnx);
  209. EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
  210. EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
  211. // ----------------------------------------------------------------------------
  212. }; // namespace android
  213. // ----------------------------------------------------------------------------
  214. #endif // ANDROID_EGL_DISPLAY_H