blob: 80ddc0280f9acf1fd72de0668cdee09f174457c6 [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>
36#include <GLES/gl.h>
37#include <GLES/glext.h>
38
39#include <pixelflinger/format.h>
40#include <pixelflinger/pixelflinger.h>
41
Mathias Agopianb51e18d2009-05-05 18:21:32 -070042#include <private/ui/android_natives_priv.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070043
Mathias Agopian68eeb802009-06-25 15:39:25 -070044#include <hardware/copybit.h>
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include "context.h"
47#include "state.h"
48#include "texture.h"
49#include "matrix.h"
50
51#undef NELEM
52#define NELEM(x) (sizeof(x)/sizeof(*(x)))
53
54// ----------------------------------------------------------------------------
55namespace android {
56// ----------------------------------------------------------------------------
57
58const unsigned int NUM_DISPLAYS = 1;
59
60static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
61static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
62static pthread_key_t gEGLErrorKey = -1;
63#ifndef HAVE_ANDROID_OS
64namespace gl {
65pthread_key_t gGLKey = -1;
66}; // namespace gl
67#endif
68
69template<typename T>
70static T setError(GLint error, T returnValue) {
71 if (ggl_unlikely(gEGLErrorKey == -1)) {
72 pthread_mutex_lock(&gErrorKeyMutex);
73 if (gEGLErrorKey == -1)
74 pthread_key_create(&gEGLErrorKey, NULL);
75 pthread_mutex_unlock(&gErrorKeyMutex);
76 }
77 pthread_setspecific(gEGLErrorKey, (void*)error);
78 return returnValue;
79}
80
81static GLint getError() {
82 if (ggl_unlikely(gEGLErrorKey == -1))
83 return EGL_SUCCESS;
84 GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
85 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
86 return error;
87}
88
89// ----------------------------------------------------------------------------
90
91struct egl_display_t
92{
93 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian1473f462009-04-10 14:24:30 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 static EGLBoolean is_valid(EGLDisplay dpy) {
98 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
99 }
100
101 NativeDisplayType type;
102 volatile int32_t initialized;
103};
104
105static egl_display_t gDisplays[NUM_DISPLAYS];
106
107egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
108 return gDisplays[uintptr_t(dpy)-1U];
109}
110
111struct egl_context_t {
112 enum {
113 IS_CURRENT = 0x00010000,
114 NEVER_CURRENT = 0x00020000
115 };
116 uint32_t flags;
117 EGLDisplay dpy;
118 EGLConfig config;
119 EGLSurface read;
120 EGLSurface draw;
121
122 static inline egl_context_t* context(EGLContext ctx) {
123 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
124 return static_cast<egl_context_t*>(gl->rasterizer.base);
125 }
126};
127
128// ----------------------------------------------------------------------------
129
130struct egl_surface_t
131{
132 enum {
133 PAGE_FLIP = 0x00000001,
134 MAGIC = 0x31415265
135 };
136
137 uint32_t magic;
138 EGLDisplay dpy;
139 EGLConfig config;
140 EGLContext ctx;
141
142 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
143 virtual ~egl_surface_t();
Mathias Agopianffbc8642009-08-20 00:12:56 -0700144 bool isValid() const;
145 virtual bool initCheck() 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;
Mathias Agopianabac0102009-07-31 14:47:00 -0700149 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700150 virtual void disconnect() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 virtual EGLint getWidth() const = 0;
152 virtual EGLint getHeight() const = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 virtual EGLint getHorizontalResolution() const;
155 virtual EGLint getVerticalResolution() const;
156 virtual EGLint getRefreshRate() const;
157 virtual EGLint getSwapBehavior() const;
158 virtual EGLBoolean swapBuffers();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700159 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700160 virtual EGLClientBuffer getRenderBuffer() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161protected:
162 GGLSurface depth;
163};
164
165egl_surface_t::egl_surface_t(EGLDisplay dpy,
166 EGLConfig config,
167 int32_t depthFormat)
168 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
169{
170 depth.version = sizeof(GGLSurface);
171 depth.data = 0;
172 depth.format = depthFormat;
173}
174egl_surface_t::~egl_surface_t()
175{
176 magic = 0;
177 free(depth.data);
178}
Mathias Agopianffbc8642009-08-20 00:12:56 -0700179bool egl_surface_t::isValid() const {
180 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
181 return magic == MAGIC;
182}
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184EGLBoolean egl_surface_t::swapBuffers() {
185 return EGL_FALSE;
186}
187EGLint egl_surface_t::getHorizontalResolution() const {
188 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
189}
190EGLint egl_surface_t::getVerticalResolution() const {
191 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
192}
193EGLint egl_surface_t::getRefreshRate() const {
194 return (60 * EGL_DISPLAY_SCALING);
195}
196EGLint egl_surface_t::getSwapBehavior() const {
197 return EGL_BUFFER_PRESERVED;
198}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700199EGLBoolean egl_surface_t::setSwapRectangle(
200 EGLint l, EGLint t, EGLint w, EGLint h)
201{
202 return EGL_FALSE;
203}
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700204EGLClientBuffer egl_surface_t::getRenderBuffer() const {
205 return 0;
206}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
208// ----------------------------------------------------------------------------
209
Mathias Agopian1473f462009-04-10 14:24:30 -0700210struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211{
Mathias Agopian1473f462009-04-10 14:24:30 -0700212 egl_window_surface_v2_t(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 EGLDisplay dpy, EGLConfig config,
214 int32_t depthFormat,
Mathias Agopian1473f462009-04-10 14:24:30 -0700215 android_native_window_t* window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Mathias Agopianffbc8642009-08-20 00:12:56 -0700217 ~egl_window_surface_v2_t();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
Mathias Agopianffbc8642009-08-20 00:12:56 -0700219 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 virtual EGLBoolean swapBuffers();
221 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
222 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopianabac0102009-07-31 14:47:00 -0700223 virtual EGLBoolean connect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700224 virtual void disconnect();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700225 virtual EGLint getWidth() const { return width; }
226 virtual EGLint getHeight() const { return height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 virtual EGLint getHorizontalResolution() const;
228 virtual EGLint getVerticalResolution() const;
229 virtual EGLint getRefreshRate() const;
230 virtual EGLint getSwapBehavior() const;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700231 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700232 virtual EGLClientBuffer getRenderBuffer() const;
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234private:
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700235 status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700236 status_t unlock(android_native_buffer_t* buf);
Mathias Agopian1473f462009-04-10 14:24:30 -0700237 android_native_window_t* nativeWindow;
238 android_native_buffer_t* buffer;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700239 android_native_buffer_t* previousBuffer;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700240 gralloc_module_t const* module;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700241 copybit_device_t* blitengine;
Mathias Agopian1473f462009-04-10 14:24:30 -0700242 int width;
243 int height;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700244 void* bits;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700245 GGLFormat const* pixelFormatTable;
246
247 struct Rect {
248 inline Rect() { };
249 inline Rect(int32_t w, int32_t h)
250 : left(0), top(0), right(w), bottom(h) { }
251 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
252 : left(l), top(t), right(r), bottom(b) { }
253 Rect& andSelf(const Rect& r) {
254 left = max(left, r.left);
255 top = max(top, r.top);
256 right = min(right, r.right);
257 bottom = min(bottom, r.bottom);
258 return *this;
259 }
260 bool isEmpty() const {
261 return (left>=right || top>=bottom);
262 }
263 void dump(char const* what) {
264 LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
265 what, left, top, right-left, bottom-top);
266 }
267
268 int32_t left;
269 int32_t top;
270 int32_t right;
271 int32_t bottom;
272 };
273
274 struct Region {
275 inline Region() : count(0) { }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700276 typedef Rect const* const_iterator;
277 const_iterator begin() const { return storage; }
278 const_iterator end() const { return storage+count; }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700279 static Region subtract(const Rect& lhs, const Rect& rhs) {
280 Region reg;
281 Rect* storage = reg.storage;
282 if (!lhs.isEmpty()) {
283 if (lhs.top < rhs.top) { // top rect
284 storage->left = lhs.left;
285 storage->top = lhs.top;
286 storage->right = lhs.right;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700287 storage->bottom = rhs.top;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700288 storage++;
289 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700290 const int32_t top = max(lhs.top, rhs.top);
291 const int32_t bot = min(lhs.bottom, rhs.bottom);
292 if (top < bot) {
293 if (lhs.left < rhs.left) { // left-side rect
294 storage->left = lhs.left;
295 storage->top = top;
296 storage->right = rhs.left;
297 storage->bottom = bot;
298 storage++;
299 }
300 if (lhs.right > rhs.right) { // right-side rect
301 storage->left = rhs.right;
302 storage->top = top;
303 storage->right = lhs.right;
304 storage->bottom = bot;
305 storage++;
306 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700307 }
308 if (lhs.bottom > rhs.bottom) { // bottom rect
309 storage->left = lhs.left;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700310 storage->top = rhs.bottom;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700311 storage->right = lhs.right;
312 storage->bottom = lhs.bottom;
313 storage++;
314 }
315 reg.count = storage - reg.storage;
316 }
317 return reg;
318 }
319 bool isEmpty() const {
320 return count<=0;
321 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700322 private:
323 Rect storage[4];
324 ssize_t count;
325 };
326
Mathias Agopian68eeb802009-06-25 15:39:25 -0700327 struct region_iterator : public copybit_region_t {
328 region_iterator(const Region& region)
329 : b(region.begin()), e(region.end()) {
330 this->next = iterate;
331 }
332 private:
333 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
334 region_iterator const* me = static_cast<region_iterator const*>(self);
335 if (me->b != me->e) {
336 *reinterpret_cast<Rect*>(rect) = *me->b++;
337 return 1;
338 }
339 return 0;
340 }
341 mutable Region::const_iterator b;
342 Region::const_iterator const e;
343 };
344
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700345 void copyBlt(
346 android_native_buffer_t* dst, void* dst_vaddr,
347 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700348 const Region& clip);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700349
350 Rect dirtyRegion;
351 Rect oldDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352};
353
Mathias Agopian1473f462009-04-10 14:24:30 -0700354egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 EGLConfig config,
356 int32_t depthFormat,
Mathias Agopian1473f462009-04-10 14:24:30 -0700357 android_native_window_t* window)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700358 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700359 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian68eeb802009-06-25 15:39:25 -0700360 blitengine(0), bits(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361{
Mathias Agopiandff8e582009-05-04 14:17:04 -0700362 hw_module_t const* pModule;
363 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
364 module = reinterpret_cast<gralloc_module_t const*>(pModule);
365
Mathias Agopian68eeb802009-06-25 15:39:25 -0700366 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
367 copybit_open(pModule, &blitengine);
368 }
369
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700370 pixelFormatTable = gglGetPixelFormatTable();
371
372 // keep a reference on the window
Mathias Agopian1473f462009-04-10 14:24:30 -0700373 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700374 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
375 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700376}
Mathias Agopian1473f462009-04-10 14:24:30 -0700377
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700378egl_window_surface_v2_t::~egl_window_surface_v2_t() {
379 if (buffer) {
380 buffer->common.decRef(&buffer->common);
381 }
382 if (previousBuffer) {
383 previousBuffer->common.decRef(&previousBuffer->common);
384 }
385 nativeWindow->common.decRef(&nativeWindow->common);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700386 if (blitengine) {
387 copybit_close(blitengine);
388 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700389}
390
Mathias Agopianabac0102009-07-31 14:47:00 -0700391EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700392{
Mathias Agopian5cec4742009-08-11 22:34:02 -0700393 // we're intending to do software rendering
394 native_window_set_usage(nativeWindow,
395 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
396
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700397 // dequeue a buffer
Mathias Agopianabac0102009-07-31 14:47:00 -0700398 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
399 return setError(EGL_BAD_ALLOC, EGL_FALSE);
400 }
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700401
402 // allocate a corresponding depth-buffer
403 width = buffer->width;
404 height = buffer->height;
405 if (depth.format) {
406 depth.width = width;
407 depth.height = height;
408 depth.stride = depth.width; // use the width here
409 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
410 if (depth.data == 0) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700411 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700412 }
413 }
414
415 // keep a reference on the buffer
416 buffer->common.incRef(&buffer->common);
417
Mathias Agopiandff8e582009-05-04 14:17:04 -0700418 // Lock the buffer
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700419 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700420 // pin the buffer down
421 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
422 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Mathias Agopian68eeb802009-06-25 15:39:25 -0700423 LOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700424 buffer, buffer->width, buffer->height);
Mathias Agopianabac0102009-07-31 14:47:00 -0700425 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700426 // FIXME: we should make sure we're not accessing the buffer anymore
427 }
Mathias Agopianabac0102009-07-31 14:47:00 -0700428 return EGL_TRUE;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700429}
430
431void egl_window_surface_v2_t::disconnect()
432{
Mathias Agopian36432cc2009-06-03 19:00:53 -0700433 if (buffer && bits) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700434 bits = NULL;
435 unlock(buffer);
436 }
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700437 // enqueue the last frame
438 nativeWindow->queueBuffer(nativeWindow, buffer);
439 if (buffer) {
440 buffer->common.decRef(&buffer->common);
441 buffer = 0;
442 }
443 if (previousBuffer) {
444 previousBuffer->common.decRef(&previousBuffer->common);
445 previousBuffer = 0;
446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447}
448
Mathias Agopiandff8e582009-05-04 14:17:04 -0700449status_t egl_window_surface_v2_t::lock(
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700450 android_native_buffer_t* buf, int usage, void** vaddr)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700451{
Mathias Agopiane633f932009-05-05 00:59:23 -0700452 int err = module->lock(module, buf->handle,
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700453 usage, 0, 0, buf->width, buf->height, vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700454 return err;
455}
456
457status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
458{
Mathias Agopianabac0102009-07-31 14:47:00 -0700459 if (!buf) return BAD_VALUE;
Mathias Agopiane633f932009-05-05 00:59:23 -0700460 int err = module->unlock(module, buf->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700461 return err;
462}
463
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700464void egl_window_surface_v2_t::copyBlt(
465 android_native_buffer_t* dst, void* dst_vaddr,
466 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700467 const Region& clip)
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700468{
469 // FIXME: use copybit if possible
470 // NOTE: dst and src must be the same format
471
Mathias Agopian68eeb802009-06-25 15:39:25 -0700472 status_t err = NO_ERROR;
473 copybit_device_t* const copybit = blitengine;
474 if (copybit) {
475 copybit_image_t simg;
476 simg.w = src->width;
477 simg.h = src->height;
478 simg.format = src->format;
479 simg.handle = const_cast<native_handle_t*>(src->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700480
Mathias Agopian68eeb802009-06-25 15:39:25 -0700481 copybit_image_t dimg;
482 dimg.w = dst->width;
483 dimg.h = dst->height;
484 dimg.format = dst->format;
485 dimg.handle = const_cast<native_handle_t*>(dst->handle);
486
487 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
488 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
489 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
490 region_iterator it(clip);
491 err = copybit->blit(copybit, &dimg, &simg, &it);
492 if (err != NO_ERROR) {
493 LOGE("copybit failed (%s)", strerror(err));
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700494 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700495 }
496
497 if (!copybit || err) {
498 Region::const_iterator cur = clip.begin();
499 Region::const_iterator end = clip.end();
500
501 const size_t bpp = pixelFormatTable[src->format].size;
502 const size_t dbpr = dst->stride * bpp;
503 const size_t sbpr = src->stride * bpp;
504
505 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
506 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
507
508 while (cur != end) {
509 const Rect& r(*cur++);
510 ssize_t w = r.right - r.left;
511 ssize_t h = r.bottom - r.top;
512 if (w <= 0 || h<=0) continue;
513 size_t size = w * bpp;
514 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
515 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
516 if (dbpr==sbpr && size==sbpr) {
517 size *= h;
518 h = 1;
519 }
520 do {
521 memcpy(d, s, size);
522 d += dbpr;
523 s += sbpr;
524 } while (--h > 0);
525 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700526 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700527}
528
529EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530{
Mathias Agopianabac0102009-07-31 14:47:00 -0700531 if (!buffer) {
532 return setError(EGL_BAD_ACCESS, EGL_FALSE);
533 }
534
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700535 /*
536 * Handle eglSetSwapRectangleANDROID()
537 * We copyback from the front buffer
538 */
539 if (!dirtyRegion.isEmpty()) {
540 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
541 if (previousBuffer) {
542 const Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
543 if (!copyBack.isEmpty()) {
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700544 void* prevBits;
545 if (lock(previousBuffer,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700546 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
547 // copy from previousBuffer to buffer
548 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700549 unlock(previousBuffer);
550 }
551 }
552 }
553 oldDirtyRegion = dirtyRegion;
554 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700555
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700556 if (previousBuffer) {
557 previousBuffer->common.decRef(&previousBuffer->common);
558 previousBuffer = 0;
559 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700560
Mathias Agopiandff8e582009-05-04 14:17:04 -0700561 unlock(buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700562 previousBuffer = buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700563 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700564 buffer = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700565
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700566 // dequeue a new buffer
Mathias Agopian1473f462009-04-10 14:24:30 -0700567 nativeWindow->dequeueBuffer(nativeWindow, &buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700568
Mathias Agopian1473f462009-04-10 14:24:30 -0700569 // TODO: lockBuffer should rather be executed when the very first
570 // direct rendering occurs.
571 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700572
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700573 // reallocate the depth-buffer if needed
Mathias Agopian1473f462009-04-10 14:24:30 -0700574 if ((width != buffer->width) || (height != buffer->height)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 // TODO: we probably should reset the swap rect here
576 // if the window size has changed
Mathias Agopian1473f462009-04-10 14:24:30 -0700577 width = buffer->width;
578 height = buffer->height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 if (depth.data) {
580 free(depth.data);
Mathias Agopian1473f462009-04-10 14:24:30 -0700581 depth.width = width;
582 depth.height = height;
583 depth.stride = buffer->stride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
585 if (depth.data == 0) {
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700586 setError(EGL_BAD_ALLOC, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 return EGL_FALSE;
588 }
589 }
590 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700591
592 // keep a reference on the buffer
593 buffer->common.incRef(&buffer->common);
594
595 // finally pin the buffer down
596 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
597 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
598 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
599 buffer, buffer->width, buffer->height);
Mathias Agopianabac0102009-07-31 14:47:00 -0700600 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700601 // FIXME: we should make sure we're not accessing the buffer anymore
602 }
603
604 return EGL_TRUE;
605}
606
607EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
608 EGLint l, EGLint t, EGLint w, EGLint h)
609{
610 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 return EGL_TRUE;
612}
613
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700614EGLClientBuffer egl_window_surface_v2_t::getRenderBuffer() const
615{
616 return buffer;
617}
618
Mathias Agopian1473f462009-04-10 14:24:30 -0700619#ifdef LIBAGL_USE_GRALLOC_COPYBITS
620
621static bool supportedCopybitsDestinationFormat(int format) {
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700622 // Hardware supported
Mathias Agopian1473f462009-04-10 14:24:30 -0700623 switch (format) {
624 case HAL_PIXEL_FORMAT_RGB_565:
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700625 case HAL_PIXEL_FORMAT_RGBA_8888:
626 case HAL_PIXEL_FORMAT_RGBA_4444:
627 case HAL_PIXEL_FORMAT_RGBA_5551:
628 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian1473f462009-04-10 14:24:30 -0700629 return true;
Mathias Agopian1473f462009-04-10 14:24:30 -0700630 }
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700631 return false;
Mathias Agopian1473f462009-04-10 14:24:30 -0700632}
633#endif
634
635EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636{
637 GGLSurface buffer;
638 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700639 buffer.width = this->buffer->width;
640 buffer.height = this->buffer->height;
641 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700642 buffer.data = (GGLubyte*)bits;
Mathias Agopian1473f462009-04-10 14:24:30 -0700643 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 gl->rasterizer.procs.colorBuffer(gl, &buffer);
645 if (depth.data != gl->rasterizer.state.buffers.depth.data)
646 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian350d6512009-06-10 16:01:54 -0700647
Mathias Agopian1473f462009-04-10 14:24:30 -0700648#ifdef LIBAGL_USE_GRALLOC_COPYBITS
Mathias Agopian350d6512009-06-10 16:01:54 -0700649 gl->copybits.drawSurfaceBuffer = 0;
Mathias Agopiana2fb72e2009-07-15 18:53:32 -0700650 if (gl->copybits.blitEngine != NULL) {
651 if (supportedCopybitsDestinationFormat(buffer.format)) {
652 buffer_handle_t handle = this->buffer->handle;
653 if (handle != NULL) {
Mathias Agopian36d01842009-11-02 17:48:33 -0800654 gl->copybits.drawSurfaceBuffer = this->buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700655 }
656 }
657 }
658#endif // LIBAGL_USE_GRALLOC_COPYBITS
Mathias Agopian350d6512009-06-10 16:01:54 -0700659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 return EGL_TRUE;
661}
Mathias Agopian1473f462009-04-10 14:24:30 -0700662EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663{
664 GGLSurface buffer;
665 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700666 buffer.width = this->buffer->width;
667 buffer.height = this->buffer->height;
668 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700669 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian1473f462009-04-10 14:24:30 -0700670 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 gl->rasterizer.procs.readBuffer(gl, &buffer);
672 return EGL_TRUE;
673}
Mathias Agopian1473f462009-04-10 14:24:30 -0700674EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
676}
Mathias Agopian1473f462009-04-10 14:24:30 -0700677EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
679}
Mathias Agopian1473f462009-04-10 14:24:30 -0700680EGLint egl_window_surface_v2_t::getRefreshRate() const {
681 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700683EGLint egl_window_surface_v2_t::getSwapBehavior() const
684{
685 /*
686 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
687 * the content of the swapped buffer.
688 *
689 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
690 *
691 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
692 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
693 * is, everything outside of this area is preserved.
694 *
695 * This implementation of EGL assumes the later case.
696 *
697 */
698
Mathias Agopian1473f462009-04-10 14:24:30 -0700699 return EGL_BUFFER_DESTROYED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700}
701
702// ----------------------------------------------------------------------------
703
704struct egl_pixmap_surface_t : public egl_surface_t
705{
706 egl_pixmap_surface_t(
707 EGLDisplay dpy, EGLConfig config,
708 int32_t depthFormat,
709 egl_native_pixmap_t const * pixmap);
710
711 virtual ~egl_pixmap_surface_t() { }
712
Mathias Agopianffbc8642009-08-20 00:12:56 -0700713 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
715 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
716 virtual EGLint getWidth() const { return nativePixmap.width; }
717 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718private:
719 egl_native_pixmap_t nativePixmap;
720};
721
722egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
723 EGLConfig config,
724 int32_t depthFormat,
725 egl_native_pixmap_t const * pixmap)
726 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
727{
728 if (depthFormat) {
729 depth.width = pixmap->width;
730 depth.height = pixmap->height;
731 depth.stride = depth.width; // use the width here
732 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
733 if (depth.data == 0) {
734 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
737}
738EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
739{
740 GGLSurface buffer;
741 buffer.version = sizeof(GGLSurface);
742 buffer.width = nativePixmap.width;
743 buffer.height = nativePixmap.height;
744 buffer.stride = nativePixmap.stride;
745 buffer.data = nativePixmap.data;
746 buffer.format = nativePixmap.format;
Mathias Agopian1473f462009-04-10 14:24:30 -0700747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 gl->rasterizer.procs.colorBuffer(gl, &buffer);
749 if (depth.data != gl->rasterizer.state.buffers.depth.data)
750 gl->rasterizer.procs.depthBuffer(gl, &depth);
751 return EGL_TRUE;
752}
753EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
754{
755 GGLSurface buffer;
756 buffer.version = sizeof(GGLSurface);
757 buffer.width = nativePixmap.width;
758 buffer.height = nativePixmap.height;
759 buffer.stride = nativePixmap.stride;
760 buffer.data = nativePixmap.data;
761 buffer.format = nativePixmap.format;
762 gl->rasterizer.procs.readBuffer(gl, &buffer);
763 return EGL_TRUE;
764}
765
766// ----------------------------------------------------------------------------
767
768struct egl_pbuffer_surface_t : public egl_surface_t
769{
770 egl_pbuffer_surface_t(
771 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
772 int32_t w, int32_t h, int32_t f);
773
774 virtual ~egl_pbuffer_surface_t();
775
Mathias Agopianffbc8642009-08-20 00:12:56 -0700776 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
778 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
779 virtual EGLint getWidth() const { return pbuffer.width; }
780 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781private:
782 GGLSurface pbuffer;
783};
784
785egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
786 EGLConfig config, int32_t depthFormat,
787 int32_t w, int32_t h, int32_t f)
788 : egl_surface_t(dpy, config, depthFormat)
789{
790 size_t size = w*h;
791 switch (f) {
792 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
793 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
794 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
795 default:
796 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
797 pbuffer.data = 0;
798 break;
799 }
800 pbuffer.version = sizeof(GGLSurface);
801 pbuffer.width = w;
802 pbuffer.height = h;
803 pbuffer.stride = w;
804 pbuffer.data = (GGLubyte*)malloc(size);
805 pbuffer.format = f;
Mathias Agopian1473f462009-04-10 14:24:30 -0700806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 if (depthFormat) {
808 depth.width = pbuffer.width;
809 depth.height = pbuffer.height;
810 depth.stride = depth.width; // use the width here
811 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
812 if (depth.data == 0) {
813 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
814 return;
815 }
816 }
817}
818egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
819 free(pbuffer.data);
820}
821EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
822{
823 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
824 if (depth.data != gl->rasterizer.state.buffers.depth.data)
825 gl->rasterizer.procs.depthBuffer(gl, &depth);
826 return EGL_TRUE;
827}
828EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
829{
830 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
831 return EGL_TRUE;
832}
833
834// ----------------------------------------------------------------------------
835
836struct config_pair_t {
837 GLint key;
838 GLint value;
839};
840
841struct configs_t {
842 const config_pair_t* array;
843 int size;
844};
845
846struct config_management_t {
847 GLint key;
848 bool (*match)(GLint reqValue, GLint confValue);
849 static bool atLeast(GLint reqValue, GLint confValue) {
850 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
851 }
852 static bool exact(GLint reqValue, GLint confValue) {
853 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
854 }
855 static bool mask(GLint reqValue, GLint confValue) {
856 return (confValue & reqValue) == reqValue;
857 }
858};
859
860// ----------------------------------------------------------------------------
861
862#define VERSION_MAJOR 1
863#define VERSION_MINOR 2
864static char const * const gVendorString = "Google Inc.";
865static char const * const gVersionString = "1.2 Android Driver";
866static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian1473f462009-04-10 14:24:30 -0700867static char const * const gExtensionsString =
Mathias Agopian927d37c2009-05-06 23:47:08 -0700868 "EGL_KHR_image_base "
Mathias Agopian1473f462009-04-10 14:24:30 -0700869 // "KHR_image_pixmap "
870 "EGL_ANDROID_image_native_buffer "
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700871 "EGL_ANDROID_swap_rectangle "
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700872 "EGL_ANDROID_get_render_buffer "
Mathias Agopian1473f462009-04-10 14:24:30 -0700873 ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874
875// ----------------------------------------------------------------------------
876
877struct extention_map_t {
878 const char * const name;
879 __eglMustCastToProperFunctionPointerType address;
880};
881
882static const extention_map_t gExtentionMap[] = {
Mathias Agopian1473f462009-04-10 14:24:30 -0700883 { "glDrawTexsOES",
884 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
885 { "glDrawTexiOES",
886 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
887 { "glDrawTexfOES",
888 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
889 { "glDrawTexxOES",
890 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
891 { "glDrawTexsvOES",
892 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
893 { "glDrawTexivOES",
894 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
895 { "glDrawTexfvOES",
896 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
897 { "glDrawTexxvOES",
898 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
899 { "glQueryMatrixxOES",
900 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
901 { "glEGLImageTargetTexture2DOES",
902 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
903 { "glEGLImageTargetRenderbufferStorageOES",
904 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
905 { "glClipPlanef",
906 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
907 { "glClipPlanex",
908 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
909 { "glBindBuffer",
910 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
911 { "glBufferData",
912 (__eglMustCastToProperFunctionPointerType)&glBufferData },
913 { "glBufferSubData",
914 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
915 { "glDeleteBuffers",
916 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
917 { "glGenBuffers",
918 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700919 { "eglCreateImageKHR",
920 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
921 { "eglDestroyImageKHR",
922 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
923 { "eglSetSwapRectangleANDROID",
924 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
925 { "eglGetRenderBufferANDROID",
926 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927};
928
Mathias Agopian1473f462009-04-10 14:24:30 -0700929/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 * In the lists below, attributes names MUST be sorted.
931 * Additionally, all configs must be sorted according to
932 * the EGL specification.
933 */
934
935static config_pair_t const config_base_attribute_list[] = {
936 { EGL_STENCIL_SIZE, 0 },
937 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
938 { EGL_LEVEL, 0 },
939 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian1473f462009-04-10 14:24:30 -0700940 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
942 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
943 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
944 { EGL_NATIVE_VISUAL_ID, 0 },
945 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
946 { EGL_SAMPLES, 0 },
947 { EGL_SAMPLE_BUFFERS, 0 },
948 { EGL_TRANSPARENT_TYPE, EGL_NONE },
949 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
950 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
951 { EGL_TRANSPARENT_RED_VALUE, 0 },
952 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
953 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
954 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700955 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700956 { EGL_LUMINANCE_SIZE, 0 },
957 { EGL_ALPHA_MASK_SIZE, 0 },
958 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700959 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700960 { EGL_CONFORMANT, 0 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961};
962
963// These configs can override the base attribute list
964// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
965
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967static config_pair_t const config_0_attribute_list[] = {
968 { EGL_BUFFER_SIZE, 16 },
969 { EGL_ALPHA_SIZE, 0 },
970 { EGL_BLUE_SIZE, 5 },
971 { EGL_GREEN_SIZE, 6 },
972 { EGL_RED_SIZE, 5 },
973 { EGL_DEPTH_SIZE, 0 },
974 { EGL_CONFIG_ID, 0 },
975 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
976};
977
978static config_pair_t const config_1_attribute_list[] = {
979 { EGL_BUFFER_SIZE, 16 },
980 { EGL_ALPHA_SIZE, 0 },
981 { EGL_BLUE_SIZE, 5 },
982 { EGL_GREEN_SIZE, 6 },
983 { EGL_RED_SIZE, 5 },
984 { EGL_DEPTH_SIZE, 16 },
985 { EGL_CONFIG_ID, 1 },
986 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
987};
988
989static config_pair_t const config_2_attribute_list[] = {
990 { EGL_BUFFER_SIZE, 32 },
991 { EGL_ALPHA_SIZE, 8 },
992 { EGL_BLUE_SIZE, 8 },
993 { EGL_GREEN_SIZE, 8 },
994 { EGL_RED_SIZE, 8 },
995 { EGL_DEPTH_SIZE, 0 },
996 { EGL_CONFIG_ID, 2 },
997 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
998};
999
1000static config_pair_t const config_3_attribute_list[] = {
1001 { EGL_BUFFER_SIZE, 32 },
1002 { EGL_ALPHA_SIZE, 8 },
1003 { EGL_BLUE_SIZE, 8 },
1004 { EGL_GREEN_SIZE, 8 },
1005 { EGL_RED_SIZE, 8 },
1006 { EGL_DEPTH_SIZE, 16 },
1007 { EGL_CONFIG_ID, 3 },
1008 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1009};
1010
1011static config_pair_t const config_4_attribute_list[] = {
1012 { EGL_BUFFER_SIZE, 8 },
1013 { EGL_ALPHA_SIZE, 8 },
1014 { EGL_BLUE_SIZE, 0 },
1015 { EGL_GREEN_SIZE, 0 },
1016 { EGL_RED_SIZE, 0 },
1017 { EGL_DEPTH_SIZE, 0 },
1018 { EGL_CONFIG_ID, 4 },
1019 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1020};
1021
1022static config_pair_t const config_5_attribute_list[] = {
1023 { EGL_BUFFER_SIZE, 8 },
1024 { EGL_ALPHA_SIZE, 8 },
1025 { EGL_BLUE_SIZE, 0 },
1026 { EGL_GREEN_SIZE, 0 },
1027 { EGL_RED_SIZE, 0 },
1028 { EGL_DEPTH_SIZE, 16 },
1029 { EGL_CONFIG_ID, 5 },
1030 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1031};
1032
1033static configs_t const gConfigs[] = {
1034 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1035 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1036 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1037 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1038 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1039 { config_5_attribute_list, NELEM(config_5_attribute_list) },
1040};
1041
1042static config_management_t const gConfigManagement[] = {
1043 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1044 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1045 { EGL_BLUE_SIZE, config_management_t::atLeast },
1046 { EGL_GREEN_SIZE, config_management_t::atLeast },
1047 { EGL_RED_SIZE, config_management_t::atLeast },
1048 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1049 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1050 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1051 { EGL_CONFIG_ID, config_management_t::exact },
1052 { EGL_LEVEL, config_management_t::exact },
1053 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::exact },
1054 { EGL_MAX_PBUFFER_PIXELS, config_management_t::exact },
1055 { EGL_MAX_PBUFFER_WIDTH, config_management_t::exact },
1056 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
1057 { EGL_NATIVE_VISUAL_ID, config_management_t::exact },
1058 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1059 { EGL_SAMPLES, config_management_t::exact },
1060 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1061 { EGL_SURFACE_TYPE, config_management_t::mask },
1062 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1063 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1064 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1065 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1066 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1067 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1068 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1069 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001070 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1071 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1072 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1073 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1074 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075};
1076
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078static config_pair_t const config_defaults[] = {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001079 // attributes that are not specified are simply ignored, if a particular
1080 // one needs not be ignored, it must be specified here, eg:
1081 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082};
1083
1084// ----------------------------------------------------------------------------
1085
1086template<typename T>
1087static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1088{
1089 while (first <= last) {
1090 int mid = (first + last) / 2;
Mathias Agopian1473f462009-04-10 14:24:30 -07001091 if (key > sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 first = mid + 1;
Mathias Agopian1473f462009-04-10 14:24:30 -07001093 } else if (key < sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 last = mid - 1;
1095 } else {
1096 return mid;
1097 }
1098 }
1099 return -1;
1100}
1101
1102static int isAttributeMatching(int i, EGLint attr, EGLint val)
1103{
1104 // look for the attribute in all of our configs
Mathias Agopian1473f462009-04-10 14:24:30 -07001105 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 int index = binarySearch<config_pair_t>(
1107 gConfigs[i].array,
1108 0, gConfigs[i].size-1,
1109 attr);
1110 if (index < 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001111 configFound = config_base_attribute_list;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 index = binarySearch<config_pair_t>(
1113 config_base_attribute_list,
1114 0, NELEM(config_base_attribute_list)-1,
1115 attr);
1116 }
1117 if (index >= 0) {
1118 // attribute found, check if this config could match
1119 int cfgMgtIndex = binarySearch<config_management_t>(
1120 gConfigManagement,
1121 0, NELEM(gConfigManagement)-1,
1122 attr);
1123 if (index >= 0) {
1124 bool match = gConfigManagement[cfgMgtIndex].match(
1125 val, configFound[index].value);
1126 if (match) {
1127 // this config matches
1128 return 1;
1129 }
1130 } else {
1131 // attribute not found. this should NEVER happen.
1132 }
1133 } else {
1134 // error, this attribute doesn't exist
1135 }
1136 return 0;
1137}
1138
1139static int makeCurrent(ogles_context_t* gl)
1140{
1141 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1142 if (gl) {
1143 egl_context_t* c = egl_context_t::context(gl);
1144 if (c->flags & egl_context_t::IS_CURRENT) {
1145 if (current != gl) {
1146 // it is an error to set a context current, if it's already
1147 // current to another thread
1148 return -1;
1149 }
1150 } else {
1151 if (current) {
1152 // mark the current context as not current, and flush
1153 glFlush();
1154 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1155 }
1156 }
1157 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1158 // The context is not current, make it current!
1159 setGlThreadSpecific(gl);
1160 c->flags |= egl_context_t::IS_CURRENT;
1161 }
1162 } else {
1163 if (current) {
1164 // mark the current context as not current, and flush
1165 glFlush();
1166 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1167 }
1168 // this thread has no context attached to it
1169 setGlThreadSpecific(0);
1170 }
1171 return 0;
1172}
1173
1174static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1175 EGLint attribute, EGLint *value)
1176{
1177 size_t numConfigs = NELEM(gConfigs);
1178 int index = (int)config;
1179 if (uint32_t(index) >= numConfigs)
1180 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1181
1182 int attrIndex;
1183 attrIndex = binarySearch<config_pair_t>(
1184 gConfigs[index].array,
1185 0, gConfigs[index].size-1,
1186 attribute);
1187 if (attrIndex>=0) {
1188 *value = gConfigs[index].array[attrIndex].value;
1189 return EGL_TRUE;
1190 }
1191
1192 attrIndex = binarySearch<config_pair_t>(
1193 config_base_attribute_list,
1194 0, NELEM(config_base_attribute_list)-1,
1195 attribute);
1196 if (attrIndex>=0) {
1197 *value = config_base_attribute_list[attrIndex].value;
1198 return EGL_TRUE;
1199 }
1200 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1201}
1202
1203static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1204 NativeWindowType window, const EGLint *attrib_list)
1205{
1206 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1207 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1208 if (window == 0)
1209 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1210
1211 EGLint surfaceType;
1212 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1213 return EGL_FALSE;
1214
1215 if (!(surfaceType & EGL_WINDOW_BIT))
1216 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1217
Mathias Agopianffbc8642009-08-20 00:12:56 -07001218 if (static_cast<android_native_window_t*>(window)->common.magic !=
1219 ANDROID_NATIVE_WINDOW_MAGIC) {
1220 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1221 }
1222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 EGLint configID;
1224 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1225 return EGL_FALSE;
1226
1227 int32_t depthFormat;
1228 int32_t pixelFormat;
1229 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001230 case 0:
1231 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 depthFormat = 0;
1233 break;
1234 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -07001235 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1237 break;
1238 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -07001239 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 depthFormat = 0;
1241 break;
1242 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -07001243 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1245 break;
1246 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -07001247 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 depthFormat = 0;
1249 break;
1250 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -07001251 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1253 break;
1254 default:
1255 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1256 }
1257
1258 // FIXME: we don't have access to the pixelFormat here just yet.
1259 // (it's possible that the surface is not fully initialized)
1260 // maybe this should be done after the page-flip
1261 //if (EGLint(info.format) != pixelFormat)
1262 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1263
Mathias Agopian1473f462009-04-10 14:24:30 -07001264 egl_surface_t* surface;
1265 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
1266 static_cast<android_native_window_t*>(window));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267
Mathias Agopianffbc8642009-08-20 00:12:56 -07001268 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 // there was a problem in the ctor, the error
1270 // flag has been set.
1271 delete surface;
1272 surface = 0;
1273 }
1274 return surface;
1275}
1276
1277static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1278 NativePixmapType pixmap, const EGLint *attrib_list)
1279{
1280 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1281 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1282 if (pixmap == 0)
1283 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1284
1285 EGLint surfaceType;
1286 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1287 return EGL_FALSE;
1288
1289 if (!(surfaceType & EGL_PIXMAP_BIT))
1290 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1291
Mathias Agopianffbc8642009-08-20 00:12:56 -07001292 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1293 sizeof(egl_native_pixmap_t)) {
1294 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1295 }
1296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 EGLint configID;
1298 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1299 return EGL_FALSE;
1300
1301 int32_t depthFormat;
1302 int32_t pixelFormat;
1303 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001304 case 0:
1305 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 depthFormat = 0;
1307 break;
1308 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -07001309 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1311 break;
1312 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -07001313 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 depthFormat = 0;
1315 break;
1316 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -07001317 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1319 break;
1320 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -07001321 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 depthFormat = 0;
1323 break;
1324 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -07001325 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1327 break;
1328 default:
1329 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1330 }
1331
1332 if (pixmap->format != pixelFormat)
1333 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1334
1335 egl_surface_t* surface =
1336 new egl_pixmap_surface_t(dpy, config, depthFormat,
1337 static_cast<egl_native_pixmap_t*>(pixmap));
1338
Mathias Agopianffbc8642009-08-20 00:12:56 -07001339 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // there was a problem in the ctor, the error
1341 // flag has been set.
1342 delete surface;
1343 surface = 0;
1344 }
1345 return surface;
1346}
1347
1348static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1349 const EGLint *attrib_list)
1350{
1351 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1352 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1353
1354 EGLint surfaceType;
1355 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1356 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 if (!(surfaceType & EGL_PBUFFER_BIT))
1359 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 EGLint configID;
1362 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1363 return EGL_FALSE;
1364
1365 int32_t depthFormat;
1366 int32_t pixelFormat;
1367 switch(configID) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001368 case 0:
1369 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 depthFormat = 0;
1371 break;
1372 case 1:
Mathias Agopian1473f462009-04-10 14:24:30 -07001373 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1375 break;
1376 case 2:
Mathias Agopian1473f462009-04-10 14:24:30 -07001377 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 depthFormat = 0;
1379 break;
1380 case 3:
Mathias Agopian1473f462009-04-10 14:24:30 -07001381 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1383 break;
1384 case 4:
Mathias Agopian1473f462009-04-10 14:24:30 -07001385 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 depthFormat = 0;
1387 break;
1388 case 5:
Mathias Agopian1473f462009-04-10 14:24:30 -07001389 pixelFormat = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1391 break;
1392 default:
1393 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1394 }
1395
1396 int32_t w = 0;
1397 int32_t h = 0;
1398 while (attrib_list[0]) {
1399 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1400 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1401 attrib_list+=2;
1402 }
1403
1404 egl_surface_t* surface =
1405 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1406
Mathias Agopianffbc8642009-08-20 00:12:56 -07001407 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 // there was a problem in the ctor, the error
1409 // flag has been set.
1410 delete surface;
1411 surface = 0;
1412 }
1413 return surface;
1414}
1415
1416// ----------------------------------------------------------------------------
1417}; // namespace android
1418// ----------------------------------------------------------------------------
1419
1420using namespace android;
1421
1422// ----------------------------------------------------------------------------
1423// Initialization
1424// ----------------------------------------------------------------------------
1425
1426EGLDisplay eglGetDisplay(NativeDisplayType display)
1427{
1428#ifndef HAVE_ANDROID_OS
1429 // this just needs to be done once
1430 if (gGLKey == -1) {
1431 pthread_mutex_lock(&gInitMutex);
1432 if (gGLKey == -1)
1433 pthread_key_create(&gGLKey, NULL);
1434 pthread_mutex_unlock(&gInitMutex);
1435 }
1436#endif
1437 if (display == EGL_DEFAULT_DISPLAY) {
1438 EGLDisplay dpy = (EGLDisplay)1;
1439 egl_display_t& d = egl_display_t::get_display(dpy);
1440 d.type = display;
1441 return dpy;
Mathias Agopian1473f462009-04-10 14:24:30 -07001442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 return EGL_NO_DISPLAY;
1444}
1445
1446EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1447{
1448 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1449 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 EGLBoolean res = EGL_TRUE;
1452 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -07001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 if (android_atomic_inc(&d.initialized) == 0) {
1455 // initialize stuff here if needed
1456 //pthread_mutex_lock(&gInitMutex);
1457 //pthread_mutex_unlock(&gInitMutex);
1458 }
1459
1460 if (res == EGL_TRUE) {
1461 if (major != NULL) *major = VERSION_MAJOR;
1462 if (minor != NULL) *minor = VERSION_MINOR;
1463 }
1464 return res;
1465}
1466
1467EGLBoolean eglTerminate(EGLDisplay dpy)
1468{
1469 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1470 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1471
1472 EGLBoolean res = EGL_TRUE;
1473 egl_display_t& d = egl_display_t::get_display(dpy);
1474 if (android_atomic_dec(&d.initialized) == 1) {
1475 // TODO: destroy all resources (surfaces, contexts, etc...)
1476 //pthread_mutex_lock(&gInitMutex);
1477 //pthread_mutex_unlock(&gInitMutex);
1478 }
1479 return res;
1480}
1481
1482// ----------------------------------------------------------------------------
1483// configuration
1484// ----------------------------------------------------------------------------
1485
1486EGLBoolean eglGetConfigs( EGLDisplay dpy,
1487 EGLConfig *configs,
1488 EGLint config_size, EGLint *num_config)
1489{
1490 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1491 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1492
1493 GLint numConfigs = NELEM(gConfigs);
1494 if (!configs) {
1495 *num_config = numConfigs;
1496 return EGL_TRUE;
1497 }
1498 GLint i;
1499 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1500 *configs++ = (EGLConfig)i;
1501 }
1502 *num_config = i;
1503 return EGL_TRUE;
1504}
1505
1506EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1507 EGLConfig *configs, EGLint config_size,
1508 EGLint *num_config)
1509{
1510 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1511 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich1badb712009-03-25 15:12:17 -07001512
1513 if (ggl_unlikely(num_config==0)) {
1514 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
Jack Palevich1badb712009-03-25 15:12:17 -07001517 if (ggl_unlikely(attrib_list==0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 *num_config = 0;
1519 return EGL_TRUE;
1520 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 int numAttributes = 0;
1523 int numConfigs = NELEM(gConfigs);
1524 uint32_t possibleMatch = (1<<numConfigs)-1;
1525 while(possibleMatch && *attrib_list != EGL_NONE) {
1526 numAttributes++;
1527 EGLint attr = *attrib_list++;
1528 EGLint val = *attrib_list++;
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001529 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (!(possibleMatch & (1<<i)))
1531 continue;
1532 if (isAttributeMatching(i, attr, val) == 0) {
1533 possibleMatch &= ~(1<<i);
1534 }
1535 }
1536 }
1537
1538 // now, handle the attributes which have a useful default value
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001539 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1540 // see if this attribute was specified, if not, apply its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 // default value
1542 if (binarySearch<config_pair_t>(
1543 (config_pair_t const*)attrib_list,
Mathias Agopianab1cf3e2009-07-09 17:33:15 -07001544 0, numAttributes-1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 config_defaults[j].key) < 0)
1546 {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001547 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 if (!(possibleMatch & (1<<i)))
1549 continue;
1550 if (isAttributeMatching(i,
1551 config_defaults[j].key,
1552 config_defaults[j].value) == 0)
1553 {
1554 possibleMatch &= ~(1<<i);
1555 }
1556 }
1557 }
1558 }
1559
1560 // return the configurations found
1561 int n=0;
1562 if (possibleMatch) {
Jack Palevich1badb712009-03-25 15:12:17 -07001563 if (configs) {
1564 for (int i=0 ; config_size && i<numConfigs ; i++) {
1565 if (possibleMatch & (1<<i)) {
1566 *configs++ = (EGLConfig)i;
1567 config_size--;
1568 n++;
1569 }
1570 }
1571 } else {
1572 for (int i=0 ; i<numConfigs ; i++) {
1573 if (possibleMatch & (1<<i)) {
1574 n++;
1575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
1577 }
1578 }
1579 *num_config = n;
1580 return EGL_TRUE;
1581}
1582
1583EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1584 EGLint attribute, EGLint *value)
1585{
1586 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1587 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1588
1589 return getConfigAttrib(dpy, config, attribute, value);
1590}
1591
1592// ----------------------------------------------------------------------------
1593// surfaces
1594// ----------------------------------------------------------------------------
1595
1596EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1597 NativeWindowType window,
1598 const EGLint *attrib_list)
1599{
1600 return createWindowSurface(dpy, config, window, attrib_list);
1601}
Mathias Agopian1473f462009-04-10 14:24:30 -07001602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1604 NativePixmapType pixmap,
1605 const EGLint *attrib_list)
1606{
1607 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1608}
1609
1610EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1611 const EGLint *attrib_list)
1612{
1613 return createPbufferSurface(dpy, config, attrib_list);
1614}
Mathias Agopian1473f462009-04-10 14:24:30 -07001615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1617{
1618 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1619 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1620 if (eglSurface != EGL_NO_SURFACE) {
1621 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopianffbc8642009-08-20 00:12:56 -07001622 if (!surface->isValid())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1624 if (surface->dpy != dpy)
1625 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001626 if (surface->ctx) {
1627 // FIXME: this surface is current check what the spec says
1628 surface->disconnect();
1629 surface->ctx = 0;
1630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 delete surface;
1632 }
1633 return EGL_TRUE;
1634}
1635
1636EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1637 EGLint attribute, EGLint *value)
1638{
1639 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1640 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1641 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001642 if (!surface->isValid())
1643 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 if (surface->dpy != dpy)
1645 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1646
1647 EGLBoolean ret = EGL_TRUE;
1648 switch (attribute) {
1649 case EGL_CONFIG_ID:
1650 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1651 break;
1652 case EGL_WIDTH:
1653 *value = surface->getWidth();
1654 break;
1655 case EGL_HEIGHT:
1656 *value = surface->getHeight();
1657 break;
1658 case EGL_LARGEST_PBUFFER:
1659 // not modified for a window or pixmap surface
1660 break;
1661 case EGL_TEXTURE_FORMAT:
1662 *value = EGL_NO_TEXTURE;
1663 break;
1664 case EGL_TEXTURE_TARGET:
1665 *value = EGL_NO_TEXTURE;
1666 break;
1667 case EGL_MIPMAP_TEXTURE:
1668 *value = EGL_FALSE;
1669 break;
1670 case EGL_MIPMAP_LEVEL:
1671 *value = 0;
1672 break;
1673 case EGL_RENDER_BUFFER:
1674 // TODO: return the real RENDER_BUFFER here
1675 *value = EGL_BACK_BUFFER;
1676 break;
1677 case EGL_HORIZONTAL_RESOLUTION:
1678 // pixel/mm * EGL_DISPLAY_SCALING
1679 *value = surface->getHorizontalResolution();
1680 break;
1681 case EGL_VERTICAL_RESOLUTION:
1682 // pixel/mm * EGL_DISPLAY_SCALING
1683 *value = surface->getVerticalResolution();
1684 break;
1685 case EGL_PIXEL_ASPECT_RATIO: {
1686 // w/h * EGL_DISPLAY_SCALING
1687 int wr = surface->getHorizontalResolution();
1688 int hr = surface->getVerticalResolution();
1689 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1690 } break;
1691 case EGL_SWAP_BEHAVIOR:
Mathias Agopian1473f462009-04-10 14:24:30 -07001692 *value = surface->getSwapBehavior();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 break;
1694 default:
1695 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1696 }
1697 return ret;
1698}
1699
1700EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1701 EGLContext share_list, const EGLint *attrib_list)
1702{
1703 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1704 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1705
1706 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1707 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1708
1709 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1710 c->flags = egl_context_t::NEVER_CURRENT;
1711 c->dpy = dpy;
1712 c->config = config;
1713 c->read = 0;
1714 c->draw = 0;
1715 return (EGLContext)gl;
1716}
1717
1718EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1719{
1720 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1721 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1722 egl_context_t* c = egl_context_t::context(ctx);
1723 if (c->flags & egl_context_t::IS_CURRENT)
1724 setGlThreadSpecific(0);
1725 ogles_uninit((ogles_context_t*)ctx);
1726 return EGL_TRUE;
1727}
1728
1729EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1730 EGLSurface read, EGLContext ctx)
1731{
1732 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1733 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1734 if (draw) {
1735 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopianffbc8642009-08-20 00:12:56 -07001736 if (!s->isValid())
1737 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 if (s->dpy != dpy)
1739 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001740 // TODO: check that draw is compatible with the context
1741 }
1742 if (read && read!=draw) {
1743 egl_surface_t* s = (egl_surface_t*)read;
1744 if (!s->isValid())
1745 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1746 if (s->dpy != dpy)
1747 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1748 // TODO: check that read is compatible with the context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750
1751 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian1473f462009-04-10 14:24:30 -07001752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1754 return setError(EGL_BAD_MATCH, EGL_FALSE);
1755
1756 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1757 return setError(EGL_BAD_MATCH, EGL_FALSE);
1758
1759 if (ctx == EGL_NO_CONTEXT) {
1760 // if we're detaching, we need the current context
1761 current_ctx = (EGLContext)getGlThreadSpecific();
1762 } else {
1763 egl_context_t* c = egl_context_t::context(ctx);
1764 egl_surface_t* d = (egl_surface_t*)draw;
1765 egl_surface_t* r = (egl_surface_t*)read;
1766 if ((d && d->ctx && d->ctx != ctx) ||
1767 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001768 // one of the surface is bound to a context in another thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1770 }
1771 }
1772
1773 ogles_context_t* gl = (ogles_context_t*)ctx;
1774 if (makeCurrent(gl) == 0) {
1775 if (ctx) {
1776 egl_context_t* c = egl_context_t::context(ctx);
1777 egl_surface_t* d = (egl_surface_t*)draw;
1778 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001779
1780 if (c->draw) {
Mathias Agopianffbc8642009-08-20 00:12:56 -07001781 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1782 s->disconnect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001783 }
1784 if (c->read) {
1785 // FIXME: unlock/disconnect the read surface too
1786 }
1787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 c->draw = draw;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001789 c->read = read;
1790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 if (c->flags & egl_context_t::NEVER_CURRENT) {
1792 c->flags &= ~egl_context_t::NEVER_CURRENT;
1793 GLint w = 0;
1794 GLint h = 0;
1795 if (draw) {
1796 w = d->getWidth();
1797 h = d->getHeight();
1798 }
1799 ogles_surfaceport(gl, 0, 0);
1800 ogles_viewport(gl, 0, 0, w, h);
1801 ogles_scissor(gl, 0, 0, w, h);
1802 }
1803 if (d) {
Mathias Agopianabac0102009-07-31 14:47:00 -07001804 if (d->connect() == EGL_FALSE) {
1805 return EGL_FALSE;
1806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 d->ctx = ctx;
1808 d->bindDrawSurface(gl);
1809 }
1810 if (r) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001811 // FIXME: lock/connect the read surface too
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 r->ctx = ctx;
1813 r->bindReadSurface(gl);
1814 }
1815 } else {
1816 // if surfaces were bound to the context bound to this thread
1817 // mark then as unbound.
1818 if (current_ctx) {
1819 egl_context_t* c = egl_context_t::context(current_ctx);
1820 egl_surface_t* d = (egl_surface_t*)c->draw;
1821 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001822 if (d) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001823 c->draw = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001824 d->ctx = EGL_NO_CONTEXT;
1825 d->disconnect();
1826 }
1827 if (r) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001828 c->read = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001829 r->ctx = EGL_NO_CONTEXT;
1830 // FIXME: unlock/disconnect the read surface too
1831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 }
1834 return EGL_TRUE;
1835 }
1836 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1837}
1838
1839EGLContext eglGetCurrentContext(void)
1840{
1841 // eglGetCurrentContext returns the current EGL rendering context,
1842 // as specified by eglMakeCurrent. If no context is current,
1843 // EGL_NO_CONTEXT is returned.
1844 return (EGLContext)getGlThreadSpecific();
1845}
1846
1847EGLSurface eglGetCurrentSurface(EGLint readdraw)
1848{
1849 // eglGetCurrentSurface returns the read or draw surface attached
1850 // to the current EGL rendering context, as specified by eglMakeCurrent.
1851 // If no context is current, EGL_NO_SURFACE is returned.
1852 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1853 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1854 egl_context_t* c = egl_context_t::context(ctx);
1855 if (readdraw == EGL_READ) {
1856 return c->read;
1857 } else if (readdraw == EGL_DRAW) {
1858 return c->draw;
1859 }
1860 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1861}
1862
1863EGLDisplay eglGetCurrentDisplay(void)
1864{
1865 // eglGetCurrentDisplay returns the current EGL display connection
1866 // for the current EGL rendering context, as specified by eglMakeCurrent.
1867 // If no context is current, EGL_NO_DISPLAY is returned.
1868 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1869 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1870 egl_context_t* c = egl_context_t::context(ctx);
1871 return c->dpy;
1872}
1873
1874EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1875 EGLint attribute, EGLint *value)
1876{
1877 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1878 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1879 egl_context_t* c = egl_context_t::context(ctx);
1880 switch (attribute) {
1881 case EGL_CONFIG_ID:
1882 // Returns the ID of the EGL frame buffer configuration with
1883 // respect to which the context was created
1884 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1885 }
1886 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1887}
1888
1889EGLBoolean eglWaitGL(void)
1890{
1891 return EGL_TRUE;
1892}
1893
1894EGLBoolean eglWaitNative(EGLint engine)
1895{
1896 return EGL_TRUE;
1897}
1898
1899EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1900{
1901 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1902 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001905 if (!d->isValid())
1906 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (d->dpy != dpy)
1908 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1909
1910 // post the surface
1911 d->swapBuffers();
1912
1913 // if it's bound to a context, update the buffer
1914 if (d->ctx != EGL_NO_CONTEXT) {
1915 d->bindDrawSurface((ogles_context_t*)d->ctx);
1916 // if this surface is also the read surface of the context
1917 // it is bound to, make sure to update the read buffer as well.
1918 // The EGL spec is a little unclear about this.
1919 egl_context_t* c = egl_context_t::context(d->ctx);
1920 if (c->read == draw) {
1921 d->bindReadSurface((ogles_context_t*)d->ctx);
1922 }
1923 }
1924
1925 return EGL_TRUE;
1926}
1927
1928EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1929 NativePixmapType target)
1930{
1931 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1932 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1933 // TODO: eglCopyBuffers()
1934 return EGL_FALSE;
1935}
1936
1937EGLint eglGetError(void)
1938{
1939 return getError();
1940}
1941
1942const char* eglQueryString(EGLDisplay dpy, EGLint name)
1943{
1944 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1945 return setError(EGL_BAD_DISPLAY, (const char*)0);
1946
1947 switch (name) {
1948 case EGL_VENDOR:
1949 return gVendorString;
1950 case EGL_VERSION:
1951 return gVersionString;
1952 case EGL_EXTENSIONS:
1953 return gExtensionsString;
1954 case EGL_CLIENT_APIS:
1955 return gClientApiString;
1956 }
1957 return setError(EGL_BAD_PARAMETER, (const char *)0);
1958}
1959
1960// ----------------------------------------------------------------------------
1961// EGL 1.1
1962// ----------------------------------------------------------------------------
1963
1964EGLBoolean eglSurfaceAttrib(
1965 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1966{
1967 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1968 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1969 // TODO: eglSurfaceAttrib()
1970 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1971}
1972
1973EGLBoolean eglBindTexImage(
1974 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1975{
1976 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1977 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1978 // TODO: eglBindTexImage()
1979 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1980}
1981
1982EGLBoolean eglReleaseTexImage(
1983 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1984{
1985 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1986 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1987 // TODO: eglReleaseTexImage()
1988 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1989}
1990
1991EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1992{
1993 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1994 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1995 // TODO: eglSwapInterval()
1996 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1997}
1998
1999// ----------------------------------------------------------------------------
2000// EGL 1.2
2001// ----------------------------------------------------------------------------
2002
2003EGLBoolean eglBindAPI(EGLenum api)
2004{
2005 if (api != EGL_OPENGL_ES_API)
2006 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2007 return EGL_TRUE;
2008}
2009
2010EGLenum eglQueryAPI(void)
2011{
2012 return EGL_OPENGL_ES_API;
2013}
2014
2015EGLBoolean eglWaitClient(void)
2016{
2017 glFinish();
2018 return EGL_TRUE;
2019}
2020
2021EGLBoolean eglReleaseThread(void)
2022{
2023 // TODO: eglReleaseThread()
2024 return EGL_TRUE;
2025}
2026
2027EGLSurface eglCreatePbufferFromClientBuffer(
2028 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2029 EGLConfig config, const EGLint *attrib_list)
2030{
2031 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2032 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2033 // TODO: eglCreatePbufferFromClientBuffer()
2034 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2035}
2036
2037// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -07002038// EGL_EGLEXT_VERSION 3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039// ----------------------------------------------------------------------------
2040
2041void (*eglGetProcAddress (const char *procname))()
2042{
2043 extention_map_t const * const map = gExtentionMap;
2044 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2045 if (!strcmp(procname, map[i].name)) {
2046 return map[i].address;
2047 }
2048 }
2049 return NULL;
2050}
Mathias Agopian1473f462009-04-10 14:24:30 -07002051
2052EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2053 const EGLint *attrib_list)
2054{
2055 EGLBoolean result = EGL_FALSE;
2056 return result;
2057}
2058
2059EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2060{
2061 EGLBoolean result = EGL_FALSE;
2062 return result;
2063}
2064
2065EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2066 EGLClientBuffer buffer, const EGLint *attrib_list)
2067{
2068 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2069 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2070 }
2071 if (ctx != EGL_NO_CONTEXT) {
2072 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2073 }
2074 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2075 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2076 }
2077
2078 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2079
2080 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2081 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2082
2083 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2084 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopianaf9a5152009-04-10 20:34:46 -07002085
Mathias Agopian1473f462009-04-10 14:24:30 -07002086 native_buffer->common.incRef(&native_buffer->common);
2087 return (EGLImageKHR)native_buffer;
2088}
2089
2090EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2091{
2092 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2093 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2094 }
2095
2096 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2097
2098 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2099 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2100
2101 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2102 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2103
Mathias Agopian1473f462009-04-10 14:24:30 -07002104 native_buffer->common.decRef(&native_buffer->common);
2105
2106 return EGL_TRUE;
2107}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002108
2109// ----------------------------------------------------------------------------
2110// ANDROID extensions
2111// ----------------------------------------------------------------------------
2112
2113EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2114 EGLint left, EGLint top, EGLint width, EGLint height)
2115{
2116 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2117 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2118
2119 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002120 if (!d->isValid())
2121 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002122 if (d->dpy != dpy)
2123 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2124
2125 // post the surface
2126 d->setSwapRectangle(left, top, width, height);
2127
2128 return EGL_TRUE;
2129}
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002130
2131EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
2132{
2133 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2134 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2135
2136 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002137 if (!d->isValid())
2138 return setError(EGL_BAD_SURFACE, (EGLClientBuffer)0);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002139 if (d->dpy != dpy)
2140 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2141
2142 // post the surface
2143 return d->getRenderBuffer();
2144}