blob: 172ef9571443834a97a6a2eedf17009236b36203 [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;
67#ifndef HAVE_ANDROID_OS
68namespace 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 }
81 pthread_setspecific(gEGLErrorKey, (void*)error);
82 return returnValue;
83}
84
85static GLint getError() {
86 if (ggl_unlikely(gEGLErrorKey == -1))
87 return EGL_SUCCESS;
88 GLint error = (GLint)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 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
95 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;
150
151 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
152 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700153 bool isValid() const;
154 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
157 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700158 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700159 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 virtual EGLint getWidth() const = 0;
161 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 virtual EGLint getHorizontalResolution() const;
164 virtual EGLint getVerticalResolution() const;
165 virtual EGLint getRefreshRate() const;
166 virtual EGLint getSwapBehavior() const;
167 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700168 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169protected:
170 GGLSurface depth;
171};
172
173egl_surface_t::egl_surface_t(EGLDisplay dpy,
174 EGLConfig config,
175 int32_t depthFormat)
176 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
177{
178 depth.version = sizeof(GGLSurface);
179 depth.data = 0;
180 depth.format = depthFormat;
181}
182egl_surface_t::~egl_surface_t()
183{
184 magic = 0;
185 free(depth.data);
186}
Mathias Agopian0696a572009-08-20 00:12:56 -0700187bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000188 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700189 return magic == MAGIC;
190}
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192EGLBoolean egl_surface_t::swapBuffers() {
193 return EGL_FALSE;
194}
195EGLint egl_surface_t::getHorizontalResolution() const {
196 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
197}
198EGLint egl_surface_t::getVerticalResolution() const {
199 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
200}
201EGLint egl_surface_t::getRefreshRate() const {
202 return (60 * EGL_DISPLAY_SCALING);
203}
204EGLint egl_surface_t::getSwapBehavior() const {
205 return EGL_BUFFER_PRESERVED;
206}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700207EGLBoolean egl_surface_t::setSwapRectangle(
208 EGLint l, EGLint t, EGLint w, EGLint h)
209{
210 return EGL_FALSE;
211}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212
213// ----------------------------------------------------------------------------
214
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 EGLDisplay dpy, EGLConfig config,
219 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700220 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221
Mathias Agopian0696a572009-08-20 00:12:56 -0700222 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223
Mathias Agopian0696a572009-08-20 00:12:56 -0700224 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 virtual EGLBoolean swapBuffers();
226 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
227 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700228 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700229 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700230 virtual EGLint getWidth() const { return width; }
231 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 virtual EGLint getHorizontalResolution() const;
233 virtual EGLint getVerticalResolution() const;
234 virtual EGLint getRefreshRate() const;
235 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700236 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700239 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
240 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700241 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700242 ANativeWindowBuffer* buffer;
243 ANativeWindowBuffer* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700244 gralloc_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700245 int width;
246 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700247 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700248 GGLFormat const* pixelFormatTable;
249
250 struct Rect {
251 inline Rect() { };
252 inline Rect(int32_t w, int32_t h)
253 : left(0), top(0), right(w), bottom(h) { }
254 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
255 : left(l), top(t), right(r), bottom(b) { }
256 Rect& andSelf(const Rect& r) {
257 left = max(left, r.left);
258 top = max(top, r.top);
259 right = min(right, r.right);
260 bottom = min(bottom, r.bottom);
261 return *this;
262 }
263 bool isEmpty() const {
264 return (left>=right || top>=bottom);
265 }
266 void dump(char const* what) {
Steve Block9d453682011-12-20 16:23:08 +0000267 ALOGD("%s { %5d, %5d, w=%5d, h=%5d }",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700268 what, left, top, right-left, bottom-top);
269 }
270
271 int32_t left;
272 int32_t top;
273 int32_t right;
274 int32_t bottom;
275 };
276
277 struct Region {
278 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700279 typedef Rect const* const_iterator;
280 const_iterator begin() const { return storage; }
281 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700282 static Region subtract(const Rect& lhs, const Rect& rhs) {
283 Region reg;
284 Rect* storage = reg.storage;
285 if (!lhs.isEmpty()) {
286 if (lhs.top < rhs.top) { // top rect
287 storage->left = lhs.left;
288 storage->top = lhs.top;
289 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700290 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700291 storage++;
292 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700293 const int32_t top = max(lhs.top, rhs.top);
294 const int32_t bot = min(lhs.bottom, rhs.bottom);
295 if (top < bot) {
296 if (lhs.left < rhs.left) { // left-side rect
297 storage->left = lhs.left;
298 storage->top = top;
299 storage->right = rhs.left;
300 storage->bottom = bot;
301 storage++;
302 }
303 if (lhs.right > rhs.right) { // right-side rect
304 storage->left = rhs.right;
305 storage->top = top;
306 storage->right = lhs.right;
307 storage->bottom = bot;
308 storage++;
309 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700310 }
311 if (lhs.bottom > rhs.bottom) { // bottom rect
312 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700313 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700314 storage->right = lhs.right;
315 storage->bottom = lhs.bottom;
316 storage++;
317 }
318 reg.count = storage - reg.storage;
319 }
320 return reg;
321 }
322 bool isEmpty() const {
323 return count<=0;
324 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700325 private:
326 Rect storage[4];
327 ssize_t count;
328 };
329
330 void copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700331 ANativeWindowBuffer* dst, void* dst_vaddr,
332 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700333 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700334
335 Rect dirtyRegion;
336 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337};
338
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 EGLConfig config,
341 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700342 ANativeWindow* window)
Mathias Agopian0926f502009-05-04 14:17:04 -0700343 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700344 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700345 bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700347 hw_module_t const* pModule;
348 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
349 module = reinterpret_cast<gralloc_module_t const*>(pModule);
350
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700351 pixelFormatTable = gglGetPixelFormatTable();
352
353 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700354 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700355 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
356 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700357}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700358
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700359egl_window_surface_v2_t::~egl_window_surface_v2_t() {
360 if (buffer) {
361 buffer->common.decRef(&buffer->common);
362 }
363 if (previousBuffer) {
364 previousBuffer->common.decRef(&previousBuffer->common);
365 }
366 nativeWindow->common.decRef(&nativeWindow->common);
367}
368
Mathias Agopiancf81c842009-07-31 14:47:00 -0700369EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700370{
Mathias Agopian52212712009-08-11 22:34:02 -0700371 // we're intending to do software rendering
372 native_window_set_usage(nativeWindow,
373 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
374
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700375 // dequeue a buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700376 int fenceFd = -1;
377 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer,
378 &fenceFd) != NO_ERROR) {
379 return setError(EGL_BAD_ALLOC, EGL_FALSE);
380 }
381
382 // wait for the buffer
383 sp<Fence> fence(new Fence(fenceFd));
384 if (fence->wait(Fence::TIMEOUT_NEVER) != NO_ERROR) {
385 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700386 return setError(EGL_BAD_ALLOC, EGL_FALSE);
387 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700388
389 // allocate a corresponding depth-buffer
390 width = buffer->width;
391 height = buffer->height;
392 if (depth.format) {
393 depth.width = width;
394 depth.height = height;
395 depth.stride = depth.width; // use the width here
396 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
397 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700398 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700399 }
400 }
401
402 // keep a reference on the buffer
403 buffer->common.incRef(&buffer->common);
404
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700405 // pin the buffer down
406 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
407 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000408 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700409 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700410 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700411 // FIXME: we should make sure we're not accessing the buffer anymore
412 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700413 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700414}
415
416void egl_window_surface_v2_t::disconnect()
417{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700418 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700419 bits = NULL;
420 unlock(buffer);
421 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700422 // enqueue the last frame
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700423 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700424 if (buffer) {
425 buffer->common.decRef(&buffer->common);
426 buffer = 0;
427 }
428 if (previousBuffer) {
429 previousBuffer->common.decRef(&previousBuffer->common);
430 previousBuffer = 0;
431 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
Mathias Agopian0926f502009-05-04 14:17:04 -0700434status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700435 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700436{
Mathias Agopianbc492912009-11-03 20:38:08 -0800437 int err;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800438
439 err = module->lock(module, buf->handle,
440 usage, 0, 0, buf->width, buf->height, vaddr);
441
Mathias Agopian0926f502009-05-04 14:17:04 -0700442 return err;
443}
444
Iliyan Malchev697526b2011-05-01 11:33:26 -0700445status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700446{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700447 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800448 int err = NO_ERROR;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800449
450 err = module->unlock(module, buf->handle);
451
Mathias Agopian0926f502009-05-04 14:17:04 -0700452 return err;
453}
454
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700455void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700456 ANativeWindowBuffer* dst, void* dst_vaddr,
457 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700458 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700459{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700460 // NOTE: dst and src must be the same format
461
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700462 Region::const_iterator cur = clip.begin();
463 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700464
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700465 const size_t bpp = pixelFormatTable[src->format].size;
466 const size_t dbpr = dst->stride * bpp;
467 const size_t sbpr = src->stride * bpp;
468
469 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
470 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
471
472 while (cur != end) {
473 const Rect& r(*cur++);
474 ssize_t w = r.right - r.left;
475 ssize_t h = r.bottom - r.top;
476 if (w <= 0 || h<=0) continue;
477 size_t size = w * bpp;
478 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
479 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
480 if (dbpr==sbpr && size==sbpr) {
481 size *= h;
482 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700483 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700484 do {
485 memcpy(d, s, size);
486 d += dbpr;
487 s += sbpr;
488 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700489 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490}
491
492EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700494 if (!buffer) {
495 return setError(EGL_BAD_ACCESS, EGL_FALSE);
496 }
497
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700498 /*
499 * Handle eglSetSwapRectangleANDROID()
500 * We copyback from the front buffer
501 */
502 if (!dirtyRegion.isEmpty()) {
503 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
504 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100505 // This was const Region copyBack, but that causes an
506 // internal compile error on simulator builds
507 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700508 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700509 void* prevBits;
510 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700511 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
512 // copy from previousBuffer to buffer
513 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700514 unlock(previousBuffer);
515 }
516 }
517 }
518 oldDirtyRegion = dirtyRegion;
519 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700520
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700521 if (previousBuffer) {
522 previousBuffer->common.decRef(&previousBuffer->common);
523 previousBuffer = 0;
524 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525
Mathias Agopian0926f502009-05-04 14:17:04 -0700526 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700527 previousBuffer = buffer;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700528 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700529 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700531 // dequeue a new buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700532 int fenceFd = -1;
533 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) == NO_ERROR) {
534 sp<Fence> fence(new Fence(fenceFd));
535 if (fence->wait(Fence::TIMEOUT_NEVER)) {
536 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
537 return setError(EGL_BAD_ALLOC, EGL_FALSE);
538 }
Mathias Agopian031213e2010-08-18 16:07:34 -0700539
540 // reallocate the depth-buffer if needed
541 if ((width != buffer->width) || (height != buffer->height)) {
542 // TODO: we probably should reset the swap rect here
543 // if the window size has changed
544 width = buffer->width;
545 height = buffer->height;
546 if (depth.data) {
547 free(depth.data);
548 depth.width = width;
549 depth.height = height;
550 depth.stride = buffer->stride;
551 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
552 if (depth.data == 0) {
553 setError(EGL_BAD_ALLOC, EGL_FALSE);
554 return EGL_FALSE;
555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
557 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700558
Mathias Agopian031213e2010-08-18 16:07:34 -0700559 // keep a reference on the buffer
560 buffer->common.incRef(&buffer->common);
561
562 // finally pin the buffer down
563 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
564 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000565 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700566 buffer, buffer->width, buffer->height);
567 return setError(EGL_BAD_ACCESS, EGL_FALSE);
568 // FIXME: we should make sure we're not accessing the buffer anymore
569 }
570 } else {
571 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700572 }
573
574 return EGL_TRUE;
575}
576
577EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
578 EGLint l, EGLint t, EGLint w, EGLint h)
579{
580 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 return EGL_TRUE;
582}
583
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700584EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585{
586 GGLSurface buffer;
587 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 buffer.width = this->buffer->width;
589 buffer.height = this->buffer->height;
590 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700591 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700592 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 gl->rasterizer.procs.colorBuffer(gl, &buffer);
594 if (depth.data != gl->rasterizer.state.buffers.depth.data)
595 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 return EGL_TRUE;
598}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600{
601 GGLSurface buffer;
602 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 buffer.width = this->buffer->width;
604 buffer.height = this->buffer->height;
605 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700606 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 gl->rasterizer.procs.readBuffer(gl, &buffer);
609 return EGL_TRUE;
610}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
613}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617EGLint egl_window_surface_v2_t::getRefreshRate() const {
618 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700620EGLint egl_window_surface_v2_t::getSwapBehavior() const
621{
622 /*
623 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
624 * the content of the swapped buffer.
625 *
626 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
627 *
628 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
629 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
630 * is, everything outside of this area is preserved.
631 *
632 * This implementation of EGL assumes the later case.
633 *
634 */
635
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637}
638
639// ----------------------------------------------------------------------------
640
641struct egl_pixmap_surface_t : public egl_surface_t
642{
643 egl_pixmap_surface_t(
644 EGLDisplay dpy, EGLConfig config,
645 int32_t depthFormat,
646 egl_native_pixmap_t const * pixmap);
647
648 virtual ~egl_pixmap_surface_t() { }
649
Mathias Agopian0696a572009-08-20 00:12:56 -0700650 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
652 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
653 virtual EGLint getWidth() const { return nativePixmap.width; }
654 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655private:
656 egl_native_pixmap_t nativePixmap;
657};
658
659egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
660 EGLConfig config,
661 int32_t depthFormat,
662 egl_native_pixmap_t const * pixmap)
663 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
664{
665 if (depthFormat) {
666 depth.width = pixmap->width;
667 depth.height = pixmap->height;
668 depth.stride = depth.width; // use the width here
669 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
670 if (depth.data == 0) {
671 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673 }
674}
675EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
676{
677 GGLSurface buffer;
678 buffer.version = sizeof(GGLSurface);
679 buffer.width = nativePixmap.width;
680 buffer.height = nativePixmap.height;
681 buffer.stride = nativePixmap.stride;
682 buffer.data = nativePixmap.data;
683 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 gl->rasterizer.procs.colorBuffer(gl, &buffer);
686 if (depth.data != gl->rasterizer.state.buffers.depth.data)
687 gl->rasterizer.procs.depthBuffer(gl, &depth);
688 return EGL_TRUE;
689}
690EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
691{
692 GGLSurface buffer;
693 buffer.version = sizeof(GGLSurface);
694 buffer.width = nativePixmap.width;
695 buffer.height = nativePixmap.height;
696 buffer.stride = nativePixmap.stride;
697 buffer.data = nativePixmap.data;
698 buffer.format = nativePixmap.format;
699 gl->rasterizer.procs.readBuffer(gl, &buffer);
700 return EGL_TRUE;
701}
702
703// ----------------------------------------------------------------------------
704
705struct egl_pbuffer_surface_t : public egl_surface_t
706{
707 egl_pbuffer_surface_t(
708 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
709 int32_t w, int32_t h, int32_t f);
710
711 virtual ~egl_pbuffer_surface_t();
712
Mathias Agopian0696a572009-08-20 00:12:56 -0700713 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
715 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
716 virtual EGLint getWidth() const { return pbuffer.width; }
717 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718private:
719 GGLSurface pbuffer;
720};
721
722egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
723 EGLConfig config, int32_t depthFormat,
724 int32_t w, int32_t h, int32_t f)
725 : egl_surface_t(dpy, config, depthFormat)
726{
727 size_t size = w*h;
728 switch (f) {
729 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
730 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
731 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800732 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000734 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 pbuffer.data = 0;
736 break;
737 }
738 pbuffer.version = sizeof(GGLSurface);
739 pbuffer.width = w;
740 pbuffer.height = h;
741 pbuffer.stride = w;
742 pbuffer.data = (GGLubyte*)malloc(size);
743 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 if (depthFormat) {
746 depth.width = pbuffer.width;
747 depth.height = pbuffer.height;
748 depth.stride = depth.width; // use the width here
749 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
750 if (depth.data == 0) {
751 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
752 return;
753 }
754 }
755}
756egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
757 free(pbuffer.data);
758}
759EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
760{
761 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
762 if (depth.data != gl->rasterizer.state.buffers.depth.data)
763 gl->rasterizer.procs.depthBuffer(gl, &depth);
764 return EGL_TRUE;
765}
766EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
767{
768 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
769 return EGL_TRUE;
770}
771
772// ----------------------------------------------------------------------------
773
774struct config_pair_t {
775 GLint key;
776 GLint value;
777};
778
779struct configs_t {
780 const config_pair_t* array;
781 int size;
782};
783
784struct config_management_t {
785 GLint key;
786 bool (*match)(GLint reqValue, GLint confValue);
787 static bool atLeast(GLint reqValue, GLint confValue) {
788 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
789 }
790 static bool exact(GLint reqValue, GLint confValue) {
791 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
792 }
793 static bool mask(GLint reqValue, GLint confValue) {
794 return (confValue & reqValue) == reqValue;
795 }
Mathias Agopian63971672010-10-25 15:51:24 -0700796 static bool ignore(GLint reqValue, GLint confValue) {
797 return true;
798 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799};
800
801// ----------------------------------------------------------------------------
802
803#define VERSION_MAJOR 1
804#define VERSION_MINOR 2
805static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700806static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700807static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700809 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700810 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 // "KHR_image_pixmap "
812 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700813 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700814 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815
816// ----------------------------------------------------------------------------
817
818struct extention_map_t {
819 const char * const name;
820 __eglMustCastToProperFunctionPointerType address;
821};
822
823static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700824 { "glDrawTexsOES",
825 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
826 { "glDrawTexiOES",
827 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
828 { "glDrawTexfOES",
829 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
830 { "glDrawTexxOES",
831 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
832 { "glDrawTexsvOES",
833 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
834 { "glDrawTexivOES",
835 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
836 { "glDrawTexfvOES",
837 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
838 { "glDrawTexxvOES",
839 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
840 { "glQueryMatrixxOES",
841 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
842 { "glEGLImageTargetTexture2DOES",
843 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
844 { "glEGLImageTargetRenderbufferStorageOES",
845 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
846 { "glClipPlanef",
847 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
848 { "glClipPlanex",
849 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
850 { "glBindBuffer",
851 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
852 { "glBufferData",
853 (__eglMustCastToProperFunctionPointerType)&glBufferData },
854 { "glBufferSubData",
855 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
856 { "glDeleteBuffers",
857 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
858 { "glGenBuffers",
859 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700860 { "eglCreateImageKHR",
861 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
862 { "eglDestroyImageKHR",
863 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700864 { "eglCreateSyncKHR",
865 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
866 { "eglDestroySyncKHR",
867 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
868 { "eglClientWaitSyncKHR",
869 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
870 { "eglGetSyncAttribKHR",
871 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700872 { "eglSetSwapRectangleANDROID",
873 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874};
875
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700876/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 * In the lists below, attributes names MUST be sorted.
878 * Additionally, all configs must be sorted according to
879 * the EGL specification.
880 */
881
882static config_pair_t const config_base_attribute_list[] = {
883 { EGL_STENCIL_SIZE, 0 },
884 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
885 { EGL_LEVEL, 0 },
886 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700887 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
889 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
890 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
891 { EGL_NATIVE_VISUAL_ID, 0 },
892 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
893 { EGL_SAMPLES, 0 },
894 { EGL_SAMPLE_BUFFERS, 0 },
895 { EGL_TRANSPARENT_TYPE, EGL_NONE },
896 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
897 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
898 { EGL_TRANSPARENT_RED_VALUE, 0 },
899 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
900 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
901 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700902 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700903 { EGL_LUMINANCE_SIZE, 0 },
904 { EGL_ALPHA_MASK_SIZE, 0 },
905 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700906 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700907 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908};
909
910// These configs can override the base attribute list
911// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
912
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800913// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914static config_pair_t const config_0_attribute_list[] = {
915 { EGL_BUFFER_SIZE, 16 },
916 { EGL_ALPHA_SIZE, 0 },
917 { EGL_BLUE_SIZE, 5 },
918 { EGL_GREEN_SIZE, 6 },
919 { EGL_RED_SIZE, 5 },
920 { EGL_DEPTH_SIZE, 0 },
921 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700922 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
924};
925
926static config_pair_t const config_1_attribute_list[] = {
927 { EGL_BUFFER_SIZE, 16 },
928 { EGL_ALPHA_SIZE, 0 },
929 { EGL_BLUE_SIZE, 5 },
930 { EGL_GREEN_SIZE, 6 },
931 { EGL_RED_SIZE, 5 },
932 { EGL_DEPTH_SIZE, 16 },
933 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700934 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
936};
937
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800938// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939static config_pair_t const config_2_attribute_list[] = {
940 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800941 { EGL_ALPHA_SIZE, 0 },
942 { EGL_BLUE_SIZE, 8 },
943 { EGL_GREEN_SIZE, 8 },
944 { EGL_RED_SIZE, 8 },
945 { EGL_DEPTH_SIZE, 0 },
946 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700947 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800948 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
949};
950
951static config_pair_t const config_3_attribute_list[] = {
952 { EGL_BUFFER_SIZE, 32 },
953 { EGL_ALPHA_SIZE, 0 },
954 { EGL_BLUE_SIZE, 8 },
955 { EGL_GREEN_SIZE, 8 },
956 { EGL_RED_SIZE, 8 },
957 { EGL_DEPTH_SIZE, 16 },
958 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700959 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800960 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
961};
962
963// 8888 configs
964static config_pair_t const config_4_attribute_list[] = {
965 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 { EGL_ALPHA_SIZE, 8 },
967 { EGL_BLUE_SIZE, 8 },
968 { EGL_GREEN_SIZE, 8 },
969 { EGL_RED_SIZE, 8 },
970 { EGL_DEPTH_SIZE, 0 },
971 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700972 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
974};
975
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800976static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 { EGL_BUFFER_SIZE, 32 },
978 { EGL_ALPHA_SIZE, 8 },
979 { EGL_BLUE_SIZE, 8 },
980 { EGL_GREEN_SIZE, 8 },
981 { EGL_RED_SIZE, 8 },
982 { EGL_DEPTH_SIZE, 16 },
983 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700984 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
986};
987
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800988// A8 configs
989static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 { EGL_BUFFER_SIZE, 8 },
991 { EGL_ALPHA_SIZE, 8 },
992 { EGL_BLUE_SIZE, 0 },
993 { EGL_GREEN_SIZE, 0 },
994 { EGL_RED_SIZE, 0 },
995 { EGL_DEPTH_SIZE, 0 },
996 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700997 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
999};
1000
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001001static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 { EGL_BUFFER_SIZE, 8 },
1003 { EGL_ALPHA_SIZE, 8 },
1004 { EGL_BLUE_SIZE, 0 },
1005 { EGL_GREEN_SIZE, 0 },
1006 { EGL_RED_SIZE, 0 },
1007 { EGL_DEPTH_SIZE, 16 },
1008 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001009 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1011};
1012
1013static configs_t const gConfigs[] = {
1014 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1015 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1016 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1017 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1018 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1019 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001020 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1021 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022};
1023
1024static config_management_t const gConfigManagement[] = {
1025 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1026 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1027 { EGL_BLUE_SIZE, config_management_t::atLeast },
1028 { EGL_GREEN_SIZE, config_management_t::atLeast },
1029 { EGL_RED_SIZE, config_management_t::atLeast },
1030 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1031 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1032 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1033 { EGL_CONFIG_ID, config_management_t::exact },
1034 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001035 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1036 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1037 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001039 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1041 { EGL_SAMPLES, config_management_t::exact },
1042 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1043 { EGL_SURFACE_TYPE, config_management_t::mask },
1044 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1045 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1046 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1047 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1048 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1049 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1050 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1051 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001052 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1053 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1054 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1055 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1056 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057};
1058
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001061 // attributes that are not specified are simply ignored, if a particular
1062 // one needs not be ignored, it must be specified here, eg:
1063 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064};
1065
1066// ----------------------------------------------------------------------------
1067
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001068static status_t getConfigFormatInfo(EGLint configID,
1069 int32_t& pixelFormat, int32_t& depthFormat)
1070{
1071 switch(configID) {
1072 case 0:
1073 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1074 depthFormat = 0;
1075 break;
1076 case 1:
1077 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1078 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1079 break;
1080 case 2:
1081 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1082 depthFormat = 0;
1083 break;
1084 case 3:
1085 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1086 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1087 break;
1088 case 4:
1089 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1090 depthFormat = 0;
1091 break;
1092 case 5:
1093 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1094 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1095 break;
1096 case 6:
1097 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1098 depthFormat = 0;
1099 break;
1100 case 7:
1101 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1102 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1103 break;
1104 default:
1105 return NAME_NOT_FOUND;
1106 }
1107 return NO_ERROR;
1108}
1109
1110// ----------------------------------------------------------------------------
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112template<typename T>
1113static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1114{
1115 while (first <= last) {
1116 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001117 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001119 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 last = mid - 1;
1121 } else {
1122 return mid;
1123 }
1124 }
1125 return -1;
1126}
1127
1128static int isAttributeMatching(int i, EGLint attr, EGLint val)
1129{
1130 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001131 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 int index = binarySearch<config_pair_t>(
1133 gConfigs[i].array,
1134 0, gConfigs[i].size-1,
1135 attr);
1136 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001137 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 index = binarySearch<config_pair_t>(
1139 config_base_attribute_list,
1140 0, NELEM(config_base_attribute_list)-1,
1141 attr);
1142 }
1143 if (index >= 0) {
1144 // attribute found, check if this config could match
1145 int cfgMgtIndex = binarySearch<config_management_t>(
1146 gConfigManagement,
1147 0, NELEM(gConfigManagement)-1,
1148 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001149 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 bool match = gConfigManagement[cfgMgtIndex].match(
1151 val, configFound[index].value);
1152 if (match) {
1153 // this config matches
1154 return 1;
1155 }
1156 } else {
1157 // attribute not found. this should NEVER happen.
1158 }
1159 } else {
1160 // error, this attribute doesn't exist
1161 }
1162 return 0;
1163}
1164
1165static int makeCurrent(ogles_context_t* gl)
1166{
1167 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1168 if (gl) {
1169 egl_context_t* c = egl_context_t::context(gl);
1170 if (c->flags & egl_context_t::IS_CURRENT) {
1171 if (current != gl) {
1172 // it is an error to set a context current, if it's already
1173 // current to another thread
1174 return -1;
1175 }
1176 } else {
1177 if (current) {
1178 // mark the current context as not current, and flush
1179 glFlush();
1180 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1181 }
1182 }
1183 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1184 // The context is not current, make it current!
1185 setGlThreadSpecific(gl);
1186 c->flags |= egl_context_t::IS_CURRENT;
1187 }
1188 } else {
1189 if (current) {
1190 // mark the current context as not current, and flush
1191 glFlush();
1192 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1193 }
1194 // this thread has no context attached to it
1195 setGlThreadSpecific(0);
1196 }
1197 return 0;
1198}
1199
1200static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1201 EGLint attribute, EGLint *value)
1202{
1203 size_t numConfigs = NELEM(gConfigs);
1204 int index = (int)config;
1205 if (uint32_t(index) >= numConfigs)
1206 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1207
1208 int attrIndex;
1209 attrIndex = binarySearch<config_pair_t>(
1210 gConfigs[index].array,
1211 0, gConfigs[index].size-1,
1212 attribute);
1213 if (attrIndex>=0) {
1214 *value = gConfigs[index].array[attrIndex].value;
1215 return EGL_TRUE;
1216 }
1217
1218 attrIndex = binarySearch<config_pair_t>(
1219 config_base_attribute_list,
1220 0, NELEM(config_base_attribute_list)-1,
1221 attribute);
1222 if (attrIndex>=0) {
1223 *value = config_base_attribute_list[attrIndex].value;
1224 return EGL_TRUE;
1225 }
1226 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1227}
1228
1229static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1230 NativeWindowType window, const EGLint *attrib_list)
1231{
1232 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1233 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1234 if (window == 0)
1235 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1236
1237 EGLint surfaceType;
1238 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1239 return EGL_FALSE;
1240
1241 if (!(surfaceType & EGL_WINDOW_BIT))
1242 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1243
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001244 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001245 ANDROID_NATIVE_WINDOW_MAGIC) {
1246 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1247 }
1248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 EGLint configID;
1250 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1251 return EGL_FALSE;
1252
1253 int32_t depthFormat;
1254 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001255 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1257 }
1258
1259 // FIXME: we don't have access to the pixelFormat here just yet.
1260 // (it's possible that the surface is not fully initialized)
1261 // maybe this should be done after the page-flip
1262 //if (EGLint(info.format) != pixelFormat)
1263 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1264
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265 egl_surface_t* surface;
1266 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001267 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268
Mathias Agopian0696a572009-08-20 00:12:56 -07001269 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 // there was a problem in the ctor, the error
1271 // flag has been set.
1272 delete surface;
1273 surface = 0;
1274 }
1275 return surface;
1276}
1277
1278static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1279 NativePixmapType pixmap, const EGLint *attrib_list)
1280{
1281 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1282 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1283 if (pixmap == 0)
1284 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1285
1286 EGLint surfaceType;
1287 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1288 return EGL_FALSE;
1289
1290 if (!(surfaceType & EGL_PIXMAP_BIT))
1291 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1292
Mathias Agopian0696a572009-08-20 00:12:56 -07001293 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1294 sizeof(egl_native_pixmap_t)) {
1295 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1296 }
1297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 EGLint configID;
1299 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1300 return EGL_FALSE;
1301
1302 int32_t depthFormat;
1303 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001304 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1306 }
1307
1308 if (pixmap->format != pixelFormat)
1309 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1310
1311 egl_surface_t* surface =
1312 new egl_pixmap_surface_t(dpy, config, depthFormat,
1313 static_cast<egl_native_pixmap_t*>(pixmap));
1314
Mathias Agopian0696a572009-08-20 00:12:56 -07001315 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 // there was a problem in the ctor, the error
1317 // flag has been set.
1318 delete surface;
1319 surface = 0;
1320 }
1321 return surface;
1322}
1323
1324static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1325 const EGLint *attrib_list)
1326{
1327 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1328 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1329
1330 EGLint surfaceType;
1331 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1332 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 if (!(surfaceType & EGL_PBUFFER_BIT))
1335 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 EGLint configID;
1338 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1339 return EGL_FALSE;
1340
1341 int32_t depthFormat;
1342 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001343 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1345 }
1346
1347 int32_t w = 0;
1348 int32_t h = 0;
1349 while (attrib_list[0]) {
1350 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1351 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1352 attrib_list+=2;
1353 }
1354
1355 egl_surface_t* surface =
1356 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1357
Mathias Agopian0696a572009-08-20 00:12:56 -07001358 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 // there was a problem in the ctor, the error
1360 // flag has been set.
1361 delete surface;
1362 surface = 0;
1363 }
1364 return surface;
1365}
1366
1367// ----------------------------------------------------------------------------
1368}; // namespace android
1369// ----------------------------------------------------------------------------
1370
1371using namespace android;
1372
1373// ----------------------------------------------------------------------------
1374// Initialization
1375// ----------------------------------------------------------------------------
1376
1377EGLDisplay eglGetDisplay(NativeDisplayType display)
1378{
1379#ifndef HAVE_ANDROID_OS
1380 // this just needs to be done once
1381 if (gGLKey == -1) {
1382 pthread_mutex_lock(&gInitMutex);
1383 if (gGLKey == -1)
1384 pthread_key_create(&gGLKey, NULL);
1385 pthread_mutex_unlock(&gInitMutex);
1386 }
1387#endif
1388 if (display == EGL_DEFAULT_DISPLAY) {
1389 EGLDisplay dpy = (EGLDisplay)1;
1390 egl_display_t& d = egl_display_t::get_display(dpy);
1391 d.type = display;
1392 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001393 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 return EGL_NO_DISPLAY;
1395}
1396
1397EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1398{
1399 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1400 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 EGLBoolean res = EGL_TRUE;
1403 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 if (android_atomic_inc(&d.initialized) == 0) {
1406 // initialize stuff here if needed
1407 //pthread_mutex_lock(&gInitMutex);
1408 //pthread_mutex_unlock(&gInitMutex);
1409 }
1410
1411 if (res == EGL_TRUE) {
1412 if (major != NULL) *major = VERSION_MAJOR;
1413 if (minor != NULL) *minor = VERSION_MINOR;
1414 }
1415 return res;
1416}
1417
1418EGLBoolean eglTerminate(EGLDisplay dpy)
1419{
1420 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1421 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1422
1423 EGLBoolean res = EGL_TRUE;
1424 egl_display_t& d = egl_display_t::get_display(dpy);
1425 if (android_atomic_dec(&d.initialized) == 1) {
1426 // TODO: destroy all resources (surfaces, contexts, etc...)
1427 //pthread_mutex_lock(&gInitMutex);
1428 //pthread_mutex_unlock(&gInitMutex);
1429 }
1430 return res;
1431}
1432
1433// ----------------------------------------------------------------------------
1434// configuration
1435// ----------------------------------------------------------------------------
1436
1437EGLBoolean eglGetConfigs( EGLDisplay dpy,
1438 EGLConfig *configs,
1439 EGLint config_size, EGLint *num_config)
1440{
1441 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1442 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1443
1444 GLint numConfigs = NELEM(gConfigs);
1445 if (!configs) {
1446 *num_config = numConfigs;
1447 return EGL_TRUE;
1448 }
1449 GLint i;
1450 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1451 *configs++ = (EGLConfig)i;
1452 }
1453 *num_config = i;
1454 return EGL_TRUE;
1455}
1456
1457EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1458 EGLConfig *configs, EGLint config_size,
1459 EGLint *num_config)
1460{
1461 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1462 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001463
1464 if (ggl_unlikely(num_config==0)) {
1465 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467
Jack Palevich749c63d2009-03-25 15:12:17 -07001468 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001469 /*
1470 * A NULL attrib_list should be treated as though it was an empty
1471 * one (terminated with EGL_NONE) as defined in
1472 * section 3.4.1 "Querying Configurations" in the EGL specification.
1473 */
1474 static const EGLint dummy = EGL_NONE;
1475 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 int numAttributes = 0;
1479 int numConfigs = NELEM(gConfigs);
1480 uint32_t possibleMatch = (1<<numConfigs)-1;
1481 while(possibleMatch && *attrib_list != EGL_NONE) {
1482 numAttributes++;
1483 EGLint attr = *attrib_list++;
1484 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001485 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 if (!(possibleMatch & (1<<i)))
1487 continue;
1488 if (isAttributeMatching(i, attr, val) == 0) {
1489 possibleMatch &= ~(1<<i);
1490 }
1491 }
1492 }
1493
1494 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001495 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1496 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 // default value
1498 if (binarySearch<config_pair_t>(
1499 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001500 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 config_defaults[j].key) < 0)
1502 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001503 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 if (!(possibleMatch & (1<<i)))
1505 continue;
1506 if (isAttributeMatching(i,
1507 config_defaults[j].key,
1508 config_defaults[j].value) == 0)
1509 {
1510 possibleMatch &= ~(1<<i);
1511 }
1512 }
1513 }
1514 }
1515
1516 // return the configurations found
1517 int n=0;
1518 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001519 if (configs) {
1520 for (int i=0 ; config_size && i<numConfigs ; i++) {
1521 if (possibleMatch & (1<<i)) {
1522 *configs++ = (EGLConfig)i;
1523 config_size--;
1524 n++;
1525 }
1526 }
1527 } else {
1528 for (int i=0 ; i<numConfigs ; i++) {
1529 if (possibleMatch & (1<<i)) {
1530 n++;
1531 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 }
1533 }
1534 }
1535 *num_config = n;
1536 return EGL_TRUE;
1537}
1538
1539EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1540 EGLint attribute, EGLint *value)
1541{
1542 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1543 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1544
1545 return getConfigAttrib(dpy, config, attribute, value);
1546}
1547
1548// ----------------------------------------------------------------------------
1549// surfaces
1550// ----------------------------------------------------------------------------
1551
1552EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1553 NativeWindowType window,
1554 const EGLint *attrib_list)
1555{
1556 return createWindowSurface(dpy, config, window, attrib_list);
1557}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1560 NativePixmapType pixmap,
1561 const EGLint *attrib_list)
1562{
1563 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1564}
1565
1566EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1567 const EGLint *attrib_list)
1568{
1569 return createPbufferSurface(dpy, config, attrib_list);
1570}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1573{
1574 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1575 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1576 if (eglSurface != EGL_NO_SURFACE) {
1577 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001578 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1580 if (surface->dpy != dpy)
1581 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001582 if (surface->ctx) {
1583 // FIXME: this surface is current check what the spec says
1584 surface->disconnect();
1585 surface->ctx = 0;
1586 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 delete surface;
1588 }
1589 return EGL_TRUE;
1590}
1591
1592EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1593 EGLint attribute, EGLint *value)
1594{
1595 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1596 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1597 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001598 if (!surface->isValid())
1599 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 if (surface->dpy != dpy)
1601 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1602
1603 EGLBoolean ret = EGL_TRUE;
1604 switch (attribute) {
1605 case EGL_CONFIG_ID:
1606 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1607 break;
1608 case EGL_WIDTH:
1609 *value = surface->getWidth();
1610 break;
1611 case EGL_HEIGHT:
1612 *value = surface->getHeight();
1613 break;
1614 case EGL_LARGEST_PBUFFER:
1615 // not modified for a window or pixmap surface
1616 break;
1617 case EGL_TEXTURE_FORMAT:
1618 *value = EGL_NO_TEXTURE;
1619 break;
1620 case EGL_TEXTURE_TARGET:
1621 *value = EGL_NO_TEXTURE;
1622 break;
1623 case EGL_MIPMAP_TEXTURE:
1624 *value = EGL_FALSE;
1625 break;
1626 case EGL_MIPMAP_LEVEL:
1627 *value = 0;
1628 break;
1629 case EGL_RENDER_BUFFER:
1630 // TODO: return the real RENDER_BUFFER here
1631 *value = EGL_BACK_BUFFER;
1632 break;
1633 case EGL_HORIZONTAL_RESOLUTION:
1634 // pixel/mm * EGL_DISPLAY_SCALING
1635 *value = surface->getHorizontalResolution();
1636 break;
1637 case EGL_VERTICAL_RESOLUTION:
1638 // pixel/mm * EGL_DISPLAY_SCALING
1639 *value = surface->getVerticalResolution();
1640 break;
1641 case EGL_PIXEL_ASPECT_RATIO: {
1642 // w/h * EGL_DISPLAY_SCALING
1643 int wr = surface->getHorizontalResolution();
1644 int hr = surface->getVerticalResolution();
1645 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1646 } break;
1647 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001648 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 break;
1650 default:
1651 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1652 }
1653 return ret;
1654}
1655
1656EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1657 EGLContext share_list, const EGLint *attrib_list)
1658{
1659 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1660 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1661
1662 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1663 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1664
1665 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1666 c->flags = egl_context_t::NEVER_CURRENT;
1667 c->dpy = dpy;
1668 c->config = config;
1669 c->read = 0;
1670 c->draw = 0;
1671 return (EGLContext)gl;
1672}
1673
1674EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1675{
1676 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1677 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1678 egl_context_t* c = egl_context_t::context(ctx);
1679 if (c->flags & egl_context_t::IS_CURRENT)
1680 setGlThreadSpecific(0);
1681 ogles_uninit((ogles_context_t*)ctx);
1682 return EGL_TRUE;
1683}
1684
1685EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1686 EGLSurface read, EGLContext ctx)
1687{
1688 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1689 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1690 if (draw) {
1691 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001692 if (!s->isValid())
1693 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 if (s->dpy != dpy)
1695 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001696 // TODO: check that draw is compatible with the context
1697 }
1698 if (read && read!=draw) {
1699 egl_surface_t* s = (egl_surface_t*)read;
1700 if (!s->isValid())
1701 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1702 if (s->dpy != dpy)
1703 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1704 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 }
1706
1707 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1710 return setError(EGL_BAD_MATCH, EGL_FALSE);
1711
1712 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1713 return setError(EGL_BAD_MATCH, EGL_FALSE);
1714
1715 if (ctx == EGL_NO_CONTEXT) {
1716 // if we're detaching, we need the current context
1717 current_ctx = (EGLContext)getGlThreadSpecific();
1718 } else {
1719 egl_context_t* c = egl_context_t::context(ctx);
1720 egl_surface_t* d = (egl_surface_t*)draw;
1721 egl_surface_t* r = (egl_surface_t*)read;
1722 if ((d && d->ctx && d->ctx != ctx) ||
1723 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001724 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1726 }
1727 }
1728
1729 ogles_context_t* gl = (ogles_context_t*)ctx;
1730 if (makeCurrent(gl) == 0) {
1731 if (ctx) {
1732 egl_context_t* c = egl_context_t::context(ctx);
1733 egl_surface_t* d = (egl_surface_t*)draw;
1734 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001735
1736 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001737 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1738 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001739 }
1740 if (c->read) {
1741 // FIXME: unlock/disconnect the read surface too
1742 }
1743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001745 c->read = read;
1746
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 if (c->flags & egl_context_t::NEVER_CURRENT) {
1748 c->flags &= ~egl_context_t::NEVER_CURRENT;
1749 GLint w = 0;
1750 GLint h = 0;
1751 if (draw) {
1752 w = d->getWidth();
1753 h = d->getHeight();
1754 }
1755 ogles_surfaceport(gl, 0, 0);
1756 ogles_viewport(gl, 0, 0, w, h);
1757 ogles_scissor(gl, 0, 0, w, h);
1758 }
1759 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001760 if (d->connect() == EGL_FALSE) {
1761 return EGL_FALSE;
1762 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 d->ctx = ctx;
1764 d->bindDrawSurface(gl);
1765 }
1766 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001767 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768 r->ctx = ctx;
1769 r->bindReadSurface(gl);
1770 }
1771 } else {
1772 // if surfaces were bound to the context bound to this thread
1773 // mark then as unbound.
1774 if (current_ctx) {
1775 egl_context_t* c = egl_context_t::context(current_ctx);
1776 egl_surface_t* d = (egl_surface_t*)c->draw;
1777 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001778 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001779 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001780 d->ctx = EGL_NO_CONTEXT;
1781 d->disconnect();
1782 }
1783 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001784 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001785 r->ctx = EGL_NO_CONTEXT;
1786 // FIXME: unlock/disconnect the read surface too
1787 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 }
1789 }
1790 return EGL_TRUE;
1791 }
1792 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1793}
1794
1795EGLContext eglGetCurrentContext(void)
1796{
1797 // eglGetCurrentContext returns the current EGL rendering context,
1798 // as specified by eglMakeCurrent. If no context is current,
1799 // EGL_NO_CONTEXT is returned.
1800 return (EGLContext)getGlThreadSpecific();
1801}
1802
1803EGLSurface eglGetCurrentSurface(EGLint readdraw)
1804{
1805 // eglGetCurrentSurface returns the read or draw surface attached
1806 // to the current EGL rendering context, as specified by eglMakeCurrent.
1807 // If no context is current, EGL_NO_SURFACE is returned.
1808 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1809 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1810 egl_context_t* c = egl_context_t::context(ctx);
1811 if (readdraw == EGL_READ) {
1812 return c->read;
1813 } else if (readdraw == EGL_DRAW) {
1814 return c->draw;
1815 }
1816 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1817}
1818
1819EGLDisplay eglGetCurrentDisplay(void)
1820{
1821 // eglGetCurrentDisplay returns the current EGL display connection
1822 // for the current EGL rendering context, as specified by eglMakeCurrent.
1823 // If no context is current, EGL_NO_DISPLAY is returned.
1824 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1825 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1826 egl_context_t* c = egl_context_t::context(ctx);
1827 return c->dpy;
1828}
1829
1830EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1831 EGLint attribute, EGLint *value)
1832{
1833 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1834 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1835 egl_context_t* c = egl_context_t::context(ctx);
1836 switch (attribute) {
1837 case EGL_CONFIG_ID:
1838 // Returns the ID of the EGL frame buffer configuration with
1839 // respect to which the context was created
1840 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1841 }
1842 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1843}
1844
1845EGLBoolean eglWaitGL(void)
1846{
1847 return EGL_TRUE;
1848}
1849
1850EGLBoolean eglWaitNative(EGLint engine)
1851{
1852 return EGL_TRUE;
1853}
1854
1855EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1856{
1857 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1858 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001859
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001861 if (!d->isValid())
1862 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 if (d->dpy != dpy)
1864 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1865
1866 // post the surface
1867 d->swapBuffers();
1868
1869 // if it's bound to a context, update the buffer
1870 if (d->ctx != EGL_NO_CONTEXT) {
1871 d->bindDrawSurface((ogles_context_t*)d->ctx);
1872 // if this surface is also the read surface of the context
1873 // it is bound to, make sure to update the read buffer as well.
1874 // The EGL spec is a little unclear about this.
1875 egl_context_t* c = egl_context_t::context(d->ctx);
1876 if (c->read == draw) {
1877 d->bindReadSurface((ogles_context_t*)d->ctx);
1878 }
1879 }
1880
1881 return EGL_TRUE;
1882}
1883
1884EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1885 NativePixmapType target)
1886{
1887 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1888 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1889 // TODO: eglCopyBuffers()
1890 return EGL_FALSE;
1891}
1892
1893EGLint eglGetError(void)
1894{
1895 return getError();
1896}
1897
1898const char* eglQueryString(EGLDisplay dpy, EGLint name)
1899{
1900 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1901 return setError(EGL_BAD_DISPLAY, (const char*)0);
1902
1903 switch (name) {
1904 case EGL_VENDOR:
1905 return gVendorString;
1906 case EGL_VERSION:
1907 return gVersionString;
1908 case EGL_EXTENSIONS:
1909 return gExtensionsString;
1910 case EGL_CLIENT_APIS:
1911 return gClientApiString;
1912 }
1913 return setError(EGL_BAD_PARAMETER, (const char *)0);
1914}
1915
1916// ----------------------------------------------------------------------------
1917// EGL 1.1
1918// ----------------------------------------------------------------------------
1919
1920EGLBoolean eglSurfaceAttrib(
1921 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1922{
1923 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1924 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1925 // TODO: eglSurfaceAttrib()
1926 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1927}
1928
1929EGLBoolean eglBindTexImage(
1930 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1931{
1932 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1933 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1934 // TODO: eglBindTexImage()
1935 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1936}
1937
1938EGLBoolean eglReleaseTexImage(
1939 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1940{
1941 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1942 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1943 // TODO: eglReleaseTexImage()
1944 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1945}
1946
1947EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1948{
1949 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1950 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1951 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001952 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953}
1954
1955// ----------------------------------------------------------------------------
1956// EGL 1.2
1957// ----------------------------------------------------------------------------
1958
1959EGLBoolean eglBindAPI(EGLenum api)
1960{
1961 if (api != EGL_OPENGL_ES_API)
1962 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1963 return EGL_TRUE;
1964}
1965
1966EGLenum eglQueryAPI(void)
1967{
1968 return EGL_OPENGL_ES_API;
1969}
1970
1971EGLBoolean eglWaitClient(void)
1972{
1973 glFinish();
1974 return EGL_TRUE;
1975}
1976
1977EGLBoolean eglReleaseThread(void)
1978{
1979 // TODO: eglReleaseThread()
1980 return EGL_TRUE;
1981}
1982
1983EGLSurface eglCreatePbufferFromClientBuffer(
1984 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1985 EGLConfig config, const EGLint *attrib_list)
1986{
1987 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1988 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1989 // TODO: eglCreatePbufferFromClientBuffer()
1990 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1991}
1992
1993// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001994// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995// ----------------------------------------------------------------------------
1996
1997void (*eglGetProcAddress (const char *procname))()
1998{
1999 extention_map_t const * const map = gExtentionMap;
2000 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2001 if (!strcmp(procname, map[i].name)) {
2002 return map[i].address;
2003 }
2004 }
2005 return NULL;
2006}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002007
2008EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2009 const EGLint *attrib_list)
2010{
2011 EGLBoolean result = EGL_FALSE;
2012 return result;
2013}
2014
2015EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2016{
2017 EGLBoolean result = EGL_FALSE;
2018 return result;
2019}
2020
2021EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2022 EGLClientBuffer buffer, const EGLint *attrib_list)
2023{
2024 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2025 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2026 }
2027 if (ctx != EGL_NO_CONTEXT) {
2028 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2029 }
2030 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2031 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2032 }
2033
Iliyan Malchev697526b2011-05-01 11:33:26 -07002034 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002035
2036 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2037 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2038
Iliyan Malchev697526b2011-05-01 11:33:26 -07002039 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002040 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002041
2042 switch (native_buffer->format) {
2043 case HAL_PIXEL_FORMAT_RGBA_8888:
2044 case HAL_PIXEL_FORMAT_RGBX_8888:
2045 case HAL_PIXEL_FORMAT_RGB_888:
2046 case HAL_PIXEL_FORMAT_RGB_565:
2047 case HAL_PIXEL_FORMAT_BGRA_8888:
2048 case HAL_PIXEL_FORMAT_RGBA_5551:
2049 case HAL_PIXEL_FORMAT_RGBA_4444:
2050 break;
2051 default:
2052 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2053 }
2054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002055 native_buffer->common.incRef(&native_buffer->common);
2056 return (EGLImageKHR)native_buffer;
2057}
2058
2059EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2060{
2061 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2062 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2063 }
2064
Iliyan Malchev697526b2011-05-01 11:33:26 -07002065 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002066
2067 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2068 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2069
Iliyan Malchev697526b2011-05-01 11:33:26 -07002070 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002071 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2072
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002073 native_buffer->common.decRef(&native_buffer->common);
2074
2075 return EGL_TRUE;
2076}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002077
2078// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002079// EGL_KHR_fence_sync
2080// ----------------------------------------------------------------------------
2081
2082#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2083
2084EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2085 const EGLint *attrib_list)
2086{
2087 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2088 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2089 }
2090
2091 if (type != EGL_SYNC_FENCE_KHR ||
2092 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2093 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2094 }
2095
2096 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2097 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2098 }
2099
2100 // AGL is synchronous; nothing to do here.
2101
2102 return FENCE_SYNC_HANDLE;
2103}
2104
2105EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2106{
2107 if (sync != FENCE_SYNC_HANDLE) {
2108 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2109 }
2110
2111 return EGL_TRUE;
2112}
2113
2114EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags,
2115 EGLTimeKHR timeout)
2116{
2117 if (sync != FENCE_SYNC_HANDLE) {
2118 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2119 }
2120
2121 return EGL_CONDITION_SATISFIED_KHR;
2122}
2123
2124EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
2125 EGLint attribute, EGLint *value)
2126{
2127 if (sync != FENCE_SYNC_HANDLE) {
2128 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2129 }
2130
2131 switch (attribute) {
2132 case EGL_SYNC_TYPE_KHR:
2133 *value = EGL_SYNC_FENCE_KHR;
2134 return EGL_TRUE;
2135 case EGL_SYNC_STATUS_KHR:
2136 *value = EGL_SIGNALED_KHR;
2137 return EGL_TRUE;
2138 case EGL_SYNC_CONDITION_KHR:
2139 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2140 return EGL_TRUE;
2141 default:
2142 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2143 }
2144}
2145
2146// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002147// ANDROID extensions
2148// ----------------------------------------------------------------------------
2149
2150EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2151 EGLint left, EGLint top, EGLint width, EGLint height)
2152{
2153 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2154 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2155
2156 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002157 if (!d->isValid())
2158 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002159 if (d->dpy != dpy)
2160 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2161
2162 // post the surface
2163 d->setSwapRectangle(left, top, width, height);
2164
2165 return EGL_TRUE;
2166}