blob: 493c0b8ec9bbd8cc0fa4ee46a690ab86bb944fa7 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39#include <GLES/gl.h>
40#include <GLES/glext.h>
41
42#include <pixelflinger/format.h>
43#include <pixelflinger/pixelflinger.h>
44
45#include "context.h"
46#include "state.h"
47#include "texture.h"
48#include "matrix.h"
49
50#undef NELEM
51#define NELEM(x) (sizeof(x)/sizeof(*(x)))
52
Mathias Agopian5f2165f2012-02-24 18:25:41 -080053// ----------------------------------------------------------------------------
Mathias Agopian4b9511c2011-11-13 23:52:47 -080054
55EGLBoolean EGLAPI eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
56 EGLint left, EGLint top, EGLint width, EGLint height);
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058// ----------------------------------------------------------------------------
59namespace android {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061// ----------------------------------------------------------------------------
62
63const unsigned int NUM_DISPLAYS = 1;
64
65static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
66static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
67static pthread_key_t gEGLErrorKey = -1;
Elliott Hughes6071da72015-08-12 15:27:47 -070068#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace gl {
70pthread_key_t gGLKey = -1;
71}; // namespace gl
72#endif
73
74template<typename T>
75static T setError(GLint error, T returnValue) {
76 if (ggl_unlikely(gEGLErrorKey == -1)) {
77 pthread_mutex_lock(&gErrorKeyMutex);
78 if (gEGLErrorKey == -1)
79 pthread_key_create(&gEGLErrorKey, NULL);
80 pthread_mutex_unlock(&gErrorKeyMutex);
81 }
Colin Cross444839b2014-01-24 14:35:39 -080082 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 return returnValue;
84}
85
86static GLint getError() {
87 if (ggl_unlikely(gEGLErrorKey == -1))
88 return EGL_SUCCESS;
Colin Cross444839b2014-01-24 14:35:39 -080089 GLint error = (GLint)(uintptr_t)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -080090 if (error == 0) {
91 // The TLS key has been created by another thread, but the value for
92 // this thread has not been initialized.
93 return EGL_SUCCESS;
94 }
Colin Cross444839b2014-01-24 14:35:39 -080095 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)EGL_SUCCESS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 return error;
97}
98
99// ----------------------------------------------------------------------------
100
101struct egl_display_t
102{
103 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 static EGLBoolean is_valid(EGLDisplay dpy) {
108 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
109 }
110
Jesse Hall3aa75f92016-05-20 10:47:07 -0700111 NativeDisplayType type;
112 std::atomic_size_t initialized;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113};
114
115static egl_display_t gDisplays[NUM_DISPLAYS];
116
117egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
118 return gDisplays[uintptr_t(dpy)-1U];
119}
120
121struct egl_context_t {
122 enum {
123 IS_CURRENT = 0x00010000,
124 NEVER_CURRENT = 0x00020000
125 };
126 uint32_t flags;
127 EGLDisplay dpy;
128 EGLConfig config;
129 EGLSurface read;
130 EGLSurface draw;
131
132 static inline egl_context_t* context(EGLContext ctx) {
133 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
134 return static_cast<egl_context_t*>(gl->rasterizer.base);
135 }
136};
137
138// ----------------------------------------------------------------------------
139
140struct egl_surface_t
141{
142 enum {
143 PAGE_FLIP = 0x00000001,
144 MAGIC = 0x31415265
145 };
146
147 uint32_t magic;
148 EGLDisplay dpy;
149 EGLConfig config;
150 EGLContext ctx;
Jesse Hall78141e32013-03-07 09:56:26 -0800151 bool zombie;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152
153 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
154 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700155 bool isValid() const;
156 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
159 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700160 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700161 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 virtual EGLint getWidth() const = 0;
163 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
165 virtual EGLint getHorizontalResolution() const;
166 virtual EGLint getVerticalResolution() const;
167 virtual EGLint getRefreshRate() const;
168 virtual EGLint getSwapBehavior() const;
169 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700170 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171protected:
172 GGLSurface depth;
173};
174
175egl_surface_t::egl_surface_t(EGLDisplay dpy,
176 EGLConfig config,
177 int32_t depthFormat)
Jesse Hall78141e32013-03-07 09:56:26 -0800178 : magic(MAGIC), dpy(dpy), config(config), ctx(0), zombie(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179{
180 depth.version = sizeof(GGLSurface);
181 depth.data = 0;
182 depth.format = depthFormat;
183}
184egl_surface_t::~egl_surface_t()
185{
186 magic = 0;
187 free(depth.data);
188}
Mathias Agopian0696a572009-08-20 00:12:56 -0700189bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000190 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700191 return magic == MAGIC;
192}
193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194EGLBoolean egl_surface_t::swapBuffers() {
195 return EGL_FALSE;
196}
197EGLint egl_surface_t::getHorizontalResolution() const {
198 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
199}
200EGLint egl_surface_t::getVerticalResolution() const {
201 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
202}
203EGLint egl_surface_t::getRefreshRate() const {
204 return (60 * EGL_DISPLAY_SCALING);
205}
206EGLint egl_surface_t::getSwapBehavior() const {
207 return EGL_BUFFER_PRESERVED;
208}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700209EGLBoolean egl_surface_t::setSwapRectangle(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700210 EGLint /*l*/, EGLint /*t*/, EGLint /*w*/, EGLint /*h*/)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700211{
212 return EGL_FALSE;
213}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214
215// ----------------------------------------------------------------------------
216
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 EGLDisplay dpy, EGLConfig config,
221 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700222 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223
Mathias Agopian0696a572009-08-20 00:12:56 -0700224 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225
Mathias Agopian0696a572009-08-20 00:12:56 -0700226 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 virtual EGLBoolean swapBuffers();
228 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
229 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700230 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700231 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700232 virtual EGLint getWidth() const { return width; }
233 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 virtual EGLint getHorizontalResolution() const;
235 virtual EGLint getVerticalResolution() const;
236 virtual EGLint getRefreshRate() const;
237 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700238 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700241 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
242 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700243 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700244 ANativeWindowBuffer* buffer;
245 ANativeWindowBuffer* previousBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246 int width;
247 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700248 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700249 GGLFormat const* pixelFormatTable;
250
251 struct Rect {
252 inline Rect() { };
253 inline Rect(int32_t w, int32_t h)
254 : left(0), top(0), right(w), bottom(h) { }
255 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
256 : left(l), top(t), right(r), bottom(b) { }
257 Rect& andSelf(const Rect& r) {
258 left = max(left, r.left);
259 top = max(top, r.top);
260 right = min(right, r.right);
261 bottom = min(bottom, r.bottom);
262 return *this;
263 }
264 bool isEmpty() const {
265 return (left>=right || top>=bottom);
266 }
267 void dump(char const* what) {
Steve Block9d453682011-12-20 16:23:08 +0000268 ALOGD("%s { %5d, %5d, w=%5d, h=%5d }",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700269 what, left, top, right-left, bottom-top);
270 }
271
272 int32_t left;
273 int32_t top;
274 int32_t right;
275 int32_t bottom;
276 };
277
278 struct Region {
279 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700280 typedef Rect const* const_iterator;
281 const_iterator begin() const { return storage; }
282 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700283 static Region subtract(const Rect& lhs, const Rect& rhs) {
284 Region reg;
285 Rect* storage = reg.storage;
286 if (!lhs.isEmpty()) {
287 if (lhs.top < rhs.top) { // top rect
288 storage->left = lhs.left;
289 storage->top = lhs.top;
290 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700291 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700292 storage++;
293 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700294 const int32_t top = max(lhs.top, rhs.top);
295 const int32_t bot = min(lhs.bottom, rhs.bottom);
296 if (top < bot) {
297 if (lhs.left < rhs.left) { // left-side rect
298 storage->left = lhs.left;
299 storage->top = top;
300 storage->right = rhs.left;
301 storage->bottom = bot;
302 storage++;
303 }
304 if (lhs.right > rhs.right) { // right-side rect
305 storage->left = rhs.right;
306 storage->top = top;
307 storage->right = lhs.right;
308 storage->bottom = bot;
309 storage++;
310 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700311 }
312 if (lhs.bottom > rhs.bottom) { // bottom rect
313 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700314 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700315 storage->right = lhs.right;
316 storage->bottom = lhs.bottom;
317 storage++;
318 }
319 reg.count = storage - reg.storage;
320 }
321 return reg;
322 }
323 bool isEmpty() const {
324 return count<=0;
325 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700326 private:
327 Rect storage[4];
328 ssize_t count;
329 };
330
331 void copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700332 ANativeWindowBuffer* dst, void* dst_vaddr,
333 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700334 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700335
336 Rect dirtyRegion;
337 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338};
339
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700340egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 EGLConfig config,
342 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700343 ANativeWindow* window)
Dan Stoza204240a2016-01-08 10:52:16 -0800344 : egl_surface_t(dpy, config, depthFormat),
345 nativeWindow(window), buffer(0), previousBuffer(0), bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700347
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700348 pixelFormatTable = gglGetPixelFormatTable();
Dan Stoza204240a2016-01-08 10:52:16 -0800349
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700350 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700351 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700352 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
353 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700354}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700356egl_window_surface_v2_t::~egl_window_surface_v2_t() {
357 if (buffer) {
358 buffer->common.decRef(&buffer->common);
359 }
360 if (previousBuffer) {
361 previousBuffer->common.decRef(&previousBuffer->common);
362 }
363 nativeWindow->common.decRef(&nativeWindow->common);
364}
365
Mathias Agopiancf81c842009-07-31 14:47:00 -0700366EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700367{
Mathias Agopian52212712009-08-11 22:34:02 -0700368 // we're intending to do software rendering
369 native_window_set_usage(nativeWindow,
370 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
371
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700372 // dequeue a buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700373 int fenceFd = -1;
374 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer,
375 &fenceFd) != NO_ERROR) {
376 return setError(EGL_BAD_ALLOC, EGL_FALSE);
377 }
378
379 // wait for the buffer
380 sp<Fence> fence(new Fence(fenceFd));
381 if (fence->wait(Fence::TIMEOUT_NEVER) != NO_ERROR) {
382 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700383 return setError(EGL_BAD_ALLOC, EGL_FALSE);
384 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700385
386 // allocate a corresponding depth-buffer
387 width = buffer->width;
388 height = buffer->height;
389 if (depth.format) {
390 depth.width = width;
391 depth.height = height;
392 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700393 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
394 static_cast<uint64_t>(depth.height) * 2;
395 if (depth.stride < 0 || depth.height > INT_MAX ||
396 allocSize > UINT32_MAX) {
397 return setError(EGL_BAD_ALLOC, EGL_FALSE);
398 }
399 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700400 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700401 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700402 }
403 }
404
405 // keep a reference on the buffer
406 buffer->common.incRef(&buffer->common);
407
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700408 // pin the buffer down
409 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
410 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000411 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700412 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700413 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700414 // FIXME: we should make sure we're not accessing the buffer anymore
415 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700416 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700417}
418
419void egl_window_surface_v2_t::disconnect()
420{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700421 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700422 bits = NULL;
423 unlock(buffer);
424 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700425 if (buffer) {
Jesse Hall5f555562013-03-07 15:14:18 -0800426 nativeWindow->cancelBuffer(nativeWindow, buffer, -1);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700427 buffer->common.decRef(&buffer->common);
428 buffer = 0;
429 }
430 if (previousBuffer) {
431 previousBuffer->common.decRef(&previousBuffer->common);
432 previousBuffer = 0;
433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434}
435
Mathias Agopian0926f502009-05-04 14:17:04 -0700436status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700437 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700438{
Dan Stoza204240a2016-01-08 10:52:16 -0800439 auto& mapper = GraphicBufferMapper::get();
440 return mapper.lock(buf->handle, usage,
441 android::Rect(buf->width, buf->height), vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700442}
443
Iliyan Malchev697526b2011-05-01 11:33:26 -0700444status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700445{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700446 if (!buf) return BAD_VALUE;
Dan Stoza204240a2016-01-08 10:52:16 -0800447 auto& mapper = GraphicBufferMapper::get();
448 return mapper.unlock(buf->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700449}
450
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700451void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700452 ANativeWindowBuffer* dst, void* dst_vaddr,
453 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700454 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700455{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700456 // NOTE: dst and src must be the same format
457
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700458 Region::const_iterator cur = clip.begin();
459 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700460
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700461 const size_t bpp = pixelFormatTable[src->format].size;
462 const size_t dbpr = dst->stride * bpp;
463 const size_t sbpr = src->stride * bpp;
464
465 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
466 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
467
468 while (cur != end) {
469 const Rect& r(*cur++);
470 ssize_t w = r.right - r.left;
471 ssize_t h = r.bottom - r.top;
472 if (w <= 0 || h<=0) continue;
473 size_t size = w * bpp;
474 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
475 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
476 if (dbpr==sbpr && size==sbpr) {
477 size *= h;
478 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700479 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700480 do {
481 memcpy(d, s, size);
482 d += dbpr;
483 s += sbpr;
484 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700485 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486}
487
488EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700490 if (!buffer) {
491 return setError(EGL_BAD_ACCESS, EGL_FALSE);
492 }
493
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700494 /*
495 * Handle eglSetSwapRectangleANDROID()
496 * We copyback from the front buffer
497 */
498 if (!dirtyRegion.isEmpty()) {
499 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
500 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100501 // This was const Region copyBack, but that causes an
502 // internal compile error on simulator builds
503 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700504 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700505 void* prevBits;
506 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700507 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
508 // copy from previousBuffer to buffer
509 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700510 unlock(previousBuffer);
511 }
512 }
513 }
514 oldDirtyRegion = dirtyRegion;
515 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700516
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700517 if (previousBuffer) {
518 previousBuffer->common.decRef(&previousBuffer->common);
519 previousBuffer = 0;
520 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521
Mathias Agopian0926f502009-05-04 14:17:04 -0700522 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700523 previousBuffer = buffer;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700524 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700525 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700526
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700527 // dequeue a new buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700528 int fenceFd = -1;
529 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) == NO_ERROR) {
530 sp<Fence> fence(new Fence(fenceFd));
531 if (fence->wait(Fence::TIMEOUT_NEVER)) {
532 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
533 return setError(EGL_BAD_ALLOC, EGL_FALSE);
534 }
Mathias Agopian031213e2010-08-18 16:07:34 -0700535
536 // reallocate the depth-buffer if needed
537 if ((width != buffer->width) || (height != buffer->height)) {
538 // TODO: we probably should reset the swap rect here
539 // if the window size has changed
540 width = buffer->width;
541 height = buffer->height;
542 if (depth.data) {
543 free(depth.data);
544 depth.width = width;
545 depth.height = height;
546 depth.stride = buffer->stride;
Michael Lentine95303882015-05-28 17:43:06 -0700547 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
548 static_cast<uint64_t>(depth.height) * 2;
549 if (depth.stride < 0 || depth.height > INT_MAX ||
550 allocSize > UINT32_MAX) {
551 setError(EGL_BAD_ALLOC, EGL_FALSE);
552 return EGL_FALSE;
553 }
554 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopian031213e2010-08-18 16:07:34 -0700555 if (depth.data == 0) {
556 setError(EGL_BAD_ALLOC, EGL_FALSE);
557 return EGL_FALSE;
558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
560 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700561
Mathias Agopian031213e2010-08-18 16:07:34 -0700562 // keep a reference on the buffer
563 buffer->common.incRef(&buffer->common);
564
565 // finally pin the buffer down
566 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
567 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000568 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700569 buffer, buffer->width, buffer->height);
570 return setError(EGL_BAD_ACCESS, EGL_FALSE);
571 // FIXME: we should make sure we're not accessing the buffer anymore
572 }
573 } else {
574 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700575 }
576
577 return EGL_TRUE;
578}
579
580EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
581 EGLint l, EGLint t, EGLint w, EGLint h)
582{
583 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 return EGL_TRUE;
585}
586
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700587EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588{
589 GGLSurface buffer;
590 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 buffer.width = this->buffer->width;
592 buffer.height = this->buffer->height;
593 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700594 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 gl->rasterizer.procs.colorBuffer(gl, &buffer);
597 if (depth.data != gl->rasterizer.state.buffers.depth.data)
598 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700599
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 return EGL_TRUE;
601}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700602EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603{
604 GGLSurface buffer;
605 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 buffer.width = this->buffer->width;
607 buffer.height = this->buffer->height;
608 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700609 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700610 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 gl->rasterizer.procs.readBuffer(gl, &buffer);
612 return EGL_TRUE;
613}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
619}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620EGLint egl_window_surface_v2_t::getRefreshRate() const {
621 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700623EGLint egl_window_surface_v2_t::getSwapBehavior() const
624{
625 /*
626 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
627 * the content of the swapped buffer.
628 *
629 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
630 *
631 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
632 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
633 * is, everything outside of this area is preserved.
634 *
635 * This implementation of EGL assumes the later case.
636 *
637 */
638
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640}
641
642// ----------------------------------------------------------------------------
643
644struct egl_pixmap_surface_t : public egl_surface_t
645{
646 egl_pixmap_surface_t(
647 EGLDisplay dpy, EGLConfig config,
648 int32_t depthFormat,
649 egl_native_pixmap_t const * pixmap);
650
651 virtual ~egl_pixmap_surface_t() { }
652
Mathias Agopian0696a572009-08-20 00:12:56 -0700653 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
655 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
656 virtual EGLint getWidth() const { return nativePixmap.width; }
657 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658private:
659 egl_native_pixmap_t nativePixmap;
660};
661
662egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
663 EGLConfig config,
664 int32_t depthFormat,
665 egl_native_pixmap_t const * pixmap)
666 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
667{
668 if (depthFormat) {
669 depth.width = pixmap->width;
670 depth.height = pixmap->height;
671 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700672 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
673 static_cast<uint64_t>(depth.height) * 2;
674 if (depth.stride < 0 || depth.height > INT_MAX ||
675 allocSize > UINT32_MAX) {
676 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
677 return;
678 }
679 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 if (depth.data == 0) {
681 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 }
683 }
684}
685EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
686{
687 GGLSurface buffer;
688 buffer.version = sizeof(GGLSurface);
689 buffer.width = nativePixmap.width;
690 buffer.height = nativePixmap.height;
691 buffer.stride = nativePixmap.stride;
692 buffer.data = nativePixmap.data;
693 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 gl->rasterizer.procs.colorBuffer(gl, &buffer);
696 if (depth.data != gl->rasterizer.state.buffers.depth.data)
697 gl->rasterizer.procs.depthBuffer(gl, &depth);
698 return EGL_TRUE;
699}
700EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
701{
702 GGLSurface buffer;
703 buffer.version = sizeof(GGLSurface);
704 buffer.width = nativePixmap.width;
705 buffer.height = nativePixmap.height;
706 buffer.stride = nativePixmap.stride;
707 buffer.data = nativePixmap.data;
708 buffer.format = nativePixmap.format;
709 gl->rasterizer.procs.readBuffer(gl, &buffer);
710 return EGL_TRUE;
711}
712
713// ----------------------------------------------------------------------------
714
715struct egl_pbuffer_surface_t : public egl_surface_t
716{
717 egl_pbuffer_surface_t(
718 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
719 int32_t w, int32_t h, int32_t f);
720
721 virtual ~egl_pbuffer_surface_t();
722
Mathias Agopian0696a572009-08-20 00:12:56 -0700723 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
725 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
726 virtual EGLint getWidth() const { return pbuffer.width; }
727 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728private:
729 GGLSurface pbuffer;
730};
731
732egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
733 EGLConfig config, int32_t depthFormat,
734 int32_t w, int32_t h, int32_t f)
735 : egl_surface_t(dpy, config, depthFormat)
736{
737 size_t size = w*h;
738 switch (f) {
739 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
740 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
741 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800742 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000744 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 pbuffer.data = 0;
746 break;
747 }
748 pbuffer.version = sizeof(GGLSurface);
749 pbuffer.width = w;
750 pbuffer.height = h;
751 pbuffer.stride = w;
752 pbuffer.data = (GGLubyte*)malloc(size);
753 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 if (depthFormat) {
756 depth.width = pbuffer.width;
757 depth.height = pbuffer.height;
758 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700759 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
760 static_cast<uint64_t>(depth.height) * 2;
761 if (depth.stride < 0 || depth.height > INT_MAX ||
762 allocSize > UINT32_MAX) {
763 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
764 return;
765 }
766 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 if (depth.data == 0) {
768 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
769 return;
770 }
771 }
772}
773egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
774 free(pbuffer.data);
775}
776EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
777{
778 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
779 if (depth.data != gl->rasterizer.state.buffers.depth.data)
780 gl->rasterizer.procs.depthBuffer(gl, &depth);
781 return EGL_TRUE;
782}
783EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
784{
785 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
786 return EGL_TRUE;
787}
788
789// ----------------------------------------------------------------------------
790
791struct config_pair_t {
792 GLint key;
793 GLint value;
794};
795
796struct configs_t {
797 const config_pair_t* array;
798 int size;
799};
800
801struct config_management_t {
802 GLint key;
803 bool (*match)(GLint reqValue, GLint confValue);
804 static bool atLeast(GLint reqValue, GLint confValue) {
805 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
806 }
807 static bool exact(GLint reqValue, GLint confValue) {
808 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
809 }
810 static bool mask(GLint reqValue, GLint confValue) {
811 return (confValue & reqValue) == reqValue;
812 }
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700813 static bool ignore(GLint /*reqValue*/, GLint /*confValue*/) {
Mathias Agopian63971672010-10-25 15:51:24 -0700814 return true;
815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816};
817
818// ----------------------------------------------------------------------------
819
820#define VERSION_MAJOR 1
821#define VERSION_MINOR 2
822static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700823static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700824static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700825static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700826 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700827 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700828 // "KHR_image_pixmap "
829 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700830 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700831 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832
833// ----------------------------------------------------------------------------
834
835struct extention_map_t {
836 const char * const name;
837 __eglMustCastToProperFunctionPointerType address;
838};
839
840static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700841 { "glDrawTexsOES",
842 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
843 { "glDrawTexiOES",
844 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
845 { "glDrawTexfOES",
846 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
847 { "glDrawTexxOES",
848 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
849 { "glDrawTexsvOES",
850 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
851 { "glDrawTexivOES",
852 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
853 { "glDrawTexfvOES",
854 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
855 { "glDrawTexxvOES",
856 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
857 { "glQueryMatrixxOES",
858 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
859 { "glEGLImageTargetTexture2DOES",
860 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
861 { "glEGLImageTargetRenderbufferStorageOES",
862 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
863 { "glClipPlanef",
864 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
865 { "glClipPlanex",
866 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
867 { "glBindBuffer",
868 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
869 { "glBufferData",
870 (__eglMustCastToProperFunctionPointerType)&glBufferData },
871 { "glBufferSubData",
872 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
873 { "glDeleteBuffers",
874 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
875 { "glGenBuffers",
876 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700877 { "eglCreateImageKHR",
878 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
879 { "eglDestroyImageKHR",
880 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700881 { "eglCreateSyncKHR",
882 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
883 { "eglDestroySyncKHR",
884 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
885 { "eglClientWaitSyncKHR",
886 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
887 { "eglGetSyncAttribKHR",
888 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700889 { "eglSetSwapRectangleANDROID",
890 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891};
892
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700893/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 * In the lists below, attributes names MUST be sorted.
895 * Additionally, all configs must be sorted according to
896 * the EGL specification.
897 */
898
899static config_pair_t const config_base_attribute_list[] = {
900 { EGL_STENCIL_SIZE, 0 },
901 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
902 { EGL_LEVEL, 0 },
903 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700904 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
906 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
907 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
908 { EGL_NATIVE_VISUAL_ID, 0 },
909 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
910 { EGL_SAMPLES, 0 },
911 { EGL_SAMPLE_BUFFERS, 0 },
912 { EGL_TRANSPARENT_TYPE, EGL_NONE },
913 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
914 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
915 { EGL_TRANSPARENT_RED_VALUE, 0 },
916 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
917 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
918 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700919 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700920 { EGL_LUMINANCE_SIZE, 0 },
921 { EGL_ALPHA_MASK_SIZE, 0 },
922 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700923 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700924 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925};
926
927// These configs can override the base attribute list
928// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
929
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800930// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931static config_pair_t const config_0_attribute_list[] = {
932 { EGL_BUFFER_SIZE, 16 },
933 { EGL_ALPHA_SIZE, 0 },
934 { EGL_BLUE_SIZE, 5 },
935 { EGL_GREEN_SIZE, 6 },
936 { EGL_RED_SIZE, 5 },
937 { EGL_DEPTH_SIZE, 0 },
938 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700939 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
941};
942
943static config_pair_t const config_1_attribute_list[] = {
944 { EGL_BUFFER_SIZE, 16 },
945 { EGL_ALPHA_SIZE, 0 },
946 { EGL_BLUE_SIZE, 5 },
947 { EGL_GREEN_SIZE, 6 },
948 { EGL_RED_SIZE, 5 },
949 { EGL_DEPTH_SIZE, 16 },
950 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700951 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
953};
954
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800955// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956static config_pair_t const config_2_attribute_list[] = {
957 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800958 { EGL_ALPHA_SIZE, 0 },
959 { EGL_BLUE_SIZE, 8 },
960 { EGL_GREEN_SIZE, 8 },
961 { EGL_RED_SIZE, 8 },
962 { EGL_DEPTH_SIZE, 0 },
963 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700964 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800965 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
966};
967
968static config_pair_t const config_3_attribute_list[] = {
969 { EGL_BUFFER_SIZE, 32 },
970 { EGL_ALPHA_SIZE, 0 },
971 { EGL_BLUE_SIZE, 8 },
972 { EGL_GREEN_SIZE, 8 },
973 { EGL_RED_SIZE, 8 },
974 { EGL_DEPTH_SIZE, 16 },
975 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700976 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800977 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
978};
979
980// 8888 configs
981static config_pair_t const config_4_attribute_list[] = {
982 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 { EGL_ALPHA_SIZE, 8 },
984 { EGL_BLUE_SIZE, 8 },
985 { EGL_GREEN_SIZE, 8 },
986 { EGL_RED_SIZE, 8 },
987 { EGL_DEPTH_SIZE, 0 },
988 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700989 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
991};
992
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800993static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 { EGL_BUFFER_SIZE, 32 },
995 { EGL_ALPHA_SIZE, 8 },
996 { EGL_BLUE_SIZE, 8 },
997 { EGL_GREEN_SIZE, 8 },
998 { EGL_RED_SIZE, 8 },
999 { EGL_DEPTH_SIZE, 16 },
1000 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001001 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1003};
1004
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001005// A8 configs
1006static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 { EGL_BUFFER_SIZE, 8 },
1008 { EGL_ALPHA_SIZE, 8 },
1009 { EGL_BLUE_SIZE, 0 },
1010 { EGL_GREEN_SIZE, 0 },
1011 { EGL_RED_SIZE, 0 },
1012 { EGL_DEPTH_SIZE, 0 },
1013 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001014 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1016};
1017
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001018static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 { EGL_BUFFER_SIZE, 8 },
1020 { EGL_ALPHA_SIZE, 8 },
1021 { EGL_BLUE_SIZE, 0 },
1022 { EGL_GREEN_SIZE, 0 },
1023 { EGL_RED_SIZE, 0 },
1024 { EGL_DEPTH_SIZE, 16 },
1025 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001026 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1028};
1029
1030static configs_t const gConfigs[] = {
1031 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1032 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1033 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1034 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1035 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1036 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001037 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1038 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039};
1040
1041static config_management_t const gConfigManagement[] = {
1042 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1043 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1044 { EGL_BLUE_SIZE, config_management_t::atLeast },
1045 { EGL_GREEN_SIZE, config_management_t::atLeast },
1046 { EGL_RED_SIZE, config_management_t::atLeast },
1047 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1048 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1049 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1050 { EGL_CONFIG_ID, config_management_t::exact },
1051 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001052 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1053 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1054 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001056 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1058 { EGL_SAMPLES, config_management_t::exact },
1059 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1060 { EGL_SURFACE_TYPE, config_management_t::mask },
1061 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1062 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1063 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1064 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1065 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1066 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1067 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1068 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001069 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1070 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1071 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1072 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1073 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074};
1075
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001078 // attributes that are not specified are simply ignored, if a particular
1079 // one needs not be ignored, it must be specified here, eg:
1080 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081};
1082
1083// ----------------------------------------------------------------------------
1084
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001085static status_t getConfigFormatInfo(EGLint configID,
1086 int32_t& pixelFormat, int32_t& depthFormat)
1087{
1088 switch(configID) {
1089 case 0:
1090 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1091 depthFormat = 0;
1092 break;
1093 case 1:
1094 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1095 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1096 break;
1097 case 2:
1098 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1099 depthFormat = 0;
1100 break;
1101 case 3:
1102 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1103 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1104 break;
1105 case 4:
1106 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1107 depthFormat = 0;
1108 break;
1109 case 5:
1110 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1111 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1112 break;
1113 case 6:
1114 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1115 depthFormat = 0;
1116 break;
1117 case 7:
1118 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1119 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1120 break;
1121 default:
1122 return NAME_NOT_FOUND;
1123 }
1124 return NO_ERROR;
1125}
1126
1127// ----------------------------------------------------------------------------
1128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129template<typename T>
1130static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1131{
1132 while (first <= last) {
1133 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001134 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001136 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 last = mid - 1;
1138 } else {
1139 return mid;
1140 }
1141 }
1142 return -1;
1143}
1144
1145static int isAttributeMatching(int i, EGLint attr, EGLint val)
1146{
1147 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001148 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 int index = binarySearch<config_pair_t>(
1150 gConfigs[i].array,
1151 0, gConfigs[i].size-1,
1152 attr);
1153 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001154 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 index = binarySearch<config_pair_t>(
1156 config_base_attribute_list,
1157 0, NELEM(config_base_attribute_list)-1,
1158 attr);
1159 }
1160 if (index >= 0) {
1161 // attribute found, check if this config could match
1162 int cfgMgtIndex = binarySearch<config_management_t>(
1163 gConfigManagement,
1164 0, NELEM(gConfigManagement)-1,
1165 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001166 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 bool match = gConfigManagement[cfgMgtIndex].match(
1168 val, configFound[index].value);
1169 if (match) {
1170 // this config matches
1171 return 1;
1172 }
1173 } else {
1174 // attribute not found. this should NEVER happen.
1175 }
1176 } else {
1177 // error, this attribute doesn't exist
1178 }
1179 return 0;
1180}
1181
1182static int makeCurrent(ogles_context_t* gl)
1183{
1184 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1185 if (gl) {
1186 egl_context_t* c = egl_context_t::context(gl);
1187 if (c->flags & egl_context_t::IS_CURRENT) {
1188 if (current != gl) {
1189 // it is an error to set a context current, if it's already
1190 // current to another thread
1191 return -1;
1192 }
1193 } else {
1194 if (current) {
1195 // mark the current context as not current, and flush
1196 glFlush();
1197 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1198 }
1199 }
1200 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1201 // The context is not current, make it current!
1202 setGlThreadSpecific(gl);
1203 c->flags |= egl_context_t::IS_CURRENT;
1204 }
1205 } else {
1206 if (current) {
1207 // mark the current context as not current, and flush
1208 glFlush();
1209 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1210 }
1211 // this thread has no context attached to it
1212 setGlThreadSpecific(0);
1213 }
1214 return 0;
1215}
1216
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001217static EGLBoolean getConfigAttrib(EGLDisplay /*dpy*/, EGLConfig config,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 EGLint attribute, EGLint *value)
1219{
1220 size_t numConfigs = NELEM(gConfigs);
Colin Cross444839b2014-01-24 14:35:39 -08001221 int index = (int)(uintptr_t)config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 if (uint32_t(index) >= numConfigs)
1223 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1224
1225 int attrIndex;
1226 attrIndex = binarySearch<config_pair_t>(
1227 gConfigs[index].array,
1228 0, gConfigs[index].size-1,
1229 attribute);
1230 if (attrIndex>=0) {
1231 *value = gConfigs[index].array[attrIndex].value;
1232 return EGL_TRUE;
1233 }
1234
1235 attrIndex = binarySearch<config_pair_t>(
1236 config_base_attribute_list,
1237 0, NELEM(config_base_attribute_list)-1,
1238 attribute);
1239 if (attrIndex>=0) {
1240 *value = config_base_attribute_list[attrIndex].value;
1241 return EGL_TRUE;
1242 }
1243 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1244}
1245
1246static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001247 NativeWindowType window, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248{
1249 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1250 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1251 if (window == 0)
1252 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1253
1254 EGLint surfaceType;
1255 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1256 return EGL_FALSE;
1257
1258 if (!(surfaceType & EGL_WINDOW_BIT))
1259 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1260
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001261 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001262 ANDROID_NATIVE_WINDOW_MAGIC) {
1263 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1264 }
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 EGLint configID;
1267 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1268 return EGL_FALSE;
1269
1270 int32_t depthFormat;
1271 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001272 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1274 }
1275
1276 // FIXME: we don't have access to the pixelFormat here just yet.
1277 // (it's possible that the surface is not fully initialized)
1278 // maybe this should be done after the page-flip
1279 //if (EGLint(info.format) != pixelFormat)
1280 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1281
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282 egl_surface_t* surface;
1283 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001284 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285
Mathias Agopian0696a572009-08-20 00:12:56 -07001286 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 // there was a problem in the ctor, the error
1288 // flag has been set.
1289 delete surface;
1290 surface = 0;
1291 }
1292 return surface;
1293}
1294
1295static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001296 NativePixmapType pixmap, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
1298 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1299 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1300 if (pixmap == 0)
1301 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1302
1303 EGLint surfaceType;
1304 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1305 return EGL_FALSE;
1306
1307 if (!(surfaceType & EGL_PIXMAP_BIT))
1308 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1309
Mathias Agopian0696a572009-08-20 00:12:56 -07001310 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1311 sizeof(egl_native_pixmap_t)) {
1312 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1313 }
1314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 EGLint configID;
1316 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1317 return EGL_FALSE;
1318
1319 int32_t depthFormat;
1320 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001321 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1323 }
1324
1325 if (pixmap->format != pixelFormat)
1326 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1327
1328 egl_surface_t* surface =
1329 new egl_pixmap_surface_t(dpy, config, depthFormat,
1330 static_cast<egl_native_pixmap_t*>(pixmap));
1331
Mathias Agopian0696a572009-08-20 00:12:56 -07001332 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 // there was a problem in the ctor, the error
1334 // flag has been set.
1335 delete surface;
1336 surface = 0;
1337 }
1338 return surface;
1339}
1340
1341static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1342 const EGLint *attrib_list)
1343{
1344 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1345 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1346
1347 EGLint surfaceType;
1348 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1349 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 if (!(surfaceType & EGL_PBUFFER_BIT))
1352 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 EGLint configID;
1355 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1356 return EGL_FALSE;
1357
1358 int32_t depthFormat;
1359 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001360 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1362 }
1363
1364 int32_t w = 0;
1365 int32_t h = 0;
Jesse Hall2e8ca9d2015-08-21 07:41:46 -07001366 while (attrib_list[0] != EGL_NONE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1368 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1369 attrib_list+=2;
1370 }
1371
1372 egl_surface_t* surface =
1373 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1374
Mathias Agopian0696a572009-08-20 00:12:56 -07001375 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 // there was a problem in the ctor, the error
1377 // flag has been set.
1378 delete surface;
1379 surface = 0;
1380 }
1381 return surface;
1382}
1383
1384// ----------------------------------------------------------------------------
1385}; // namespace android
1386// ----------------------------------------------------------------------------
1387
1388using namespace android;
1389
1390// ----------------------------------------------------------------------------
1391// Initialization
1392// ----------------------------------------------------------------------------
1393
1394EGLDisplay eglGetDisplay(NativeDisplayType display)
1395{
Elliott Hughes6071da72015-08-12 15:27:47 -07001396#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 // this just needs to be done once
1398 if (gGLKey == -1) {
1399 pthread_mutex_lock(&gInitMutex);
1400 if (gGLKey == -1)
1401 pthread_key_create(&gGLKey, NULL);
1402 pthread_mutex_unlock(&gInitMutex);
1403 }
1404#endif
1405 if (display == EGL_DEFAULT_DISPLAY) {
1406 EGLDisplay dpy = (EGLDisplay)1;
1407 egl_display_t& d = egl_display_t::get_display(dpy);
1408 d.type = display;
1409 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001410 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 return EGL_NO_DISPLAY;
1412}
1413
1414EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1415{
1416 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1417 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 EGLBoolean res = EGL_TRUE;
1420 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001421
Jesse Hall3aa75f92016-05-20 10:47:07 -07001422 if (d.initialized.fetch_add(1, std::memory_order_acquire) == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 // initialize stuff here if needed
1424 //pthread_mutex_lock(&gInitMutex);
1425 //pthread_mutex_unlock(&gInitMutex);
1426 }
1427
1428 if (res == EGL_TRUE) {
1429 if (major != NULL) *major = VERSION_MAJOR;
1430 if (minor != NULL) *minor = VERSION_MINOR;
1431 }
1432 return res;
1433}
1434
1435EGLBoolean eglTerminate(EGLDisplay dpy)
1436{
1437 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1438 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1439
1440 EGLBoolean res = EGL_TRUE;
1441 egl_display_t& d = egl_display_t::get_display(dpy);
Jesse Hall3aa75f92016-05-20 10:47:07 -07001442 if (d.initialized.fetch_sub(1, std::memory_order_release) == 1) {
1443 std::atomic_thread_fence(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 // TODO: destroy all resources (surfaces, contexts, etc...)
1445 //pthread_mutex_lock(&gInitMutex);
1446 //pthread_mutex_unlock(&gInitMutex);
1447 }
1448 return res;
1449}
1450
1451// ----------------------------------------------------------------------------
1452// configuration
1453// ----------------------------------------------------------------------------
1454
1455EGLBoolean eglGetConfigs( EGLDisplay dpy,
1456 EGLConfig *configs,
1457 EGLint config_size, EGLint *num_config)
1458{
1459 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1460 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1461
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001462 if (ggl_unlikely(num_config==NULL))
1463 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1464
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 GLint numConfigs = NELEM(gConfigs);
1466 if (!configs) {
1467 *num_config = numConfigs;
1468 return EGL_TRUE;
1469 }
1470 GLint i;
1471 for (i=0 ; i<numConfigs && i<config_size ; i++) {
Colin Cross444839b2014-01-24 14:35:39 -08001472 *configs++ = (EGLConfig)(uintptr_t)i;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 }
1474 *num_config = i;
1475 return EGL_TRUE;
1476}
1477
1478EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1479 EGLConfig *configs, EGLint config_size,
1480 EGLint *num_config)
1481{
1482 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1483 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001484
1485 if (ggl_unlikely(num_config==NULL)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001486 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1487 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488
Jack Palevich749c63d2009-03-25 15:12:17 -07001489 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001490 /*
1491 * A NULL attrib_list should be treated as though it was an empty
1492 * one (terminated with EGL_NONE) as defined in
1493 * section 3.4.1 "Querying Configurations" in the EGL specification.
1494 */
1495 static const EGLint dummy = EGL_NONE;
1496 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001498
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 int numAttributes = 0;
1500 int numConfigs = NELEM(gConfigs);
1501 uint32_t possibleMatch = (1<<numConfigs)-1;
1502 while(possibleMatch && *attrib_list != EGL_NONE) {
1503 numAttributes++;
1504 EGLint attr = *attrib_list++;
1505 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001506 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 if (!(possibleMatch & (1<<i)))
1508 continue;
1509 if (isAttributeMatching(i, attr, val) == 0) {
1510 possibleMatch &= ~(1<<i);
1511 }
1512 }
1513 }
1514
1515 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001516 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1517 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 // default value
1519 if (binarySearch<config_pair_t>(
1520 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001521 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 config_defaults[j].key) < 0)
1523 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001524 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 if (!(possibleMatch & (1<<i)))
1526 continue;
1527 if (isAttributeMatching(i,
1528 config_defaults[j].key,
1529 config_defaults[j].value) == 0)
1530 {
1531 possibleMatch &= ~(1<<i);
1532 }
1533 }
1534 }
1535 }
1536
1537 // return the configurations found
1538 int n=0;
1539 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001540 if (configs) {
1541 for (int i=0 ; config_size && i<numConfigs ; i++) {
1542 if (possibleMatch & (1<<i)) {
Colin Cross444839b2014-01-24 14:35:39 -08001543 *configs++ = (EGLConfig)(uintptr_t)i;
Jack Palevich749c63d2009-03-25 15:12:17 -07001544 config_size--;
1545 n++;
1546 }
1547 }
1548 } else {
1549 for (int i=0 ; i<numConfigs ; i++) {
1550 if (possibleMatch & (1<<i)) {
1551 n++;
1552 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 }
1554 }
1555 }
1556 *num_config = n;
1557 return EGL_TRUE;
1558}
1559
1560EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1561 EGLint attribute, EGLint *value)
1562{
1563 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1564 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1565
1566 return getConfigAttrib(dpy, config, attribute, value);
1567}
1568
1569// ----------------------------------------------------------------------------
1570// surfaces
1571// ----------------------------------------------------------------------------
1572
1573EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1574 NativeWindowType window,
1575 const EGLint *attrib_list)
1576{
1577 return createWindowSurface(dpy, config, window, attrib_list);
1578}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1581 NativePixmapType pixmap,
1582 const EGLint *attrib_list)
1583{
1584 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1585}
1586
1587EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1588 const EGLint *attrib_list)
1589{
1590 return createPbufferSurface(dpy, config, attrib_list);
1591}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001592
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1594{
1595 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1596 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1597 if (eglSurface != EGL_NO_SURFACE) {
1598 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001599 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1601 if (surface->dpy != dpy)
1602 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001603 if (surface->ctx) {
Jesse Hall78141e32013-03-07 09:56:26 -08001604 // defer disconnect/delete until no longer current
1605 surface->zombie = true;
1606 } else {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001607 surface->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001608 delete surface;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001609 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 }
1611 return EGL_TRUE;
1612}
1613
1614EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1615 EGLint attribute, EGLint *value)
1616{
1617 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1618 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1619 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001620 if (!surface->isValid())
1621 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 if (surface->dpy != dpy)
1623 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1624
1625 EGLBoolean ret = EGL_TRUE;
1626 switch (attribute) {
1627 case EGL_CONFIG_ID:
1628 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1629 break;
1630 case EGL_WIDTH:
1631 *value = surface->getWidth();
1632 break;
1633 case EGL_HEIGHT:
1634 *value = surface->getHeight();
1635 break;
1636 case EGL_LARGEST_PBUFFER:
1637 // not modified for a window or pixmap surface
1638 break;
1639 case EGL_TEXTURE_FORMAT:
1640 *value = EGL_NO_TEXTURE;
1641 break;
1642 case EGL_TEXTURE_TARGET:
1643 *value = EGL_NO_TEXTURE;
1644 break;
1645 case EGL_MIPMAP_TEXTURE:
1646 *value = EGL_FALSE;
1647 break;
1648 case EGL_MIPMAP_LEVEL:
1649 *value = 0;
1650 break;
1651 case EGL_RENDER_BUFFER:
1652 // TODO: return the real RENDER_BUFFER here
1653 *value = EGL_BACK_BUFFER;
1654 break;
1655 case EGL_HORIZONTAL_RESOLUTION:
1656 // pixel/mm * EGL_DISPLAY_SCALING
1657 *value = surface->getHorizontalResolution();
1658 break;
1659 case EGL_VERTICAL_RESOLUTION:
1660 // pixel/mm * EGL_DISPLAY_SCALING
1661 *value = surface->getVerticalResolution();
1662 break;
1663 case EGL_PIXEL_ASPECT_RATIO: {
1664 // w/h * EGL_DISPLAY_SCALING
1665 int wr = surface->getHorizontalResolution();
1666 int hr = surface->getVerticalResolution();
1667 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1668 } break;
1669 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001670 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 break;
1672 default:
1673 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1674 }
1675 return ret;
1676}
1677
1678EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001679 EGLContext /*share_list*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680{
1681 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1682 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1683
1684 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1685 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1686
1687 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1688 c->flags = egl_context_t::NEVER_CURRENT;
1689 c->dpy = dpy;
1690 c->config = config;
1691 c->read = 0;
1692 c->draw = 0;
1693 return (EGLContext)gl;
1694}
1695
1696EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1697{
1698 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1699 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1700 egl_context_t* c = egl_context_t::context(ctx);
1701 if (c->flags & egl_context_t::IS_CURRENT)
1702 setGlThreadSpecific(0);
1703 ogles_uninit((ogles_context_t*)ctx);
1704 return EGL_TRUE;
1705}
1706
1707EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1708 EGLSurface read, EGLContext ctx)
1709{
1710 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1711 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1712 if (draw) {
1713 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001714 if (!s->isValid())
1715 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 if (s->dpy != dpy)
1717 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001718 // TODO: check that draw is compatible with the context
1719 }
1720 if (read && read!=draw) {
1721 egl_surface_t* s = (egl_surface_t*)read;
1722 if (!s->isValid())
1723 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1724 if (s->dpy != dpy)
1725 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1726 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 }
1728
1729 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1732 return setError(EGL_BAD_MATCH, EGL_FALSE);
1733
1734 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1735 return setError(EGL_BAD_MATCH, EGL_FALSE);
1736
1737 if (ctx == EGL_NO_CONTEXT) {
1738 // if we're detaching, we need the current context
1739 current_ctx = (EGLContext)getGlThreadSpecific();
1740 } else {
1741 egl_context_t* c = egl_context_t::context(ctx);
1742 egl_surface_t* d = (egl_surface_t*)draw;
1743 egl_surface_t* r = (egl_surface_t*)read;
1744 if ((d && d->ctx && d->ctx != ctx) ||
1745 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001746 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1748 }
1749 }
1750
1751 ogles_context_t* gl = (ogles_context_t*)ctx;
1752 if (makeCurrent(gl) == 0) {
1753 if (ctx) {
1754 egl_context_t* c = egl_context_t::context(ctx);
1755 egl_surface_t* d = (egl_surface_t*)draw;
1756 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001757
1758 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001759 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1760 s->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001761 s->ctx = EGL_NO_CONTEXT;
1762 if (s->zombie)
1763 delete s;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001764 }
1765 if (c->read) {
1766 // FIXME: unlock/disconnect the read surface too
1767 }
1768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001770 c->read = read;
1771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772 if (c->flags & egl_context_t::NEVER_CURRENT) {
1773 c->flags &= ~egl_context_t::NEVER_CURRENT;
1774 GLint w = 0;
1775 GLint h = 0;
1776 if (draw) {
1777 w = d->getWidth();
1778 h = d->getHeight();
1779 }
1780 ogles_surfaceport(gl, 0, 0);
1781 ogles_viewport(gl, 0, 0, w, h);
1782 ogles_scissor(gl, 0, 0, w, h);
1783 }
1784 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001785 if (d->connect() == EGL_FALSE) {
1786 return EGL_FALSE;
1787 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 d->ctx = ctx;
1789 d->bindDrawSurface(gl);
1790 }
1791 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001792 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 r->ctx = ctx;
1794 r->bindReadSurface(gl);
1795 }
1796 } else {
1797 // if surfaces were bound to the context bound to this thread
1798 // mark then as unbound.
1799 if (current_ctx) {
1800 egl_context_t* c = egl_context_t::context(current_ctx);
1801 egl_surface_t* d = (egl_surface_t*)c->draw;
1802 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001803 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001804 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001805 d->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001806 d->ctx = EGL_NO_CONTEXT;
1807 if (d->zombie)
1808 delete d;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001809 }
1810 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001811 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001812 r->ctx = EGL_NO_CONTEXT;
1813 // FIXME: unlock/disconnect the read surface too
1814 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001815 }
1816 }
1817 return EGL_TRUE;
1818 }
1819 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1820}
1821
1822EGLContext eglGetCurrentContext(void)
1823{
1824 // eglGetCurrentContext returns the current EGL rendering context,
1825 // as specified by eglMakeCurrent. If no context is current,
1826 // EGL_NO_CONTEXT is returned.
1827 return (EGLContext)getGlThreadSpecific();
1828}
1829
1830EGLSurface eglGetCurrentSurface(EGLint readdraw)
1831{
1832 // eglGetCurrentSurface returns the read or draw surface attached
1833 // to the current EGL rendering context, as specified by eglMakeCurrent.
1834 // If no context is current, EGL_NO_SURFACE is returned.
1835 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1836 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1837 egl_context_t* c = egl_context_t::context(ctx);
1838 if (readdraw == EGL_READ) {
1839 return c->read;
1840 } else if (readdraw == EGL_DRAW) {
1841 return c->draw;
1842 }
1843 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1844}
1845
1846EGLDisplay eglGetCurrentDisplay(void)
1847{
1848 // eglGetCurrentDisplay returns the current EGL display connection
1849 // for the current EGL rendering context, as specified by eglMakeCurrent.
1850 // If no context is current, EGL_NO_DISPLAY is returned.
1851 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1852 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1853 egl_context_t* c = egl_context_t::context(ctx);
1854 return c->dpy;
1855}
1856
1857EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1858 EGLint attribute, EGLint *value)
1859{
1860 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1861 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1862 egl_context_t* c = egl_context_t::context(ctx);
1863 switch (attribute) {
1864 case EGL_CONFIG_ID:
1865 // Returns the ID of the EGL frame buffer configuration with
1866 // respect to which the context was created
1867 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1868 }
1869 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1870}
1871
1872EGLBoolean eglWaitGL(void)
1873{
1874 return EGL_TRUE;
1875}
1876
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001877EGLBoolean eglWaitNative(EGLint /*engine*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878{
1879 return EGL_TRUE;
1880}
1881
1882EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1883{
1884 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1885 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001886
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001888 if (!d->isValid())
1889 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 if (d->dpy != dpy)
1891 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1892
1893 // post the surface
1894 d->swapBuffers();
1895
1896 // if it's bound to a context, update the buffer
1897 if (d->ctx != EGL_NO_CONTEXT) {
1898 d->bindDrawSurface((ogles_context_t*)d->ctx);
1899 // if this surface is also the read surface of the context
1900 // it is bound to, make sure to update the read buffer as well.
1901 // The EGL spec is a little unclear about this.
1902 egl_context_t* c = egl_context_t::context(d->ctx);
1903 if (c->read == draw) {
1904 d->bindReadSurface((ogles_context_t*)d->ctx);
1905 }
1906 }
1907
1908 return EGL_TRUE;
1909}
1910
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001911EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface /*surface*/,
1912 NativePixmapType /*target*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913{
1914 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1915 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1916 // TODO: eglCopyBuffers()
1917 return EGL_FALSE;
1918}
1919
1920EGLint eglGetError(void)
1921{
1922 return getError();
1923}
1924
1925const char* eglQueryString(EGLDisplay dpy, EGLint name)
1926{
1927 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1928 return setError(EGL_BAD_DISPLAY, (const char*)0);
1929
1930 switch (name) {
1931 case EGL_VENDOR:
1932 return gVendorString;
1933 case EGL_VERSION:
1934 return gVersionString;
1935 case EGL_EXTENSIONS:
1936 return gExtensionsString;
1937 case EGL_CLIENT_APIS:
1938 return gClientApiString;
1939 }
1940 return setError(EGL_BAD_PARAMETER, (const char *)0);
1941}
1942
1943// ----------------------------------------------------------------------------
1944// EGL 1.1
1945// ----------------------------------------------------------------------------
1946
1947EGLBoolean eglSurfaceAttrib(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001948 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*attribute*/, EGLint /*value*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949{
1950 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1951 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1952 // TODO: eglSurfaceAttrib()
1953 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1954}
1955
1956EGLBoolean eglBindTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001957 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958{
1959 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1960 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1961 // TODO: eglBindTexImage()
1962 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1963}
1964
1965EGLBoolean eglReleaseTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001966 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001967{
1968 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1969 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1970 // TODO: eglReleaseTexImage()
1971 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1972}
1973
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001974EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint /*interval*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975{
1976 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1977 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1978 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001979 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001980}
1981
1982// ----------------------------------------------------------------------------
1983// EGL 1.2
1984// ----------------------------------------------------------------------------
1985
1986EGLBoolean eglBindAPI(EGLenum api)
1987{
1988 if (api != EGL_OPENGL_ES_API)
1989 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1990 return EGL_TRUE;
1991}
1992
1993EGLenum eglQueryAPI(void)
1994{
1995 return EGL_OPENGL_ES_API;
1996}
1997
1998EGLBoolean eglWaitClient(void)
1999{
2000 glFinish();
2001 return EGL_TRUE;
2002}
2003
2004EGLBoolean eglReleaseThread(void)
2005{
2006 // TODO: eglReleaseThread()
2007 return EGL_TRUE;
2008}
2009
2010EGLSurface eglCreatePbufferFromClientBuffer(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002011 EGLDisplay dpy, EGLenum /*buftype*/, EGLClientBuffer /*buffer*/,
2012 EGLConfig /*config*/, const EGLint* /*attrib_list*/)
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_NO_SURFACE);
2016 // TODO: eglCreatePbufferFromClientBuffer()
2017 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2018}
2019
2020// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002021// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022// ----------------------------------------------------------------------------
2023
2024void (*eglGetProcAddress (const char *procname))()
2025{
2026 extention_map_t const * const map = gExtentionMap;
2027 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2028 if (!strcmp(procname, map[i].name)) {
2029 return map[i].address;
2030 }
2031 }
2032 return NULL;
2033}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002034
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002035EGLBoolean eglLockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/,
2036 const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002037{
2038 EGLBoolean result = EGL_FALSE;
2039 return result;
2040}
2041
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002042EGLBoolean eglUnlockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002043{
2044 EGLBoolean result = EGL_FALSE;
2045 return result;
2046}
2047
2048EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002049 EGLClientBuffer buffer, const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002050{
2051 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2052 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2053 }
2054 if (ctx != EGL_NO_CONTEXT) {
2055 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2056 }
2057 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2058 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2059 }
2060
Iliyan Malchev697526b2011-05-01 11:33:26 -07002061 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002062
2063 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2064 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2065
Iliyan Malchev697526b2011-05-01 11:33:26 -07002066 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002067 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002068
2069 switch (native_buffer->format) {
2070 case HAL_PIXEL_FORMAT_RGBA_8888:
2071 case HAL_PIXEL_FORMAT_RGBX_8888:
2072 case HAL_PIXEL_FORMAT_RGB_888:
2073 case HAL_PIXEL_FORMAT_RGB_565:
2074 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian8dccb262010-02-04 17:04:53 -08002075 break;
2076 default:
2077 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2078 }
2079
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002080 native_buffer->common.incRef(&native_buffer->common);
2081 return (EGLImageKHR)native_buffer;
2082}
2083
2084EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2085{
2086 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2087 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2088 }
2089
Iliyan Malchev697526b2011-05-01 11:33:26 -07002090 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002091
2092 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2093 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2094
Iliyan Malchev697526b2011-05-01 11:33:26 -07002095 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002096 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2097
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002098 native_buffer->common.decRef(&native_buffer->common);
2099
2100 return EGL_TRUE;
2101}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002102
2103// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002104// EGL_KHR_fence_sync
2105// ----------------------------------------------------------------------------
2106
2107#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2108
2109EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2110 const EGLint *attrib_list)
2111{
2112 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2113 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2114 }
2115
2116 if (type != EGL_SYNC_FENCE_KHR ||
2117 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2118 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2119 }
2120
2121 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2122 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2123 }
2124
2125 // AGL is synchronous; nothing to do here.
2126
2127 return FENCE_SYNC_HANDLE;
2128}
2129
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002130EGLBoolean eglDestroySyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002131{
2132 if (sync != FENCE_SYNC_HANDLE) {
2133 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2134 }
2135
2136 return EGL_TRUE;
2137}
2138
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002139EGLint eglClientWaitSyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync, EGLint /*flags*/,
2140 EGLTimeKHR /*timeout*/)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002141{
2142 if (sync != FENCE_SYNC_HANDLE) {
2143 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2144 }
2145
2146 return EGL_CONDITION_SATISFIED_KHR;
2147}
2148
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002149EGLBoolean eglGetSyncAttribKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync,
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002150 EGLint attribute, EGLint *value)
2151{
2152 if (sync != FENCE_SYNC_HANDLE) {
2153 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2154 }
2155
2156 switch (attribute) {
2157 case EGL_SYNC_TYPE_KHR:
2158 *value = EGL_SYNC_FENCE_KHR;
2159 return EGL_TRUE;
2160 case EGL_SYNC_STATUS_KHR:
2161 *value = EGL_SIGNALED_KHR;
2162 return EGL_TRUE;
2163 case EGL_SYNC_CONDITION_KHR:
2164 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2165 return EGL_TRUE;
2166 default:
2167 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2168 }
2169}
2170
2171// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002172// ANDROID extensions
2173// ----------------------------------------------------------------------------
2174
2175EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2176 EGLint left, EGLint top, EGLint width, EGLint height)
2177{
2178 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2179 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2180
2181 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002182 if (!d->isValid())
2183 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002184 if (d->dpy != dpy)
2185 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2186
2187 // post the surface
2188 d->setSwapRectangle(left, top, width, height);
2189
2190 return EGL_TRUE;
2191}