blob: 40fa1481494f0e9289d505983c99c244eb91468c [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>
33
34#include <EGL/egl.h>
35#include <EGL/eglext.h>
36#include <GLES/gl.h>
37#include <GLES/glext.h>
38
39#include <pixelflinger/format.h>
40#include <pixelflinger/pixelflinger.h>
41
Mathias Agopian58a79f42009-05-05 18:21:32 -070042#include <private/ui/android_natives_priv.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#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
52// ----------------------------------------------------------------------------
53namespace android {
54// ----------------------------------------------------------------------------
55
56const unsigned int NUM_DISPLAYS = 1;
57
58static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
59static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
60static pthread_key_t gEGLErrorKey = -1;
61#ifndef HAVE_ANDROID_OS
62namespace gl {
63pthread_key_t gGLKey = -1;
64}; // namespace gl
65#endif
66
67template<typename T>
68static T setError(GLint error, T returnValue) {
69 if (ggl_unlikely(gEGLErrorKey == -1)) {
70 pthread_mutex_lock(&gErrorKeyMutex);
71 if (gEGLErrorKey == -1)
72 pthread_key_create(&gEGLErrorKey, NULL);
73 pthread_mutex_unlock(&gErrorKeyMutex);
74 }
75 pthread_setspecific(gEGLErrorKey, (void*)error);
76 return returnValue;
77}
78
79static GLint getError() {
80 if (ggl_unlikely(gEGLErrorKey == -1))
81 return EGL_SUCCESS;
82 GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -080083 if (error == 0) {
84 // The TLS key has been created by another thread, but the value for
85 // this thread has not been initialized.
86 return EGL_SUCCESS;
87 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
89 return error;
90}
91
92// ----------------------------------------------------------------------------
93
94struct egl_display_t
95{
96 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 static EGLBoolean is_valid(EGLDisplay dpy) {
101 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
102 }
103
104 NativeDisplayType type;
105 volatile int32_t initialized;
106};
107
108static egl_display_t gDisplays[NUM_DISPLAYS];
109
110egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
111 return gDisplays[uintptr_t(dpy)-1U];
112}
113
114struct egl_context_t {
115 enum {
116 IS_CURRENT = 0x00010000,
117 NEVER_CURRENT = 0x00020000
118 };
119 uint32_t flags;
120 EGLDisplay dpy;
121 EGLConfig config;
122 EGLSurface read;
123 EGLSurface draw;
124
125 static inline egl_context_t* context(EGLContext ctx) {
126 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
127 return static_cast<egl_context_t*>(gl->rasterizer.base);
128 }
129};
130
131// ----------------------------------------------------------------------------
132
133struct egl_surface_t
134{
135 enum {
136 PAGE_FLIP = 0x00000001,
137 MAGIC = 0x31415265
138 };
139
140 uint32_t magic;
141 EGLDisplay dpy;
142 EGLConfig config;
143 EGLContext ctx;
144
145 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
146 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700147 bool isValid() const;
148 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
151 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700152 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700153 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 virtual EGLint getWidth() const = 0;
155 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156
157 virtual EGLint getHorizontalResolution() const;
158 virtual EGLint getVerticalResolution() const;
159 virtual EGLint getRefreshRate() const;
160 virtual EGLint getSwapBehavior() const;
161 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700162 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163protected:
164 GGLSurface depth;
165};
166
167egl_surface_t::egl_surface_t(EGLDisplay dpy,
168 EGLConfig config,
169 int32_t depthFormat)
170 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
171{
172 depth.version = sizeof(GGLSurface);
173 depth.data = 0;
174 depth.format = depthFormat;
175}
176egl_surface_t::~egl_surface_t()
177{
178 magic = 0;
179 free(depth.data);
180}
Mathias Agopian0696a572009-08-20 00:12:56 -0700181bool egl_surface_t::isValid() const {
182 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
183 return magic == MAGIC;
184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186EGLBoolean egl_surface_t::swapBuffers() {
187 return EGL_FALSE;
188}
189EGLint egl_surface_t::getHorizontalResolution() const {
190 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
191}
192EGLint egl_surface_t::getVerticalResolution() const {
193 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
194}
195EGLint egl_surface_t::getRefreshRate() const {
196 return (60 * EGL_DISPLAY_SCALING);
197}
198EGLint egl_surface_t::getSwapBehavior() const {
199 return EGL_BUFFER_PRESERVED;
200}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700201EGLBoolean egl_surface_t::setSwapRectangle(
202 EGLint l, EGLint t, EGLint w, EGLint h)
203{
204 return EGL_FALSE;
205}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206
207// ----------------------------------------------------------------------------
208
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 EGLDisplay dpy, EGLConfig config,
213 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700214 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215
Mathias Agopian0696a572009-08-20 00:12:56 -0700216 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217
Mathias Agopian0696a572009-08-20 00:12:56 -0700218 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 virtual EGLBoolean swapBuffers();
220 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
221 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700222 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700223 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700224 virtual EGLint getWidth() const { return width; }
225 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 virtual EGLint getHorizontalResolution() const;
227 virtual EGLint getVerticalResolution() const;
228 virtual EGLint getRefreshRate() const;
229 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700230 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232private:
Mathias Agopiane71212b2009-05-05 00:37:46 -0700233 status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700234 status_t unlock(android_native_buffer_t* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700235 ANativeWindow* nativeWindow;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236 android_native_buffer_t* buffer;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700237 android_native_buffer_t* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700238 gralloc_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 int width;
240 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700241 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700242 GGLFormat const* pixelFormatTable;
243
244 struct Rect {
245 inline Rect() { };
246 inline Rect(int32_t w, int32_t h)
247 : left(0), top(0), right(w), bottom(h) { }
248 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
249 : left(l), top(t), right(r), bottom(b) { }
250 Rect& andSelf(const Rect& r) {
251 left = max(left, r.left);
252 top = max(top, r.top);
253 right = min(right, r.right);
254 bottom = min(bottom, r.bottom);
255 return *this;
256 }
257 bool isEmpty() const {
258 return (left>=right || top>=bottom);
259 }
260 void dump(char const* what) {
261 LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
262 what, left, top, right-left, bottom-top);
263 }
264
265 int32_t left;
266 int32_t top;
267 int32_t right;
268 int32_t bottom;
269 };
270
271 struct Region {
272 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700273 typedef Rect const* const_iterator;
274 const_iterator begin() const { return storage; }
275 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700276 static Region subtract(const Rect& lhs, const Rect& rhs) {
277 Region reg;
278 Rect* storage = reg.storage;
279 if (!lhs.isEmpty()) {
280 if (lhs.top < rhs.top) { // top rect
281 storage->left = lhs.left;
282 storage->top = lhs.top;
283 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700284 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700285 storage++;
286 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700287 const int32_t top = max(lhs.top, rhs.top);
288 const int32_t bot = min(lhs.bottom, rhs.bottom);
289 if (top < bot) {
290 if (lhs.left < rhs.left) { // left-side rect
291 storage->left = lhs.left;
292 storage->top = top;
293 storage->right = rhs.left;
294 storage->bottom = bot;
295 storage++;
296 }
297 if (lhs.right > rhs.right) { // right-side rect
298 storage->left = rhs.right;
299 storage->top = top;
300 storage->right = lhs.right;
301 storage->bottom = bot;
302 storage++;
303 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700304 }
305 if (lhs.bottom > rhs.bottom) { // bottom rect
306 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700307 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700308 storage->right = lhs.right;
309 storage->bottom = lhs.bottom;
310 storage++;
311 }
312 reg.count = storage - reg.storage;
313 }
314 return reg;
315 }
316 bool isEmpty() const {
317 return count<=0;
318 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700319 private:
320 Rect storage[4];
321 ssize_t count;
322 };
323
324 void copyBlt(
325 android_native_buffer_t* dst, void* dst_vaddr,
326 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700327 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700328
329 Rect dirtyRegion;
330 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331};
332
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700333egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 EGLConfig config,
335 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700336 ANativeWindow* window)
Mathias Agopian0926f502009-05-04 14:17:04 -0700337 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700338 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700339 bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700341 pixelFormatTable = gglGetPixelFormatTable();
342
343 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700344 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700345 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
346 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700347}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700348
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700349egl_window_surface_v2_t::~egl_window_surface_v2_t() {
350 if (buffer) {
351 buffer->common.decRef(&buffer->common);
352 }
353 if (previousBuffer) {
354 previousBuffer->common.decRef(&previousBuffer->common);
355 }
356 nativeWindow->common.decRef(&nativeWindow->common);
357}
358
Mathias Agopiancf81c842009-07-31 14:47:00 -0700359EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700360{
Mathias Agopian52212712009-08-11 22:34:02 -0700361 // we're intending to do software rendering
362 native_window_set_usage(nativeWindow,
363 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
364
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700365 // dequeue a buffer
Mathias Agopiancf81c842009-07-31 14:47:00 -0700366 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
367 return setError(EGL_BAD_ALLOC, EGL_FALSE);
368 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700369
370 // allocate a corresponding depth-buffer
371 width = buffer->width;
372 height = buffer->height;
373 if (depth.format) {
374 depth.width = width;
375 depth.height = height;
376 depth.stride = depth.width; // use the width here
377 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
378 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700379 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700380 }
381 }
382
383 // keep a reference on the buffer
384 buffer->common.incRef(&buffer->common);
385
Mathias Agopian0926f502009-05-04 14:17:04 -0700386 // Lock the buffer
Mathias Agopiane71212b2009-05-05 00:37:46 -0700387 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700388 // pin the buffer down
389 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
390 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700391 LOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700392 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700393 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700394 // FIXME: we should make sure we're not accessing the buffer anymore
395 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700396 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700397}
398
399void egl_window_surface_v2_t::disconnect()
400{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700401 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700402 bits = NULL;
403 unlock(buffer);
404 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700405 // enqueue the last frame
406 nativeWindow->queueBuffer(nativeWindow, buffer);
407 if (buffer) {
408 buffer->common.decRef(&buffer->common);
409 buffer = 0;
410 }
411 if (previousBuffer) {
412 previousBuffer->common.decRef(&previousBuffer->common);
413 previousBuffer = 0;
414 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415}
416
Mathias Agopian0926f502009-05-04 14:17:04 -0700417status_t egl_window_surface_v2_t::lock(
Mathias Agopiane71212b2009-05-05 00:37:46 -0700418 android_native_buffer_t* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700419{
Mathias Agopianbc492912009-11-03 20:38:08 -0800420 int err;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800421
422 err = module->lock(module, buf->handle,
423 usage, 0, 0, buf->width, buf->height, vaddr);
424
Mathias Agopian0926f502009-05-04 14:17:04 -0700425 return err;
426}
427
428status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
429{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700430 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800431 int err = NO_ERROR;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800432
433 err = module->unlock(module, buf->handle);
434
Mathias Agopian0926f502009-05-04 14:17:04 -0700435 return err;
436}
437
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700438void egl_window_surface_v2_t::copyBlt(
439 android_native_buffer_t* dst, void* dst_vaddr,
440 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700441 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700442{
443 // FIXME: use copybit if possible
444 // NOTE: dst and src must be the same format
445
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700446 Region::const_iterator cur = clip.begin();
447 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700448
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700449 const size_t bpp = pixelFormatTable[src->format].size;
450 const size_t dbpr = dst->stride * bpp;
451 const size_t sbpr = src->stride * bpp;
452
453 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
454 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
455
456 while (cur != end) {
457 const Rect& r(*cur++);
458 ssize_t w = r.right - r.left;
459 ssize_t h = r.bottom - r.top;
460 if (w <= 0 || h<=0) continue;
461 size_t size = w * bpp;
462 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
463 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
464 if (dbpr==sbpr && size==sbpr) {
465 size *= h;
466 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700467 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700468 do {
469 memcpy(d, s, size);
470 d += dbpr;
471 s += sbpr;
472 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700473 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700474}
475
476EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700478 if (!buffer) {
479 return setError(EGL_BAD_ACCESS, EGL_FALSE);
480 }
481
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700482 /*
483 * Handle eglSetSwapRectangleANDROID()
484 * We copyback from the front buffer
485 */
486 if (!dirtyRegion.isEmpty()) {
487 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
488 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100489 // This was const Region copyBack, but that causes an
490 // internal compile error on simulator builds
491 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700492 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700493 void* prevBits;
494 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700495 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
496 // copy from previousBuffer to buffer
497 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700498 unlock(previousBuffer);
499 }
500 }
501 }
502 oldDirtyRegion = dirtyRegion;
503 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700505 if (previousBuffer) {
506 previousBuffer->common.decRef(&previousBuffer->common);
507 previousBuffer = 0;
508 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700509
Mathias Agopian0926f502009-05-04 14:17:04 -0700510 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700511 previousBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700512 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700513 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700514
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700515 // dequeue a new buffer
Mathias Agopian031213e2010-08-18 16:07:34 -0700516 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
517
518 // TODO: lockBuffer should rather be executed when the very first
519 // direct rendering occurs.
520 nativeWindow->lockBuffer(nativeWindow, buffer);
521
522 // reallocate the depth-buffer if needed
523 if ((width != buffer->width) || (height != buffer->height)) {
524 // TODO: we probably should reset the swap rect here
525 // if the window size has changed
526 width = buffer->width;
527 height = buffer->height;
528 if (depth.data) {
529 free(depth.data);
530 depth.width = width;
531 depth.height = height;
532 depth.stride = buffer->stride;
533 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
534 if (depth.data == 0) {
535 setError(EGL_BAD_ALLOC, EGL_FALSE);
536 return EGL_FALSE;
537 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
539 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700540
Mathias Agopian031213e2010-08-18 16:07:34 -0700541 // keep a reference on the buffer
542 buffer->common.incRef(&buffer->common);
543
544 // finally pin the buffer down
545 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
546 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
547 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
548 buffer, buffer->width, buffer->height);
549 return setError(EGL_BAD_ACCESS, EGL_FALSE);
550 // FIXME: we should make sure we're not accessing the buffer anymore
551 }
552 } else {
553 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700554 }
555
556 return EGL_TRUE;
557}
558
559EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
560 EGLint l, EGLint t, EGLint w, EGLint h)
561{
562 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 return EGL_TRUE;
564}
565
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700566EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567{
568 GGLSurface buffer;
569 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 buffer.width = this->buffer->width;
571 buffer.height = this->buffer->height;
572 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700573 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 gl->rasterizer.procs.colorBuffer(gl, &buffer);
576 if (depth.data != gl->rasterizer.state.buffers.depth.data)
577 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 return EGL_TRUE;
580}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582{
583 GGLSurface buffer;
584 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 buffer.width = this->buffer->width;
586 buffer.height = this->buffer->height;
587 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700588 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 gl->rasterizer.procs.readBuffer(gl, &buffer);
591 return EGL_TRUE;
592}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
595}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
598}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599EGLint egl_window_surface_v2_t::getRefreshRate() const {
600 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700602EGLint egl_window_surface_v2_t::getSwapBehavior() const
603{
604 /*
605 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
606 * the content of the swapped buffer.
607 *
608 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
609 *
610 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
611 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
612 * is, everything outside of this area is preserved.
613 *
614 * This implementation of EGL assumes the later case.
615 *
616 */
617
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619}
620
621// ----------------------------------------------------------------------------
622
623struct egl_pixmap_surface_t : public egl_surface_t
624{
625 egl_pixmap_surface_t(
626 EGLDisplay dpy, EGLConfig config,
627 int32_t depthFormat,
628 egl_native_pixmap_t const * pixmap);
629
630 virtual ~egl_pixmap_surface_t() { }
631
Mathias Agopian0696a572009-08-20 00:12:56 -0700632 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
634 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
635 virtual EGLint getWidth() const { return nativePixmap.width; }
636 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637private:
638 egl_native_pixmap_t nativePixmap;
639};
640
641egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
642 EGLConfig config,
643 int32_t depthFormat,
644 egl_native_pixmap_t const * pixmap)
645 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
646{
647 if (depthFormat) {
648 depth.width = pixmap->width;
649 depth.height = pixmap->height;
650 depth.stride = depth.width; // use the width here
651 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
652 if (depth.data == 0) {
653 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 }
655 }
656}
657EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
658{
659 GGLSurface buffer;
660 buffer.version = sizeof(GGLSurface);
661 buffer.width = nativePixmap.width;
662 buffer.height = nativePixmap.height;
663 buffer.stride = nativePixmap.stride;
664 buffer.data = nativePixmap.data;
665 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 gl->rasterizer.procs.colorBuffer(gl, &buffer);
668 if (depth.data != gl->rasterizer.state.buffers.depth.data)
669 gl->rasterizer.procs.depthBuffer(gl, &depth);
670 return EGL_TRUE;
671}
672EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
673{
674 GGLSurface buffer;
675 buffer.version = sizeof(GGLSurface);
676 buffer.width = nativePixmap.width;
677 buffer.height = nativePixmap.height;
678 buffer.stride = nativePixmap.stride;
679 buffer.data = nativePixmap.data;
680 buffer.format = nativePixmap.format;
681 gl->rasterizer.procs.readBuffer(gl, &buffer);
682 return EGL_TRUE;
683}
684
685// ----------------------------------------------------------------------------
686
687struct egl_pbuffer_surface_t : public egl_surface_t
688{
689 egl_pbuffer_surface_t(
690 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
691 int32_t w, int32_t h, int32_t f);
692
693 virtual ~egl_pbuffer_surface_t();
694
Mathias Agopian0696a572009-08-20 00:12:56 -0700695 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
697 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
698 virtual EGLint getWidth() const { return pbuffer.width; }
699 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700private:
701 GGLSurface pbuffer;
702};
703
704egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
705 EGLConfig config, int32_t depthFormat,
706 int32_t w, int32_t h, int32_t f)
707 : egl_surface_t(dpy, config, depthFormat)
708{
709 size_t size = w*h;
710 switch (f) {
711 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
712 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
713 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800714 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 default:
716 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
717 pbuffer.data = 0;
718 break;
719 }
720 pbuffer.version = sizeof(GGLSurface);
721 pbuffer.width = w;
722 pbuffer.height = h;
723 pbuffer.stride = w;
724 pbuffer.data = (GGLubyte*)malloc(size);
725 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 if (depthFormat) {
728 depth.width = pbuffer.width;
729 depth.height = pbuffer.height;
730 depth.stride = depth.width; // use the width here
731 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
732 if (depth.data == 0) {
733 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
734 return;
735 }
736 }
737}
738egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
739 free(pbuffer.data);
740}
741EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
742{
743 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
744 if (depth.data != gl->rasterizer.state.buffers.depth.data)
745 gl->rasterizer.procs.depthBuffer(gl, &depth);
746 return EGL_TRUE;
747}
748EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
749{
750 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
751 return EGL_TRUE;
752}
753
754// ----------------------------------------------------------------------------
755
756struct config_pair_t {
757 GLint key;
758 GLint value;
759};
760
761struct configs_t {
762 const config_pair_t* array;
763 int size;
764};
765
766struct config_management_t {
767 GLint key;
768 bool (*match)(GLint reqValue, GLint confValue);
769 static bool atLeast(GLint reqValue, GLint confValue) {
770 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
771 }
772 static bool exact(GLint reqValue, GLint confValue) {
773 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
774 }
775 static bool mask(GLint reqValue, GLint confValue) {
776 return (confValue & reqValue) == reqValue;
777 }
Mathias Agopian63971672010-10-25 15:51:24 -0700778 static bool ignore(GLint reqValue, GLint confValue) {
779 return true;
780 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781};
782
783// ----------------------------------------------------------------------------
784
785#define VERSION_MAJOR 1
786#define VERSION_MINOR 2
787static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700788static char const * const gVersionString = "1.2 Android Driver 1.2.0";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790static char const * const gExtensionsString =
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700791 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700792 // "KHR_image_pixmap "
793 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700794 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700795 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796
797// ----------------------------------------------------------------------------
798
799struct extention_map_t {
800 const char * const name;
801 __eglMustCastToProperFunctionPointerType address;
802};
803
804static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700805 { "glDrawTexsOES",
806 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
807 { "glDrawTexiOES",
808 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
809 { "glDrawTexfOES",
810 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
811 { "glDrawTexxOES",
812 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
813 { "glDrawTexsvOES",
814 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
815 { "glDrawTexivOES",
816 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
817 { "glDrawTexfvOES",
818 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
819 { "glDrawTexxvOES",
820 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
821 { "glQueryMatrixxOES",
822 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
823 { "glEGLImageTargetTexture2DOES",
824 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
825 { "glEGLImageTargetRenderbufferStorageOES",
826 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
827 { "glClipPlanef",
828 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
829 { "glClipPlanex",
830 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
831 { "glBindBuffer",
832 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
833 { "glBufferData",
834 (__eglMustCastToProperFunctionPointerType)&glBufferData },
835 { "glBufferSubData",
836 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
837 { "glDeleteBuffers",
838 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
839 { "glGenBuffers",
840 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700841 { "eglCreateImageKHR",
842 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
843 { "eglDestroyImageKHR",
844 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
845 { "eglSetSwapRectangleANDROID",
846 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847};
848
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700849/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 * In the lists below, attributes names MUST be sorted.
851 * Additionally, all configs must be sorted according to
852 * the EGL specification.
853 */
854
855static config_pair_t const config_base_attribute_list[] = {
856 { EGL_STENCIL_SIZE, 0 },
857 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
858 { EGL_LEVEL, 0 },
859 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700860 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
862 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
863 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
864 { EGL_NATIVE_VISUAL_ID, 0 },
865 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
866 { EGL_SAMPLES, 0 },
867 { EGL_SAMPLE_BUFFERS, 0 },
868 { EGL_TRANSPARENT_TYPE, EGL_NONE },
869 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
870 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
871 { EGL_TRANSPARENT_RED_VALUE, 0 },
872 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
873 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
874 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700875 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700876 { EGL_LUMINANCE_SIZE, 0 },
877 { EGL_ALPHA_MASK_SIZE, 0 },
878 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700879 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700880 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881};
882
883// These configs can override the base attribute list
884// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
885
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800886// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887static config_pair_t const config_0_attribute_list[] = {
888 { EGL_BUFFER_SIZE, 16 },
889 { EGL_ALPHA_SIZE, 0 },
890 { EGL_BLUE_SIZE, 5 },
891 { EGL_GREEN_SIZE, 6 },
892 { EGL_RED_SIZE, 5 },
893 { EGL_DEPTH_SIZE, 0 },
894 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700895 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
897};
898
899static config_pair_t const config_1_attribute_list[] = {
900 { EGL_BUFFER_SIZE, 16 },
901 { EGL_ALPHA_SIZE, 0 },
902 { EGL_BLUE_SIZE, 5 },
903 { EGL_GREEN_SIZE, 6 },
904 { EGL_RED_SIZE, 5 },
905 { EGL_DEPTH_SIZE, 16 },
906 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700907 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
909};
910
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800911// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912static config_pair_t const config_2_attribute_list[] = {
913 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800914 { EGL_ALPHA_SIZE, 0 },
915 { EGL_BLUE_SIZE, 8 },
916 { EGL_GREEN_SIZE, 8 },
917 { EGL_RED_SIZE, 8 },
918 { EGL_DEPTH_SIZE, 0 },
919 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700920 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800921 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
922};
923
924static config_pair_t const config_3_attribute_list[] = {
925 { EGL_BUFFER_SIZE, 32 },
926 { EGL_ALPHA_SIZE, 0 },
927 { EGL_BLUE_SIZE, 8 },
928 { EGL_GREEN_SIZE, 8 },
929 { EGL_RED_SIZE, 8 },
930 { EGL_DEPTH_SIZE, 16 },
931 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700932 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800933 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
934};
935
936// 8888 configs
937static config_pair_t const config_4_attribute_list[] = {
938 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 { EGL_ALPHA_SIZE, 8 },
940 { EGL_BLUE_SIZE, 8 },
941 { EGL_GREEN_SIZE, 8 },
942 { EGL_RED_SIZE, 8 },
943 { EGL_DEPTH_SIZE, 0 },
944 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700945 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
947};
948
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800949static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 { EGL_BUFFER_SIZE, 32 },
951 { EGL_ALPHA_SIZE, 8 },
952 { EGL_BLUE_SIZE, 8 },
953 { EGL_GREEN_SIZE, 8 },
954 { EGL_RED_SIZE, 8 },
955 { EGL_DEPTH_SIZE, 16 },
956 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700957 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
959};
960
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800961// A8 configs
962static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 { EGL_BUFFER_SIZE, 8 },
964 { EGL_ALPHA_SIZE, 8 },
965 { EGL_BLUE_SIZE, 0 },
966 { EGL_GREEN_SIZE, 0 },
967 { EGL_RED_SIZE, 0 },
968 { EGL_DEPTH_SIZE, 0 },
969 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700970 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
972};
973
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800974static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 { EGL_BUFFER_SIZE, 8 },
976 { EGL_ALPHA_SIZE, 8 },
977 { EGL_BLUE_SIZE, 0 },
978 { EGL_GREEN_SIZE, 0 },
979 { EGL_RED_SIZE, 0 },
980 { EGL_DEPTH_SIZE, 16 },
981 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700982 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
984};
985
986static configs_t const gConfigs[] = {
987 { config_0_attribute_list, NELEM(config_0_attribute_list) },
988 { config_1_attribute_list, NELEM(config_1_attribute_list) },
989 { config_2_attribute_list, NELEM(config_2_attribute_list) },
990 { config_3_attribute_list, NELEM(config_3_attribute_list) },
991 { config_4_attribute_list, NELEM(config_4_attribute_list) },
992 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800993 { config_6_attribute_list, NELEM(config_6_attribute_list) },
994 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995};
996
997static config_management_t const gConfigManagement[] = {
998 { EGL_BUFFER_SIZE, config_management_t::atLeast },
999 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1000 { EGL_BLUE_SIZE, config_management_t::atLeast },
1001 { EGL_GREEN_SIZE, config_management_t::atLeast },
1002 { EGL_RED_SIZE, config_management_t::atLeast },
1003 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1004 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1005 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1006 { EGL_CONFIG_ID, config_management_t::exact },
1007 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001008 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1009 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1010 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001012 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1014 { EGL_SAMPLES, config_management_t::exact },
1015 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1016 { EGL_SURFACE_TYPE, config_management_t::mask },
1017 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1018 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1019 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1020 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1021 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1022 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1023 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1024 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001025 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1026 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1027 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1028 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1029 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030};
1031
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001034 // attributes that are not specified are simply ignored, if a particular
1035 // one needs not be ignored, it must be specified here, eg:
1036 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037};
1038
1039// ----------------------------------------------------------------------------
1040
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001041static status_t getConfigFormatInfo(EGLint configID,
1042 int32_t& pixelFormat, int32_t& depthFormat)
1043{
1044 switch(configID) {
1045 case 0:
1046 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1047 depthFormat = 0;
1048 break;
1049 case 1:
1050 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1051 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1052 break;
1053 case 2:
1054 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1055 depthFormat = 0;
1056 break;
1057 case 3:
1058 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1059 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1060 break;
1061 case 4:
1062 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1063 depthFormat = 0;
1064 break;
1065 case 5:
1066 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1067 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1068 break;
1069 case 6:
1070 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1071 depthFormat = 0;
1072 break;
1073 case 7:
1074 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1075 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1076 break;
1077 default:
1078 return NAME_NOT_FOUND;
1079 }
1080 return NO_ERROR;
1081}
1082
1083// ----------------------------------------------------------------------------
1084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085template<typename T>
1086static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1087{
1088 while (first <= last) {
1089 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001090 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 last = mid - 1;
1094 } else {
1095 return mid;
1096 }
1097 }
1098 return -1;
1099}
1100
1101static int isAttributeMatching(int i, EGLint attr, EGLint val)
1102{
1103 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001104 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 int index = binarySearch<config_pair_t>(
1106 gConfigs[i].array,
1107 0, gConfigs[i].size-1,
1108 attr);
1109 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001110 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 index = binarySearch<config_pair_t>(
1112 config_base_attribute_list,
1113 0, NELEM(config_base_attribute_list)-1,
1114 attr);
1115 }
1116 if (index >= 0) {
1117 // attribute found, check if this config could match
1118 int cfgMgtIndex = binarySearch<config_management_t>(
1119 gConfigManagement,
1120 0, NELEM(gConfigManagement)-1,
1121 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001122 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 bool match = gConfigManagement[cfgMgtIndex].match(
1124 val, configFound[index].value);
1125 if (match) {
1126 // this config matches
1127 return 1;
1128 }
1129 } else {
1130 // attribute not found. this should NEVER happen.
1131 }
1132 } else {
1133 // error, this attribute doesn't exist
1134 }
1135 return 0;
1136}
1137
1138static int makeCurrent(ogles_context_t* gl)
1139{
1140 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1141 if (gl) {
1142 egl_context_t* c = egl_context_t::context(gl);
1143 if (c->flags & egl_context_t::IS_CURRENT) {
1144 if (current != gl) {
1145 // it is an error to set a context current, if it's already
1146 // current to another thread
1147 return -1;
1148 }
1149 } else {
1150 if (current) {
1151 // mark the current context as not current, and flush
1152 glFlush();
1153 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1154 }
1155 }
1156 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1157 // The context is not current, make it current!
1158 setGlThreadSpecific(gl);
1159 c->flags |= egl_context_t::IS_CURRENT;
1160 }
1161 } else {
1162 if (current) {
1163 // mark the current context as not current, and flush
1164 glFlush();
1165 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1166 }
1167 // this thread has no context attached to it
1168 setGlThreadSpecific(0);
1169 }
1170 return 0;
1171}
1172
1173static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1174 EGLint attribute, EGLint *value)
1175{
1176 size_t numConfigs = NELEM(gConfigs);
1177 int index = (int)config;
1178 if (uint32_t(index) >= numConfigs)
1179 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1180
1181 int attrIndex;
1182 attrIndex = binarySearch<config_pair_t>(
1183 gConfigs[index].array,
1184 0, gConfigs[index].size-1,
1185 attribute);
1186 if (attrIndex>=0) {
1187 *value = gConfigs[index].array[attrIndex].value;
1188 return EGL_TRUE;
1189 }
1190
1191 attrIndex = binarySearch<config_pair_t>(
1192 config_base_attribute_list,
1193 0, NELEM(config_base_attribute_list)-1,
1194 attribute);
1195 if (attrIndex>=0) {
1196 *value = config_base_attribute_list[attrIndex].value;
1197 return EGL_TRUE;
1198 }
1199 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1200}
1201
1202static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1203 NativeWindowType window, const EGLint *attrib_list)
1204{
1205 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1206 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1207 if (window == 0)
1208 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1209
1210 EGLint surfaceType;
1211 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1212 return EGL_FALSE;
1213
1214 if (!(surfaceType & EGL_WINDOW_BIT))
1215 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1216
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001217 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001218 ANDROID_NATIVE_WINDOW_MAGIC) {
1219 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1220 }
1221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 EGLint configID;
1223 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1224 return EGL_FALSE;
1225
1226 int32_t depthFormat;
1227 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001228 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1230 }
1231
1232 // FIXME: we don't have access to the pixelFormat here just yet.
1233 // (it's possible that the surface is not fully initialized)
1234 // maybe this should be done after the page-flip
1235 //if (EGLint(info.format) != pixelFormat)
1236 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1237
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001238 egl_surface_t* surface;
1239 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001240 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241
Mathias Agopian0696a572009-08-20 00:12:56 -07001242 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 // there was a problem in the ctor, the error
1244 // flag has been set.
1245 delete surface;
1246 surface = 0;
1247 }
1248 return surface;
1249}
1250
1251static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1252 NativePixmapType pixmap, const EGLint *attrib_list)
1253{
1254 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1255 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1256 if (pixmap == 0)
1257 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1258
1259 EGLint surfaceType;
1260 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1261 return EGL_FALSE;
1262
1263 if (!(surfaceType & EGL_PIXMAP_BIT))
1264 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1265
Mathias Agopian0696a572009-08-20 00:12:56 -07001266 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1267 sizeof(egl_native_pixmap_t)) {
1268 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1269 }
1270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 EGLint configID;
1272 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1273 return EGL_FALSE;
1274
1275 int32_t depthFormat;
1276 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001277 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1279 }
1280
1281 if (pixmap->format != pixelFormat)
1282 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1283
1284 egl_surface_t* surface =
1285 new egl_pixmap_surface_t(dpy, config, depthFormat,
1286 static_cast<egl_native_pixmap_t*>(pixmap));
1287
Mathias Agopian0696a572009-08-20 00:12:56 -07001288 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 // there was a problem in the ctor, the error
1290 // flag has been set.
1291 delete surface;
1292 surface = 0;
1293 }
1294 return surface;
1295}
1296
1297static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1298 const EGLint *attrib_list)
1299{
1300 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1301 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1302
1303 EGLint surfaceType;
1304 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1305 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 if (!(surfaceType & EGL_PBUFFER_BIT))
1308 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 EGLint configID;
1311 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1312 return EGL_FALSE;
1313
1314 int32_t depthFormat;
1315 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001316 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1318 }
1319
1320 int32_t w = 0;
1321 int32_t h = 0;
1322 while (attrib_list[0]) {
1323 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1324 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1325 attrib_list+=2;
1326 }
1327
1328 egl_surface_t* surface =
1329 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1330
Mathias Agopian0696a572009-08-20 00:12:56 -07001331 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 // there was a problem in the ctor, the error
1333 // flag has been set.
1334 delete surface;
1335 surface = 0;
1336 }
1337 return surface;
1338}
1339
1340// ----------------------------------------------------------------------------
1341}; // namespace android
1342// ----------------------------------------------------------------------------
1343
1344using namespace android;
1345
1346// ----------------------------------------------------------------------------
1347// Initialization
1348// ----------------------------------------------------------------------------
1349
1350EGLDisplay eglGetDisplay(NativeDisplayType display)
1351{
1352#ifndef HAVE_ANDROID_OS
1353 // this just needs to be done once
1354 if (gGLKey == -1) {
1355 pthread_mutex_lock(&gInitMutex);
1356 if (gGLKey == -1)
1357 pthread_key_create(&gGLKey, NULL);
1358 pthread_mutex_unlock(&gInitMutex);
1359 }
1360#endif
1361 if (display == EGL_DEFAULT_DISPLAY) {
1362 EGLDisplay dpy = (EGLDisplay)1;
1363 egl_display_t& d = egl_display_t::get_display(dpy);
1364 d.type = display;
1365 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001366 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 return EGL_NO_DISPLAY;
1368}
1369
1370EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1371{
1372 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1373 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 EGLBoolean res = EGL_TRUE;
1376 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 if (android_atomic_inc(&d.initialized) == 0) {
1379 // initialize stuff here if needed
1380 //pthread_mutex_lock(&gInitMutex);
1381 //pthread_mutex_unlock(&gInitMutex);
1382 }
1383
1384 if (res == EGL_TRUE) {
1385 if (major != NULL) *major = VERSION_MAJOR;
1386 if (minor != NULL) *minor = VERSION_MINOR;
1387 }
1388 return res;
1389}
1390
1391EGLBoolean eglTerminate(EGLDisplay dpy)
1392{
1393 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1394 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1395
1396 EGLBoolean res = EGL_TRUE;
1397 egl_display_t& d = egl_display_t::get_display(dpy);
1398 if (android_atomic_dec(&d.initialized) == 1) {
1399 // TODO: destroy all resources (surfaces, contexts, etc...)
1400 //pthread_mutex_lock(&gInitMutex);
1401 //pthread_mutex_unlock(&gInitMutex);
1402 }
1403 return res;
1404}
1405
1406// ----------------------------------------------------------------------------
1407// configuration
1408// ----------------------------------------------------------------------------
1409
1410EGLBoolean eglGetConfigs( EGLDisplay dpy,
1411 EGLConfig *configs,
1412 EGLint config_size, EGLint *num_config)
1413{
1414 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1415 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1416
1417 GLint numConfigs = NELEM(gConfigs);
1418 if (!configs) {
1419 *num_config = numConfigs;
1420 return EGL_TRUE;
1421 }
1422 GLint i;
1423 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1424 *configs++ = (EGLConfig)i;
1425 }
1426 *num_config = i;
1427 return EGL_TRUE;
1428}
1429
1430EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1431 EGLConfig *configs, EGLint config_size,
1432 EGLint *num_config)
1433{
1434 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1435 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001436
1437 if (ggl_unlikely(num_config==0)) {
1438 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1439 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440
Jack Palevich749c63d2009-03-25 15:12:17 -07001441 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001442 /*
1443 * A NULL attrib_list should be treated as though it was an empty
1444 * one (terminated with EGL_NONE) as defined in
1445 * section 3.4.1 "Querying Configurations" in the EGL specification.
1446 */
1447 static const EGLint dummy = EGL_NONE;
1448 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 int numAttributes = 0;
1452 int numConfigs = NELEM(gConfigs);
1453 uint32_t possibleMatch = (1<<numConfigs)-1;
1454 while(possibleMatch && *attrib_list != EGL_NONE) {
1455 numAttributes++;
1456 EGLint attr = *attrib_list++;
1457 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001458 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 if (!(possibleMatch & (1<<i)))
1460 continue;
1461 if (isAttributeMatching(i, attr, val) == 0) {
1462 possibleMatch &= ~(1<<i);
1463 }
1464 }
1465 }
1466
1467 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001468 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1469 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 // default value
1471 if (binarySearch<config_pair_t>(
1472 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001473 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 config_defaults[j].key) < 0)
1475 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001476 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 if (!(possibleMatch & (1<<i)))
1478 continue;
1479 if (isAttributeMatching(i,
1480 config_defaults[j].key,
1481 config_defaults[j].value) == 0)
1482 {
1483 possibleMatch &= ~(1<<i);
1484 }
1485 }
1486 }
1487 }
1488
1489 // return the configurations found
1490 int n=0;
1491 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001492 if (configs) {
1493 for (int i=0 ; config_size && i<numConfigs ; i++) {
1494 if (possibleMatch & (1<<i)) {
1495 *configs++ = (EGLConfig)i;
1496 config_size--;
1497 n++;
1498 }
1499 }
1500 } else {
1501 for (int i=0 ; i<numConfigs ; i++) {
1502 if (possibleMatch & (1<<i)) {
1503 n++;
1504 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 }
1506 }
1507 }
1508 *num_config = n;
1509 return EGL_TRUE;
1510}
1511
1512EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1513 EGLint attribute, EGLint *value)
1514{
1515 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1516 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1517
1518 return getConfigAttrib(dpy, config, attribute, value);
1519}
1520
1521// ----------------------------------------------------------------------------
1522// surfaces
1523// ----------------------------------------------------------------------------
1524
1525EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1526 NativeWindowType window,
1527 const EGLint *attrib_list)
1528{
1529 return createWindowSurface(dpy, config, window, attrib_list);
1530}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1533 NativePixmapType pixmap,
1534 const EGLint *attrib_list)
1535{
1536 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1537}
1538
1539EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1540 const EGLint *attrib_list)
1541{
1542 return createPbufferSurface(dpy, config, attrib_list);
1543}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1546{
1547 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1548 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1549 if (eglSurface != EGL_NO_SURFACE) {
1550 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001551 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1553 if (surface->dpy != dpy)
1554 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001555 if (surface->ctx) {
1556 // FIXME: this surface is current check what the spec says
1557 surface->disconnect();
1558 surface->ctx = 0;
1559 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560 delete surface;
1561 }
1562 return EGL_TRUE;
1563}
1564
1565EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1566 EGLint attribute, EGLint *value)
1567{
1568 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1569 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1570 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001571 if (!surface->isValid())
1572 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 if (surface->dpy != dpy)
1574 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1575
1576 EGLBoolean ret = EGL_TRUE;
1577 switch (attribute) {
1578 case EGL_CONFIG_ID:
1579 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1580 break;
1581 case EGL_WIDTH:
1582 *value = surface->getWidth();
1583 break;
1584 case EGL_HEIGHT:
1585 *value = surface->getHeight();
1586 break;
1587 case EGL_LARGEST_PBUFFER:
1588 // not modified for a window or pixmap surface
1589 break;
1590 case EGL_TEXTURE_FORMAT:
1591 *value = EGL_NO_TEXTURE;
1592 break;
1593 case EGL_TEXTURE_TARGET:
1594 *value = EGL_NO_TEXTURE;
1595 break;
1596 case EGL_MIPMAP_TEXTURE:
1597 *value = EGL_FALSE;
1598 break;
1599 case EGL_MIPMAP_LEVEL:
1600 *value = 0;
1601 break;
1602 case EGL_RENDER_BUFFER:
1603 // TODO: return the real RENDER_BUFFER here
1604 *value = EGL_BACK_BUFFER;
1605 break;
1606 case EGL_HORIZONTAL_RESOLUTION:
1607 // pixel/mm * EGL_DISPLAY_SCALING
1608 *value = surface->getHorizontalResolution();
1609 break;
1610 case EGL_VERTICAL_RESOLUTION:
1611 // pixel/mm * EGL_DISPLAY_SCALING
1612 *value = surface->getVerticalResolution();
1613 break;
1614 case EGL_PIXEL_ASPECT_RATIO: {
1615 // w/h * EGL_DISPLAY_SCALING
1616 int wr = surface->getHorizontalResolution();
1617 int hr = surface->getVerticalResolution();
1618 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1619 } break;
1620 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001621 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 break;
1623 default:
1624 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1625 }
1626 return ret;
1627}
1628
1629EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1630 EGLContext share_list, const EGLint *attrib_list)
1631{
1632 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1633 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1634
1635 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1636 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1637
1638 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1639 c->flags = egl_context_t::NEVER_CURRENT;
1640 c->dpy = dpy;
1641 c->config = config;
1642 c->read = 0;
1643 c->draw = 0;
1644 return (EGLContext)gl;
1645}
1646
1647EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1648{
1649 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1650 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1651 egl_context_t* c = egl_context_t::context(ctx);
1652 if (c->flags & egl_context_t::IS_CURRENT)
1653 setGlThreadSpecific(0);
1654 ogles_uninit((ogles_context_t*)ctx);
1655 return EGL_TRUE;
1656}
1657
1658EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1659 EGLSurface read, EGLContext ctx)
1660{
1661 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1662 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1663 if (draw) {
1664 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001665 if (!s->isValid())
1666 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 if (s->dpy != dpy)
1668 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001669 // TODO: check that draw is compatible with the context
1670 }
1671 if (read && read!=draw) {
1672 egl_surface_t* s = (egl_surface_t*)read;
1673 if (!s->isValid())
1674 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1675 if (s->dpy != dpy)
1676 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1677 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678 }
1679
1680 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1683 return setError(EGL_BAD_MATCH, EGL_FALSE);
1684
1685 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1686 return setError(EGL_BAD_MATCH, EGL_FALSE);
1687
1688 if (ctx == EGL_NO_CONTEXT) {
1689 // if we're detaching, we need the current context
1690 current_ctx = (EGLContext)getGlThreadSpecific();
1691 } else {
1692 egl_context_t* c = egl_context_t::context(ctx);
1693 egl_surface_t* d = (egl_surface_t*)draw;
1694 egl_surface_t* r = (egl_surface_t*)read;
1695 if ((d && d->ctx && d->ctx != ctx) ||
1696 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001697 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1699 }
1700 }
1701
1702 ogles_context_t* gl = (ogles_context_t*)ctx;
1703 if (makeCurrent(gl) == 0) {
1704 if (ctx) {
1705 egl_context_t* c = egl_context_t::context(ctx);
1706 egl_surface_t* d = (egl_surface_t*)draw;
1707 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001708
1709 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001710 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1711 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001712 }
1713 if (c->read) {
1714 // FIXME: unlock/disconnect the read surface too
1715 }
1716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001718 c->read = read;
1719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 if (c->flags & egl_context_t::NEVER_CURRENT) {
1721 c->flags &= ~egl_context_t::NEVER_CURRENT;
1722 GLint w = 0;
1723 GLint h = 0;
1724 if (draw) {
1725 w = d->getWidth();
1726 h = d->getHeight();
1727 }
1728 ogles_surfaceport(gl, 0, 0);
1729 ogles_viewport(gl, 0, 0, w, h);
1730 ogles_scissor(gl, 0, 0, w, h);
1731 }
1732 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001733 if (d->connect() == EGL_FALSE) {
1734 return EGL_FALSE;
1735 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 d->ctx = ctx;
1737 d->bindDrawSurface(gl);
1738 }
1739 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001740 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 r->ctx = ctx;
1742 r->bindReadSurface(gl);
1743 }
1744 } else {
1745 // if surfaces were bound to the context bound to this thread
1746 // mark then as unbound.
1747 if (current_ctx) {
1748 egl_context_t* c = egl_context_t::context(current_ctx);
1749 egl_surface_t* d = (egl_surface_t*)c->draw;
1750 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001751 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001752 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001753 d->ctx = EGL_NO_CONTEXT;
1754 d->disconnect();
1755 }
1756 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001757 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001758 r->ctx = EGL_NO_CONTEXT;
1759 // FIXME: unlock/disconnect the read surface too
1760 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761 }
1762 }
1763 return EGL_TRUE;
1764 }
1765 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1766}
1767
1768EGLContext eglGetCurrentContext(void)
1769{
1770 // eglGetCurrentContext returns the current EGL rendering context,
1771 // as specified by eglMakeCurrent. If no context is current,
1772 // EGL_NO_CONTEXT is returned.
1773 return (EGLContext)getGlThreadSpecific();
1774}
1775
1776EGLSurface eglGetCurrentSurface(EGLint readdraw)
1777{
1778 // eglGetCurrentSurface returns the read or draw surface attached
1779 // to the current EGL rendering context, as specified by eglMakeCurrent.
1780 // If no context is current, EGL_NO_SURFACE is returned.
1781 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1782 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1783 egl_context_t* c = egl_context_t::context(ctx);
1784 if (readdraw == EGL_READ) {
1785 return c->read;
1786 } else if (readdraw == EGL_DRAW) {
1787 return c->draw;
1788 }
1789 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1790}
1791
1792EGLDisplay eglGetCurrentDisplay(void)
1793{
1794 // eglGetCurrentDisplay returns the current EGL display connection
1795 // for the current EGL rendering context, as specified by eglMakeCurrent.
1796 // If no context is current, EGL_NO_DISPLAY is returned.
1797 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1798 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1799 egl_context_t* c = egl_context_t::context(ctx);
1800 return c->dpy;
1801}
1802
1803EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1804 EGLint attribute, EGLint *value)
1805{
1806 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1807 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1808 egl_context_t* c = egl_context_t::context(ctx);
1809 switch (attribute) {
1810 case EGL_CONFIG_ID:
1811 // Returns the ID of the EGL frame buffer configuration with
1812 // respect to which the context was created
1813 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1814 }
1815 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1816}
1817
1818EGLBoolean eglWaitGL(void)
1819{
1820 return EGL_TRUE;
1821}
1822
1823EGLBoolean eglWaitNative(EGLint engine)
1824{
1825 return EGL_TRUE;
1826}
1827
1828EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1829{
1830 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1831 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001834 if (!d->isValid())
1835 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 if (d->dpy != dpy)
1837 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1838
1839 // post the surface
1840 d->swapBuffers();
1841
1842 // if it's bound to a context, update the buffer
1843 if (d->ctx != EGL_NO_CONTEXT) {
1844 d->bindDrawSurface((ogles_context_t*)d->ctx);
1845 // if this surface is also the read surface of the context
1846 // it is bound to, make sure to update the read buffer as well.
1847 // The EGL spec is a little unclear about this.
1848 egl_context_t* c = egl_context_t::context(d->ctx);
1849 if (c->read == draw) {
1850 d->bindReadSurface((ogles_context_t*)d->ctx);
1851 }
1852 }
1853
1854 return EGL_TRUE;
1855}
1856
1857EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1858 NativePixmapType target)
1859{
1860 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1861 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1862 // TODO: eglCopyBuffers()
1863 return EGL_FALSE;
1864}
1865
1866EGLint eglGetError(void)
1867{
1868 return getError();
1869}
1870
1871const char* eglQueryString(EGLDisplay dpy, EGLint name)
1872{
1873 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1874 return setError(EGL_BAD_DISPLAY, (const char*)0);
1875
1876 switch (name) {
1877 case EGL_VENDOR:
1878 return gVendorString;
1879 case EGL_VERSION:
1880 return gVersionString;
1881 case EGL_EXTENSIONS:
1882 return gExtensionsString;
1883 case EGL_CLIENT_APIS:
1884 return gClientApiString;
1885 }
1886 return setError(EGL_BAD_PARAMETER, (const char *)0);
1887}
1888
1889// ----------------------------------------------------------------------------
1890// EGL 1.1
1891// ----------------------------------------------------------------------------
1892
1893EGLBoolean eglSurfaceAttrib(
1894 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1895{
1896 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1897 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1898 // TODO: eglSurfaceAttrib()
1899 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1900}
1901
1902EGLBoolean eglBindTexImage(
1903 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1904{
1905 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1906 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1907 // TODO: eglBindTexImage()
1908 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1909}
1910
1911EGLBoolean eglReleaseTexImage(
1912 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1913{
1914 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1915 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1916 // TODO: eglReleaseTexImage()
1917 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1918}
1919
1920EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1921{
1922 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1923 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1924 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001925 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926}
1927
1928// ----------------------------------------------------------------------------
1929// EGL 1.2
1930// ----------------------------------------------------------------------------
1931
1932EGLBoolean eglBindAPI(EGLenum api)
1933{
1934 if (api != EGL_OPENGL_ES_API)
1935 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1936 return EGL_TRUE;
1937}
1938
1939EGLenum eglQueryAPI(void)
1940{
1941 return EGL_OPENGL_ES_API;
1942}
1943
1944EGLBoolean eglWaitClient(void)
1945{
1946 glFinish();
1947 return EGL_TRUE;
1948}
1949
1950EGLBoolean eglReleaseThread(void)
1951{
1952 // TODO: eglReleaseThread()
1953 return EGL_TRUE;
1954}
1955
1956EGLSurface eglCreatePbufferFromClientBuffer(
1957 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1958 EGLConfig config, const EGLint *attrib_list)
1959{
1960 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1961 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1962 // TODO: eglCreatePbufferFromClientBuffer()
1963 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1964}
1965
1966// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001967// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968// ----------------------------------------------------------------------------
1969
1970void (*eglGetProcAddress (const char *procname))()
1971{
1972 extention_map_t const * const map = gExtentionMap;
1973 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
1974 if (!strcmp(procname, map[i].name)) {
1975 return map[i].address;
1976 }
1977 }
1978 return NULL;
1979}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001980
1981EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1982 const EGLint *attrib_list)
1983{
1984 EGLBoolean result = EGL_FALSE;
1985 return result;
1986}
1987
1988EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1989{
1990 EGLBoolean result = EGL_FALSE;
1991 return result;
1992}
1993
1994EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1995 EGLClientBuffer buffer, const EGLint *attrib_list)
1996{
1997 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
1998 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1999 }
2000 if (ctx != EGL_NO_CONTEXT) {
2001 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2002 }
2003 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2004 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2005 }
2006
2007 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2008
2009 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2010 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2011
2012 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2013 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002014
2015 switch (native_buffer->format) {
2016 case HAL_PIXEL_FORMAT_RGBA_8888:
2017 case HAL_PIXEL_FORMAT_RGBX_8888:
2018 case HAL_PIXEL_FORMAT_RGB_888:
2019 case HAL_PIXEL_FORMAT_RGB_565:
2020 case HAL_PIXEL_FORMAT_BGRA_8888:
2021 case HAL_PIXEL_FORMAT_RGBA_5551:
2022 case HAL_PIXEL_FORMAT_RGBA_4444:
2023 break;
2024 default:
2025 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2026 }
2027
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002028 native_buffer->common.incRef(&native_buffer->common);
2029 return (EGLImageKHR)native_buffer;
2030}
2031
2032EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2033{
2034 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2035 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2036 }
2037
2038 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2039
2040 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2041 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2042
2043 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2044 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2045
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002046 native_buffer->common.decRef(&native_buffer->common);
2047
2048 return EGL_TRUE;
2049}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002050
2051// ----------------------------------------------------------------------------
2052// ANDROID extensions
2053// ----------------------------------------------------------------------------
2054
2055EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2056 EGLint left, EGLint top, EGLint width, EGLint height)
2057{
2058 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2059 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2060
2061 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002062 if (!d->isValid())
2063 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002064 if (d->dpy != dpy)
2065 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2066
2067 // post the surface
2068 d->setSwapRectangle(left, top, width, height);
2069
2070 return EGL_TRUE;
2071}