blob: 32e6c9f7ef3c3f58a3648ac421891a3161a99c5e [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,
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 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);
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700238 ANativeWindow* nativeWindow;
Mathias Agopian1473f462009-04-10 14:24:30 -0700239 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,
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700358 ANativeWindow* 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;
Mathias Palmqvist89995152010-06-02 16:03:04 +0200488 simg.w = src->stride;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700489 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;
Mathias Palmqvist89995152010-06-02 16:03:04 +0200494 dimg.w = dst->stride;
Mathias Agopian68eeb802009-06-25 15:39:25 -0700495 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);
Christoffer Gurell4a783af2009-10-12 11:57:27 +02001208 if (cfgMgtIndex >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 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
Dianne Hackborn8b49bd12010-06-30 13:56:17 -07001303 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopianffbc8642009-08-20 00:12:56 -07001304 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,
Dianne Hackborn8b49bd12010-06-30 13:56:17 -07001326 static_cast<ANativeWindow*>(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)) {
Mathias Agopian7e71fcf2010-05-17 14:45:43 -07001528 /*
1529 * A NULL attrib_list should be treated as though it was an empty
1530 * one (terminated with EGL_NONE) as defined in
1531 * section 3.4.1 "Querying Configurations" in the EGL specification.
1532 */
1533 static const EGLint dummy = EGL_NONE;
1534 attrib_list = &dummy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 int numAttributes = 0;
1538 int numConfigs = NELEM(gConfigs);
1539 uint32_t possibleMatch = (1<<numConfigs)-1;
1540 while(possibleMatch && *attrib_list != EGL_NONE) {
1541 numAttributes++;
1542 EGLint attr = *attrib_list++;
1543 EGLint val = *attrib_list++;
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001544 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (!(possibleMatch & (1<<i)))
1546 continue;
1547 if (isAttributeMatching(i, attr, val) == 0) {
1548 possibleMatch &= ~(1<<i);
1549 }
1550 }
1551 }
1552
1553 // now, handle the attributes which have a useful default value
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001554 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1555 // see if this attribute was specified, if not, apply its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 // default value
1557 if (binarySearch<config_pair_t>(
1558 (config_pair_t const*)attrib_list,
Mathias Agopianab1cf3e2009-07-09 17:33:15 -07001559 0, numAttributes-1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 config_defaults[j].key) < 0)
1561 {
Mathias Agopian0953c1d2009-10-19 14:46:27 -07001562 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (!(possibleMatch & (1<<i)))
1564 continue;
1565 if (isAttributeMatching(i,
1566 config_defaults[j].key,
1567 config_defaults[j].value) == 0)
1568 {
1569 possibleMatch &= ~(1<<i);
1570 }
1571 }
1572 }
1573 }
1574
1575 // return the configurations found
1576 int n=0;
1577 if (possibleMatch) {
Jack Palevich1badb712009-03-25 15:12:17 -07001578 if (configs) {
1579 for (int i=0 ; config_size && i<numConfigs ; i++) {
1580 if (possibleMatch & (1<<i)) {
1581 *configs++ = (EGLConfig)i;
1582 config_size--;
1583 n++;
1584 }
1585 }
1586 } else {
1587 for (int i=0 ; i<numConfigs ; i++) {
1588 if (possibleMatch & (1<<i)) {
1589 n++;
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592 }
1593 }
1594 *num_config = n;
1595 return EGL_TRUE;
1596}
1597
1598EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1599 EGLint attribute, EGLint *value)
1600{
1601 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1602 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1603
1604 return getConfigAttrib(dpy, config, attribute, value);
1605}
1606
1607// ----------------------------------------------------------------------------
1608// surfaces
1609// ----------------------------------------------------------------------------
1610
1611EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1612 NativeWindowType window,
1613 const EGLint *attrib_list)
1614{
1615 return createWindowSurface(dpy, config, window, attrib_list);
1616}
Mathias Agopian1473f462009-04-10 14:24:30 -07001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1619 NativePixmapType pixmap,
1620 const EGLint *attrib_list)
1621{
1622 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1623}
1624
1625EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1626 const EGLint *attrib_list)
1627{
1628 return createPbufferSurface(dpy, config, attrib_list);
1629}
Mathias Agopian1473f462009-04-10 14:24:30 -07001630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1632{
1633 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1634 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1635 if (eglSurface != EGL_NO_SURFACE) {
1636 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopianffbc8642009-08-20 00:12:56 -07001637 if (!surface->isValid())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1639 if (surface->dpy != dpy)
1640 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001641 if (surface->ctx) {
1642 // FIXME: this surface is current check what the spec says
1643 surface->disconnect();
1644 surface->ctx = 0;
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 delete surface;
1647 }
1648 return EGL_TRUE;
1649}
1650
1651EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1652 EGLint attribute, EGLint *value)
1653{
1654 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1655 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1656 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001657 if (!surface->isValid())
1658 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 if (surface->dpy != dpy)
1660 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1661
1662 EGLBoolean ret = EGL_TRUE;
1663 switch (attribute) {
1664 case EGL_CONFIG_ID:
1665 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1666 break;
1667 case EGL_WIDTH:
1668 *value = surface->getWidth();
1669 break;
1670 case EGL_HEIGHT:
1671 *value = surface->getHeight();
1672 break;
1673 case EGL_LARGEST_PBUFFER:
1674 // not modified for a window or pixmap surface
1675 break;
1676 case EGL_TEXTURE_FORMAT:
1677 *value = EGL_NO_TEXTURE;
1678 break;
1679 case EGL_TEXTURE_TARGET:
1680 *value = EGL_NO_TEXTURE;
1681 break;
1682 case EGL_MIPMAP_TEXTURE:
1683 *value = EGL_FALSE;
1684 break;
1685 case EGL_MIPMAP_LEVEL:
1686 *value = 0;
1687 break;
1688 case EGL_RENDER_BUFFER:
1689 // TODO: return the real RENDER_BUFFER here
1690 *value = EGL_BACK_BUFFER;
1691 break;
1692 case EGL_HORIZONTAL_RESOLUTION:
1693 // pixel/mm * EGL_DISPLAY_SCALING
1694 *value = surface->getHorizontalResolution();
1695 break;
1696 case EGL_VERTICAL_RESOLUTION:
1697 // pixel/mm * EGL_DISPLAY_SCALING
1698 *value = surface->getVerticalResolution();
1699 break;
1700 case EGL_PIXEL_ASPECT_RATIO: {
1701 // w/h * EGL_DISPLAY_SCALING
1702 int wr = surface->getHorizontalResolution();
1703 int hr = surface->getVerticalResolution();
1704 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1705 } break;
1706 case EGL_SWAP_BEHAVIOR:
Mathias Agopian1473f462009-04-10 14:24:30 -07001707 *value = surface->getSwapBehavior();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 break;
1709 default:
1710 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1711 }
1712 return ret;
1713}
1714
1715EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1716 EGLContext share_list, const EGLint *attrib_list)
1717{
1718 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1719 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1720
1721 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1722 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1723
1724 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1725 c->flags = egl_context_t::NEVER_CURRENT;
1726 c->dpy = dpy;
1727 c->config = config;
1728 c->read = 0;
1729 c->draw = 0;
1730 return (EGLContext)gl;
1731}
1732
1733EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1734{
1735 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1736 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1737 egl_context_t* c = egl_context_t::context(ctx);
1738 if (c->flags & egl_context_t::IS_CURRENT)
1739 setGlThreadSpecific(0);
1740 ogles_uninit((ogles_context_t*)ctx);
1741 return EGL_TRUE;
1742}
1743
1744EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1745 EGLSurface read, EGLContext ctx)
1746{
1747 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1748 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1749 if (draw) {
1750 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopianffbc8642009-08-20 00:12:56 -07001751 if (!s->isValid())
1752 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if (s->dpy != dpy)
1754 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001755 // TODO: check that draw is compatible with the context
1756 }
1757 if (read && read!=draw) {
1758 egl_surface_t* s = (egl_surface_t*)read;
1759 if (!s->isValid())
1760 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1761 if (s->dpy != dpy)
1762 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1763 // TODO: check that read is compatible with the context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765
1766 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian1473f462009-04-10 14:24:30 -07001767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1769 return setError(EGL_BAD_MATCH, EGL_FALSE);
1770
1771 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1772 return setError(EGL_BAD_MATCH, EGL_FALSE);
1773
1774 if (ctx == EGL_NO_CONTEXT) {
1775 // if we're detaching, we need the current context
1776 current_ctx = (EGLContext)getGlThreadSpecific();
1777 } else {
1778 egl_context_t* c = egl_context_t::context(ctx);
1779 egl_surface_t* d = (egl_surface_t*)draw;
1780 egl_surface_t* r = (egl_surface_t*)read;
1781 if ((d && d->ctx && d->ctx != ctx) ||
1782 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001783 // one of the surface is bound to a context in another thread
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1785 }
1786 }
1787
1788 ogles_context_t* gl = (ogles_context_t*)ctx;
1789 if (makeCurrent(gl) == 0) {
1790 if (ctx) {
1791 egl_context_t* c = egl_context_t::context(ctx);
1792 egl_surface_t* d = (egl_surface_t*)draw;
1793 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001794
1795 if (c->draw) {
Mathias Agopianffbc8642009-08-20 00:12:56 -07001796 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1797 s->disconnect();
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001798 }
1799 if (c->read) {
1800 // FIXME: unlock/disconnect the read surface too
1801 }
1802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 c->draw = draw;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001804 c->read = read;
1805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 if (c->flags & egl_context_t::NEVER_CURRENT) {
1807 c->flags &= ~egl_context_t::NEVER_CURRENT;
1808 GLint w = 0;
1809 GLint h = 0;
1810 if (draw) {
1811 w = d->getWidth();
1812 h = d->getHeight();
1813 }
1814 ogles_surfaceport(gl, 0, 0);
1815 ogles_viewport(gl, 0, 0, w, h);
1816 ogles_scissor(gl, 0, 0, w, h);
1817 }
1818 if (d) {
Mathias Agopianabac0102009-07-31 14:47:00 -07001819 if (d->connect() == EGL_FALSE) {
1820 return EGL_FALSE;
1821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 d->ctx = ctx;
1823 d->bindDrawSurface(gl);
1824 }
1825 if (r) {
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001826 // FIXME: lock/connect the read surface too
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 r->ctx = ctx;
1828 r->bindReadSurface(gl);
1829 }
1830 } else {
1831 // if surfaces were bound to the context bound to this thread
1832 // mark then as unbound.
1833 if (current_ctx) {
1834 egl_context_t* c = egl_context_t::context(current_ctx);
1835 egl_surface_t* d = (egl_surface_t*)c->draw;
1836 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001837 if (d) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001838 c->draw = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001839 d->ctx = EGL_NO_CONTEXT;
1840 d->disconnect();
1841 }
1842 if (r) {
Mathias Agopian36432cc2009-06-03 19:00:53 -07001843 c->read = 0;
Mathias Agopian430f2ed2009-05-05 00:37:46 -07001844 r->ctx = EGL_NO_CONTEXT;
1845 // FIXME: unlock/disconnect the read surface too
1846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848 }
1849 return EGL_TRUE;
1850 }
1851 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1852}
1853
1854EGLContext eglGetCurrentContext(void)
1855{
1856 // eglGetCurrentContext returns the current EGL rendering context,
1857 // as specified by eglMakeCurrent. If no context is current,
1858 // EGL_NO_CONTEXT is returned.
1859 return (EGLContext)getGlThreadSpecific();
1860}
1861
1862EGLSurface eglGetCurrentSurface(EGLint readdraw)
1863{
1864 // eglGetCurrentSurface returns the read or draw surface attached
1865 // to the current EGL rendering context, as specified by eglMakeCurrent.
1866 // If no context is current, EGL_NO_SURFACE is returned.
1867 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1868 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1869 egl_context_t* c = egl_context_t::context(ctx);
1870 if (readdraw == EGL_READ) {
1871 return c->read;
1872 } else if (readdraw == EGL_DRAW) {
1873 return c->draw;
1874 }
1875 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1876}
1877
1878EGLDisplay eglGetCurrentDisplay(void)
1879{
1880 // eglGetCurrentDisplay returns the current EGL display connection
1881 // for the current EGL rendering context, as specified by eglMakeCurrent.
1882 // If no context is current, EGL_NO_DISPLAY is returned.
1883 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1884 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1885 egl_context_t* c = egl_context_t::context(ctx);
1886 return c->dpy;
1887}
1888
1889EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1890 EGLint attribute, EGLint *value)
1891{
1892 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1893 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1894 egl_context_t* c = egl_context_t::context(ctx);
1895 switch (attribute) {
1896 case EGL_CONFIG_ID:
1897 // Returns the ID of the EGL frame buffer configuration with
1898 // respect to which the context was created
1899 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1900 }
1901 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1902}
1903
1904EGLBoolean eglWaitGL(void)
1905{
1906 return EGL_TRUE;
1907}
1908
1909EGLBoolean eglWaitNative(EGLint engine)
1910{
1911 return EGL_TRUE;
1912}
1913
1914EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1915{
1916 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1917 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07001920 if (!d->isValid())
1921 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (d->dpy != dpy)
1923 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1924
1925 // post the surface
1926 d->swapBuffers();
1927
1928 // if it's bound to a context, update the buffer
1929 if (d->ctx != EGL_NO_CONTEXT) {
1930 d->bindDrawSurface((ogles_context_t*)d->ctx);
1931 // if this surface is also the read surface of the context
1932 // it is bound to, make sure to update the read buffer as well.
1933 // The EGL spec is a little unclear about this.
1934 egl_context_t* c = egl_context_t::context(d->ctx);
1935 if (c->read == draw) {
1936 d->bindReadSurface((ogles_context_t*)d->ctx);
1937 }
1938 }
1939
1940 return EGL_TRUE;
1941}
1942
1943EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1944 NativePixmapType target)
1945{
1946 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1947 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1948 // TODO: eglCopyBuffers()
1949 return EGL_FALSE;
1950}
1951
1952EGLint eglGetError(void)
1953{
1954 return getError();
1955}
1956
1957const char* eglQueryString(EGLDisplay dpy, EGLint name)
1958{
1959 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1960 return setError(EGL_BAD_DISPLAY, (const char*)0);
1961
1962 switch (name) {
1963 case EGL_VENDOR:
1964 return gVendorString;
1965 case EGL_VERSION:
1966 return gVersionString;
1967 case EGL_EXTENSIONS:
1968 return gExtensionsString;
1969 case EGL_CLIENT_APIS:
1970 return gClientApiString;
1971 }
1972 return setError(EGL_BAD_PARAMETER, (const char *)0);
1973}
1974
1975// ----------------------------------------------------------------------------
1976// EGL 1.1
1977// ----------------------------------------------------------------------------
1978
1979EGLBoolean eglSurfaceAttrib(
1980 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1981{
1982 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1983 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1984 // TODO: eglSurfaceAttrib()
1985 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1986}
1987
1988EGLBoolean eglBindTexImage(
1989 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1990{
1991 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1992 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1993 // TODO: eglBindTexImage()
1994 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1995}
1996
1997EGLBoolean eglReleaseTexImage(
1998 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1999{
2000 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2001 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2002 // TODO: eglReleaseTexImage()
2003 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2004}
2005
2006EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
2007{
2008 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2009 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2010 // TODO: eglSwapInterval()
2011 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2012}
2013
2014// ----------------------------------------------------------------------------
2015// EGL 1.2
2016// ----------------------------------------------------------------------------
2017
2018EGLBoolean eglBindAPI(EGLenum api)
2019{
2020 if (api != EGL_OPENGL_ES_API)
2021 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2022 return EGL_TRUE;
2023}
2024
2025EGLenum eglQueryAPI(void)
2026{
2027 return EGL_OPENGL_ES_API;
2028}
2029
2030EGLBoolean eglWaitClient(void)
2031{
2032 glFinish();
2033 return EGL_TRUE;
2034}
2035
2036EGLBoolean eglReleaseThread(void)
2037{
2038 // TODO: eglReleaseThread()
2039 return EGL_TRUE;
2040}
2041
2042EGLSurface eglCreatePbufferFromClientBuffer(
2043 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2044 EGLConfig config, const EGLint *attrib_list)
2045{
2046 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2047 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2048 // TODO: eglCreatePbufferFromClientBuffer()
2049 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2050}
2051
2052// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -07002053// EGL_EGLEXT_VERSION 3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054// ----------------------------------------------------------------------------
2055
2056void (*eglGetProcAddress (const char *procname))()
2057{
2058 extention_map_t const * const map = gExtentionMap;
2059 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2060 if (!strcmp(procname, map[i].name)) {
2061 return map[i].address;
2062 }
2063 }
2064 return NULL;
2065}
Mathias Agopian1473f462009-04-10 14:24:30 -07002066
2067EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2068 const EGLint *attrib_list)
2069{
2070 EGLBoolean result = EGL_FALSE;
2071 return result;
2072}
2073
2074EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2075{
2076 EGLBoolean result = EGL_FALSE;
2077 return result;
2078}
2079
2080EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2081 EGLClientBuffer buffer, const EGLint *attrib_list)
2082{
2083 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2084 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2085 }
2086 if (ctx != EGL_NO_CONTEXT) {
2087 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2088 }
2089 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2090 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2091 }
2092
2093 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2094
2095 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2096 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2097
2098 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2099 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian42d99d22010-02-04 17:04:53 -08002100
2101 switch (native_buffer->format) {
2102 case HAL_PIXEL_FORMAT_RGBA_8888:
2103 case HAL_PIXEL_FORMAT_RGBX_8888:
2104 case HAL_PIXEL_FORMAT_RGB_888:
2105 case HAL_PIXEL_FORMAT_RGB_565:
2106 case HAL_PIXEL_FORMAT_BGRA_8888:
2107 case HAL_PIXEL_FORMAT_RGBA_5551:
2108 case HAL_PIXEL_FORMAT_RGBA_4444:
2109 break;
2110 default:
2111 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2112 }
2113
Mathias Agopian1473f462009-04-10 14:24:30 -07002114 native_buffer->common.incRef(&native_buffer->common);
2115 return (EGLImageKHR)native_buffer;
2116}
2117
2118EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2119{
2120 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2121 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2122 }
2123
2124 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2125
2126 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2127 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2128
2129 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2130 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2131
Mathias Agopian1473f462009-04-10 14:24:30 -07002132 native_buffer->common.decRef(&native_buffer->common);
2133
2134 return EGL_TRUE;
2135}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002136
2137// ----------------------------------------------------------------------------
2138// ANDROID extensions
2139// ----------------------------------------------------------------------------
2140
2141EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2142 EGLint left, EGLint top, EGLint width, EGLint height)
2143{
2144 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2145 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2146
2147 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002148 if (!d->isValid())
2149 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002150 if (d->dpy != dpy)
2151 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2152
2153 // post the surface
2154 d->setSwapRectangle(left, top, width, height);
2155
2156 return EGL_TRUE;
2157}
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002158
2159EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
2160{
2161 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2162 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2163
2164 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopianffbc8642009-08-20 00:12:56 -07002165 if (!d->isValid())
2166 return setError(EGL_BAD_SURFACE, (EGLClientBuffer)0);
Mathias Agopianc1e3ec52009-06-24 22:37:39 -07002167 if (d->dpy != dpy)
2168 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2169
2170 // post the surface
2171 return d->getRenderBuffer();
2172}