blob: e593b1cf5b5959b1d9b9b72d154558d21f4b3635 [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
Yiwei Zhang8af03062020-08-12 21:28:15 -070020#include <EGL/egl.h>
21#include <EGL/eglext.h>
22#include <log/log.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080023#include <stddef.h>
Yiwei Zhang8af03062020-08-12 21:28:15 -070024#include <stdint.h>
25#include <system/window.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070026
Yiwei Zhang8af03062020-08-12 21:28:15 -070027#include <atomic>
Mathias Agopian65421432017-03-08 11:49:05 -080028#include <string>
29#include <vector>
30
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include "egl_display.h"
32
Mathias Agopian518ec112011-05-13 16:21:08 -070033namespace android {
Mathias Agopian518ec112011-05-13 16:21:08 -070034
Dan Alberteacd31f2016-02-02 15:08:34 -080035class egl_display_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070036
37class egl_object_t {
Yiwei Zhang8af03062020-08-12 21:28:15 -070038 egl_display_t* display;
Jesse Hall3aa75f92016-05-20 10:47:07 -070039 mutable std::atomic_size_t count;
Mathias Agopian518ec112011-05-13 16:21:08 -070040
Mathias Agopian5b287a62011-05-16 18:58:55 -070041protected:
42 virtual ~egl_object_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -070043 virtual void terminate();
Mathias Agopian5b287a62011-05-16 18:58:55 -070044
Mathias Agopian518ec112011-05-13 16:21:08 -070045public:
Chih-Hung Hsiehe8761d62016-09-01 11:26:34 -070046 explicit egl_object_t(egl_display_t* display);
Mathias Agopian5b287a62011-05-16 18:58:55 -070047 void destroy();
Mathias Agopian518ec112011-05-13 16:21:08 -070048
Jesse Hall3aa75f92016-05-20 10:47:07 -070049 inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
50 inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
Mathias Agopianf0480de2011-11-13 20:50:07 -080051 inline egl_display_t* getDisplay() const { return display; }
Mathias Agopian518ec112011-05-13 16:21:08 -070052
53private:
Mathias Agopianf0480de2011-11-13 20:50:07 -080054 static bool get(egl_display_t const* display, egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070055
56public:
57 template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070058 class LocalRef {
59 egl_object_t* ref;
Mathias Agopian311b4792017-02-28 15:00:49 -080060 LocalRef() = delete;
61 LocalRef(const LocalRef* rhs) = delete;
Yiwei Zhang8af03062020-08-12 21:28:15 -070062
Mathias Agopian5b287a62011-05-16 18:58:55 -070063 public:
64 ~LocalRef();
65 explicit LocalRef(egl_object_t* rhs);
Yi Kong48a6cd22018-07-18 10:07:09 -070066 explicit LocalRef(egl_display_t const* display, T o) : ref(nullptr) {
Mathias Agopian5b287a62011-05-16 18:58:55 -070067 egl_object_t* native = reinterpret_cast<N*>(o);
Mathias Agopianf0480de2011-11-13 20:50:07 -080068 if (o && egl_object_t::get(display, native)) {
Mathias Agopian518ec112011-05-13 16:21:08 -070069 ref = native;
70 }
71 }
Yiwei Zhang8af03062020-08-12 21:28:15 -070072 inline N* get() { return static_cast<N*>(ref); }
Mathias Agopian5b287a62011-05-16 18:58:55 -070073 void acquire() const;
74 void release() const;
75 void terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070076 };
Mathias Agopian5b287a62011-05-16 18:58:55 -070077 template <typename N, typename T>
78 friend class LocalRef;
Mathias Agopian518ec112011-05-13 16:21:08 -070079};
80
Yiwei Zhang8af03062020-08-12 21:28:15 -070081template <typename N, typename T>
Mathias Agopian5b287a62011-05-16 18:58:55 -070082egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
83 if (ref) {
84 ref->incRef();
85 }
86}
87
88template <typename N, typename T>
Yiwei Zhang8af03062020-08-12 21:28:15 -070089egl_object_t::LocalRef<N, T>::~LocalRef() {
Mathias Agopian5b287a62011-05-16 18:58:55 -070090 if (ref) {
91 ref->destroy();
92 }
93}
94
95template <typename N, typename T>
Yiwei Zhang8af03062020-08-12 21:28:15 -070096void egl_object_t::LocalRef<N, T>::acquire() const {
Mathias Agopian5b287a62011-05-16 18:58:55 -070097 if (ref) {
98 ref->incRef();
99 }
100}
101
102template <typename N, typename T>
Yiwei Zhang8af03062020-08-12 21:28:15 -0700103void egl_object_t::LocalRef<N, T>::release() const {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700104 if (ref) {
105 if (ref->decRef() == 1) {
106 // shouldn't happen because this is called from LocalRef
Steve Blocke6f43dd2012-01-06 19:20:56 +0000107 ALOGE("LocalRef::release() removed the last reference!");
Mathias Agopian5b287a62011-05-16 18:58:55 -0700108 }
109 }
110}
111
112template <typename N, typename T>
Yiwei Zhang8af03062020-08-12 21:28:15 -0700113void egl_object_t::LocalRef<N, T>::terminate() {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700114 if (ref) {
115 ref->terminate();
116 }
117}
118
Mathias Agopian518ec112011-05-13 16:21:08 -0700119// ----------------------------------------------------------------------------
120
Mathias Agopianada798b2012-02-13 17:09:30 -0800121class egl_surface_t : public egl_object_t {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700122protected:
Jesse Hall25838592012-04-05 15:53:28 -0700123 ~egl_surface_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700124 void terminate() override;
Yiwei Zhang8af03062020-08-12 21:28:15 -0700125
Mathias Agopian5b287a62011-05-16 18:58:55 -0700126public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700127 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
128
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600129 egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win, EGLSurface surface,
130 EGLint colorSpace, egl_connection_t const* cnx);
Jesse Hall25838592012-04-05 15:53:28 -0700131
Mathias Agopian65421432017-03-08 11:49:05 -0800132 ANativeWindow* getNativeWindow() { return win; }
133 ANativeWindow* getNativeWindow() const { return win; }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600134 EGLint getColorSpace() const { return colorSpace; }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700135 EGLBoolean setSmpte2086Attribute(EGLint attribute, EGLint value);
136 EGLBoolean setCta8613Attribute(EGLint attribute, EGLint value);
137 EGLBoolean getColorSpaceAttribute(EGLint attribute, EGLint* value) const;
138 EGLBoolean getSmpte2086Attribute(EGLint attribute, EGLint* value) const;
139 EGLBoolean getCta8613Attribute(EGLint attribute, EGLint* value) const;
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700140 EGLBoolean getSmpte2086Metadata(android_smpte2086_metadata& smpte2086) const;
141 EGLBoolean getCta8613Metadata(android_cta861_3_metadata& cta861_3) const;
142 void resetSmpte2086Metadata() { egl_smpte2086_dirty = false; }
143 void resetCta8613Metadata() { egl_cta861_3_dirty = false; }
Mathias Agopian65421432017-03-08 11:49:05 -0800144
145 // Try to keep the order of these fields and size unchanged. It's not public API, but
146 // it's not hard to imagine native games accessing them.
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 EGLSurface surface;
148 EGLConfig config;
Yiwei Zhang8af03062020-08-12 21:28:15 -0700149
Mathias Agopian65421432017-03-08 11:49:05 -0800150private:
151 ANativeWindow* win;
Yiwei Zhang8af03062020-08-12 21:28:15 -0700152
Mathias Agopian65421432017-03-08 11:49:05 -0800153public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 egl_connection_t const* cnx;
Yiwei Zhang8af03062020-08-12 21:28:15 -0700155
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700156private:
157 bool connected;
158 void disconnect();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600159 EGLint colorSpace;
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800160
161 struct egl_xy_color {
162 EGLint x;
163 EGLint y;
164 };
165
166 struct egl_smpte2086_metadata {
167 struct egl_xy_color displayPrimaryRed;
168 struct egl_xy_color displayPrimaryGreen;
169 struct egl_xy_color displayPrimaryBlue;
170 struct egl_xy_color whitePoint;
171 EGLint maxLuminance;
172 EGLint minLuminance;
173 };
174
175 struct egl_cta861_3_metadata {
176 EGLint maxContentLightLevel;
177 EGLint maxFrameAverageLightLevel;
178 };
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700179
180 bool egl_smpte2086_dirty;
181 bool egl_cta861_3_dirty;
182
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800183 egl_smpte2086_metadata egl_smpte2086_metadata;
184 egl_cta861_3_metadata egl_cta861_3_metadata;
Mathias Agopian518ec112011-05-13 16:21:08 -0700185};
186
Yiwei Zhang8af03062020-08-12 21:28:15 -0700187class egl_context_t : public egl_object_t {
Mathias Agopian5b287a62011-05-16 18:58:55 -0700188protected:
189 ~egl_context_t() {}
Yiwei Zhang8af03062020-08-12 21:28:15 -0700190
Mathias Agopian5b287a62011-05-16 18:58:55 -0700191public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700192 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
193
Yiwei Zhang8af03062020-08-12 21:28:15 -0700194 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, egl_connection_t const* cnx,
195 int version);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800196
197 void onLooseCurrent();
198 void onMakeCurrent(EGLSurface draw, EGLSurface read);
199
Mathias Agopian518ec112011-05-13 16:21:08 -0700200 EGLDisplay dpy;
201 EGLContext context;
202 EGLConfig config;
203 EGLSurface read;
204 EGLSurface draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700205 egl_connection_t const* cnx;
206 int version;
Mathias Agopian65421432017-03-08 11:49:05 -0800207 std::string gl_extensions;
208 std::vector<std::string> tokenized_gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700209};
210
Yiwei Zhang8af03062020-08-12 21:28:15 -0700211typedef egl_surface_t::Ref SurfaceRef;
212typedef egl_context_t::Ref ContextRef;
Mathias Agopian518ec112011-05-13 16:21:08 -0700213
Yiwei Zhang8af03062020-08-12 21:28:15 -0700214template <typename NATIVE, typename EGL>
Mathias Agopian518ec112011-05-13 16:21:08 -0700215static inline NATIVE* egl_to_native_cast(EGL arg) {
216 return reinterpret_cast<NATIVE*>(arg);
217}
218
Yiwei Zhang8af03062020-08-12 21:28:15 -0700219static inline egl_surface_t* get_surface(EGLSurface surface) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700220 return egl_to_native_cast<egl_surface_t>(surface);
221}
222
Yiwei Zhang8af03062020-08-12 21:28:15 -0700223static inline egl_context_t* get_context(EGLContext context) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700224 return egl_to_native_cast<egl_context_t>(context);
225}
226
Mathias Agopian518ec112011-05-13 16:21:08 -0700227}; // namespace android
Mathias Agopian518ec112011-05-13 16:21:08 -0700228
229#endif // ANDROID_EGL_OBJECT_H