egl_object.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_OBJECT_H
  17. #define ANDROID_EGL_OBJECT_H
  18. #include <atomic>
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <string>
  22. #include <vector>
  23. #include <EGL/egl.h>
  24. #include <EGL/eglext.h>
  25. #include <system/window.h>
  26. #include <log/log.h>
  27. #include "egl_display.h"
  28. // ----------------------------------------------------------------------------
  29. namespace android {
  30. // ----------------------------------------------------------------------------
  31. class egl_display_t;
  32. class egl_object_t {
  33. egl_display_t *display;
  34. mutable std::atomic_size_t count;
  35. protected:
  36. virtual ~egl_object_t();
  37. virtual void terminate();
  38. public:
  39. explicit egl_object_t(egl_display_t* display);
  40. void destroy();
  41. inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
  42. inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
  43. inline egl_display_t* getDisplay() const { return display; }
  44. private:
  45. static bool get(egl_display_t const* display, egl_object_t* object);
  46. public:
  47. template <typename N, typename T>
  48. class LocalRef {
  49. egl_object_t* ref;
  50. LocalRef() = delete;
  51. LocalRef(const LocalRef* rhs) = delete;
  52. public:
  53. ~LocalRef();
  54. explicit LocalRef(egl_object_t* rhs);
  55. explicit LocalRef(egl_display_t const* display, T o) : ref(nullptr) {
  56. egl_object_t* native = reinterpret_cast<N*>(o);
  57. if (o && egl_object_t::get(display, native)) {
  58. ref = native;
  59. }
  60. }
  61. inline N* get() {
  62. return static_cast<N*>(ref);
  63. }
  64. void acquire() const;
  65. void release() const;
  66. void terminate();
  67. };
  68. template <typename N, typename T>
  69. friend class LocalRef;
  70. };
  71. template<typename N, typename T>
  72. egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
  73. if (ref) {
  74. ref->incRef();
  75. }
  76. }
  77. template <typename N, typename T>
  78. egl_object_t::LocalRef<N,T>::~LocalRef() {
  79. if (ref) {
  80. ref->destroy();
  81. }
  82. }
  83. template <typename N, typename T>
  84. void egl_object_t::LocalRef<N,T>::acquire() const {
  85. if (ref) {
  86. ref->incRef();
  87. }
  88. }
  89. template <typename N, typename T>
  90. void egl_object_t::LocalRef<N,T>::release() const {
  91. if (ref) {
  92. if (ref->decRef() == 1) {
  93. // shouldn't happen because this is called from LocalRef
  94. ALOGE("LocalRef::release() removed the last reference!");
  95. }
  96. }
  97. }
  98. template <typename N, typename T>
  99. void egl_object_t::LocalRef<N,T>::terminate() {
  100. if (ref) {
  101. ref->terminate();
  102. }
  103. }
  104. // ----------------------------------------------------------------------------
  105. class egl_surface_t : public egl_object_t {
  106. protected:
  107. ~egl_surface_t();
  108. void terminate() override;
  109. public:
  110. typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
  111. egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win, EGLSurface surface,
  112. EGLint colorSpace, egl_connection_t const* cnx);
  113. ANativeWindow* getNativeWindow() { return win; }
  114. ANativeWindow* getNativeWindow() const { return win; }
  115. EGLint getColorSpace() const { return colorSpace; }
  116. EGLBoolean setSmpte2086Attribute(EGLint attribute, EGLint value);
  117. EGLBoolean setCta8613Attribute(EGLint attribute, EGLint value);
  118. EGLBoolean getColorSpaceAttribute(EGLint attribute, EGLint* value) const;
  119. EGLBoolean getSmpte2086Attribute(EGLint attribute, EGLint* value) const;
  120. EGLBoolean getCta8613Attribute(EGLint attribute, EGLint* value) const;
  121. EGLBoolean getSmpte2086Metadata(android_smpte2086_metadata& smpte2086) const;
  122. EGLBoolean getCta8613Metadata(android_cta861_3_metadata& cta861_3) const;
  123. void resetSmpte2086Metadata() { egl_smpte2086_dirty = false; }
  124. void resetCta8613Metadata() { egl_cta861_3_dirty = false; }
  125. // Try to keep the order of these fields and size unchanged. It's not public API, but
  126. // it's not hard to imagine native games accessing them.
  127. EGLSurface surface;
  128. EGLConfig config;
  129. private:
  130. ANativeWindow* win;
  131. public:
  132. egl_connection_t const* cnx;
  133. private:
  134. bool connected;
  135. void disconnect();
  136. EGLint colorSpace;
  137. struct egl_xy_color {
  138. EGLint x;
  139. EGLint y;
  140. };
  141. struct egl_smpte2086_metadata {
  142. struct egl_xy_color displayPrimaryRed;
  143. struct egl_xy_color displayPrimaryGreen;
  144. struct egl_xy_color displayPrimaryBlue;
  145. struct egl_xy_color whitePoint;
  146. EGLint maxLuminance;
  147. EGLint minLuminance;
  148. };
  149. struct egl_cta861_3_metadata {
  150. EGLint maxContentLightLevel;
  151. EGLint maxFrameAverageLightLevel;
  152. };
  153. bool egl_smpte2086_dirty;
  154. bool egl_cta861_3_dirty;
  155. egl_smpte2086_metadata egl_smpte2086_metadata;
  156. egl_cta861_3_metadata egl_cta861_3_metadata;
  157. };
  158. class egl_context_t: public egl_object_t {
  159. protected:
  160. ~egl_context_t() {}
  161. public:
  162. typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
  163. egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
  164. egl_connection_t const* cnx, int version);
  165. void onLooseCurrent();
  166. void onMakeCurrent(EGLSurface draw, EGLSurface read);
  167. EGLDisplay dpy;
  168. EGLContext context;
  169. EGLConfig config;
  170. EGLSurface read;
  171. EGLSurface draw;
  172. egl_connection_t const* cnx;
  173. int version;
  174. std::string gl_extensions;
  175. std::vector<std::string> tokenized_gl_extensions;
  176. };
  177. // ----------------------------------------------------------------------------
  178. typedef egl_surface_t::Ref SurfaceRef;
  179. typedef egl_context_t::Ref ContextRef;
  180. // ----------------------------------------------------------------------------
  181. template<typename NATIVE, typename EGL>
  182. static inline NATIVE* egl_to_native_cast(EGL arg) {
  183. return reinterpret_cast<NATIVE*>(arg);
  184. }
  185. static inline
  186. egl_surface_t* get_surface(EGLSurface surface) {
  187. return egl_to_native_cast<egl_surface_t>(surface);
  188. }
  189. static inline
  190. egl_context_t* get_context(EGLContext context) {
  191. return egl_to_native_cast<egl_context_t>(context);
  192. }
  193. // ----------------------------------------------------------------------------
  194. }; // namespace android
  195. // ----------------------------------------------------------------------------
  196. #endif // ANDROID_EGL_OBJECT_H