blob: 8268900a416ebfe04d149577fe8527d7363b4cad [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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 Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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 Agopian518ec112011-05-13 16:21:08 -070014 ** 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 Agopian518ec112011-05-13 16:21:08 -070027
28#include <utils/threads.h>
Mathias Agopian48d438d2012-01-28 21:44:00 -080029#include <utils/String8.h>
Alistair Strachanedfe72e2015-05-22 14:10:09 -070030#include <utils/Vector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070031
32#include <system/window.h>
33
34#include "egl_display.h"
35
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
Dan Alberteacd31f2016-02-02 15:08:34 -080040class egl_display_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070041
42class egl_object_t {
Mathias Agopian518ec112011-05-13 16:21:08 -070043 mutable volatile int32_t count;
44
Mathias Agopian5b287a62011-05-16 18:58:55 -070045protected:
46 virtual ~egl_object_t();
Pablo Ceballos541de492016-04-09 15:56:12 -070047 virtual void terminate();
48 egl_display_t *display;
Mathias Agopian5b287a62011-05-16 18:58:55 -070049
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 Agopianf0480de2011-11-13 20:50:07 -080059 static bool get(egl_display_t const* display, egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070060
61public:
62 template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070063 class LocalRef {
64 egl_object_t* ref;
65 LocalRef();
66 LocalRef(const LocalRef* rhs);
67 public:
68 ~LocalRef();
69 explicit LocalRef(egl_object_t* rhs);
Mathias Agopianf0480de2011-11-13 20:50:07 -080070 explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
Mathias Agopian5b287a62011-05-16 18:58:55 -070071 egl_object_t* native = reinterpret_cast<N*>(o);
Mathias Agopianf0480de2011-11-13 20:50:07 -080072 if (o && egl_object_t::get(display, native)) {
Mathias Agopian518ec112011-05-13 16:21:08 -070073 ref = native;
74 }
75 }
Mathias Agopian518ec112011-05-13 16:21:08 -070076 inline N* get() {
Mathias Agopian5b287a62011-05-16 18:58:55 -070077 return static_cast<N*>(ref);
Mathias Agopian518ec112011-05-13 16:21:08 -070078 }
Mathias Agopian5b287a62011-05-16 18:58:55 -070079 void acquire() const;
80 void release() const;
81 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070082 };
Mathias Agopian5b287a62011-05-16 18:58:55 -070083 template <typename N, typename T>
84 friend class LocalRef;
Mathias Agopian518ec112011-05-13 16:21:08 -070085};
86
Mathias Agopian5b287a62011-05-16 18:58:55 -070087template<typename N, typename T>
88egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
89 if (ref) {
90 ref->incRef();
91 }
92}
93
94template <typename N, typename T>
95egl_object_t::LocalRef<N,T>::~LocalRef() {
96 if (ref) {
97 ref->destroy();
98 }
99}
100
101template <typename N, typename T>
102void egl_object_t::LocalRef<N,T>::acquire() const {
103 if (ref) {
104 ref->incRef();
105 }
106}
107
108template <typename N, typename T>
109void egl_object_t::LocalRef<N,T>::release() const {
110 if (ref) {
111 if (ref->decRef() == 1) {
112 // shouldn't happen because this is called from LocalRef
Steve Blocke6f43dd2012-01-06 19:20:56 +0000113 ALOGE("LocalRef::release() removed the last reference!");
Mathias Agopian5b287a62011-05-16 18:58:55 -0700114 }
115 }
116}
117
118template <typename N, typename T>
119void egl_object_t::LocalRef<N,T>::terminate() {
120 if (ref) {
121 ref->terminate();
122 }
123}
124
Mathias Agopian518ec112011-05-13 16:21:08 -0700125// ----------------------------------------------------------------------------
126
Mathias Agopianada798b2012-02-13 17:09:30 -0800127class egl_surface_t : public egl_object_t {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700128protected:
Jesse Hall25838592012-04-05 15:53:28 -0700129 ~egl_surface_t();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700130public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700131 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
132
Jesse Hallb29e5e82012-04-04 16:53:42 -0700133 egl_surface_t(egl_display_t* dpy, EGLConfig config,
134 EGLNativeWindowType win, EGLSurface surface,
Jesse Hall25838592012-04-05 15:53:28 -0700135 egl_connection_t const* cnx);
136
Mathias Agopian518ec112011-05-13 16:21:08 -0700137 EGLSurface surface;
138 EGLConfig config;
139 sp<ANativeWindow> win;
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 egl_connection_t const* cnx;
141};
142
Mathias Agopian5b287a62011-05-16 18:58:55 -0700143class egl_context_t: public egl_object_t {
144protected:
145 ~egl_context_t() {}
Pablo Ceballos541de492016-04-09 15:56:12 -0700146 void terminate() override;
Mathias Agopian5b287a62011-05-16 18:58:55 -0700147public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
149
150 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopianada798b2012-02-13 17:09:30 -0800151 egl_connection_t const* cnx, int version);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800152
153 void onLooseCurrent();
154 void onMakeCurrent(EGLSurface draw, EGLSurface read);
155
Mathias Agopian518ec112011-05-13 16:21:08 -0700156 EGLDisplay dpy;
157 EGLContext context;
158 EGLConfig config;
159 EGLSurface read;
160 EGLSurface draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700161 egl_connection_t const* cnx;
162 int version;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800163 String8 gl_extensions;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700164 Vector<String8> tokenized_gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700165};
166
Mathias Agopian518ec112011-05-13 16:21:08 -0700167// ----------------------------------------------------------------------------
168
169typedef egl_surface_t::Ref SurfaceRef;
170typedef egl_context_t::Ref ContextRef;
Mathias Agopian518ec112011-05-13 16:21:08 -0700171
172// ----------------------------------------------------------------------------
173
174template<typename NATIVE, typename EGL>
175static inline NATIVE* egl_to_native_cast(EGL arg) {
176 return reinterpret_cast<NATIVE*>(arg);
177}
178
179static inline
180egl_surface_t* get_surface(EGLSurface surface) {
181 return egl_to_native_cast<egl_surface_t>(surface);
182}
183
184static inline
185egl_context_t* get_context(EGLContext context) {
186 return egl_to_native_cast<egl_context_t>(context);
187}
188
Mathias Agopian518ec112011-05-13 16:21:08 -0700189// ----------------------------------------------------------------------------
190}; // namespace android
191// ----------------------------------------------------------------------------
192
193#endif // ANDROID_EGL_OBJECT_H