Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 4 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 10 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_EGL_OBJECT_H |
| 18 | #define ANDROID_EGL_OBJECT_H |
| 19 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 20 | #include <EGL/egl.h> |
| 21 | #include <EGL/eglext.h> |
| 22 | #include <log/log.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 23 | #include <stddef.h> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <system/window.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 26 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 27 | #include <atomic> |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | #include "egl_display.h" |
| 32 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 33 | namespace android { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 34 | |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 35 | class egl_display_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 36 | |
| 37 | class egl_object_t { |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 38 | egl_display_t* display; |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 39 | mutable std::atomic_size_t count; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 40 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 41 | protected: |
| 42 | virtual ~egl_object_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 43 | virtual void terminate(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 44 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 45 | public: |
Chih-Hung Hsieh | e8761d6 | 2016-09-01 11:26:34 -0700 | [diff] [blame] | 46 | explicit egl_object_t(egl_display_t* display); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 47 | void destroy(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 48 | |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 49 | 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 Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 51 | inline egl_display_t* getDisplay() const { return display; } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 54 | static bool get(egl_display_t const* display, egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 55 | |
| 56 | public: |
| 57 | template <typename N, typename T> |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 58 | class LocalRef { |
| 59 | egl_object_t* ref; |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 60 | LocalRef() = delete; |
| 61 | LocalRef(const LocalRef* rhs) = delete; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 62 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 63 | public: |
| 64 | ~LocalRef(); |
| 65 | explicit LocalRef(egl_object_t* rhs); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 66 | explicit LocalRef(egl_display_t const* display, T o) : ref(nullptr) { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 67 | egl_object_t* native = reinterpret_cast<N*>(o); |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 68 | if (o && egl_object_t::get(display, native)) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 69 | ref = native; |
| 70 | } |
| 71 | } |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 72 | inline N* get() { return static_cast<N*>(ref); } |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 73 | void acquire() const; |
| 74 | void release() const; |
| 75 | void terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 76 | }; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 77 | template <typename N, typename T> |
| 78 | friend class LocalRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 81 | template <typename N, typename T> |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 82 | egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) { |
| 83 | if (ref) { |
| 84 | ref->incRef(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | template <typename N, typename T> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 89 | egl_object_t::LocalRef<N, T>::~LocalRef() { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 90 | if (ref) { |
| 91 | ref->destroy(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | template <typename N, typename T> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 96 | void egl_object_t::LocalRef<N, T>::acquire() const { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 97 | if (ref) { |
| 98 | ref->incRef(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | template <typename N, typename T> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 103 | void egl_object_t::LocalRef<N, T>::release() const { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 104 | if (ref) { |
| 105 | if (ref->decRef() == 1) { |
| 106 | // shouldn't happen because this is called from LocalRef |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 107 | ALOGE("LocalRef::release() removed the last reference!"); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | template <typename N, typename T> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 113 | void egl_object_t::LocalRef<N, T>::terminate() { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 114 | if (ref) { |
| 115 | ref->terminate(); |
| 116 | } |
| 117 | } |
| 118 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 119 | // ---------------------------------------------------------------------------- |
| 120 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 121 | class egl_surface_t : public egl_object_t { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 122 | protected: |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 123 | ~egl_surface_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 124 | void terminate() override; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 125 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 126 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 127 | typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref; |
| 128 | |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 129 | egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win, EGLSurface surface, |
| 130 | EGLint colorSpace, egl_connection_t const* cnx); |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 131 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 132 | ANativeWindow* getNativeWindow() { return win; } |
| 133 | ANativeWindow* getNativeWindow() const { return win; } |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 134 | EGLint getColorSpace() const { return colorSpace; } |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 135 | 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 Goeltzenleuchter | 936799c | 2018-03-02 16:47:08 -0700 | [diff] [blame] | 140 | 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 Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 144 | |
| 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 147 | EGLSurface surface; |
| 148 | EGLConfig config; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 149 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 150 | private: |
| 151 | ANativeWindow* win; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 152 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 153 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 154 | egl_connection_t const* cnx; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 155 | |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 156 | private: |
| 157 | bool connected; |
| 158 | void disconnect(); |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 159 | EGLint colorSpace; |
Courtney Goeltzenleuchter | 786ab88 | 2018-01-26 13:37:33 -0800 | [diff] [blame] | 160 | |
| 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 Goeltzenleuchter | 936799c | 2018-03-02 16:47:08 -0700 | [diff] [blame] | 179 | |
| 180 | bool egl_smpte2086_dirty; |
| 181 | bool egl_cta861_3_dirty; |
| 182 | |
Courtney Goeltzenleuchter | 786ab88 | 2018-01-26 13:37:33 -0800 | [diff] [blame] | 183 | egl_smpte2086_metadata egl_smpte2086_metadata; |
| 184 | egl_cta861_3_metadata egl_cta861_3_metadata; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 187 | class egl_context_t : public egl_object_t { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 188 | protected: |
| 189 | ~egl_context_t() {} |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 190 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 191 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 192 | typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; |
| 193 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 194 | egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, egl_connection_t const* cnx, |
| 195 | int version); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 196 | |
| 197 | void onLooseCurrent(); |
| 198 | void onMakeCurrent(EGLSurface draw, EGLSurface read); |
| 199 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 200 | EGLDisplay dpy; |
| 201 | EGLContext context; |
| 202 | EGLConfig config; |
| 203 | EGLSurface read; |
| 204 | EGLSurface draw; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 205 | egl_connection_t const* cnx; |
| 206 | int version; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 207 | std::string gl_extensions; |
| 208 | std::vector<std::string> tokenized_gl_extensions; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 211 | typedef egl_surface_t::Ref SurfaceRef; |
| 212 | typedef egl_context_t::Ref ContextRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 213 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 214 | template <typename NATIVE, typename EGL> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 215 | static inline NATIVE* egl_to_native_cast(EGL arg) { |
| 216 | return reinterpret_cast<NATIVE*>(arg); |
| 217 | } |
| 218 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 219 | static inline egl_surface_t* get_surface(EGLSurface surface) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 220 | return egl_to_native_cast<egl_surface_t>(surface); |
| 221 | } |
| 222 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 223 | static inline egl_context_t* get_context(EGLContext context) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 224 | return egl_to_native_cast<egl_context_t>(context); |
| 225 | } |
| 226 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 227 | }; // namespace android |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 228 | |
| 229 | #endif // ANDROID_EGL_OBJECT_H |