egl_object.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <ctype.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <EGL/egl.h>
  22. #include <EGL/eglext.h>
  23. #include <utils/threads.h>
  24. #include <utils/String8.h>
  25. #include <utils/Vector.h>
  26. #include <system/window.h>
  27. #include "egl_display.h"
  28. // ----------------------------------------------------------------------------
  29. namespace android {
  30. // ----------------------------------------------------------------------------
  31. struct egl_display_t;
  32. class egl_object_t {
  33. egl_display_t *display;
  34. mutable volatile int32_t count;
  35. protected:
  36. virtual ~egl_object_t();
  37. public:
  38. egl_object_t(egl_display_t* display);
  39. void destroy();
  40. inline int32_t incRef() { return android_atomic_inc(&count); }
  41. inline int32_t decRef() { return android_atomic_dec(&count); }
  42. inline egl_display_t* getDisplay() const { return display; }
  43. private:
  44. void terminate();
  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();
  51. LocalRef(const LocalRef* rhs);
  52. public:
  53. ~LocalRef();
  54. explicit LocalRef(egl_object_t* rhs);
  55. explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
  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. public:
  109. typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
  110. egl_surface_t(egl_display_t* dpy, EGLConfig config,
  111. EGLNativeWindowType win, EGLSurface surface,
  112. egl_connection_t const* cnx);
  113. EGLSurface surface;
  114. EGLConfig config;
  115. sp<ANativeWindow> win;
  116. egl_connection_t const* cnx;
  117. };
  118. class egl_context_t: public egl_object_t {
  119. protected:
  120. ~egl_context_t() {}
  121. public:
  122. typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
  123. egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
  124. egl_connection_t const* cnx, int version);
  125. void onLooseCurrent();
  126. void onMakeCurrent(EGLSurface draw, EGLSurface read);
  127. EGLDisplay dpy;
  128. EGLContext context;
  129. EGLConfig config;
  130. EGLSurface read;
  131. EGLSurface draw;
  132. egl_connection_t const* cnx;
  133. int version;
  134. String8 gl_extensions;
  135. Vector<String8> tokenized_gl_extensions;
  136. };
  137. // ----------------------------------------------------------------------------
  138. typedef egl_surface_t::Ref SurfaceRef;
  139. typedef egl_context_t::Ref ContextRef;
  140. // ----------------------------------------------------------------------------
  141. template<typename NATIVE, typename EGL>
  142. static inline NATIVE* egl_to_native_cast(EGL arg) {
  143. return reinterpret_cast<NATIVE*>(arg);
  144. }
  145. static inline
  146. egl_surface_t* get_surface(EGLSurface surface) {
  147. return egl_to_native_cast<egl_surface_t>(surface);
  148. }
  149. static inline
  150. egl_context_t* get_context(EGLContext context) {
  151. return egl_to_native_cast<egl_context_t>(context);
  152. }
  153. // ----------------------------------------------------------------------------
  154. }; // namespace android
  155. // ----------------------------------------------------------------------------
  156. #endif // ANDROID_EGL_OBJECT_H