| 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_DISPLAY_H | 
|  | 18 | #define ANDROID_EGL_DISPLAY_H | 
|  | 19 |  | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 20 | #include <EGL/egl.h> | 
|  | 21 | #include <EGL/eglext.h> | 
| Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 22 | #include <stddef.h> | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 23 | #include <stdint.h> | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 24 |  | 
| Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 25 | #include <condition_variable> | 
| Woody Chow | a9550f3 | 2020-08-31 14:34:12 +0900 | [diff] [blame] | 26 | #include <map> | 
|  | 27 | #include <memory> | 
| Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 28 | #include <mutex> | 
|  | 29 | #include <string> | 
|  | 30 | #include <unordered_set> | 
|  | 31 |  | 
| Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 32 | #include "../hooks.h" | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 33 | #include "egldefs.h" | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 34 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 35 | namespace android { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | class egl_object_t; | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 38 | class egl_context_t; | 
| Bernhard Rosenkränzer | 9f42591 | 2014-11-17 21:12:15 +0100 | [diff] [blame] | 39 | struct egl_connection_t; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 40 |  | 
| Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 41 | bool findExtension(const char* exts, const char* name, size_t nameLen = 0); | 
| Krzysztof Kosiński | dbccd22 | 2019-05-16 14:10:25 -0700 | [diff] [blame] | 42 | bool needsAndroidPEglMitigation(); | 
| Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 43 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 44 | class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes | 
| Woody Chow | a9550f3 | 2020-08-31 14:34:12 +0900 | [diff] [blame] | 45 | static std::map<EGLDisplay, std::unique_ptr<egl_display_t>> displayMap; | 
|  | 46 | static std::mutex displayMapLock; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 47 | EGLDisplay getDisplay(EGLNativeDisplayType display); | 
| Woody Chow | a9550f3 | 2020-08-31 14:34:12 +0900 | [diff] [blame] | 48 | static EGLDisplay getPlatformDisplay(EGLNativeDisplayType display, | 
|  | 49 | const EGLAttrib* attrib_list); | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 50 | void loseCurrentImpl(egl_context_t* cur_c); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | public: | 
|  | 53 | enum { | 
|  | 54 | NOT_INITIALIZED = 0, | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 55 | INITIALIZED = 1, | 
|  | 56 | TERMINATED = 2, | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 57 | }; | 
|  | 58 |  | 
|  | 59 | egl_display_t(); | 
|  | 60 | ~egl_display_t(); | 
|  | 61 |  | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 62 | EGLBoolean initialize(EGLint* major, EGLint* minor); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 63 | EGLBoolean terminate(); | 
|  | 64 |  | 
|  | 65 | // add object to this display's list | 
|  | 66 | void addObject(egl_object_t* object); | 
| Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 67 | // remove object from this display's list | 
|  | 68 | void removeObject(egl_object_t* object); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 69 | // add reference to this object. returns true if this is a valid object. | 
| Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 70 | bool getObject(egl_object_t* object) const; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 71 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 72 | static egl_display_t* get(EGLDisplay dpy); | 
| Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 73 | static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp, const EGLAttrib* attrib_list); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 74 |  | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 75 | EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw, EGLSurface read, | 
|  | 76 | EGLContext ctx, EGLSurface impl_draw, EGLSurface impl_read, | 
|  | 77 | EGLContext impl_ctx); | 
|  | 78 | static void loseCurrent(egl_context_t* cur_c); | 
| Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 79 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 80 | inline bool isReady() const { return (refs > 0); } | 
|  | 81 | inline bool isValid() const { return magic == '_dpy'; } | 
|  | 82 | inline bool isAlive() const { return isValid(); } | 
|  | 83 |  | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 84 | char const* getVendorString() const { return mVendorString.c_str(); } | 
|  | 85 | char const* getVersionString() const { return mVersionString.c_str(); } | 
|  | 86 | char const* getClientApiString() const { return mClientApiString.c_str(); } | 
|  | 87 | char const* getExtensionString() const { return mExtensionString.c_str(); } | 
| Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 88 |  | 
| Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 89 | bool haveExtension(const char* name, size_t nameLen = 0) const; | 
|  | 90 |  | 
| Romain Guy | 4725e2c | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 91 | inline uint32_t getRefsCount() const { return refs; } | 
|  | 92 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 93 | struct strings_t { | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 94 | char const* vendor; | 
|  | 95 | char const* version; | 
|  | 96 | char const* clientApi; | 
|  | 97 | char const* extensions; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
|  | 100 | struct DisplayImpl { | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 101 | DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) {} | 
|  | 102 | EGLDisplay dpy; | 
|  | 103 | EGLint state; | 
|  | 104 | strings_t queryString; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 105 | }; | 
|  | 106 |  | 
|  | 107 | private: | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 108 | uint32_t magic; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | public: | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 111 | DisplayImpl disp; | 
|  | 112 | bool finishOnSwap;       // property: debug.egl.finish | 
|  | 113 | bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion | 
|  | 114 | bool hasColorSpaceSupport; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | private: | 
| Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 117 | uint32_t refs; | 
|  | 118 | bool eglIsInitialized; | 
|  | 119 | mutable std::mutex lock; | 
|  | 120 | mutable std::mutex refLock; | 
|  | 121 | mutable std::condition_variable refCond; | 
|  | 122 | std::unordered_set<egl_object_t*> objects; | 
|  | 123 | std::string mVendorString; | 
|  | 124 | std::string mVersionString; | 
|  | 125 | std::string mClientApiString; | 
|  | 126 | std::string mExtensionString; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 127 | }; | 
|  | 128 |  | 
| Yiwei Zhang | 0657fba | 2020-08-12 19:09:26 -0700 | [diff] [blame] | 129 | inline egl_display_t* get_display(EGLDisplay dpy) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 130 | return egl_display_t::get(dpy); | 
|  | 131 | } | 
|  | 132 |  | 
| Yiwei Zhang | 0657fba | 2020-08-12 19:09:26 -0700 | [diff] [blame] | 133 | egl_display_t* validate_display(EGLDisplay dpy); | 
|  | 134 | egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 135 | EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx); | 
|  | 136 | EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface); | 
|  | 137 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 138 | }; // namespace android | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 139 |  | 
|  | 140 | #endif // ANDROID_EGL_DISPLAY_H |