blob: e17558cbd03f824a1fef994ddf6afeaff6bfc4bb [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_DISPLAY_H
18#define ANDROID_EGL_DISPLAY_H
19
20
21#include <ctype.h>
22#include <stdint.h>
23#include <stdlib.h>
24
25#include <EGL/egl.h>
26#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070027
Jesse Hallb29e5e82012-04-04 16:53:42 -070028#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029#include <utils/SortedVector.h>
30#include <utils/threads.h>
Mathias Agopian4b9511c2011-11-13 23:52:47 -080031#include <utils/String8.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032
Mathias Agopian1cadb252011-05-23 17:26:14 -070033#include "egldefs.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070034#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070035
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
40class egl_object_t;
Mathias Agopianfb87e542012-01-30 18:20:52 -080041class egl_context_t;
Bernhard Rosenkränzer9f425912014-11-17 21:12:15 +010042struct egl_connection_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070043
44// ----------------------------------------------------------------------------
45
Jamie Gennis98c63832011-11-07 17:03:54 -080046class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
Mathias Agopian518ec112011-05-13 16:21:08 -070047 static egl_display_t sDisplay[NUM_DISPLAYS];
48 EGLDisplay getDisplay(EGLNativeDisplayType display);
Mathias Agopiana4b2c042012-02-03 15:24:51 -080049 void loseCurrentImpl(egl_context_t * cur_c);
Mathias Agopian518ec112011-05-13 16:21:08 -070050
51public:
52 enum {
53 NOT_INITIALIZED = 0,
54 INITIALIZED = 1,
55 TERMINATED = 2
56 };
57
58 egl_display_t();
59 ~egl_display_t();
60
61 EGLBoolean initialize(EGLint *major, EGLint *minor);
62 EGLBoolean terminate();
63
64 // add object to this display's list
65 void addObject(egl_object_t* object);
Mathias Agopian5b287a62011-05-16 18:58:55 -070066 // remove object from this display's list
67 void removeObject(egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070068 // add reference to this object. returns true if this is a valid object.
Mathias Agopianf0480de2011-11-13 20:50:07 -080069 bool getObject(egl_object_t* object) const;
Mathias Agopian518ec112011-05-13 16:21:08 -070070
Mathias Agopian518ec112011-05-13 16:21:08 -070071 static egl_display_t* get(EGLDisplay dpy);
72 static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
73
Mathias Agopianfb87e542012-01-30 18:20:52 -080074 EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
75 EGLSurface draw, EGLSurface read, EGLContext ctx,
76 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
77 static void loseCurrent(egl_context_t * cur_c);
78
Mathias Agopian518ec112011-05-13 16:21:08 -070079 inline bool isReady() const { return (refs > 0); }
80 inline bool isValid() const { return magic == '_dpy'; }
81 inline bool isAlive() const { return isValid(); }
82
Mathias Agopian4b9511c2011-11-13 23:52:47 -080083 char const * getVendorString() const { return mVendorString.string(); }
84 char const * getVersionString() const { return mVersionString.string(); }
85 char const * getClientApiString() const { return mClientApiString.string(); }
86 char const * getExtensionString() const { return mExtensionString.string(); }
87
Jesse Hallc2e41222013-08-08 13:40:22 -070088 bool haveExtension(const char* name, size_t nameLen = 0) const;
89
Romain Guy4725e2c2011-11-09 20:10:18 -080090 inline uint32_t getRefsCount() const { return refs; }
91
Mathias Agopian518ec112011-05-13 16:21:08 -070092 struct strings_t {
93 char const * vendor;
94 char const * version;
95 char const * clientApi;
96 char const * extensions;
97 };
98
99 struct DisplayImpl {
Mathias Agopian7773c432012-02-13 20:06:08 -0800100 DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
Mathias Agopian518ec112011-05-13 16:21:08 -0700101 EGLDisplay dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700102 EGLint state;
Mathias Agopian518ec112011-05-13 16:21:08 -0700103 strings_t queryString;
104 };
105
106private:
107 uint32_t magic;
108
109public:
Mathias Agopianada798b2012-02-13 17:09:30 -0800110 DisplayImpl disp;
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700111 bool finishOnSwap; // property: debug.egl.finish
112 bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
Mathias Agopian518ec112011-05-13 16:21:08 -0700113
114private:
Jesse Hallb29e5e82012-04-04 16:53:42 -0700115 friend class egl_display_ptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700116
Mathias Agopianf0480de2011-11-13 20:50:07 -0800117 uint32_t refs;
Michael Lentine54466bc2015-01-27 09:01:03 -0800118 bool eglIsInitialized;
119 mutable Mutex lock, refLock;
120 mutable Condition refCond;
Mathias Agopianf0480de2011-11-13 20:50:07 -0800121 SortedVector<egl_object_t*> objects;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800122 String8 mVendorString;
123 String8 mVersionString;
124 String8 mClientApiString;
125 String8 mExtensionString;
Mathias Agopian518ec112011-05-13 16:21:08 -0700126};
127
128// ----------------------------------------------------------------------------
129
Jesse Hall25838592012-04-05 15:53:28 -0700130// An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
131// It doesn't refcount the egl_display_t, but does ensure that the underlying
132// EGL implementation is "awake" (not hibernating) and ready for use as long
133// as the egl_display_ptr exists.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700134class egl_display_ptr {
135public:
Dan Willemsene9cbb652016-10-11 12:46:38 -0700136 explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700137
138 // We only really need a C++11 move constructor, not a copy constructor.
139 // A move constructor would save an enter()/leave() pair on every EGL API
140 // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
Jesse Hall25838592012-04-05 15:53:28 -0700141 // can't use a move constructor until those are cleaned up.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700142 //
143 // egl_display_ptr(egl_display_ptr&& other) {
144 // mDpy = other.mDpy;
145 // other.mDpy = NULL;
146 // }
Jesse Hall25838592012-04-05 15:53:28 -0700147 //
Dan Willemsene9cbb652016-10-11 12:46:38 -0700148 egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700149
Dan Willemsene9cbb652016-10-11 12:46:38 -0700150 ~egl_display_ptr() {}
Jesse Hallb29e5e82012-04-04 16:53:42 -0700151
152 const egl_display_t* operator->() const { return mDpy; }
153 egl_display_t* operator->() { return mDpy; }
154
155 const egl_display_t* get() const { return mDpy; }
156 egl_display_t* get() { return mDpy; }
157
158 operator bool() const { return mDpy != NULL; }
159
160private:
161 egl_display_t* mDpy;
162
163 // non-assignable
164 egl_display_ptr& operator=(const egl_display_ptr&);
165};
166
167// ----------------------------------------------------------------------------
168
169inline egl_display_ptr get_display(EGLDisplay dpy) {
170 return egl_display_ptr(egl_display_t::get(dpy));
171}
172
173// Does not ensure EGL is unhibernated. Use with caution: calls into the
174// underlying EGL implementation are not safe.
175inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700176 return egl_display_t::get(dpy);
177}
178
179// ----------------------------------------------------------------------------
180
Jesse Hallb29e5e82012-04-04 16:53:42 -0700181egl_display_ptr validate_display(EGLDisplay dpy);
182egl_display_ptr validate_display_connection(EGLDisplay dpy,
183 egl_connection_t*& cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700184EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
185EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
186
187// ----------------------------------------------------------------------------
188}; // namespace android
189// ----------------------------------------------------------------------------
190
191#endif // ANDROID_EGL_DISPLAY_H