blob: 3fb322a9591e3ef90562dd91829e3363cd50987b [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;
483 simg.w = src->width;
484 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;
489 dimg.w = dst->width;
490 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) {
549 const Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
550 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700551 void* prevBits;
552 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700553 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
554 // copy from previousBuffer to buffer
555 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700556 unlock(previousBuffer);
557 }
558 }
559 }
560 oldDirtyRegion = dirtyRegion;
561 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700562
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700563 if (previousBuffer) {
564 previousBuffer->common.decRef(&previousBuffer->common);
565 previousBuffer = 0;
566 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567
Mathias Agopian0926f502009-05-04 14:17:04 -0700568 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700569 previousBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700571 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700573 // dequeue a new buffer
Mathias Agopian031213e2010-08-18 16:07:34 -0700574 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
575
576 // TODO: lockBuffer should rather be executed when the very first
577 // direct rendering occurs.
578 nativeWindow->lockBuffer(nativeWindow, buffer);
579
580 // reallocate the depth-buffer if needed
581 if ((width != buffer->width) || (height != buffer->height)) {
582 // TODO: we probably should reset the swap rect here
583 // if the window size has changed
584 width = buffer->width;
585 height = buffer->height;
586 if (depth.data) {
587 free(depth.data);
588 depth.width = width;
589 depth.height = height;
590 depth.stride = buffer->stride;
591 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
592 if (depth.data == 0) {
593 setError(EGL_BAD_ALLOC, EGL_FALSE);
594 return EGL_FALSE;
595 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 }
597 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700598
Mathias Agopian031213e2010-08-18 16:07:34 -0700599 // keep a reference on the buffer
600 buffer->common.incRef(&buffer->common);
601
602 // finally pin the buffer down
603 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
604 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
605 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
606 buffer, buffer->width, buffer->height);
607 return setError(EGL_BAD_ACCESS, EGL_FALSE);
608 // FIXME: we should make sure we're not accessing the buffer anymore
609 }
610 } else {
611 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700612 }
613
614 return EGL_TRUE;
615}
616
617EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
618 EGLint l, EGLint t, EGLint w, EGLint h)
619{
620 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 return EGL_TRUE;
622}
623
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625{
626 GGLSurface buffer;
627 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700628 buffer.width = this->buffer->width;
629 buffer.height = this->buffer->height;
630 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700631 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 gl->rasterizer.procs.colorBuffer(gl, &buffer);
634 if (depth.data != gl->rasterizer.state.buffers.depth.data)
635 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 return EGL_TRUE;
638}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640{
641 GGLSurface buffer;
642 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700643 buffer.width = this->buffer->width;
644 buffer.height = this->buffer->height;
645 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700646 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 gl->rasterizer.procs.readBuffer(gl, &buffer);
649 return EGL_TRUE;
650}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
653}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
656}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700657EGLint egl_window_surface_v2_t::getRefreshRate() const {
658 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700660EGLint egl_window_surface_v2_t::getSwapBehavior() const
661{
662 /*
663 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
664 * the content of the swapped buffer.
665 *
666 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
667 *
668 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
669 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
670 * is, everything outside of this area is preserved.
671 *
672 * This implementation of EGL assumes the later case.
673 *
674 */
675
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700676 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677}
678
679// ----------------------------------------------------------------------------
680
681struct egl_pixmap_surface_t : public egl_surface_t
682{
683 egl_pixmap_surface_t(
684 EGLDisplay dpy, EGLConfig config,
685 int32_t depthFormat,
686 egl_native_pixmap_t const * pixmap);
687
688 virtual ~egl_pixmap_surface_t() { }
689
Mathias Agopian0696a572009-08-20 00:12:56 -0700690 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
692 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
693 virtual EGLint getWidth() const { return nativePixmap.width; }
694 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695private:
696 egl_native_pixmap_t nativePixmap;
697};
698
699egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
700 EGLConfig config,
701 int32_t depthFormat,
702 egl_native_pixmap_t const * pixmap)
703 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
704{
705 if (depthFormat) {
706 depth.width = pixmap->width;
707 depth.height = pixmap->height;
708 depth.stride = depth.width; // use the width here
709 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
710 if (depth.data == 0) {
711 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 }
713 }
714}
715EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
716{
717 GGLSurface buffer;
718 buffer.version = sizeof(GGLSurface);
719 buffer.width = nativePixmap.width;
720 buffer.height = nativePixmap.height;
721 buffer.stride = nativePixmap.stride;
722 buffer.data = nativePixmap.data;
723 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 gl->rasterizer.procs.colorBuffer(gl, &buffer);
726 if (depth.data != gl->rasterizer.state.buffers.depth.data)
727 gl->rasterizer.procs.depthBuffer(gl, &depth);
728 return EGL_TRUE;
729}
730EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
731{
732 GGLSurface buffer;
733 buffer.version = sizeof(GGLSurface);
734 buffer.width = nativePixmap.width;
735 buffer.height = nativePixmap.height;
736 buffer.stride = nativePixmap.stride;
737 buffer.data = nativePixmap.data;
738 buffer.format = nativePixmap.format;
739 gl->rasterizer.procs.readBuffer(gl, &buffer);
740 return EGL_TRUE;
741}
742
743// ----------------------------------------------------------------------------
744
745struct egl_pbuffer_surface_t : public egl_surface_t
746{
747 egl_pbuffer_surface_t(
748 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
749 int32_t w, int32_t h, int32_t f);
750
751 virtual ~egl_pbuffer_surface_t();
752
Mathias Agopian0696a572009-08-20 00:12:56 -0700753 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
755 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
756 virtual EGLint getWidth() const { return pbuffer.width; }
757 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758private:
759 GGLSurface pbuffer;
760};
761
762egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
763 EGLConfig config, int32_t depthFormat,
764 int32_t w, int32_t h, int32_t f)
765 : egl_surface_t(dpy, config, depthFormat)
766{
767 size_t size = w*h;
768 switch (f) {
769 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
770 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
771 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800772 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 default:
774 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
775 pbuffer.data = 0;
776 break;
777 }
778 pbuffer.version = sizeof(GGLSurface);
779 pbuffer.width = w;
780 pbuffer.height = h;
781 pbuffer.stride = w;
782 pbuffer.data = (GGLubyte*)malloc(size);
783 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 if (depthFormat) {
786 depth.width = pbuffer.width;
787 depth.height = pbuffer.height;
788 depth.stride = depth.width; // use the width here
789 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
790 if (depth.data == 0) {
791 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
792 return;
793 }
794 }
795}
796egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
797 free(pbuffer.data);
798}
799EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
800{
801 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
802 if (depth.data != gl->rasterizer.state.buffers.depth.data)
803 gl->rasterizer.procs.depthBuffer(gl, &depth);
804 return EGL_TRUE;
805}
806EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
807{
808 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
809 return EGL_TRUE;
810}
811
812// ----------------------------------------------------------------------------
813
814struct config_pair_t {
815 GLint key;
816 GLint value;
817};
818
819struct configs_t {
820 const config_pair_t* array;
821 int size;
822};
823
824struct config_management_t {
825 GLint key;
826 bool (*match)(GLint reqValue, GLint confValue);
827 static bool atLeast(GLint reqValue, GLint confValue) {
828 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
829 }
830 static bool exact(GLint reqValue, GLint confValue) {
831 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
832 }
833 static bool mask(GLint reqValue, GLint confValue) {
834 return (confValue & reqValue) == reqValue;
835 }
836};
837
838// ----------------------------------------------------------------------------
839
840#define VERSION_MAJOR 1
841#define VERSION_MINOR 2
842static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700843static char const * const gVersionString = "1.2 Android Driver 1.2.0";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700845static char const * const gExtensionsString =
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700846 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700847 // "KHR_image_pixmap "
848 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700849 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700850 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851
852// ----------------------------------------------------------------------------
853
854struct extention_map_t {
855 const char * const name;
856 __eglMustCastToProperFunctionPointerType address;
857};
858
859static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700860 { "glDrawTexsOES",
861 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
862 { "glDrawTexiOES",
863 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
864 { "glDrawTexfOES",
865 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
866 { "glDrawTexxOES",
867 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
868 { "glDrawTexsvOES",
869 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
870 { "glDrawTexivOES",
871 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
872 { "glDrawTexfvOES",
873 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
874 { "glDrawTexxvOES",
875 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
876 { "glQueryMatrixxOES",
877 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
878 { "glEGLImageTargetTexture2DOES",
879 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
880 { "glEGLImageTargetRenderbufferStorageOES",
881 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
882 { "glClipPlanef",
883 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
884 { "glClipPlanex",
885 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
886 { "glBindBuffer",
887 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
888 { "glBufferData",
889 (__eglMustCastToProperFunctionPointerType)&glBufferData },
890 { "glBufferSubData",
891 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
892 { "glDeleteBuffers",
893 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
894 { "glGenBuffers",
895 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700896 { "eglCreateImageKHR",
897 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
898 { "eglDestroyImageKHR",
899 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
900 { "eglSetSwapRectangleANDROID",
901 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902};
903
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700904/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 * In the lists below, attributes names MUST be sorted.
906 * Additionally, all configs must be sorted according to
907 * the EGL specification.
908 */
909
910static config_pair_t const config_base_attribute_list[] = {
911 { EGL_STENCIL_SIZE, 0 },
912 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
913 { EGL_LEVEL, 0 },
914 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700915 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
917 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
918 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
919 { EGL_NATIVE_VISUAL_ID, 0 },
920 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
921 { EGL_SAMPLES, 0 },
922 { EGL_SAMPLE_BUFFERS, 0 },
923 { EGL_TRANSPARENT_TYPE, EGL_NONE },
924 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
925 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
926 { EGL_TRANSPARENT_RED_VALUE, 0 },
927 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
928 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
929 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700930 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700931 { EGL_LUMINANCE_SIZE, 0 },
932 { EGL_ALPHA_MASK_SIZE, 0 },
933 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700934 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700935 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936};
937
938// These configs can override the base attribute list
939// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
940
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800941// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942static config_pair_t const config_0_attribute_list[] = {
943 { EGL_BUFFER_SIZE, 16 },
944 { EGL_ALPHA_SIZE, 0 },
945 { EGL_BLUE_SIZE, 5 },
946 { EGL_GREEN_SIZE, 6 },
947 { EGL_RED_SIZE, 5 },
948 { EGL_DEPTH_SIZE, 0 },
949 { EGL_CONFIG_ID, 0 },
950 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
951};
952
953static config_pair_t const config_1_attribute_list[] = {
954 { EGL_BUFFER_SIZE, 16 },
955 { EGL_ALPHA_SIZE, 0 },
956 { EGL_BLUE_SIZE, 5 },
957 { EGL_GREEN_SIZE, 6 },
958 { EGL_RED_SIZE, 5 },
959 { EGL_DEPTH_SIZE, 16 },
960 { EGL_CONFIG_ID, 1 },
961 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
962};
963
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800964// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965static config_pair_t const config_2_attribute_list[] = {
966 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800967 { EGL_ALPHA_SIZE, 0 },
968 { EGL_BLUE_SIZE, 8 },
969 { EGL_GREEN_SIZE, 8 },
970 { EGL_RED_SIZE, 8 },
971 { EGL_DEPTH_SIZE, 0 },
972 { EGL_CONFIG_ID, 6 },
973 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
974};
975
976static config_pair_t const config_3_attribute_list[] = {
977 { EGL_BUFFER_SIZE, 32 },
978 { EGL_ALPHA_SIZE, 0 },
979 { EGL_BLUE_SIZE, 8 },
980 { EGL_GREEN_SIZE, 8 },
981 { EGL_RED_SIZE, 8 },
982 { EGL_DEPTH_SIZE, 16 },
983 { EGL_CONFIG_ID, 7 },
984 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
985};
986
987// 8888 configs
988static config_pair_t const config_4_attribute_list[] = {
989 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 { EGL_ALPHA_SIZE, 8 },
991 { EGL_BLUE_SIZE, 8 },
992 { EGL_GREEN_SIZE, 8 },
993 { EGL_RED_SIZE, 8 },
994 { EGL_DEPTH_SIZE, 0 },
995 { EGL_CONFIG_ID, 2 },
996 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
997};
998
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800999static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 { EGL_BUFFER_SIZE, 32 },
1001 { EGL_ALPHA_SIZE, 8 },
1002 { EGL_BLUE_SIZE, 8 },
1003 { EGL_GREEN_SIZE, 8 },
1004 { EGL_RED_SIZE, 8 },
1005 { EGL_DEPTH_SIZE, 16 },
1006 { EGL_CONFIG_ID, 3 },
1007 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1008};
1009
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001010// A8 configs
1011static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 { EGL_BUFFER_SIZE, 8 },
1013 { EGL_ALPHA_SIZE, 8 },
1014 { EGL_BLUE_SIZE, 0 },
1015 { EGL_GREEN_SIZE, 0 },
1016 { EGL_RED_SIZE, 0 },
1017 { EGL_DEPTH_SIZE, 0 },
1018 { EGL_CONFIG_ID, 4 },
1019 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1020};
1021
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001022static config_pair_t const config_7_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, 16 },
1029 { EGL_CONFIG_ID, 5 },
1030 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1031};
1032
1033static configs_t const gConfigs[] = {
1034 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1035 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1036 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1037 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1038 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1039 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001040 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1041 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042};
1043
1044static config_management_t const gConfigManagement[] = {
1045 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1046 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1047 { EGL_BLUE_SIZE, config_management_t::atLeast },
1048 { EGL_GREEN_SIZE, config_management_t::atLeast },
1049 { EGL_RED_SIZE, config_management_t::atLeast },
1050 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1051 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1052 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1053 { EGL_CONFIG_ID, config_management_t::exact },
1054 { EGL_LEVEL, config_management_t::exact },
1055 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::exact },
1056 { EGL_MAX_PBUFFER_PIXELS, config_management_t::exact },
1057 { EGL_MAX_PBUFFER_WIDTH, config_management_t::exact },
1058 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
1059 { EGL_NATIVE_VISUAL_ID, config_management_t::exact },
1060 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1061 { EGL_SAMPLES, config_management_t::exact },
1062 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1063 { EGL_SURFACE_TYPE, config_management_t::mask },
1064 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1065 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1066 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1067 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1068 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1069 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1070 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1071 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001072 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1073 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1074 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1075 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1076 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077};
1078
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001081 // attributes that are not specified are simply ignored, if a particular
1082 // one needs not be ignored, it must be specified here, eg:
1083 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084};
1085
1086// ----------------------------------------------------------------------------
1087
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001088static status_t getConfigFormatInfo(EGLint configID,
1089 int32_t& pixelFormat, int32_t& depthFormat)
1090{
1091 switch(configID) {
1092 case 0:
1093 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1094 depthFormat = 0;
1095 break;
1096 case 1:
1097 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1098 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1099 break;
1100 case 2:
1101 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1102 depthFormat = 0;
1103 break;
1104 case 3:
1105 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1106 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1107 break;
1108 case 4:
1109 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1110 depthFormat = 0;
1111 break;
1112 case 5:
1113 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1114 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1115 break;
1116 case 6:
1117 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1118 depthFormat = 0;
1119 break;
1120 case 7:
1121 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1122 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1123 break;
1124 default:
1125 return NAME_NOT_FOUND;
1126 }
1127 return NO_ERROR;
1128}
1129
1130// ----------------------------------------------------------------------------
1131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132template<typename T>
1133static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1134{
1135 while (first <= last) {
1136 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001137 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001139 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 last = mid - 1;
1141 } else {
1142 return mid;
1143 }
1144 }
1145 return -1;
1146}
1147
1148static int isAttributeMatching(int i, EGLint attr, EGLint val)
1149{
1150 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001151 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 int index = binarySearch<config_pair_t>(
1153 gConfigs[i].array,
1154 0, gConfigs[i].size-1,
1155 attr);
1156 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001157 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 index = binarySearch<config_pair_t>(
1159 config_base_attribute_list,
1160 0, NELEM(config_base_attribute_list)-1,
1161 attr);
1162 }
1163 if (index >= 0) {
1164 // attribute found, check if this config could match
1165 int cfgMgtIndex = binarySearch<config_management_t>(
1166 gConfigManagement,
1167 0, NELEM(gConfigManagement)-1,
1168 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001169 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 bool match = gConfigManagement[cfgMgtIndex].match(
1171 val, configFound[index].value);
1172 if (match) {
1173 // this config matches
1174 return 1;
1175 }
1176 } else {
1177 // attribute not found. this should NEVER happen.
1178 }
1179 } else {
1180 // error, this attribute doesn't exist
1181 }
1182 return 0;
1183}
1184
1185static int makeCurrent(ogles_context_t* gl)
1186{
1187 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1188 if (gl) {
1189 egl_context_t* c = egl_context_t::context(gl);
1190 if (c->flags & egl_context_t::IS_CURRENT) {
1191 if (current != gl) {
1192 // it is an error to set a context current, if it's already
1193 // current to another thread
1194 return -1;
1195 }
1196 } else {
1197 if (current) {
1198 // mark the current context as not current, and flush
1199 glFlush();
1200 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1201 }
1202 }
1203 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1204 // The context is not current, make it current!
1205 setGlThreadSpecific(gl);
1206 c->flags |= egl_context_t::IS_CURRENT;
1207 }
1208 } else {
1209 if (current) {
1210 // mark the current context as not current, and flush
1211 glFlush();
1212 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1213 }
1214 // this thread has no context attached to it
1215 setGlThreadSpecific(0);
1216 }
1217 return 0;
1218}
1219
1220static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1221 EGLint attribute, EGLint *value)
1222{
1223 size_t numConfigs = NELEM(gConfigs);
1224 int index = (int)config;
1225 if (uint32_t(index) >= numConfigs)
1226 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1227
1228 int attrIndex;
1229 attrIndex = binarySearch<config_pair_t>(
1230 gConfigs[index].array,
1231 0, gConfigs[index].size-1,
1232 attribute);
1233 if (attrIndex>=0) {
1234 *value = gConfigs[index].array[attrIndex].value;
1235 return EGL_TRUE;
1236 }
1237
1238 attrIndex = binarySearch<config_pair_t>(
1239 config_base_attribute_list,
1240 0, NELEM(config_base_attribute_list)-1,
1241 attribute);
1242 if (attrIndex>=0) {
1243 *value = config_base_attribute_list[attrIndex].value;
1244 return EGL_TRUE;
1245 }
1246 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1247}
1248
1249static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1250 NativeWindowType window, const EGLint *attrib_list)
1251{
1252 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1253 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1254 if (window == 0)
1255 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1256
1257 EGLint surfaceType;
1258 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1259 return EGL_FALSE;
1260
1261 if (!(surfaceType & EGL_WINDOW_BIT))
1262 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1263
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001264 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001265 ANDROID_NATIVE_WINDOW_MAGIC) {
1266 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1267 }
1268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 EGLint configID;
1270 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1271 return EGL_FALSE;
1272
1273 int32_t depthFormat;
1274 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001275 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1277 }
1278
1279 // FIXME: we don't have access to the pixelFormat here just yet.
1280 // (it's possible that the surface is not fully initialized)
1281 // maybe this should be done after the page-flip
1282 //if (EGLint(info.format) != pixelFormat)
1283 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1284
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001285 egl_surface_t* surface;
1286 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001287 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288
Mathias Agopian0696a572009-08-20 00:12:56 -07001289 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 // there was a problem in the ctor, the error
1291 // flag has been set.
1292 delete surface;
1293 surface = 0;
1294 }
1295 return surface;
1296}
1297
1298static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1299 NativePixmapType pixmap, const EGLint *attrib_list)
1300{
1301 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1302 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1303 if (pixmap == 0)
1304 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1305
1306 EGLint surfaceType;
1307 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1308 return EGL_FALSE;
1309
1310 if (!(surfaceType & EGL_PIXMAP_BIT))
1311 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1312
Mathias Agopian0696a572009-08-20 00:12:56 -07001313 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1314 sizeof(egl_native_pixmap_t)) {
1315 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1316 }
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 EGLint configID;
1319 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1320 return EGL_FALSE;
1321
1322 int32_t depthFormat;
1323 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001324 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1326 }
1327
1328 if (pixmap->format != pixelFormat)
1329 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1330
1331 egl_surface_t* surface =
1332 new egl_pixmap_surface_t(dpy, config, depthFormat,
1333 static_cast<egl_native_pixmap_t*>(pixmap));
1334
Mathias Agopian0696a572009-08-20 00:12:56 -07001335 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 // there was a problem in the ctor, the error
1337 // flag has been set.
1338 delete surface;
1339 surface = 0;
1340 }
1341 return surface;
1342}
1343
1344static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1345 const EGLint *attrib_list)
1346{
1347 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1348 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1349
1350 EGLint surfaceType;
1351 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1352 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 if (!(surfaceType & EGL_PBUFFER_BIT))
1355 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001356
The Android Open Source Projectedbf3b62009-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 Agopian8b6b95a2009-11-03 16:17:55 -08001363 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1365 }
1366
1367 int32_t w = 0;
1368 int32_t h = 0;
1369 while (attrib_list[0]) {
1370 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1371 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1372 attrib_list+=2;
1373 }
1374
1375 egl_surface_t* surface =
1376 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1377
Mathias Agopian0696a572009-08-20 00:12:56 -07001378 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 // there was a problem in the ctor, the error
1380 // flag has been set.
1381 delete surface;
1382 surface = 0;
1383 }
1384 return surface;
1385}
1386
1387// ----------------------------------------------------------------------------
1388}; // namespace android
1389// ----------------------------------------------------------------------------
1390
1391using namespace android;
1392
1393// ----------------------------------------------------------------------------
1394// Initialization
1395// ----------------------------------------------------------------------------
1396
1397EGLDisplay eglGetDisplay(NativeDisplayType display)
1398{
1399#ifndef HAVE_ANDROID_OS
1400 // this just needs to be done once
1401 if (gGLKey == -1) {
1402 pthread_mutex_lock(&gInitMutex);
1403 if (gGLKey == -1)
1404 pthread_key_create(&gGLKey, NULL);
1405 pthread_mutex_unlock(&gInitMutex);
1406 }
1407#endif
1408 if (display == EGL_DEFAULT_DISPLAY) {
1409 EGLDisplay dpy = (EGLDisplay)1;
1410 egl_display_t& d = egl_display_t::get_display(dpy);
1411 d.type = display;
1412 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001413 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 return EGL_NO_DISPLAY;
1415}
1416
1417EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1418{
1419 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1420 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 EGLBoolean res = EGL_TRUE;
1423 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 if (android_atomic_inc(&d.initialized) == 0) {
1426 // initialize stuff here if needed
1427 //pthread_mutex_lock(&gInitMutex);
1428 //pthread_mutex_unlock(&gInitMutex);
1429 }
1430
1431 if (res == EGL_TRUE) {
1432 if (major != NULL) *major = VERSION_MAJOR;
1433 if (minor != NULL) *minor = VERSION_MINOR;
1434 }
1435 return res;
1436}
1437
1438EGLBoolean eglTerminate(EGLDisplay dpy)
1439{
1440 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1441 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1442
1443 EGLBoolean res = EGL_TRUE;
1444 egl_display_t& d = egl_display_t::get_display(dpy);
1445 if (android_atomic_dec(&d.initialized) == 1) {
1446 // TODO: destroy all resources (surfaces, contexts, etc...)
1447 //pthread_mutex_lock(&gInitMutex);
1448 //pthread_mutex_unlock(&gInitMutex);
1449 }
1450 return res;
1451}
1452
1453// ----------------------------------------------------------------------------
1454// configuration
1455// ----------------------------------------------------------------------------
1456
1457EGLBoolean eglGetConfigs( EGLDisplay dpy,
1458 EGLConfig *configs,
1459 EGLint config_size, EGLint *num_config)
1460{
1461 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1462 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1463
1464 GLint numConfigs = NELEM(gConfigs);
1465 if (!configs) {
1466 *num_config = numConfigs;
1467 return EGL_TRUE;
1468 }
1469 GLint i;
1470 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1471 *configs++ = (EGLConfig)i;
1472 }
1473 *num_config = i;
1474 return EGL_TRUE;
1475}
1476
1477EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1478 EGLConfig *configs, EGLint config_size,
1479 EGLint *num_config)
1480{
1481 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1482 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001483
1484 if (ggl_unlikely(num_config==0)) {
1485 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487
Jack Palevich749c63d2009-03-25 15:12:17 -07001488 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001489 /*
1490 * A NULL attrib_list should be treated as though it was an empty
1491 * one (terminated with EGL_NONE) as defined in
1492 * section 3.4.1 "Querying Configurations" in the EGL specification.
1493 */
1494 static const EGLint dummy = EGL_NONE;
1495 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 int numAttributes = 0;
1499 int numConfigs = NELEM(gConfigs);
1500 uint32_t possibleMatch = (1<<numConfigs)-1;
1501 while(possibleMatch && *attrib_list != EGL_NONE) {
1502 numAttributes++;
1503 EGLint attr = *attrib_list++;
1504 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001505 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 if (!(possibleMatch & (1<<i)))
1507 continue;
1508 if (isAttributeMatching(i, attr, val) == 0) {
1509 possibleMatch &= ~(1<<i);
1510 }
1511 }
1512 }
1513
1514 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001515 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1516 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 // default value
1518 if (binarySearch<config_pair_t>(
1519 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001520 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 config_defaults[j].key) < 0)
1522 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001523 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 if (!(possibleMatch & (1<<i)))
1525 continue;
1526 if (isAttributeMatching(i,
1527 config_defaults[j].key,
1528 config_defaults[j].value) == 0)
1529 {
1530 possibleMatch &= ~(1<<i);
1531 }
1532 }
1533 }
1534 }
1535
1536 // return the configurations found
1537 int n=0;
1538 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001539 if (configs) {
1540 for (int i=0 ; config_size && i<numConfigs ; i++) {
1541 if (possibleMatch & (1<<i)) {
1542 *configs++ = (EGLConfig)i;
1543 config_size--;
1544 n++;
1545 }
1546 }
1547 } else {
1548 for (int i=0 ; i<numConfigs ; i++) {
1549 if (possibleMatch & (1<<i)) {
1550 n++;
1551 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 }
1553 }
1554 }
1555 *num_config = n;
1556 return EGL_TRUE;
1557}
1558
1559EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1560 EGLint attribute, EGLint *value)
1561{
1562 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1563 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1564
1565 return getConfigAttrib(dpy, config, attribute, value);
1566}
1567
1568// ----------------------------------------------------------------------------
1569// surfaces
1570// ----------------------------------------------------------------------------
1571
1572EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1573 NativeWindowType window,
1574 const EGLint *attrib_list)
1575{
1576 return createWindowSurface(dpy, config, window, attrib_list);
1577}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1580 NativePixmapType pixmap,
1581 const EGLint *attrib_list)
1582{
1583 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1584}
1585
1586EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1587 const EGLint *attrib_list)
1588{
1589 return createPbufferSurface(dpy, config, attrib_list);
1590}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1593{
1594 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1595 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1596 if (eglSurface != EGL_NO_SURFACE) {
1597 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001598 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1600 if (surface->dpy != dpy)
1601 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001602 if (surface->ctx) {
1603 // FIXME: this surface is current check what the spec says
1604 surface->disconnect();
1605 surface->ctx = 0;
1606 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 delete surface;
1608 }
1609 return EGL_TRUE;
1610}
1611
1612EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1613 EGLint attribute, EGLint *value)
1614{
1615 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1616 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1617 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001618 if (!surface->isValid())
1619 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 if (surface->dpy != dpy)
1621 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1622
1623 EGLBoolean ret = EGL_TRUE;
1624 switch (attribute) {
1625 case EGL_CONFIG_ID:
1626 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1627 break;
1628 case EGL_WIDTH:
1629 *value = surface->getWidth();
1630 break;
1631 case EGL_HEIGHT:
1632 *value = surface->getHeight();
1633 break;
1634 case EGL_LARGEST_PBUFFER:
1635 // not modified for a window or pixmap surface
1636 break;
1637 case EGL_TEXTURE_FORMAT:
1638 *value = EGL_NO_TEXTURE;
1639 break;
1640 case EGL_TEXTURE_TARGET:
1641 *value = EGL_NO_TEXTURE;
1642 break;
1643 case EGL_MIPMAP_TEXTURE:
1644 *value = EGL_FALSE;
1645 break;
1646 case EGL_MIPMAP_LEVEL:
1647 *value = 0;
1648 break;
1649 case EGL_RENDER_BUFFER:
1650 // TODO: return the real RENDER_BUFFER here
1651 *value = EGL_BACK_BUFFER;
1652 break;
1653 case EGL_HORIZONTAL_RESOLUTION:
1654 // pixel/mm * EGL_DISPLAY_SCALING
1655 *value = surface->getHorizontalResolution();
1656 break;
1657 case EGL_VERTICAL_RESOLUTION:
1658 // pixel/mm * EGL_DISPLAY_SCALING
1659 *value = surface->getVerticalResolution();
1660 break;
1661 case EGL_PIXEL_ASPECT_RATIO: {
1662 // w/h * EGL_DISPLAY_SCALING
1663 int wr = surface->getHorizontalResolution();
1664 int hr = surface->getVerticalResolution();
1665 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1666 } break;
1667 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 break;
1670 default:
1671 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1672 }
1673 return ret;
1674}
1675
1676EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1677 EGLContext share_list, const EGLint *attrib_list)
1678{
1679 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1680 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1681
1682 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1683 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1684
1685 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1686 c->flags = egl_context_t::NEVER_CURRENT;
1687 c->dpy = dpy;
1688 c->config = config;
1689 c->read = 0;
1690 c->draw = 0;
1691 return (EGLContext)gl;
1692}
1693
1694EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1695{
1696 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1697 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1698 egl_context_t* c = egl_context_t::context(ctx);
1699 if (c->flags & egl_context_t::IS_CURRENT)
1700 setGlThreadSpecific(0);
1701 ogles_uninit((ogles_context_t*)ctx);
1702 return EGL_TRUE;
1703}
1704
1705EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1706 EGLSurface read, EGLContext ctx)
1707{
1708 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1709 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1710 if (draw) {
1711 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001712 if (!s->isValid())
1713 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 if (s->dpy != dpy)
1715 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001716 // TODO: check that draw is compatible with the context
1717 }
1718 if (read && read!=draw) {
1719 egl_surface_t* s = (egl_surface_t*)read;
1720 if (!s->isValid())
1721 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1722 if (s->dpy != dpy)
1723 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1724 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
1726
1727 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1730 return setError(EGL_BAD_MATCH, EGL_FALSE);
1731
1732 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1733 return setError(EGL_BAD_MATCH, EGL_FALSE);
1734
1735 if (ctx == EGL_NO_CONTEXT) {
1736 // if we're detaching, we need the current context
1737 current_ctx = (EGLContext)getGlThreadSpecific();
1738 } else {
1739 egl_context_t* c = egl_context_t::context(ctx);
1740 egl_surface_t* d = (egl_surface_t*)draw;
1741 egl_surface_t* r = (egl_surface_t*)read;
1742 if ((d && d->ctx && d->ctx != ctx) ||
1743 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001744 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1746 }
1747 }
1748
1749 ogles_context_t* gl = (ogles_context_t*)ctx;
1750 if (makeCurrent(gl) == 0) {
1751 if (ctx) {
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;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001755
1756 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001757 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1758 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001759 }
1760 if (c->read) {
1761 // FIXME: unlock/disconnect the read surface too
1762 }
1763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001765 c->read = read;
1766
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 if (c->flags & egl_context_t::NEVER_CURRENT) {
1768 c->flags &= ~egl_context_t::NEVER_CURRENT;
1769 GLint w = 0;
1770 GLint h = 0;
1771 if (draw) {
1772 w = d->getWidth();
1773 h = d->getHeight();
1774 }
1775 ogles_surfaceport(gl, 0, 0);
1776 ogles_viewport(gl, 0, 0, w, h);
1777 ogles_scissor(gl, 0, 0, w, h);
1778 }
1779 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001780 if (d->connect() == EGL_FALSE) {
1781 return EGL_FALSE;
1782 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 d->ctx = ctx;
1784 d->bindDrawSurface(gl);
1785 }
1786 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001787 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 r->ctx = ctx;
1789 r->bindReadSurface(gl);
1790 }
1791 } else {
1792 // if surfaces were bound to the context bound to this thread
1793 // mark then as unbound.
1794 if (current_ctx) {
1795 egl_context_t* c = egl_context_t::context(current_ctx);
1796 egl_surface_t* d = (egl_surface_t*)c->draw;
1797 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001798 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001799 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001800 d->ctx = EGL_NO_CONTEXT;
1801 d->disconnect();
1802 }
1803 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001804 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001805 r->ctx = EGL_NO_CONTEXT;
1806 // FIXME: unlock/disconnect the read surface too
1807 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001808 }
1809 }
1810 return EGL_TRUE;
1811 }
1812 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1813}
1814
1815EGLContext eglGetCurrentContext(void)
1816{
1817 // eglGetCurrentContext returns the current EGL rendering context,
1818 // as specified by eglMakeCurrent. If no context is current,
1819 // EGL_NO_CONTEXT is returned.
1820 return (EGLContext)getGlThreadSpecific();
1821}
1822
1823EGLSurface eglGetCurrentSurface(EGLint readdraw)
1824{
1825 // eglGetCurrentSurface returns the read or draw surface attached
1826 // to the current EGL rendering context, as specified by eglMakeCurrent.
1827 // If no context is current, EGL_NO_SURFACE is returned.
1828 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1829 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1830 egl_context_t* c = egl_context_t::context(ctx);
1831 if (readdraw == EGL_READ) {
1832 return c->read;
1833 } else if (readdraw == EGL_DRAW) {
1834 return c->draw;
1835 }
1836 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1837}
1838
1839EGLDisplay eglGetCurrentDisplay(void)
1840{
1841 // eglGetCurrentDisplay returns the current EGL display connection
1842 // for the current EGL rendering context, as specified by eglMakeCurrent.
1843 // If no context is current, EGL_NO_DISPLAY is returned.
1844 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1845 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1846 egl_context_t* c = egl_context_t::context(ctx);
1847 return c->dpy;
1848}
1849
1850EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1851 EGLint attribute, EGLint *value)
1852{
1853 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1854 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1855 egl_context_t* c = egl_context_t::context(ctx);
1856 switch (attribute) {
1857 case EGL_CONFIG_ID:
1858 // Returns the ID of the EGL frame buffer configuration with
1859 // respect to which the context was created
1860 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1861 }
1862 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1863}
1864
1865EGLBoolean eglWaitGL(void)
1866{
1867 return EGL_TRUE;
1868}
1869
1870EGLBoolean eglWaitNative(EGLint engine)
1871{
1872 return EGL_TRUE;
1873}
1874
1875EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1876{
1877 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1878 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001879
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001881 if (!d->isValid())
1882 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 if (d->dpy != dpy)
1884 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1885
1886 // post the surface
1887 d->swapBuffers();
1888
1889 // if it's bound to a context, update the buffer
1890 if (d->ctx != EGL_NO_CONTEXT) {
1891 d->bindDrawSurface((ogles_context_t*)d->ctx);
1892 // if this surface is also the read surface of the context
1893 // it is bound to, make sure to update the read buffer as well.
1894 // The EGL spec is a little unclear about this.
1895 egl_context_t* c = egl_context_t::context(d->ctx);
1896 if (c->read == draw) {
1897 d->bindReadSurface((ogles_context_t*)d->ctx);
1898 }
1899 }
1900
1901 return EGL_TRUE;
1902}
1903
1904EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1905 NativePixmapType target)
1906{
1907 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1908 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1909 // TODO: eglCopyBuffers()
1910 return EGL_FALSE;
1911}
1912
1913EGLint eglGetError(void)
1914{
1915 return getError();
1916}
1917
1918const char* eglQueryString(EGLDisplay dpy, EGLint name)
1919{
1920 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1921 return setError(EGL_BAD_DISPLAY, (const char*)0);
1922
1923 switch (name) {
1924 case EGL_VENDOR:
1925 return gVendorString;
1926 case EGL_VERSION:
1927 return gVersionString;
1928 case EGL_EXTENSIONS:
1929 return gExtensionsString;
1930 case EGL_CLIENT_APIS:
1931 return gClientApiString;
1932 }
1933 return setError(EGL_BAD_PARAMETER, (const char *)0);
1934}
1935
1936// ----------------------------------------------------------------------------
1937// EGL 1.1
1938// ----------------------------------------------------------------------------
1939
1940EGLBoolean eglSurfaceAttrib(
1941 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1942{
1943 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1944 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1945 // TODO: eglSurfaceAttrib()
1946 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1947}
1948
1949EGLBoolean eglBindTexImage(
1950 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1951{
1952 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1953 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1954 // TODO: eglBindTexImage()
1955 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1956}
1957
1958EGLBoolean eglReleaseTexImage(
1959 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1960{
1961 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1962 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1963 // TODO: eglReleaseTexImage()
1964 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1965}
1966
1967EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1968{
1969 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1970 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1971 // TODO: eglSwapInterval()
1972 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1973}
1974
1975// ----------------------------------------------------------------------------
1976// EGL 1.2
1977// ----------------------------------------------------------------------------
1978
1979EGLBoolean eglBindAPI(EGLenum api)
1980{
1981 if (api != EGL_OPENGL_ES_API)
1982 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1983 return EGL_TRUE;
1984}
1985
1986EGLenum eglQueryAPI(void)
1987{
1988 return EGL_OPENGL_ES_API;
1989}
1990
1991EGLBoolean eglWaitClient(void)
1992{
1993 glFinish();
1994 return EGL_TRUE;
1995}
1996
1997EGLBoolean eglReleaseThread(void)
1998{
1999 // TODO: eglReleaseThread()
2000 return EGL_TRUE;
2001}
2002
2003EGLSurface eglCreatePbufferFromClientBuffer(
2004 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2005 EGLConfig config, const EGLint *attrib_list)
2006{
2007 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2008 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2009 // TODO: eglCreatePbufferFromClientBuffer()
2010 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2011}
2012
2013// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002014// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002015// ----------------------------------------------------------------------------
2016
2017void (*eglGetProcAddress (const char *procname))()
2018{
2019 extention_map_t const * const map = gExtentionMap;
2020 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2021 if (!strcmp(procname, map[i].name)) {
2022 return map[i].address;
2023 }
2024 }
2025 return NULL;
2026}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002027
2028EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2029 const EGLint *attrib_list)
2030{
2031 EGLBoolean result = EGL_FALSE;
2032 return result;
2033}
2034
2035EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2036{
2037 EGLBoolean result = EGL_FALSE;
2038 return result;
2039}
2040
2041EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2042 EGLClientBuffer buffer, const EGLint *attrib_list)
2043{
2044 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2045 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2046 }
2047 if (ctx != EGL_NO_CONTEXT) {
2048 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2049 }
2050 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2051 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2052 }
2053
2054 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2055
2056 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2057 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2058
2059 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2060 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002061
2062 switch (native_buffer->format) {
2063 case HAL_PIXEL_FORMAT_RGBA_8888:
2064 case HAL_PIXEL_FORMAT_RGBX_8888:
2065 case HAL_PIXEL_FORMAT_RGB_888:
2066 case HAL_PIXEL_FORMAT_RGB_565:
2067 case HAL_PIXEL_FORMAT_BGRA_8888:
2068 case HAL_PIXEL_FORMAT_RGBA_5551:
2069 case HAL_PIXEL_FORMAT_RGBA_4444:
2070 break;
2071 default:
2072 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2073 }
2074
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002075 native_buffer->common.incRef(&native_buffer->common);
2076 return (EGLImageKHR)native_buffer;
2077}
2078
2079EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2080{
2081 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2082 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2083 }
2084
2085 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2086
2087 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2088 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2089
2090 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2091 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2092
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002093 native_buffer->common.decRef(&native_buffer->common);
2094
2095 return EGL_TRUE;
2096}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002097
2098// ----------------------------------------------------------------------------
2099// ANDROID extensions
2100// ----------------------------------------------------------------------------
2101
2102EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2103 EGLint left, EGLint top, EGLint width, EGLint height)
2104{
2105 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2106 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2107
2108 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002109 if (!d->isValid())
2110 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002111 if (d->dpy != dpy)
2112 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2113
2114 // post the surface
2115 d->setSwapRectangle(left, top, width, height);
2116
2117 return EGL_TRUE;
2118}