blob: f4012abb399b2aef3a247191b37fc6989679c159 [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
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include <stdint.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021#include <stddef.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070022
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
Mathias Agopian311b4792017-02-28 15:00:49 -080026#include <utils/StrongPointer.h>
Mathias Agopian48d438d2012-01-28 21:44:00 -080027#include <utils/String8.h>
Alistair Strachanedfe72e2015-05-22 14:10:09 -070028#include <utils/Vector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30#include <system/window.h>
31
32#include "egl_display.h"
33
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
Dan Alberteacd31f2016-02-02 15:08:34 -080038class egl_display_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070039
40class egl_object_t {
Pablo Ceballos1a5c4de2016-04-25 20:40:08 +000041 egl_display_t *display;
Jesse Hall3aa75f92016-05-20 10:47:07 -070042 mutable std::atomic_size_t count;
Mathias Agopian518ec112011-05-13 16:21:08 -070043
Mathias Agopian5b287a62011-05-16 18:58:55 -070044protected:
45 virtual ~egl_object_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -070046 virtual void terminate();
Mathias Agopian5b287a62011-05-16 18:58:55 -070047
Mathias Agopian518ec112011-05-13 16:21:08 -070048public:
Chih-Hung Hsiehe8761d62016-09-01 11:26:34 -070049 explicit egl_object_t(egl_display_t* display);
Mathias Agopian5b287a62011-05-16 18:58:55 -070050 void destroy();
Mathias Agopian518ec112011-05-13 16:21:08 -070051
Jesse Hall3aa75f92016-05-20 10:47:07 -070052 inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
53 inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
Mathias Agopianf0480de2011-11-13 20:50:07 -080054 inline egl_display_t* getDisplay() const { return display; }
Mathias Agopian518ec112011-05-13 16:21:08 -070055
56private:
Mathias Agopianf0480de2011-11-13 20:50:07 -080057 static bool get(egl_display_t const* display, egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070058
59public:
60 template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070061 class LocalRef {
62 egl_object_t* ref;
Mathias Agopian311b4792017-02-28 15:00:49 -080063 LocalRef() = delete;
64 LocalRef(const LocalRef* rhs) = delete;
Mathias Agopian5b287a62011-05-16 18:58:55 -070065 public:
66 ~LocalRef();
67 explicit LocalRef(egl_object_t* rhs);
Mathias Agopianf0480de2011-11-13 20:50:07 -080068 explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
Mathias Agopian5b287a62011-05-16 18:58:55 -070069 egl_object_t* native = reinterpret_cast<N*>(o);
Mathias Agopianf0480de2011-11-13 20:50:07 -080070 if (o && egl_object_t::get(display, native)) {
Mathias Agopian518ec112011-05-13 16:21:08 -070071 ref = native;
72 }
73 }
Mathias Agopian518ec112011-05-13 16:21:08 -070074 inline N* get() {
Mathias Agopian5b287a62011-05-16 18:58:55 -070075 return static_cast<N*>(ref);
Mathias Agopian518ec112011-05-13 16:21:08 -070076 }
Mathias Agopian5b287a62011-05-16 18:58:55 -070077 void acquire() const;
78 void release() const;
79 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070080 };
Mathias Agopian5b287a62011-05-16 18:58:55 -070081 template <typename N, typename T>
82 friend class LocalRef;
Mathias Agopian518ec112011-05-13 16:21:08 -070083};
84
Mathias Agopian5b287a62011-05-16 18:58:55 -070085template<typename N, typename T>
86egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
87 if (ref) {
88 ref->incRef();
89 }
90}
91
92template <typename N, typename T>
93egl_object_t::LocalRef<N,T>::~LocalRef() {
94 if (ref) {
95 ref->destroy();
96 }
97}
98
99template <typename N, typename T>
100void egl_object_t::LocalRef<N,T>::acquire() const {
101 if (ref) {
102 ref->incRef();
103 }
104}
105
106template <typename N, typename T>
107void egl_object_t::LocalRef<N,T>::release() const {
108 if (ref) {
109 if (ref->decRef() == 1) {
110 // shouldn't happen because this is called from LocalRef
Steve Blocke6f43dd2012-01-06 19:20:56 +0000111 ALOGE("LocalRef::release() removed the last reference!");
Mathias Agopian5b287a62011-05-16 18:58:55 -0700112 }
113 }
114}
115
116template <typename N, typename T>
117void egl_object_t::LocalRef<N,T>::terminate() {
118 if (ref) {
119 ref->terminate();
120 }
121}
122
Mathias Agopian518ec112011-05-13 16:21:08 -0700123// ----------------------------------------------------------------------------
124
Mathias Agopianada798b2012-02-13 17:09:30 -0800125class egl_surface_t : public egl_object_t {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700126protected:
Jesse Hall25838592012-04-05 15:53:28 -0700127 ~egl_surface_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700128 void terminate() override;
Mathias Agopian5b287a62011-05-16 18:58:55 -0700129public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700130 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
131
Jesse Hallb29e5e82012-04-04 16:53:42 -0700132 egl_surface_t(egl_display_t* dpy, EGLConfig config,
133 EGLNativeWindowType win, EGLSurface surface,
Jesse Hall25838592012-04-05 15:53:28 -0700134 egl_connection_t const* cnx);
135
Mathias Agopian518ec112011-05-13 16:21:08 -0700136 EGLSurface surface;
137 EGLConfig config;
138 sp<ANativeWindow> win;
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 egl_connection_t const* cnx;
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700140private:
141 bool connected;
142 void disconnect();
Mathias Agopian518ec112011-05-13 16:21:08 -0700143};
144
Mathias Agopian5b287a62011-05-16 18:58:55 -0700145class egl_context_t: public egl_object_t {
146protected:
147 ~egl_context_t() {}
148public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
150
151 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopianada798b2012-02-13 17:09:30 -0800152 egl_connection_t const* cnx, int version);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800153
154 void onLooseCurrent();
155 void onMakeCurrent(EGLSurface draw, EGLSurface read);
156
Mathias Agopian518ec112011-05-13 16:21:08 -0700157 EGLDisplay dpy;
158 EGLContext context;
159 EGLConfig config;
160 EGLSurface read;
161 EGLSurface draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700162 egl_connection_t const* cnx;
163 int version;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800164 String8 gl_extensions;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700165 Vector<String8> tokenized_gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700166};
167
Mathias Agopian518ec112011-05-13 16:21:08 -0700168// ----------------------------------------------------------------------------
169
170typedef egl_surface_t::Ref SurfaceRef;
171typedef egl_context_t::Ref ContextRef;
Mathias Agopian518ec112011-05-13 16:21:08 -0700172
173// ----------------------------------------------------------------------------
174
175template<typename NATIVE, typename EGL>
176static inline NATIVE* egl_to_native_cast(EGL arg) {
177 return reinterpret_cast<NATIVE*>(arg);
178}
179
180static inline
181egl_surface_t* get_surface(EGLSurface surface) {
182 return egl_to_native_cast<egl_surface_t>(surface);
183}
184
185static inline
186egl_context_t* get_context(EGLContext context) {
187 return egl_to_native_cast<egl_context_t>(context);
188}
189
Mathias Agopian518ec112011-05-13 16:21:08 -0700190// ----------------------------------------------------------------------------
191}; // namespace android
192// ----------------------------------------------------------------------------
193
194#endif // ANDROID_EGL_OBJECT_H