blob: 7560d8fdf06b97268ff94ac45ecfed428af7c8a6 [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>
19#include <errno.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
26#include <sys/types.h>
27#include <sys/mman.h>
28
29#include <cutils/log.h>
30#include <cutils/atomic.h>
31
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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38#include <GLES/gl.h>
39#include <GLES/glext.h>
40
41#include <pixelflinger/format.h>
42#include <pixelflinger/pixelflinger.h>
43
44#include "context.h"
45#include "state.h"
46#include "texture.h"
47#include "matrix.h"
48
49#undef NELEM
50#define NELEM(x) (sizeof(x)/sizeof(*(x)))
51
Mathias Agopian5f2165f2012-02-24 18:25:41 -080052// ----------------------------------------------------------------------------
Mathias Agopian4b9511c2011-11-13 23:52:47 -080053
54EGLBoolean EGLAPI eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
55 EGLint left, EGLint top, EGLint width, EGLint height);
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057// ----------------------------------------------------------------------------
58namespace android {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060// ----------------------------------------------------------------------------
61
62const unsigned int NUM_DISPLAYS = 1;
63
64static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
65static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
66static pthread_key_t gEGLErrorKey = -1;
Elliott Hughes6071da72015-08-12 15:27:47 -070067#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068namespace gl {
69pthread_key_t gGLKey = -1;
70}; // namespace gl
71#endif
72
73template<typename T>
74static T setError(GLint error, T returnValue) {
75 if (ggl_unlikely(gEGLErrorKey == -1)) {
76 pthread_mutex_lock(&gErrorKeyMutex);
77 if (gEGLErrorKey == -1)
78 pthread_key_create(&gEGLErrorKey, NULL);
79 pthread_mutex_unlock(&gErrorKeyMutex);
80 }
Colin Cross444839b2014-01-24 14:35:39 -080081 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 return returnValue;
83}
84
85static GLint getError() {
86 if (ggl_unlikely(gEGLErrorKey == -1))
87 return EGL_SUCCESS;
Colin Cross444839b2014-01-24 14:35:39 -080088 GLint error = (GLint)(uintptr_t)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -080089 if (error == 0) {
90 // The TLS key has been created by another thread, but the value for
91 // this thread has not been initialized.
92 return EGL_SUCCESS;
93 }
Colin Cross444839b2014-01-24 14:35:39 -080094 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)EGL_SUCCESS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 return error;
96}
97
98// ----------------------------------------------------------------------------
99
100struct egl_display_t
101{
102 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 static EGLBoolean is_valid(EGLDisplay dpy) {
107 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
108 }
109
110 NativeDisplayType type;
111 volatile int32_t initialized;
112};
113
114static egl_display_t gDisplays[NUM_DISPLAYS];
115
116egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
117 return gDisplays[uintptr_t(dpy)-1U];
118}
119
120struct egl_context_t {
121 enum {
122 IS_CURRENT = 0x00010000,
123 NEVER_CURRENT = 0x00020000
124 };
125 uint32_t flags;
126 EGLDisplay dpy;
127 EGLConfig config;
128 EGLSurface read;
129 EGLSurface draw;
130
131 static inline egl_context_t* context(EGLContext ctx) {
132 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
133 return static_cast<egl_context_t*>(gl->rasterizer.base);
134 }
135};
136
137// ----------------------------------------------------------------------------
138
139struct egl_surface_t
140{
141 enum {
142 PAGE_FLIP = 0x00000001,
143 MAGIC = 0x31415265
144 };
145
146 uint32_t magic;
147 EGLDisplay dpy;
148 EGLConfig config;
149 EGLContext ctx;
Jesse Hall78141e32013-03-07 09:56:26 -0800150 bool zombie;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151
152 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
153 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700154 bool isValid() const;
155 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
158 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700159 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700160 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 virtual EGLint getWidth() const = 0;
162 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
164 virtual EGLint getHorizontalResolution() const;
165 virtual EGLint getVerticalResolution() const;
166 virtual EGLint getRefreshRate() const;
167 virtual EGLint getSwapBehavior() const;
168 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700169 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170protected:
171 GGLSurface depth;
172};
173
174egl_surface_t::egl_surface_t(EGLDisplay dpy,
175 EGLConfig config,
176 int32_t depthFormat)
Jesse Hall78141e32013-03-07 09:56:26 -0800177 : magic(MAGIC), dpy(dpy), config(config), ctx(0), zombie(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
179 depth.version = sizeof(GGLSurface);
180 depth.data = 0;
181 depth.format = depthFormat;
182}
183egl_surface_t::~egl_surface_t()
184{
185 magic = 0;
186 free(depth.data);
187}
Mathias Agopian0696a572009-08-20 00:12:56 -0700188bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000189 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700190 return magic == MAGIC;
191}
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193EGLBoolean egl_surface_t::swapBuffers() {
194 return EGL_FALSE;
195}
196EGLint egl_surface_t::getHorizontalResolution() const {
197 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
198}
199EGLint egl_surface_t::getVerticalResolution() const {
200 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
201}
202EGLint egl_surface_t::getRefreshRate() const {
203 return (60 * EGL_DISPLAY_SCALING);
204}
205EGLint egl_surface_t::getSwapBehavior() const {
206 return EGL_BUFFER_PRESERVED;
207}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700208EGLBoolean egl_surface_t::setSwapRectangle(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700209 EGLint /*l*/, EGLint /*t*/, EGLint /*w*/, EGLint /*h*/)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700210{
211 return EGL_FALSE;
212}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
214// ----------------------------------------------------------------------------
215
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 EGLDisplay dpy, EGLConfig config,
220 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700221 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222
Mathias Agopian0696a572009-08-20 00:12:56 -0700223 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224
Mathias Agopian0696a572009-08-20 00:12:56 -0700225 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 virtual EGLBoolean swapBuffers();
227 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
228 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700229 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700230 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700231 virtual EGLint getWidth() const { return width; }
232 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 virtual EGLint getHorizontalResolution() const;
234 virtual EGLint getVerticalResolution() const;
235 virtual EGLint getRefreshRate() const;
236 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700237 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700240 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
241 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700242 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700243 ANativeWindowBuffer* buffer;
244 ANativeWindowBuffer* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700245 gralloc_module_t const* module;
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)
Mathias Agopian0926f502009-05-04 14:17:04 -0700344 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700345 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700346 bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700348 hw_module_t const* pModule;
349 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
350 module = reinterpret_cast<gralloc_module_t const*>(pModule);
351
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700352 pixelFormatTable = gglGetPixelFormatTable();
353
354 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700356 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
357 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700358}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700359
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700360egl_window_surface_v2_t::~egl_window_surface_v2_t() {
361 if (buffer) {
362 buffer->common.decRef(&buffer->common);
363 }
364 if (previousBuffer) {
365 previousBuffer->common.decRef(&previousBuffer->common);
366 }
367 nativeWindow->common.decRef(&nativeWindow->common);
368}
369
Mathias Agopiancf81c842009-07-31 14:47:00 -0700370EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700371{
Mathias Agopian52212712009-08-11 22:34:02 -0700372 // we're intending to do software rendering
373 native_window_set_usage(nativeWindow,
374 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
375
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700376 // dequeue a buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700377 int fenceFd = -1;
378 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer,
379 &fenceFd) != NO_ERROR) {
380 return setError(EGL_BAD_ALLOC, EGL_FALSE);
381 }
382
383 // wait for the buffer
384 sp<Fence> fence(new Fence(fenceFd));
385 if (fence->wait(Fence::TIMEOUT_NEVER) != NO_ERROR) {
386 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700387 return setError(EGL_BAD_ALLOC, EGL_FALSE);
388 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700389
390 // allocate a corresponding depth-buffer
391 width = buffer->width;
392 height = buffer->height;
393 if (depth.format) {
394 depth.width = width;
395 depth.height = height;
396 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700397 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
398 static_cast<uint64_t>(depth.height) * 2;
399 if (depth.stride < 0 || depth.height > INT_MAX ||
400 allocSize > UINT32_MAX) {
401 return setError(EGL_BAD_ALLOC, EGL_FALSE);
402 }
403 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700404 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700405 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700406 }
407 }
408
409 // keep a reference on the buffer
410 buffer->common.incRef(&buffer->common);
411
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700412 // pin the buffer down
413 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
414 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000415 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700416 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700417 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700418 // FIXME: we should make sure we're not accessing the buffer anymore
419 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700420 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700421}
422
423void egl_window_surface_v2_t::disconnect()
424{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700425 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700426 bits = NULL;
427 unlock(buffer);
428 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700429 if (buffer) {
Jesse Hall5f555562013-03-07 15:14:18 -0800430 nativeWindow->cancelBuffer(nativeWindow, buffer, -1);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700431 buffer->common.decRef(&buffer->common);
432 buffer = 0;
433 }
434 if (previousBuffer) {
435 previousBuffer->common.decRef(&previousBuffer->common);
436 previousBuffer = 0;
437 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438}
439
Mathias Agopian0926f502009-05-04 14:17:04 -0700440status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700441 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700442{
Mathias Agopianbc492912009-11-03 20:38:08 -0800443 int err;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800444
445 err = module->lock(module, buf->handle,
446 usage, 0, 0, buf->width, buf->height, vaddr);
447
Mathias Agopian0926f502009-05-04 14:17:04 -0700448 return err;
449}
450
Iliyan Malchev697526b2011-05-01 11:33:26 -0700451status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700452{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700453 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800454 int err = NO_ERROR;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800455
456 err = module->unlock(module, buf->handle);
457
Mathias Agopian0926f502009-05-04 14:17:04 -0700458 return err;
459}
460
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700461void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700462 ANativeWindowBuffer* dst, void* dst_vaddr,
463 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700464 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700465{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700466 // NOTE: dst and src must be the same format
467
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700468 Region::const_iterator cur = clip.begin();
469 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700470
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700471 const size_t bpp = pixelFormatTable[src->format].size;
472 const size_t dbpr = dst->stride * bpp;
473 const size_t sbpr = src->stride * bpp;
474
475 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
476 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
477
478 while (cur != end) {
479 const Rect& r(*cur++);
480 ssize_t w = r.right - r.left;
481 ssize_t h = r.bottom - r.top;
482 if (w <= 0 || h<=0) continue;
483 size_t size = w * bpp;
484 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
485 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
486 if (dbpr==sbpr && size==sbpr) {
487 size *= h;
488 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700489 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700490 do {
491 memcpy(d, s, size);
492 d += dbpr;
493 s += sbpr;
494 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700495 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700496}
497
498EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700500 if (!buffer) {
501 return setError(EGL_BAD_ACCESS, EGL_FALSE);
502 }
503
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700504 /*
505 * Handle eglSetSwapRectangleANDROID()
506 * We copyback from the front buffer
507 */
508 if (!dirtyRegion.isEmpty()) {
509 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
510 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100511 // This was const Region copyBack, but that causes an
512 // internal compile error on simulator builds
513 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700514 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700515 void* prevBits;
516 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700517 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
518 // copy from previousBuffer to buffer
519 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700520 unlock(previousBuffer);
521 }
522 }
523 }
524 oldDirtyRegion = dirtyRegion;
525 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700526
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700527 if (previousBuffer) {
528 previousBuffer->common.decRef(&previousBuffer->common);
529 previousBuffer = 0;
530 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700531
Mathias Agopian0926f502009-05-04 14:17:04 -0700532 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700533 previousBuffer = buffer;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700534 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700535 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700537 // dequeue a new buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700538 int fenceFd = -1;
539 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) == NO_ERROR) {
540 sp<Fence> fence(new Fence(fenceFd));
541 if (fence->wait(Fence::TIMEOUT_NEVER)) {
542 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
543 return setError(EGL_BAD_ALLOC, EGL_FALSE);
544 }
Mathias Agopian031213e2010-08-18 16:07:34 -0700545
546 // reallocate the depth-buffer if needed
547 if ((width != buffer->width) || (height != buffer->height)) {
548 // TODO: we probably should reset the swap rect here
549 // if the window size has changed
550 width = buffer->width;
551 height = buffer->height;
552 if (depth.data) {
553 free(depth.data);
554 depth.width = width;
555 depth.height = height;
556 depth.stride = buffer->stride;
Michael Lentine95303882015-05-28 17:43:06 -0700557 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
558 static_cast<uint64_t>(depth.height) * 2;
559 if (depth.stride < 0 || depth.height > INT_MAX ||
560 allocSize > UINT32_MAX) {
561 setError(EGL_BAD_ALLOC, EGL_FALSE);
562 return EGL_FALSE;
563 }
564 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopian031213e2010-08-18 16:07:34 -0700565 if (depth.data == 0) {
566 setError(EGL_BAD_ALLOC, EGL_FALSE);
567 return EGL_FALSE;
568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
570 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700571
Mathias Agopian031213e2010-08-18 16:07:34 -0700572 // keep a reference on the buffer
573 buffer->common.incRef(&buffer->common);
574
575 // finally pin the buffer down
576 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
577 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000578 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700579 buffer, buffer->width, buffer->height);
580 return setError(EGL_BAD_ACCESS, EGL_FALSE);
581 // FIXME: we should make sure we're not accessing the buffer anymore
582 }
583 } else {
584 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700585 }
586
587 return EGL_TRUE;
588}
589
590EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
591 EGLint l, EGLint t, EGLint w, EGLint h)
592{
593 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 return EGL_TRUE;
595}
596
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598{
599 GGLSurface buffer;
600 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601 buffer.width = this->buffer->width;
602 buffer.height = this->buffer->height;
603 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700604 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 gl->rasterizer.procs.colorBuffer(gl, &buffer);
607 if (depth.data != gl->rasterizer.state.buffers.depth.data)
608 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 return EGL_TRUE;
611}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613{
614 GGLSurface buffer;
615 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616 buffer.width = this->buffer->width;
617 buffer.height = this->buffer->height;
618 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700619 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 gl->rasterizer.procs.readBuffer(gl, &buffer);
622 return EGL_TRUE;
623}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
626}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
629}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630EGLint egl_window_surface_v2_t::getRefreshRate() const {
631 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700633EGLint egl_window_surface_v2_t::getSwapBehavior() const
634{
635 /*
636 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
637 * the content of the swapped buffer.
638 *
639 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
640 *
641 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
642 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
643 * is, everything outside of this area is preserved.
644 *
645 * This implementation of EGL assumes the later case.
646 *
647 */
648
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650}
651
652// ----------------------------------------------------------------------------
653
654struct egl_pixmap_surface_t : public egl_surface_t
655{
656 egl_pixmap_surface_t(
657 EGLDisplay dpy, EGLConfig config,
658 int32_t depthFormat,
659 egl_native_pixmap_t const * pixmap);
660
661 virtual ~egl_pixmap_surface_t() { }
662
Mathias Agopian0696a572009-08-20 00:12:56 -0700663 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
665 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
666 virtual EGLint getWidth() const { return nativePixmap.width; }
667 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668private:
669 egl_native_pixmap_t nativePixmap;
670};
671
672egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
673 EGLConfig config,
674 int32_t depthFormat,
675 egl_native_pixmap_t const * pixmap)
676 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
677{
678 if (depthFormat) {
679 depth.width = pixmap->width;
680 depth.height = pixmap->height;
681 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700682 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
683 static_cast<uint64_t>(depth.height) * 2;
684 if (depth.stride < 0 || depth.height > INT_MAX ||
685 allocSize > UINT32_MAX) {
686 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
687 return;
688 }
689 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 if (depth.data == 0) {
691 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 }
693 }
694}
695EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
696{
697 GGLSurface buffer;
698 buffer.version = sizeof(GGLSurface);
699 buffer.width = nativePixmap.width;
700 buffer.height = nativePixmap.height;
701 buffer.stride = nativePixmap.stride;
702 buffer.data = nativePixmap.data;
703 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 gl->rasterizer.procs.colorBuffer(gl, &buffer);
706 if (depth.data != gl->rasterizer.state.buffers.depth.data)
707 gl->rasterizer.procs.depthBuffer(gl, &depth);
708 return EGL_TRUE;
709}
710EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
711{
712 GGLSurface buffer;
713 buffer.version = sizeof(GGLSurface);
714 buffer.width = nativePixmap.width;
715 buffer.height = nativePixmap.height;
716 buffer.stride = nativePixmap.stride;
717 buffer.data = nativePixmap.data;
718 buffer.format = nativePixmap.format;
719 gl->rasterizer.procs.readBuffer(gl, &buffer);
720 return EGL_TRUE;
721}
722
723// ----------------------------------------------------------------------------
724
725struct egl_pbuffer_surface_t : public egl_surface_t
726{
727 egl_pbuffer_surface_t(
728 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
729 int32_t w, int32_t h, int32_t f);
730
731 virtual ~egl_pbuffer_surface_t();
732
Mathias Agopian0696a572009-08-20 00:12:56 -0700733 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
735 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
736 virtual EGLint getWidth() const { return pbuffer.width; }
737 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738private:
739 GGLSurface pbuffer;
740};
741
742egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
743 EGLConfig config, int32_t depthFormat,
744 int32_t w, int32_t h, int32_t f)
745 : egl_surface_t(dpy, config, depthFormat)
746{
747 size_t size = w*h;
748 switch (f) {
749 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
750 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
751 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800752 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000754 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 pbuffer.data = 0;
756 break;
757 }
758 pbuffer.version = sizeof(GGLSurface);
759 pbuffer.width = w;
760 pbuffer.height = h;
761 pbuffer.stride = w;
762 pbuffer.data = (GGLubyte*)malloc(size);
763 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 if (depthFormat) {
766 depth.width = pbuffer.width;
767 depth.height = pbuffer.height;
768 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700769 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
770 static_cast<uint64_t>(depth.height) * 2;
771 if (depth.stride < 0 || depth.height > INT_MAX ||
772 allocSize > UINT32_MAX) {
773 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
774 return;
775 }
776 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 if (depth.data == 0) {
778 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
779 return;
780 }
781 }
782}
783egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
784 free(pbuffer.data);
785}
786EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
787{
788 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
789 if (depth.data != gl->rasterizer.state.buffers.depth.data)
790 gl->rasterizer.procs.depthBuffer(gl, &depth);
791 return EGL_TRUE;
792}
793EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
794{
795 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
796 return EGL_TRUE;
797}
798
799// ----------------------------------------------------------------------------
800
801struct config_pair_t {
802 GLint key;
803 GLint value;
804};
805
806struct configs_t {
807 const config_pair_t* array;
808 int size;
809};
810
811struct config_management_t {
812 GLint key;
813 bool (*match)(GLint reqValue, GLint confValue);
814 static bool atLeast(GLint reqValue, GLint confValue) {
815 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
816 }
817 static bool exact(GLint reqValue, GLint confValue) {
818 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
819 }
820 static bool mask(GLint reqValue, GLint confValue) {
821 return (confValue & reqValue) == reqValue;
822 }
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700823 static bool ignore(GLint /*reqValue*/, GLint /*confValue*/) {
Mathias Agopian63971672010-10-25 15:51:24 -0700824 return true;
825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826};
827
828// ----------------------------------------------------------------------------
829
830#define VERSION_MAJOR 1
831#define VERSION_MINOR 2
832static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700833static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700834static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700835static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700836 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700837 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838 // "KHR_image_pixmap "
839 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700840 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700841 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842
843// ----------------------------------------------------------------------------
844
845struct extention_map_t {
846 const char * const name;
847 __eglMustCastToProperFunctionPointerType address;
848};
849
850static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700851 { "glDrawTexsOES",
852 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
853 { "glDrawTexiOES",
854 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
855 { "glDrawTexfOES",
856 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
857 { "glDrawTexxOES",
858 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
859 { "glDrawTexsvOES",
860 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
861 { "glDrawTexivOES",
862 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
863 { "glDrawTexfvOES",
864 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
865 { "glDrawTexxvOES",
866 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
867 { "glQueryMatrixxOES",
868 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
869 { "glEGLImageTargetTexture2DOES",
870 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
871 { "glEGLImageTargetRenderbufferStorageOES",
872 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
873 { "glClipPlanef",
874 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
875 { "glClipPlanex",
876 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
877 { "glBindBuffer",
878 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
879 { "glBufferData",
880 (__eglMustCastToProperFunctionPointerType)&glBufferData },
881 { "glBufferSubData",
882 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
883 { "glDeleteBuffers",
884 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
885 { "glGenBuffers",
886 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700887 { "eglCreateImageKHR",
888 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
889 { "eglDestroyImageKHR",
890 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700891 { "eglCreateSyncKHR",
892 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
893 { "eglDestroySyncKHR",
894 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
895 { "eglClientWaitSyncKHR",
896 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
897 { "eglGetSyncAttribKHR",
898 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700899 { "eglSetSwapRectangleANDROID",
900 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901};
902
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700903/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 * In the lists below, attributes names MUST be sorted.
905 * Additionally, all configs must be sorted according to
906 * the EGL specification.
907 */
908
909static config_pair_t const config_base_attribute_list[] = {
910 { EGL_STENCIL_SIZE, 0 },
911 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
912 { EGL_LEVEL, 0 },
913 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700914 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
916 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
917 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
918 { EGL_NATIVE_VISUAL_ID, 0 },
919 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
920 { EGL_SAMPLES, 0 },
921 { EGL_SAMPLE_BUFFERS, 0 },
922 { EGL_TRANSPARENT_TYPE, EGL_NONE },
923 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
924 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
925 { EGL_TRANSPARENT_RED_VALUE, 0 },
926 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
927 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
928 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700929 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700930 { EGL_LUMINANCE_SIZE, 0 },
931 { EGL_ALPHA_MASK_SIZE, 0 },
932 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700933 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700934 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935};
936
937// These configs can override the base attribute list
938// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
939
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800940// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941static config_pair_t const config_0_attribute_list[] = {
942 { EGL_BUFFER_SIZE, 16 },
943 { EGL_ALPHA_SIZE, 0 },
944 { EGL_BLUE_SIZE, 5 },
945 { EGL_GREEN_SIZE, 6 },
946 { EGL_RED_SIZE, 5 },
947 { EGL_DEPTH_SIZE, 0 },
948 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700949 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
951};
952
953static config_pair_t const config_1_attribute_list[] = {
954 { EGL_BUFFER_SIZE, 16 },
955 { EGL_ALPHA_SIZE, 0 },
956 { EGL_BLUE_SIZE, 5 },
957 { EGL_GREEN_SIZE, 6 },
958 { EGL_RED_SIZE, 5 },
959 { EGL_DEPTH_SIZE, 16 },
960 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700961 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
963};
964
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800965// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966static config_pair_t const config_2_attribute_list[] = {
967 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800968 { EGL_ALPHA_SIZE, 0 },
969 { EGL_BLUE_SIZE, 8 },
970 { EGL_GREEN_SIZE, 8 },
971 { EGL_RED_SIZE, 8 },
972 { EGL_DEPTH_SIZE, 0 },
973 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700974 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800975 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
976};
977
978static config_pair_t const config_3_attribute_list[] = {
979 { EGL_BUFFER_SIZE, 32 },
980 { EGL_ALPHA_SIZE, 0 },
981 { EGL_BLUE_SIZE, 8 },
982 { EGL_GREEN_SIZE, 8 },
983 { EGL_RED_SIZE, 8 },
984 { EGL_DEPTH_SIZE, 16 },
985 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700986 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800987 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
988};
989
990// 8888 configs
991static config_pair_t const config_4_attribute_list[] = {
992 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 { EGL_ALPHA_SIZE, 8 },
994 { EGL_BLUE_SIZE, 8 },
995 { EGL_GREEN_SIZE, 8 },
996 { EGL_RED_SIZE, 8 },
997 { EGL_DEPTH_SIZE, 0 },
998 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700999 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1001};
1002
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001003static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 { EGL_BUFFER_SIZE, 32 },
1005 { EGL_ALPHA_SIZE, 8 },
1006 { EGL_BLUE_SIZE, 8 },
1007 { EGL_GREEN_SIZE, 8 },
1008 { EGL_RED_SIZE, 8 },
1009 { EGL_DEPTH_SIZE, 16 },
1010 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001011 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1013};
1014
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001015// A8 configs
1016static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 { EGL_BUFFER_SIZE, 8 },
1018 { EGL_ALPHA_SIZE, 8 },
1019 { EGL_BLUE_SIZE, 0 },
1020 { EGL_GREEN_SIZE, 0 },
1021 { EGL_RED_SIZE, 0 },
1022 { EGL_DEPTH_SIZE, 0 },
1023 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001024 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1026};
1027
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001028static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 { EGL_BUFFER_SIZE, 8 },
1030 { EGL_ALPHA_SIZE, 8 },
1031 { EGL_BLUE_SIZE, 0 },
1032 { EGL_GREEN_SIZE, 0 },
1033 { EGL_RED_SIZE, 0 },
1034 { EGL_DEPTH_SIZE, 16 },
1035 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001036 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1038};
1039
1040static configs_t const gConfigs[] = {
1041 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1042 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1043 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1044 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1045 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1046 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001047 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1048 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049};
1050
1051static config_management_t const gConfigManagement[] = {
1052 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1053 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1054 { EGL_BLUE_SIZE, config_management_t::atLeast },
1055 { EGL_GREEN_SIZE, config_management_t::atLeast },
1056 { EGL_RED_SIZE, config_management_t::atLeast },
1057 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1058 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1059 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1060 { EGL_CONFIG_ID, config_management_t::exact },
1061 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001062 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1063 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1064 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001066 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1068 { EGL_SAMPLES, config_management_t::exact },
1069 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1070 { EGL_SURFACE_TYPE, config_management_t::mask },
1071 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1072 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1073 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1074 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1075 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1076 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1077 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1078 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001079 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1080 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1081 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1082 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1083 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084};
1085
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001088 // attributes that are not specified are simply ignored, if a particular
1089 // one needs not be ignored, it must be specified here, eg:
1090 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091};
1092
1093// ----------------------------------------------------------------------------
1094
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001095static status_t getConfigFormatInfo(EGLint configID,
1096 int32_t& pixelFormat, int32_t& depthFormat)
1097{
1098 switch(configID) {
1099 case 0:
1100 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1101 depthFormat = 0;
1102 break;
1103 case 1:
1104 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1105 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1106 break;
1107 case 2:
1108 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1109 depthFormat = 0;
1110 break;
1111 case 3:
1112 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1113 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1114 break;
1115 case 4:
1116 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1117 depthFormat = 0;
1118 break;
1119 case 5:
1120 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1121 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1122 break;
1123 case 6:
1124 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1125 depthFormat = 0;
1126 break;
1127 case 7:
1128 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1129 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1130 break;
1131 default:
1132 return NAME_NOT_FOUND;
1133 }
1134 return NO_ERROR;
1135}
1136
1137// ----------------------------------------------------------------------------
1138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139template<typename T>
1140static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1141{
1142 while (first <= last) {
1143 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001144 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001146 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 last = mid - 1;
1148 } else {
1149 return mid;
1150 }
1151 }
1152 return -1;
1153}
1154
1155static int isAttributeMatching(int i, EGLint attr, EGLint val)
1156{
1157 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001158 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 int index = binarySearch<config_pair_t>(
1160 gConfigs[i].array,
1161 0, gConfigs[i].size-1,
1162 attr);
1163 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001164 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 index = binarySearch<config_pair_t>(
1166 config_base_attribute_list,
1167 0, NELEM(config_base_attribute_list)-1,
1168 attr);
1169 }
1170 if (index >= 0) {
1171 // attribute found, check if this config could match
1172 int cfgMgtIndex = binarySearch<config_management_t>(
1173 gConfigManagement,
1174 0, NELEM(gConfigManagement)-1,
1175 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001176 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 bool match = gConfigManagement[cfgMgtIndex].match(
1178 val, configFound[index].value);
1179 if (match) {
1180 // this config matches
1181 return 1;
1182 }
1183 } else {
1184 // attribute not found. this should NEVER happen.
1185 }
1186 } else {
1187 // error, this attribute doesn't exist
1188 }
1189 return 0;
1190}
1191
1192static int makeCurrent(ogles_context_t* gl)
1193{
1194 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1195 if (gl) {
1196 egl_context_t* c = egl_context_t::context(gl);
1197 if (c->flags & egl_context_t::IS_CURRENT) {
1198 if (current != gl) {
1199 // it is an error to set a context current, if it's already
1200 // current to another thread
1201 return -1;
1202 }
1203 } else {
1204 if (current) {
1205 // mark the current context as not current, and flush
1206 glFlush();
1207 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1208 }
1209 }
1210 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1211 // The context is not current, make it current!
1212 setGlThreadSpecific(gl);
1213 c->flags |= egl_context_t::IS_CURRENT;
1214 }
1215 } else {
1216 if (current) {
1217 // mark the current context as not current, and flush
1218 glFlush();
1219 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1220 }
1221 // this thread has no context attached to it
1222 setGlThreadSpecific(0);
1223 }
1224 return 0;
1225}
1226
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001227static EGLBoolean getConfigAttrib(EGLDisplay /*dpy*/, EGLConfig config,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 EGLint attribute, EGLint *value)
1229{
1230 size_t numConfigs = NELEM(gConfigs);
Colin Cross444839b2014-01-24 14:35:39 -08001231 int index = (int)(uintptr_t)config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 if (uint32_t(index) >= numConfigs)
1233 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1234
1235 int attrIndex;
1236 attrIndex = binarySearch<config_pair_t>(
1237 gConfigs[index].array,
1238 0, gConfigs[index].size-1,
1239 attribute);
1240 if (attrIndex>=0) {
1241 *value = gConfigs[index].array[attrIndex].value;
1242 return EGL_TRUE;
1243 }
1244
1245 attrIndex = binarySearch<config_pair_t>(
1246 config_base_attribute_list,
1247 0, NELEM(config_base_attribute_list)-1,
1248 attribute);
1249 if (attrIndex>=0) {
1250 *value = config_base_attribute_list[attrIndex].value;
1251 return EGL_TRUE;
1252 }
1253 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1254}
1255
1256static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001257 NativeWindowType window, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258{
1259 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1260 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1261 if (window == 0)
1262 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1263
1264 EGLint surfaceType;
1265 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1266 return EGL_FALSE;
1267
1268 if (!(surfaceType & EGL_WINDOW_BIT))
1269 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1270
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001271 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001272 ANDROID_NATIVE_WINDOW_MAGIC) {
1273 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1274 }
1275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 EGLint configID;
1277 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1278 return EGL_FALSE;
1279
1280 int32_t depthFormat;
1281 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001282 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1284 }
1285
1286 // FIXME: we don't have access to the pixelFormat here just yet.
1287 // (it's possible that the surface is not fully initialized)
1288 // maybe this should be done after the page-flip
1289 //if (EGLint(info.format) != pixelFormat)
1290 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1291
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292 egl_surface_t* surface;
1293 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001294 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295
Mathias Agopian0696a572009-08-20 00:12:56 -07001296 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 // there was a problem in the ctor, the error
1298 // flag has been set.
1299 delete surface;
1300 surface = 0;
1301 }
1302 return surface;
1303}
1304
1305static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001306 NativePixmapType pixmap, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
1308 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1309 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1310 if (pixmap == 0)
1311 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1312
1313 EGLint surfaceType;
1314 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1315 return EGL_FALSE;
1316
1317 if (!(surfaceType & EGL_PIXMAP_BIT))
1318 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1319
Mathias Agopian0696a572009-08-20 00:12:56 -07001320 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1321 sizeof(egl_native_pixmap_t)) {
1322 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1323 }
1324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 EGLint configID;
1326 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1327 return EGL_FALSE;
1328
1329 int32_t depthFormat;
1330 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001331 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1333 }
1334
1335 if (pixmap->format != pixelFormat)
1336 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1337
1338 egl_surface_t* surface =
1339 new egl_pixmap_surface_t(dpy, config, depthFormat,
1340 static_cast<egl_native_pixmap_t*>(pixmap));
1341
Mathias Agopian0696a572009-08-20 00:12:56 -07001342 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 // there was a problem in the ctor, the error
1344 // flag has been set.
1345 delete surface;
1346 surface = 0;
1347 }
1348 return surface;
1349}
1350
1351static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1352 const EGLint *attrib_list)
1353{
1354 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1355 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1356
1357 EGLint surfaceType;
1358 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1359 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 if (!(surfaceType & EGL_PBUFFER_BIT))
1362 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001363
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 EGLint configID;
1365 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1366 return EGL_FALSE;
1367
1368 int32_t depthFormat;
1369 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001370 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1372 }
1373
1374 int32_t w = 0;
1375 int32_t h = 0;
Jesse Hall2e8ca9d2015-08-21 07:41:46 -07001376 while (attrib_list[0] != EGL_NONE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1378 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1379 attrib_list+=2;
1380 }
1381
1382 egl_surface_t* surface =
1383 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1384
Mathias Agopian0696a572009-08-20 00:12:56 -07001385 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 // there was a problem in the ctor, the error
1387 // flag has been set.
1388 delete surface;
1389 surface = 0;
1390 }
1391 return surface;
1392}
1393
1394// ----------------------------------------------------------------------------
1395}; // namespace android
1396// ----------------------------------------------------------------------------
1397
1398using namespace android;
1399
1400// ----------------------------------------------------------------------------
1401// Initialization
1402// ----------------------------------------------------------------------------
1403
1404EGLDisplay eglGetDisplay(NativeDisplayType display)
1405{
Elliott Hughes6071da72015-08-12 15:27:47 -07001406#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 // this just needs to be done once
1408 if (gGLKey == -1) {
1409 pthread_mutex_lock(&gInitMutex);
1410 if (gGLKey == -1)
1411 pthread_key_create(&gGLKey, NULL);
1412 pthread_mutex_unlock(&gInitMutex);
1413 }
1414#endif
1415 if (display == EGL_DEFAULT_DISPLAY) {
1416 EGLDisplay dpy = (EGLDisplay)1;
1417 egl_display_t& d = egl_display_t::get_display(dpy);
1418 d.type = display;
1419 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001420 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 return EGL_NO_DISPLAY;
1422}
1423
1424EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1425{
1426 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1427 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 EGLBoolean res = EGL_TRUE;
1430 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001431
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 if (android_atomic_inc(&d.initialized) == 0) {
1433 // initialize stuff here if needed
1434 //pthread_mutex_lock(&gInitMutex);
1435 //pthread_mutex_unlock(&gInitMutex);
1436 }
1437
1438 if (res == EGL_TRUE) {
1439 if (major != NULL) *major = VERSION_MAJOR;
1440 if (minor != NULL) *minor = VERSION_MINOR;
1441 }
1442 return res;
1443}
1444
1445EGLBoolean eglTerminate(EGLDisplay dpy)
1446{
1447 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1448 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1449
1450 EGLBoolean res = EGL_TRUE;
1451 egl_display_t& d = egl_display_t::get_display(dpy);
1452 if (android_atomic_dec(&d.initialized) == 1) {
1453 // TODO: destroy all resources (surfaces, contexts, etc...)
1454 //pthread_mutex_lock(&gInitMutex);
1455 //pthread_mutex_unlock(&gInitMutex);
1456 }
1457 return res;
1458}
1459
1460// ----------------------------------------------------------------------------
1461// configuration
1462// ----------------------------------------------------------------------------
1463
1464EGLBoolean eglGetConfigs( EGLDisplay dpy,
1465 EGLConfig *configs,
1466 EGLint config_size, EGLint *num_config)
1467{
1468 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1469 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1470
1471 GLint numConfigs = NELEM(gConfigs);
1472 if (!configs) {
1473 *num_config = numConfigs;
1474 return EGL_TRUE;
1475 }
1476 GLint i;
1477 for (i=0 ; i<numConfigs && i<config_size ; i++) {
Colin Cross444839b2014-01-24 14:35:39 -08001478 *configs++ = (EGLConfig)(uintptr_t)i;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 }
1480 *num_config = i;
1481 return EGL_TRUE;
1482}
1483
1484EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1485 EGLConfig *configs, EGLint config_size,
1486 EGLint *num_config)
1487{
1488 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1489 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001490
1491 if (ggl_unlikely(num_config==0)) {
1492 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1493 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494
Jack Palevich749c63d2009-03-25 15:12:17 -07001495 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001496 /*
1497 * A NULL attrib_list should be treated as though it was an empty
1498 * one (terminated with EGL_NONE) as defined in
1499 * section 3.4.1 "Querying Configurations" in the EGL specification.
1500 */
1501 static const EGLint dummy = EGL_NONE;
1502 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 int numAttributes = 0;
1506 int numConfigs = NELEM(gConfigs);
1507 uint32_t possibleMatch = (1<<numConfigs)-1;
1508 while(possibleMatch && *attrib_list != EGL_NONE) {
1509 numAttributes++;
1510 EGLint attr = *attrib_list++;
1511 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001512 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 if (!(possibleMatch & (1<<i)))
1514 continue;
1515 if (isAttributeMatching(i, attr, val) == 0) {
1516 possibleMatch &= ~(1<<i);
1517 }
1518 }
1519 }
1520
1521 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001522 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1523 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 // default value
1525 if (binarySearch<config_pair_t>(
1526 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001527 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 config_defaults[j].key) < 0)
1529 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001530 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 if (!(possibleMatch & (1<<i)))
1532 continue;
1533 if (isAttributeMatching(i,
1534 config_defaults[j].key,
1535 config_defaults[j].value) == 0)
1536 {
1537 possibleMatch &= ~(1<<i);
1538 }
1539 }
1540 }
1541 }
1542
1543 // return the configurations found
1544 int n=0;
1545 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001546 if (configs) {
1547 for (int i=0 ; config_size && i<numConfigs ; i++) {
1548 if (possibleMatch & (1<<i)) {
Colin Cross444839b2014-01-24 14:35:39 -08001549 *configs++ = (EGLConfig)(uintptr_t)i;
Jack Palevich749c63d2009-03-25 15:12:17 -07001550 config_size--;
1551 n++;
1552 }
1553 }
1554 } else {
1555 for (int i=0 ; i<numConfigs ; i++) {
1556 if (possibleMatch & (1<<i)) {
1557 n++;
1558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 }
1560 }
1561 }
1562 *num_config = n;
1563 return EGL_TRUE;
1564}
1565
1566EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1567 EGLint attribute, EGLint *value)
1568{
1569 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1570 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1571
1572 return getConfigAttrib(dpy, config, attribute, value);
1573}
1574
1575// ----------------------------------------------------------------------------
1576// surfaces
1577// ----------------------------------------------------------------------------
1578
1579EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1580 NativeWindowType window,
1581 const EGLint *attrib_list)
1582{
1583 return createWindowSurface(dpy, config, window, attrib_list);
1584}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1587 NativePixmapType pixmap,
1588 const EGLint *attrib_list)
1589{
1590 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1591}
1592
1593EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1594 const EGLint *attrib_list)
1595{
1596 return createPbufferSurface(dpy, config, attrib_list);
1597}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001598
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1600{
1601 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1602 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1603 if (eglSurface != EGL_NO_SURFACE) {
1604 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001605 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1607 if (surface->dpy != dpy)
1608 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001609 if (surface->ctx) {
Jesse Hall78141e32013-03-07 09:56:26 -08001610 // defer disconnect/delete until no longer current
1611 surface->zombie = true;
1612 } else {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001613 surface->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001614 delete surface;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001615 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 }
1617 return EGL_TRUE;
1618}
1619
1620EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1621 EGLint attribute, EGLint *value)
1622{
1623 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1624 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1625 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001626 if (!surface->isValid())
1627 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 if (surface->dpy != dpy)
1629 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1630
1631 EGLBoolean ret = EGL_TRUE;
1632 switch (attribute) {
1633 case EGL_CONFIG_ID:
1634 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1635 break;
1636 case EGL_WIDTH:
1637 *value = surface->getWidth();
1638 break;
1639 case EGL_HEIGHT:
1640 *value = surface->getHeight();
1641 break;
1642 case EGL_LARGEST_PBUFFER:
1643 // not modified for a window or pixmap surface
1644 break;
1645 case EGL_TEXTURE_FORMAT:
1646 *value = EGL_NO_TEXTURE;
1647 break;
1648 case EGL_TEXTURE_TARGET:
1649 *value = EGL_NO_TEXTURE;
1650 break;
1651 case EGL_MIPMAP_TEXTURE:
1652 *value = EGL_FALSE;
1653 break;
1654 case EGL_MIPMAP_LEVEL:
1655 *value = 0;
1656 break;
1657 case EGL_RENDER_BUFFER:
1658 // TODO: return the real RENDER_BUFFER here
1659 *value = EGL_BACK_BUFFER;
1660 break;
1661 case EGL_HORIZONTAL_RESOLUTION:
1662 // pixel/mm * EGL_DISPLAY_SCALING
1663 *value = surface->getHorizontalResolution();
1664 break;
1665 case EGL_VERTICAL_RESOLUTION:
1666 // pixel/mm * EGL_DISPLAY_SCALING
1667 *value = surface->getVerticalResolution();
1668 break;
1669 case EGL_PIXEL_ASPECT_RATIO: {
1670 // w/h * EGL_DISPLAY_SCALING
1671 int wr = surface->getHorizontalResolution();
1672 int hr = surface->getVerticalResolution();
1673 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1674 } break;
1675 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 break;
1678 default:
1679 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1680 }
1681 return ret;
1682}
1683
1684EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001685 EGLContext /*share_list*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686{
1687 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1688 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1689
1690 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1691 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1692
1693 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1694 c->flags = egl_context_t::NEVER_CURRENT;
1695 c->dpy = dpy;
1696 c->config = config;
1697 c->read = 0;
1698 c->draw = 0;
1699 return (EGLContext)gl;
1700}
1701
1702EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1703{
1704 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1705 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1706 egl_context_t* c = egl_context_t::context(ctx);
1707 if (c->flags & egl_context_t::IS_CURRENT)
1708 setGlThreadSpecific(0);
1709 ogles_uninit((ogles_context_t*)ctx);
1710 return EGL_TRUE;
1711}
1712
1713EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1714 EGLSurface read, EGLContext ctx)
1715{
1716 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1717 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1718 if (draw) {
1719 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001720 if (!s->isValid())
1721 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722 if (s->dpy != dpy)
1723 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001724 // TODO: check that draw is compatible with the context
1725 }
1726 if (read && read!=draw) {
1727 egl_surface_t* s = (egl_surface_t*)read;
1728 if (!s->isValid())
1729 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1730 if (s->dpy != dpy)
1731 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1732 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 }
1734
1735 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1738 return setError(EGL_BAD_MATCH, EGL_FALSE);
1739
1740 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1741 return setError(EGL_BAD_MATCH, EGL_FALSE);
1742
1743 if (ctx == EGL_NO_CONTEXT) {
1744 // if we're detaching, we need the current context
1745 current_ctx = (EGLContext)getGlThreadSpecific();
1746 } else {
1747 egl_context_t* c = egl_context_t::context(ctx);
1748 egl_surface_t* d = (egl_surface_t*)draw;
1749 egl_surface_t* r = (egl_surface_t*)read;
1750 if ((d && d->ctx && d->ctx != ctx) ||
1751 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001752 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1754 }
1755 }
1756
1757 ogles_context_t* gl = (ogles_context_t*)ctx;
1758 if (makeCurrent(gl) == 0) {
1759 if (ctx) {
1760 egl_context_t* c = egl_context_t::context(ctx);
1761 egl_surface_t* d = (egl_surface_t*)draw;
1762 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001763
1764 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001765 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1766 s->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001767 s->ctx = EGL_NO_CONTEXT;
1768 if (s->zombie)
1769 delete s;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001770 }
1771 if (c->read) {
1772 // FIXME: unlock/disconnect the read surface too
1773 }
1774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001776 c->read = read;
1777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778 if (c->flags & egl_context_t::NEVER_CURRENT) {
1779 c->flags &= ~egl_context_t::NEVER_CURRENT;
1780 GLint w = 0;
1781 GLint h = 0;
1782 if (draw) {
1783 w = d->getWidth();
1784 h = d->getHeight();
1785 }
1786 ogles_surfaceport(gl, 0, 0);
1787 ogles_viewport(gl, 0, 0, w, h);
1788 ogles_scissor(gl, 0, 0, w, h);
1789 }
1790 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001791 if (d->connect() == EGL_FALSE) {
1792 return EGL_FALSE;
1793 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794 d->ctx = ctx;
1795 d->bindDrawSurface(gl);
1796 }
1797 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001798 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 r->ctx = ctx;
1800 r->bindReadSurface(gl);
1801 }
1802 } else {
1803 // if surfaces were bound to the context bound to this thread
1804 // mark then as unbound.
1805 if (current_ctx) {
1806 egl_context_t* c = egl_context_t::context(current_ctx);
1807 egl_surface_t* d = (egl_surface_t*)c->draw;
1808 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001809 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001810 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001811 d->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001812 d->ctx = EGL_NO_CONTEXT;
1813 if (d->zombie)
1814 delete d;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001815 }
1816 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001817 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001818 r->ctx = EGL_NO_CONTEXT;
1819 // FIXME: unlock/disconnect the read surface too
1820 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 }
1822 }
1823 return EGL_TRUE;
1824 }
1825 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1826}
1827
1828EGLContext eglGetCurrentContext(void)
1829{
1830 // eglGetCurrentContext returns the current EGL rendering context,
1831 // as specified by eglMakeCurrent. If no context is current,
1832 // EGL_NO_CONTEXT is returned.
1833 return (EGLContext)getGlThreadSpecific();
1834}
1835
1836EGLSurface eglGetCurrentSurface(EGLint readdraw)
1837{
1838 // eglGetCurrentSurface returns the read or draw surface attached
1839 // to the current EGL rendering context, as specified by eglMakeCurrent.
1840 // If no context is current, EGL_NO_SURFACE is returned.
1841 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1842 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1843 egl_context_t* c = egl_context_t::context(ctx);
1844 if (readdraw == EGL_READ) {
1845 return c->read;
1846 } else if (readdraw == EGL_DRAW) {
1847 return c->draw;
1848 }
1849 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1850}
1851
1852EGLDisplay eglGetCurrentDisplay(void)
1853{
1854 // eglGetCurrentDisplay returns the current EGL display connection
1855 // for the current EGL rendering context, as specified by eglMakeCurrent.
1856 // If no context is current, EGL_NO_DISPLAY is returned.
1857 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1858 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1859 egl_context_t* c = egl_context_t::context(ctx);
1860 return c->dpy;
1861}
1862
1863EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1864 EGLint attribute, EGLint *value)
1865{
1866 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1867 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1868 egl_context_t* c = egl_context_t::context(ctx);
1869 switch (attribute) {
1870 case EGL_CONFIG_ID:
1871 // Returns the ID of the EGL frame buffer configuration with
1872 // respect to which the context was created
1873 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1874 }
1875 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1876}
1877
1878EGLBoolean eglWaitGL(void)
1879{
1880 return EGL_TRUE;
1881}
1882
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001883EGLBoolean eglWaitNative(EGLint /*engine*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884{
1885 return EGL_TRUE;
1886}
1887
1888EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1889{
1890 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1891 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001892
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001894 if (!d->isValid())
1895 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 if (d->dpy != dpy)
1897 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1898
1899 // post the surface
1900 d->swapBuffers();
1901
1902 // if it's bound to a context, update the buffer
1903 if (d->ctx != EGL_NO_CONTEXT) {
1904 d->bindDrawSurface((ogles_context_t*)d->ctx);
1905 // if this surface is also the read surface of the context
1906 // it is bound to, make sure to update the read buffer as well.
1907 // The EGL spec is a little unclear about this.
1908 egl_context_t* c = egl_context_t::context(d->ctx);
1909 if (c->read == draw) {
1910 d->bindReadSurface((ogles_context_t*)d->ctx);
1911 }
1912 }
1913
1914 return EGL_TRUE;
1915}
1916
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001917EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface /*surface*/,
1918 NativePixmapType /*target*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919{
1920 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1921 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1922 // TODO: eglCopyBuffers()
1923 return EGL_FALSE;
1924}
1925
1926EGLint eglGetError(void)
1927{
1928 return getError();
1929}
1930
1931const char* eglQueryString(EGLDisplay dpy, EGLint name)
1932{
1933 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1934 return setError(EGL_BAD_DISPLAY, (const char*)0);
1935
1936 switch (name) {
1937 case EGL_VENDOR:
1938 return gVendorString;
1939 case EGL_VERSION:
1940 return gVersionString;
1941 case EGL_EXTENSIONS:
1942 return gExtensionsString;
1943 case EGL_CLIENT_APIS:
1944 return gClientApiString;
1945 }
1946 return setError(EGL_BAD_PARAMETER, (const char *)0);
1947}
1948
1949// ----------------------------------------------------------------------------
1950// EGL 1.1
1951// ----------------------------------------------------------------------------
1952
1953EGLBoolean eglSurfaceAttrib(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001954 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*attribute*/, EGLint /*value*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955{
1956 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1957 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1958 // TODO: eglSurfaceAttrib()
1959 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1960}
1961
1962EGLBoolean eglBindTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001963 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964{
1965 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1966 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1967 // TODO: eglBindTexImage()
1968 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1969}
1970
1971EGLBoolean eglReleaseTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001972 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001973{
1974 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1975 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1976 // TODO: eglReleaseTexImage()
1977 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1978}
1979
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001980EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint /*interval*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981{
1982 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1983 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1984 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001985 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986}
1987
1988// ----------------------------------------------------------------------------
1989// EGL 1.2
1990// ----------------------------------------------------------------------------
1991
1992EGLBoolean eglBindAPI(EGLenum api)
1993{
1994 if (api != EGL_OPENGL_ES_API)
1995 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1996 return EGL_TRUE;
1997}
1998
1999EGLenum eglQueryAPI(void)
2000{
2001 return EGL_OPENGL_ES_API;
2002}
2003
2004EGLBoolean eglWaitClient(void)
2005{
2006 glFinish();
2007 return EGL_TRUE;
2008}
2009
2010EGLBoolean eglReleaseThread(void)
2011{
2012 // TODO: eglReleaseThread()
2013 return EGL_TRUE;
2014}
2015
2016EGLSurface eglCreatePbufferFromClientBuffer(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002017 EGLDisplay dpy, EGLenum /*buftype*/, EGLClientBuffer /*buffer*/,
2018 EGLConfig /*config*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019{
2020 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2021 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2022 // TODO: eglCreatePbufferFromClientBuffer()
2023 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2024}
2025
2026// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002027// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028// ----------------------------------------------------------------------------
2029
2030void (*eglGetProcAddress (const char *procname))()
2031{
2032 extention_map_t const * const map = gExtentionMap;
2033 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2034 if (!strcmp(procname, map[i].name)) {
2035 return map[i].address;
2036 }
2037 }
2038 return NULL;
2039}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002040
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002041EGLBoolean eglLockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/,
2042 const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002043{
2044 EGLBoolean result = EGL_FALSE;
2045 return result;
2046}
2047
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002048EGLBoolean eglUnlockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002049{
2050 EGLBoolean result = EGL_FALSE;
2051 return result;
2052}
2053
2054EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002055 EGLClientBuffer buffer, const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002056{
2057 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2058 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2059 }
2060 if (ctx != EGL_NO_CONTEXT) {
2061 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2062 }
2063 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2064 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2065 }
2066
Iliyan Malchev697526b2011-05-01 11:33:26 -07002067 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002068
2069 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2070 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2071
Iliyan Malchev697526b2011-05-01 11:33:26 -07002072 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002073 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002074
2075 switch (native_buffer->format) {
2076 case HAL_PIXEL_FORMAT_RGBA_8888:
2077 case HAL_PIXEL_FORMAT_RGBX_8888:
2078 case HAL_PIXEL_FORMAT_RGB_888:
2079 case HAL_PIXEL_FORMAT_RGB_565:
2080 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian8dccb262010-02-04 17:04:53 -08002081 break;
2082 default:
2083 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2084 }
2085
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002086 native_buffer->common.incRef(&native_buffer->common);
2087 return (EGLImageKHR)native_buffer;
2088}
2089
2090EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2091{
2092 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2093 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2094 }
2095
Iliyan Malchev697526b2011-05-01 11:33:26 -07002096 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002097
2098 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2099 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2100
Iliyan Malchev697526b2011-05-01 11:33:26 -07002101 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002102 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2103
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002104 native_buffer->common.decRef(&native_buffer->common);
2105
2106 return EGL_TRUE;
2107}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002108
2109// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002110// EGL_KHR_fence_sync
2111// ----------------------------------------------------------------------------
2112
2113#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2114
2115EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2116 const EGLint *attrib_list)
2117{
2118 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2119 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2120 }
2121
2122 if (type != EGL_SYNC_FENCE_KHR ||
2123 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2124 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2125 }
2126
2127 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2128 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2129 }
2130
2131 // AGL is synchronous; nothing to do here.
2132
2133 return FENCE_SYNC_HANDLE;
2134}
2135
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002136EGLBoolean eglDestroySyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002137{
2138 if (sync != FENCE_SYNC_HANDLE) {
2139 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2140 }
2141
2142 return EGL_TRUE;
2143}
2144
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002145EGLint eglClientWaitSyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync, EGLint /*flags*/,
2146 EGLTimeKHR /*timeout*/)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002147{
2148 if (sync != FENCE_SYNC_HANDLE) {
2149 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2150 }
2151
2152 return EGL_CONDITION_SATISFIED_KHR;
2153}
2154
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002155EGLBoolean eglGetSyncAttribKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync,
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002156 EGLint attribute, EGLint *value)
2157{
2158 if (sync != FENCE_SYNC_HANDLE) {
2159 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2160 }
2161
2162 switch (attribute) {
2163 case EGL_SYNC_TYPE_KHR:
2164 *value = EGL_SYNC_FENCE_KHR;
2165 return EGL_TRUE;
2166 case EGL_SYNC_STATUS_KHR:
2167 *value = EGL_SIGNALED_KHR;
2168 return EGL_TRUE;
2169 case EGL_SYNC_CONDITION_KHR:
2170 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2171 return EGL_TRUE;
2172 default:
2173 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2174 }
2175}
2176
2177// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002178// ANDROID extensions
2179// ----------------------------------------------------------------------------
2180
2181EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2182 EGLint left, EGLint top, EGLint width, EGLint height)
2183{
2184 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2185 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2186
2187 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002188 if (!d->isValid())
2189 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002190 if (d->dpy != dpy)
2191 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2192
2193 // post the surface
2194 d->setSwapRectangle(left, top, width, height);
2195
2196 return EGL_TRUE;
2197}