blob: 781b8c33fdc5cacc77e2fa199dd83209d26bdc35 [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 Agopianbbf945f2009-11-03 20:38:08 -080043#include <private/ui/sw_gralloc_handle.h>
Mathias Agopianac2523b2009-05-05 18:11:11 -070044
Mathias Agopian68eeb802009-06-25 15:39:25 -070045#include <hardware/copybit.h>
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include "context.h"
48#include "state.h"
49#include "texture.h"
50#include "matrix.h"
51
52#undef NELEM
53#define NELEM(x) (sizeof(x)/sizeof(*(x)))
54
55// ----------------------------------------------------------------------------
56namespace android {
57// ----------------------------------------------------------------------------
58
59const unsigned int NUM_DISPLAYS = 1;
60
61static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
62static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
63static pthread_key_t gEGLErrorKey = -1;
64#ifndef HAVE_ANDROID_OS
65namespace gl {
66pthread_key_t gGLKey = -1;
67}; // namespace gl
68#endif
69
70template<typename T>
71static T setError(GLint error, T returnValue) {
72 if (ggl_unlikely(gEGLErrorKey == -1)) {
73 pthread_mutex_lock(&gErrorKeyMutex);
74 if (gEGLErrorKey == -1)
75 pthread_key_create(&gEGLErrorKey, NULL);
76 pthread_mutex_unlock(&gErrorKeyMutex);
77 }
78 pthread_setspecific(gEGLErrorKey, (void*)error);
79 return returnValue;
80}
81
82static GLint getError() {
83 if (ggl_unlikely(gEGLErrorKey == -1))
84 return EGL_SUCCESS;
85 GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
86 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
87 return error;
88}
89
90// ----------------------------------------------------------------------------
91
92struct egl_display_t
93{
94 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian1473f462009-04-10 14:24:30 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -070097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 static EGLBoolean is_valid(EGLDisplay dpy) {
99 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
100 }
101
102 NativeDisplayType type;
103 volatile int32_t initialized;
104};
105
106static egl_display_t gDisplays[NUM_DISPLAYS];
107
108egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
109 return gDisplays[uintptr_t(dpy)-1U];
110}
111
112struct egl_context_t {
113 enum {
114 IS_CURRENT = 0x00010000,
115 NEVER_CURRENT = 0x00020000
116 };
117 uint32_t flags;
118 EGLDisplay dpy;
119 EGLConfig config;
120 EGLSurface read;
121 EGLSurface draw;
122
123 static inline egl_context_t* context(EGLContext ctx) {
124 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
125 return static_cast<egl_context_t*>(gl->rasterizer.base);
126 }
127};
128
129// ----------------------------------------------------------------------------
130
131struct egl_surface_t
132{
133 enum {
134 PAGE_FLIP = 0x00000001,
135 MAGIC = 0x31415265
136 };
137
138 uint32_t magic;
139 EGLDisplay dpy;
140 EGLConfig config;
141 EGLContext ctx;
142
143 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
144 virtual ~egl_surface_t();
Mathias Agopianffbc8642009-08-20 00:12:56 -0700145 bool isValid() const;
146 virtual bool initCheck() const = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
149 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopianabac0102009-07-31 14:47:00 -0700150 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700151 virtual void disconnect() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 virtual EGLint getWidth() const = 0;
153 virtual EGLint getHeight() const = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155 virtual EGLint getHorizontalResolution() const;
156 virtual EGLint getVerticalResolution() const;
157 virtual EGLint getRefreshRate() const;
158 virtual EGLint getSwapBehavior() const;
159 virtual EGLBoolean swapBuffers();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700160 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700161 virtual EGLClientBuffer getRenderBuffer() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162protected:
163 GGLSurface depth;
164};
165
166egl_surface_t::egl_surface_t(EGLDisplay dpy,
167 EGLConfig config,
168 int32_t depthFormat)
169 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
170{
171 depth.version = sizeof(GGLSurface);
172 depth.data = 0;
173 depth.format = depthFormat;
174}
175egl_surface_t::~egl_surface_t()
176{
177 magic = 0;
178 free(depth.data);
179}
Mathias Agopianffbc8642009-08-20 00:12:56 -0700180bool egl_surface_t::isValid() const {
181 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
182 return magic == MAGIC;
183}
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185EGLBoolean egl_surface_t::swapBuffers() {
186 return EGL_FALSE;
187}
188EGLint egl_surface_t::getHorizontalResolution() const {
189 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
190}
191EGLint egl_surface_t::getVerticalResolution() const {
192 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
193}
194EGLint egl_surface_t::getRefreshRate() const {
195 return (60 * EGL_DISPLAY_SCALING);
196}
197EGLint egl_surface_t::getSwapBehavior() const {
198 return EGL_BUFFER_PRESERVED;
199}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700200EGLBoolean egl_surface_t::setSwapRectangle(
201 EGLint l, EGLint t, EGLint w, EGLint h)
202{
203 return EGL_FALSE;
204}
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700205EGLClientBuffer egl_surface_t::getRenderBuffer() const {
206 return 0;
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,
Mathias Agopian1473f462009-04-10 14:24:30 -0700216 android_native_window_t* 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 virtual EGLClientBuffer getRenderBuffer() const;
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235private:
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700236 status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700237 status_t unlock(android_native_buffer_t* buf);
Mathias Agopian1473f462009-04-10 14:24:30 -0700238 android_native_window_t* nativeWindow;
239 android_native_buffer_t* buffer;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700240 android_native_buffer_t* previousBuffer;
Mathias Agopiandff8e582009-05-04 14:17:04 -0700241 gralloc_module_t const* module;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700242 copybit_device_t* blitengine;
Mathias Agopian1473f462009-04-10 14:24:30 -0700243 int width;
244 int height;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700245 void* bits;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700246 GGLFormat const* pixelFormatTable;
247
248 struct Rect {
249 inline Rect() { };
250 inline Rect(int32_t w, int32_t h)
251 : left(0), top(0), right(w), bottom(h) { }
252 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
253 : left(l), top(t), right(r), bottom(b) { }
254 Rect& andSelf(const Rect& r) {
255 left = max(left, r.left);
256 top = max(top, r.top);
257 right = min(right, r.right);
258 bottom = min(bottom, r.bottom);
259 return *this;
260 }
261 bool isEmpty() const {
262 return (left>=right || top>=bottom);
263 }
264 void dump(char const* what) {
265 LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
266 what, left, top, right-left, bottom-top);
267 }
268
269 int32_t left;
270 int32_t top;
271 int32_t right;
272 int32_t bottom;
273 };
274
275 struct Region {
276 inline Region() : count(0) { }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700277 typedef Rect const* const_iterator;
278 const_iterator begin() const { return storage; }
279 const_iterator end() const { return storage+count; }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700280 static Region subtract(const Rect& lhs, const Rect& rhs) {
281 Region reg;
282 Rect* storage = reg.storage;
283 if (!lhs.isEmpty()) {
284 if (lhs.top < rhs.top) { // top rect
285 storage->left = lhs.left;
286 storage->top = lhs.top;
287 storage->right = lhs.right;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700288 storage->bottom = rhs.top;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700289 storage++;
290 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700291 const int32_t top = max(lhs.top, rhs.top);
292 const int32_t bot = min(lhs.bottom, rhs.bottom);
293 if (top < bot) {
294 if (lhs.left < rhs.left) { // left-side rect
295 storage->left = lhs.left;
296 storage->top = top;
297 storage->right = rhs.left;
298 storage->bottom = bot;
299 storage++;
300 }
301 if (lhs.right > rhs.right) { // right-side rect
302 storage->left = rhs.right;
303 storage->top = top;
304 storage->right = lhs.right;
305 storage->bottom = bot;
306 storage++;
307 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700308 }
309 if (lhs.bottom > rhs.bottom) { // bottom rect
310 storage->left = lhs.left;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700311 storage->top = rhs.bottom;
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700312 storage->right = lhs.right;
313 storage->bottom = lhs.bottom;
314 storage++;
315 }
316 reg.count = storage - reg.storage;
317 }
318 return reg;
319 }
320 bool isEmpty() const {
321 return count<=0;
322 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700323 private:
324 Rect storage[4];
325 ssize_t count;
326 };
327
Mathias Agopian68eeb802009-06-25 15:39:25 -0700328 struct region_iterator : public copybit_region_t {
329 region_iterator(const Region& region)
330 : b(region.begin()), e(region.end()) {
331 this->next = iterate;
332 }
333 private:
334 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
335 region_iterator const* me = static_cast<region_iterator const*>(self);
336 if (me->b != me->e) {
337 *reinterpret_cast<Rect*>(rect) = *me->b++;
338 return 1;
339 }
340 return 0;
341 }
342 mutable Region::const_iterator b;
343 Region::const_iterator const e;
344 };
345
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700346 void copyBlt(
347 android_native_buffer_t* dst, void* dst_vaddr,
348 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700349 const Region& clip);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700350
351 Rect dirtyRegion;
352 Rect oldDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353};
354
Mathias Agopian1473f462009-04-10 14:24:30 -0700355egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 EGLConfig config,
357 int32_t depthFormat,
Mathias Agopian1473f462009-04-10 14:24:30 -0700358 android_native_window_t* window)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700359 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700360 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian68eeb802009-06-25 15:39:25 -0700361 blitengine(0), bits(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362{
Mathias Agopiandff8e582009-05-04 14:17:04 -0700363 hw_module_t const* pModule;
364 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
365 module = reinterpret_cast<gralloc_module_t const*>(pModule);
366
Mathias Agopian68eeb802009-06-25 15:39:25 -0700367 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
368 copybit_open(pModule, &blitengine);
369 }
370
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700371 pixelFormatTable = gglGetPixelFormatTable();
372
373 // keep a reference on the window
Mathias Agopian1473f462009-04-10 14:24:30 -0700374 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700375 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
376 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700377}
Mathias Agopian1473f462009-04-10 14:24:30 -0700378
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700379egl_window_surface_v2_t::~egl_window_surface_v2_t() {
380 if (buffer) {
381 buffer->common.decRef(&buffer->common);
382 }
383 if (previousBuffer) {
384 previousBuffer->common.decRef(&previousBuffer->common);
385 }
386 nativeWindow->common.decRef(&nativeWindow->common);
Mathias Agopian68eeb802009-06-25 15:39:25 -0700387 if (blitengine) {
388 copybit_close(blitengine);
389 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700390}
391
Mathias Agopianabac0102009-07-31 14:47:00 -0700392EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700393{
Mathias Agopian5cec4742009-08-11 22:34:02 -0700394 // we're intending to do software rendering
395 native_window_set_usage(nativeWindow,
396 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
397
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700398 // dequeue a buffer
Mathias Agopianabac0102009-07-31 14:47:00 -0700399 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
400 return setError(EGL_BAD_ALLOC, EGL_FALSE);
401 }
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700402
403 // allocate a corresponding depth-buffer
404 width = buffer->width;
405 height = buffer->height;
406 if (depth.format) {
407 depth.width = width;
408 depth.height = height;
409 depth.stride = depth.width; // use the width here
410 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
411 if (depth.data == 0) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700412 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700413 }
414 }
415
416 // keep a reference on the buffer
417 buffer->common.incRef(&buffer->common);
418
Mathias Agopiandff8e582009-05-04 14:17:04 -0700419 // Lock the buffer
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700420 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700421 // pin the buffer down
422 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
423 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Mathias Agopian68eeb802009-06-25 15:39:25 -0700424 LOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700425 buffer, buffer->width, buffer->height);
Mathias Agopianabac0102009-07-31 14:47:00 -0700426 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700427 // FIXME: we should make sure we're not accessing the buffer anymore
428 }
Mathias Agopianabac0102009-07-31 14:47:00 -0700429 return EGL_TRUE;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700430}
431
432void egl_window_surface_v2_t::disconnect()
433{
Mathias Agopian36432cc2009-06-03 19:00:53 -0700434 if (buffer && bits) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700435 bits = NULL;
436 unlock(buffer);
437 }
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700438 // enqueue the last frame
439 nativeWindow->queueBuffer(nativeWindow, buffer);
440 if (buffer) {
441 buffer->common.decRef(&buffer->common);
442 buffer = 0;
443 }
444 if (previousBuffer) {
445 previousBuffer->common.decRef(&previousBuffer->common);
446 previousBuffer = 0;
447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448}
449
Mathias Agopiandff8e582009-05-04 14:17:04 -0700450status_t egl_window_surface_v2_t::lock(
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700451 android_native_buffer_t* buf, int usage, void** vaddr)
Mathias Agopiandff8e582009-05-04 14:17:04 -0700452{
Mathias Agopianbbf945f2009-11-03 20:38:08 -0800453 int err;
454 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
455 err = module->lock(module, buf->handle,
456 usage, 0, 0, buf->width, buf->height, vaddr);
457 } else {
458 sw_gralloc_handle_t const* hnd =
459 reinterpret_cast<sw_gralloc_handle_t const*>(buf->handle);
460 *vaddr = (void*)hnd->base;
461 err = NO_ERROR;
462 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700463 return err;
464}
465
466status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
467{
Mathias Agopianabac0102009-07-31 14:47:00 -0700468 if (!buf) return BAD_VALUE;
Mathias Agopianbbf945f2009-11-03 20:38:08 -0800469 int err = NO_ERROR;
470 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
471 err = module->unlock(module, buf->handle);
472 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700473 return err;
474}
475
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700476void egl_window_surface_v2_t::copyBlt(
477 android_native_buffer_t* dst, void* dst_vaddr,
478 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700479 const Region& clip)
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700480{
481 // FIXME: use copybit if possible
482 // NOTE: dst and src must be the same format
483
Mathias Agopian68eeb802009-06-25 15:39:25 -0700484 status_t err = NO_ERROR;
485 copybit_device_t* const copybit = blitengine;
486 if (copybit) {
487 copybit_image_t simg;
488 simg.w = src->width;
489 simg.h = src->height;
490 simg.format = src->format;
491 simg.handle = const_cast<native_handle_t*>(src->handle);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700492
Mathias Agopian68eeb802009-06-25 15:39:25 -0700493 copybit_image_t dimg;
494 dimg.w = dst->width;
495 dimg.h = dst->height;
496 dimg.format = dst->format;
497 dimg.handle = const_cast<native_handle_t*>(dst->handle);
498
499 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
500 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
501 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
502 region_iterator it(clip);
503 err = copybit->blit(copybit, &dimg, &simg, &it);
504 if (err != NO_ERROR) {
505 LOGE("copybit failed (%s)", strerror(err));
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700506 }
Mathias Agopian68eeb802009-06-25 15:39:25 -0700507 }
508
509 if (!copybit || err) {
510 Region::const_iterator cur = clip.begin();
511 Region::const_iterator end = clip.end();
512
513 const size_t bpp = pixelFormatTable[src->format].size;
514 const size_t dbpr = dst->stride * bpp;
515 const size_t sbpr = src->stride * bpp;
516
517 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
518 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
519
520 while (cur != end) {
521 const Rect& r(*cur++);
522 ssize_t w = r.right - r.left;
523 ssize_t h = r.bottom - r.top;
524 if (w <= 0 || h<=0) continue;
525 size_t size = w * bpp;
526 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
527 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
528 if (dbpr==sbpr && size==sbpr) {
529 size *= h;
530 h = 1;
531 }
532 do {
533 memcpy(d, s, size);
534 d += dbpr;
535 s += sbpr;
536 } while (--h > 0);
537 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700538 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700539}
540
541EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542{
Mathias Agopianabac0102009-07-31 14:47:00 -0700543 if (!buffer) {
544 return setError(EGL_BAD_ACCESS, EGL_FALSE);
545 }
546
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700547 /*
548 * Handle eglSetSwapRectangleANDROID()
549 * We copyback from the front buffer
550 */
551 if (!dirtyRegion.isEmpty()) {
552 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
553 if (previousBuffer) {
554 const Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
555 if (!copyBack.isEmpty()) {
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700556 void* prevBits;
557 if (lock(previousBuffer,
Mathias Agopian68eeb802009-06-25 15:39:25 -0700558 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
559 // copy from previousBuffer to buffer
560 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700561 unlock(previousBuffer);
562 }
563 }
564 }
565 oldDirtyRegion = dirtyRegion;
566 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700567
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700568 if (previousBuffer) {
569 previousBuffer->common.decRef(&previousBuffer->common);
570 previousBuffer = 0;
571 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700572
Mathias Agopiandff8e582009-05-04 14:17:04 -0700573 unlock(buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700574 previousBuffer = buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700575 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700576 buffer = 0;
Mathias Agopian1473f462009-04-10 14:24:30 -0700577
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700578 // dequeue a new buffer
Mathias Agopian1473f462009-04-10 14:24:30 -0700579 nativeWindow->dequeueBuffer(nativeWindow, &buffer);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700580
Mathias Agopian1473f462009-04-10 14:24:30 -0700581 // TODO: lockBuffer should rather be executed when the very first
582 // direct rendering occurs.
583 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700584
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700585 // reallocate the depth-buffer if needed
Mathias Agopian1473f462009-04-10 14:24:30 -0700586 if ((width != buffer->width) || (height != buffer->height)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 // TODO: we probably should reset the swap rect here
588 // if the window size has changed
Mathias Agopian1473f462009-04-10 14:24:30 -0700589 width = buffer->width;
590 height = buffer->height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 if (depth.data) {
592 free(depth.data);
Mathias Agopian1473f462009-04-10 14:24:30 -0700593 depth.width = width;
594 depth.height = height;
595 depth.stride = buffer->stride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
597 if (depth.data == 0) {
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700598 setError(EGL_BAD_ALLOC, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 return EGL_FALSE;
600 }
601 }
602 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700603
604 // keep a reference on the buffer
605 buffer->common.incRef(&buffer->common);
606
607 // finally pin the buffer down
608 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
609 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
610 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
611 buffer, buffer->width, buffer->height);
Mathias Agopianabac0102009-07-31 14:47:00 -0700612 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700613 // FIXME: we should make sure we're not accessing the buffer anymore
614 }
615
616 return EGL_TRUE;
617}
618
619EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
620 EGLint l, EGLint t, EGLint w, EGLint h)
621{
622 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 return EGL_TRUE;
624}
625
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700626EGLClientBuffer egl_window_surface_v2_t::getRenderBuffer() const
627{
628 return buffer;
629}
630
Mathias Agopian1473f462009-04-10 14:24:30 -0700631#ifdef LIBAGL_USE_GRALLOC_COPYBITS
632
633static bool supportedCopybitsDestinationFormat(int format) {
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700634 // Hardware supported
Mathias Agopian1473f462009-04-10 14:24:30 -0700635 switch (format) {
636 case HAL_PIXEL_FORMAT_RGB_565:
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700637 case HAL_PIXEL_FORMAT_RGBA_8888:
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800638 case HAL_PIXEL_FORMAT_RGBX_8888:
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700639 case HAL_PIXEL_FORMAT_RGBA_4444:
640 case HAL_PIXEL_FORMAT_RGBA_5551:
641 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian1473f462009-04-10 14:24:30 -0700642 return true;
Mathias Agopian1473f462009-04-10 14:24:30 -0700643 }
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700644 return false;
Mathias Agopian1473f462009-04-10 14:24:30 -0700645}
646#endif
647
648EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649{
650 GGLSurface buffer;
651 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700652 buffer.width = this->buffer->width;
653 buffer.height = this->buffer->height;
654 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700655 buffer.data = (GGLubyte*)bits;
Mathias Agopian1473f462009-04-10 14:24:30 -0700656 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 gl->rasterizer.procs.colorBuffer(gl, &buffer);
658 if (depth.data != gl->rasterizer.state.buffers.depth.data)
659 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian350d6512009-06-10 16:01:54 -0700660
Mathias Agopian1473f462009-04-10 14:24:30 -0700661#ifdef LIBAGL_USE_GRALLOC_COPYBITS
Mathias Agopian350d6512009-06-10 16:01:54 -0700662 gl->copybits.drawSurfaceBuffer = 0;
Mathias Agopiana2fb72e2009-07-15 18:53:32 -0700663 if (gl->copybits.blitEngine != NULL) {
664 if (supportedCopybitsDestinationFormat(buffer.format)) {
665 buffer_handle_t handle = this->buffer->handle;
666 if (handle != NULL) {
Mathias Agopian36d01842009-11-02 17:48:33 -0800667 gl->copybits.drawSurfaceBuffer = this->buffer;
Mathias Agopian1473f462009-04-10 14:24:30 -0700668 }
669 }
670 }
671#endif // LIBAGL_USE_GRALLOC_COPYBITS
Mathias Agopian350d6512009-06-10 16:01:54 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 return EGL_TRUE;
674}
Mathias Agopian1473f462009-04-10 14:24:30 -0700675EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676{
677 GGLSurface buffer;
678 buffer.version = sizeof(GGLSurface);
Mathias Agopian1473f462009-04-10 14:24:30 -0700679 buffer.width = this->buffer->width;
680 buffer.height = this->buffer->height;
681 buffer.stride = this->buffer->stride;
Mathias Agopian430f2ed2009-05-05 00:37:46 -0700682 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian1473f462009-04-10 14:24:30 -0700683 buffer.format = this->buffer->format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 gl->rasterizer.procs.readBuffer(gl, &buffer);
685 return EGL_TRUE;
686}
Mathias Agopian1473f462009-04-10 14:24:30 -0700687EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
689}
Mathias Agopian1473f462009-04-10 14:24:30 -0700690EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
692}
Mathias Agopian1473f462009-04-10 14:24:30 -0700693EGLint egl_window_surface_v2_t::getRefreshRate() const {
694 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695}
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700696EGLint egl_window_surface_v2_t::getSwapBehavior() const
697{
698 /*
699 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
700 * the content of the swapped buffer.
701 *
702 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
703 *
704 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
705 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
706 * is, everything outside of this area is preserved.
707 *
708 * This implementation of EGL assumes the later case.
709 *
710 */
711
Mathias Agopian1473f462009-04-10 14:24:30 -0700712 return EGL_BUFFER_DESTROYED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713}
714
715// ----------------------------------------------------------------------------
716
717struct egl_pixmap_surface_t : public egl_surface_t
718{
719 egl_pixmap_surface_t(
720 EGLDisplay dpy, EGLConfig config,
721 int32_t depthFormat,
722 egl_native_pixmap_t const * pixmap);
723
724 virtual ~egl_pixmap_surface_t() { }
725
Mathias Agopianffbc8642009-08-20 00:12:56 -0700726 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
728 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
729 virtual EGLint getWidth() const { return nativePixmap.width; }
730 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731private:
732 egl_native_pixmap_t nativePixmap;
733};
734
735egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
736 EGLConfig config,
737 int32_t depthFormat,
738 egl_native_pixmap_t const * pixmap)
739 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
740{
741 if (depthFormat) {
742 depth.width = pixmap->width;
743 depth.height = pixmap->height;
744 depth.stride = depth.width; // use the width here
745 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
746 if (depth.data == 0) {
747 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749 }
750}
751EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
752{
753 GGLSurface buffer;
754 buffer.version = sizeof(GGLSurface);
755 buffer.width = nativePixmap.width;
756 buffer.height = nativePixmap.height;
757 buffer.stride = nativePixmap.stride;
758 buffer.data = nativePixmap.data;
759 buffer.format = nativePixmap.format;
Mathias Agopian1473f462009-04-10 14:24:30 -0700760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 gl->rasterizer.procs.colorBuffer(gl, &buffer);
762 if (depth.data != gl->rasterizer.state.buffers.depth.data)
763 gl->rasterizer.procs.depthBuffer(gl, &depth);
764 return EGL_TRUE;
765}
766EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
767{
768 GGLSurface buffer;
769 buffer.version = sizeof(GGLSurface);
770 buffer.width = nativePixmap.width;
771 buffer.height = nativePixmap.height;
772 buffer.stride = nativePixmap.stride;
773 buffer.data = nativePixmap.data;
774 buffer.format = nativePixmap.format;
775 gl->rasterizer.procs.readBuffer(gl, &buffer);
776 return EGL_TRUE;
777}
778
779// ----------------------------------------------------------------------------
780
781struct egl_pbuffer_surface_t : public egl_surface_t
782{
783 egl_pbuffer_surface_t(
784 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
785 int32_t w, int32_t h, int32_t f);
786
787 virtual ~egl_pbuffer_surface_t();
788
Mathias Agopianffbc8642009-08-20 00:12:56 -0700789 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
791 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
792 virtual EGLint getWidth() const { return pbuffer.width; }
793 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794private:
795 GGLSurface pbuffer;
796};
797
798egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
799 EGLConfig config, int32_t depthFormat,
800 int32_t w, int32_t h, int32_t f)
801 : egl_surface_t(dpy, config, depthFormat)
802{
803 size_t size = w*h;
804 switch (f) {
805 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
806 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
807 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800808 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 default:
810 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
811 pbuffer.data = 0;
812 break;
813 }
814 pbuffer.version = sizeof(GGLSurface);
815 pbuffer.width = w;
816 pbuffer.height = h;
817 pbuffer.stride = w;
818 pbuffer.data = (GGLubyte*)malloc(size);
819 pbuffer.format = f;
Mathias Agopian1473f462009-04-10 14:24:30 -0700820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 if (depthFormat) {
822 depth.width = pbuffer.width;
823 depth.height = pbuffer.height;
824 depth.stride = depth.width; // use the width here
825 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
826 if (depth.data == 0) {
827 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
828 return;
829 }
830 }
831}
832egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
833 free(pbuffer.data);
834}
835EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
836{
837 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
838 if (depth.data != gl->rasterizer.state.buffers.depth.data)
839 gl->rasterizer.procs.depthBuffer(gl, &depth);
840 return EGL_TRUE;
841}
842EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
843{
844 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
845 return EGL_TRUE;
846}
847
848// ----------------------------------------------------------------------------
849
850struct config_pair_t {
851 GLint key;
852 GLint value;
853};
854
855struct configs_t {
856 const config_pair_t* array;
857 int size;
858};
859
860struct config_management_t {
861 GLint key;
862 bool (*match)(GLint reqValue, GLint confValue);
863 static bool atLeast(GLint reqValue, GLint confValue) {
864 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
865 }
866 static bool exact(GLint reqValue, GLint confValue) {
867 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
868 }
869 static bool mask(GLint reqValue, GLint confValue) {
870 return (confValue & reqValue) == reqValue;
871 }
872};
873
874// ----------------------------------------------------------------------------
875
876#define VERSION_MAJOR 1
877#define VERSION_MINOR 2
878static char const * const gVendorString = "Google Inc.";
Mathias Agopiancf335092009-11-12 15:19:42 -0800879static char const * const gVersionString = "1.2 Android Driver 1.1.0";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian1473f462009-04-10 14:24:30 -0700881static char const * const gExtensionsString =
Mathias Agopian927d37c2009-05-06 23:47:08 -0700882 "EGL_KHR_image_base "
Mathias Agopian1473f462009-04-10 14:24:30 -0700883 // "KHR_image_pixmap "
884 "EGL_ANDROID_image_native_buffer "
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700885 "EGL_ANDROID_swap_rectangle "
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700886 "EGL_ANDROID_get_render_buffer "
Mathias Agopian1473f462009-04-10 14:24:30 -0700887 ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888
889// ----------------------------------------------------------------------------
890
891struct extention_map_t {
892 const char * const name;
893 __eglMustCastToProperFunctionPointerType address;
894};
895
896static const extention_map_t gExtentionMap[] = {
Mathias Agopian1473f462009-04-10 14:24:30 -0700897 { "glDrawTexsOES",
898 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
899 { "glDrawTexiOES",
900 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
901 { "glDrawTexfOES",
902 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
903 { "glDrawTexxOES",
904 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
905 { "glDrawTexsvOES",
906 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
907 { "glDrawTexivOES",
908 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
909 { "glDrawTexfvOES",
910 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
911 { "glDrawTexxvOES",
912 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
913 { "glQueryMatrixxOES",
914 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
915 { "glEGLImageTargetTexture2DOES",
916 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
917 { "glEGLImageTargetRenderbufferStorageOES",
918 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
919 { "glClipPlanef",
920 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
921 { "glClipPlanex",
922 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
923 { "glBindBuffer",
924 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
925 { "glBufferData",
926 (__eglMustCastToProperFunctionPointerType)&glBufferData },
927 { "glBufferSubData",
928 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
929 { "glDeleteBuffers",
930 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
931 { "glGenBuffers",
932 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700933 { "eglCreateImageKHR",
934 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
935 { "eglDestroyImageKHR",
936 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
937 { "eglSetSwapRectangleANDROID",
938 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
939 { "eglGetRenderBufferANDROID",
940 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941};
942
Mathias Agopian1473f462009-04-10 14:24:30 -0700943/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 * In the lists below, attributes names MUST be sorted.
945 * Additionally, all configs must be sorted according to
946 * the EGL specification.
947 */
948
949static config_pair_t const config_base_attribute_list[] = {
950 { EGL_STENCIL_SIZE, 0 },
951 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
952 { EGL_LEVEL, 0 },
953 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian1473f462009-04-10 14:24:30 -0700954 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
956 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
957 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
958 { EGL_NATIVE_VISUAL_ID, 0 },
959 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
960 { EGL_SAMPLES, 0 },
961 { EGL_SAMPLE_BUFFERS, 0 },
962 { EGL_TRANSPARENT_TYPE, EGL_NONE },
963 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
964 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
965 { EGL_TRANSPARENT_RED_VALUE, 0 },
966 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
967 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
968 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700969 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700970 { EGL_LUMINANCE_SIZE, 0 },
971 { EGL_ALPHA_MASK_SIZE, 0 },
972 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian594d02e2009-09-27 20:18:16 -0700973 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0953c1d2009-10-19 14:46:27 -0700974 { EGL_CONFORMANT, 0 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975};
976
977// These configs can override the base attribute list
978// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
979
Mathias Agopian36fe3ee2009-11-03 16:17:55 -0800980// 565 configs
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981static config_pair_t const config_0_attribute_list[] = {
982 { EGL_BUFFER_SIZE, 16 },
983 { EGL_ALPHA_SIZE, 0 },
984 { EGL_BLUE_SIZE, 5 },
985 { EGL_GREEN_SIZE, 6 },
986 { EGL_RED_SIZE, 5 },
987 { EGL_DEPTH_SIZE, 0 },
988 { EGL_CONFIG_ID, 0 },
989 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
990};
991
992static config_pair_t const config_1_attribute_list[] = {
993 { EGL_BUFFER_SIZE, 16 },
994 { EGL_ALPHA_SIZE, 0 },
995 { EGL_BLUE_SIZE, 5 },
996 { EGL_GREEN_SIZE, 6 },
997 { EGL_RED_SIZE, 5 },
998 { EGL_DEPTH_SIZE, 16 },
999 { EGL_CONFIG_ID, 1 },
1000 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1001};
1002
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001003// RGB 888 configs
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004static config_pair_t const config_2_attribute_list[] = {
1005 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001006 { EGL_ALPHA_SIZE, 0 },
1007 { EGL_BLUE_SIZE, 8 },
1008 { EGL_GREEN_SIZE, 8 },
1009 { EGL_RED_SIZE, 8 },
1010 { EGL_DEPTH_SIZE, 0 },
1011 { EGL_CONFIG_ID, 6 },
1012 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1013};
1014
1015static config_pair_t const config_3_attribute_list[] = {
1016 { EGL_BUFFER_SIZE, 32 },
1017 { EGL_ALPHA_SIZE, 0 },
1018 { EGL_BLUE_SIZE, 8 },
1019 { EGL_GREEN_SIZE, 8 },
1020 { EGL_RED_SIZE, 8 },
1021 { EGL_DEPTH_SIZE, 16 },
1022 { EGL_CONFIG_ID, 7 },
1023 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1024};
1025
1026// 8888 configs
1027static config_pair_t const config_4_attribute_list[] = {
1028 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 { EGL_ALPHA_SIZE, 8 },
1030 { EGL_BLUE_SIZE, 8 },
1031 { EGL_GREEN_SIZE, 8 },
1032 { EGL_RED_SIZE, 8 },
1033 { EGL_DEPTH_SIZE, 0 },
1034 { EGL_CONFIG_ID, 2 },
1035 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1036};
1037
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001038static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 { EGL_BUFFER_SIZE, 32 },
1040 { EGL_ALPHA_SIZE, 8 },
1041 { EGL_BLUE_SIZE, 8 },
1042 { EGL_GREEN_SIZE, 8 },
1043 { EGL_RED_SIZE, 8 },
1044 { EGL_DEPTH_SIZE, 16 },
1045 { EGL_CONFIG_ID, 3 },
1046 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1047};
1048
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001049// A8 configs
1050static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 { EGL_BUFFER_SIZE, 8 },
1052 { EGL_ALPHA_SIZE, 8 },
1053 { EGL_BLUE_SIZE, 0 },
1054 { EGL_GREEN_SIZE, 0 },
1055 { EGL_RED_SIZE, 0 },
1056 { EGL_DEPTH_SIZE, 0 },
1057 { EGL_CONFIG_ID, 4 },
1058 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1059};
1060
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001061static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 { EGL_BUFFER_SIZE, 8 },
1063 { EGL_ALPHA_SIZE, 8 },
1064 { EGL_BLUE_SIZE, 0 },
1065 { EGL_GREEN_SIZE, 0 },
1066 { EGL_RED_SIZE, 0 },
1067 { EGL_DEPTH_SIZE, 16 },
1068 { EGL_CONFIG_ID, 5 },
1069 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1070};
1071
1072static configs_t const gConfigs[] = {
1073 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1074 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1075 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1076 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1077 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1078 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001079 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1080 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081};
1082
1083static config_management_t const gConfigManagement[] = {
1084 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1085 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1086 { EGL_BLUE_SIZE, config_management_t::atLeast },
1087 { EGL_GREEN_SIZE, config_management_t::atLeast },
1088 { EGL_RED_SIZE, config_management_t::atLeast },
1089 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1090 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1091 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1092 { EGL_CONFIG_ID, config_management_t::exact },
1093 { EGL_LEVEL, config_management_t::exact },
1094 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::exact },
1095 { EGL_MAX_PBUFFER_PIXELS, config_management_t::exact },
1096 { EGL_MAX_PBUFFER_WIDTH, config_management_t::exact },
1097 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
1098 { EGL_NATIVE_VISUAL_ID, config_management_t::exact },
1099 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1100 { EGL_SAMPLES, config_management_t::exact },
1101 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1102 { EGL_SURFACE_TYPE, config_management_t::mask },
1103 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1104 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1105 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1106 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1107 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1108 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1109 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1110 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001111 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1112 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1113 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1114 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1115 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116};
1117
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119static config_pair_t const config_defaults[] = {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001120 // attributes that are not specified are simply ignored, if a particular
1121 // one needs not be ignored, it must be specified here, eg:
1122 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123};
1124
1125// ----------------------------------------------------------------------------
1126
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001127static status_t getConfigFormatInfo(EGLint configID,
1128 int32_t& pixelFormat, int32_t& depthFormat)
1129{
1130 switch(configID) {
1131 case 0:
1132 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1133 depthFormat = 0;
1134 break;
1135 case 1:
1136 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1137 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1138 break;
1139 case 2:
1140 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1141 depthFormat = 0;
1142 break;
1143 case 3:
1144 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1145 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1146 break;
1147 case 4:
1148 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1149 depthFormat = 0;
1150 break;
1151 case 5:
1152 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1153 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1154 break;
1155 case 6:
1156 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1157 depthFormat = 0;
1158 break;
1159 case 7:
1160 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1161 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1162 break;
1163 default:
1164 return NAME_NOT_FOUND;
1165 }
1166 return NO_ERROR;
1167}
1168
1169// ----------------------------------------------------------------------------
1170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171template<typename T>
1172static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1173{
1174 while (first <= last) {
1175 int mid = (first + last) / 2;
Mathias Agopian1473f462009-04-10 14:24:30 -07001176 if (key > sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 first = mid + 1;
Mathias Agopian1473f462009-04-10 14:24:30 -07001178 } else if (key < sortedArray[mid].key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 last = mid - 1;
1180 } else {
1181 return mid;
1182 }
1183 }
1184 return -1;
1185}
1186
1187static int isAttributeMatching(int i, EGLint attr, EGLint val)
1188{
1189 // look for the attribute in all of our configs
Mathias Agopian1473f462009-04-10 14:24:30 -07001190 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 int index = binarySearch<config_pair_t>(
1192 gConfigs[i].array,
1193 0, gConfigs[i].size-1,
1194 attr);
1195 if (index < 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001196 configFound = config_base_attribute_list;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 index = binarySearch<config_pair_t>(
1198 config_base_attribute_list,
1199 0, NELEM(config_base_attribute_list)-1,
1200 attr);
1201 }
1202 if (index >= 0) {
1203 // attribute found, check if this config could match
1204 int cfgMgtIndex = binarySearch<config_management_t>(
1205 gConfigManagement,
1206 0, NELEM(gConfigManagement)-1,
1207 attr);
1208 if (index >= 0) {
1209 bool match = gConfigManagement[cfgMgtIndex].match(
1210 val, configFound[index].value);
1211 if (match) {
1212 // this config matches
1213 return 1;
1214 }
1215 } else {
1216 // attribute not found. this should NEVER happen.
1217 }
1218 } else {
1219 // error, this attribute doesn't exist
1220 }
1221 return 0;
1222}
1223
1224static int makeCurrent(ogles_context_t* gl)
1225{
1226 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1227 if (gl) {
1228 egl_context_t* c = egl_context_t::context(gl);
1229 if (c->flags & egl_context_t::IS_CURRENT) {
1230 if (current != gl) {
1231 // it is an error to set a context current, if it's already
1232 // current to another thread
1233 return -1;
1234 }
1235 } else {
1236 if (current) {
1237 // mark the current context as not current, and flush
1238 glFlush();
1239 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1240 }
1241 }
1242 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1243 // The context is not current, make it current!
1244 setGlThreadSpecific(gl);
1245 c->flags |= egl_context_t::IS_CURRENT;
1246 }
1247 } else {
1248 if (current) {
1249 // mark the current context as not current, and flush
1250 glFlush();
1251 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1252 }
1253 // this thread has no context attached to it
1254 setGlThreadSpecific(0);
1255 }
1256 return 0;
1257}
1258
1259static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1260 EGLint attribute, EGLint *value)
1261{
1262 size_t numConfigs = NELEM(gConfigs);
1263 int index = (int)config;
1264 if (uint32_t(index) >= numConfigs)
1265 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1266
1267 int attrIndex;
1268 attrIndex = binarySearch<config_pair_t>(
1269 gConfigs[index].array,
1270 0, gConfigs[index].size-1,
1271 attribute);
1272 if (attrIndex>=0) {
1273 *value = gConfigs[index].array[attrIndex].value;
1274 return EGL_TRUE;
1275 }
1276
1277 attrIndex = binarySearch<config_pair_t>(
1278 config_base_attribute_list,
1279 0, NELEM(config_base_attribute_list)-1,
1280 attribute);
1281 if (attrIndex>=0) {
1282 *value = config_base_attribute_list[attrIndex].value;
1283 return EGL_TRUE;
1284 }
1285 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1286}
1287
1288static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1289 NativeWindowType window, const EGLint *attrib_list)
1290{
1291 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1292 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1293 if (window == 0)
1294 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1295
1296 EGLint surfaceType;
1297 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1298 return EGL_FALSE;
1299
1300 if (!(surfaceType & EGL_WINDOW_BIT))
1301 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1302
Mathias Agopianffbc8642009-08-20 00:12:56 -07001303 if (static_cast<android_native_window_t*>(window)->common.magic !=
1304 ANDROID_NATIVE_WINDOW_MAGIC) {
1305 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1306 }
1307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 EGLint configID;
1309 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1310 return EGL_FALSE;
1311
1312 int32_t depthFormat;
1313 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001314 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1316 }
1317
1318 // FIXME: we don't have access to the pixelFormat here just yet.
1319 // (it's possible that the surface is not fully initialized)
1320 // maybe this should be done after the page-flip
1321 //if (EGLint(info.format) != pixelFormat)
1322 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1323
Mathias Agopian1473f462009-04-10 14:24:30 -07001324 egl_surface_t* surface;
1325 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
1326 static_cast<android_native_window_t*>(window));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327
Mathias Agopianffbc8642009-08-20 00:12:56 -07001328 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 // there was a problem in the ctor, the error
1330 // flag has been set.
1331 delete surface;
1332 surface = 0;
1333 }
1334 return surface;
1335}
1336
1337static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1338 NativePixmapType pixmap, const EGLint *attrib_list)
1339{
1340 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1341 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1342 if (pixmap == 0)
1343 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1344
1345 EGLint surfaceType;
1346 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1347 return EGL_FALSE;
1348
1349 if (!(surfaceType & EGL_PIXMAP_BIT))
1350 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1351
Mathias Agopianffbc8642009-08-20 00:12:56 -07001352 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1353 sizeof(egl_native_pixmap_t)) {
1354 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1355 }
1356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 EGLint configID;
1358 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1359 return EGL_FALSE;
1360
1361 int32_t depthFormat;
1362 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001363 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1365 }
1366
1367 if (pixmap->format != pixelFormat)
1368 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1369
1370 egl_surface_t* surface =
1371 new egl_pixmap_surface_t(dpy, config, depthFormat,
1372 static_cast<egl_native_pixmap_t*>(pixmap));
1373
Mathias Agopianffbc8642009-08-20 00:12:56 -07001374 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 // there was a problem in the ctor, the error
1376 // flag has been set.
1377 delete surface;
1378 surface = 0;
1379 }
1380 return surface;
1381}
1382
1383static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1384 const EGLint *attrib_list)
1385{
1386 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1387 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1388
1389 EGLint surfaceType;
1390 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1391 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 if (!(surfaceType & EGL_PBUFFER_BIT))
1394 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 EGLint configID;
1397 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1398 return EGL_FALSE;
1399
1400 int32_t depthFormat;
1401 int32_t pixelFormat;
Mathias Agopian36fe3ee2009-11-03 16:17:55 -08001402 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1404 }
1405
1406 int32_t w = 0;
1407 int32_t h = 0;
1408 while (attrib_list[0]) {
1409 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1410 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1411 attrib_list+=2;
1412 }
1413
1414 egl_surface_t* surface =
1415 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1416
Mathias Agopianffbc8642009-08-20 00:12:56 -07001417 if (!surface->initCheck()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 // there was a problem in the ctor, the error
1419 // flag has been set.
1420 delete surface;
1421 surface = 0;
1422 }
1423 return surface;
1424}
1425
1426// ----------------------------------------------------------------------------
1427}; // namespace android
1428// ----------------------------------------------------------------------------
1429
1430using namespace android;
1431
1432// ----------------------------------------------------------------------------
1433// Initialization
1434// ----------------------------------------------------------------------------
1435
1436EGLDisplay eglGetDisplay(NativeDisplayType display)
1437{
1438#ifndef HAVE_ANDROID_OS
1439 // this just needs to be done once
1440 if (gGLKey == -1) {
1441 pthread_mutex_lock(&gInitMutex);
1442 if (gGLKey == -1)
1443 pthread_key_create(&gGLKey, NULL);
1444 pthread_mutex_unlock(&gInitMutex);
1445 }
1446#endif
1447 if (display == EGL_DEFAULT_DISPLAY) {
1448 EGLDisplay dpy = (EGLDisplay)1;
1449 egl_display_t& d = egl_display_t::get_display(dpy);
1450 d.type = display;
1451 return dpy;
Mathias Agopian1473f462009-04-10 14:24:30 -07001452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 return EGL_NO_DISPLAY;
1454}
1455
1456EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1457{
1458 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1459 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 EGLBoolean res = EGL_TRUE;
1462 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -07001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 if (android_atomic_inc(&d.initialized) == 0) {
1465 // initialize stuff here if needed
1466 //pthread_mutex_lock(&gInitMutex);
1467 //pthread_mutex_unlock(&gInitMutex);
1468 }
1469
1470 if (res == EGL_TRUE) {
1471 if (major != NULL) *major = VERSION_MAJOR;
1472 if (minor != NULL) *minor = VERSION_MINOR;
1473 }
1474 return res;
1475}
1476
1477EGLBoolean eglTerminate(EGLDisplay dpy)
1478{
1479 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1480 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1481
1482 EGLBoolean res = EGL_TRUE;
1483 egl_display_t& d = egl_display_t::get_display(dpy);
1484 if (android_atomic_dec(&d.initialized) == 1) {
1485 // TODO: destroy all resources (surfaces, contexts, etc...)
1486 //pthread_mutex_lock(&gInitMutex);
1487 //pthread_mutex_unlock(&gInitMutex);
1488 }
1489 return res;
1490}
1491
1492// ----------------------------------------------------------------------------
1493// configuration
1494// ----------------------------------------------------------------------------
1495
1496EGLBoolean eglGetConfigs( EGLDisplay dpy,
1497 EGLConfig *configs,
1498 EGLint config_size, EGLint *num_config)
1499{
1500 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1501 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1502
1503 GLint numConfigs = NELEM(gConfigs);
1504 if (!configs) {
1505 *num_config = numConfigs;
1506 return EGL_TRUE;
1507 }
1508 GLint i;
1509 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1510 *configs++ = (EGLConfig)i;
1511 }
1512 *num_config = i;
1513 return EGL_TRUE;
1514}
1515
1516EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1517 EGLConfig *configs, EGLint config_size,
1518 EGLint *num_config)
1519{
1520 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1521 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich1badb712009-03-25 15:12:17 -07001522
1523 if (ggl_unlikely(num_config==0)) {
1524 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526
Jack Palevich1badb712009-03-25 15:12:17 -07001527 if (ggl_unlikely(attrib_list==0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 *num_config = 0;
1529 return EGL_TRUE;
1530 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 int numAttributes = 0;
1533 int numConfigs = NELEM(gConfigs);
1534 uint32_t possibleMatch = (1<<numConfigs)-1;
1535 while(possibleMatch && *attrib_list != EGL_NONE) {
1536 numAttributes++;
1537 EGLint attr = *attrib_list++;
1538 EGLint val = *attrib_list++;
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001539 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 if (!(possibleMatch & (1<<i)))
1541 continue;
1542 if (isAttributeMatching(i, attr, val) == 0) {
1543 possibleMatch &= ~(1<<i);
1544 }
1545 }
1546 }
1547
1548 // now, handle the attributes which have a useful default value
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001549 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1550 // see if this attribute was specified, if not, apply its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 // default value
1552 if (binarySearch<config_pair_t>(
1553 (config_pair_t const*)attrib_list,
Mathias Agopianab1cf3e2009-07-09 17:33:15 -07001554 0, numAttributes-1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 config_defaults[j].key) < 0)
1556 {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001557 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (!(possibleMatch & (1<<i)))
1559 continue;
1560 if (isAttributeMatching(i,
1561 config_defaults[j].key,
1562 config_defaults[j].value) == 0)
1563 {
1564 possibleMatch &= ~(1<<i);
1565 }
1566 }
1567 }
1568 }
1569
1570 // return the configurations found
1571 int n=0;
1572 if (possibleMatch) {
Jack Palevich1badb712009-03-25 15:12:17 -07001573 if (configs) {
1574 for (int i=0 ; config_size && i<numConfigs ; i++) {
1575 if (possibleMatch & (1<<i)) {
1576 *configs++ = (EGLConfig)i;
1577 config_size--;
1578 n++;
1579 }
1580 }
1581 } else {
1582 for (int i=0 ; i<numConfigs ; i++) {
1583 if (possibleMatch & (1<<i)) {
1584 n++;
1585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
1587 }
1588 }
1589 *num_config = n;
1590 return EGL_TRUE;
1591}
1592
1593EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1594 EGLint attribute, EGLint *value)
1595{
1596 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1597 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1598
1599 return getConfigAttrib(dpy, config, attribute, value);
1600}
1601
1602// ----------------------------------------------------------------------------
1603// surfaces
1604// ----------------------------------------------------------------------------
1605
1606EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1607 NativeWindowType window,
1608 const EGLint *attrib_list)
1609{
1610 return createWindowSurface(dpy, config, window, attrib_list);
1611}
Mathias Agopian1473f462009-04-10 14:24:30 -07001612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1614 NativePixmapType pixmap,
1615 const EGLint *attrib_list)
1616{
1617 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1618}
1619
1620EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1621 const EGLint *attrib_list)
1622{
1623 return createPbufferSurface(dpy, config, attrib_list);
1624}
Mathias Agopian1473f462009-04-10 14:24:30 -07001625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1627{
1628 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1629 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1630 if (eglSurface != EGL_NO_SURFACE) {
1631 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopianffbc8642009-08-20 00:12:56 -07001632 if (!surface->isValid())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1634 if (surface->dpy != dpy)
1635 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001636 if (surface->ctx) {
1637 // FIXME: this surface is current check what the spec says
1638 surface->disconnect();
1639 surface->ctx = 0;
1640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 delete surface;
1642 }
1643 return EGL_TRUE;
1644}
1645
1646EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1647 EGLint attribute, EGLint *value)
1648{
1649 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1650 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1651 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001652 if (!surface->isValid())
1653 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (surface->dpy != dpy)
1655 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1656
1657 EGLBoolean ret = EGL_TRUE;
1658 switch (attribute) {
1659 case EGL_CONFIG_ID:
1660 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1661 break;
1662 case EGL_WIDTH:
1663 *value = surface->getWidth();
1664 break;
1665 case EGL_HEIGHT:
1666 *value = surface->getHeight();
1667 break;
1668 case EGL_LARGEST_PBUFFER:
1669 // not modified for a window or pixmap surface
1670 break;
1671 case EGL_TEXTURE_FORMAT:
1672 *value = EGL_NO_TEXTURE;
1673 break;
1674 case EGL_TEXTURE_TARGET:
1675 *value = EGL_NO_TEXTURE;
1676 break;
1677 case EGL_MIPMAP_TEXTURE:
1678 *value = EGL_FALSE;
1679 break;
1680 case EGL_MIPMAP_LEVEL:
1681 *value = 0;
1682 break;
1683 case EGL_RENDER_BUFFER:
1684 // TODO: return the real RENDER_BUFFER here
1685 *value = EGL_BACK_BUFFER;
1686 break;
1687 case EGL_HORIZONTAL_RESOLUTION:
1688 // pixel/mm * EGL_DISPLAY_SCALING
1689 *value = surface->getHorizontalResolution();
1690 break;
1691 case EGL_VERTICAL_RESOLUTION:
1692 // pixel/mm * EGL_DISPLAY_SCALING
1693 *value = surface->getVerticalResolution();
1694 break;
1695 case EGL_PIXEL_ASPECT_RATIO: {
1696 // w/h * EGL_DISPLAY_SCALING
1697 int wr = surface->getHorizontalResolution();
1698 int hr = surface->getVerticalResolution();
1699 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1700 } break;
1701 case EGL_SWAP_BEHAVIOR:
Mathias Agopian1473f462009-04-10 14:24:30 -07001702 *value = surface->getSwapBehavior();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 break;
1704 default:
1705 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1706 }
1707 return ret;
1708}
1709
1710EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1711 EGLContext share_list, const EGLint *attrib_list)
1712{
1713 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1714 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1715
1716 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1717 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1718
1719 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1720 c->flags = egl_context_t::NEVER_CURRENT;
1721 c->dpy = dpy;
1722 c->config = config;
1723 c->read = 0;
1724 c->draw = 0;
1725 return (EGLContext)gl;
1726}
1727
1728EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1729{
1730 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1731 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1732 egl_context_t* c = egl_context_t::context(ctx);
1733 if (c->flags & egl_context_t::IS_CURRENT)
1734 setGlThreadSpecific(0);
1735 ogles_uninit((ogles_context_t*)ctx);
1736 return EGL_TRUE;
1737}
1738
1739EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1740 EGLSurface read, EGLContext ctx)
1741{
1742 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1743 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1744 if (draw) {
1745 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopianffbc8642009-08-20 00:12:56 -07001746 if (!s->isValid())
1747 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 if (s->dpy != dpy)
1749 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001750 // TODO: check that draw is compatible with the context
1751 }
1752 if (read && read!=draw) {
1753 egl_surface_t* s = (egl_surface_t*)read;
1754 if (!s->isValid())
1755 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1756 if (s->dpy != dpy)
1757 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1758 // TODO: check that read is compatible with the context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760
1761 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian1473f462009-04-10 14:24:30 -07001762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1764 return setError(EGL_BAD_MATCH, EGL_FALSE);
1765
1766 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1767 return setError(EGL_BAD_MATCH, EGL_FALSE);
1768
1769 if (ctx == EGL_NO_CONTEXT) {
1770 // if we're detaching, we need the current context
1771 current_ctx = (EGLContext)getGlThreadSpecific();
1772 } else {
1773 egl_context_t* c = egl_context_t::context(ctx);
1774 egl_surface_t* d = (egl_surface_t*)draw;
1775 egl_surface_t* r = (egl_surface_t*)read;
1776 if ((d && d->ctx && d->ctx != ctx) ||
1777 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001778 // one of the surface is bound to a context in another thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1780 }
1781 }
1782
1783 ogles_context_t* gl = (ogles_context_t*)ctx;
1784 if (makeCurrent(gl) == 0) {
1785 if (ctx) {
1786 egl_context_t* c = egl_context_t::context(ctx);
1787 egl_surface_t* d = (egl_surface_t*)draw;
1788 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001789
1790 if (c->draw) {
Mathias Agopianffbc8642009-08-20 00:12:56 -07001791 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1792 s->disconnect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001793 }
1794 if (c->read) {
1795 // FIXME: unlock/disconnect the read surface too
1796 }
1797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 c->draw = draw;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001799 c->read = read;
1800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if (c->flags & egl_context_t::NEVER_CURRENT) {
1802 c->flags &= ~egl_context_t::NEVER_CURRENT;
1803 GLint w = 0;
1804 GLint h = 0;
1805 if (draw) {
1806 w = d->getWidth();
1807 h = d->getHeight();
1808 }
1809 ogles_surfaceport(gl, 0, 0);
1810 ogles_viewport(gl, 0, 0, w, h);
1811 ogles_scissor(gl, 0, 0, w, h);
1812 }
1813 if (d) {
Mathias Agopianabac0102009-07-31 14:47:00 -07001814 if (d->connect() == EGL_FALSE) {
1815 return EGL_FALSE;
1816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 d->ctx = ctx;
1818 d->bindDrawSurface(gl);
1819 }
1820 if (r) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001821 // FIXME: lock/connect the read surface too
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 r->ctx = ctx;
1823 r->bindReadSurface(gl);
1824 }
1825 } else {
1826 // if surfaces were bound to the context bound to this thread
1827 // mark then as unbound.
1828 if (current_ctx) {
1829 egl_context_t* c = egl_context_t::context(current_ctx);
1830 egl_surface_t* d = (egl_surface_t*)c->draw;
1831 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001832 if (d) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001833 c->draw = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001834 d->ctx = EGL_NO_CONTEXT;
1835 d->disconnect();
1836 }
1837 if (r) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001838 c->read = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001839 r->ctx = EGL_NO_CONTEXT;
1840 // FIXME: unlock/disconnect the read surface too
1841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843 }
1844 return EGL_TRUE;
1845 }
1846 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1847}
1848
1849EGLContext eglGetCurrentContext(void)
1850{
1851 // eglGetCurrentContext returns the current EGL rendering context,
1852 // as specified by eglMakeCurrent. If no context is current,
1853 // EGL_NO_CONTEXT is returned.
1854 return (EGLContext)getGlThreadSpecific();
1855}
1856
1857EGLSurface eglGetCurrentSurface(EGLint readdraw)
1858{
1859 // eglGetCurrentSurface returns the read or draw surface attached
1860 // to the current EGL rendering context, as specified by eglMakeCurrent.
1861 // If no context is current, EGL_NO_SURFACE is returned.
1862 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1863 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1864 egl_context_t* c = egl_context_t::context(ctx);
1865 if (readdraw == EGL_READ) {
1866 return c->read;
1867 } else if (readdraw == EGL_DRAW) {
1868 return c->draw;
1869 }
1870 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1871}
1872
1873EGLDisplay eglGetCurrentDisplay(void)
1874{
1875 // eglGetCurrentDisplay returns the current EGL display connection
1876 // for the current EGL rendering context, as specified by eglMakeCurrent.
1877 // If no context is current, EGL_NO_DISPLAY is returned.
1878 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1879 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1880 egl_context_t* c = egl_context_t::context(ctx);
1881 return c->dpy;
1882}
1883
1884EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1885 EGLint attribute, EGLint *value)
1886{
1887 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1888 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1889 egl_context_t* c = egl_context_t::context(ctx);
1890 switch (attribute) {
1891 case EGL_CONFIG_ID:
1892 // Returns the ID of the EGL frame buffer configuration with
1893 // respect to which the context was created
1894 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1895 }
1896 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1897}
1898
1899EGLBoolean eglWaitGL(void)
1900{
1901 return EGL_TRUE;
1902}
1903
1904EGLBoolean eglWaitNative(EGLint engine)
1905{
1906 return EGL_TRUE;
1907}
1908
1909EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1910{
1911 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1912 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001915 if (!d->isValid())
1916 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 if (d->dpy != dpy)
1918 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1919
1920 // post the surface
1921 d->swapBuffers();
1922
1923 // if it's bound to a context, update the buffer
1924 if (d->ctx != EGL_NO_CONTEXT) {
1925 d->bindDrawSurface((ogles_context_t*)d->ctx);
1926 // if this surface is also the read surface of the context
1927 // it is bound to, make sure to update the read buffer as well.
1928 // The EGL spec is a little unclear about this.
1929 egl_context_t* c = egl_context_t::context(d->ctx);
1930 if (c->read == draw) {
1931 d->bindReadSurface((ogles_context_t*)d->ctx);
1932 }
1933 }
1934
1935 return EGL_TRUE;
1936}
1937
1938EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1939 NativePixmapType target)
1940{
1941 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1942 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1943 // TODO: eglCopyBuffers()
1944 return EGL_FALSE;
1945}
1946
1947EGLint eglGetError(void)
1948{
1949 return getError();
1950}
1951
1952const char* eglQueryString(EGLDisplay dpy, EGLint name)
1953{
1954 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1955 return setError(EGL_BAD_DISPLAY, (const char*)0);
1956
1957 switch (name) {
1958 case EGL_VENDOR:
1959 return gVendorString;
1960 case EGL_VERSION:
1961 return gVersionString;
1962 case EGL_EXTENSIONS:
1963 return gExtensionsString;
1964 case EGL_CLIENT_APIS:
1965 return gClientApiString;
1966 }
1967 return setError(EGL_BAD_PARAMETER, (const char *)0);
1968}
1969
1970// ----------------------------------------------------------------------------
1971// EGL 1.1
1972// ----------------------------------------------------------------------------
1973
1974EGLBoolean eglSurfaceAttrib(
1975 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1976{
1977 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1978 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1979 // TODO: eglSurfaceAttrib()
1980 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1981}
1982
1983EGLBoolean eglBindTexImage(
1984 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1985{
1986 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1987 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1988 // TODO: eglBindTexImage()
1989 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1990}
1991
1992EGLBoolean eglReleaseTexImage(
1993 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1994{
1995 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1996 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1997 // TODO: eglReleaseTexImage()
1998 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1999}
2000
2001EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
2002{
2003 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2004 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2005 // TODO: eglSwapInterval()
2006 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2007}
2008
2009// ----------------------------------------------------------------------------
2010// EGL 1.2
2011// ----------------------------------------------------------------------------
2012
2013EGLBoolean eglBindAPI(EGLenum api)
2014{
2015 if (api != EGL_OPENGL_ES_API)
2016 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2017 return EGL_TRUE;
2018}
2019
2020EGLenum eglQueryAPI(void)
2021{
2022 return EGL_OPENGL_ES_API;
2023}
2024
2025EGLBoolean eglWaitClient(void)
2026{
2027 glFinish();
2028 return EGL_TRUE;
2029}
2030
2031EGLBoolean eglReleaseThread(void)
2032{
2033 // TODO: eglReleaseThread()
2034 return EGL_TRUE;
2035}
2036
2037EGLSurface eglCreatePbufferFromClientBuffer(
2038 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2039 EGLConfig config, const EGLint *attrib_list)
2040{
2041 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2042 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2043 // TODO: eglCreatePbufferFromClientBuffer()
2044 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2045}
2046
2047// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -07002048// EGL_EGLEXT_VERSION 3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049// ----------------------------------------------------------------------------
2050
2051void (*eglGetProcAddress (const char *procname))()
2052{
2053 extention_map_t const * const map = gExtentionMap;
2054 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2055 if (!strcmp(procname, map[i].name)) {
2056 return map[i].address;
2057 }
2058 }
2059 return NULL;
2060}
Mathias Agopian1473f462009-04-10 14:24:30 -07002061
2062EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2063 const EGLint *attrib_list)
2064{
2065 EGLBoolean result = EGL_FALSE;
2066 return result;
2067}
2068
2069EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2070{
2071 EGLBoolean result = EGL_FALSE;
2072 return result;
2073}
2074
2075EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2076 EGLClientBuffer buffer, const EGLint *attrib_list)
2077{
2078 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2079 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2080 }
2081 if (ctx != EGL_NO_CONTEXT) {
2082 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2083 }
2084 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2085 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2086 }
2087
2088 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2089
2090 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2091 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2092
2093 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2094 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopianaf9a5152009-04-10 20:34:46 -07002095
Mathias Agopian1473f462009-04-10 14:24:30 -07002096 native_buffer->common.incRef(&native_buffer->common);
2097 return (EGLImageKHR)native_buffer;
2098}
2099
2100EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2101{
2102 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2103 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2104 }
2105
2106 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2107
2108 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2109 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2110
2111 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2112 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2113
Mathias Agopian1473f462009-04-10 14:24:30 -07002114 native_buffer->common.decRef(&native_buffer->common);
2115
2116 return EGL_TRUE;
2117}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002118
2119// ----------------------------------------------------------------------------
2120// ANDROID extensions
2121// ----------------------------------------------------------------------------
2122
2123EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2124 EGLint left, EGLint top, EGLint width, EGLint height)
2125{
2126 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2127 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2128
2129 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002130 if (!d->isValid())
2131 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002132 if (d->dpy != dpy)
2133 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2134
2135 // post the surface
2136 d->setSwapRectangle(left, top, width, height);
2137
2138 return EGL_TRUE;
2139}
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002140
2141EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
2142{
2143 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2144 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2145
2146 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002147 if (!d->isValid())
2148 return setError(EGL_BAD_SURFACE, (EGLClientBuffer)0);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002149 if (d->dpy != dpy)
2150 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2151
2152 // post the surface
2153 return d->getRenderBuffer();
2154}