blob: 46f7139ad42eefdca6d837a8eda9e6d1fca07ef6 [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>
31
32#include <system/window.h>
33
34#include "egl_display.h"
35
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
40struct egl_display_t;
41
42class egl_object_t {
43 egl_display_t *display;
Mathias Agopian518ec112011-05-13 16:21:08 -070044 mutable volatile int32_t count;
45
Mathias Agopian5b287a62011-05-16 18:58:55 -070046protected:
47 virtual ~egl_object_t();
48
Mathias Agopian518ec112011-05-13 16:21:08 -070049public:
50 egl_object_t(egl_display_t* display);
Mathias Agopian5b287a62011-05-16 18:58:55 -070051 void destroy();
Mathias Agopian518ec112011-05-13 16:21:08 -070052
Mathias Agopian518ec112011-05-13 16:21:08 -070053 inline int32_t incRef() { return android_atomic_inc(&count); }
54 inline int32_t decRef() { return android_atomic_dec(&count); }
55
56private:
Mathias Agopian5b287a62011-05-16 18:58:55 -070057 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070058 bool get();
Mathias Agopian518ec112011-05-13 16:21:08 -070059
60public:
61 template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070062 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 Agopian518ec112011-05-13 16:21:08 -070071 if (o && native->get()) {
72 ref = native;
73 }
74 }
Mathias Agopian518ec112011-05-13 16:21:08 -070075 inline N* get() {
Mathias Agopian5b287a62011-05-16 18:58:55 -070076 return static_cast<N*>(ref);
Mathias Agopian518ec112011-05-13 16:21:08 -070077 }
Mathias Agopian5b287a62011-05-16 18:58:55 -070078 void acquire() const;
79 void release() const;
80 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070081 };
Mathias Agopian5b287a62011-05-16 18:58:55 -070082 template <typename N, typename T>
83 friend class LocalRef;
Mathias Agopian518ec112011-05-13 16:21:08 -070084};
85
Mathias Agopian5b287a62011-05-16 18:58:55 -070086template<typename N, typename T>
87egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
88 if (ref) {
89 ref->incRef();
90 }
91}
92
93template <typename N, typename T>
94egl_object_t::LocalRef<N,T>::~LocalRef() {
95 if (ref) {
96 ref->destroy();
97 }
98}
99
100template <typename N, typename T>
101void egl_object_t::LocalRef<N,T>::acquire() const {
102 if (ref) {
103 ref->incRef();
104 }
105}
106
107template <typename N, typename T>
108void 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
117template <typename N, typename T>
118void egl_object_t::LocalRef<N,T>::terminate() {
119 if (ref) {
120 ref->terminate();
121 }
122}
123
Mathias Agopian518ec112011-05-13 16:21:08 -0700124// ----------------------------------------------------------------------------
125
Mathias Agopian5b287a62011-05-16 18:58:55 -0700126class egl_surface_t: public egl_object_t {
127protected:
Jamie Gennis9f24fd02011-08-24 14:54:56 -0700128 ~egl_surface_t() {
129 ANativeWindow* const window = win.get();
130 if (window != NULL) {
131 native_window_set_buffers_format(window, 0);
132 if (native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL)) {
Mathias Agopian06649fe2011-09-16 11:56:40 -0700133 LOGW("EGLNativeWindowType %p disconnect failed", window);
Jamie Gennis9f24fd02011-08-24 14:54:56 -0700134 }
135 }
136 }
Mathias Agopian5b287a62011-05-16 18:58:55 -0700137public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
139
140 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
141 EGLSurface surface, int impl, egl_connection_t const* cnx) :
142 egl_object_t(get_display(dpy)), dpy(dpy), surface(surface),
143 config(config), win(win), impl(impl), cnx(cnx) {
144 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700145 EGLDisplay dpy;
146 EGLSurface surface;
147 EGLConfig config;
148 sp<ANativeWindow> win;
149 int impl;
150 egl_connection_t const* cnx;
151};
152
Mathias Agopian5b287a62011-05-16 18:58:55 -0700153class egl_context_t: public egl_object_t {
154protected:
155 ~egl_context_t() {}
156public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700157 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
158
159 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
160 int impl, egl_connection_t const* cnx, int version) :
161 egl_object_t(get_display(dpy)), dpy(dpy), context(context),
162 config(config), read(0), draw(0), impl(impl), cnx(cnx),
163 version(version) {
164 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700165 EGLDisplay dpy;
166 EGLContext context;
167 EGLConfig config;
168 EGLSurface read;
169 EGLSurface draw;
170 int impl;
171 egl_connection_t const* cnx;
172 int version;
173};
174
Mathias Agopian5b287a62011-05-16 18:58:55 -0700175class egl_image_t: public egl_object_t {
176protected:
177 ~egl_image_t() {}
178public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700179 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
180
181 egl_image_t(EGLDisplay dpy, EGLContext context) :
182 egl_object_t(get_display(dpy)), dpy(dpy), context(context) {
183 memset(images, 0, sizeof(images));
184 }
185 EGLDisplay dpy;
186 EGLContext context;
187 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
188};
189
Mathias Agopian5b287a62011-05-16 18:58:55 -0700190class egl_sync_t: public egl_object_t {
191protected:
192 ~egl_sync_t() {}
193public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700194 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
195
196 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync) :
197 egl_object_t(get_display(dpy)), dpy(dpy), context(context), sync(sync) {
198 }
199 EGLDisplay dpy;
200 EGLContext context;
201 EGLSyncKHR sync;
202};
203
204// ----------------------------------------------------------------------------
205
206typedef egl_surface_t::Ref SurfaceRef;
207typedef egl_context_t::Ref ContextRef;
208typedef egl_image_t::Ref ImageRef;
209typedef egl_sync_t::Ref SyncRef;
210
211// ----------------------------------------------------------------------------
212
213template<typename NATIVE, typename EGL>
214static inline NATIVE* egl_to_native_cast(EGL arg) {
215 return reinterpret_cast<NATIVE*>(arg);
216}
217
218static inline
219egl_surface_t* get_surface(EGLSurface surface) {
220 return egl_to_native_cast<egl_surface_t>(surface);
221}
222
223static inline
224egl_context_t* get_context(EGLContext context) {
225 return egl_to_native_cast<egl_context_t>(context);
226}
227
228static inline
229egl_image_t* get_image(EGLImageKHR image) {
230 return egl_to_native_cast<egl_image_t>(image);
231}
232
233static inline
234egl_sync_t* get_sync(EGLSyncKHR sync) {
235 return egl_to_native_cast<egl_sync_t>(sync);
236}
237
238// ----------------------------------------------------------------------------
239}; // namespace android
240// ----------------------------------------------------------------------------
241
242#endif // ANDROID_EGL_OBJECT_H