blob: 8fd23eb68ff2d4ca82190229111e62191b746fce [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
Mathias Agopian518ec112011-05-13 16:21:08 -070073 static egl_display_t* get(EGLDisplay dpy);
74 static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
75
Mathias Agopianfb87e542012-01-30 18:20:52 -080076 EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
77 EGLSurface draw, EGLSurface read, EGLContext ctx,
78 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
79 static void loseCurrent(egl_context_t * cur_c);
80
Mathias Agopian518ec112011-05-13 16:21:08 -070081 inline bool isReady() const { return (refs > 0); }
82 inline bool isValid() const { return magic == '_dpy'; }
83 inline bool isAlive() const { return isValid(); }
84
Mathias Agopian4b9511c2011-11-13 23:52:47 -080085 char const * getVendorString() const { return mVendorString.string(); }
86 char const * getVersionString() const { return mVersionString.string(); }
87 char const * getClientApiString() const { return mClientApiString.string(); }
88 char const * getExtensionString() const { return mExtensionString.string(); }
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;
116 bool enter();
117 void leave();
118
Mathias Agopianf0480de2011-11-13 20:50:07 -0800119 uint32_t refs;
120 mutable Mutex lock;
121 SortedVector<egl_object_t*> objects;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800122 String8 mVendorString;
123 String8 mVersionString;
124 String8 mClientApiString;
125 String8 mExtensionString;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700126 int32_t mWakeCount;
Mathias Agopian518ec112011-05-13 16:21:08 -0700127};
128
129// ----------------------------------------------------------------------------
130
Jesse Hallb29e5e82012-04-04 16:53:42 -0700131class egl_display_ptr {
132public:
133 explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
134 if (mDpy) {
135 if (CC_UNLIKELY(!mDpy->enter())) {
136 mDpy = NULL;
137 }
138 }
139 }
140
141 // We only really need a C++11 move constructor, not a copy constructor.
142 // A move constructor would save an enter()/leave() pair on every EGL API
143 // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
144 // can't use a move constructor yet.
145 //
146 // egl_display_ptr(egl_display_ptr&& other) {
147 // mDpy = other.mDpy;
148 // other.mDpy = NULL;
149 // }
150 egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
151 if (mDpy) {
152 mDpy->enter();
153 }
154 }
155
156 ~egl_display_ptr() {
157 if (mDpy) {
158 mDpy->leave();
159 }
160 }
161
162 const egl_display_t* operator->() const { return mDpy; }
163 egl_display_t* operator->() { return mDpy; }
164
165 const egl_display_t* get() const { return mDpy; }
166 egl_display_t* get() { return mDpy; }
167
168 operator bool() const { return mDpy != NULL; }
169
170private:
171 egl_display_t* mDpy;
172
173 // non-assignable
174 egl_display_ptr& operator=(const egl_display_ptr&);
175};
176
177// ----------------------------------------------------------------------------
178
179inline egl_display_ptr get_display(EGLDisplay dpy) {
180 return egl_display_ptr(egl_display_t::get(dpy));
181}
182
183// Does not ensure EGL is unhibernated. Use with caution: calls into the
184// underlying EGL implementation are not safe.
185inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700186 return egl_display_t::get(dpy);
187}
188
189// ----------------------------------------------------------------------------
190
Jesse Hallb29e5e82012-04-04 16:53:42 -0700191egl_display_ptr validate_display(EGLDisplay dpy);
192egl_display_ptr validate_display_connection(EGLDisplay dpy,
193 egl_connection_t*& cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700194EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
195EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
196
197// ----------------------------------------------------------------------------
198}; // namespace android
199// ----------------------------------------------------------------------------
200
201#endif // ANDROID_EGL_DISPLAY_H