Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 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 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_EGL_OBJECT_H |
| 18 | #define ANDROID_EGL_OBJECT_H |
| 19 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame^] | 21 | #include <stddef.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 22 | |
| 23 | #include <EGL/egl.h> |
| 24 | #include <EGL/eglext.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 25 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame^] | 26 | #include <utils/StrongPointer.h> |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 29 | |
| 30 | #include <system/window.h> |
| 31 | |
| 32 | #include "egl_display.h" |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 38 | class egl_display_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 39 | |
| 40 | class egl_object_t { |
Pablo Ceballos | 1a5c4de | 2016-04-25 20:40:08 +0000 | [diff] [blame] | 41 | egl_display_t *display; |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 42 | mutable std::atomic_size_t count; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 43 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 44 | protected: |
| 45 | virtual ~egl_object_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 46 | virtual void terminate(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 47 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 48 | public: |
Chih-Hung Hsieh | e8761d6 | 2016-09-01 11:26:34 -0700 | [diff] [blame] | 49 | explicit egl_object_t(egl_display_t* display); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 50 | void destroy(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 51 | |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 52 | inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); } |
| 53 | inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); } |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 54 | inline egl_display_t* getDisplay() const { return display; } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 55 | |
| 56 | private: |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 57 | static bool get(egl_display_t const* display, egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 58 | |
| 59 | public: |
| 60 | template <typename N, typename T> |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 61 | class LocalRef { |
| 62 | egl_object_t* ref; |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame^] | 63 | LocalRef() = delete; |
| 64 | LocalRef(const LocalRef* rhs) = delete; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 65 | public: |
| 66 | ~LocalRef(); |
| 67 | explicit LocalRef(egl_object_t* rhs); |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 68 | explicit LocalRef(egl_display_t const* display, T o) : ref(0) { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 69 | egl_object_t* native = reinterpret_cast<N*>(o); |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 70 | if (o && egl_object_t::get(display, native)) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 71 | ref = native; |
| 72 | } |
| 73 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 74 | inline N* get() { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 75 | return static_cast<N*>(ref); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 76 | } |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 77 | void acquire() const; |
| 78 | void release() const; |
| 79 | void terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 80 | }; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 81 | template <typename N, typename T> |
| 82 | friend class LocalRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 85 | template<typename N, typename T> |
| 86 | egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) { |
| 87 | if (ref) { |
| 88 | ref->incRef(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | template <typename N, typename T> |
| 93 | egl_object_t::LocalRef<N,T>::~LocalRef() { |
| 94 | if (ref) { |
| 95 | ref->destroy(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | template <typename N, typename T> |
| 100 | void egl_object_t::LocalRef<N,T>::acquire() const { |
| 101 | if (ref) { |
| 102 | ref->incRef(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | template <typename N, typename T> |
| 107 | void egl_object_t::LocalRef<N,T>::release() const { |
| 108 | if (ref) { |
| 109 | if (ref->decRef() == 1) { |
| 110 | // shouldn't happen because this is called from LocalRef |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 111 | ALOGE("LocalRef::release() removed the last reference!"); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | template <typename N, typename T> |
| 117 | void egl_object_t::LocalRef<N,T>::terminate() { |
| 118 | if (ref) { |
| 119 | ref->terminate(); |
| 120 | } |
| 121 | } |
| 122 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 123 | // ---------------------------------------------------------------------------- |
| 124 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 125 | class egl_surface_t : public egl_object_t { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 126 | protected: |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 127 | ~egl_surface_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 128 | void terminate() override; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 129 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 130 | typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref; |
| 131 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 132 | egl_surface_t(egl_display_t* dpy, EGLConfig config, |
| 133 | EGLNativeWindowType win, EGLSurface surface, |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 134 | egl_connection_t const* cnx); |
| 135 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 136 | EGLSurface surface; |
| 137 | EGLConfig config; |
| 138 | sp<ANativeWindow> win; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 139 | egl_connection_t const* cnx; |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 140 | private: |
| 141 | bool connected; |
| 142 | void disconnect(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 145 | class egl_context_t: public egl_object_t { |
| 146 | protected: |
| 147 | ~egl_context_t() {} |
| 148 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 149 | typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; |
| 150 | |
| 151 | egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 152 | egl_connection_t const* cnx, int version); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 153 | |
| 154 | void onLooseCurrent(); |
| 155 | void onMakeCurrent(EGLSurface draw, EGLSurface read); |
| 156 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 157 | EGLDisplay dpy; |
| 158 | EGLContext context; |
| 159 | EGLConfig config; |
| 160 | EGLSurface read; |
| 161 | EGLSurface draw; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 162 | egl_connection_t const* cnx; |
| 163 | int version; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 164 | String8 gl_extensions; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 165 | Vector<String8> tokenized_gl_extensions; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 168 | // ---------------------------------------------------------------------------- |
| 169 | |
| 170 | typedef egl_surface_t::Ref SurfaceRef; |
| 171 | typedef egl_context_t::Ref ContextRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 172 | |
| 173 | // ---------------------------------------------------------------------------- |
| 174 | |
| 175 | template<typename NATIVE, typename EGL> |
| 176 | static inline NATIVE* egl_to_native_cast(EGL arg) { |
| 177 | return reinterpret_cast<NATIVE*>(arg); |
| 178 | } |
| 179 | |
| 180 | static inline |
| 181 | egl_surface_t* get_surface(EGLSurface surface) { |
| 182 | return egl_to_native_cast<egl_surface_t>(surface); |
| 183 | } |
| 184 | |
| 185 | static inline |
| 186 | egl_context_t* get_context(EGLContext context) { |
| 187 | return egl_to_native_cast<egl_context_t>(context); |
| 188 | } |
| 189 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 190 | // ---------------------------------------------------------------------------- |
| 191 | }; // namespace android |
| 192 | // ---------------------------------------------------------------------------- |
| 193 | |
| 194 | #endif // ANDROID_EGL_OBJECT_H |