blob: 5c91c92bb1dbeec061390422a2e672c09896a18d [file] [log] [blame]
Mathias Agopian1473f462009-04-10 14:24:30 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
3** Copyright 2007 The Android Open Source Project
4**
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -08008**
Mathias Agopian1473f462009-04-10 14:24:30 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Mathias Agopian1473f462009-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 Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
The Android Open Source Project9066cfe2009-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>
Mathias Agopian1473f462009-04-10 14:24:30 -070036#include <EGL/android_natives.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <GLES/gl.h>
38#include <GLES/glext.h>
39
40#include <pixelflinger/format.h>
41#include <pixelflinger/pixelflinger.h>
42
43#include "context.h"
44#include "state.h"
45#include "texture.h"
46#include "matrix.h"
47
Mathias Agopian1473f462009-04-10 14:24:30 -070048#ifdef LIBAGL_USE_GRALLOC_COPYBITS
49#include "gralloc_priv.h"
50#endif // LIBAGL_USE_GRALLOC_COPYBITS
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#undef NELEM
53#define NELEM(x) (sizeof(x)/sizeof(*(x)))
54
55// ----------------------------------------------------------------------------
56namespace android {
57// ----------------------------------------------------------------------------
58
59const unsigned int NUM_DISPLAYS = 1;
60
61static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
62static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
63static pthread_key_t gEGLErrorKey = -1;
64#ifndef HAVE_ANDROID_OS
65namespace gl {
66pthread_key_t gGLKey = -1;
67}; // namespace gl
68#endif
69
70template<typename T>
71static T setError(GLint error, T returnValue) {
72 if (ggl_unlikely(gEGLErrorKey == -1)) {
73 pthread_mutex_lock(&gErrorKeyMutex);
74 if (gEGLErrorKey == -1)
75 pthread_key_create(&gEGLErrorKey, NULL);
76 pthread_mutex_unlock(&gErrorKeyMutex);
77 }
78 pthread_setspecific(gEGLErrorKey, (void*)error);
79 return returnValue;
80}
81
82static GLint getError() {
83 if (ggl_unlikely(gEGLErrorKey == -1))
84 return EGL_SUCCESS;
85 GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
86 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
87 return error;
88}
89
90// ----------------------------------------------------------------------------
91
92struct egl_display_t
93{
94 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian1473f462009-04-10 14:24:30 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -070097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 static EGLBoolean is_valid(EGLDisplay dpy) {
99 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
100 }
101
102 NativeDisplayType type;
103 volatile int32_t initialized;
104};
105
106static egl_display_t gDisplays[NUM_DISPLAYS];
107
108egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
109 return gDisplays[uintptr_t(dpy)-1U];
110}
111
112struct egl_context_t {
113 enum {
114 IS_CURRENT = 0x00010000,
115 NEVER_CURRENT = 0x00020000
116 };
117 uint32_t flags;
118 EGLDisplay dpy;
119 EGLConfig config;
120 EGLSurface read;
121 EGLSurface draw;
122
123 static inline egl_context_t* context(EGLContext ctx) {
124 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
125 return static_cast<egl_context_t*>(gl->rasterizer.base);
126 }
127};
128
129// ----------------------------------------------------------------------------
130
131struct egl_surface_t
132{
133 enum {
134 PAGE_FLIP = 0x00000001,
135 MAGIC = 0x31415265
136 };
137
138 uint32_t magic;
139 EGLDisplay dpy;
140 EGLConfig config;
141 EGLContext ctx;
142
143 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
144 virtual ~egl_surface_t();
145 virtual bool isValid() const = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
148 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
149 virtual EGLint getWidth() const = 0;
150 virtual EGLint getHeight() const = 0;
151 virtual void* getBits() const = 0;
152
153 virtual EGLint getHorizontalResolution() const;
154 virtual EGLint getVerticalResolution() const;
155 virtual EGLint getRefreshRate() const;
156 virtual EGLint getSwapBehavior() const;
157 virtual EGLBoolean swapBuffers();
158protected:
159 GGLSurface depth;
160};
161
162egl_surface_t::egl_surface_t(EGLDisplay dpy,
163 EGLConfig config,
164 int32_t depthFormat)
165 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
166{
167 depth.version = sizeof(GGLSurface);
168 depth.data = 0;
169 depth.format = depthFormat;
170}
171egl_surface_t::~egl_surface_t()
172{
173 magic = 0;
174 free(depth.data);
175}
176EGLBoolean egl_surface_t::swapBuffers() {
177 return EGL_FALSE;
178}
179EGLint egl_surface_t::getHorizontalResolution() const {
180 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
181}
182EGLint egl_surface_t::getVerticalResolution() const {
183 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
184}
185EGLint egl_surface_t::getRefreshRate() const {
186 return (60 * EGL_DISPLAY_SCALING);
187}
188EGLint egl_surface_t::getSwapBehavior() const {
189 return EGL_BUFFER_PRESERVED;
190}
191
192// ----------------------------------------------------------------------------
193
Mathias Agopian1473f462009-04-10 14:24:30 -0700194struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195{
Mathias Agopian1473f462009-04-10 14:24:30 -0700196 egl_window_surface_v2_t(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 EGLDisplay dpy, EGLConfig config,
198 int32_t depthFormat,
Mathias Agopian1473f462009-04-10 14:24:30 -0700199 android_native_window_t* window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Mathias Agopian1473f462009-04-10 14:24:30 -0700201 ~egl_window_surface_v2_t();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
Mathias Agopian1473f462009-04-10 14:24:30 -0700203 virtual bool isValid() const { return nativeWindow->common.magic == ANDROID_NATIVE_WINDOW_MAGIC; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 virtual EGLBoolean swapBuffers();
205 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
206 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopian1473f462009-04-10 14:24:30 -0700207 virtual EGLint getWidth() const { return buffer->width; }
208 virtual EGLint getHeight() const { return buffer->height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 virtual void* getBits() const;
210 virtual EGLint getHorizontalResolution() const;
211 virtual EGLint getVerticalResolution() const;
212 virtual EGLint getRefreshRate() const;
213 virtual EGLint getSwapBehavior() const;
214private:
Mathias Agopian1473f462009-04-10 14:24:30 -0700215 android_native_window_t* nativeWindow;
216 android_native_buffer_t* buffer;
217 int width;
218 int height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219};
220
Mathias Agopian1473f462009-04-10 14:24:30 -0700221egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 EGLConfig config,
223 int32_t depthFormat,
Mathias Agopian1473f462009-04-10 14:24:30 -0700224 android_native_window_t* window)
225 : egl_surface_t(dpy, config, depthFormat), nativeWindow(window), buffer(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226{
Mathias Agopian1473f462009-04-10 14:24:30 -0700227 nativeWindow->common.incRef(&nativeWindow->common);
228
229 nativeWindow->dequeueBuffer(nativeWindow, &buffer);
230
231 width = buffer->width;
232 height = buffer->height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 if (depthFormat) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700234 depth.width = width;
235 depth.height = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 depth.stride = depth.width; // use the width here
237 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
238 if (depth.data == 0) {
239 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
240 return;
241 }
242 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700243
244 // TODO: lockBuffer should rather be executed when the very first
245 // direct rendering occurs.
246 buffer->common.incRef(&buffer->common);
247 nativeWindow->lockBuffer(nativeWindow, buffer);
248
249 // FIXME: we need to gralloc lock the buffer
250 // FIXME: we need to handle the copy-back if needed, but
251 // for now we're a "non preserving" implementation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252}
253
Mathias Agopian1473f462009-04-10 14:24:30 -0700254egl_window_surface_v2_t::~egl_window_surface_v2_t() {
255 if (buffer) {
256 buffer->common.decRef(&buffer->common);
257 }
258 nativeWindow->common.decRef(&nativeWindow->common);
259}
260
261EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262{
Mathias Agopian1473f462009-04-10 14:24:30 -0700263 // TODO: this is roughly the code needed for preserving the back buffer
264 // efficiently. dirty is the area that has been modified.
265 //Region newDirty(dirty);
266 //newDirty.andSelf(Rect(nativeWindow->width, nativeWindow->height));
267 //mDirty = newDirty;
268 //const Region copyback(mDirty.subtract(newDirty));
269 //mDisplaySurface->copyFrontToBack(copyback);
270
271
272 nativeWindow->queueBuffer(nativeWindow, buffer);
273 buffer->common.decRef(&buffer->common); buffer = 0;
274
275 nativeWindow->dequeueBuffer(nativeWindow, &buffer);
276 buffer->common.incRef(&buffer->common);
277
278 // TODO: lockBuffer should rather be executed when the very first
279 // direct rendering occurs.
280 nativeWindow->lockBuffer(nativeWindow, buffer);
281
282
283 if ((width != buffer->width) || (height != buffer->height)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 // TODO: we probably should reset the swap rect here
285 // if the window size has changed
Mathias Agopian1473f462009-04-10 14:24:30 -0700286 width = buffer->width;
287 height = buffer->height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 if (depth.data) {
289 free(depth.data);
Mathias Agopian1473f462009-04-10 14:24:30 -0700290 depth.width = width;
291 depth.height = height;
292 depth.stride = buffer->stride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
294 if (depth.data == 0) {
295 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
296 return EGL_FALSE;
297 }
298 }
299 }
300 return EGL_TRUE;
301}
302
Mathias Agopian1473f462009-04-10 14:24:30 -0700303#ifdef LIBAGL_USE_GRALLOC_COPYBITS
304
305static bool supportedCopybitsDestinationFormat(int format) {
306 // Hardware supported and no destination alpha
307 switch (format) {
308 case HAL_PIXEL_FORMAT_RGB_565:
309 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
310 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
311 return true;
312 default:
313 return false;
314 }
315}
316#endif
317
318EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319{
320 GGLSurface buffer;
321 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700322 buffer.width = this->buffer->width;
323 buffer.height = this->buffer->height;
324 buffer.stride = this->buffer->stride;
325 buffer.data = (GGLubyte*)this->buffer->bits;
326 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 gl->rasterizer.procs.colorBuffer(gl, &buffer);
328 if (depth.data != gl->rasterizer.state.buffers.depth.data)
329 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian1473f462009-04-10 14:24:30 -0700330#ifdef LIBAGL_USE_GRALLOC_COPYBITS
331 gl->copybits.drawSurfaceFd = -1;
332 if (supportedCopybitsDestinationFormat(buffer.format)) {
333 buffer_handle_t handle;
334 this->buffer->getHandle(this->buffer, &handle);
335 if (handle != NULL) {
336 private_handle_t* hand = private_handle_t::dynamicCast(handle);
337 if (hand != NULL) {
338 if (hand->usesPhysicallyContiguousMemory()) {
339 gl->copybits.drawSurfaceFd = hand->fd;
340 }
341 }
342 }
343 }
344#endif // LIBAGL_USE_GRALLOC_COPYBITS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return EGL_TRUE;
346}
Mathias Agopian1473f462009-04-10 14:24:30 -0700347EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348{
349 GGLSurface buffer;
350 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700351 buffer.width = this->buffer->width;
352 buffer.height = this->buffer->height;
353 buffer.stride = this->buffer->stride;
354 buffer.data = (GGLubyte*)this->buffer->bits;
355 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 gl->rasterizer.procs.readBuffer(gl, &buffer);
357 return EGL_TRUE;
358}
Mathias Agopian1473f462009-04-10 14:24:30 -0700359void* egl_window_surface_v2_t::getBits() const {
360 return (GGLubyte*)buffer->bits;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361}
Mathias Agopian1473f462009-04-10 14:24:30 -0700362EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
364}
Mathias Agopian1473f462009-04-10 14:24:30 -0700365EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
367}
Mathias Agopian1473f462009-04-10 14:24:30 -0700368EGLint egl_window_surface_v2_t::getRefreshRate() const {
369 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370}
Mathias Agopian1473f462009-04-10 14:24:30 -0700371EGLint egl_window_surface_v2_t::getSwapBehavior() const {
372 //uint32_t flags = nativeWindow->flags;
373 //if (flags & SURFACE_FLAG_PRESERVE_CONTENT)
374 // return EGL_BUFFER_PRESERVED;
375 // This is now a feature of EGL, currently we don't preserve
376 // the content of the buffers.
377 return EGL_BUFFER_DESTROYED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378}
379
380// ----------------------------------------------------------------------------
381
382struct egl_pixmap_surface_t : public egl_surface_t
383{
384 egl_pixmap_surface_t(
385 EGLDisplay dpy, EGLConfig config,
386 int32_t depthFormat,
387 egl_native_pixmap_t const * pixmap);
388
389 virtual ~egl_pixmap_surface_t() { }
390
Mathias Agopian1473f462009-04-10 14:24:30 -0700391 virtual bool isValid() const { return nativePixmap.version == sizeof(egl_native_pixmap_t); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
393 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
394 virtual EGLint getWidth() const { return nativePixmap.width; }
395 virtual EGLint getHeight() const { return nativePixmap.height; }
396 virtual void* getBits() const { return nativePixmap.data; }
397private:
398 egl_native_pixmap_t nativePixmap;
399};
400
401egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
402 EGLConfig config,
403 int32_t depthFormat,
404 egl_native_pixmap_t const * pixmap)
405 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
406{
407 if (depthFormat) {
408 depth.width = pixmap->width;
409 depth.height = pixmap->height;
410 depth.stride = depth.width; // use the width here
411 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
412 if (depth.data == 0) {
413 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
414 return;
415 }
416 }
417}
418EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
419{
420 GGLSurface buffer;
421 buffer.version = sizeof(GGLSurface);
422 buffer.width = nativePixmap.width;
423 buffer.height = nativePixmap.height;
424 buffer.stride = nativePixmap.stride;
425 buffer.data = nativePixmap.data;
426 buffer.format = nativePixmap.format;
Mathias Agopian1473f462009-04-10 14:24:30 -0700427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 gl->rasterizer.procs.colorBuffer(gl, &buffer);
429 if (depth.data != gl->rasterizer.state.buffers.depth.data)
430 gl->rasterizer.procs.depthBuffer(gl, &depth);
431 return EGL_TRUE;
432}
433EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
434{
435 GGLSurface buffer;
436 buffer.version = sizeof(GGLSurface);
437 buffer.width = nativePixmap.width;
438 buffer.height = nativePixmap.height;
439 buffer.stride = nativePixmap.stride;
440 buffer.data = nativePixmap.data;
441 buffer.format = nativePixmap.format;
442 gl->rasterizer.procs.readBuffer(gl, &buffer);
443 return EGL_TRUE;
444}
445
446// ----------------------------------------------------------------------------
447
448struct egl_pbuffer_surface_t : public egl_surface_t
449{
450 egl_pbuffer_surface_t(
451 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
452 int32_t w, int32_t h, int32_t f);
453
454 virtual ~egl_pbuffer_surface_t();
455
Mathias Agopian1473f462009-04-10 14:24:30 -0700456 virtual bool isValid() const { return pbuffer.data != 0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
458 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
459 virtual EGLint getWidth() const { return pbuffer.width; }
460 virtual EGLint getHeight() const { return pbuffer.height; }
461 virtual void* getBits() const { return pbuffer.data; }
462private:
463 GGLSurface pbuffer;
464};
465
466egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
467 EGLConfig config, int32_t depthFormat,
468 int32_t w, int32_t h, int32_t f)
469 : egl_surface_t(dpy, config, depthFormat)
470{
471 size_t size = w*h;
472 switch (f) {
473 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
474 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
475 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
476 default:
477 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
478 pbuffer.data = 0;
479 break;
480 }
481 pbuffer.version = sizeof(GGLSurface);
482 pbuffer.width = w;
483 pbuffer.height = h;
484 pbuffer.stride = w;
485 pbuffer.data = (GGLubyte*)malloc(size);
486 pbuffer.format = f;
Mathias Agopian1473f462009-04-10 14:24:30 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 if (depthFormat) {
489 depth.width = pbuffer.width;
490 depth.height = pbuffer.height;
491 depth.stride = depth.width; // use the width here
492 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
493 if (depth.data == 0) {
494 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
495 return;
496 }
497 }
498}
499egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
500 free(pbuffer.data);
501}
502EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
503{
504 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
505 if (depth.data != gl->rasterizer.state.buffers.depth.data)
506 gl->rasterizer.procs.depthBuffer(gl, &depth);
507 return EGL_TRUE;
508}
509EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
510{
511 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
512 return EGL_TRUE;
513}
514
515// ----------------------------------------------------------------------------
516
517struct config_pair_t {
518 GLint key;
519 GLint value;
520};
521
522struct configs_t {
523 const config_pair_t* array;
524 int size;
525};
526
527struct config_management_t {
528 GLint key;
529 bool (*match)(GLint reqValue, GLint confValue);
530 static bool atLeast(GLint reqValue, GLint confValue) {
531 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
532 }
533 static bool exact(GLint reqValue, GLint confValue) {
534 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
535 }
536 static bool mask(GLint reqValue, GLint confValue) {
537 return (confValue & reqValue) == reqValue;
538 }
539};
540
541// ----------------------------------------------------------------------------
542
543#define VERSION_MAJOR 1
544#define VERSION_MINOR 2
545static char const * const gVendorString = "Google Inc.";
546static char const * const gVersionString = "1.2 Android Driver";
547static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian1473f462009-04-10 14:24:30 -0700548static char const * const gExtensionsString =
549 "KHR_image_base "
550 // "KHR_image_pixmap "
551 "EGL_ANDROID_image_native_buffer "
552 ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
554// ----------------------------------------------------------------------------
555
556struct extention_map_t {
557 const char * const name;
558 __eglMustCastToProperFunctionPointerType address;
559};
560
561static const extention_map_t gExtentionMap[] = {
Mathias Agopian1473f462009-04-10 14:24:30 -0700562 { "glDrawTexsOES",
563 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
564 { "glDrawTexiOES",
565 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
566 { "glDrawTexfOES",
567 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
568 { "glDrawTexxOES",
569 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
570 { "glDrawTexsvOES",
571 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
572 { "glDrawTexivOES",
573 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
574 { "glDrawTexfvOES",
575 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
576 { "glDrawTexxvOES",
577 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
578 { "glQueryMatrixxOES",
579 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
580 { "glEGLImageTargetTexture2DOES",
581 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
582 { "glEGLImageTargetRenderbufferStorageOES",
583 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
584 { "glClipPlanef",
585 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
586 { "glClipPlanex",
587 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
588 { "glBindBuffer",
589 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
590 { "glBufferData",
591 (__eglMustCastToProperFunctionPointerType)&glBufferData },
592 { "glBufferSubData",
593 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
594 { "glDeleteBuffers",
595 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
596 { "glGenBuffers",
597 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598};
599
Mathias Agopian1473f462009-04-10 14:24:30 -0700600/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * In the lists below, attributes names MUST be sorted.
602 * Additionally, all configs must be sorted according to
603 * the EGL specification.
604 */
605
606static config_pair_t const config_base_attribute_list[] = {
607 { EGL_STENCIL_SIZE, 0 },
608 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
609 { EGL_LEVEL, 0 },
610 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian1473f462009-04-10 14:24:30 -0700611 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
613 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
614 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
615 { EGL_NATIVE_VISUAL_ID, 0 },
616 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
617 { EGL_SAMPLES, 0 },
618 { EGL_SAMPLE_BUFFERS, 0 },
619 { EGL_TRANSPARENT_TYPE, EGL_NONE },
620 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
621 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
622 { EGL_TRANSPARENT_RED_VALUE, 0 },
623 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
624 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
625 { EGL_MIN_SWAP_INTERVAL, 1 },
626 { EGL_MAX_SWAP_INTERVAL, 4 },
627};
628
629// These configs can override the base attribute list
630// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
631
632static config_pair_t const config_0_attribute_list[] = {
633 { EGL_BUFFER_SIZE, 16 },
634 { EGL_ALPHA_SIZE, 0 },
635 { EGL_BLUE_SIZE, 5 },
636 { EGL_GREEN_SIZE, 6 },
637 { EGL_RED_SIZE, 5 },
638 { EGL_DEPTH_SIZE, 0 },
639 { EGL_CONFIG_ID, 0 },
640 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
641};
642
643static config_pair_t const config_1_attribute_list[] = {
644 { EGL_BUFFER_SIZE, 16 },
645 { EGL_ALPHA_SIZE, 0 },
646 { EGL_BLUE_SIZE, 5 },
647 { EGL_GREEN_SIZE, 6 },
648 { EGL_RED_SIZE, 5 },
649 { EGL_DEPTH_SIZE, 16 },
650 { EGL_CONFIG_ID, 1 },
651 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
652};
653
654static config_pair_t const config_2_attribute_list[] = {
655 { EGL_BUFFER_SIZE, 32 },
656 { EGL_ALPHA_SIZE, 8 },
657 { EGL_BLUE_SIZE, 8 },
658 { EGL_GREEN_SIZE, 8 },
659 { EGL_RED_SIZE, 8 },
660 { EGL_DEPTH_SIZE, 0 },
661 { EGL_CONFIG_ID, 2 },
662 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
663};
664
665static config_pair_t const config_3_attribute_list[] = {
666 { EGL_BUFFER_SIZE, 32 },
667 { EGL_ALPHA_SIZE, 8 },
668 { EGL_BLUE_SIZE, 8 },
669 { EGL_GREEN_SIZE, 8 },
670 { EGL_RED_SIZE, 8 },
671 { EGL_DEPTH_SIZE, 16 },
672 { EGL_CONFIG_ID, 3 },
673 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
674};
675
676static config_pair_t const config_4_attribute_list[] = {
677 { EGL_BUFFER_SIZE, 8 },
678 { EGL_ALPHA_SIZE, 8 },
679 { EGL_BLUE_SIZE, 0 },
680 { EGL_GREEN_SIZE, 0 },
681 { EGL_RED_SIZE, 0 },
682 { EGL_DEPTH_SIZE, 0 },
683 { EGL_CONFIG_ID, 4 },
684 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
685};
686
687static config_pair_t const config_5_attribute_list[] = {
688 { EGL_BUFFER_SIZE, 8 },
689 { EGL_ALPHA_SIZE, 8 },
690 { EGL_BLUE_SIZE, 0 },
691 { EGL_GREEN_SIZE, 0 },
692 { EGL_RED_SIZE, 0 },
693 { EGL_DEPTH_SIZE, 16 },
694 { EGL_CONFIG_ID, 5 },
695 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
696};
697
698static configs_t const gConfigs[] = {
699 { config_0_attribute_list, NELEM(config_0_attribute_list) },
700 { config_1_attribute_list, NELEM(config_1_attribute_list) },
701 { config_2_attribute_list, NELEM(config_2_attribute_list) },
702 { config_3_attribute_list, NELEM(config_3_attribute_list) },
703 { config_4_attribute_list, NELEM(config_4_attribute_list) },
704 { config_5_attribute_list, NELEM(config_5_attribute_list) },
705};
706
707static config_management_t const gConfigManagement[] = {
708 { EGL_BUFFER_SIZE, config_management_t::atLeast },
709 { EGL_ALPHA_SIZE, config_management_t::atLeast },
710 { EGL_BLUE_SIZE, config_management_t::atLeast },
711 { EGL_GREEN_SIZE, config_management_t::atLeast },
712 { EGL_RED_SIZE, config_management_t::atLeast },
713 { EGL_DEPTH_SIZE, config_management_t::atLeast },
714 { EGL_STENCIL_SIZE, config_management_t::atLeast },
715 { EGL_CONFIG_CAVEAT, config_management_t::exact },
716 { EGL_CONFIG_ID, config_management_t::exact },
717 { EGL_LEVEL, config_management_t::exact },
718 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::exact },
719 { EGL_MAX_PBUFFER_PIXELS, config_management_t::exact },
720 { EGL_MAX_PBUFFER_WIDTH, config_management_t::exact },
721 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
722 { EGL_NATIVE_VISUAL_ID, config_management_t::exact },
723 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
724 { EGL_SAMPLES, config_management_t::exact },
725 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
726 { EGL_SURFACE_TYPE, config_management_t::mask },
727 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
728 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
729 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
730 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
731 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
732 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
733 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
734 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
735};
736
737static config_pair_t const config_defaults[] = {
738 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
739};
740
741// ----------------------------------------------------------------------------
742
743template<typename T>
744static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
745{
746 while (first <= last) {
747 int mid = (first + last) / 2;
Mathias Agopian1473f462009-04-10 14:24:30 -0700748 if (key > sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 first = mid + 1;
Mathias Agopian1473f462009-04-10 14:24:30 -0700750 } else if (key < sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 last = mid - 1;
752 } else {
753 return mid;
754 }
755 }
756 return -1;
757}
758
759static int isAttributeMatching(int i, EGLint attr, EGLint val)
760{
761 // look for the attribute in all of our configs
Mathias Agopian1473f462009-04-10 14:24:30 -0700762 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 int index = binarySearch<config_pair_t>(
764 gConfigs[i].array,
765 0, gConfigs[i].size-1,
766 attr);
767 if (index < 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700768 configFound = config_base_attribute_list;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 index = binarySearch<config_pair_t>(
770 config_base_attribute_list,
771 0, NELEM(config_base_attribute_list)-1,
772 attr);
773 }
774 if (index >= 0) {
775 // attribute found, check if this config could match
776 int cfgMgtIndex = binarySearch<config_management_t>(
777 gConfigManagement,
778 0, NELEM(gConfigManagement)-1,
779 attr);
780 if (index >= 0) {
781 bool match = gConfigManagement[cfgMgtIndex].match(
782 val, configFound[index].value);
783 if (match) {
784 // this config matches
785 return 1;
786 }
787 } else {
788 // attribute not found. this should NEVER happen.
789 }
790 } else {
791 // error, this attribute doesn't exist
792 }
793 return 0;
794}
795
796static int makeCurrent(ogles_context_t* gl)
797{
798 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
799 if (gl) {
800 egl_context_t* c = egl_context_t::context(gl);
801 if (c->flags & egl_context_t::IS_CURRENT) {
802 if (current != gl) {
803 // it is an error to set a context current, if it's already
804 // current to another thread
805 return -1;
806 }
807 } else {
808 if (current) {
809 // mark the current context as not current, and flush
810 glFlush();
811 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
812 }
813 }
814 if (!(c->flags & egl_context_t::IS_CURRENT)) {
815 // The context is not current, make it current!
816 setGlThreadSpecific(gl);
817 c->flags |= egl_context_t::IS_CURRENT;
818 }
819 } else {
820 if (current) {
821 // mark the current context as not current, and flush
822 glFlush();
823 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
824 }
825 // this thread has no context attached to it
826 setGlThreadSpecific(0);
827 }
828 return 0;
829}
830
831static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
832 EGLint attribute, EGLint *value)
833{
834 size_t numConfigs = NELEM(gConfigs);
835 int index = (int)config;
836 if (uint32_t(index) >= numConfigs)
837 return setError(EGL_BAD_CONFIG, EGL_FALSE);
838
839 int attrIndex;
840 attrIndex = binarySearch<config_pair_t>(
841 gConfigs[index].array,
842 0, gConfigs[index].size-1,
843 attribute);
844 if (attrIndex>=0) {
845 *value = gConfigs[index].array[attrIndex].value;
846 return EGL_TRUE;
847 }
848
849 attrIndex = binarySearch<config_pair_t>(
850 config_base_attribute_list,
851 0, NELEM(config_base_attribute_list)-1,
852 attribute);
853 if (attrIndex>=0) {
854 *value = config_base_attribute_list[attrIndex].value;
855 return EGL_TRUE;
856 }
857 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
858}
859
860static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
861 NativeWindowType window, const EGLint *attrib_list)
862{
863 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
864 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
865 if (window == 0)
866 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
867
868 EGLint surfaceType;
869 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
870 return EGL_FALSE;
871
872 if (!(surfaceType & EGL_WINDOW_BIT))
873 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
874
875 EGLint configID;
876 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
877 return EGL_FALSE;
878
879 int32_t depthFormat;
880 int32_t pixelFormat;
881 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700882 case 0:
883 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 depthFormat = 0;
885 break;
886 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -0700887 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 depthFormat = GGL_PIXEL_FORMAT_Z_16;
889 break;
890 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -0700891 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 depthFormat = 0;
893 break;
894 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -0700895 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 depthFormat = GGL_PIXEL_FORMAT_Z_16;
897 break;
898 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -0700899 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 depthFormat = 0;
901 break;
902 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -0700903 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 depthFormat = GGL_PIXEL_FORMAT_Z_16;
905 break;
906 default:
907 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
908 }
909
910 // FIXME: we don't have access to the pixelFormat here just yet.
911 // (it's possible that the surface is not fully initialized)
912 // maybe this should be done after the page-flip
913 //if (EGLint(info.format) != pixelFormat)
914 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
915
Mathias Agopian1473f462009-04-10 14:24:30 -0700916 egl_surface_t* surface;
917 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
918 static_cast<android_native_window_t*>(window));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919
920 if (!surface->isValid()) {
921 // there was a problem in the ctor, the error
922 // flag has been set.
923 delete surface;
924 surface = 0;
925 }
926 return surface;
927}
928
929static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
930 NativePixmapType pixmap, const EGLint *attrib_list)
931{
932 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
933 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
934 if (pixmap == 0)
935 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
936
937 EGLint surfaceType;
938 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
939 return EGL_FALSE;
940
941 if (!(surfaceType & EGL_PIXMAP_BIT))
942 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
943
944 EGLint configID;
945 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
946 return EGL_FALSE;
947
948 int32_t depthFormat;
949 int32_t pixelFormat;
950 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700951 case 0:
952 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 depthFormat = 0;
954 break;
955 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -0700956 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 depthFormat = GGL_PIXEL_FORMAT_Z_16;
958 break;
959 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -0700960 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 depthFormat = 0;
962 break;
963 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -0700964 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 depthFormat = GGL_PIXEL_FORMAT_Z_16;
966 break;
967 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -0700968 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 depthFormat = 0;
970 break;
971 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -0700972 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 depthFormat = GGL_PIXEL_FORMAT_Z_16;
974 break;
975 default:
976 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
977 }
978
979 if (pixmap->format != pixelFormat)
980 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
981
982 egl_surface_t* surface =
983 new egl_pixmap_surface_t(dpy, config, depthFormat,
984 static_cast<egl_native_pixmap_t*>(pixmap));
985
986 if (!surface->isValid()) {
987 // there was a problem in the ctor, the error
988 // flag has been set.
989 delete surface;
990 surface = 0;
991 }
992 return surface;
993}
994
995static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
996 const EGLint *attrib_list)
997{
998 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
999 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1000
1001 EGLint surfaceType;
1002 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1003 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 if (!(surfaceType & EGL_PBUFFER_BIT))
1006 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 EGLint configID;
1009 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1010 return EGL_FALSE;
1011
1012 int32_t depthFormat;
1013 int32_t pixelFormat;
1014 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001015 case 0:
1016 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 depthFormat = 0;
1018 break;
1019 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -07001020 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1022 break;
1023 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -07001024 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 depthFormat = 0;
1026 break;
1027 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -07001028 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1030 break;
1031 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -07001032 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 depthFormat = 0;
1034 break;
1035 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -07001036 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1038 break;
1039 default:
1040 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1041 }
1042
1043 int32_t w = 0;
1044 int32_t h = 0;
1045 while (attrib_list[0]) {
1046 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1047 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1048 attrib_list+=2;
1049 }
1050
1051 egl_surface_t* surface =
1052 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1053
1054 if (!surface->isValid()) {
1055 // there was a problem in the ctor, the error
1056 // flag has been set.
1057 delete surface;
1058 surface = 0;
1059 }
1060 return surface;
1061}
1062
1063// ----------------------------------------------------------------------------
1064}; // namespace android
1065// ----------------------------------------------------------------------------
1066
1067using namespace android;
1068
1069// ----------------------------------------------------------------------------
1070// Initialization
1071// ----------------------------------------------------------------------------
1072
1073EGLDisplay eglGetDisplay(NativeDisplayType display)
1074{
1075#ifndef HAVE_ANDROID_OS
1076 // this just needs to be done once
1077 if (gGLKey == -1) {
1078 pthread_mutex_lock(&gInitMutex);
1079 if (gGLKey == -1)
1080 pthread_key_create(&gGLKey, NULL);
1081 pthread_mutex_unlock(&gInitMutex);
1082 }
1083#endif
1084 if (display == EGL_DEFAULT_DISPLAY) {
1085 EGLDisplay dpy = (EGLDisplay)1;
1086 egl_display_t& d = egl_display_t::get_display(dpy);
1087 d.type = display;
1088 return dpy;
Mathias Agopian1473f462009-04-10 14:24:30 -07001089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 return EGL_NO_DISPLAY;
1091}
1092
1093EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1094{
1095 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1096 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 EGLBoolean res = EGL_TRUE;
1099 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -07001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (android_atomic_inc(&d.initialized) == 0) {
1102 // initialize stuff here if needed
1103 //pthread_mutex_lock(&gInitMutex);
1104 //pthread_mutex_unlock(&gInitMutex);
1105 }
1106
1107 if (res == EGL_TRUE) {
1108 if (major != NULL) *major = VERSION_MAJOR;
1109 if (minor != NULL) *minor = VERSION_MINOR;
1110 }
1111 return res;
1112}
1113
1114EGLBoolean eglTerminate(EGLDisplay dpy)
1115{
1116 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1117 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1118
1119 EGLBoolean res = EGL_TRUE;
1120 egl_display_t& d = egl_display_t::get_display(dpy);
1121 if (android_atomic_dec(&d.initialized) == 1) {
1122 // TODO: destroy all resources (surfaces, contexts, etc...)
1123 //pthread_mutex_lock(&gInitMutex);
1124 //pthread_mutex_unlock(&gInitMutex);
1125 }
1126 return res;
1127}
1128
1129// ----------------------------------------------------------------------------
1130// configuration
1131// ----------------------------------------------------------------------------
1132
1133EGLBoolean eglGetConfigs( EGLDisplay dpy,
1134 EGLConfig *configs,
1135 EGLint config_size, EGLint *num_config)
1136{
1137 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1138 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1139
1140 GLint numConfigs = NELEM(gConfigs);
1141 if (!configs) {
1142 *num_config = numConfigs;
1143 return EGL_TRUE;
1144 }
1145 GLint i;
1146 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1147 *configs++ = (EGLConfig)i;
1148 }
1149 *num_config = i;
1150 return EGL_TRUE;
1151}
1152
1153EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1154 EGLConfig *configs, EGLint config_size,
1155 EGLint *num_config)
1156{
1157 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1158 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich1badb712009-03-25 15:12:17 -07001159
1160 if (ggl_unlikely(num_config==0)) {
1161 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163
Jack Palevich1badb712009-03-25 15:12:17 -07001164 if (ggl_unlikely(attrib_list==0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 *num_config = 0;
1166 return EGL_TRUE;
1167 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 int numAttributes = 0;
1170 int numConfigs = NELEM(gConfigs);
1171 uint32_t possibleMatch = (1<<numConfigs)-1;
1172 while(possibleMatch && *attrib_list != EGL_NONE) {
1173 numAttributes++;
1174 EGLint attr = *attrib_list++;
1175 EGLint val = *attrib_list++;
1176 for (int i=0 ; i<numConfigs ; i++) {
1177 if (!(possibleMatch & (1<<i)))
1178 continue;
1179 if (isAttributeMatching(i, attr, val) == 0) {
1180 possibleMatch &= ~(1<<i);
1181 }
1182 }
1183 }
1184
1185 // now, handle the attributes which have a useful default value
1186 for (size_t j=0 ; j<NELEM(config_defaults) ; j++) {
1187 // see if this attribute was specified, if not apply its
1188 // default value
1189 if (binarySearch<config_pair_t>(
1190 (config_pair_t const*)attrib_list,
1191 0, numAttributes,
1192 config_defaults[j].key) < 0)
1193 {
1194 for (int i=0 ; i<numConfigs ; i++) {
1195 if (!(possibleMatch & (1<<i)))
1196 continue;
1197 if (isAttributeMatching(i,
1198 config_defaults[j].key,
1199 config_defaults[j].value) == 0)
1200 {
1201 possibleMatch &= ~(1<<i);
1202 }
1203 }
1204 }
1205 }
1206
1207 // return the configurations found
1208 int n=0;
1209 if (possibleMatch) {
Jack Palevich1badb712009-03-25 15:12:17 -07001210 if (configs) {
1211 for (int i=0 ; config_size && i<numConfigs ; i++) {
1212 if (possibleMatch & (1<<i)) {
1213 *configs++ = (EGLConfig)i;
1214 config_size--;
1215 n++;
1216 }
1217 }
1218 } else {
1219 for (int i=0 ; i<numConfigs ; i++) {
1220 if (possibleMatch & (1<<i)) {
1221 n++;
1222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 }
1225 }
1226 *num_config = n;
1227 return EGL_TRUE;
1228}
1229
1230EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1231 EGLint attribute, EGLint *value)
1232{
1233 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1234 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1235
1236 return getConfigAttrib(dpy, config, attribute, value);
1237}
1238
1239// ----------------------------------------------------------------------------
1240// surfaces
1241// ----------------------------------------------------------------------------
1242
1243EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1244 NativeWindowType window,
1245 const EGLint *attrib_list)
1246{
1247 return createWindowSurface(dpy, config, window, attrib_list);
1248}
Mathias Agopian1473f462009-04-10 14:24:30 -07001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1251 NativePixmapType pixmap,
1252 const EGLint *attrib_list)
1253{
1254 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1255}
1256
1257EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1258 const EGLint *attrib_list)
1259{
1260 return createPbufferSurface(dpy, config, attrib_list);
1261}
Mathias Agopian1473f462009-04-10 14:24:30 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1264{
1265 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1266 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1267 if (eglSurface != EGL_NO_SURFACE) {
1268 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
1269 if (surface->magic != egl_surface_t::MAGIC)
1270 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1271 if (surface->dpy != dpy)
1272 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1273 delete surface;
1274 }
1275 return EGL_TRUE;
1276}
1277
1278EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1279 EGLint attribute, EGLint *value)
1280{
1281 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1282 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1283 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
1284 if (surface->dpy != dpy)
1285 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1286
1287 EGLBoolean ret = EGL_TRUE;
1288 switch (attribute) {
1289 case EGL_CONFIG_ID:
1290 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1291 break;
1292 case EGL_WIDTH:
1293 *value = surface->getWidth();
1294 break;
1295 case EGL_HEIGHT:
1296 *value = surface->getHeight();
1297 break;
1298 case EGL_LARGEST_PBUFFER:
1299 // not modified for a window or pixmap surface
1300 break;
1301 case EGL_TEXTURE_FORMAT:
1302 *value = EGL_NO_TEXTURE;
1303 break;
1304 case EGL_TEXTURE_TARGET:
1305 *value = EGL_NO_TEXTURE;
1306 break;
1307 case EGL_MIPMAP_TEXTURE:
1308 *value = EGL_FALSE;
1309 break;
1310 case EGL_MIPMAP_LEVEL:
1311 *value = 0;
1312 break;
1313 case EGL_RENDER_BUFFER:
1314 // TODO: return the real RENDER_BUFFER here
1315 *value = EGL_BACK_BUFFER;
1316 break;
1317 case EGL_HORIZONTAL_RESOLUTION:
1318 // pixel/mm * EGL_DISPLAY_SCALING
1319 *value = surface->getHorizontalResolution();
1320 break;
1321 case EGL_VERTICAL_RESOLUTION:
1322 // pixel/mm * EGL_DISPLAY_SCALING
1323 *value = surface->getVerticalResolution();
1324 break;
1325 case EGL_PIXEL_ASPECT_RATIO: {
1326 // w/h * EGL_DISPLAY_SCALING
1327 int wr = surface->getHorizontalResolution();
1328 int hr = surface->getVerticalResolution();
1329 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1330 } break;
1331 case EGL_SWAP_BEHAVIOR:
Mathias Agopian1473f462009-04-10 14:24:30 -07001332 *value = surface->getSwapBehavior();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 break;
1334 default:
1335 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1336 }
1337 return ret;
1338}
1339
1340EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1341 EGLContext share_list, const EGLint *attrib_list)
1342{
1343 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1344 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1345
1346 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1347 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1348
1349 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1350 c->flags = egl_context_t::NEVER_CURRENT;
1351 c->dpy = dpy;
1352 c->config = config;
1353 c->read = 0;
1354 c->draw = 0;
1355 return (EGLContext)gl;
1356}
1357
1358EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1359{
1360 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1361 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1362 egl_context_t* c = egl_context_t::context(ctx);
1363 if (c->flags & egl_context_t::IS_CURRENT)
1364 setGlThreadSpecific(0);
1365 ogles_uninit((ogles_context_t*)ctx);
1366 return EGL_TRUE;
1367}
1368
1369EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1370 EGLSurface read, EGLContext ctx)
1371{
1372 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1373 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1374 if (draw) {
1375 egl_surface_t* s = (egl_surface_t*)draw;
1376 if (s->dpy != dpy)
1377 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1378 // TODO: check that draw and read are compatible with the context
1379 }
1380
1381 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian1473f462009-04-10 14:24:30 -07001382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1384 return setError(EGL_BAD_MATCH, EGL_FALSE);
1385
1386 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1387 return setError(EGL_BAD_MATCH, EGL_FALSE);
1388
1389 if (ctx == EGL_NO_CONTEXT) {
1390 // if we're detaching, we need the current context
1391 current_ctx = (EGLContext)getGlThreadSpecific();
1392 } else {
1393 egl_context_t* c = egl_context_t::context(ctx);
1394 egl_surface_t* d = (egl_surface_t*)draw;
1395 egl_surface_t* r = (egl_surface_t*)read;
1396 if ((d && d->ctx && d->ctx != ctx) ||
1397 (r && r->ctx && r->ctx != ctx)) {
1398 // once of the surface is bound to a context in another thread
1399 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1400 }
1401 }
1402
Mathias Agopian1473f462009-04-10 14:24:30 -07001403 // TODO: call connect / disconnect on the surface
1404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 ogles_context_t* gl = (ogles_context_t*)ctx;
1406 if (makeCurrent(gl) == 0) {
1407 if (ctx) {
1408 egl_context_t* c = egl_context_t::context(ctx);
1409 egl_surface_t* d = (egl_surface_t*)draw;
1410 egl_surface_t* r = (egl_surface_t*)read;
1411 c->read = read;
1412 c->draw = draw;
1413 if (c->flags & egl_context_t::NEVER_CURRENT) {
1414 c->flags &= ~egl_context_t::NEVER_CURRENT;
1415 GLint w = 0;
1416 GLint h = 0;
1417 if (draw) {
1418 w = d->getWidth();
1419 h = d->getHeight();
1420 }
1421 ogles_surfaceport(gl, 0, 0);
1422 ogles_viewport(gl, 0, 0, w, h);
1423 ogles_scissor(gl, 0, 0, w, h);
1424 }
1425 if (d) {
1426 d->ctx = ctx;
1427 d->bindDrawSurface(gl);
1428 }
1429 if (r) {
1430 r->ctx = ctx;
1431 r->bindReadSurface(gl);
1432 }
1433 } else {
1434 // if surfaces were bound to the context bound to this thread
1435 // mark then as unbound.
1436 if (current_ctx) {
1437 egl_context_t* c = egl_context_t::context(current_ctx);
1438 egl_surface_t* d = (egl_surface_t*)c->draw;
1439 egl_surface_t* r = (egl_surface_t*)c->read;
1440 if (d) d->ctx = EGL_NO_CONTEXT;
1441 if (r) r->ctx = EGL_NO_CONTEXT;
1442 }
1443 }
1444 return EGL_TRUE;
1445 }
1446 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1447}
1448
1449EGLContext eglGetCurrentContext(void)
1450{
1451 // eglGetCurrentContext returns the current EGL rendering context,
1452 // as specified by eglMakeCurrent. If no context is current,
1453 // EGL_NO_CONTEXT is returned.
1454 return (EGLContext)getGlThreadSpecific();
1455}
1456
1457EGLSurface eglGetCurrentSurface(EGLint readdraw)
1458{
1459 // eglGetCurrentSurface returns the read or draw surface attached
1460 // to the current EGL rendering context, as specified by eglMakeCurrent.
1461 // If no context is current, EGL_NO_SURFACE is returned.
1462 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1463 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1464 egl_context_t* c = egl_context_t::context(ctx);
1465 if (readdraw == EGL_READ) {
1466 return c->read;
1467 } else if (readdraw == EGL_DRAW) {
1468 return c->draw;
1469 }
1470 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1471}
1472
1473EGLDisplay eglGetCurrentDisplay(void)
1474{
1475 // eglGetCurrentDisplay returns the current EGL display connection
1476 // for the current EGL rendering context, as specified by eglMakeCurrent.
1477 // If no context is current, EGL_NO_DISPLAY is returned.
1478 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1479 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1480 egl_context_t* c = egl_context_t::context(ctx);
1481 return c->dpy;
1482}
1483
1484EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1485 EGLint attribute, EGLint *value)
1486{
1487 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1488 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1489 egl_context_t* c = egl_context_t::context(ctx);
1490 switch (attribute) {
1491 case EGL_CONFIG_ID:
1492 // Returns the ID of the EGL frame buffer configuration with
1493 // respect to which the context was created
1494 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1495 }
1496 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1497}
1498
1499EGLBoolean eglWaitGL(void)
1500{
1501 return EGL_TRUE;
1502}
1503
1504EGLBoolean eglWaitNative(EGLint engine)
1505{
1506 return EGL_TRUE;
1507}
1508
1509EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1510{
1511 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1512 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
1515 if (d->dpy != dpy)
1516 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1517
1518 // post the surface
1519 d->swapBuffers();
1520
1521 // if it's bound to a context, update the buffer
1522 if (d->ctx != EGL_NO_CONTEXT) {
1523 d->bindDrawSurface((ogles_context_t*)d->ctx);
1524 // if this surface is also the read surface of the context
1525 // it is bound to, make sure to update the read buffer as well.
1526 // The EGL spec is a little unclear about this.
1527 egl_context_t* c = egl_context_t::context(d->ctx);
1528 if (c->read == draw) {
1529 d->bindReadSurface((ogles_context_t*)d->ctx);
1530 }
1531 }
1532
1533 return EGL_TRUE;
1534}
1535
1536EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1537 NativePixmapType target)
1538{
1539 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1540 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1541 // TODO: eglCopyBuffers()
1542 return EGL_FALSE;
1543}
1544
1545EGLint eglGetError(void)
1546{
1547 return getError();
1548}
1549
1550const char* eglQueryString(EGLDisplay dpy, EGLint name)
1551{
1552 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1553 return setError(EGL_BAD_DISPLAY, (const char*)0);
1554
1555 switch (name) {
1556 case EGL_VENDOR:
1557 return gVendorString;
1558 case EGL_VERSION:
1559 return gVersionString;
1560 case EGL_EXTENSIONS:
1561 return gExtensionsString;
1562 case EGL_CLIENT_APIS:
1563 return gClientApiString;
1564 }
1565 return setError(EGL_BAD_PARAMETER, (const char *)0);
1566}
1567
1568// ----------------------------------------------------------------------------
1569// EGL 1.1
1570// ----------------------------------------------------------------------------
1571
1572EGLBoolean eglSurfaceAttrib(
1573 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1574{
1575 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1576 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1577 // TODO: eglSurfaceAttrib()
1578 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1579}
1580
1581EGLBoolean eglBindTexImage(
1582 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1583{
1584 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1585 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1586 // TODO: eglBindTexImage()
1587 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1588}
1589
1590EGLBoolean eglReleaseTexImage(
1591 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1592{
1593 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1594 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1595 // TODO: eglReleaseTexImage()
1596 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1597}
1598
1599EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1600{
1601 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1602 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1603 // TODO: eglSwapInterval()
1604 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1605}
1606
1607// ----------------------------------------------------------------------------
1608// EGL 1.2
1609// ----------------------------------------------------------------------------
1610
1611EGLBoolean eglBindAPI(EGLenum api)
1612{
1613 if (api != EGL_OPENGL_ES_API)
1614 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1615 return EGL_TRUE;
1616}
1617
1618EGLenum eglQueryAPI(void)
1619{
1620 return EGL_OPENGL_ES_API;
1621}
1622
1623EGLBoolean eglWaitClient(void)
1624{
1625 glFinish();
1626 return EGL_TRUE;
1627}
1628
1629EGLBoolean eglReleaseThread(void)
1630{
1631 // TODO: eglReleaseThread()
1632 return EGL_TRUE;
1633}
1634
1635EGLSurface eglCreatePbufferFromClientBuffer(
1636 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1637 EGLConfig config, const EGLint *attrib_list)
1638{
1639 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1640 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1641 // TODO: eglCreatePbufferFromClientBuffer()
1642 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1643}
1644
1645// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -07001646// EGL_EGLEXT_VERSION 3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647// ----------------------------------------------------------------------------
1648
1649void (*eglGetProcAddress (const char *procname))()
1650{
1651 extention_map_t const * const map = gExtentionMap;
1652 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
1653 if (!strcmp(procname, map[i].name)) {
1654 return map[i].address;
1655 }
1656 }
1657 return NULL;
1658}
Mathias Agopian1473f462009-04-10 14:24:30 -07001659
1660EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1661 const EGLint *attrib_list)
1662{
1663 EGLBoolean result = EGL_FALSE;
1664 return result;
1665}
1666
1667EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1668{
1669 EGLBoolean result = EGL_FALSE;
1670 return result;
1671}
1672
1673EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1674 EGLClientBuffer buffer, const EGLint *attrib_list)
1675{
1676 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
1677 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1678 }
1679 if (ctx != EGL_NO_CONTEXT) {
1680 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1681 }
1682 if (target != EGL_NATIVE_BUFFER_ANDROID) {
1683 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1684 }
1685
1686 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
1687
1688 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1689 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1690
1691 if (native_buffer->common.version != sizeof(android_native_buffer_t))
1692 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1693
1694 hw_module_t const* pModule;
1695 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule))
1696 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1697 buffer_handle_t bufferHandle;
1698 gralloc_module_t const* module =
1699 reinterpret_cast<gralloc_module_t const*>(pModule);
1700 if (native_buffer->getHandle(native_buffer, &bufferHandle) < 0)
1701 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1702 if (module->map(module, bufferHandle, &native_buffer->bits) < 0)
1703 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1704
1705 native_buffer->common.incRef(&native_buffer->common);
1706 return (EGLImageKHR)native_buffer;
1707}
1708
1709EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1710{
1711 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
1712 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1713 }
1714
1715 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
1716
1717 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1718 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1719
1720 if (native_buffer->common.version != sizeof(android_native_buffer_t))
1721 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1722
1723 hw_module_t const* pModule;
1724 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule) == 0) {
1725 buffer_handle_t bufferHandle;
1726 gralloc_module_t const* module =
1727 reinterpret_cast<gralloc_module_t const*>(pModule);
1728 if (native_buffer->getHandle(native_buffer, &bufferHandle) == 0) {
1729 module->unmap(module, bufferHandle);
1730 }
1731 }
1732
1733 native_buffer->common.decRef(&native_buffer->common);
1734
1735 return EGL_TRUE;
1736}