blob: f452969a1751ac0d625a2a50551f7c853f3dbe80 [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
Steven Moreland113c6b72017-03-10 10:08:45 -080020#include <atomic>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdint.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080022#include <stddef.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070023
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070026
Mathias Agopian311b4792017-02-28 15:00:49 -080027#include <utils/StrongPointer.h>
Mathias Agopian48d438d2012-01-28 21:44:00 -080028#include <utils/String8.h>
Alistair Strachanedfe72e2015-05-22 14:10:09 -070029#include <utils/Vector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070030
31#include <system/window.h>
32
33#include "egl_display.h"
34
35// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
Dan Alberteacd31f2016-02-02 15:08:34 -080039class egl_display_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070040
41class egl_object_t {
Pablo Ceballos1a5c4de2016-04-25 20:40:08 +000042 egl_display_t *display;
Jesse Hall3aa75f92016-05-20 10:47:07 -070043 mutable std::atomic_size_t count;
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Mathias Agopian5b287a62011-05-16 18:58:55 -070045protected:
46 virtual ~egl_object_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -070047 virtual void terminate();
Mathias Agopian5b287a62011-05-16 18:58:55 -070048
Mathias Agopian518ec112011-05-13 16:21:08 -070049public:
Chih-Hung Hsiehe8761d62016-09-01 11:26:34 -070050 explicit 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
Jesse Hall3aa75f92016-05-20 10:47:07 -070053 inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
54 inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
Mathias Agopianf0480de2011-11-13 20:50:07 -080055 inline egl_display_t* getDisplay() const { return display; }
Mathias Agopian518ec112011-05-13 16:21:08 -070056
57private:
Mathias Agopianf0480de2011-11-13 20:50:07 -080058 static bool get(egl_display_t const* display, egl_object_t* object);
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;
Mathias Agopian311b4792017-02-28 15:00:49 -080064 LocalRef() = delete;
65 LocalRef(const LocalRef* rhs) = delete;
Mathias Agopian5b287a62011-05-16 18:58:55 -070066 public:
67 ~LocalRef();
68 explicit LocalRef(egl_object_t* rhs);
Mathias Agopianf0480de2011-11-13 20:50:07 -080069 explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
Mathias Agopian5b287a62011-05-16 18:58:55 -070070 egl_object_t* native = reinterpret_cast<N*>(o);
Mathias Agopianf0480de2011-11-13 20:50:07 -080071 if (o && egl_object_t::get(display, native)) {
Mathias Agopian518ec112011-05-13 16:21:08 -070072 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
Steve Blocke6f43dd2012-01-06 19:20:56 +0000112 ALOGE("LocalRef::release() removed the last reference!");
Mathias Agopian5b287a62011-05-16 18:58:55 -0700113 }
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 Agopianada798b2012-02-13 17:09:30 -0800126class egl_surface_t : public egl_object_t {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700127protected:
Jesse Hall25838592012-04-05 15:53:28 -0700128 ~egl_surface_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700129 void terminate() override;
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;
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700141private:
142 bool connected;
143 void disconnect();
Mathias Agopian518ec112011-05-13 16:21:08 -0700144};
145
Mathias Agopian5b287a62011-05-16 18:58:55 -0700146class egl_context_t: public egl_object_t {
147protected:
148 ~egl_context_t() {}
149public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700150 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
151
152 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopianada798b2012-02-13 17:09:30 -0800153 egl_connection_t const* cnx, int version);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800154
155 void onLooseCurrent();
156 void onMakeCurrent(EGLSurface draw, EGLSurface read);
157
Mathias Agopian518ec112011-05-13 16:21:08 -0700158 EGLDisplay dpy;
159 EGLContext context;
160 EGLConfig config;
161 EGLSurface read;
162 EGLSurface draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700163 egl_connection_t const* cnx;
164 int version;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800165 String8 gl_extensions;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700166 Vector<String8> tokenized_gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700167};
168
Mathias Agopian518ec112011-05-13 16:21:08 -0700169// ----------------------------------------------------------------------------
170
171typedef egl_surface_t::Ref SurfaceRef;
172typedef egl_context_t::Ref ContextRef;
Mathias Agopian518ec112011-05-13 16:21:08 -0700173
174// ----------------------------------------------------------------------------
175
176template<typename NATIVE, typename EGL>
177static inline NATIVE* egl_to_native_cast(EGL arg) {
178 return reinterpret_cast<NATIVE*>(arg);
179}
180
181static inline
182egl_surface_t* get_surface(EGLSurface surface) {
183 return egl_to_native_cast<egl_surface_t>(surface);
184}
185
186static inline
187egl_context_t* get_context(EGLContext context) {
188 return egl_to_native_cast<egl_context_t>(context);
189}
190
Mathias Agopian518ec112011-05-13 16:21:08 -0700191// ----------------------------------------------------------------------------
192}; // namespace android
193// ----------------------------------------------------------------------------
194
195#endif // ANDROID_EGL_OBJECT_H