| 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 |  | 
|  | 20 |  | 
|  | 21 | #include <ctype.h> | 
|  | 22 | #include <stdint.h> | 
|  | 23 | #include <stdlib.h> | 
|  | 24 |  | 
|  | 25 | #include <EGL/egl.h> | 
|  | 26 | #include <EGL/eglext.h> | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | #include <utils/threads.h> | 
| Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 29 | #include <utils/String8.h> | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #include <system/window.h> | 
|  | 32 |  | 
|  | 33 | #include "egl_display.h" | 
|  | 34 |  | 
|  | 35 | // ---------------------------------------------------------------------------- | 
|  | 36 | namespace android { | 
|  | 37 | // ---------------------------------------------------------------------------- | 
|  | 38 |  | 
|  | 39 | struct egl_display_t; | 
|  | 40 |  | 
|  | 41 | class egl_object_t { | 
|  | 42 | egl_display_t *display; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 43 | mutable volatile int32_t count; | 
|  | 44 |  | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 45 | protected: | 
|  | 46 | virtual ~egl_object_t(); | 
|  | 47 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 48 | public: | 
|  | 49 | 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 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 52 | inline int32_t incRef() { return android_atomic_inc(&count); } | 
|  | 53 | inline int32_t decRef() { return android_atomic_dec(&count); } | 
| 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 | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 57 | void terminate(); | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 58 | static bool get(egl_display_t const* display, egl_object_t* object); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 59 |  | 
|  | 60 | public: | 
|  | 61 | template <typename N, typename T> | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 62 | class LocalRef { | 
|  | 63 | egl_object_t* ref; | 
|  | 64 | LocalRef(); | 
|  | 65 | LocalRef(const LocalRef* rhs); | 
|  | 66 | public: | 
|  | 67 | ~LocalRef(); | 
|  | 68 | explicit LocalRef(egl_object_t* rhs); | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 69 | explicit LocalRef(egl_display_t const* display, T o) : ref(0) { | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 70 | egl_object_t* native = reinterpret_cast<N*>(o); | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 71 | if (o && egl_object_t::get(display, native)) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 72 | ref = native; | 
|  | 73 | } | 
|  | 74 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 75 | inline N* get() { | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 76 | return static_cast<N*>(ref); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 77 | } | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 78 | void acquire() const; | 
|  | 79 | void release() const; | 
|  | 80 | void terminate(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 81 | }; | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 82 | template <typename N, typename T> | 
|  | 83 | friend class LocalRef; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 84 | }; | 
|  | 85 |  | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 86 | template<typename N, typename T> | 
|  | 87 | egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) { | 
|  | 88 | if (ref) { | 
|  | 89 | ref->incRef(); | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | template <typename N, typename T> | 
|  | 94 | egl_object_t::LocalRef<N,T>::~LocalRef() { | 
|  | 95 | if (ref) { | 
|  | 96 | ref->destroy(); | 
|  | 97 | } | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | template <typename N, typename T> | 
|  | 101 | void egl_object_t::LocalRef<N,T>::acquire() const { | 
|  | 102 | if (ref) { | 
|  | 103 | ref->incRef(); | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | template <typename N, typename T> | 
|  | 108 | void egl_object_t::LocalRef<N,T>::release() const { | 
|  | 109 | if (ref) { | 
|  | 110 | if (ref->decRef() == 1) { | 
|  | 111 | // shouldn't happen because this is called from LocalRef | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 112 | ALOGE("LocalRef::release() removed the last reference!"); | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 113 | } | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | template <typename N, typename T> | 
|  | 118 | void egl_object_t::LocalRef<N,T>::terminate() { | 
|  | 119 | if (ref) { | 
|  | 120 | ref->terminate(); | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 124 | // ---------------------------------------------------------------------------- | 
|  | 125 |  | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 126 | class egl_surface_t : public egl_object_t { | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 127 | protected: | 
| Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 128 | ~egl_surface_t(); | 
| 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; | 
|  | 140 | }; | 
|  | 141 |  | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 142 | class egl_context_t: public egl_object_t { | 
|  | 143 | protected: | 
|  | 144 | ~egl_context_t() {} | 
|  | 145 | public: | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 146 | typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; | 
|  | 147 |  | 
|  | 148 | egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, | 
| Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 149 | egl_connection_t const* cnx, int version); | 
| Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 150 |  | 
|  | 151 | void onLooseCurrent(); | 
|  | 152 | void onMakeCurrent(EGLSurface draw, EGLSurface read); | 
|  | 153 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 154 | EGLDisplay dpy; | 
|  | 155 | EGLContext context; | 
|  | 156 | EGLConfig config; | 
|  | 157 | EGLSurface read; | 
|  | 158 | EGLSurface draw; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 159 | egl_connection_t const* cnx; | 
|  | 160 | int version; | 
| Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 161 | String8 gl_extensions; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 162 | }; | 
|  | 163 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 164 | // ---------------------------------------------------------------------------- | 
|  | 165 |  | 
|  | 166 | typedef egl_surface_t::Ref  SurfaceRef; | 
|  | 167 | typedef egl_context_t::Ref  ContextRef; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | // ---------------------------------------------------------------------------- | 
|  | 170 |  | 
|  | 171 | template<typename NATIVE, typename EGL> | 
|  | 172 | static inline NATIVE* egl_to_native_cast(EGL arg) { | 
|  | 173 | return reinterpret_cast<NATIVE*>(arg); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | static inline | 
|  | 177 | egl_surface_t* get_surface(EGLSurface surface) { | 
|  | 178 | return egl_to_native_cast<egl_surface_t>(surface); | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | static inline | 
|  | 182 | egl_context_t* get_context(EGLContext context) { | 
|  | 183 | return egl_to_native_cast<egl_context_t>(context); | 
|  | 184 | } | 
|  | 185 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 186 | // ---------------------------------------------------------------------------- | 
|  | 187 | }; // namespace android | 
|  | 188 | // ---------------------------------------------------------------------------- | 
|  | 189 |  | 
|  | 190 | #endif // ANDROID_EGL_OBJECT_H |