blob: 412568bf43439b43a8b6e63dec64cb95af186d64 [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
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
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
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
14 ** 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>
27#include <GLES/gl.h>
28#include <GLES/glext.h>
29
Jesse Hallb29e5e82012-04-04 16:53:42 -070030#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include <utils/SortedVector.h>
32#include <utils/threads.h>
Mathias Agopian4b9511c2011-11-13 23:52:47 -080033#include <utils/String8.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070034
Mathias Agopian1cadb252011-05-23 17:26:14 -070035#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070036#include "hooks.h"
37
38// ----------------------------------------------------------------------------
39namespace android {
40// ----------------------------------------------------------------------------
41
42class egl_object_t;
Mathias Agopianfb87e542012-01-30 18:20:52 -080043class egl_context_t;
Mathias Agopian518ec112011-05-13 16:21:08 -070044class egl_connection_t;
45
46// ----------------------------------------------------------------------------
47
Jamie Gennis98c63832011-11-07 17:03:54 -080048class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
Mathias Agopian518ec112011-05-13 16:21:08 -070049 static egl_display_t sDisplay[NUM_DISPLAYS];
50 EGLDisplay getDisplay(EGLNativeDisplayType display);
Mathias Agopiana4b2c042012-02-03 15:24:51 -080051 void loseCurrentImpl(egl_context_t * cur_c);
Mathias Agopian518ec112011-05-13 16:21:08 -070052
53public:
54 enum {
55 NOT_INITIALIZED = 0,
56 INITIALIZED = 1,
57 TERMINATED = 2
58 };
59
60 egl_display_t();
61 ~egl_display_t();
62
63 EGLBoolean initialize(EGLint *major, EGLint *minor);
64 EGLBoolean terminate();
65
66 // add object to this display's list
67 void addObject(egl_object_t* object);
Mathias Agopian5b287a62011-05-16 18:58:55 -070068 // remove object from this display's list
69 void removeObject(egl_object_t* object);
Mathias Agopian518ec112011-05-13 16:21:08 -070070 // add reference to this object. returns true if this is a valid object.
Mathias Agopianf0480de2011-11-13 20:50:07 -080071 bool getObject(egl_object_t* object) const;
Mathias Agopian518ec112011-05-13 16:21:08 -070072
Jesse Hall25838592012-04-05 15:53:28 -070073 // These notifications allow the display to keep track of how many window
74 // surfaces exist, which it uses to decide whether to hibernate the
75 // underlying EGL implementation. They can be called by any thread without
76 // holding a lock, but must be called via egl_display_ptr to ensure
77 // proper hibernate/wakeup sequencing. If a surface destruction triggers
78 // hibernation, hibernation will be delayed at least until the calling
79 // thread's egl_display_ptr is destroyed.
Jesse Halla0fef1c2012-04-17 12:02:26 -070080 void onWindowSurfaceCreated() {
81 mHibernation.incWakeCount(HibernationMachine::STRONG);
82 }
83 void onWindowSurfaceDestroyed() {
84 mHibernation.decWakeCount(HibernationMachine::STRONG);
85 }
Jesse Hall25838592012-04-05 15:53:28 -070086
Mathias Agopian518ec112011-05-13 16:21:08 -070087 static egl_display_t* get(EGLDisplay dpy);
88 static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
89
Mathias Agopianfb87e542012-01-30 18:20:52 -080090 EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
91 EGLSurface draw, EGLSurface read, EGLContext ctx,
92 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
93 static void loseCurrent(egl_context_t * cur_c);
94
Mathias Agopian518ec112011-05-13 16:21:08 -070095 inline bool isReady() const { return (refs > 0); }
96 inline bool isValid() const { return magic == '_dpy'; }
97 inline bool isAlive() const { return isValid(); }
98
Mathias Agopian4b9511c2011-11-13 23:52:47 -080099 char const * getVendorString() const { return mVendorString.string(); }
100 char const * getVersionString() const { return mVersionString.string(); }
101 char const * getClientApiString() const { return mClientApiString.string(); }
102 char const * getExtensionString() const { return mExtensionString.string(); }
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;
134 mutable Mutex lock;
135 SortedVector<egl_object_t*> objects;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800136 String8 mVendorString;
137 String8 mVersionString;
138 String8 mClientApiString;
139 String8 mExtensionString;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700140
141 // HibernationMachine uses its own internal mutex to protect its own data.
142 // The owning egl_display_t's lock may be but is not required to be held
143 // when calling HibernationMachine methods. As a result, nothing in this
144 // class may call back up to egl_display_t directly or indirectly.
145 class HibernationMachine {
146 public:
147 // STRONG refs cancel (inc) or initiate (dec) a hibernation attempt
148 // the next time the wakecount reaches zero. WEAK refs don't affect
149 // whether a hibernation attempt will be made. Use STRONG refs only
150 // for infrequent/heavy changes that are likely to indicate the
151 // EGLDisplay is entering or leaving a long-term idle state.
152 enum WakeRefStrength {
153 WEAK = 0,
154 STRONG = 1,
155 };
156
157 HibernationMachine(): mWakeCount(0), mHibernating(false),
158 mAttemptHibernation(false), mDpyValid(false)
159 {}
160 ~HibernationMachine() {}
161
162 bool incWakeCount(WakeRefStrength strenth);
163 void decWakeCount(WakeRefStrength strenth);
164
165 void setDisplayValid(bool valid);
166
167 private:
168 Mutex mLock;
169 int32_t mWakeCount;
170 bool mHibernating;
171 bool mAttemptHibernation;
172 bool mDpyValid;
173 };
174 HibernationMachine mHibernation;
Mathias Agopian518ec112011-05-13 16:21:08 -0700175};
176
177// ----------------------------------------------------------------------------
178
Jesse Hall25838592012-04-05 15:53:28 -0700179// An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
180// It doesn't refcount the egl_display_t, but does ensure that the underlying
181// EGL implementation is "awake" (not hibernating) and ready for use as long
182// as the egl_display_ptr exists.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700183class egl_display_ptr {
184public:
185 explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
186 if (mDpy) {
187 if (CC_UNLIKELY(!mDpy->enter())) {
188 mDpy = NULL;
189 }
190 }
191 }
192
193 // We only really need a C++11 move constructor, not a copy constructor.
194 // A move constructor would save an enter()/leave() pair on every EGL API
195 // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
Jesse Hall25838592012-04-05 15:53:28 -0700196 // can't use a move constructor until those are cleaned up.
Jesse Hallb29e5e82012-04-04 16:53:42 -0700197 //
198 // egl_display_ptr(egl_display_ptr&& other) {
199 // mDpy = other.mDpy;
200 // other.mDpy = NULL;
201 // }
Jesse Hall25838592012-04-05 15:53:28 -0700202 //
Jesse Hallb29e5e82012-04-04 16:53:42 -0700203 egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
204 if (mDpy) {
205 mDpy->enter();
206 }
207 }
208
209 ~egl_display_ptr() {
210 if (mDpy) {
211 mDpy->leave();
212 }
213 }
214
215 const egl_display_t* operator->() const { return mDpy; }
216 egl_display_t* operator->() { return mDpy; }
217
218 const egl_display_t* get() const { return mDpy; }
219 egl_display_t* get() { return mDpy; }
220
221 operator bool() const { return mDpy != NULL; }
222
223private:
224 egl_display_t* mDpy;
225
226 // non-assignable
227 egl_display_ptr& operator=(const egl_display_ptr&);
228};
229
230// ----------------------------------------------------------------------------
231
232inline egl_display_ptr get_display(EGLDisplay dpy) {
233 return egl_display_ptr(egl_display_t::get(dpy));
234}
235
236// Does not ensure EGL is unhibernated. Use with caution: calls into the
237// underlying EGL implementation are not safe.
238inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700239 return egl_display_t::get(dpy);
240}
241
242// ----------------------------------------------------------------------------
243
Jesse Hallb29e5e82012-04-04 16:53:42 -0700244egl_display_ptr validate_display(EGLDisplay dpy);
245egl_display_ptr validate_display_connection(EGLDisplay dpy,
246 egl_connection_t*& cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700247EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
248EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
249
250// ----------------------------------------------------------------------------
251}; // namespace android
252// ----------------------------------------------------------------------------
253
254#endif // ANDROID_EGL_DISPLAY_H