Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 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 | |
| 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> |
| 27 | #include <GLES/gl.h> |
| 28 | #include <GLES/glext.h> |
| 29 | |
| 30 | #include <utils/threads.h> |
| 31 | |
| 32 | #include <system/window.h> |
| 33 | |
| 34 | #include "egl_display.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | namespace android { |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | struct egl_display_t; |
| 41 | |
| 42 | class egl_object_t { |
| 43 | egl_display_t *display; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | mutable volatile int32_t count; |
| 45 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 46 | protected: |
| 47 | virtual ~egl_object_t(); |
| 48 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 49 | public: |
| 50 | egl_object_t(egl_display_t* display); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 51 | void destroy(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 53 | inline int32_t incRef() { return android_atomic_inc(&count); } |
| 54 | inline int32_t decRef() { return android_atomic_dec(&count); } |
| 55 | |
| 56 | private: |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 57 | void terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 58 | bool get(); |
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); |
| 69 | explicit LocalRef(T o) : ref(0) { |
| 70 | egl_object_t* native = reinterpret_cast<N*>(o); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 71 | if (o && native->get()) { |
| 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 |
| 112 | LOGE("LocalRef::release() removed the last reference!"); |
| 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 | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 126 | class egl_surface_t: public egl_object_t { |
| 127 | protected: |
| 128 | ~egl_surface_t() {} |
| 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 | |
| 132 | egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, |
| 133 | EGLSurface surface, int impl, egl_connection_t const* cnx) : |
| 134 | egl_object_t(get_display(dpy)), dpy(dpy), surface(surface), |
| 135 | config(config), win(win), impl(impl), cnx(cnx) { |
| 136 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 137 | EGLDisplay dpy; |
| 138 | EGLSurface surface; |
| 139 | EGLConfig config; |
| 140 | sp<ANativeWindow> win; |
| 141 | int impl; |
| 142 | egl_connection_t const* cnx; |
| 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, |
| 152 | int impl, egl_connection_t const* cnx, int version) : |
| 153 | egl_object_t(get_display(dpy)), dpy(dpy), context(context), |
| 154 | config(config), read(0), draw(0), impl(impl), cnx(cnx), |
| 155 | version(version) { |
| 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; |
| 162 | int impl; |
| 163 | egl_connection_t const* cnx; |
| 164 | int version; |
| 165 | }; |
| 166 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 167 | class egl_image_t: public egl_object_t { |
| 168 | protected: |
| 169 | ~egl_image_t() {} |
| 170 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 171 | typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref; |
| 172 | |
| 173 | egl_image_t(EGLDisplay dpy, EGLContext context) : |
| 174 | egl_object_t(get_display(dpy)), dpy(dpy), context(context) { |
| 175 | memset(images, 0, sizeof(images)); |
| 176 | } |
| 177 | EGLDisplay dpy; |
| 178 | EGLContext context; |
| 179 | EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS]; |
| 180 | }; |
| 181 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame^] | 182 | class egl_sync_t: public egl_object_t { |
| 183 | protected: |
| 184 | ~egl_sync_t() {} |
| 185 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 186 | typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref; |
| 187 | |
| 188 | egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync) : |
| 189 | egl_object_t(get_display(dpy)), dpy(dpy), context(context), sync(sync) { |
| 190 | } |
| 191 | EGLDisplay dpy; |
| 192 | EGLContext context; |
| 193 | EGLSyncKHR sync; |
| 194 | }; |
| 195 | |
| 196 | // ---------------------------------------------------------------------------- |
| 197 | |
| 198 | typedef egl_surface_t::Ref SurfaceRef; |
| 199 | typedef egl_context_t::Ref ContextRef; |
| 200 | typedef egl_image_t::Ref ImageRef; |
| 201 | typedef egl_sync_t::Ref SyncRef; |
| 202 | |
| 203 | // ---------------------------------------------------------------------------- |
| 204 | |
| 205 | template<typename NATIVE, typename EGL> |
| 206 | static inline NATIVE* egl_to_native_cast(EGL arg) { |
| 207 | return reinterpret_cast<NATIVE*>(arg); |
| 208 | } |
| 209 | |
| 210 | static inline |
| 211 | egl_surface_t* get_surface(EGLSurface surface) { |
| 212 | return egl_to_native_cast<egl_surface_t>(surface); |
| 213 | } |
| 214 | |
| 215 | static inline |
| 216 | egl_context_t* get_context(EGLContext context) { |
| 217 | return egl_to_native_cast<egl_context_t>(context); |
| 218 | } |
| 219 | |
| 220 | static inline |
| 221 | egl_image_t* get_image(EGLImageKHR image) { |
| 222 | return egl_to_native_cast<egl_image_t>(image); |
| 223 | } |
| 224 | |
| 225 | static inline |
| 226 | egl_sync_t* get_sync(EGLSyncKHR sync) { |
| 227 | return egl_to_native_cast<egl_sync_t>(sync); |
| 228 | } |
| 229 | |
| 230 | // ---------------------------------------------------------------------------- |
| 231 | }; // namespace android |
| 232 | // ---------------------------------------------------------------------------- |
| 233 | |
| 234 | #endif // ANDROID_EGL_OBJECT_H |
| 235 | |