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