blob: 7c496e7f097ecc57cf6a31402244780770ff5614 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002**
3** Copyright 2007 The Android Open Source Project
4**
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -08008**
Mathias Agopian076b1cc2009-04-10 14:24:30 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080010**
Mathias Agopian076b1cc2009-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 Projectedbf3b62009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
The Android Open Source Projectedbf3b62009-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 Agopian58a79f42009-05-05 18:21:32 -070042#include <private/ui/android_natives_priv.h>
Mathias Agopianbc492912009-11-03 20:38:08 -080043#include <private/ui/sw_gralloc_handle.h>
Mathias Agopian7189c002009-05-05 18:11:11 -070044
Mathias Agopian240c9fe2009-06-25 15:39:25 -070045#include <hardware/copybit.h>
46
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -070095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097
The Android Open Source Projectedbf3b62009-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 Agopian0696a572009-08-20 00:12:56 -0700145 bool isValid() const;
146 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
149 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700150 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700151 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 virtual EGLint getWidth() const = 0;
153 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-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 Agopiandf3ca302009-05-04 19:29:25 -0700160 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161protected:
162 GGLSurface depth;
163};
164
165egl_surface_t::egl_surface_t(EGLDisplay dpy,
166 EGLConfig config,
167 int32_t depthFormat)
168 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
169{
170 depth.version = sizeof(GGLSurface);
171 depth.data = 0;
172 depth.format = depthFormat;
173}
174egl_surface_t::~egl_surface_t()
175{
176 magic = 0;
177 free(depth.data);
178}
Mathias Agopian0696a572009-08-20 00:12:56 -0700179bool egl_surface_t::isValid() const {
180 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
181 return magic == MAGIC;
182}
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184EGLBoolean egl_surface_t::swapBuffers() {
185 return EGL_FALSE;
186}
187EGLint egl_surface_t::getHorizontalResolution() const {
188 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
189}
190EGLint egl_surface_t::getVerticalResolution() const {
191 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
192}
193EGLint egl_surface_t::getRefreshRate() const {
194 return (60 * EGL_DISPLAY_SCALING);
195}
196EGLint egl_surface_t::getSwapBehavior() const {
197 return EGL_BUFFER_PRESERVED;
198}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700199EGLBoolean egl_surface_t::setSwapRectangle(
200 EGLint l, EGLint t, EGLint w, EGLint h)
201{
202 return EGL_FALSE;
203}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204
205// ----------------------------------------------------------------------------
206
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 EGLDisplay dpy, EGLConfig config,
211 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700212 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
Mathias Agopian0696a572009-08-20 00:12:56 -0700214 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215
Mathias Agopian0696a572009-08-20 00:12:56 -0700216 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 virtual EGLBoolean swapBuffers();
218 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
219 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700220 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700221 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700222 virtual EGLint getWidth() const { return width; }
223 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 virtual EGLint getHorizontalResolution() const;
225 virtual EGLint getVerticalResolution() const;
226 virtual EGLint getRefreshRate() const;
227 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700228 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230private:
Mathias Agopiane71212b2009-05-05 00:37:46 -0700231 status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700232 status_t unlock(android_native_buffer_t* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700233 ANativeWindow* nativeWindow;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 android_native_buffer_t* buffer;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700235 android_native_buffer_t* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700236 gralloc_module_t const* module;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700237 copybit_device_t* blitengine;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238 int width;
239 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700240 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700241 GGLFormat const* pixelFormatTable;
242
243 struct Rect {
244 inline Rect() { };
245 inline Rect(int32_t w, int32_t h)
246 : left(0), top(0), right(w), bottom(h) { }
247 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
248 : left(l), top(t), right(r), bottom(b) { }
249 Rect& andSelf(const Rect& r) {
250 left = max(left, r.left);
251 top = max(top, r.top);
252 right = min(right, r.right);
253 bottom = min(bottom, r.bottom);
254 return *this;
255 }
256 bool isEmpty() const {
257 return (left>=right || top>=bottom);
258 }
259 void dump(char const* what) {
260 LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
261 what, left, top, right-left, bottom-top);
262 }
263
264 int32_t left;
265 int32_t top;
266 int32_t right;
267 int32_t bottom;
268 };
269
270 struct Region {
271 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700272 typedef Rect const* const_iterator;
273 const_iterator begin() const { return storage; }
274 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700275 static Region subtract(const Rect& lhs, const Rect& rhs) {
276 Region reg;
277 Rect* storage = reg.storage;
278 if (!lhs.isEmpty()) {
279 if (lhs.top < rhs.top) { // top rect
280 storage->left = lhs.left;
281 storage->top = lhs.top;
282 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700283 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700284 storage++;
285 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700286 const int32_t top = max(lhs.top, rhs.top);
287 const int32_t bot = min(lhs.bottom, rhs.bottom);
288 if (top < bot) {
289 if (lhs.left < rhs.left) { // left-side rect
290 storage->left = lhs.left;
291 storage->top = top;
292 storage->right = rhs.left;
293 storage->bottom = bot;
294 storage++;
295 }
296 if (lhs.right > rhs.right) { // right-side rect
297 storage->left = rhs.right;
298 storage->top = top;
299 storage->right = lhs.right;
300 storage->bottom = bot;
301 storage++;
302 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700303 }
304 if (lhs.bottom > rhs.bottom) { // bottom rect
305 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700306 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700307 storage->right = lhs.right;
308 storage->bottom = lhs.bottom;
309 storage++;
310 }
311 reg.count = storage - reg.storage;
312 }
313 return reg;
314 }
315 bool isEmpty() const {
316 return count<=0;
317 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700318 private:
319 Rect storage[4];
320 ssize_t count;
321 };
322
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700323 struct region_iterator : public copybit_region_t {
324 region_iterator(const Region& region)
325 : b(region.begin()), e(region.end()) {
326 this->next = iterate;
327 }
328 private:
329 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
330 region_iterator const* me = static_cast<region_iterator const*>(self);
331 if (me->b != me->e) {
332 *reinterpret_cast<Rect*>(rect) = *me->b++;
333 return 1;
334 }
335 return 0;
336 }
337 mutable Region::const_iterator b;
338 Region::const_iterator const e;
339 };
340
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700341 void copyBlt(
342 android_native_buffer_t* dst, void* dst_vaddr,
343 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700344 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700345
346 Rect dirtyRegion;
347 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348};
349
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700350egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 EGLConfig config,
352 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700353 ANativeWindow* window)
Mathias Agopian0926f502009-05-04 14:17:04 -0700354 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700355 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700356 blitengine(0), bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357{
Mathias Agopian0926f502009-05-04 14:17:04 -0700358 hw_module_t const* pModule;
359 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
360 module = reinterpret_cast<gralloc_module_t const*>(pModule);
361
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700362 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
363 copybit_open(pModule, &blitengine);
364 }
365
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700366 pixelFormatTable = gglGetPixelFormatTable();
367
368 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700369 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700370 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
371 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700372}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700373
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700374egl_window_surface_v2_t::~egl_window_surface_v2_t() {
375 if (buffer) {
376 buffer->common.decRef(&buffer->common);
377 }
378 if (previousBuffer) {
379 previousBuffer->common.decRef(&previousBuffer->common);
380 }
381 nativeWindow->common.decRef(&nativeWindow->common);
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700382 if (blitengine) {
383 copybit_close(blitengine);
384 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700385}
386
Mathias Agopiancf81c842009-07-31 14:47:00 -0700387EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700388{
Mathias Agopian52212712009-08-11 22:34:02 -0700389 // we're intending to do software rendering
390 native_window_set_usage(nativeWindow,
391 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
392
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700393 // dequeue a buffer
Mathias Agopiancf81c842009-07-31 14:47:00 -0700394 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
395 return setError(EGL_BAD_ALLOC, EGL_FALSE);
396 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700397
398 // allocate a corresponding depth-buffer
399 width = buffer->width;
400 height = buffer->height;
401 if (depth.format) {
402 depth.width = width;
403 depth.height = height;
404 depth.stride = depth.width; // use the width here
405 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
406 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700407 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700408 }
409 }
410
411 // keep a reference on the buffer
412 buffer->common.incRef(&buffer->common);
413
Mathias Agopian0926f502009-05-04 14:17:04 -0700414 // Lock the buffer
Mathias Agopiane71212b2009-05-05 00:37:46 -0700415 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700416 // pin the buffer down
417 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
418 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700419 LOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700420 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700421 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700422 // FIXME: we should make sure we're not accessing the buffer anymore
423 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700424 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700425}
426
427void egl_window_surface_v2_t::disconnect()
428{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700429 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700430 bits = NULL;
431 unlock(buffer);
432 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700433 // enqueue the last frame
434 nativeWindow->queueBuffer(nativeWindow, buffer);
435 if (buffer) {
436 buffer->common.decRef(&buffer->common);
437 buffer = 0;
438 }
439 if (previousBuffer) {
440 previousBuffer->common.decRef(&previousBuffer->common);
441 previousBuffer = 0;
442 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443}
444
Mathias Agopian0926f502009-05-04 14:17:04 -0700445status_t egl_window_surface_v2_t::lock(
Mathias Agopiane71212b2009-05-05 00:37:46 -0700446 android_native_buffer_t* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700447{
Mathias Agopianbc492912009-11-03 20:38:08 -0800448 int err;
449 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
450 err = module->lock(module, buf->handle,
451 usage, 0, 0, buf->width, buf->height, vaddr);
452 } else {
453 sw_gralloc_handle_t const* hnd =
454 reinterpret_cast<sw_gralloc_handle_t const*>(buf->handle);
455 *vaddr = (void*)hnd->base;
456 err = NO_ERROR;
457 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700458 return err;
459}
460
461status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
462{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700463 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800464 int err = NO_ERROR;
465 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
466 err = module->unlock(module, buf->handle);
467 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700468 return err;
469}
470
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700471void egl_window_surface_v2_t::copyBlt(
472 android_native_buffer_t* dst, void* dst_vaddr,
473 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700474 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700475{
476 // FIXME: use copybit if possible
477 // NOTE: dst and src must be the same format
478
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700479 status_t err = NO_ERROR;
480 copybit_device_t* const copybit = blitengine;
481 if (copybit) {
482 copybit_image_t simg;
Mathias Palmqvista409e192010-06-02 16:03:04 +0200483 simg.w = src->stride;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700484 simg.h = src->height;
485 simg.format = src->format;
486 simg.handle = const_cast<native_handle_t*>(src->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700487
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700488 copybit_image_t dimg;
Mathias Palmqvista409e192010-06-02 16:03:04 +0200489 dimg.w = dst->stride;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700490 dimg.h = dst->height;
491 dimg.format = dst->format;
492 dimg.handle = const_cast<native_handle_t*>(dst->handle);
493
494 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
495 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
496 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
497 region_iterator it(clip);
498 err = copybit->blit(copybit, &dimg, &simg, &it);
499 if (err != NO_ERROR) {
500 LOGE("copybit failed (%s)", strerror(err));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700501 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700502 }
503
504 if (!copybit || err) {
505 Region::const_iterator cur = clip.begin();
506 Region::const_iterator end = clip.end();
507
508 const size_t bpp = pixelFormatTable[src->format].size;
509 const size_t dbpr = dst->stride * bpp;
510 const size_t sbpr = src->stride * bpp;
511
512 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
513 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
514
515 while (cur != end) {
516 const Rect& r(*cur++);
517 ssize_t w = r.right - r.left;
518 ssize_t h = r.bottom - r.top;
519 if (w <= 0 || h<=0) continue;
520 size_t size = w * bpp;
521 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
522 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
523 if (dbpr==sbpr && size==sbpr) {
524 size *= h;
525 h = 1;
526 }
527 do {
528 memcpy(d, s, size);
529 d += dbpr;
530 s += sbpr;
531 } while (--h > 0);
532 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700533 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534}
535
536EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700538 if (!buffer) {
539 return setError(EGL_BAD_ACCESS, EGL_FALSE);
540 }
541
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700542 /*
543 * Handle eglSetSwapRectangleANDROID()
544 * We copyback from the front buffer
545 */
546 if (!dirtyRegion.isEmpty()) {
547 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
548 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100549 // This was const Region copyBack, but that causes an
550 // internal compile error on simulator builds
551 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700552 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700553 void* prevBits;
554 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700555 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
556 // copy from previousBuffer to buffer
557 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700558 unlock(previousBuffer);
559 }
560 }
561 }
562 oldDirtyRegion = dirtyRegion;
563 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700565 if (previousBuffer) {
566 previousBuffer->common.decRef(&previousBuffer->common);
567 previousBuffer = 0;
568 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700569
Mathias Agopian0926f502009-05-04 14:17:04 -0700570 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700571 previousBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700573 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700575 // dequeue a new buffer
Mathias Agopian031213e2010-08-18 16:07:34 -0700576 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
577
578 // TODO: lockBuffer should rather be executed when the very first
579 // direct rendering occurs.
580 nativeWindow->lockBuffer(nativeWindow, buffer);
581
582 // reallocate the depth-buffer if needed
583 if ((width != buffer->width) || (height != buffer->height)) {
584 // TODO: we probably should reset the swap rect here
585 // if the window size has changed
586 width = buffer->width;
587 height = buffer->height;
588 if (depth.data) {
589 free(depth.data);
590 depth.width = width;
591 depth.height = height;
592 depth.stride = buffer->stride;
593 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
594 if (depth.data == 0) {
595 setError(EGL_BAD_ALLOC, EGL_FALSE);
596 return EGL_FALSE;
597 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 }
599 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700600
Mathias Agopian031213e2010-08-18 16:07:34 -0700601 // keep a reference on the buffer
602 buffer->common.incRef(&buffer->common);
603
604 // finally pin the buffer down
605 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
606 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
607 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
608 buffer, buffer->width, buffer->height);
609 return setError(EGL_BAD_ACCESS, EGL_FALSE);
610 // FIXME: we should make sure we're not accessing the buffer anymore
611 }
612 } else {
613 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700614 }
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 Projectedbf3b62009-03-03 19:31:44 -0800623 return EGL_TRUE;
624}
625
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700626EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627{
628 GGLSurface buffer;
629 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700630 buffer.width = this->buffer->width;
631 buffer.height = this->buffer->height;
632 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700633 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700634 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 gl->rasterizer.procs.colorBuffer(gl, &buffer);
636 if (depth.data != gl->rasterizer.state.buffers.depth.data)
637 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 return EGL_TRUE;
640}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642{
643 GGLSurface buffer;
644 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 buffer.width = this->buffer->width;
646 buffer.height = this->buffer->height;
647 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700648 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 gl->rasterizer.procs.readBuffer(gl, &buffer);
651 return EGL_TRUE;
652}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700653EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
655}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
658}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659EGLint egl_window_surface_v2_t::getRefreshRate() const {
660 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700662EGLint egl_window_surface_v2_t::getSwapBehavior() const
663{
664 /*
665 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
666 * the content of the swapped buffer.
667 *
668 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
669 *
670 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
671 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
672 * is, everything outside of this area is preserved.
673 *
674 * This implementation of EGL assumes the later case.
675 *
676 */
677
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700678 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679}
680
681// ----------------------------------------------------------------------------
682
683struct egl_pixmap_surface_t : public egl_surface_t
684{
685 egl_pixmap_surface_t(
686 EGLDisplay dpy, EGLConfig config,
687 int32_t depthFormat,
688 egl_native_pixmap_t const * pixmap);
689
690 virtual ~egl_pixmap_surface_t() { }
691
Mathias Agopian0696a572009-08-20 00:12:56 -0700692 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
694 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
695 virtual EGLint getWidth() const { return nativePixmap.width; }
696 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697private:
698 egl_native_pixmap_t nativePixmap;
699};
700
701egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
702 EGLConfig config,
703 int32_t depthFormat,
704 egl_native_pixmap_t const * pixmap)
705 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
706{
707 if (depthFormat) {
708 depth.width = pixmap->width;
709 depth.height = pixmap->height;
710 depth.stride = depth.width; // use the width here
711 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
712 if (depth.data == 0) {
713 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 }
715 }
716}
717EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
718{
719 GGLSurface buffer;
720 buffer.version = sizeof(GGLSurface);
721 buffer.width = nativePixmap.width;
722 buffer.height = nativePixmap.height;
723 buffer.stride = nativePixmap.stride;
724 buffer.data = nativePixmap.data;
725 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 gl->rasterizer.procs.colorBuffer(gl, &buffer);
728 if (depth.data != gl->rasterizer.state.buffers.depth.data)
729 gl->rasterizer.procs.depthBuffer(gl, &depth);
730 return EGL_TRUE;
731}
732EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
733{
734 GGLSurface buffer;
735 buffer.version = sizeof(GGLSurface);
736 buffer.width = nativePixmap.width;
737 buffer.height = nativePixmap.height;
738 buffer.stride = nativePixmap.stride;
739 buffer.data = nativePixmap.data;
740 buffer.format = nativePixmap.format;
741 gl->rasterizer.procs.readBuffer(gl, &buffer);
742 return EGL_TRUE;
743}
744
745// ----------------------------------------------------------------------------
746
747struct egl_pbuffer_surface_t : public egl_surface_t
748{
749 egl_pbuffer_surface_t(
750 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
751 int32_t w, int32_t h, int32_t f);
752
753 virtual ~egl_pbuffer_surface_t();
754
Mathias Agopian0696a572009-08-20 00:12:56 -0700755 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
757 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
758 virtual EGLint getWidth() const { return pbuffer.width; }
759 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760private:
761 GGLSurface pbuffer;
762};
763
764egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
765 EGLConfig config, int32_t depthFormat,
766 int32_t w, int32_t h, int32_t f)
767 : egl_surface_t(dpy, config, depthFormat)
768{
769 size_t size = w*h;
770 switch (f) {
771 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
772 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
773 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800774 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 default:
776 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
777 pbuffer.data = 0;
778 break;
779 }
780 pbuffer.version = sizeof(GGLSurface);
781 pbuffer.width = w;
782 pbuffer.height = h;
783 pbuffer.stride = w;
784 pbuffer.data = (GGLubyte*)malloc(size);
785 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 if (depthFormat) {
788 depth.width = pbuffer.width;
789 depth.height = pbuffer.height;
790 depth.stride = depth.width; // use the width here
791 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
792 if (depth.data == 0) {
793 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
794 return;
795 }
796 }
797}
798egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
799 free(pbuffer.data);
800}
801EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
802{
803 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
804 if (depth.data != gl->rasterizer.state.buffers.depth.data)
805 gl->rasterizer.procs.depthBuffer(gl, &depth);
806 return EGL_TRUE;
807}
808EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
809{
810 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
811 return EGL_TRUE;
812}
813
814// ----------------------------------------------------------------------------
815
816struct config_pair_t {
817 GLint key;
818 GLint value;
819};
820
821struct configs_t {
822 const config_pair_t* array;
823 int size;
824};
825
826struct config_management_t {
827 GLint key;
828 bool (*match)(GLint reqValue, GLint confValue);
829 static bool atLeast(GLint reqValue, GLint confValue) {
830 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
831 }
832 static bool exact(GLint reqValue, GLint confValue) {
833 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
834 }
835 static bool mask(GLint reqValue, GLint confValue) {
836 return (confValue & reqValue) == reqValue;
837 }
Mathias Agopian63971672010-10-25 15:51:24 -0700838 static bool ignore(GLint reqValue, GLint confValue) {
839 return true;
840 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841};
842
843// ----------------------------------------------------------------------------
844
845#define VERSION_MAJOR 1
846#define VERSION_MINOR 2
847static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700848static char const * const gVersionString = "1.2 Android Driver 1.2.0";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700850static char const * const gExtensionsString =
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700851 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700852 // "KHR_image_pixmap "
853 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700854 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700855 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856
857// ----------------------------------------------------------------------------
858
859struct extention_map_t {
860 const char * const name;
861 __eglMustCastToProperFunctionPointerType address;
862};
863
864static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700865 { "glDrawTexsOES",
866 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
867 { "glDrawTexiOES",
868 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
869 { "glDrawTexfOES",
870 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
871 { "glDrawTexxOES",
872 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
873 { "glDrawTexsvOES",
874 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
875 { "glDrawTexivOES",
876 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
877 { "glDrawTexfvOES",
878 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
879 { "glDrawTexxvOES",
880 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
881 { "glQueryMatrixxOES",
882 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
883 { "glEGLImageTargetTexture2DOES",
884 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
885 { "glEGLImageTargetRenderbufferStorageOES",
886 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
887 { "glClipPlanef",
888 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
889 { "glClipPlanex",
890 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
891 { "glBindBuffer",
892 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
893 { "glBufferData",
894 (__eglMustCastToProperFunctionPointerType)&glBufferData },
895 { "glBufferSubData",
896 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
897 { "glDeleteBuffers",
898 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
899 { "glGenBuffers",
900 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700901 { "eglCreateImageKHR",
902 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
903 { "eglDestroyImageKHR",
904 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
905 { "eglSetSwapRectangleANDROID",
906 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907};
908
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700909/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 * In the lists below, attributes names MUST be sorted.
911 * Additionally, all configs must be sorted according to
912 * the EGL specification.
913 */
914
915static config_pair_t const config_base_attribute_list[] = {
916 { EGL_STENCIL_SIZE, 0 },
917 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
918 { EGL_LEVEL, 0 },
919 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700920 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
922 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
923 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
924 { EGL_NATIVE_VISUAL_ID, 0 },
925 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
926 { EGL_SAMPLES, 0 },
927 { EGL_SAMPLE_BUFFERS, 0 },
928 { EGL_TRANSPARENT_TYPE, EGL_NONE },
929 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
930 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
931 { EGL_TRANSPARENT_RED_VALUE, 0 },
932 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
933 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
934 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700935 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700936 { EGL_LUMINANCE_SIZE, 0 },
937 { EGL_ALPHA_MASK_SIZE, 0 },
938 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700939 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700940 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941};
942
943// These configs can override the base attribute list
944// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
945
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800946// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947static config_pair_t const config_0_attribute_list[] = {
948 { EGL_BUFFER_SIZE, 16 },
949 { EGL_ALPHA_SIZE, 0 },
950 { EGL_BLUE_SIZE, 5 },
951 { EGL_GREEN_SIZE, 6 },
952 { EGL_RED_SIZE, 5 },
953 { EGL_DEPTH_SIZE, 0 },
954 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700955 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
957};
958
959static config_pair_t const config_1_attribute_list[] = {
960 { EGL_BUFFER_SIZE, 16 },
961 { EGL_ALPHA_SIZE, 0 },
962 { EGL_BLUE_SIZE, 5 },
963 { EGL_GREEN_SIZE, 6 },
964 { EGL_RED_SIZE, 5 },
965 { EGL_DEPTH_SIZE, 16 },
966 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700967 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
969};
970
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800971// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972static config_pair_t const config_2_attribute_list[] = {
973 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800974 { EGL_ALPHA_SIZE, 0 },
975 { EGL_BLUE_SIZE, 8 },
976 { EGL_GREEN_SIZE, 8 },
977 { EGL_RED_SIZE, 8 },
978 { EGL_DEPTH_SIZE, 0 },
979 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700980 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800981 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
982};
983
984static config_pair_t const config_3_attribute_list[] = {
985 { EGL_BUFFER_SIZE, 32 },
986 { EGL_ALPHA_SIZE, 0 },
987 { EGL_BLUE_SIZE, 8 },
988 { EGL_GREEN_SIZE, 8 },
989 { EGL_RED_SIZE, 8 },
990 { EGL_DEPTH_SIZE, 16 },
991 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700992 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800993 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
994};
995
996// 8888 configs
997static config_pair_t const config_4_attribute_list[] = {
998 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 { EGL_ALPHA_SIZE, 8 },
1000 { EGL_BLUE_SIZE, 8 },
1001 { EGL_GREEN_SIZE, 8 },
1002 { EGL_RED_SIZE, 8 },
1003 { EGL_DEPTH_SIZE, 0 },
1004 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001005 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1007};
1008
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001009static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 { EGL_BUFFER_SIZE, 32 },
1011 { EGL_ALPHA_SIZE, 8 },
1012 { EGL_BLUE_SIZE, 8 },
1013 { EGL_GREEN_SIZE, 8 },
1014 { EGL_RED_SIZE, 8 },
1015 { EGL_DEPTH_SIZE, 16 },
1016 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001017 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1019};
1020
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001021// A8 configs
1022static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 { EGL_BUFFER_SIZE, 8 },
1024 { EGL_ALPHA_SIZE, 8 },
1025 { EGL_BLUE_SIZE, 0 },
1026 { EGL_GREEN_SIZE, 0 },
1027 { EGL_RED_SIZE, 0 },
1028 { EGL_DEPTH_SIZE, 0 },
1029 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001030 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1032};
1033
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001034static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 { EGL_BUFFER_SIZE, 8 },
1036 { EGL_ALPHA_SIZE, 8 },
1037 { EGL_BLUE_SIZE, 0 },
1038 { EGL_GREEN_SIZE, 0 },
1039 { EGL_RED_SIZE, 0 },
1040 { EGL_DEPTH_SIZE, 16 },
1041 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001042 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1044};
1045
1046static configs_t const gConfigs[] = {
1047 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1048 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1049 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1050 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1051 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1052 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001053 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1054 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055};
1056
1057static config_management_t const gConfigManagement[] = {
1058 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1059 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1060 { EGL_BLUE_SIZE, config_management_t::atLeast },
1061 { EGL_GREEN_SIZE, config_management_t::atLeast },
1062 { EGL_RED_SIZE, config_management_t::atLeast },
1063 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1064 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1065 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1066 { EGL_CONFIG_ID, config_management_t::exact },
1067 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001068 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1069 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1070 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001072 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1074 { EGL_SAMPLES, config_management_t::exact },
1075 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1076 { EGL_SURFACE_TYPE, config_management_t::mask },
1077 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1078 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1079 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1080 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1081 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1082 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1083 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1084 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001085 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1086 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1087 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1088 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1089 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090};
1091
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001094 // attributes that are not specified are simply ignored, if a particular
1095 // one needs not be ignored, it must be specified here, eg:
1096 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097};
1098
1099// ----------------------------------------------------------------------------
1100
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001101static status_t getConfigFormatInfo(EGLint configID,
1102 int32_t& pixelFormat, int32_t& depthFormat)
1103{
1104 switch(configID) {
1105 case 0:
1106 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1107 depthFormat = 0;
1108 break;
1109 case 1:
1110 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1111 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1112 break;
1113 case 2:
1114 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1115 depthFormat = 0;
1116 break;
1117 case 3:
1118 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1119 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1120 break;
1121 case 4:
1122 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1123 depthFormat = 0;
1124 break;
1125 case 5:
1126 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1127 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1128 break;
1129 case 6:
1130 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1131 depthFormat = 0;
1132 break;
1133 case 7:
1134 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1135 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1136 break;
1137 default:
1138 return NAME_NOT_FOUND;
1139 }
1140 return NO_ERROR;
1141}
1142
1143// ----------------------------------------------------------------------------
1144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145template<typename T>
1146static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1147{
1148 while (first <= last) {
1149 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001150 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001152 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 last = mid - 1;
1154 } else {
1155 return mid;
1156 }
1157 }
1158 return -1;
1159}
1160
1161static int isAttributeMatching(int i, EGLint attr, EGLint val)
1162{
1163 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001164 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 int index = binarySearch<config_pair_t>(
1166 gConfigs[i].array,
1167 0, gConfigs[i].size-1,
1168 attr);
1169 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001170 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 index = binarySearch<config_pair_t>(
1172 config_base_attribute_list,
1173 0, NELEM(config_base_attribute_list)-1,
1174 attr);
1175 }
1176 if (index >= 0) {
1177 // attribute found, check if this config could match
1178 int cfgMgtIndex = binarySearch<config_management_t>(
1179 gConfigManagement,
1180 0, NELEM(gConfigManagement)-1,
1181 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001182 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 bool match = gConfigManagement[cfgMgtIndex].match(
1184 val, configFound[index].value);
1185 if (match) {
1186 // this config matches
1187 return 1;
1188 }
1189 } else {
1190 // attribute not found. this should NEVER happen.
1191 }
1192 } else {
1193 // error, this attribute doesn't exist
1194 }
1195 return 0;
1196}
1197
1198static int makeCurrent(ogles_context_t* gl)
1199{
1200 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1201 if (gl) {
1202 egl_context_t* c = egl_context_t::context(gl);
1203 if (c->flags & egl_context_t::IS_CURRENT) {
1204 if (current != gl) {
1205 // it is an error to set a context current, if it's already
1206 // current to another thread
1207 return -1;
1208 }
1209 } else {
1210 if (current) {
1211 // mark the current context as not current, and flush
1212 glFlush();
1213 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1214 }
1215 }
1216 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1217 // The context is not current, make it current!
1218 setGlThreadSpecific(gl);
1219 c->flags |= egl_context_t::IS_CURRENT;
1220 }
1221 } else {
1222 if (current) {
1223 // mark the current context as not current, and flush
1224 glFlush();
1225 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1226 }
1227 // this thread has no context attached to it
1228 setGlThreadSpecific(0);
1229 }
1230 return 0;
1231}
1232
1233static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1234 EGLint attribute, EGLint *value)
1235{
1236 size_t numConfigs = NELEM(gConfigs);
1237 int index = (int)config;
1238 if (uint32_t(index) >= numConfigs)
1239 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1240
1241 int attrIndex;
1242 attrIndex = binarySearch<config_pair_t>(
1243 gConfigs[index].array,
1244 0, gConfigs[index].size-1,
1245 attribute);
1246 if (attrIndex>=0) {
1247 *value = gConfigs[index].array[attrIndex].value;
1248 return EGL_TRUE;
1249 }
1250
1251 attrIndex = binarySearch<config_pair_t>(
1252 config_base_attribute_list,
1253 0, NELEM(config_base_attribute_list)-1,
1254 attribute);
1255 if (attrIndex>=0) {
1256 *value = config_base_attribute_list[attrIndex].value;
1257 return EGL_TRUE;
1258 }
1259 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1260}
1261
1262static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1263 NativeWindowType window, const EGLint *attrib_list)
1264{
1265 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1266 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1267 if (window == 0)
1268 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1269
1270 EGLint surfaceType;
1271 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1272 return EGL_FALSE;
1273
1274 if (!(surfaceType & EGL_WINDOW_BIT))
1275 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1276
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001277 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001278 ANDROID_NATIVE_WINDOW_MAGIC) {
1279 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1280 }
1281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 EGLint configID;
1283 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1284 return EGL_FALSE;
1285
1286 int32_t depthFormat;
1287 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001288 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1290 }
1291
1292 // FIXME: we don't have access to the pixelFormat here just yet.
1293 // (it's possible that the surface is not fully initialized)
1294 // maybe this should be done after the page-flip
1295 //if (EGLint(info.format) != pixelFormat)
1296 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1297
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001298 egl_surface_t* surface;
1299 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001300 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
Mathias Agopian0696a572009-08-20 00:12:56 -07001302 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 // there was a problem in the ctor, the error
1304 // flag has been set.
1305 delete surface;
1306 surface = 0;
1307 }
1308 return surface;
1309}
1310
1311static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1312 NativePixmapType pixmap, const EGLint *attrib_list)
1313{
1314 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1315 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1316 if (pixmap == 0)
1317 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1318
1319 EGLint surfaceType;
1320 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1321 return EGL_FALSE;
1322
1323 if (!(surfaceType & EGL_PIXMAP_BIT))
1324 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1325
Mathias Agopian0696a572009-08-20 00:12:56 -07001326 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1327 sizeof(egl_native_pixmap_t)) {
1328 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1329 }
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 EGLint configID;
1332 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1333 return EGL_FALSE;
1334
1335 int32_t depthFormat;
1336 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001337 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1339 }
1340
1341 if (pixmap->format != pixelFormat)
1342 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1343
1344 egl_surface_t* surface =
1345 new egl_pixmap_surface_t(dpy, config, depthFormat,
1346 static_cast<egl_native_pixmap_t*>(pixmap));
1347
Mathias Agopian0696a572009-08-20 00:12:56 -07001348 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 // there was a problem in the ctor, the error
1350 // flag has been set.
1351 delete surface;
1352 surface = 0;
1353 }
1354 return surface;
1355}
1356
1357static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1358 const EGLint *attrib_list)
1359{
1360 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1361 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1362
1363 EGLint surfaceType;
1364 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1365 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 if (!(surfaceType & EGL_PBUFFER_BIT))
1368 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 EGLint configID;
1371 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1372 return EGL_FALSE;
1373
1374 int32_t depthFormat;
1375 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001376 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1378 }
1379
1380 int32_t w = 0;
1381 int32_t h = 0;
1382 while (attrib_list[0]) {
1383 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1384 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1385 attrib_list+=2;
1386 }
1387
1388 egl_surface_t* surface =
1389 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1390
Mathias Agopian0696a572009-08-20 00:12:56 -07001391 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 // there was a problem in the ctor, the error
1393 // flag has been set.
1394 delete surface;
1395 surface = 0;
1396 }
1397 return surface;
1398}
1399
1400// ----------------------------------------------------------------------------
1401}; // namespace android
1402// ----------------------------------------------------------------------------
1403
1404using namespace android;
1405
1406// ----------------------------------------------------------------------------
1407// Initialization
1408// ----------------------------------------------------------------------------
1409
1410EGLDisplay eglGetDisplay(NativeDisplayType display)
1411{
1412#ifndef HAVE_ANDROID_OS
1413 // this just needs to be done once
1414 if (gGLKey == -1) {
1415 pthread_mutex_lock(&gInitMutex);
1416 if (gGLKey == -1)
1417 pthread_key_create(&gGLKey, NULL);
1418 pthread_mutex_unlock(&gInitMutex);
1419 }
1420#endif
1421 if (display == EGL_DEFAULT_DISPLAY) {
1422 EGLDisplay dpy = (EGLDisplay)1;
1423 egl_display_t& d = egl_display_t::get_display(dpy);
1424 d.type = display;
1425 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001426 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 return EGL_NO_DISPLAY;
1428}
1429
1430EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1431{
1432 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1433 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 EGLBoolean res = EGL_TRUE;
1436 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 if (android_atomic_inc(&d.initialized) == 0) {
1439 // initialize stuff here if needed
1440 //pthread_mutex_lock(&gInitMutex);
1441 //pthread_mutex_unlock(&gInitMutex);
1442 }
1443
1444 if (res == EGL_TRUE) {
1445 if (major != NULL) *major = VERSION_MAJOR;
1446 if (minor != NULL) *minor = VERSION_MINOR;
1447 }
1448 return res;
1449}
1450
1451EGLBoolean eglTerminate(EGLDisplay dpy)
1452{
1453 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1454 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1455
1456 EGLBoolean res = EGL_TRUE;
1457 egl_display_t& d = egl_display_t::get_display(dpy);
1458 if (android_atomic_dec(&d.initialized) == 1) {
1459 // TODO: destroy all resources (surfaces, contexts, etc...)
1460 //pthread_mutex_lock(&gInitMutex);
1461 //pthread_mutex_unlock(&gInitMutex);
1462 }
1463 return res;
1464}
1465
1466// ----------------------------------------------------------------------------
1467// configuration
1468// ----------------------------------------------------------------------------
1469
1470EGLBoolean eglGetConfigs( EGLDisplay dpy,
1471 EGLConfig *configs,
1472 EGLint config_size, EGLint *num_config)
1473{
1474 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1475 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1476
1477 GLint numConfigs = NELEM(gConfigs);
1478 if (!configs) {
1479 *num_config = numConfigs;
1480 return EGL_TRUE;
1481 }
1482 GLint i;
1483 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1484 *configs++ = (EGLConfig)i;
1485 }
1486 *num_config = i;
1487 return EGL_TRUE;
1488}
1489
1490EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1491 EGLConfig *configs, EGLint config_size,
1492 EGLint *num_config)
1493{
1494 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1495 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001496
1497 if (ggl_unlikely(num_config==0)) {
1498 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500
Jack Palevich749c63d2009-03-25 15:12:17 -07001501 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001502 /*
1503 * A NULL attrib_list should be treated as though it was an empty
1504 * one (terminated with EGL_NONE) as defined in
1505 * section 3.4.1 "Querying Configurations" in the EGL specification.
1506 */
1507 static const EGLint dummy = EGL_NONE;
1508 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 int numAttributes = 0;
1512 int numConfigs = NELEM(gConfigs);
1513 uint32_t possibleMatch = (1<<numConfigs)-1;
1514 while(possibleMatch && *attrib_list != EGL_NONE) {
1515 numAttributes++;
1516 EGLint attr = *attrib_list++;
1517 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001518 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 if (!(possibleMatch & (1<<i)))
1520 continue;
1521 if (isAttributeMatching(i, attr, val) == 0) {
1522 possibleMatch &= ~(1<<i);
1523 }
1524 }
1525 }
1526
1527 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001528 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1529 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 // default value
1531 if (binarySearch<config_pair_t>(
1532 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001533 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 config_defaults[j].key) < 0)
1535 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001536 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 if (!(possibleMatch & (1<<i)))
1538 continue;
1539 if (isAttributeMatching(i,
1540 config_defaults[j].key,
1541 config_defaults[j].value) == 0)
1542 {
1543 possibleMatch &= ~(1<<i);
1544 }
1545 }
1546 }
1547 }
1548
1549 // return the configurations found
1550 int n=0;
1551 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001552 if (configs) {
1553 for (int i=0 ; config_size && i<numConfigs ; i++) {
1554 if (possibleMatch & (1<<i)) {
1555 *configs++ = (EGLConfig)i;
1556 config_size--;
1557 n++;
1558 }
1559 }
1560 } else {
1561 for (int i=0 ; i<numConfigs ; i++) {
1562 if (possibleMatch & (1<<i)) {
1563 n++;
1564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 }
1566 }
1567 }
1568 *num_config = n;
1569 return EGL_TRUE;
1570}
1571
1572EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1573 EGLint attribute, EGLint *value)
1574{
1575 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1576 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1577
1578 return getConfigAttrib(dpy, config, attribute, value);
1579}
1580
1581// ----------------------------------------------------------------------------
1582// surfaces
1583// ----------------------------------------------------------------------------
1584
1585EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1586 NativeWindowType window,
1587 const EGLint *attrib_list)
1588{
1589 return createWindowSurface(dpy, config, window, attrib_list);
1590}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1593 NativePixmapType pixmap,
1594 const EGLint *attrib_list)
1595{
1596 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1597}
1598
1599EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1600 const EGLint *attrib_list)
1601{
1602 return createPbufferSurface(dpy, config, attrib_list);
1603}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001604
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1606{
1607 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1608 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1609 if (eglSurface != EGL_NO_SURFACE) {
1610 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001611 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1613 if (surface->dpy != dpy)
1614 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001615 if (surface->ctx) {
1616 // FIXME: this surface is current check what the spec says
1617 surface->disconnect();
1618 surface->ctx = 0;
1619 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 delete surface;
1621 }
1622 return EGL_TRUE;
1623}
1624
1625EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1626 EGLint attribute, EGLint *value)
1627{
1628 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1629 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1630 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001631 if (!surface->isValid())
1632 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 if (surface->dpy != dpy)
1634 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1635
1636 EGLBoolean ret = EGL_TRUE;
1637 switch (attribute) {
1638 case EGL_CONFIG_ID:
1639 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1640 break;
1641 case EGL_WIDTH:
1642 *value = surface->getWidth();
1643 break;
1644 case EGL_HEIGHT:
1645 *value = surface->getHeight();
1646 break;
1647 case EGL_LARGEST_PBUFFER:
1648 // not modified for a window or pixmap surface
1649 break;
1650 case EGL_TEXTURE_FORMAT:
1651 *value = EGL_NO_TEXTURE;
1652 break;
1653 case EGL_TEXTURE_TARGET:
1654 *value = EGL_NO_TEXTURE;
1655 break;
1656 case EGL_MIPMAP_TEXTURE:
1657 *value = EGL_FALSE;
1658 break;
1659 case EGL_MIPMAP_LEVEL:
1660 *value = 0;
1661 break;
1662 case EGL_RENDER_BUFFER:
1663 // TODO: return the real RENDER_BUFFER here
1664 *value = EGL_BACK_BUFFER;
1665 break;
1666 case EGL_HORIZONTAL_RESOLUTION:
1667 // pixel/mm * EGL_DISPLAY_SCALING
1668 *value = surface->getHorizontalResolution();
1669 break;
1670 case EGL_VERTICAL_RESOLUTION:
1671 // pixel/mm * EGL_DISPLAY_SCALING
1672 *value = surface->getVerticalResolution();
1673 break;
1674 case EGL_PIXEL_ASPECT_RATIO: {
1675 // w/h * EGL_DISPLAY_SCALING
1676 int wr = surface->getHorizontalResolution();
1677 int hr = surface->getVerticalResolution();
1678 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1679 } break;
1680 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001681 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 break;
1683 default:
1684 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1685 }
1686 return ret;
1687}
1688
1689EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1690 EGLContext share_list, const EGLint *attrib_list)
1691{
1692 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1693 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1694
1695 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1696 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1697
1698 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1699 c->flags = egl_context_t::NEVER_CURRENT;
1700 c->dpy = dpy;
1701 c->config = config;
1702 c->read = 0;
1703 c->draw = 0;
1704 return (EGLContext)gl;
1705}
1706
1707EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1708{
1709 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1710 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1711 egl_context_t* c = egl_context_t::context(ctx);
1712 if (c->flags & egl_context_t::IS_CURRENT)
1713 setGlThreadSpecific(0);
1714 ogles_uninit((ogles_context_t*)ctx);
1715 return EGL_TRUE;
1716}
1717
1718EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1719 EGLSurface read, EGLContext ctx)
1720{
1721 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1722 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1723 if (draw) {
1724 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001725 if (!s->isValid())
1726 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 if (s->dpy != dpy)
1728 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001729 // TODO: check that draw is compatible with the context
1730 }
1731 if (read && read!=draw) {
1732 egl_surface_t* s = (egl_surface_t*)read;
1733 if (!s->isValid())
1734 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1735 if (s->dpy != dpy)
1736 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1737 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 }
1739
1740 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1743 return setError(EGL_BAD_MATCH, EGL_FALSE);
1744
1745 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1746 return setError(EGL_BAD_MATCH, EGL_FALSE);
1747
1748 if (ctx == EGL_NO_CONTEXT) {
1749 // if we're detaching, we need the current context
1750 current_ctx = (EGLContext)getGlThreadSpecific();
1751 } else {
1752 egl_context_t* c = egl_context_t::context(ctx);
1753 egl_surface_t* d = (egl_surface_t*)draw;
1754 egl_surface_t* r = (egl_surface_t*)read;
1755 if ((d && d->ctx && d->ctx != ctx) ||
1756 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001757 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1759 }
1760 }
1761
1762 ogles_context_t* gl = (ogles_context_t*)ctx;
1763 if (makeCurrent(gl) == 0) {
1764 if (ctx) {
1765 egl_context_t* c = egl_context_t::context(ctx);
1766 egl_surface_t* d = (egl_surface_t*)draw;
1767 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001768
1769 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001770 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1771 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001772 }
1773 if (c->read) {
1774 // FIXME: unlock/disconnect the read surface too
1775 }
1776
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001777 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001778 c->read = read;
1779
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780 if (c->flags & egl_context_t::NEVER_CURRENT) {
1781 c->flags &= ~egl_context_t::NEVER_CURRENT;
1782 GLint w = 0;
1783 GLint h = 0;
1784 if (draw) {
1785 w = d->getWidth();
1786 h = d->getHeight();
1787 }
1788 ogles_surfaceport(gl, 0, 0);
1789 ogles_viewport(gl, 0, 0, w, h);
1790 ogles_scissor(gl, 0, 0, w, h);
1791 }
1792 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001793 if (d->connect() == EGL_FALSE) {
1794 return EGL_FALSE;
1795 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796 d->ctx = ctx;
1797 d->bindDrawSurface(gl);
1798 }
1799 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001800 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 r->ctx = ctx;
1802 r->bindReadSurface(gl);
1803 }
1804 } else {
1805 // if surfaces were bound to the context bound to this thread
1806 // mark then as unbound.
1807 if (current_ctx) {
1808 egl_context_t* c = egl_context_t::context(current_ctx);
1809 egl_surface_t* d = (egl_surface_t*)c->draw;
1810 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001811 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001812 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001813 d->ctx = EGL_NO_CONTEXT;
1814 d->disconnect();
1815 }
1816 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001817 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001818 r->ctx = EGL_NO_CONTEXT;
1819 // FIXME: unlock/disconnect the read surface too
1820 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 }
1822 }
1823 return EGL_TRUE;
1824 }
1825 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1826}
1827
1828EGLContext eglGetCurrentContext(void)
1829{
1830 // eglGetCurrentContext returns the current EGL rendering context,
1831 // as specified by eglMakeCurrent. If no context is current,
1832 // EGL_NO_CONTEXT is returned.
1833 return (EGLContext)getGlThreadSpecific();
1834}
1835
1836EGLSurface eglGetCurrentSurface(EGLint readdraw)
1837{
1838 // eglGetCurrentSurface returns the read or draw surface attached
1839 // to the current EGL rendering context, as specified by eglMakeCurrent.
1840 // If no context is current, EGL_NO_SURFACE is returned.
1841 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1842 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1843 egl_context_t* c = egl_context_t::context(ctx);
1844 if (readdraw == EGL_READ) {
1845 return c->read;
1846 } else if (readdraw == EGL_DRAW) {
1847 return c->draw;
1848 }
1849 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1850}
1851
1852EGLDisplay eglGetCurrentDisplay(void)
1853{
1854 // eglGetCurrentDisplay returns the current EGL display connection
1855 // for the current EGL rendering context, as specified by eglMakeCurrent.
1856 // If no context is current, EGL_NO_DISPLAY is returned.
1857 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1858 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1859 egl_context_t* c = egl_context_t::context(ctx);
1860 return c->dpy;
1861}
1862
1863EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1864 EGLint attribute, EGLint *value)
1865{
1866 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1867 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1868 egl_context_t* c = egl_context_t::context(ctx);
1869 switch (attribute) {
1870 case EGL_CONFIG_ID:
1871 // Returns the ID of the EGL frame buffer configuration with
1872 // respect to which the context was created
1873 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1874 }
1875 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1876}
1877
1878EGLBoolean eglWaitGL(void)
1879{
1880 return EGL_TRUE;
1881}
1882
1883EGLBoolean eglWaitNative(EGLint engine)
1884{
1885 return EGL_TRUE;
1886}
1887
1888EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1889{
1890 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1891 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001892
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001894 if (!d->isValid())
1895 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 if (d->dpy != dpy)
1897 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1898
1899 // post the surface
1900 d->swapBuffers();
1901
1902 // if it's bound to a context, update the buffer
1903 if (d->ctx != EGL_NO_CONTEXT) {
1904 d->bindDrawSurface((ogles_context_t*)d->ctx);
1905 // if this surface is also the read surface of the context
1906 // it is bound to, make sure to update the read buffer as well.
1907 // The EGL spec is a little unclear about this.
1908 egl_context_t* c = egl_context_t::context(d->ctx);
1909 if (c->read == draw) {
1910 d->bindReadSurface((ogles_context_t*)d->ctx);
1911 }
1912 }
1913
1914 return EGL_TRUE;
1915}
1916
1917EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1918 NativePixmapType target)
1919{
1920 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1921 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1922 // TODO: eglCopyBuffers()
1923 return EGL_FALSE;
1924}
1925
1926EGLint eglGetError(void)
1927{
1928 return getError();
1929}
1930
1931const char* eglQueryString(EGLDisplay dpy, EGLint name)
1932{
1933 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1934 return setError(EGL_BAD_DISPLAY, (const char*)0);
1935
1936 switch (name) {
1937 case EGL_VENDOR:
1938 return gVendorString;
1939 case EGL_VERSION:
1940 return gVersionString;
1941 case EGL_EXTENSIONS:
1942 return gExtensionsString;
1943 case EGL_CLIENT_APIS:
1944 return gClientApiString;
1945 }
1946 return setError(EGL_BAD_PARAMETER, (const char *)0);
1947}
1948
1949// ----------------------------------------------------------------------------
1950// EGL 1.1
1951// ----------------------------------------------------------------------------
1952
1953EGLBoolean eglSurfaceAttrib(
1954 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1955{
1956 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1957 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1958 // TODO: eglSurfaceAttrib()
1959 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1960}
1961
1962EGLBoolean eglBindTexImage(
1963 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1964{
1965 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1966 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1967 // TODO: eglBindTexImage()
1968 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1969}
1970
1971EGLBoolean eglReleaseTexImage(
1972 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1973{
1974 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1975 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1976 // TODO: eglReleaseTexImage()
1977 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1978}
1979
1980EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1981{
1982 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1983 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1984 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001985 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986}
1987
1988// ----------------------------------------------------------------------------
1989// EGL 1.2
1990// ----------------------------------------------------------------------------
1991
1992EGLBoolean eglBindAPI(EGLenum api)
1993{
1994 if (api != EGL_OPENGL_ES_API)
1995 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1996 return EGL_TRUE;
1997}
1998
1999EGLenum eglQueryAPI(void)
2000{
2001 return EGL_OPENGL_ES_API;
2002}
2003
2004EGLBoolean eglWaitClient(void)
2005{
2006 glFinish();
2007 return EGL_TRUE;
2008}
2009
2010EGLBoolean eglReleaseThread(void)
2011{
2012 // TODO: eglReleaseThread()
2013 return EGL_TRUE;
2014}
2015
2016EGLSurface eglCreatePbufferFromClientBuffer(
2017 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2018 EGLConfig config, const EGLint *attrib_list)
2019{
2020 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2021 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2022 // TODO: eglCreatePbufferFromClientBuffer()
2023 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2024}
2025
2026// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002027// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028// ----------------------------------------------------------------------------
2029
2030void (*eglGetProcAddress (const char *procname))()
2031{
2032 extention_map_t const * const map = gExtentionMap;
2033 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2034 if (!strcmp(procname, map[i].name)) {
2035 return map[i].address;
2036 }
2037 }
2038 return NULL;
2039}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002040
2041EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2042 const EGLint *attrib_list)
2043{
2044 EGLBoolean result = EGL_FALSE;
2045 return result;
2046}
2047
2048EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2049{
2050 EGLBoolean result = EGL_FALSE;
2051 return result;
2052}
2053
2054EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2055 EGLClientBuffer buffer, const EGLint *attrib_list)
2056{
2057 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2058 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2059 }
2060 if (ctx != EGL_NO_CONTEXT) {
2061 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2062 }
2063 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2064 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2065 }
2066
2067 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2068
2069 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2070 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2071
2072 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2073 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002074
2075 switch (native_buffer->format) {
2076 case HAL_PIXEL_FORMAT_RGBA_8888:
2077 case HAL_PIXEL_FORMAT_RGBX_8888:
2078 case HAL_PIXEL_FORMAT_RGB_888:
2079 case HAL_PIXEL_FORMAT_RGB_565:
2080 case HAL_PIXEL_FORMAT_BGRA_8888:
2081 case HAL_PIXEL_FORMAT_RGBA_5551:
2082 case HAL_PIXEL_FORMAT_RGBA_4444:
2083 break;
2084 default:
2085 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2086 }
2087
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002088 native_buffer->common.incRef(&native_buffer->common);
2089 return (EGLImageKHR)native_buffer;
2090}
2091
2092EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2093{
2094 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2095 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2096 }
2097
2098 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2099
2100 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2101 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2102
2103 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2104 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2105
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002106 native_buffer->common.decRef(&native_buffer->common);
2107
2108 return EGL_TRUE;
2109}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002110
2111// ----------------------------------------------------------------------------
2112// ANDROID extensions
2113// ----------------------------------------------------------------------------
2114
2115EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2116 EGLint left, EGLint top, EGLint width, EGLint height)
2117{
2118 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2119 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2120
2121 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002122 if (!d->isValid())
2123 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002124 if (d->dpy != dpy)
2125 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2126
2127 // post the surface
2128 d->setSwapRectangle(left, top, width, height);
2129
2130 return EGL_TRUE;
2131}