blob: b79051c499e3ddce04325498b678846117a66018 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002**
3** Copyright 2007 The Android Open Source Project
4**
Mathias Agopian076b1cc2009-04-10 14:24:30 -07005** Licensed under the Apache License Version 2.0(the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08008**
Mathias Agopian076b1cc2009-04-10 14:24:30 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080010**
Mathias Agopian076b1cc2009-04-10 14:24:30 -070011** Unless required by applicable law or agreed to in writing software
12** distributed under the License is distributed on an "AS IS" BASIS
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#include <assert.h>
Jesse Hall3aa75f92016-05-20 10:47:07 -070019#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <errno.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/ioctl.h>
26#include <sys/types.h>
27#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mark Salyzyn7823e122016-09-29 08:08:05 -070030#include <log/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/threads.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080033#include <ui/ANativeObjectBase.h>
Jamie Gennisd8e812c2012-06-13 16:32:25 -070034#include <ui/Fence.h>
Dan Stoza204240a2016-01-08 10:52:16 -080035#include <ui/GraphicBufferMapper.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include <EGL/egl.h>
39#include <EGL/eglext.h>
40#include <GLES/gl.h>
41#include <GLES/glext.h>
42
43#include <pixelflinger/format.h>
44#include <pixelflinger/pixelflinger.h>
45
46#include "context.h"
47#include "state.h"
48#include "texture.h"
49#include "matrix.h"
50
51#undef NELEM
52#define NELEM(x) (sizeof(x)/sizeof(*(x)))
53
Mathias Agopian5f2165f2012-02-24 18:25:41 -080054// ----------------------------------------------------------------------------
Mathias Agopian4b9511c2011-11-13 23:52:47 -080055
56EGLBoolean EGLAPI eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
57 EGLint left, EGLint top, EGLint width, EGLint height);
58
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070059
60typedef struct egl_native_pixmap_t
61{
62 int32_t version; /* must be 32 */
63 int32_t width;
64 int32_t height;
65 int32_t stride;
66 uint8_t* data;
67 uint8_t format;
68 uint8_t rfu[3];
69 union {
70 uint32_t compressedFormat;
71 int32_t vstride;
72 };
73 int32_t reserved;
74} egl_native_pixmap_t;
75
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ----------------------------------------------------------------------------
78namespace android {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080// ----------------------------------------------------------------------------
81
82const unsigned int NUM_DISPLAYS = 1;
83
84static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
85static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
86static pthread_key_t gEGLErrorKey = -1;
Elliott Hughes6071da72015-08-12 15:27:47 -070087#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088namespace gl {
89pthread_key_t gGLKey = -1;
90}; // namespace gl
91#endif
92
93template<typename T>
94static T setError(GLint error, T returnValue) {
95 if (ggl_unlikely(gEGLErrorKey == -1)) {
96 pthread_mutex_lock(&gErrorKeyMutex);
97 if (gEGLErrorKey == -1)
98 pthread_key_create(&gEGLErrorKey, NULL);
99 pthread_mutex_unlock(&gErrorKeyMutex);
100 }
Colin Cross444839b2014-01-24 14:35:39 -0800101 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 return returnValue;
103}
104
105static GLint getError() {
106 if (ggl_unlikely(gEGLErrorKey == -1))
107 return EGL_SUCCESS;
Colin Cross444839b2014-01-24 14:35:39 -0800108 GLint error = (GLint)(uintptr_t)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -0800109 if (error == 0) {
110 // The TLS key has been created by another thread, but the value for
111 // this thread has not been initialized.
112 return EGL_SUCCESS;
113 }
Colin Cross444839b2014-01-24 14:35:39 -0800114 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)EGL_SUCCESS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 return error;
116}
117
118// ----------------------------------------------------------------------------
119
120struct egl_display_t
121{
122 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 static EGLBoolean is_valid(EGLDisplay dpy) {
127 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
128 }
129
Jesse Hall3aa75f92016-05-20 10:47:07 -0700130 NativeDisplayType type;
131 std::atomic_size_t initialized;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132};
133
134static egl_display_t gDisplays[NUM_DISPLAYS];
135
136egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
137 return gDisplays[uintptr_t(dpy)-1U];
138}
139
140struct egl_context_t {
141 enum {
142 IS_CURRENT = 0x00010000,
143 NEVER_CURRENT = 0x00020000
144 };
145 uint32_t flags;
146 EGLDisplay dpy;
147 EGLConfig config;
148 EGLSurface read;
149 EGLSurface draw;
150
151 static inline egl_context_t* context(EGLContext ctx) {
152 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
153 return static_cast<egl_context_t*>(gl->rasterizer.base);
154 }
155};
156
157// ----------------------------------------------------------------------------
158
159struct egl_surface_t
160{
161 enum {
162 PAGE_FLIP = 0x00000001,
163 MAGIC = 0x31415265
164 };
165
166 uint32_t magic;
167 EGLDisplay dpy;
168 EGLConfig config;
169 EGLContext ctx;
Jesse Hall78141e32013-03-07 09:56:26 -0800170 bool zombie;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171
172 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
173 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700174 bool isValid() const;
175 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
178 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700179 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700180 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 virtual EGLint getWidth() const = 0;
182 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
184 virtual EGLint getHorizontalResolution() const;
185 virtual EGLint getVerticalResolution() const;
186 virtual EGLint getRefreshRate() const;
187 virtual EGLint getSwapBehavior() const;
188 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700189 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190protected:
191 GGLSurface depth;
192};
193
194egl_surface_t::egl_surface_t(EGLDisplay dpy,
195 EGLConfig config,
196 int32_t depthFormat)
Jesse Hall78141e32013-03-07 09:56:26 -0800197 : magic(MAGIC), dpy(dpy), config(config), ctx(0), zombie(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198{
199 depth.version = sizeof(GGLSurface);
200 depth.data = 0;
201 depth.format = depthFormat;
202}
203egl_surface_t::~egl_surface_t()
204{
205 magic = 0;
206 free(depth.data);
207}
Mathias Agopian0696a572009-08-20 00:12:56 -0700208bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000209 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700210 return magic == MAGIC;
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213EGLBoolean egl_surface_t::swapBuffers() {
214 return EGL_FALSE;
215}
216EGLint egl_surface_t::getHorizontalResolution() const {
217 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
218}
219EGLint egl_surface_t::getVerticalResolution() const {
220 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
221}
222EGLint egl_surface_t::getRefreshRate() const {
223 return (60 * EGL_DISPLAY_SCALING);
224}
225EGLint egl_surface_t::getSwapBehavior() const {
226 return EGL_BUFFER_PRESERVED;
227}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700228EGLBoolean egl_surface_t::setSwapRectangle(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700229 EGLint /*l*/, EGLint /*t*/, EGLint /*w*/, EGLint /*h*/)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700230{
231 return EGL_FALSE;
232}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
234// ----------------------------------------------------------------------------
235
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 EGLDisplay dpy, EGLConfig config,
240 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700241 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242
Mathias Agopian0696a572009-08-20 00:12:56 -0700243 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244
Mathias Agopian0696a572009-08-20 00:12:56 -0700245 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 virtual EGLBoolean swapBuffers();
247 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
248 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700249 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700250 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700251 virtual EGLint getWidth() const { return width; }
252 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 virtual EGLint getHorizontalResolution() const;
254 virtual EGLint getVerticalResolution() const;
255 virtual EGLint getRefreshRate() const;
256 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700257 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700260 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
261 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700262 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700263 ANativeWindowBuffer* buffer;
264 ANativeWindowBuffer* previousBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 int width;
266 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700267 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700268 GGLFormat const* pixelFormatTable;
269
270 struct Rect {
271 inline Rect() { };
272 inline Rect(int32_t w, int32_t h)
273 : left(0), top(0), right(w), bottom(h) { }
274 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
275 : left(l), top(t), right(r), bottom(b) { }
276 Rect& andSelf(const Rect& r) {
277 left = max(left, r.left);
278 top = max(top, r.top);
279 right = min(right, r.right);
280 bottom = min(bottom, r.bottom);
281 return *this;
282 }
283 bool isEmpty() const {
284 return (left>=right || top>=bottom);
285 }
286 void dump(char const* what) {
Steve Block9d453682011-12-20 16:23:08 +0000287 ALOGD("%s { %5d, %5d, w=%5d, h=%5d }",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700288 what, left, top, right-left, bottom-top);
289 }
290
291 int32_t left;
292 int32_t top;
293 int32_t right;
294 int32_t bottom;
295 };
296
297 struct Region {
298 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700299 typedef Rect const* const_iterator;
300 const_iterator begin() const { return storage; }
301 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700302 static Region subtract(const Rect& lhs, const Rect& rhs) {
303 Region reg;
304 Rect* storage = reg.storage;
305 if (!lhs.isEmpty()) {
306 if (lhs.top < rhs.top) { // top rect
307 storage->left = lhs.left;
308 storage->top = lhs.top;
309 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700310 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700311 storage++;
312 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700313 const int32_t top = max(lhs.top, rhs.top);
314 const int32_t bot = min(lhs.bottom, rhs.bottom);
315 if (top < bot) {
316 if (lhs.left < rhs.left) { // left-side rect
317 storage->left = lhs.left;
318 storage->top = top;
319 storage->right = rhs.left;
320 storage->bottom = bot;
321 storage++;
322 }
323 if (lhs.right > rhs.right) { // right-side rect
324 storage->left = rhs.right;
325 storage->top = top;
326 storage->right = lhs.right;
327 storage->bottom = bot;
328 storage++;
329 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700330 }
331 if (lhs.bottom > rhs.bottom) { // bottom rect
332 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700333 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700334 storage->right = lhs.right;
335 storage->bottom = lhs.bottom;
336 storage++;
337 }
338 reg.count = storage - reg.storage;
339 }
340 return reg;
341 }
342 bool isEmpty() const {
343 return count<=0;
344 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700345 private:
346 Rect storage[4];
347 ssize_t count;
348 };
349
350 void copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700351 ANativeWindowBuffer* dst, void* dst_vaddr,
352 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700353 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700354
355 Rect dirtyRegion;
356 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357};
358
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700359egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 EGLConfig config,
361 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700362 ANativeWindow* window)
Dan Stoza204240a2016-01-08 10:52:16 -0800363 : egl_surface_t(dpy, config, depthFormat),
364 nativeWindow(window), buffer(0), previousBuffer(0), bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700366
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700367 pixelFormatTable = gglGetPixelFormatTable();
Dan Stoza204240a2016-01-08 10:52:16 -0800368
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700369 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700370 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700371 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
372 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700373}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700374
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700375egl_window_surface_v2_t::~egl_window_surface_v2_t() {
376 if (buffer) {
377 buffer->common.decRef(&buffer->common);
378 }
379 if (previousBuffer) {
380 previousBuffer->common.decRef(&previousBuffer->common);
381 }
382 nativeWindow->common.decRef(&nativeWindow->common);
383}
384
Mathias Agopiancf81c842009-07-31 14:47:00 -0700385EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700386{
Mathias Agopian52212712009-08-11 22:34:02 -0700387 // we're intending to do software rendering
388 native_window_set_usage(nativeWindow,
389 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
390
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700391 // dequeue a buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700392 int fenceFd = -1;
393 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer,
394 &fenceFd) != NO_ERROR) {
395 return setError(EGL_BAD_ALLOC, EGL_FALSE);
396 }
397
398 // wait for the buffer
399 sp<Fence> fence(new Fence(fenceFd));
400 if (fence->wait(Fence::TIMEOUT_NEVER) != NO_ERROR) {
401 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700402 return setError(EGL_BAD_ALLOC, EGL_FALSE);
403 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700404
405 // allocate a corresponding depth-buffer
406 width = buffer->width;
407 height = buffer->height;
408 if (depth.format) {
409 depth.width = width;
410 depth.height = height;
411 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700412 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
413 static_cast<uint64_t>(depth.height) * 2;
414 if (depth.stride < 0 || depth.height > INT_MAX ||
415 allocSize > UINT32_MAX) {
416 return setError(EGL_BAD_ALLOC, EGL_FALSE);
417 }
418 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700419 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700420 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700421 }
422 }
423
424 // keep a reference on the buffer
425 buffer->common.incRef(&buffer->common);
426
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700427 // pin the buffer down
428 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
429 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000430 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700431 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700432 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700433 // FIXME: we should make sure we're not accessing the buffer anymore
434 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700435 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700436}
437
438void egl_window_surface_v2_t::disconnect()
439{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700440 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700441 bits = NULL;
442 unlock(buffer);
443 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700444 if (buffer) {
Jesse Hall5f555562013-03-07 15:14:18 -0800445 nativeWindow->cancelBuffer(nativeWindow, buffer, -1);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700446 buffer->common.decRef(&buffer->common);
447 buffer = 0;
448 }
449 if (previousBuffer) {
450 previousBuffer->common.decRef(&previousBuffer->common);
451 previousBuffer = 0;
452 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
Mathias Agopian0926f502009-05-04 14:17:04 -0700455status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700456 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700457{
Dan Stoza204240a2016-01-08 10:52:16 -0800458 auto& mapper = GraphicBufferMapper::get();
459 return mapper.lock(buf->handle, usage,
460 android::Rect(buf->width, buf->height), vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700461}
462
Iliyan Malchev697526b2011-05-01 11:33:26 -0700463status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700464{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700465 if (!buf) return BAD_VALUE;
Dan Stoza204240a2016-01-08 10:52:16 -0800466 auto& mapper = GraphicBufferMapper::get();
467 return mapper.unlock(buf->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700468}
469
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700470void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700471 ANativeWindowBuffer* dst, void* dst_vaddr,
472 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700473 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700474{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700475 // NOTE: dst and src must be the same format
476
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700477 Region::const_iterator cur = clip.begin();
478 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700479
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700480 const size_t bpp = pixelFormatTable[src->format].size;
481 const size_t dbpr = dst->stride * bpp;
482 const size_t sbpr = src->stride * bpp;
483
484 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
485 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
486
487 while (cur != end) {
488 const Rect& r(*cur++);
489 ssize_t w = r.right - r.left;
490 ssize_t h = r.bottom - r.top;
491 if (w <= 0 || h<=0) continue;
492 size_t size = w * bpp;
493 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
494 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
495 if (dbpr==sbpr && size==sbpr) {
496 size *= h;
497 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700498 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700499 do {
500 memcpy(d, s, size);
501 d += dbpr;
502 s += sbpr;
503 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700505}
506
507EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700509 if (!buffer) {
510 return setError(EGL_BAD_ACCESS, EGL_FALSE);
511 }
512
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700513 /*
514 * Handle eglSetSwapRectangleANDROID()
515 * We copyback from the front buffer
516 */
517 if (!dirtyRegion.isEmpty()) {
518 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
519 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100520 // This was const Region copyBack, but that causes an
521 // internal compile error on simulator builds
522 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700523 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700524 void* prevBits;
525 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700526 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
527 // copy from previousBuffer to buffer
528 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700529 unlock(previousBuffer);
530 }
531 }
532 }
533 oldDirtyRegion = dirtyRegion;
534 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700535
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700536 if (previousBuffer) {
537 previousBuffer->common.decRef(&previousBuffer->common);
538 previousBuffer = 0;
539 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540
Mathias Agopian0926f502009-05-04 14:17:04 -0700541 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700542 previousBuffer = buffer;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700543 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700544 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700546 // dequeue a new buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700547 int fenceFd = -1;
548 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) == NO_ERROR) {
549 sp<Fence> fence(new Fence(fenceFd));
550 if (fence->wait(Fence::TIMEOUT_NEVER)) {
551 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
552 return setError(EGL_BAD_ALLOC, EGL_FALSE);
553 }
Mathias Agopian031213e2010-08-18 16:07:34 -0700554
555 // reallocate the depth-buffer if needed
556 if ((width != buffer->width) || (height != buffer->height)) {
557 // TODO: we probably should reset the swap rect here
558 // if the window size has changed
559 width = buffer->width;
560 height = buffer->height;
561 if (depth.data) {
562 free(depth.data);
563 depth.width = width;
564 depth.height = height;
565 depth.stride = buffer->stride;
Michael Lentine95303882015-05-28 17:43:06 -0700566 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
567 static_cast<uint64_t>(depth.height) * 2;
568 if (depth.stride < 0 || depth.height > INT_MAX ||
569 allocSize > UINT32_MAX) {
570 setError(EGL_BAD_ALLOC, EGL_FALSE);
571 return EGL_FALSE;
572 }
573 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopian031213e2010-08-18 16:07:34 -0700574 if (depth.data == 0) {
575 setError(EGL_BAD_ALLOC, EGL_FALSE);
576 return EGL_FALSE;
577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
579 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700580
Mathias Agopian031213e2010-08-18 16:07:34 -0700581 // keep a reference on the buffer
582 buffer->common.incRef(&buffer->common);
583
584 // finally pin the buffer down
585 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
586 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000587 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700588 buffer, buffer->width, buffer->height);
589 return setError(EGL_BAD_ACCESS, EGL_FALSE);
590 // FIXME: we should make sure we're not accessing the buffer anymore
591 }
592 } else {
593 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700594 }
595
596 return EGL_TRUE;
597}
598
599EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
600 EGLint l, EGLint t, EGLint w, EGLint h)
601{
602 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 return EGL_TRUE;
604}
605
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607{
608 GGLSurface buffer;
609 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700610 buffer.width = this->buffer->width;
611 buffer.height = this->buffer->height;
612 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700613 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 gl->rasterizer.procs.colorBuffer(gl, &buffer);
616 if (depth.data != gl->rasterizer.state.buffers.depth.data)
617 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 return EGL_TRUE;
620}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622{
623 GGLSurface buffer;
624 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700625 buffer.width = this->buffer->width;
626 buffer.height = this->buffer->height;
627 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700628 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700629 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 gl->rasterizer.procs.readBuffer(gl, &buffer);
631 return EGL_TRUE;
632}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700633EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
635}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
638}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639EGLint egl_window_surface_v2_t::getRefreshRate() const {
640 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700642EGLint egl_window_surface_v2_t::getSwapBehavior() const
643{
644 /*
645 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
646 * the content of the swapped buffer.
647 *
648 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
649 *
650 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
651 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
652 * is, everything outside of this area is preserved.
653 *
654 * This implementation of EGL assumes the later case.
655 *
656 */
657
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659}
660
661// ----------------------------------------------------------------------------
662
663struct egl_pixmap_surface_t : public egl_surface_t
664{
665 egl_pixmap_surface_t(
666 EGLDisplay dpy, EGLConfig config,
667 int32_t depthFormat,
668 egl_native_pixmap_t const * pixmap);
669
670 virtual ~egl_pixmap_surface_t() { }
671
Mathias Agopian0696a572009-08-20 00:12:56 -0700672 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
674 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
675 virtual EGLint getWidth() const { return nativePixmap.width; }
676 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677private:
678 egl_native_pixmap_t nativePixmap;
679};
680
681egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
682 EGLConfig config,
683 int32_t depthFormat,
684 egl_native_pixmap_t const * pixmap)
685 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
686{
687 if (depthFormat) {
688 depth.width = pixmap->width;
689 depth.height = pixmap->height;
690 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700691 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
692 static_cast<uint64_t>(depth.height) * 2;
693 if (depth.stride < 0 || depth.height > INT_MAX ||
694 allocSize > UINT32_MAX) {
695 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
696 return;
697 }
698 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 if (depth.data == 0) {
700 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 }
702 }
703}
704EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
705{
706 GGLSurface buffer;
707 buffer.version = sizeof(GGLSurface);
708 buffer.width = nativePixmap.width;
709 buffer.height = nativePixmap.height;
710 buffer.stride = nativePixmap.stride;
711 buffer.data = nativePixmap.data;
712 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 gl->rasterizer.procs.colorBuffer(gl, &buffer);
715 if (depth.data != gl->rasterizer.state.buffers.depth.data)
716 gl->rasterizer.procs.depthBuffer(gl, &depth);
717 return EGL_TRUE;
718}
719EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
720{
721 GGLSurface buffer;
722 buffer.version = sizeof(GGLSurface);
723 buffer.width = nativePixmap.width;
724 buffer.height = nativePixmap.height;
725 buffer.stride = nativePixmap.stride;
726 buffer.data = nativePixmap.data;
727 buffer.format = nativePixmap.format;
728 gl->rasterizer.procs.readBuffer(gl, &buffer);
729 return EGL_TRUE;
730}
731
732// ----------------------------------------------------------------------------
733
734struct egl_pbuffer_surface_t : public egl_surface_t
735{
736 egl_pbuffer_surface_t(
737 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
738 int32_t w, int32_t h, int32_t f);
739
740 virtual ~egl_pbuffer_surface_t();
741
Mathias Agopian0696a572009-08-20 00:12:56 -0700742 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
744 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
745 virtual EGLint getWidth() const { return pbuffer.width; }
746 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747private:
748 GGLSurface pbuffer;
749};
750
751egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
752 EGLConfig config, int32_t depthFormat,
753 int32_t w, int32_t h, int32_t f)
754 : egl_surface_t(dpy, config, depthFormat)
755{
756 size_t size = w*h;
757 switch (f) {
758 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
759 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
760 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800761 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
Amit Pundir90c75a22015-04-18 20:37:58 +0530762 case GGL_PIXEL_FORMAT_BGRA_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000764 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 pbuffer.data = 0;
766 break;
767 }
768 pbuffer.version = sizeof(GGLSurface);
769 pbuffer.width = w;
770 pbuffer.height = h;
771 pbuffer.stride = w;
772 pbuffer.data = (GGLubyte*)malloc(size);
773 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 if (depthFormat) {
776 depth.width = pbuffer.width;
777 depth.height = pbuffer.height;
778 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700779 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
780 static_cast<uint64_t>(depth.height) * 2;
781 if (depth.stride < 0 || depth.height > INT_MAX ||
782 allocSize > UINT32_MAX) {
783 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
784 return;
785 }
786 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 if (depth.data == 0) {
788 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
789 return;
790 }
791 }
792}
793egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
794 free(pbuffer.data);
795}
796EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
797{
798 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
799 if (depth.data != gl->rasterizer.state.buffers.depth.data)
800 gl->rasterizer.procs.depthBuffer(gl, &depth);
801 return EGL_TRUE;
802}
803EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
804{
805 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
806 return EGL_TRUE;
807}
808
809// ----------------------------------------------------------------------------
810
811struct config_pair_t {
812 GLint key;
813 GLint value;
814};
815
816struct configs_t {
817 const config_pair_t* array;
818 int size;
819};
820
821struct config_management_t {
822 GLint key;
823 bool (*match)(GLint reqValue, GLint confValue);
824 static bool atLeast(GLint reqValue, GLint confValue) {
825 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
826 }
827 static bool exact(GLint reqValue, GLint confValue) {
828 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
829 }
830 static bool mask(GLint reqValue, GLint confValue) {
831 return (confValue & reqValue) == reqValue;
832 }
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700833 static bool ignore(GLint /*reqValue*/, GLint /*confValue*/) {
Mathias Agopian63971672010-10-25 15:51:24 -0700834 return true;
835 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836};
837
838// ----------------------------------------------------------------------------
839
840#define VERSION_MAJOR 1
841#define VERSION_MINOR 2
842static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700843static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700844static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700845static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700846 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700847 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700848 // "KHR_image_pixmap "
849 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700850 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700851 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852
853// ----------------------------------------------------------------------------
854
855struct extention_map_t {
856 const char * const name;
857 __eglMustCastToProperFunctionPointerType address;
858};
859
860static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700861 { "glDrawTexsOES",
862 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
863 { "glDrawTexiOES",
864 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
865 { "glDrawTexfOES",
866 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
867 { "glDrawTexxOES",
868 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
869 { "glDrawTexsvOES",
870 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
871 { "glDrawTexivOES",
872 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
873 { "glDrawTexfvOES",
874 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
875 { "glDrawTexxvOES",
876 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
877 { "glQueryMatrixxOES",
878 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
879 { "glEGLImageTargetTexture2DOES",
880 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
881 { "glEGLImageTargetRenderbufferStorageOES",
882 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
883 { "glClipPlanef",
884 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
885 { "glClipPlanex",
886 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
887 { "glBindBuffer",
888 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
889 { "glBufferData",
890 (__eglMustCastToProperFunctionPointerType)&glBufferData },
891 { "glBufferSubData",
892 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
893 { "glDeleteBuffers",
894 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
895 { "glGenBuffers",
896 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700897 { "eglCreateImageKHR",
898 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
899 { "eglDestroyImageKHR",
900 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700901 { "eglCreateSyncKHR",
902 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
903 { "eglDestroySyncKHR",
904 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
905 { "eglClientWaitSyncKHR",
906 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
907 { "eglGetSyncAttribKHR",
908 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700909 { "eglSetSwapRectangleANDROID",
910 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911};
912
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700913/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 * In the lists below, attributes names MUST be sorted.
915 * Additionally, all configs must be sorted according to
916 * the EGL specification.
917 */
918
919static config_pair_t const config_base_attribute_list[] = {
920 { EGL_STENCIL_SIZE, 0 },
921 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
922 { EGL_LEVEL, 0 },
923 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700924 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
926 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
927 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
928 { EGL_NATIVE_VISUAL_ID, 0 },
929 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
930 { EGL_SAMPLES, 0 },
931 { EGL_SAMPLE_BUFFERS, 0 },
932 { EGL_TRANSPARENT_TYPE, EGL_NONE },
933 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
934 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
935 { EGL_TRANSPARENT_RED_VALUE, 0 },
936 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
937 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
938 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700939 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700940 { EGL_LUMINANCE_SIZE, 0 },
941 { EGL_ALPHA_MASK_SIZE, 0 },
942 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700943 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700944 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945};
946
947// These configs can override the base attribute list
948// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
949
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800950// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951static config_pair_t const config_0_attribute_list[] = {
952 { EGL_BUFFER_SIZE, 16 },
953 { EGL_ALPHA_SIZE, 0 },
954 { EGL_BLUE_SIZE, 5 },
955 { EGL_GREEN_SIZE, 6 },
956 { EGL_RED_SIZE, 5 },
957 { EGL_DEPTH_SIZE, 0 },
958 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700959 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
961};
962
963static config_pair_t const config_1_attribute_list[] = {
964 { EGL_BUFFER_SIZE, 16 },
965 { EGL_ALPHA_SIZE, 0 },
966 { EGL_BLUE_SIZE, 5 },
967 { EGL_GREEN_SIZE, 6 },
968 { EGL_RED_SIZE, 5 },
969 { EGL_DEPTH_SIZE, 16 },
970 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700971 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
973};
974
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800975// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976static config_pair_t const config_2_attribute_list[] = {
977 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800978 { EGL_ALPHA_SIZE, 0 },
979 { EGL_BLUE_SIZE, 8 },
980 { EGL_GREEN_SIZE, 8 },
981 { EGL_RED_SIZE, 8 },
982 { EGL_DEPTH_SIZE, 0 },
983 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700984 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800985 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
986};
987
988static config_pair_t const config_3_attribute_list[] = {
989 { EGL_BUFFER_SIZE, 32 },
990 { EGL_ALPHA_SIZE, 0 },
991 { EGL_BLUE_SIZE, 8 },
992 { EGL_GREEN_SIZE, 8 },
993 { EGL_RED_SIZE, 8 },
994 { EGL_DEPTH_SIZE, 16 },
995 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700996 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800997 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
998};
999
1000// 8888 configs
1001static config_pair_t const config_4_attribute_list[] = {
1002 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 { EGL_ALPHA_SIZE, 8 },
1004 { EGL_BLUE_SIZE, 8 },
1005 { EGL_GREEN_SIZE, 8 },
1006 { EGL_RED_SIZE, 8 },
1007 { EGL_DEPTH_SIZE, 0 },
1008 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001009 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1011};
1012
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001013static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 { EGL_BUFFER_SIZE, 32 },
1015 { EGL_ALPHA_SIZE, 8 },
1016 { EGL_BLUE_SIZE, 8 },
1017 { EGL_GREEN_SIZE, 8 },
1018 { EGL_RED_SIZE, 8 },
1019 { EGL_DEPTH_SIZE, 16 },
1020 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001021 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1023};
1024
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001025// A8 configs
1026static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 { EGL_BUFFER_SIZE, 8 },
1028 { EGL_ALPHA_SIZE, 8 },
1029 { EGL_BLUE_SIZE, 0 },
1030 { EGL_GREEN_SIZE, 0 },
1031 { EGL_RED_SIZE, 0 },
1032 { EGL_DEPTH_SIZE, 0 },
1033 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001034 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1036};
1037
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001038static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 { EGL_BUFFER_SIZE, 8 },
1040 { EGL_ALPHA_SIZE, 8 },
1041 { EGL_BLUE_SIZE, 0 },
1042 { EGL_GREEN_SIZE, 0 },
1043 { EGL_RED_SIZE, 0 },
1044 { EGL_DEPTH_SIZE, 16 },
1045 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001046 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1048};
1049
Amit Pundir90c75a22015-04-18 20:37:58 +05301050// BGRA 8888 config
1051static config_pair_t const config_8_attribute_list[] = {
1052 { EGL_BUFFER_SIZE, 32 },
1053 { EGL_ALPHA_SIZE, 8 },
1054 { EGL_BLUE_SIZE, 8 },
1055 { EGL_GREEN_SIZE, 8 },
1056 { EGL_RED_SIZE, 8 },
1057 { EGL_DEPTH_SIZE, 0 },
1058 { EGL_CONFIG_ID, 8 },
1059 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_BGRA_8888 },
1060 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1061};
1062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063static configs_t const gConfigs[] = {
1064 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1065 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1066 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1067 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1068 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1069 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001070 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1071 { config_7_attribute_list, NELEM(config_7_attribute_list) },
Amit Pundir90c75a22015-04-18 20:37:58 +05301072 { config_8_attribute_list, NELEM(config_8_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073};
1074
1075static config_management_t const gConfigManagement[] = {
1076 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1077 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1078 { EGL_BLUE_SIZE, config_management_t::atLeast },
1079 { EGL_GREEN_SIZE, config_management_t::atLeast },
1080 { EGL_RED_SIZE, config_management_t::atLeast },
1081 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1082 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1083 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1084 { EGL_CONFIG_ID, config_management_t::exact },
1085 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001086 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1087 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1088 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001090 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1092 { EGL_SAMPLES, config_management_t::exact },
1093 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1094 { EGL_SURFACE_TYPE, config_management_t::mask },
1095 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1096 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1097 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1098 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1099 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1100 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1101 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1102 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001103 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1104 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1105 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1106 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1107 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108};
1109
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001112 // attributes that are not specified are simply ignored, if a particular
1113 // one needs not be ignored, it must be specified here, eg:
1114 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115};
1116
1117// ----------------------------------------------------------------------------
1118
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001119static status_t getConfigFormatInfo(EGLint configID,
1120 int32_t& pixelFormat, int32_t& depthFormat)
1121{
1122 switch(configID) {
1123 case 0:
1124 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1125 depthFormat = 0;
1126 break;
1127 case 1:
1128 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1129 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1130 break;
1131 case 2:
1132 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1133 depthFormat = 0;
1134 break;
1135 case 3:
1136 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1137 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1138 break;
1139 case 4:
1140 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1141 depthFormat = 0;
1142 break;
1143 case 5:
1144 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1145 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1146 break;
1147 case 6:
1148 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1149 depthFormat = 0;
1150 break;
1151 case 7:
1152 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1153 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1154 break;
Amit Pundir90c75a22015-04-18 20:37:58 +05301155 case 8:
1156 pixelFormat = GGL_PIXEL_FORMAT_BGRA_8888;
1157 depthFormat = 0;
1158 break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001159 default:
1160 return NAME_NOT_FOUND;
1161 }
1162 return NO_ERROR;
1163}
1164
1165// ----------------------------------------------------------------------------
1166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167template<typename T>
1168static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1169{
1170 while (first <= last) {
1171 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001172 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001174 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 last = mid - 1;
1176 } else {
1177 return mid;
1178 }
1179 }
1180 return -1;
1181}
1182
1183static int isAttributeMatching(int i, EGLint attr, EGLint val)
1184{
1185 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001186 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 int index = binarySearch<config_pair_t>(
1188 gConfigs[i].array,
1189 0, gConfigs[i].size-1,
1190 attr);
1191 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001192 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 index = binarySearch<config_pair_t>(
1194 config_base_attribute_list,
1195 0, NELEM(config_base_attribute_list)-1,
1196 attr);
1197 }
1198 if (index >= 0) {
1199 // attribute found, check if this config could match
1200 int cfgMgtIndex = binarySearch<config_management_t>(
1201 gConfigManagement,
1202 0, NELEM(gConfigManagement)-1,
1203 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001204 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 bool match = gConfigManagement[cfgMgtIndex].match(
1206 val, configFound[index].value);
1207 if (match) {
1208 // this config matches
1209 return 1;
1210 }
1211 } else {
1212 // attribute not found. this should NEVER happen.
1213 }
1214 } else {
1215 // error, this attribute doesn't exist
1216 }
1217 return 0;
1218}
1219
1220static int makeCurrent(ogles_context_t* gl)
1221{
1222 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1223 if (gl) {
1224 egl_context_t* c = egl_context_t::context(gl);
1225 if (c->flags & egl_context_t::IS_CURRENT) {
1226 if (current != gl) {
1227 // it is an error to set a context current, if it's already
1228 // current to another thread
1229 return -1;
1230 }
1231 } else {
1232 if (current) {
1233 // mark the current context as not current, and flush
1234 glFlush();
1235 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1236 }
1237 }
1238 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1239 // The context is not current, make it current!
1240 setGlThreadSpecific(gl);
1241 c->flags |= egl_context_t::IS_CURRENT;
1242 }
1243 } else {
1244 if (current) {
1245 // mark the current context as not current, and flush
1246 glFlush();
1247 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1248 }
1249 // this thread has no context attached to it
1250 setGlThreadSpecific(0);
1251 }
1252 return 0;
1253}
1254
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001255static EGLBoolean getConfigAttrib(EGLDisplay /*dpy*/, EGLConfig config,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 EGLint attribute, EGLint *value)
1257{
1258 size_t numConfigs = NELEM(gConfigs);
Colin Cross444839b2014-01-24 14:35:39 -08001259 int index = (int)(uintptr_t)config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 if (uint32_t(index) >= numConfigs)
1261 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1262
1263 int attrIndex;
1264 attrIndex = binarySearch<config_pair_t>(
1265 gConfigs[index].array,
1266 0, gConfigs[index].size-1,
1267 attribute);
1268 if (attrIndex>=0) {
1269 *value = gConfigs[index].array[attrIndex].value;
1270 return EGL_TRUE;
1271 }
1272
1273 attrIndex = binarySearch<config_pair_t>(
1274 config_base_attribute_list,
1275 0, NELEM(config_base_attribute_list)-1,
1276 attribute);
1277 if (attrIndex>=0) {
1278 *value = config_base_attribute_list[attrIndex].value;
1279 return EGL_TRUE;
1280 }
1281 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1282}
1283
1284static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001285 NativeWindowType window, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286{
1287 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1288 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1289 if (window == 0)
1290 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1291
1292 EGLint surfaceType;
1293 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1294 return EGL_FALSE;
1295
1296 if (!(surfaceType & EGL_WINDOW_BIT))
1297 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1298
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001299 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001300 ANDROID_NATIVE_WINDOW_MAGIC) {
1301 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1302 }
1303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 EGLint configID;
1305 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1306 return EGL_FALSE;
1307
1308 int32_t depthFormat;
1309 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001310 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1312 }
1313
1314 // FIXME: we don't have access to the pixelFormat here just yet.
1315 // (it's possible that the surface is not fully initialized)
1316 // maybe this should be done after the page-flip
1317 //if (EGLint(info.format) != pixelFormat)
1318 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1319
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001320 egl_surface_t* surface;
1321 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001322 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323
Mathias Agopian0696a572009-08-20 00:12:56 -07001324 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 // there was a problem in the ctor, the error
1326 // flag has been set.
1327 delete surface;
1328 surface = 0;
1329 }
1330 return surface;
1331}
1332
1333static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001334 NativePixmapType pixmap, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335{
1336 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1337 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1338 if (pixmap == 0)
1339 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1340
1341 EGLint surfaceType;
1342 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1343 return EGL_FALSE;
1344
1345 if (!(surfaceType & EGL_PIXMAP_BIT))
1346 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1347
Mathias Agopian0696a572009-08-20 00:12:56 -07001348 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1349 sizeof(egl_native_pixmap_t)) {
1350 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1351 }
1352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 EGLint configID;
1354 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1355 return EGL_FALSE;
1356
1357 int32_t depthFormat;
1358 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001359 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1361 }
1362
1363 if (pixmap->format != pixelFormat)
1364 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1365
1366 egl_surface_t* surface =
1367 new egl_pixmap_surface_t(dpy, config, depthFormat,
1368 static_cast<egl_native_pixmap_t*>(pixmap));
1369
Mathias Agopian0696a572009-08-20 00:12:56 -07001370 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 // there was a problem in the ctor, the error
1372 // flag has been set.
1373 delete surface;
1374 surface = 0;
1375 }
1376 return surface;
1377}
1378
1379static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1380 const EGLint *attrib_list)
1381{
1382 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1383 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1384
1385 EGLint surfaceType;
1386 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1387 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 if (!(surfaceType & EGL_PBUFFER_BIT))
1390 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 EGLint configID;
1393 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1394 return EGL_FALSE;
1395
1396 int32_t depthFormat;
1397 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001398 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1400 }
1401
1402 int32_t w = 0;
1403 int32_t h = 0;
Jesse Hall2e8ca9d2015-08-21 07:41:46 -07001404 while (attrib_list[0] != EGL_NONE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1406 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1407 attrib_list+=2;
1408 }
1409
1410 egl_surface_t* surface =
1411 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1412
Mathias Agopian0696a572009-08-20 00:12:56 -07001413 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 // there was a problem in the ctor, the error
1415 // flag has been set.
1416 delete surface;
1417 surface = 0;
1418 }
1419 return surface;
1420}
1421
1422// ----------------------------------------------------------------------------
1423}; // namespace android
1424// ----------------------------------------------------------------------------
1425
1426using namespace android;
1427
1428// ----------------------------------------------------------------------------
1429// Initialization
1430// ----------------------------------------------------------------------------
1431
1432EGLDisplay eglGetDisplay(NativeDisplayType display)
1433{
Elliott Hughes6071da72015-08-12 15:27:47 -07001434#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 // this just needs to be done once
1436 if (gGLKey == -1) {
1437 pthread_mutex_lock(&gInitMutex);
1438 if (gGLKey == -1)
1439 pthread_key_create(&gGLKey, NULL);
1440 pthread_mutex_unlock(&gInitMutex);
1441 }
1442#endif
1443 if (display == EGL_DEFAULT_DISPLAY) {
1444 EGLDisplay dpy = (EGLDisplay)1;
1445 egl_display_t& d = egl_display_t::get_display(dpy);
1446 d.type = display;
1447 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001448 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 return EGL_NO_DISPLAY;
1450}
1451
1452EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1453{
1454 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1455 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 EGLBoolean res = EGL_TRUE;
1458 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001459
Jesse Hall3aa75f92016-05-20 10:47:07 -07001460 if (d.initialized.fetch_add(1, std::memory_order_acquire) == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 // initialize stuff here if needed
1462 //pthread_mutex_lock(&gInitMutex);
1463 //pthread_mutex_unlock(&gInitMutex);
1464 }
1465
1466 if (res == EGL_TRUE) {
1467 if (major != NULL) *major = VERSION_MAJOR;
1468 if (minor != NULL) *minor = VERSION_MINOR;
1469 }
1470 return res;
1471}
1472
1473EGLBoolean eglTerminate(EGLDisplay dpy)
1474{
1475 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1476 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1477
1478 EGLBoolean res = EGL_TRUE;
1479 egl_display_t& d = egl_display_t::get_display(dpy);
Jesse Hall3aa75f92016-05-20 10:47:07 -07001480 if (d.initialized.fetch_sub(1, std::memory_order_release) == 1) {
1481 std::atomic_thread_fence(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 // TODO: destroy all resources (surfaces, contexts, etc...)
1483 //pthread_mutex_lock(&gInitMutex);
1484 //pthread_mutex_unlock(&gInitMutex);
1485 }
1486 return res;
1487}
1488
1489// ----------------------------------------------------------------------------
1490// configuration
1491// ----------------------------------------------------------------------------
1492
1493EGLBoolean eglGetConfigs( EGLDisplay dpy,
1494 EGLConfig *configs,
1495 EGLint config_size, EGLint *num_config)
1496{
1497 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1498 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1499
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001500 if (ggl_unlikely(num_config==NULL))
1501 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1502
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 GLint numConfigs = NELEM(gConfigs);
1504 if (!configs) {
1505 *num_config = numConfigs;
1506 return EGL_TRUE;
1507 }
1508 GLint i;
1509 for (i=0 ; i<numConfigs && i<config_size ; i++) {
Colin Cross444839b2014-01-24 14:35:39 -08001510 *configs++ = (EGLConfig)(uintptr_t)i;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 }
1512 *num_config = i;
1513 return EGL_TRUE;
1514}
1515
1516EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1517 EGLConfig *configs, EGLint config_size,
1518 EGLint *num_config)
1519{
1520 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1521 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001522
1523 if (ggl_unlikely(num_config==NULL)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001524 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1525 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526
Jack Palevich749c63d2009-03-25 15:12:17 -07001527 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001528 /*
1529 * A NULL attrib_list should be treated as though it was an empty
1530 * one (terminated with EGL_NONE) as defined in
1531 * section 3.4.1 "Querying Configurations" in the EGL specification.
1532 */
1533 static const EGLint dummy = EGL_NONE;
1534 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 int numAttributes = 0;
1538 int numConfigs = NELEM(gConfigs);
1539 uint32_t possibleMatch = (1<<numConfigs)-1;
1540 while(possibleMatch && *attrib_list != EGL_NONE) {
1541 numAttributes++;
1542 EGLint attr = *attrib_list++;
1543 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001544 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 if (!(possibleMatch & (1<<i)))
1546 continue;
1547 if (isAttributeMatching(i, attr, val) == 0) {
1548 possibleMatch &= ~(1<<i);
1549 }
1550 }
1551 }
1552
1553 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001554 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1555 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 // default value
1557 if (binarySearch<config_pair_t>(
1558 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001559 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560 config_defaults[j].key) < 0)
1561 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001562 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 if (!(possibleMatch & (1<<i)))
1564 continue;
1565 if (isAttributeMatching(i,
1566 config_defaults[j].key,
1567 config_defaults[j].value) == 0)
1568 {
1569 possibleMatch &= ~(1<<i);
1570 }
1571 }
1572 }
1573 }
1574
1575 // return the configurations found
1576 int n=0;
1577 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001578 if (configs) {
1579 for (int i=0 ; config_size && i<numConfigs ; i++) {
1580 if (possibleMatch & (1<<i)) {
Colin Cross444839b2014-01-24 14:35:39 -08001581 *configs++ = (EGLConfig)(uintptr_t)i;
Jack Palevich749c63d2009-03-25 15:12:17 -07001582 config_size--;
1583 n++;
1584 }
1585 }
1586 } else {
1587 for (int i=0 ; i<numConfigs ; i++) {
1588 if (possibleMatch & (1<<i)) {
1589 n++;
1590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 }
1592 }
1593 }
1594 *num_config = n;
1595 return EGL_TRUE;
1596}
1597
1598EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1599 EGLint attribute, EGLint *value)
1600{
1601 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1602 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1603
1604 return getConfigAttrib(dpy, config, attribute, value);
1605}
1606
1607// ----------------------------------------------------------------------------
1608// surfaces
1609// ----------------------------------------------------------------------------
1610
1611EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1612 NativeWindowType window,
1613 const EGLint *attrib_list)
1614{
1615 return createWindowSurface(dpy, config, window, attrib_list);
1616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001617
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1619 NativePixmapType pixmap,
1620 const EGLint *attrib_list)
1621{
1622 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1623}
1624
1625EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1626 const EGLint *attrib_list)
1627{
1628 return createPbufferSurface(dpy, config, attrib_list);
1629}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1632{
1633 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1634 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1635 if (eglSurface != EGL_NO_SURFACE) {
1636 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001637 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1639 if (surface->dpy != dpy)
1640 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001641 if (surface->ctx) {
Jesse Hall78141e32013-03-07 09:56:26 -08001642 // defer disconnect/delete until no longer current
1643 surface->zombie = true;
1644 } else {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001645 surface->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001646 delete surface;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001647 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648 }
1649 return EGL_TRUE;
1650}
1651
1652EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1653 EGLint attribute, EGLint *value)
1654{
1655 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1656 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1657 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001658 if (!surface->isValid())
1659 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 if (surface->dpy != dpy)
1661 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1662
1663 EGLBoolean ret = EGL_TRUE;
1664 switch (attribute) {
1665 case EGL_CONFIG_ID:
1666 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1667 break;
1668 case EGL_WIDTH:
1669 *value = surface->getWidth();
1670 break;
1671 case EGL_HEIGHT:
1672 *value = surface->getHeight();
1673 break;
1674 case EGL_LARGEST_PBUFFER:
1675 // not modified for a window or pixmap surface
1676 break;
1677 case EGL_TEXTURE_FORMAT:
1678 *value = EGL_NO_TEXTURE;
1679 break;
1680 case EGL_TEXTURE_TARGET:
1681 *value = EGL_NO_TEXTURE;
1682 break;
1683 case EGL_MIPMAP_TEXTURE:
1684 *value = EGL_FALSE;
1685 break;
1686 case EGL_MIPMAP_LEVEL:
1687 *value = 0;
1688 break;
1689 case EGL_RENDER_BUFFER:
1690 // TODO: return the real RENDER_BUFFER here
1691 *value = EGL_BACK_BUFFER;
1692 break;
1693 case EGL_HORIZONTAL_RESOLUTION:
1694 // pixel/mm * EGL_DISPLAY_SCALING
1695 *value = surface->getHorizontalResolution();
1696 break;
1697 case EGL_VERTICAL_RESOLUTION:
1698 // pixel/mm * EGL_DISPLAY_SCALING
1699 *value = surface->getVerticalResolution();
1700 break;
1701 case EGL_PIXEL_ASPECT_RATIO: {
1702 // w/h * EGL_DISPLAY_SCALING
1703 int wr = surface->getHorizontalResolution();
1704 int hr = surface->getVerticalResolution();
1705 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1706 } break;
1707 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001708 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 break;
1710 default:
1711 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1712 }
1713 return ret;
1714}
1715
1716EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001717 EGLContext /*share_list*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718{
1719 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1720 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1721
1722 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1723 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1724
1725 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1726 c->flags = egl_context_t::NEVER_CURRENT;
1727 c->dpy = dpy;
1728 c->config = config;
1729 c->read = 0;
1730 c->draw = 0;
1731 return (EGLContext)gl;
1732}
1733
1734EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1735{
1736 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1737 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1738 egl_context_t* c = egl_context_t::context(ctx);
1739 if (c->flags & egl_context_t::IS_CURRENT)
1740 setGlThreadSpecific(0);
1741 ogles_uninit((ogles_context_t*)ctx);
1742 return EGL_TRUE;
1743}
1744
1745EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1746 EGLSurface read, EGLContext ctx)
1747{
1748 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1749 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1750 if (draw) {
1751 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001752 if (!s->isValid())
1753 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 if (s->dpy != dpy)
1755 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001756 // TODO: check that draw is compatible with the context
1757 }
1758 if (read && read!=draw) {
1759 egl_surface_t* s = (egl_surface_t*)read;
1760 if (!s->isValid())
1761 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1762 if (s->dpy != dpy)
1763 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1764 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 }
1766
1767 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1770 return setError(EGL_BAD_MATCH, EGL_FALSE);
1771
1772 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1773 return setError(EGL_BAD_MATCH, EGL_FALSE);
1774
1775 if (ctx == EGL_NO_CONTEXT) {
1776 // if we're detaching, we need the current context
1777 current_ctx = (EGLContext)getGlThreadSpecific();
1778 } else {
1779 egl_context_t* c = egl_context_t::context(ctx);
1780 egl_surface_t* d = (egl_surface_t*)draw;
1781 egl_surface_t* r = (egl_surface_t*)read;
1782 if ((d && d->ctx && d->ctx != ctx) ||
1783 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001784 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1786 }
1787 }
1788
1789 ogles_context_t* gl = (ogles_context_t*)ctx;
1790 if (makeCurrent(gl) == 0) {
1791 if (ctx) {
1792 egl_context_t* c = egl_context_t::context(ctx);
1793 egl_surface_t* d = (egl_surface_t*)draw;
1794 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001795
1796 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001797 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1798 s->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001799 s->ctx = EGL_NO_CONTEXT;
1800 if (s->zombie)
1801 delete s;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001802 }
1803 if (c->read) {
1804 // FIXME: unlock/disconnect the read surface too
1805 }
1806
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001808 c->read = read;
1809
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 if (c->flags & egl_context_t::NEVER_CURRENT) {
1811 c->flags &= ~egl_context_t::NEVER_CURRENT;
1812 GLint w = 0;
1813 GLint h = 0;
1814 if (draw) {
1815 w = d->getWidth();
1816 h = d->getHeight();
1817 }
1818 ogles_surfaceport(gl, 0, 0);
1819 ogles_viewport(gl, 0, 0, w, h);
1820 ogles_scissor(gl, 0, 0, w, h);
1821 }
1822 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001823 if (d->connect() == EGL_FALSE) {
1824 return EGL_FALSE;
1825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 d->ctx = ctx;
1827 d->bindDrawSurface(gl);
1828 }
1829 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001830 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 r->ctx = ctx;
1832 r->bindReadSurface(gl);
1833 }
1834 } else {
1835 // if surfaces were bound to the context bound to this thread
1836 // mark then as unbound.
1837 if (current_ctx) {
1838 egl_context_t* c = egl_context_t::context(current_ctx);
1839 egl_surface_t* d = (egl_surface_t*)c->draw;
1840 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001841 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001842 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001843 d->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001844 d->ctx = EGL_NO_CONTEXT;
1845 if (d->zombie)
1846 delete d;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001847 }
1848 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001849 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001850 r->ctx = EGL_NO_CONTEXT;
1851 // FIXME: unlock/disconnect the read surface too
1852 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 }
1854 }
1855 return EGL_TRUE;
1856 }
1857 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1858}
1859
1860EGLContext eglGetCurrentContext(void)
1861{
1862 // eglGetCurrentContext returns the current EGL rendering context,
1863 // as specified by eglMakeCurrent. If no context is current,
1864 // EGL_NO_CONTEXT is returned.
1865 return (EGLContext)getGlThreadSpecific();
1866}
1867
1868EGLSurface eglGetCurrentSurface(EGLint readdraw)
1869{
1870 // eglGetCurrentSurface returns the read or draw surface attached
1871 // to the current EGL rendering context, as specified by eglMakeCurrent.
1872 // If no context is current, EGL_NO_SURFACE is returned.
1873 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1874 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1875 egl_context_t* c = egl_context_t::context(ctx);
1876 if (readdraw == EGL_READ) {
1877 return c->read;
1878 } else if (readdraw == EGL_DRAW) {
1879 return c->draw;
1880 }
1881 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1882}
1883
1884EGLDisplay eglGetCurrentDisplay(void)
1885{
1886 // eglGetCurrentDisplay returns the current EGL display connection
1887 // for the current EGL rendering context, as specified by eglMakeCurrent.
1888 // If no context is current, EGL_NO_DISPLAY is returned.
1889 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1890 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1891 egl_context_t* c = egl_context_t::context(ctx);
1892 return c->dpy;
1893}
1894
1895EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1896 EGLint attribute, EGLint *value)
1897{
1898 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1899 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1900 egl_context_t* c = egl_context_t::context(ctx);
1901 switch (attribute) {
1902 case EGL_CONFIG_ID:
1903 // Returns the ID of the EGL frame buffer configuration with
1904 // respect to which the context was created
1905 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1906 }
1907 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1908}
1909
1910EGLBoolean eglWaitGL(void)
1911{
1912 return EGL_TRUE;
1913}
1914
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001915EGLBoolean eglWaitNative(EGLint /*engine*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916{
1917 return EGL_TRUE;
1918}
1919
1920EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1921{
1922 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1923 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001926 if (!d->isValid())
1927 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 if (d->dpy != dpy)
1929 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1930
1931 // post the surface
1932 d->swapBuffers();
1933
1934 // if it's bound to a context, update the buffer
1935 if (d->ctx != EGL_NO_CONTEXT) {
1936 d->bindDrawSurface((ogles_context_t*)d->ctx);
1937 // if this surface is also the read surface of the context
1938 // it is bound to, make sure to update the read buffer as well.
1939 // The EGL spec is a little unclear about this.
1940 egl_context_t* c = egl_context_t::context(d->ctx);
1941 if (c->read == draw) {
1942 d->bindReadSurface((ogles_context_t*)d->ctx);
1943 }
1944 }
1945
1946 return EGL_TRUE;
1947}
1948
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001949EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface /*surface*/,
1950 NativePixmapType /*target*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951{
1952 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1953 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1954 // TODO: eglCopyBuffers()
1955 return EGL_FALSE;
1956}
1957
1958EGLint eglGetError(void)
1959{
1960 return getError();
1961}
1962
1963const char* eglQueryString(EGLDisplay dpy, EGLint name)
1964{
1965 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1966 return setError(EGL_BAD_DISPLAY, (const char*)0);
1967
1968 switch (name) {
1969 case EGL_VENDOR:
1970 return gVendorString;
1971 case EGL_VERSION:
1972 return gVersionString;
1973 case EGL_EXTENSIONS:
1974 return gExtensionsString;
1975 case EGL_CLIENT_APIS:
1976 return gClientApiString;
1977 }
1978 return setError(EGL_BAD_PARAMETER, (const char *)0);
1979}
1980
1981// ----------------------------------------------------------------------------
1982// EGL 1.1
1983// ----------------------------------------------------------------------------
1984
1985EGLBoolean eglSurfaceAttrib(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001986 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*attribute*/, EGLint /*value*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987{
1988 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1989 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1990 // TODO: eglSurfaceAttrib()
1991 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1992}
1993
1994EGLBoolean eglBindTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001995 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001996{
1997 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1998 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1999 // TODO: eglBindTexImage()
2000 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2001}
2002
2003EGLBoolean eglReleaseTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002004 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005{
2006 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2007 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2008 // TODO: eglReleaseTexImage()
2009 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2010}
2011
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002012EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint /*interval*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013{
2014 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2015 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2016 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03002017 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018}
2019
2020// ----------------------------------------------------------------------------
2021// EGL 1.2
2022// ----------------------------------------------------------------------------
2023
2024EGLBoolean eglBindAPI(EGLenum api)
2025{
2026 if (api != EGL_OPENGL_ES_API)
2027 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2028 return EGL_TRUE;
2029}
2030
2031EGLenum eglQueryAPI(void)
2032{
2033 return EGL_OPENGL_ES_API;
2034}
2035
2036EGLBoolean eglWaitClient(void)
2037{
2038 glFinish();
2039 return EGL_TRUE;
2040}
2041
2042EGLBoolean eglReleaseThread(void)
2043{
2044 // TODO: eglReleaseThread()
2045 return EGL_TRUE;
2046}
2047
2048EGLSurface eglCreatePbufferFromClientBuffer(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002049 EGLDisplay dpy, EGLenum /*buftype*/, EGLClientBuffer /*buffer*/,
2050 EGLConfig /*config*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051{
2052 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2053 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2054 // TODO: eglCreatePbufferFromClientBuffer()
2055 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2056}
2057
2058// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002059// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060// ----------------------------------------------------------------------------
2061
2062void (*eglGetProcAddress (const char *procname))()
2063{
2064 extention_map_t const * const map = gExtentionMap;
2065 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2066 if (!strcmp(procname, map[i].name)) {
2067 return map[i].address;
2068 }
2069 }
2070 return NULL;
2071}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002072
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002073EGLBoolean eglLockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/,
2074 const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002075{
2076 EGLBoolean result = EGL_FALSE;
2077 return result;
2078}
2079
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002080EGLBoolean eglUnlockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002081{
2082 EGLBoolean result = EGL_FALSE;
2083 return result;
2084}
2085
2086EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002087 EGLClientBuffer buffer, const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002088{
2089 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2090 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2091 }
2092 if (ctx != EGL_NO_CONTEXT) {
2093 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2094 }
2095 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2096 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2097 }
2098
Iliyan Malchev697526b2011-05-01 11:33:26 -07002099 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002100
2101 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2102 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2103
Iliyan Malchev697526b2011-05-01 11:33:26 -07002104 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002105 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002106
2107 switch (native_buffer->format) {
2108 case HAL_PIXEL_FORMAT_RGBA_8888:
2109 case HAL_PIXEL_FORMAT_RGBX_8888:
2110 case HAL_PIXEL_FORMAT_RGB_888:
2111 case HAL_PIXEL_FORMAT_RGB_565:
2112 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian8dccb262010-02-04 17:04:53 -08002113 break;
2114 default:
2115 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2116 }
2117
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002118 native_buffer->common.incRef(&native_buffer->common);
2119 return (EGLImageKHR)native_buffer;
2120}
2121
2122EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2123{
2124 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2125 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2126 }
2127
Iliyan Malchev697526b2011-05-01 11:33:26 -07002128 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002129
2130 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2131 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2132
Iliyan Malchev697526b2011-05-01 11:33:26 -07002133 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002134 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2135
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002136 native_buffer->common.decRef(&native_buffer->common);
2137
2138 return EGL_TRUE;
2139}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002140
2141// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002142// EGL_KHR_fence_sync
2143// ----------------------------------------------------------------------------
2144
2145#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2146
2147EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2148 const EGLint *attrib_list)
2149{
2150 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2151 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2152 }
2153
2154 if (type != EGL_SYNC_FENCE_KHR ||
2155 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2156 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2157 }
2158
2159 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2160 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2161 }
2162
2163 // AGL is synchronous; nothing to do here.
2164
2165 return FENCE_SYNC_HANDLE;
2166}
2167
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002168EGLBoolean eglDestroySyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002169{
2170 if (sync != FENCE_SYNC_HANDLE) {
2171 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2172 }
2173
2174 return EGL_TRUE;
2175}
2176
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002177EGLint eglClientWaitSyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync, EGLint /*flags*/,
2178 EGLTimeKHR /*timeout*/)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002179{
2180 if (sync != FENCE_SYNC_HANDLE) {
2181 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2182 }
2183
2184 return EGL_CONDITION_SATISFIED_KHR;
2185}
2186
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002187EGLBoolean eglGetSyncAttribKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync,
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002188 EGLint attribute, EGLint *value)
2189{
2190 if (sync != FENCE_SYNC_HANDLE) {
2191 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2192 }
2193
2194 switch (attribute) {
2195 case EGL_SYNC_TYPE_KHR:
2196 *value = EGL_SYNC_FENCE_KHR;
2197 return EGL_TRUE;
2198 case EGL_SYNC_STATUS_KHR:
2199 *value = EGL_SIGNALED_KHR;
2200 return EGL_TRUE;
2201 case EGL_SYNC_CONDITION_KHR:
2202 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2203 return EGL_TRUE;
2204 default:
2205 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2206 }
2207}
2208
2209// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002210// ANDROID extensions
2211// ----------------------------------------------------------------------------
2212
2213EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2214 EGLint left, EGLint top, EGLint width, EGLint height)
2215{
2216 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2217 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2218
2219 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002220 if (!d->isValid())
2221 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002222 if (d->dpy != dpy)
2223 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2224
2225 // post the surface
2226 d->setSwapRectangle(left, top, width, height);
2227
2228 return EGL_TRUE;
2229}