blob: a1cb23a250b9e95d7c467b45800a078be476045b [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);
Jamie Gennisae2aa282011-01-30 15:59:36 -080085 if (error == 0) {
86 // The TLS key has been created by another thread, but the value for
87 // this thread has not been initialized.
88 return EGL_SUCCESS;
89 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
91 return error;
92}
93
94// ----------------------------------------------------------------------------
95
96struct egl_display_t
97{
98 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian1473f462009-04-10 14:24:30 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 static EGLBoolean is_valid(EGLDisplay dpy) {
103 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
104 }
105
106 NativeDisplayType type;
107 volatile int32_t initialized;
108};
109
110static egl_display_t gDisplays[NUM_DISPLAYS];
111
112egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
113 return gDisplays[uintptr_t(dpy)-1U];
114}
115
116struct egl_context_t {
117 enum {
118 IS_CURRENT = 0x00010000,
119 NEVER_CURRENT = 0x00020000
120 };
121 uint32_t flags;
122 EGLDisplay dpy;
123 EGLConfig config;
124 EGLSurface read;
125 EGLSurface draw;
126
127 static inline egl_context_t* context(EGLContext ctx) {
128 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
129 return static_cast<egl_context_t*>(gl->rasterizer.base);
130 }
131};
132
133// ----------------------------------------------------------------------------
134
135struct egl_surface_t
136{
137 enum {
138 PAGE_FLIP = 0x00000001,
139 MAGIC = 0x31415265
140 };
141
142 uint32_t magic;
143 EGLDisplay dpy;
144 EGLConfig config;
145 EGLContext ctx;
146
147 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
148 virtual ~egl_surface_t();
Mathias Agopianffbc8642009-08-20 00:12:56 -0700149 bool isValid() const;
150 virtual bool initCheck() const = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
153 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopianabac0102009-07-31 14:47:00 -0700154 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700155 virtual void disconnect() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 virtual EGLint getWidth() const = 0;
157 virtual EGLint getHeight() const = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 virtual EGLint getHorizontalResolution() const;
160 virtual EGLint getVerticalResolution() const;
161 virtual EGLint getRefreshRate() const;
162 virtual EGLint getSwapBehavior() const;
163 virtual EGLBoolean swapBuffers();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700164 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165protected:
166 GGLSurface depth;
167};
168
169egl_surface_t::egl_surface_t(EGLDisplay dpy,
170 EGLConfig config,
171 int32_t depthFormat)
172 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
173{
174 depth.version = sizeof(GGLSurface);
175 depth.data = 0;
176 depth.format = depthFormat;
177}
178egl_surface_t::~egl_surface_t()
179{
180 magic = 0;
181 free(depth.data);
182}
Mathias Agopianffbc8642009-08-20 00:12:56 -0700183bool egl_surface_t::isValid() const {
184 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
185 return magic == MAGIC;
186}
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188EGLBoolean egl_surface_t::swapBuffers() {
189 return EGL_FALSE;
190}
191EGLint egl_surface_t::getHorizontalResolution() const {
192 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
193}
194EGLint egl_surface_t::getVerticalResolution() const {
195 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
196}
197EGLint egl_surface_t::getRefreshRate() const {
198 return (60 * EGL_DISPLAY_SCALING);
199}
200EGLint egl_surface_t::getSwapBehavior() const {
201 return EGL_BUFFER_PRESERVED;
202}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700203EGLBoolean egl_surface_t::setSwapRectangle(
204 EGLint l, EGLint t, EGLint w, EGLint h)
205{
206 return EGL_FALSE;
207}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209// ----------------------------------------------------------------------------
210
Mathias Agopian1473f462009-04-10 14:24:30 -0700211struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212{
Mathias Agopian1473f462009-04-10 14:24:30 -0700213 egl_window_surface_v2_t(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 EGLDisplay dpy, EGLConfig config,
215 int32_t depthFormat,
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700216 ANativeWindow* window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Mathias Agopianffbc8642009-08-20 00:12:56 -0700218 ~egl_window_surface_v2_t();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Mathias Agopianffbc8642009-08-20 00:12:56 -0700220 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 virtual EGLBoolean swapBuffers();
222 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
223 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopianabac0102009-07-31 14:47:00 -0700224 virtual EGLBoolean connect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700225 virtual void disconnect();
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700226 virtual EGLint getWidth() const { return width; }
227 virtual EGLint getHeight() const { return height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 virtual EGLint getHorizontalResolution() const;
229 virtual EGLint getVerticalResolution() const;
230 virtual EGLint getRefreshRate() const;
231 virtual EGLint getSwapBehavior() const;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700232 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700233
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);
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700237 ANativeWindow* nativeWindow;
Mathias Agopian1473f462009-04-10 14:24:30 -0700238 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,
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700357 ANativeWindow* 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 Agopianbbf945f2009-11-03 20:38:08 -0800452 int err;
Mathias Agopianb5393dc2010-12-08 17:44:07 -0800453
454 err = module->lock(module, buf->handle,
455 usage, 0, 0, buf->width, buf->height, vaddr);
456
Mathias Agopiandff8e582009-05-04 14:17:04 -0700457 return err;
458}
459
460status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
461{
Mathias Agopianabac0102009-07-31 14:47:00 -0700462 if (!buf) return BAD_VALUE;
Mathias Agopianbbf945f2009-11-03 20:38:08 -0800463 int err = NO_ERROR;
Mathias Agopianb5393dc2010-12-08 17:44:07 -0800464
465 err = module->unlock(module, buf->handle);
466
Mathias Agopiandff8e582009-05-04 14:17:04 -0700467 return err;
468}
469
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700470void egl_window_surface_v2_t::copyBlt(
471 android_native_buffer_t* dst, void* dst_vaddr,
472 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700473 const Region& clip)
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700474{
475 // FIXME: use copybit if possible
476 // NOTE: dst and src must be the same format
477
Mathias Agopian68eeb802009-06-25 15:39:25 -0700478 status_t err = NO_ERROR;
479 copybit_device_t* const copybit = blitengine;
480 if (copybit) {
481 copybit_image_t simg;
Mathias Palmqvist89995152010-06-02 16:03:04 +0200482 simg.w = src->stride;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700483 simg.h = src->height;
484 simg.format = src->format;
485 simg.handle = const_cast<native_handle_t*>(src->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700486
Mathias Agopian68eeb802009-06-25 15:39:25 -0700487 copybit_image_t dimg;
Mathias Palmqvist89995152010-06-02 16:03:04 +0200488 dimg.w = dst->stride;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700489 dimg.h = dst->height;
490 dimg.format = dst->format;
491 dimg.handle = const_cast<native_handle_t*>(dst->handle);
492
493 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
494 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
495 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
496 region_iterator it(clip);
497 err = copybit->blit(copybit, &dimg, &simg, &it);
498 if (err != NO_ERROR) {
499 LOGE("copybit failed (%s)", strerror(err));
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700500 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700501 }
502
503 if (!copybit || err) {
504 Region::const_iterator cur = clip.begin();
505 Region::const_iterator end = clip.end();
506
507 const size_t bpp = pixelFormatTable[src->format].size;
508 const size_t dbpr = dst->stride * bpp;
509 const size_t sbpr = src->stride * bpp;
510
511 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
512 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
513
514 while (cur != end) {
515 const Rect& r(*cur++);
516 ssize_t w = r.right - r.left;
517 ssize_t h = r.bottom - r.top;
518 if (w <= 0 || h<=0) continue;
519 size_t size = w * bpp;
520 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
521 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
522 if (dbpr==sbpr && size==sbpr) {
523 size *= h;
524 h = 1;
525 }
526 do {
527 memcpy(d, s, size);
528 d += dbpr;
529 s += sbpr;
530 } while (--h > 0);
531 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700532 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700533}
534
535EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536{
Mathias Agopianabac0102009-07-31 14:47:00 -0700537 if (!buffer) {
538 return setError(EGL_BAD_ACCESS, EGL_FALSE);
539 }
540
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700541 /*
542 * Handle eglSetSwapRectangleANDROID()
543 * We copyback from the front buffer
544 */
545 if (!dirtyRegion.isEmpty()) {
546 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
547 if (previousBuffer) {
Kristian Monsen732734a2010-10-27 17:59:09 +0100548 // This was const Region copyBack, but that causes an
549 // internal compile error on simulator builds
550 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700551 if (!copyBack.isEmpty()) {
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700552 void* prevBits;
553 if (lock(previousBuffer,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700554 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
555 // copy from previousBuffer to buffer
556 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700557 unlock(previousBuffer);
558 }
559 }
560 }
561 oldDirtyRegion = dirtyRegion;
562 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700563
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700564 if (previousBuffer) {
565 previousBuffer->common.decRef(&previousBuffer->common);
566 previousBuffer = 0;
567 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700568
Mathias Agopiandff8e582009-05-04 14:17:04 -0700569 unlock(buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700570 previousBuffer = buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700571 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700572 buffer = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700573
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700574 // dequeue a new buffer
Mathias Agopian100f42a2010-08-18 16:07:34 -0700575 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
576
577 // TODO: lockBuffer should rather be executed when the very first
578 // direct rendering occurs.
579 nativeWindow->lockBuffer(nativeWindow, buffer);
580
581 // reallocate the depth-buffer if needed
582 if ((width != buffer->width) || (height != buffer->height)) {
583 // TODO: we probably should reset the swap rect here
584 // if the window size has changed
585 width = buffer->width;
586 height = buffer->height;
587 if (depth.data) {
588 free(depth.data);
589 depth.width = width;
590 depth.height = height;
591 depth.stride = buffer->stride;
592 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
593 if (depth.data == 0) {
594 setError(EGL_BAD_ALLOC, EGL_FALSE);
595 return EGL_FALSE;
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
598 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700599
Mathias Agopian100f42a2010-08-18 16:07:34 -0700600 // keep a reference on the buffer
601 buffer->common.incRef(&buffer->common);
602
603 // finally pin the buffer down
604 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
605 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
606 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
607 buffer, buffer->width, buffer->height);
608 return setError(EGL_BAD_ACCESS, EGL_FALSE);
609 // FIXME: we should make sure we're not accessing the buffer anymore
610 }
611 } else {
612 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700613 }
614
615 return EGL_TRUE;
616}
617
618EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
619 EGLint l, EGLint t, EGLint w, EGLint h)
620{
621 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 return EGL_TRUE;
623}
624
Mathias Agopian1473f462009-04-10 14:24:30 -0700625EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626{
627 GGLSurface buffer;
628 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700629 buffer.width = this->buffer->width;
630 buffer.height = this->buffer->height;
631 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700632 buffer.data = (GGLubyte*)bits;
Mathias Agopian1473f462009-04-10 14:24:30 -0700633 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 gl->rasterizer.procs.colorBuffer(gl, &buffer);
635 if (depth.data != gl->rasterizer.state.buffers.depth.data)
636 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian350d6512009-06-10 16:01:54 -0700637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 return EGL_TRUE;
639}
Mathias Agopian1473f462009-04-10 14:24:30 -0700640EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641{
642 GGLSurface buffer;
643 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700644 buffer.width = this->buffer->width;
645 buffer.height = this->buffer->height;
646 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700647 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian1473f462009-04-10 14:24:30 -0700648 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 gl->rasterizer.procs.readBuffer(gl, &buffer);
650 return EGL_TRUE;
651}
Mathias Agopian1473f462009-04-10 14:24:30 -0700652EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
654}
Mathias Agopian1473f462009-04-10 14:24:30 -0700655EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
657}
Mathias Agopian1473f462009-04-10 14:24:30 -0700658EGLint egl_window_surface_v2_t::getRefreshRate() const {
659 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700661EGLint egl_window_surface_v2_t::getSwapBehavior() const
662{
663 /*
664 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
665 * the content of the swapped buffer.
666 *
667 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
668 *
669 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
670 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
671 * is, everything outside of this area is preserved.
672 *
673 * This implementation of EGL assumes the later case.
674 *
675 */
676
Mathias Agopian1473f462009-04-10 14:24:30 -0700677 return EGL_BUFFER_DESTROYED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678}
679
680// ----------------------------------------------------------------------------
681
682struct egl_pixmap_surface_t : public egl_surface_t
683{
684 egl_pixmap_surface_t(
685 EGLDisplay dpy, EGLConfig config,
686 int32_t depthFormat,
687 egl_native_pixmap_t const * pixmap);
688
689 virtual ~egl_pixmap_surface_t() { }
690
Mathias Agopianffbc8642009-08-20 00:12:56 -0700691 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
693 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
694 virtual EGLint getWidth() const { return nativePixmap.width; }
695 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696private:
697 egl_native_pixmap_t nativePixmap;
698};
699
700egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
701 EGLConfig config,
702 int32_t depthFormat,
703 egl_native_pixmap_t const * pixmap)
704 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
705{
706 if (depthFormat) {
707 depth.width = pixmap->width;
708 depth.height = pixmap->height;
709 depth.stride = depth.width; // use the width here
710 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
711 if (depth.data == 0) {
712 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 }
715}
716EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
717{
718 GGLSurface buffer;
719 buffer.version = sizeof(GGLSurface);
720 buffer.width = nativePixmap.width;
721 buffer.height = nativePixmap.height;
722 buffer.stride = nativePixmap.stride;
723 buffer.data = nativePixmap.data;
724 buffer.format = nativePixmap.format;
Mathias Agopian1473f462009-04-10 14:24:30 -0700725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 gl->rasterizer.procs.colorBuffer(gl, &buffer);
727 if (depth.data != gl->rasterizer.state.buffers.depth.data)
728 gl->rasterizer.procs.depthBuffer(gl, &depth);
729 return EGL_TRUE;
730}
731EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
732{
733 GGLSurface buffer;
734 buffer.version = sizeof(GGLSurface);
735 buffer.width = nativePixmap.width;
736 buffer.height = nativePixmap.height;
737 buffer.stride = nativePixmap.stride;
738 buffer.data = nativePixmap.data;
739 buffer.format = nativePixmap.format;
740 gl->rasterizer.procs.readBuffer(gl, &buffer);
741 return EGL_TRUE;
742}
743
744// ----------------------------------------------------------------------------
745
746struct egl_pbuffer_surface_t : public egl_surface_t
747{
748 egl_pbuffer_surface_t(
749 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
750 int32_t w, int32_t h, int32_t f);
751
752 virtual ~egl_pbuffer_surface_t();
753
Mathias Agopianffbc8642009-08-20 00:12:56 -0700754 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
756 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
757 virtual EGLint getWidth() const { return pbuffer.width; }
758 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759private:
760 GGLSurface pbuffer;
761};
762
763egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
764 EGLConfig config, int32_t depthFormat,
765 int32_t w, int32_t h, int32_t f)
766 : egl_surface_t(dpy, config, depthFormat)
767{
768 size_t size = w*h;
769 switch (f) {
770 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
771 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
772 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800773 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 default:
775 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
776 pbuffer.data = 0;
777 break;
778 }
779 pbuffer.version = sizeof(GGLSurface);
780 pbuffer.width = w;
781 pbuffer.height = h;
782 pbuffer.stride = w;
783 pbuffer.data = (GGLubyte*)malloc(size);
784 pbuffer.format = f;
Mathias Agopian1473f462009-04-10 14:24:30 -0700785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 if (depthFormat) {
787 depth.width = pbuffer.width;
788 depth.height = pbuffer.height;
789 depth.stride = depth.width; // use the width here
790 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
791 if (depth.data == 0) {
792 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
793 return;
794 }
795 }
796}
797egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
798 free(pbuffer.data);
799}
800EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
801{
802 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
803 if (depth.data != gl->rasterizer.state.buffers.depth.data)
804 gl->rasterizer.procs.depthBuffer(gl, &depth);
805 return EGL_TRUE;
806}
807EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
808{
809 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
810 return EGL_TRUE;
811}
812
813// ----------------------------------------------------------------------------
814
815struct config_pair_t {
816 GLint key;
817 GLint value;
818};
819
820struct configs_t {
821 const config_pair_t* array;
822 int size;
823};
824
825struct config_management_t {
826 GLint key;
827 bool (*match)(GLint reqValue, GLint confValue);
828 static bool atLeast(GLint reqValue, GLint confValue) {
829 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
830 }
831 static bool exact(GLint reqValue, GLint confValue) {
832 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
833 }
834 static bool mask(GLint reqValue, GLint confValue) {
835 return (confValue & reqValue) == reqValue;
836 }
Mathias Agopiand8e350b2010-10-25 15:51:24 -0700837 static bool ignore(GLint reqValue, GLint confValue) {
838 return true;
839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840};
841
842// ----------------------------------------------------------------------------
843
844#define VERSION_MAJOR 1
845#define VERSION_MINOR 2
846static char const * const gVendorString = "Google Inc.";
Mathias Agopianf91bff92010-10-19 14:47:08 -0700847static char const * const gVersionString = "1.2 Android Driver 1.2.0";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian1473f462009-04-10 14:24:30 -0700849static char const * const gExtensionsString =
Mathias Agopian927d37c2009-05-06 23:47:08 -0700850 "EGL_KHR_image_base "
Mathias Agopian1473f462009-04-10 14:24:30 -0700851 // "KHR_image_pixmap "
852 "EGL_ANDROID_image_native_buffer "
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700853 "EGL_ANDROID_swap_rectangle "
Mathias Agopian1473f462009-04-10 14:24:30 -0700854 ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856// ----------------------------------------------------------------------------
857
858struct extention_map_t {
859 const char * const name;
860 __eglMustCastToProperFunctionPointerType address;
861};
862
863static const extention_map_t gExtentionMap[] = {
Mathias Agopian1473f462009-04-10 14:24:30 -0700864 { "glDrawTexsOES",
865 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
866 { "glDrawTexiOES",
867 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
868 { "glDrawTexfOES",
869 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
870 { "glDrawTexxOES",
871 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
872 { "glDrawTexsvOES",
873 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
874 { "glDrawTexivOES",
875 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
876 { "glDrawTexfvOES",
877 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
878 { "glDrawTexxvOES",
879 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
880 { "glQueryMatrixxOES",
881 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
882 { "glEGLImageTargetTexture2DOES",
883 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
884 { "glEGLImageTargetRenderbufferStorageOES",
885 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
886 { "glClipPlanef",
887 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
888 { "glClipPlanex",
889 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
890 { "glBindBuffer",
891 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
892 { "glBufferData",
893 (__eglMustCastToProperFunctionPointerType)&glBufferData },
894 { "glBufferSubData",
895 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
896 { "glDeleteBuffers",
897 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
898 { "glGenBuffers",
899 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700900 { "eglCreateImageKHR",
901 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
902 { "eglDestroyImageKHR",
903 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
904 { "eglSetSwapRectangleANDROID",
905 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906};
907
Mathias Agopian1473f462009-04-10 14:24:30 -0700908/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * In the lists below, attributes names MUST be sorted.
910 * Additionally, all configs must be sorted according to
911 * the EGL specification.
912 */
913
914static config_pair_t const config_base_attribute_list[] = {
915 { EGL_STENCIL_SIZE, 0 },
916 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
917 { EGL_LEVEL, 0 },
918 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian1473f462009-04-10 14:24:30 -0700919 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
921 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
922 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
923 { EGL_NATIVE_VISUAL_ID, 0 },
924 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
925 { EGL_SAMPLES, 0 },
926 { EGL_SAMPLE_BUFFERS, 0 },
927 { EGL_TRANSPARENT_TYPE, EGL_NONE },
928 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
929 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
930 { EGL_TRANSPARENT_RED_VALUE, 0 },
931 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
932 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
933 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700934 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700935 { EGL_LUMINANCE_SIZE, 0 },
936 { EGL_ALPHA_MASK_SIZE, 0 },
937 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700938 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700939 { EGL_CONFORMANT, 0 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940};
941
942// These configs can override the base attribute list
943// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
944
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800945// 565 configs
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946static config_pair_t const config_0_attribute_list[] = {
947 { EGL_BUFFER_SIZE, 16 },
948 { EGL_ALPHA_SIZE, 0 },
949 { EGL_BLUE_SIZE, 5 },
950 { EGL_GREEN_SIZE, 6 },
951 { EGL_RED_SIZE, 5 },
952 { EGL_DEPTH_SIZE, 0 },
953 { EGL_CONFIG_ID, 0 },
Mathias Agopianc9923872010-10-20 17:21:43 -0700954 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
956};
957
958static config_pair_t const config_1_attribute_list[] = {
959 { EGL_BUFFER_SIZE, 16 },
960 { EGL_ALPHA_SIZE, 0 },
961 { EGL_BLUE_SIZE, 5 },
962 { EGL_GREEN_SIZE, 6 },
963 { EGL_RED_SIZE, 5 },
964 { EGL_DEPTH_SIZE, 16 },
965 { EGL_CONFIG_ID, 1 },
Mathias Agopianc9923872010-10-20 17:21:43 -0700966 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
968};
969
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800970// RGB 888 configs
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971static config_pair_t const config_2_attribute_list[] = {
972 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800973 { EGL_ALPHA_SIZE, 0 },
974 { EGL_BLUE_SIZE, 8 },
975 { EGL_GREEN_SIZE, 8 },
976 { EGL_RED_SIZE, 8 },
977 { EGL_DEPTH_SIZE, 0 },
978 { EGL_CONFIG_ID, 6 },
Mathias Agopianc9923872010-10-20 17:21:43 -0700979 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800980 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
981};
982
983static config_pair_t const config_3_attribute_list[] = {
984 { EGL_BUFFER_SIZE, 32 },
985 { EGL_ALPHA_SIZE, 0 },
986 { EGL_BLUE_SIZE, 8 },
987 { EGL_GREEN_SIZE, 8 },
988 { EGL_RED_SIZE, 8 },
989 { EGL_DEPTH_SIZE, 16 },
990 { EGL_CONFIG_ID, 7 },
Mathias Agopianc9923872010-10-20 17:21:43 -0700991 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800992 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
993};
994
995// 8888 configs
996static config_pair_t const config_4_attribute_list[] = {
997 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 { EGL_ALPHA_SIZE, 8 },
999 { EGL_BLUE_SIZE, 8 },
1000 { EGL_GREEN_SIZE, 8 },
1001 { EGL_RED_SIZE, 8 },
1002 { EGL_DEPTH_SIZE, 0 },
1003 { EGL_CONFIG_ID, 2 },
Mathias Agopianc4e84b82010-10-21 15:58:25 -07001004 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1006};
1007
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001008static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 { EGL_BUFFER_SIZE, 32 },
1010 { EGL_ALPHA_SIZE, 8 },
1011 { EGL_BLUE_SIZE, 8 },
1012 { EGL_GREEN_SIZE, 8 },
1013 { EGL_RED_SIZE, 8 },
1014 { EGL_DEPTH_SIZE, 16 },
1015 { EGL_CONFIG_ID, 3 },
Mathias Agopianc9923872010-10-20 17:21:43 -07001016 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1018};
1019
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001020// A8 configs
1021static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 { EGL_BUFFER_SIZE, 8 },
1023 { EGL_ALPHA_SIZE, 8 },
1024 { EGL_BLUE_SIZE, 0 },
1025 { EGL_GREEN_SIZE, 0 },
1026 { EGL_RED_SIZE, 0 },
1027 { EGL_DEPTH_SIZE, 0 },
1028 { EGL_CONFIG_ID, 4 },
Mathias Agopianc9923872010-10-20 17:21:43 -07001029 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1031};
1032
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001033static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 { EGL_BUFFER_SIZE, 8 },
1035 { EGL_ALPHA_SIZE, 8 },
1036 { EGL_BLUE_SIZE, 0 },
1037 { EGL_GREEN_SIZE, 0 },
1038 { EGL_RED_SIZE, 0 },
1039 { EGL_DEPTH_SIZE, 16 },
1040 { EGL_CONFIG_ID, 5 },
Mathias Agopianc4e84b82010-10-21 15:58:25 -07001041 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1043};
1044
1045static configs_t const gConfigs[] = {
1046 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1047 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1048 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1049 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1050 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1051 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001052 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1053 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054};
1055
1056static config_management_t const gConfigManagement[] = {
1057 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1058 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1059 { EGL_BLUE_SIZE, config_management_t::atLeast },
1060 { EGL_GREEN_SIZE, config_management_t::atLeast },
1061 { EGL_RED_SIZE, config_management_t::atLeast },
1062 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1063 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1064 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1065 { EGL_CONFIG_ID, config_management_t::exact },
1066 { EGL_LEVEL, config_management_t::exact },
Mathias Agopiand8e350b2010-10-25 15:51:24 -07001067 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1068 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1069 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopiand8e350b2010-10-25 15:51:24 -07001071 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1073 { EGL_SAMPLES, config_management_t::exact },
1074 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1075 { EGL_SURFACE_TYPE, config_management_t::mask },
1076 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1077 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1078 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1079 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1080 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1081 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1082 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1083 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001084 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1085 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1086 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1087 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1088 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089};
1090
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092static config_pair_t const config_defaults[] = {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001093 // attributes that are not specified are simply ignored, if a particular
1094 // one needs not be ignored, it must be specified here, eg:
1095 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096};
1097
1098// ----------------------------------------------------------------------------
1099
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001100static status_t getConfigFormatInfo(EGLint configID,
1101 int32_t& pixelFormat, int32_t& depthFormat)
1102{
1103 switch(configID) {
1104 case 0:
1105 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1106 depthFormat = 0;
1107 break;
1108 case 1:
1109 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1110 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1111 break;
1112 case 2:
1113 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1114 depthFormat = 0;
1115 break;
1116 case 3:
1117 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1118 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1119 break;
1120 case 4:
1121 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1122 depthFormat = 0;
1123 break;
1124 case 5:
1125 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1126 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1127 break;
1128 case 6:
1129 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1130 depthFormat = 0;
1131 break;
1132 case 7:
1133 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1134 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1135 break;
1136 default:
1137 return NAME_NOT_FOUND;
1138 }
1139 return NO_ERROR;
1140}
1141
1142// ----------------------------------------------------------------------------
1143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144template<typename T>
1145static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1146{
1147 while (first <= last) {
1148 int mid = (first + last) / 2;
Mathias Agopian1473f462009-04-10 14:24:30 -07001149 if (key > sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 first = mid + 1;
Mathias Agopian1473f462009-04-10 14:24:30 -07001151 } else if (key < sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 last = mid - 1;
1153 } else {
1154 return mid;
1155 }
1156 }
1157 return -1;
1158}
1159
1160static int isAttributeMatching(int i, EGLint attr, EGLint val)
1161{
1162 // look for the attribute in all of our configs
Mathias Agopian1473f462009-04-10 14:24:30 -07001163 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 int index = binarySearch<config_pair_t>(
1165 gConfigs[i].array,
1166 0, gConfigs[i].size-1,
1167 attr);
1168 if (index < 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001169 configFound = config_base_attribute_list;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 index = binarySearch<config_pair_t>(
1171 config_base_attribute_list,
1172 0, NELEM(config_base_attribute_list)-1,
1173 attr);
1174 }
1175 if (index >= 0) {
1176 // attribute found, check if this config could match
1177 int cfgMgtIndex = binarySearch<config_management_t>(
1178 gConfigManagement,
1179 0, NELEM(gConfigManagement)-1,
1180 attr);
Christoffer Gurell4a783af2009-10-12 11:57:27 +02001181 if (cfgMgtIndex >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 bool match = gConfigManagement[cfgMgtIndex].match(
1183 val, configFound[index].value);
1184 if (match) {
1185 // this config matches
1186 return 1;
1187 }
1188 } else {
1189 // attribute not found. this should NEVER happen.
1190 }
1191 } else {
1192 // error, this attribute doesn't exist
1193 }
1194 return 0;
1195}
1196
1197static int makeCurrent(ogles_context_t* gl)
1198{
1199 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1200 if (gl) {
1201 egl_context_t* c = egl_context_t::context(gl);
1202 if (c->flags & egl_context_t::IS_CURRENT) {
1203 if (current != gl) {
1204 // it is an error to set a context current, if it's already
1205 // current to another thread
1206 return -1;
1207 }
1208 } else {
1209 if (current) {
1210 // mark the current context as not current, and flush
1211 glFlush();
1212 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1213 }
1214 }
1215 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1216 // The context is not current, make it current!
1217 setGlThreadSpecific(gl);
1218 c->flags |= egl_context_t::IS_CURRENT;
1219 }
1220 } else {
1221 if (current) {
1222 // mark the current context as not current, and flush
1223 glFlush();
1224 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1225 }
1226 // this thread has no context attached to it
1227 setGlThreadSpecific(0);
1228 }
1229 return 0;
1230}
1231
1232static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1233 EGLint attribute, EGLint *value)
1234{
1235 size_t numConfigs = NELEM(gConfigs);
1236 int index = (int)config;
1237 if (uint32_t(index) >= numConfigs)
1238 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1239
1240 int attrIndex;
1241 attrIndex = binarySearch<config_pair_t>(
1242 gConfigs[index].array,
1243 0, gConfigs[index].size-1,
1244 attribute);
1245 if (attrIndex>=0) {
1246 *value = gConfigs[index].array[attrIndex].value;
1247 return EGL_TRUE;
1248 }
1249
1250 attrIndex = binarySearch<config_pair_t>(
1251 config_base_attribute_list,
1252 0, NELEM(config_base_attribute_list)-1,
1253 attribute);
1254 if (attrIndex>=0) {
1255 *value = config_base_attribute_list[attrIndex].value;
1256 return EGL_TRUE;
1257 }
1258 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1259}
1260
1261static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1262 NativeWindowType window, const EGLint *attrib_list)
1263{
1264 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1265 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1266 if (window == 0)
1267 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1268
1269 EGLint surfaceType;
1270 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1271 return EGL_FALSE;
1272
1273 if (!(surfaceType & EGL_WINDOW_BIT))
1274 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1275
Dianne Hackborn8b49bd12010-06-30 13:56:17 -07001276 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopianffbc8642009-08-20 00:12:56 -07001277 ANDROID_NATIVE_WINDOW_MAGIC) {
1278 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1279 }
1280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 EGLint configID;
1282 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1283 return EGL_FALSE;
1284
1285 int32_t depthFormat;
1286 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001287 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1289 }
1290
1291 // FIXME: we don't have access to the pixelFormat here just yet.
1292 // (it's possible that the surface is not fully initialized)
1293 // maybe this should be done after the page-flip
1294 //if (EGLint(info.format) != pixelFormat)
1295 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1296
Mathias Agopian1473f462009-04-10 14:24:30 -07001297 egl_surface_t* surface;
1298 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn8b49bd12010-06-30 13:56:17 -07001299 static_cast<ANativeWindow*>(window));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300
Mathias Agopianffbc8642009-08-20 00:12:56 -07001301 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 // there was a problem in the ctor, the error
1303 // flag has been set.
1304 delete surface;
1305 surface = 0;
1306 }
1307 return surface;
1308}
1309
1310static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1311 NativePixmapType pixmap, const EGLint *attrib_list)
1312{
1313 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1314 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1315 if (pixmap == 0)
1316 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1317
1318 EGLint surfaceType;
1319 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1320 return EGL_FALSE;
1321
1322 if (!(surfaceType & EGL_PIXMAP_BIT))
1323 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1324
Mathias Agopianffbc8642009-08-20 00:12:56 -07001325 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1326 sizeof(egl_native_pixmap_t)) {
1327 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1328 }
1329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 EGLint configID;
1331 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1332 return EGL_FALSE;
1333
1334 int32_t depthFormat;
1335 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001336 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1338 }
1339
1340 if (pixmap->format != pixelFormat)
1341 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1342
1343 egl_surface_t* surface =
1344 new egl_pixmap_surface_t(dpy, config, depthFormat,
1345 static_cast<egl_native_pixmap_t*>(pixmap));
1346
Mathias Agopianffbc8642009-08-20 00:12:56 -07001347 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 // there was a problem in the ctor, the error
1349 // flag has been set.
1350 delete surface;
1351 surface = 0;
1352 }
1353 return surface;
1354}
1355
1356static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1357 const EGLint *attrib_list)
1358{
1359 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1360 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1361
1362 EGLint surfaceType;
1363 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1364 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (!(surfaceType & EGL_PBUFFER_BIT))
1367 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 EGLint configID;
1370 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1371 return EGL_FALSE;
1372
1373 int32_t depthFormat;
1374 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001375 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1377 }
1378
1379 int32_t w = 0;
1380 int32_t h = 0;
1381 while (attrib_list[0]) {
1382 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1383 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1384 attrib_list+=2;
1385 }
1386
1387 egl_surface_t* surface =
1388 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1389
Mathias Agopianffbc8642009-08-20 00:12:56 -07001390 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 // there was a problem in the ctor, the error
1392 // flag has been set.
1393 delete surface;
1394 surface = 0;
1395 }
1396 return surface;
1397}
1398
1399// ----------------------------------------------------------------------------
1400}; // namespace android
1401// ----------------------------------------------------------------------------
1402
1403using namespace android;
1404
1405// ----------------------------------------------------------------------------
1406// Initialization
1407// ----------------------------------------------------------------------------
1408
1409EGLDisplay eglGetDisplay(NativeDisplayType display)
1410{
1411#ifndef HAVE_ANDROID_OS
1412 // this just needs to be done once
1413 if (gGLKey == -1) {
1414 pthread_mutex_lock(&gInitMutex);
1415 if (gGLKey == -1)
1416 pthread_key_create(&gGLKey, NULL);
1417 pthread_mutex_unlock(&gInitMutex);
1418 }
1419#endif
1420 if (display == EGL_DEFAULT_DISPLAY) {
1421 EGLDisplay dpy = (EGLDisplay)1;
1422 egl_display_t& d = egl_display_t::get_display(dpy);
1423 d.type = display;
1424 return dpy;
Mathias Agopian1473f462009-04-10 14:24:30 -07001425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 return EGL_NO_DISPLAY;
1427}
1428
1429EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1430{
1431 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1432 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 EGLBoolean res = EGL_TRUE;
1435 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -07001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (android_atomic_inc(&d.initialized) == 0) {
1438 // initialize stuff here if needed
1439 //pthread_mutex_lock(&gInitMutex);
1440 //pthread_mutex_unlock(&gInitMutex);
1441 }
1442
1443 if (res == EGL_TRUE) {
1444 if (major != NULL) *major = VERSION_MAJOR;
1445 if (minor != NULL) *minor = VERSION_MINOR;
1446 }
1447 return res;
1448}
1449
1450EGLBoolean eglTerminate(EGLDisplay dpy)
1451{
1452 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1453 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1454
1455 EGLBoolean res = EGL_TRUE;
1456 egl_display_t& d = egl_display_t::get_display(dpy);
1457 if (android_atomic_dec(&d.initialized) == 1) {
1458 // TODO: destroy all resources (surfaces, contexts, etc...)
1459 //pthread_mutex_lock(&gInitMutex);
1460 //pthread_mutex_unlock(&gInitMutex);
1461 }
1462 return res;
1463}
1464
1465// ----------------------------------------------------------------------------
1466// configuration
1467// ----------------------------------------------------------------------------
1468
1469EGLBoolean eglGetConfigs( EGLDisplay dpy,
1470 EGLConfig *configs,
1471 EGLint config_size, EGLint *num_config)
1472{
1473 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1474 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1475
1476 GLint numConfigs = NELEM(gConfigs);
1477 if (!configs) {
1478 *num_config = numConfigs;
1479 return EGL_TRUE;
1480 }
1481 GLint i;
1482 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1483 *configs++ = (EGLConfig)i;
1484 }
1485 *num_config = i;
1486 return EGL_TRUE;
1487}
1488
1489EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1490 EGLConfig *configs, EGLint config_size,
1491 EGLint *num_config)
1492{
1493 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1494 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich1badb712009-03-25 15:12:17 -07001495
1496 if (ggl_unlikely(num_config==0)) {
1497 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499
Jack Palevich1badb712009-03-25 15:12:17 -07001500 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian7e71fcf2010-05-17 14:45:43 -07001501 /*
1502 * A NULL attrib_list should be treated as though it was an empty
1503 * one (terminated with EGL_NONE) as defined in
1504 * section 3.4.1 "Querying Configurations" in the EGL specification.
1505 */
1506 static const EGLint dummy = EGL_NONE;
1507 attrib_list = &dummy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 int numAttributes = 0;
1511 int numConfigs = NELEM(gConfigs);
1512 uint32_t possibleMatch = (1<<numConfigs)-1;
1513 while(possibleMatch && *attrib_list != EGL_NONE) {
1514 numAttributes++;
1515 EGLint attr = *attrib_list++;
1516 EGLint val = *attrib_list++;
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001517 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 if (!(possibleMatch & (1<<i)))
1519 continue;
1520 if (isAttributeMatching(i, attr, val) == 0) {
1521 possibleMatch &= ~(1<<i);
1522 }
1523 }
1524 }
1525
1526 // now, handle the attributes which have a useful default value
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001527 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1528 // see if this attribute was specified, if not, apply its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 // default value
1530 if (binarySearch<config_pair_t>(
1531 (config_pair_t const*)attrib_list,
Mathias Agopianab1cf3e2009-07-09 17:33:15 -07001532 0, numAttributes-1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 config_defaults[j].key) < 0)
1534 {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001535 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 if (!(possibleMatch & (1<<i)))
1537 continue;
1538 if (isAttributeMatching(i,
1539 config_defaults[j].key,
1540 config_defaults[j].value) == 0)
1541 {
1542 possibleMatch &= ~(1<<i);
1543 }
1544 }
1545 }
1546 }
1547
1548 // return the configurations found
1549 int n=0;
1550 if (possibleMatch) {
Jack Palevich1badb712009-03-25 15:12:17 -07001551 if (configs) {
1552 for (int i=0 ; config_size && i<numConfigs ; i++) {
1553 if (possibleMatch & (1<<i)) {
1554 *configs++ = (EGLConfig)i;
1555 config_size--;
1556 n++;
1557 }
1558 }
1559 } else {
1560 for (int i=0 ; i<numConfigs ; i++) {
1561 if (possibleMatch & (1<<i)) {
1562 n++;
1563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565 }
1566 }
1567 *num_config = n;
1568 return EGL_TRUE;
1569}
1570
1571EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1572 EGLint attribute, EGLint *value)
1573{
1574 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1575 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1576
1577 return getConfigAttrib(dpy, config, attribute, value);
1578}
1579
1580// ----------------------------------------------------------------------------
1581// surfaces
1582// ----------------------------------------------------------------------------
1583
1584EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1585 NativeWindowType window,
1586 const EGLint *attrib_list)
1587{
1588 return createWindowSurface(dpy, config, window, attrib_list);
1589}
Mathias Agopian1473f462009-04-10 14:24:30 -07001590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1592 NativePixmapType pixmap,
1593 const EGLint *attrib_list)
1594{
1595 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1596}
1597
1598EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1599 const EGLint *attrib_list)
1600{
1601 return createPbufferSurface(dpy, config, attrib_list);
1602}
Mathias Agopian1473f462009-04-10 14:24:30 -07001603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1605{
1606 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1607 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1608 if (eglSurface != EGL_NO_SURFACE) {
1609 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopianffbc8642009-08-20 00:12:56 -07001610 if (!surface->isValid())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1612 if (surface->dpy != dpy)
1613 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001614 if (surface->ctx) {
1615 // FIXME: this surface is current check what the spec says
1616 surface->disconnect();
1617 surface->ctx = 0;
1618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 delete surface;
1620 }
1621 return EGL_TRUE;
1622}
1623
1624EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1625 EGLint attribute, EGLint *value)
1626{
1627 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1628 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1629 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001630 if (!surface->isValid())
1631 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 if (surface->dpy != dpy)
1633 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1634
1635 EGLBoolean ret = EGL_TRUE;
1636 switch (attribute) {
1637 case EGL_CONFIG_ID:
1638 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1639 break;
1640 case EGL_WIDTH:
1641 *value = surface->getWidth();
1642 break;
1643 case EGL_HEIGHT:
1644 *value = surface->getHeight();
1645 break;
1646 case EGL_LARGEST_PBUFFER:
1647 // not modified for a window or pixmap surface
1648 break;
1649 case EGL_TEXTURE_FORMAT:
1650 *value = EGL_NO_TEXTURE;
1651 break;
1652 case EGL_TEXTURE_TARGET:
1653 *value = EGL_NO_TEXTURE;
1654 break;
1655 case EGL_MIPMAP_TEXTURE:
1656 *value = EGL_FALSE;
1657 break;
1658 case EGL_MIPMAP_LEVEL:
1659 *value = 0;
1660 break;
1661 case EGL_RENDER_BUFFER:
1662 // TODO: return the real RENDER_BUFFER here
1663 *value = EGL_BACK_BUFFER;
1664 break;
1665 case EGL_HORIZONTAL_RESOLUTION:
1666 // pixel/mm * EGL_DISPLAY_SCALING
1667 *value = surface->getHorizontalResolution();
1668 break;
1669 case EGL_VERTICAL_RESOLUTION:
1670 // pixel/mm * EGL_DISPLAY_SCALING
1671 *value = surface->getVerticalResolution();
1672 break;
1673 case EGL_PIXEL_ASPECT_RATIO: {
1674 // w/h * EGL_DISPLAY_SCALING
1675 int wr = surface->getHorizontalResolution();
1676 int hr = surface->getVerticalResolution();
1677 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1678 } break;
1679 case EGL_SWAP_BEHAVIOR:
Mathias Agopian1473f462009-04-10 14:24:30 -07001680 *value = surface->getSwapBehavior();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 break;
1682 default:
1683 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1684 }
1685 return ret;
1686}
1687
1688EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1689 EGLContext share_list, const EGLint *attrib_list)
1690{
1691 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1692 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1693
1694 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1695 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1696
1697 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1698 c->flags = egl_context_t::NEVER_CURRENT;
1699 c->dpy = dpy;
1700 c->config = config;
1701 c->read = 0;
1702 c->draw = 0;
1703 return (EGLContext)gl;
1704}
1705
1706EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1707{
1708 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1709 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1710 egl_context_t* c = egl_context_t::context(ctx);
1711 if (c->flags & egl_context_t::IS_CURRENT)
1712 setGlThreadSpecific(0);
1713 ogles_uninit((ogles_context_t*)ctx);
1714 return EGL_TRUE;
1715}
1716
1717EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1718 EGLSurface read, EGLContext ctx)
1719{
1720 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1721 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1722 if (draw) {
1723 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopianffbc8642009-08-20 00:12:56 -07001724 if (!s->isValid())
1725 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 if (s->dpy != dpy)
1727 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001728 // TODO: check that draw is compatible with the context
1729 }
1730 if (read && read!=draw) {
1731 egl_surface_t* s = (egl_surface_t*)read;
1732 if (!s->isValid())
1733 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1734 if (s->dpy != dpy)
1735 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1736 // TODO: check that read is compatible with the context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738
1739 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian1473f462009-04-10 14:24:30 -07001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1742 return setError(EGL_BAD_MATCH, EGL_FALSE);
1743
1744 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1745 return setError(EGL_BAD_MATCH, EGL_FALSE);
1746
1747 if (ctx == EGL_NO_CONTEXT) {
1748 // if we're detaching, we need the current context
1749 current_ctx = (EGLContext)getGlThreadSpecific();
1750 } else {
1751 egl_context_t* c = egl_context_t::context(ctx);
1752 egl_surface_t* d = (egl_surface_t*)draw;
1753 egl_surface_t* r = (egl_surface_t*)read;
1754 if ((d && d->ctx && d->ctx != ctx) ||
1755 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001756 // one of the surface is bound to a context in another thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1758 }
1759 }
1760
1761 ogles_context_t* gl = (ogles_context_t*)ctx;
1762 if (makeCurrent(gl) == 0) {
1763 if (ctx) {
1764 egl_context_t* c = egl_context_t::context(ctx);
1765 egl_surface_t* d = (egl_surface_t*)draw;
1766 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001767
1768 if (c->draw) {
Mathias Agopianffbc8642009-08-20 00:12:56 -07001769 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1770 s->disconnect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001771 }
1772 if (c->read) {
1773 // FIXME: unlock/disconnect the read surface too
1774 }
1775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 c->draw = draw;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001777 c->read = read;
1778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 if (c->flags & egl_context_t::NEVER_CURRENT) {
1780 c->flags &= ~egl_context_t::NEVER_CURRENT;
1781 GLint w = 0;
1782 GLint h = 0;
1783 if (draw) {
1784 w = d->getWidth();
1785 h = d->getHeight();
1786 }
1787 ogles_surfaceport(gl, 0, 0);
1788 ogles_viewport(gl, 0, 0, w, h);
1789 ogles_scissor(gl, 0, 0, w, h);
1790 }
1791 if (d) {
Mathias Agopianabac0102009-07-31 14:47:00 -07001792 if (d->connect() == EGL_FALSE) {
1793 return EGL_FALSE;
1794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 d->ctx = ctx;
1796 d->bindDrawSurface(gl);
1797 }
1798 if (r) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001799 // FIXME: lock/connect the read surface too
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 r->ctx = ctx;
1801 r->bindReadSurface(gl);
1802 }
1803 } else {
1804 // if surfaces were bound to the context bound to this thread
1805 // mark then as unbound.
1806 if (current_ctx) {
1807 egl_context_t* c = egl_context_t::context(current_ctx);
1808 egl_surface_t* d = (egl_surface_t*)c->draw;
1809 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001810 if (d) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001811 c->draw = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001812 d->ctx = EGL_NO_CONTEXT;
1813 d->disconnect();
1814 }
1815 if (r) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001816 c->read = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001817 r->ctx = EGL_NO_CONTEXT;
1818 // FIXME: unlock/disconnect the read surface too
1819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
1821 }
1822 return EGL_TRUE;
1823 }
1824 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1825}
1826
1827EGLContext eglGetCurrentContext(void)
1828{
1829 // eglGetCurrentContext returns the current EGL rendering context,
1830 // as specified by eglMakeCurrent. If no context is current,
1831 // EGL_NO_CONTEXT is returned.
1832 return (EGLContext)getGlThreadSpecific();
1833}
1834
1835EGLSurface eglGetCurrentSurface(EGLint readdraw)
1836{
1837 // eglGetCurrentSurface returns the read or draw surface attached
1838 // to the current EGL rendering context, as specified by eglMakeCurrent.
1839 // If no context is current, EGL_NO_SURFACE is returned.
1840 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1841 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1842 egl_context_t* c = egl_context_t::context(ctx);
1843 if (readdraw == EGL_READ) {
1844 return c->read;
1845 } else if (readdraw == EGL_DRAW) {
1846 return c->draw;
1847 }
1848 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1849}
1850
1851EGLDisplay eglGetCurrentDisplay(void)
1852{
1853 // eglGetCurrentDisplay returns the current EGL display connection
1854 // for the current EGL rendering context, as specified by eglMakeCurrent.
1855 // If no context is current, EGL_NO_DISPLAY is returned.
1856 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1857 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1858 egl_context_t* c = egl_context_t::context(ctx);
1859 return c->dpy;
1860}
1861
1862EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1863 EGLint attribute, EGLint *value)
1864{
1865 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1866 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1867 egl_context_t* c = egl_context_t::context(ctx);
1868 switch (attribute) {
1869 case EGL_CONFIG_ID:
1870 // Returns the ID of the EGL frame buffer configuration with
1871 // respect to which the context was created
1872 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1873 }
1874 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1875}
1876
1877EGLBoolean eglWaitGL(void)
1878{
1879 return EGL_TRUE;
1880}
1881
1882EGLBoolean eglWaitNative(EGLint engine)
1883{
1884 return EGL_TRUE;
1885}
1886
1887EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1888{
1889 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1890 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001893 if (!d->isValid())
1894 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 if (d->dpy != dpy)
1896 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1897
1898 // post the surface
1899 d->swapBuffers();
1900
1901 // if it's bound to a context, update the buffer
1902 if (d->ctx != EGL_NO_CONTEXT) {
1903 d->bindDrawSurface((ogles_context_t*)d->ctx);
1904 // if this surface is also the read surface of the context
1905 // it is bound to, make sure to update the read buffer as well.
1906 // The EGL spec is a little unclear about this.
1907 egl_context_t* c = egl_context_t::context(d->ctx);
1908 if (c->read == draw) {
1909 d->bindReadSurface((ogles_context_t*)d->ctx);
1910 }
1911 }
1912
1913 return EGL_TRUE;
1914}
1915
1916EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1917 NativePixmapType target)
1918{
1919 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1920 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1921 // TODO: eglCopyBuffers()
1922 return EGL_FALSE;
1923}
1924
1925EGLint eglGetError(void)
1926{
1927 return getError();
1928}
1929
1930const char* eglQueryString(EGLDisplay dpy, EGLint name)
1931{
1932 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1933 return setError(EGL_BAD_DISPLAY, (const char*)0);
1934
1935 switch (name) {
1936 case EGL_VENDOR:
1937 return gVendorString;
1938 case EGL_VERSION:
1939 return gVersionString;
1940 case EGL_EXTENSIONS:
1941 return gExtensionsString;
1942 case EGL_CLIENT_APIS:
1943 return gClientApiString;
1944 }
1945 return setError(EGL_BAD_PARAMETER, (const char *)0);
1946}
1947
1948// ----------------------------------------------------------------------------
1949// EGL 1.1
1950// ----------------------------------------------------------------------------
1951
1952EGLBoolean eglSurfaceAttrib(
1953 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1954{
1955 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1956 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1957 // TODO: eglSurfaceAttrib()
1958 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1959}
1960
1961EGLBoolean eglBindTexImage(
1962 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1963{
1964 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1965 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1966 // TODO: eglBindTexImage()
1967 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1968}
1969
1970EGLBoolean eglReleaseTexImage(
1971 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1972{
1973 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1974 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1975 // TODO: eglReleaseTexImage()
1976 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1977}
1978
1979EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1980{
1981 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1982 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1983 // TODO: eglSwapInterval()
Ari Hirvonen1d836312010-10-01 19:00:54 +03001984 return EGL_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985}
1986
1987// ----------------------------------------------------------------------------
1988// EGL 1.2
1989// ----------------------------------------------------------------------------
1990
1991EGLBoolean eglBindAPI(EGLenum api)
1992{
1993 if (api != EGL_OPENGL_ES_API)
1994 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1995 return EGL_TRUE;
1996}
1997
1998EGLenum eglQueryAPI(void)
1999{
2000 return EGL_OPENGL_ES_API;
2001}
2002
2003EGLBoolean eglWaitClient(void)
2004{
2005 glFinish();
2006 return EGL_TRUE;
2007}
2008
2009EGLBoolean eglReleaseThread(void)
2010{
2011 // TODO: eglReleaseThread()
2012 return EGL_TRUE;
2013}
2014
2015EGLSurface eglCreatePbufferFromClientBuffer(
2016 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2017 EGLConfig config, const EGLint *attrib_list)
2018{
2019 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2020 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2021 // TODO: eglCreatePbufferFromClientBuffer()
2022 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2023}
2024
2025// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -07002026// EGL_EGLEXT_VERSION 3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027// ----------------------------------------------------------------------------
2028
2029void (*eglGetProcAddress (const char *procname))()
2030{
2031 extention_map_t const * const map = gExtentionMap;
2032 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2033 if (!strcmp(procname, map[i].name)) {
2034 return map[i].address;
2035 }
2036 }
2037 return NULL;
2038}
Mathias Agopian1473f462009-04-10 14:24:30 -07002039
2040EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2041 const EGLint *attrib_list)
2042{
2043 EGLBoolean result = EGL_FALSE;
2044 return result;
2045}
2046
2047EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2048{
2049 EGLBoolean result = EGL_FALSE;
2050 return result;
2051}
2052
2053EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2054 EGLClientBuffer buffer, const EGLint *attrib_list)
2055{
2056 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2057 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2058 }
2059 if (ctx != EGL_NO_CONTEXT) {
2060 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2061 }
2062 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2063 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2064 }
2065
2066 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2067
2068 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2069 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2070
2071 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2072 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian42d99d22010-02-04 17:04:53 -08002073
2074 switch (native_buffer->format) {
2075 case HAL_PIXEL_FORMAT_RGBA_8888:
2076 case HAL_PIXEL_FORMAT_RGBX_8888:
2077 case HAL_PIXEL_FORMAT_RGB_888:
2078 case HAL_PIXEL_FORMAT_RGB_565:
2079 case HAL_PIXEL_FORMAT_BGRA_8888:
2080 case HAL_PIXEL_FORMAT_RGBA_5551:
2081 case HAL_PIXEL_FORMAT_RGBA_4444:
2082 break;
2083 default:
2084 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2085 }
2086
Mathias Agopian1473f462009-04-10 14:24:30 -07002087 native_buffer->common.incRef(&native_buffer->common);
2088 return (EGLImageKHR)native_buffer;
2089}
2090
2091EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2092{
2093 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2094 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2095 }
2096
2097 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2098
2099 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2100 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2101
2102 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2103 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2104
Mathias Agopian1473f462009-04-10 14:24:30 -07002105 native_buffer->common.decRef(&native_buffer->common);
2106
2107 return EGL_TRUE;
2108}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002109
2110// ----------------------------------------------------------------------------
2111// ANDROID extensions
2112// ----------------------------------------------------------------------------
2113
2114EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2115 EGLint left, EGLint top, EGLint width, EGLint height)
2116{
2117 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2118 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2119
2120 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002121 if (!d->isValid())
2122 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002123 if (d->dpy != dpy)
2124 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2125
2126 // post the surface
2127 d->setSwapRectangle(left, top, width, height);
2128
2129 return EGL_TRUE;
2130}