egl_display.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <stdint.h>
  19. #include <stddef.h>
  20. #include <condition_variable>
  21. #include <mutex>
  22. #include <string>
  23. #include <unordered_set>
  24. #include <EGL/egl.h>
  25. #include <EGL/eglext.h>
  26. #include <cutils/compiler.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. bool findExtension(const char* exts, const char* name, size_t nameLen = 0);
  36. bool needsAndroidPEglMitigation();
  37. // ----------------------------------------------------------------------------
  38. class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
  39. static egl_display_t sDisplay[NUM_DISPLAYS];
  40. EGLDisplay getDisplay(EGLNativeDisplayType display);
  41. EGLDisplay getPlatformDisplay(EGLNativeDisplayType display, const EGLAttrib* attrib_list);
  42. void loseCurrentImpl(egl_context_t * cur_c);
  43. public:
  44. enum {
  45. NOT_INITIALIZED = 0,
  46. INITIALIZED = 1,
  47. TERMINATED = 2
  48. };
  49. egl_display_t();
  50. ~egl_display_t();
  51. EGLBoolean initialize(EGLint *major, EGLint *minor);
  52. EGLBoolean terminate();
  53. // add object to this display's list
  54. void addObject(egl_object_t* object);
  55. // remove object from this display's list
  56. void removeObject(egl_object_t* object);
  57. // add reference to this object. returns true if this is a valid object.
  58. bool getObject(egl_object_t* object) const;
  59. static egl_display_t* get(EGLDisplay dpy);
  60. static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp, const EGLAttrib* attrib_list);
  61. EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
  62. EGLSurface draw, EGLSurface read, EGLContext ctx,
  63. EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
  64. static void loseCurrent(egl_context_t * cur_c);
  65. inline bool isReady() const { return (refs > 0); }
  66. inline bool isValid() const { return magic == '_dpy'; }
  67. inline bool isAlive() const { return isValid(); }
  68. char const * getVendorString() const { return mVendorString.c_str(); }
  69. char const * getVersionString() const { return mVersionString.c_str(); }
  70. char const * getClientApiString() const { return mClientApiString.c_str(); }
  71. char const * getExtensionString() const { return mExtensionString.c_str(); }
  72. bool haveExtension(const char* name, size_t nameLen = 0) const;
  73. inline uint32_t getRefsCount() const { return refs; }
  74. struct strings_t {
  75. char const * vendor;
  76. char const * version;
  77. char const * clientApi;
  78. char const * extensions;
  79. };
  80. struct DisplayImpl {
  81. DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
  82. EGLDisplay dpy;
  83. EGLint state;
  84. strings_t queryString;
  85. };
  86. private:
  87. uint32_t magic;
  88. public:
  89. DisplayImpl disp;
  90. bool finishOnSwap; // property: debug.egl.finish
  91. bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
  92. bool hasColorSpaceSupport;
  93. private:
  94. friend class egl_display_ptr;
  95. uint32_t refs;
  96. bool eglIsInitialized;
  97. mutable std::mutex lock;
  98. mutable std::mutex refLock;
  99. mutable std::condition_variable refCond;
  100. std::unordered_set<egl_object_t*> objects;
  101. std::string mVendorString;
  102. std::string mVersionString;
  103. std::string mClientApiString;
  104. std::string mExtensionString;
  105. };
  106. // ----------------------------------------------------------------------------
  107. // An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
  108. // It doesn't refcount the egl_display_t, but does ensure that the underlying
  109. // EGL implementation is "awake" (not hibernating) and ready for use as long
  110. // as the egl_display_ptr exists.
  111. class egl_display_ptr {
  112. public:
  113. explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {}
  114. // We only really need a C++11 move constructor, not a copy constructor.
  115. // A move constructor would save an enter()/leave() pair on every EGL API
  116. // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
  117. // can't use a move constructor until those are cleaned up.
  118. //
  119. // egl_display_ptr(egl_display_ptr&& other) {
  120. // mDpy = other.mDpy;
  121. // other.mDpy = NULL;
  122. // }
  123. //
  124. egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {}
  125. ~egl_display_ptr() {}
  126. const egl_display_t* operator->() const { return mDpy; }
  127. egl_display_t* operator->() { return mDpy; }
  128. const egl_display_t* get() const { return mDpy; }
  129. egl_display_t* get() { return mDpy; }
  130. operator bool() const { return mDpy != nullptr; }
  131. private:
  132. egl_display_t* mDpy;
  133. // non-assignable
  134. egl_display_ptr& operator=(const egl_display_ptr&);
  135. };
  136. // ----------------------------------------------------------------------------
  137. inline egl_display_ptr get_display(EGLDisplay dpy) {
  138. return egl_display_ptr(egl_display_t::get(dpy));
  139. }
  140. // Does not ensure EGL is unhibernated. Use with caution: calls into the
  141. // underlying EGL implementation are not safe.
  142. inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
  143. return egl_display_t::get(dpy);
  144. }
  145. // ----------------------------------------------------------------------------
  146. egl_display_ptr validate_display(EGLDisplay dpy);
  147. egl_display_ptr validate_display_connection(EGLDisplay dpy,
  148. egl_connection_t*& cnx);
  149. EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
  150. EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
  151. // ----------------------------------------------------------------------------
  152. }; // namespace android
  153. // ----------------------------------------------------------------------------
  154. #endif // ANDROID_EGL_DISPLAY_H