blob: abd4cbb962a69f0c1ab01c52a0eaf3e7e08fe392 [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
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 Agopian48d438d2012-01-28 21:44:00 -080031#include <utils/String8.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032
33#include <system/window.h>
34
35#include "egl_display.h"
36
37// ----------------------------------------------------------------------------
38namespace android {
39// ----------------------------------------------------------------------------
40
41struct egl_display_t;
42
43class egl_object_t {
44 egl_display_t *display;
Mathias Agopian518ec112011-05-13 16:21:08 -070045 mutable volatile int32_t count;
46
Mathias Agopian5b287a62011-05-16 18:58:55 -070047protected:
48 virtual ~egl_object_t();
49
Mathias Agopian518ec112011-05-13 16:21:08 -070050public:
51 egl_object_t(egl_display_t* display);
Mathias Agopian5b287a62011-05-16 18:58:55 -070052 void destroy();
Mathias Agopian518ec112011-05-13 16:21:08 -070053
Mathias Agopian518ec112011-05-13 16:21:08 -070054 inline int32_t incRef() { return android_atomic_inc(&count); }
55 inline int32_t decRef() { return android_atomic_dec(&count); }
Mathias Agopianf0480de2011-11-13 20:50:07 -080056 inline egl_display_t* getDisplay() const { return display; }
Mathias Agopian518ec112011-05-13 16:21:08 -070057
58private:
Mathias Agopian5b287a62011-05-16 18:58:55 -070059 void terminate();
Mathias Agopianf0480de2011-11-13 20:50:07 -080060 static bool get(egl_display_t const* display, egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070061
62public:
63 template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070064 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 Agopianf0480de2011-11-13 20:50:07 -080071 explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
Mathias Agopian5b287a62011-05-16 18:58:55 -070072 egl_object_t* native = reinterpret_cast<N*>(o);
Mathias Agopianf0480de2011-11-13 20:50:07 -080073 if (o && egl_object_t::get(display, native)) {
Mathias Agopian518ec112011-05-13 16:21:08 -070074 ref = native;
75 }
76 }
Mathias Agopian518ec112011-05-13 16:21:08 -070077 inline N* get() {
Mathias Agopian5b287a62011-05-16 18:58:55 -070078 return static_cast<N*>(ref);
Mathias Agopian518ec112011-05-13 16:21:08 -070079 }
Mathias Agopian5b287a62011-05-16 18:58:55 -070080 void acquire() const;
81 void release() const;
82 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070083 };
Mathias Agopian5b287a62011-05-16 18:58:55 -070084 template <typename N, typename T>
85 friend class LocalRef;
Mathias Agopian518ec112011-05-13 16:21:08 -070086};
87
Mathias Agopian5b287a62011-05-16 18:58:55 -070088template<typename N, typename T>
89egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
90 if (ref) {
91 ref->incRef();
92 }
93}
94
95template <typename N, typename T>
96egl_object_t::LocalRef<N,T>::~LocalRef() {
97 if (ref) {
98 ref->destroy();
99 }
100}
101
102template <typename N, typename T>
103void egl_object_t::LocalRef<N,T>::acquire() const {
104 if (ref) {
105 ref->incRef();
106 }
107}
108
109template <typename N, typename T>
110void 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 Blocke6f43dd2012-01-06 19:20:56 +0000114 ALOGE("LocalRef::release() removed the last reference!");
Mathias Agopian5b287a62011-05-16 18:58:55 -0700115 }
116 }
117}
118
119template <typename N, typename T>
120void egl_object_t::LocalRef<N,T>::terminate() {
121 if (ref) {
122 ref->terminate();
123 }
124}
125
Mathias Agopian518ec112011-05-13 16:21:08 -0700126// ----------------------------------------------------------------------------
127
Mathias Agopian5b287a62011-05-16 18:58:55 -0700128class egl_surface_t: public egl_object_t {
129protected:
Jamie Gennis9f24fd02011-08-24 14:54:56 -0700130 ~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 Block32397c12012-01-05 23:22:43 +0000135 ALOGW("EGLNativeWindowType %p disconnect failed", window);
Jamie Gennis9f24fd02011-08-24 14:54:56 -0700136 }
137 }
138 }
Mathias Agopian5b287a62011-05-16 18:58:55 -0700139public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
141
142 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
143 EGLSurface surface, int impl, egl_connection_t const* cnx) :
144 egl_object_t(get_display(dpy)), dpy(dpy), surface(surface),
145 config(config), win(win), impl(impl), cnx(cnx) {
146 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 EGLDisplay dpy;
148 EGLSurface surface;
149 EGLConfig config;
150 sp<ANativeWindow> win;
151 int impl;
152 egl_connection_t const* cnx;
153};
154
Mathias Agopian5b287a62011-05-16 18:58:55 -0700155class egl_context_t: public egl_object_t {
156protected:
157 ~egl_context_t() {}
158public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700159 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
160
161 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian48d438d2012-01-28 21:44:00 -0800162 int impl, egl_connection_t const* cnx, int version);
163
164 void onLooseCurrent();
165 void onMakeCurrent(EGLSurface draw, EGLSurface read);
166
Mathias Agopian518ec112011-05-13 16:21:08 -0700167 EGLDisplay dpy;
168 EGLContext context;
169 EGLConfig config;
170 EGLSurface read;
171 EGLSurface draw;
172 int impl;
173 egl_connection_t const* cnx;
174 int version;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800175 String8 gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700176};
177
Mathias Agopian5b287a62011-05-16 18:58:55 -0700178class egl_image_t: public egl_object_t {
179protected:
180 ~egl_image_t() {}
181public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700182 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
183
184 egl_image_t(EGLDisplay dpy, EGLContext context) :
185 egl_object_t(get_display(dpy)), dpy(dpy), context(context) {
186 memset(images, 0, sizeof(images));
187 }
188 EGLDisplay dpy;
189 EGLContext context;
190 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
191};
192
Mathias Agopian5b287a62011-05-16 18:58:55 -0700193class egl_sync_t: public egl_object_t {
194protected:
195 ~egl_sync_t() {}
196public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700197 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
198
199 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync) :
200 egl_object_t(get_display(dpy)), dpy(dpy), context(context), sync(sync) {
201 }
202 EGLDisplay dpy;
203 EGLContext context;
204 EGLSyncKHR sync;
205};
206
207// ----------------------------------------------------------------------------
208
209typedef egl_surface_t::Ref SurfaceRef;
210typedef egl_context_t::Ref ContextRef;
211typedef egl_image_t::Ref ImageRef;
212typedef egl_sync_t::Ref SyncRef;
213
214// ----------------------------------------------------------------------------
215
216template<typename NATIVE, typename EGL>
217static inline NATIVE* egl_to_native_cast(EGL arg) {
218 return reinterpret_cast<NATIVE*>(arg);
219}
220
221static inline
222egl_surface_t* get_surface(EGLSurface surface) {
223 return egl_to_native_cast<egl_surface_t>(surface);
224}
225
226static inline
227egl_context_t* get_context(EGLContext context) {
228 return egl_to_native_cast<egl_context_t>(context);
229}
230
231static inline
232egl_image_t* get_image(EGLImageKHR image) {
233 return egl_to_native_cast<egl_image_t>(image);
234}
235
236static inline
237egl_sync_t* get_sync(EGLSyncKHR sync) {
238 return egl_to_native_cast<egl_sync_t>(sync);
239}
240
241// ----------------------------------------------------------------------------
242}; // namespace android
243// ----------------------------------------------------------------------------
244
245#endif // ANDROID_EGL_OBJECT_H