blob: 4949ef212f48248d90f2194267b939eb50713809 [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;
Mathias Agopian518ec112011-05-13 16:21:08 -070042class egl_connection_t;
43
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
Jesse Hall25838592012-04-05 15:53:28 -070071 // These notifications allow the display to keep track of how many window
72 // surfaces exist, which it uses to decide whether to hibernate the
73 // underlying EGL implementation. They can be called by any thread without
74 // holding a lock, but must be called via egl_display_ptr to ensure
75 // proper hibernate/wakeup sequencing. If a surface destruction triggers
76 // hibernation, hibernation will be delayed at least until the calling
77 // thread's egl_display_ptr is destroyed.
Jesse Halla0fef1c2012-04-17 12:02:26 -070078 void onWindowSurfaceCreated() {
79 mHibernation.incWakeCount(HibernationMachine::STRONG);
80 }
81 void onWindowSurfaceDestroyed() {
82 mHibernation.decWakeCount(HibernationMachine::STRONG);
83 }
Jesse Hall25838592012-04-05 15:53:28 -070084
Mathias Agopian518ec112011-05-13 16:21:08 -070085 static egl_display_t* get(EGLDisplay dpy);
86 static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
87
Mathias Agopianfb87e542012-01-30 18:20:52 -080088 EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
89 EGLSurface draw, EGLSurface read, EGLContext ctx,
90 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
91 static void loseCurrent(egl_context_t * cur_c);
92
Mathias Agopian518ec112011-05-13 16:21:08 -070093 inline bool isReady() const { return (refs > 0); }
94 inline bool isValid() const { return magic == '_dpy'; }
95 inline bool isAlive() const { return isValid(); }
96
Mathias Agopian4b9511c2011-11-13 23:52:47 -080097 char const * getVendorString() const { return mVendorString.string(); }
98 char const * getVersionString() const { return mVersionString.string(); }
99 char const * getClientApiString() const { return mClientApiString.string(); }
100 char const * getExtensionString() const { return mExtensionString.string(); }
101
Jesse Hallc2e41222013-08-08 13:40:22 -0700102 bool haveExtension(const char* name, size_t nameLen = 0) const;
103
Romain Guy4725e2c2011-11-09 20:10:18 -0800104 inline uint32_t getRefsCount() const { return refs; }
105
Mathias Agopian518ec112011-05-13 16:21:08 -0700106 struct strings_t {
107 char const * vendor;
108 char const * version;
109 char const * clientApi;
110 char const * extensions;
111 };
112
113 struct DisplayImpl {
Mathias Agopian7773c432012-02-13 20:06:08 -0800114 DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
Mathias Agopian518ec112011-05-13 16:21:08 -0700115 EGLDisplay dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700116 EGLint state;
Mathias Agopian518ec112011-05-13 16:21:08 -0700117 strings_t queryString;
118 };
119
120private:
121 uint32_t magic;
122
123public:
Mathias Agopianada798b2012-02-13 17:09:30 -0800124 DisplayImpl disp;
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700125 bool finishOnSwap; // property: debug.egl.finish
126 bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
Mathias Agopian518ec112011-05-13 16:21:08 -0700127
128private:
Jesse Hallb29e5e82012-04-04 16:53:42 -0700129 friend class egl_display_ptr;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700130 bool enter() { return mHibernation.incWakeCount(HibernationMachine::WEAK); }
131 void leave() { return mHibernation.decWakeCount(HibernationMachine::WEAK); }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700132
Mathias Agopianf0480de2011-11-13 20:50:07 -0800133 uint32_t refs;
Michael Lentine54466bc2015-01-27 09:01:03 -0800134 bool eglIsInitialized;
135 mutable Mutex lock, refLock;
136 mutable Condition refCond;
Mathias Agopianf0480de2011-11-13 20:50:07 -0800137 SortedVector<egl_object_t*> objects;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800138 String8 mVendorString;
139 String8 mVersionString;
140 String8 mClientApiString;
141 String8 mExtensionString;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700142
143 // HibernationMachine uses its own internal mutex to protect its own data.
144 // The owning egl_display_t's lock may be but is not required to be held
145 // when calling HibernationMachine methods. As a result, nothing in this
146 // class may call back up to egl_display_t directly or indirectly.
147 class HibernationMachine {
148 public:
149 // STRONG refs cancel (inc) or initiate (dec) a hibernation attempt
150 // the next time the wakecount reaches zero. WEAK refs don't affect
151 // whether a hibernation attempt will be made. Use STRONG refs only
152 // for infrequent/heavy changes that are likely to indicate the
153 // EGLDisplay is entering or leaving a long-term idle state.
154 enum WakeRefStrength {
155 WEAK = 0,
156 STRONG = 1,
157 };
158
159 HibernationMachine(): mWakeCount(0), mHibernating(false),
Jesse Hall201f3b22012-05-04 14:52:40 -0700160 mAttemptHibernation(false), mDpyValid(false),
161#if BOARD_ALLOW_EGL_HIBERNATION
162 mAllowHibernation(true)
163#else
164 mAllowHibernation(false)
165#endif
Jesse Halla0fef1c2012-04-17 12:02:26 -0700166 {}
167 ~HibernationMachine() {}
168
169 bool incWakeCount(WakeRefStrength strenth);
170 void decWakeCount(WakeRefStrength strenth);
171
172 void setDisplayValid(bool valid);
173
174 private:
Jesse Hall201f3b22012-05-04 14:52:40 -0700175 Mutex mLock;
176 int32_t mWakeCount;
177 bool mHibernating;
178 bool mAttemptHibernation;
179 bool mDpyValid;
180 const bool mAllowHibernation;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700181 };
182 HibernationMachine mHibernation;
Mathias Agopian518ec112011-05-13 16:21:08 -0700183};
184
185// ----------------------------------------------------------------------------
186
Jesse Hall25838592012-04-05 15:53:28 -0700187// An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
188// It doesn't refcount the egl_display_t, but does ensure that the underlying
189// EGL implementation is "awake" (not hibernating) and ready for use as long
190// as the egl_display_ptr exists.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700191class egl_display_ptr {
192public:
193 explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
194 if (mDpy) {
195 if (CC_UNLIKELY(!mDpy->enter())) {
196 mDpy = NULL;
197 }
198 }
199 }
200
201 // We only really need a C++11 move constructor, not a copy constructor.
202 // A move constructor would save an enter()/leave() pair on every EGL API
203 // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
Jesse Hall25838592012-04-05 15:53:28 -0700204 // can't use a move constructor until those are cleaned up.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700205 //
206 // egl_display_ptr(egl_display_ptr&& other) {
207 // mDpy = other.mDpy;
208 // other.mDpy = NULL;
209 // }
Jesse Hall25838592012-04-05 15:53:28 -0700210 //
Jesse Hallb29e5e82012-04-04 16:53:42 -0700211 egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
212 if (mDpy) {
213 mDpy->enter();
214 }
215 }
216
217 ~egl_display_ptr() {
218 if (mDpy) {
219 mDpy->leave();
220 }
221 }
222
223 const egl_display_t* operator->() const { return mDpy; }
224 egl_display_t* operator->() { return mDpy; }
225
226 const egl_display_t* get() const { return mDpy; }
227 egl_display_t* get() { return mDpy; }
228
229 operator bool() const { return mDpy != NULL; }
230
231private:
232 egl_display_t* mDpy;
233
234 // non-assignable
235 egl_display_ptr& operator=(const egl_display_ptr&);
236};
237
238// ----------------------------------------------------------------------------
239
240inline egl_display_ptr get_display(EGLDisplay dpy) {
241 return egl_display_ptr(egl_display_t::get(dpy));
242}
243
244// Does not ensure EGL is unhibernated. Use with caution: calls into the
245// underlying EGL implementation are not safe.
246inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700247 return egl_display_t::get(dpy);
248}
249
250// ----------------------------------------------------------------------------
251
Jesse Hallb29e5e82012-04-04 16:53:42 -0700252egl_display_ptr validate_display(EGLDisplay dpy);
253egl_display_ptr validate_display_connection(EGLDisplay dpy,
254 egl_connection_t*& cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700255EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
256EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
257
258// ----------------------------------------------------------------------------
259}; // namespace android
260// ----------------------------------------------------------------------------
261
262#endif // ANDROID_EGL_DISPLAY_H