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 | |
Steven Moreland | 113c6b7 | 2017-03-10 10:08:45 -0800 | [diff] [blame] | 20 | #include <atomic> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 22 | #include <stddef.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 23 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 27 | #include <EGL/egl.h> |
| 28 | #include <EGL/eglext.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 29 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 30 | #include <system/window.h> |
| 31 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 32 | #include <log/log.h> |
| 33 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 34 | #include "egl_display.h" |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | namespace android { |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 40 | class egl_display_t; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 41 | |
| 42 | class egl_object_t { |
Pablo Ceballos | 1a5c4de | 2016-04-25 20:40:08 +0000 | [diff] [blame] | 43 | egl_display_t *display; |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 44 | mutable std::atomic_size_t count; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 46 | protected: |
| 47 | virtual ~egl_object_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 48 | virtual void terminate(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 49 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 50 | public: |
Chih-Hung Hsieh | e8761d6 | 2016-09-01 11:26:34 -0700 | [diff] [blame] | 51 | explicit egl_object_t(egl_display_t* display); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 52 | void destroy(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 53 | |
Jesse Hall | 3aa75f9 | 2016-05-20 10:47:07 -0700 | [diff] [blame] | 54 | 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 Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 56 | inline egl_display_t* getDisplay() const { return display; } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 59 | static bool get(egl_display_t const* display, egl_object_t* object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 60 | |
| 61 | public: |
| 62 | template <typename N, typename T> |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 63 | class LocalRef { |
| 64 | egl_object_t* ref; |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 65 | LocalRef() = delete; |
| 66 | LocalRef(const LocalRef* rhs) = delete; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 67 | public: |
| 68 | ~LocalRef(); |
| 69 | explicit LocalRef(egl_object_t* rhs); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 70 | explicit LocalRef(egl_display_t const* display, T o) : ref(nullptr) { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 71 | egl_object_t* native = reinterpret_cast<N*>(o); |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 72 | if (o && egl_object_t::get(display, native)) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 73 | ref = native; |
| 74 | } |
| 75 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 76 | inline N* get() { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 77 | return static_cast<N*>(ref); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 78 | } |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 79 | void acquire() const; |
| 80 | void release() const; |
| 81 | void terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 82 | }; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 83 | template <typename N, typename T> |
| 84 | friend class LocalRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 87 | template<typename N, typename T> |
| 88 | egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) { |
| 89 | if (ref) { |
| 90 | ref->incRef(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | template <typename N, typename T> |
| 95 | egl_object_t::LocalRef<N,T>::~LocalRef() { |
| 96 | if (ref) { |
| 97 | ref->destroy(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | template <typename N, typename T> |
| 102 | void egl_object_t::LocalRef<N,T>::acquire() const { |
| 103 | if (ref) { |
| 104 | ref->incRef(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename N, typename T> |
| 109 | void 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 Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 113 | ALOGE("LocalRef::release() removed the last reference!"); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | template <typename N, typename T> |
| 119 | void egl_object_t::LocalRef<N,T>::terminate() { |
| 120 | if (ref) { |
| 121 | ref->terminate(); |
| 122 | } |
| 123 | } |
| 124 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 125 | // ---------------------------------------------------------------------------- |
| 126 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 127 | class egl_surface_t : public egl_object_t { |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 128 | protected: |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 129 | ~egl_surface_t(); |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 130 | void terminate() override; |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 131 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 132 | typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref; |
| 133 | |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 134 | egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win, EGLSurface surface, |
| 135 | EGLint colorSpace, egl_connection_t const* cnx); |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 136 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 137 | ANativeWindow* getNativeWindow() { return win; } |
| 138 | ANativeWindow* getNativeWindow() const { return win; } |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 139 | EGLint getColorSpace() const { return colorSpace; } |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 140 | 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 Goeltzenleuchter | 936799c | 2018-03-02 16:47:08 -0700 | [diff] [blame] | 145 | EGLBoolean getSmpte2086Metadata(android_smpte2086_metadata& smpte2086) const; |
| 146 | EGLBoolean getCta8613Metadata(android_cta861_3_metadata& cta861_3) const; |
| 147 | void resetSmpte2086Metadata() { egl_smpte2086_dirty = false; } |
| 148 | void resetCta8613Metadata() { egl_cta861_3_dirty = false; } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 149 | |
| 150 | // Try to keep the order of these fields and size unchanged. It's not public API, but |
| 151 | // it's not hard to imagine native games accessing them. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 152 | EGLSurface surface; |
| 153 | EGLConfig config; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 154 | private: |
| 155 | ANativeWindow* win; |
| 156 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 157 | egl_connection_t const* cnx; |
Pablo Ceballos | ae8cf0b | 2016-05-02 11:24:13 -0700 | [diff] [blame] | 158 | private: |
| 159 | bool connected; |
| 160 | void disconnect(); |
Courtney Goeltzenleuchter | 3783657 | 2017-04-26 15:07:51 -0600 | [diff] [blame] | 161 | EGLint colorSpace; |
Courtney Goeltzenleuchter | 786ab88 | 2018-01-26 13:37:33 -0800 | [diff] [blame] | 162 | |
| 163 | struct egl_xy_color { |
| 164 | EGLint x; |
| 165 | EGLint y; |
| 166 | }; |
| 167 | |
| 168 | struct egl_smpte2086_metadata { |
| 169 | struct egl_xy_color displayPrimaryRed; |
| 170 | struct egl_xy_color displayPrimaryGreen; |
| 171 | struct egl_xy_color displayPrimaryBlue; |
| 172 | struct egl_xy_color whitePoint; |
| 173 | EGLint maxLuminance; |
| 174 | EGLint minLuminance; |
| 175 | }; |
| 176 | |
| 177 | struct egl_cta861_3_metadata { |
| 178 | EGLint maxContentLightLevel; |
| 179 | EGLint maxFrameAverageLightLevel; |
| 180 | }; |
Courtney Goeltzenleuchter | 936799c | 2018-03-02 16:47:08 -0700 | [diff] [blame] | 181 | |
| 182 | bool egl_smpte2086_dirty; |
| 183 | bool egl_cta861_3_dirty; |
| 184 | |
Courtney Goeltzenleuchter | 786ab88 | 2018-01-26 13:37:33 -0800 | [diff] [blame] | 185 | egl_smpte2086_metadata egl_smpte2086_metadata; |
| 186 | egl_cta861_3_metadata egl_cta861_3_metadata; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 189 | class egl_context_t: public egl_object_t { |
| 190 | protected: |
| 191 | ~egl_context_t() {} |
| 192 | public: |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 193 | typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; |
| 194 | |
| 195 | egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 196 | egl_connection_t const* cnx, int version); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 197 | |
| 198 | void onLooseCurrent(); |
| 199 | void onMakeCurrent(EGLSurface draw, EGLSurface read); |
| 200 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 201 | EGLDisplay dpy; |
| 202 | EGLContext context; |
| 203 | EGLConfig config; |
| 204 | EGLSurface read; |
| 205 | EGLSurface draw; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 206 | egl_connection_t const* cnx; |
| 207 | int version; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 208 | std::string gl_extensions; |
| 209 | std::vector<std::string> tokenized_gl_extensions; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 212 | // ---------------------------------------------------------------------------- |
| 213 | |
| 214 | typedef egl_surface_t::Ref SurfaceRef; |
| 215 | typedef egl_context_t::Ref ContextRef; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 216 | |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | |
| 219 | template<typename NATIVE, typename EGL> |
| 220 | static inline NATIVE* egl_to_native_cast(EGL arg) { |
| 221 | return reinterpret_cast<NATIVE*>(arg); |
| 222 | } |
| 223 | |
| 224 | static inline |
| 225 | egl_surface_t* get_surface(EGLSurface surface) { |
| 226 | return egl_to_native_cast<egl_surface_t>(surface); |
| 227 | } |
| 228 | |
| 229 | static inline |
| 230 | egl_context_t* get_context(EGLContext context) { |
| 231 | return egl_to_native_cast<egl_context_t>(context); |
| 232 | } |
| 233 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 234 | // ---------------------------------------------------------------------------- |
| 235 | }; // namespace android |
| 236 | // ---------------------------------------------------------------------------- |
| 237 | |
| 238 | #endif // ANDROID_EGL_OBJECT_H |