blob: bda91bb28076ac681db72055801281e51973ebef [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
Mathias Agopian65421432017-03-08 11:49:05 -080024#include <string>
25#include <vector>
26
Mathias Agopian518ec112011-05-13 16:21:08 -070027#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include <system/window.h>
31
Mathias Agopian65421432017-03-08 11:49:05 -080032#include <log/log.h>
33
Mathias Agopian518ec112011-05-13 16:21:08 -070034#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 {
Pablo Ceballos1a5c4de2016-04-25 20:40:08 +000043 egl_display_t *display;
Jesse Hall3aa75f92016-05-20 10:47:07 -070044 mutable std::atomic_size_t count;
Mathias Agopian518ec112011-05-13 16:21:08 -070045
Mathias Agopian5b287a62011-05-16 18:58:55 -070046protected:
47 virtual ~egl_object_t();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -070048 virtual void terminate();
Mathias Agopian5b287a62011-05-16 18:58:55 -070049
Mathias Agopian518ec112011-05-13 16:21:08 -070050public:
Chih-Hung Hsiehe8761d62016-09-01 11:26:34 -070051 explicit 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
Jesse Hall3aa75f92016-05-20 10:47:07 -070054 inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
55 inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
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;
Mathias Agopian311b4792017-02-28 15:00:49 -080065 LocalRef() = delete;
66 LocalRef(const LocalRef* rhs) = delete;
Mathias Agopian5b287a62011-05-16 18:58:55 -070067 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();
Pablo Ceballosae8cf0b2016-05-02 11:24:13 -0700130 void terminate() override;
Mathias Agopian5b287a62011-05-16 18:58:55 -0700131public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700132 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
133
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600134 egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win, EGLSurface surface,
135 EGLint colorSpace, egl_connection_t const* cnx);
Jesse Hall25838592012-04-05 15:53:28 -0700136
Mathias Agopian65421432017-03-08 11:49:05 -0800137 ANativeWindow* getNativeWindow() { return win; }
138 ANativeWindow* getNativeWindow() const { return win; }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600139 EGLint getColorSpace() const { return colorSpace; }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700140 EGLBoolean setSmpte2086Attribute(EGLint attribute, EGLint value);
141 EGLBoolean setCta8613Attribute(EGLint attribute, EGLint value);
142 EGLBoolean getColorSpaceAttribute(EGLint attribute, EGLint* value) const;
143 EGLBoolean getSmpte2086Attribute(EGLint attribute, EGLint* value) const;
144 EGLBoolean getCta8613Attribute(EGLint attribute, EGLint* value) const;
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800145 const android_smpte2086_metadata getSmpte2086Metadata();
146 const android_cta861_3_metadata getCta8613Metadata();
Mathias Agopian65421432017-03-08 11:49:05 -0800147
148 // Try to keep the order of these fields and size unchanged. It's not public API, but
149 // it's not hard to imagine native games accessing them.
Mathias Agopian518ec112011-05-13 16:21:08 -0700150 EGLSurface surface;
151 EGLConfig config;
Mathias Agopian65421432017-03-08 11:49:05 -0800152private:
153 ANativeWindow* win;
154public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700155 egl_connection_t const* cnx;
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 };
179 egl_smpte2086_metadata egl_smpte2086_metadata;
180 egl_cta861_3_metadata egl_cta861_3_metadata;
Mathias Agopian518ec112011-05-13 16:21:08 -0700181};
182
Mathias Agopian5b287a62011-05-16 18:58:55 -0700183class egl_context_t: public egl_object_t {
184protected:
185 ~egl_context_t() {}
186public:
Mathias Agopian518ec112011-05-13 16:21:08 -0700187 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
188
189 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopianada798b2012-02-13 17:09:30 -0800190 egl_connection_t const* cnx, int version);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800191
192 void onLooseCurrent();
193 void onMakeCurrent(EGLSurface draw, EGLSurface read);
194
Mathias Agopian518ec112011-05-13 16:21:08 -0700195 EGLDisplay dpy;
196 EGLContext context;
197 EGLConfig config;
198 EGLSurface read;
199 EGLSurface draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700200 egl_connection_t const* cnx;
201 int version;
Mathias Agopian65421432017-03-08 11:49:05 -0800202 std::string gl_extensions;
203 std::vector<std::string> tokenized_gl_extensions;
Mathias Agopian518ec112011-05-13 16:21:08 -0700204};
205
Mathias Agopian518ec112011-05-13 16:21:08 -0700206// ----------------------------------------------------------------------------
207
208typedef egl_surface_t::Ref SurfaceRef;
209typedef egl_context_t::Ref ContextRef;
Mathias Agopian518ec112011-05-13 16:21:08 -0700210
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
Mathias Agopian518ec112011-05-13 16:21:08 -0700228// ----------------------------------------------------------------------------
229}; // namespace android
230// ----------------------------------------------------------------------------
231
232#endif // ANDROID_EGL_OBJECT_H