blob: 5bbe441b13c8e25f32d5a440dfeb94b7041bd6ea [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);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700161 virtual EGLClientBuffer getRenderBuffer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162protected:
163 GGLSurface depth;
164};
165
166egl_surface_t::egl_surface_t(EGLDisplay dpy,
167 EGLConfig config,
168 int32_t depthFormat)
169 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
170{
171 depth.version = sizeof(GGLSurface);
172 depth.data = 0;
173 depth.format = depthFormat;
174}
175egl_surface_t::~egl_surface_t()
176{
177 magic = 0;
178 free(depth.data);
179}
Mathias Agopian0696a572009-08-20 00:12:56 -0700180bool egl_surface_t::isValid() const {
181 LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
182 return magic == MAGIC;
183}
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185EGLBoolean egl_surface_t::swapBuffers() {
186 return EGL_FALSE;
187}
188EGLint egl_surface_t::getHorizontalResolution() const {
189 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
190}
191EGLint egl_surface_t::getVerticalResolution() const {
192 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
193}
194EGLint egl_surface_t::getRefreshRate() const {
195 return (60 * EGL_DISPLAY_SCALING);
196}
197EGLint egl_surface_t::getSwapBehavior() const {
198 return EGL_BUFFER_PRESERVED;
199}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700200EGLBoolean egl_surface_t::setSwapRectangle(
201 EGLint l, EGLint t, EGLint w, EGLint h)
202{
203 return EGL_FALSE;
204}
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700205EGLClientBuffer egl_surface_t::getRenderBuffer() const {
206 return 0;
207}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209// ----------------------------------------------------------------------------
210
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 EGLDisplay dpy, EGLConfig config,
215 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700216 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217
Mathias Agopian0696a572009-08-20 00:12:56 -0700218 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219
Mathias Agopian0696a572009-08-20 00:12:56 -0700220 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 virtual EGLBoolean swapBuffers();
222 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
223 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700224 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700225 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700226 virtual EGLint getWidth() const { return width; }
227 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 virtual EGLint getHorizontalResolution() const;
229 virtual EGLint getVerticalResolution() const;
230 virtual EGLint getRefreshRate() const;
231 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700232 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700233 virtual EGLClientBuffer getRenderBuffer() const;
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235private:
Mathias Agopiane71212b2009-05-05 00:37:46 -0700236 status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700237 status_t unlock(android_native_buffer_t* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700238 ANativeWindow* nativeWindow;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 android_native_buffer_t* buffer;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700240 android_native_buffer_t* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700241 gralloc_module_t const* module;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700242 copybit_device_t* blitengine;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 int width;
244 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700245 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700246 GGLFormat const* pixelFormatTable;
247
248 struct Rect {
249 inline Rect() { };
250 inline Rect(int32_t w, int32_t h)
251 : left(0), top(0), right(w), bottom(h) { }
252 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
253 : left(l), top(t), right(r), bottom(b) { }
254 Rect& andSelf(const Rect& r) {
255 left = max(left, r.left);
256 top = max(top, r.top);
257 right = min(right, r.right);
258 bottom = min(bottom, r.bottom);
259 return *this;
260 }
261 bool isEmpty() const {
262 return (left>=right || top>=bottom);
263 }
264 void dump(char const* what) {
265 LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
266 what, left, top, right-left, bottom-top);
267 }
268
269 int32_t left;
270 int32_t top;
271 int32_t right;
272 int32_t bottom;
273 };
274
275 struct Region {
276 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700277 typedef Rect const* const_iterator;
278 const_iterator begin() const { return storage; }
279 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700280 static Region subtract(const Rect& lhs, const Rect& rhs) {
281 Region reg;
282 Rect* storage = reg.storage;
283 if (!lhs.isEmpty()) {
284 if (lhs.top < rhs.top) { // top rect
285 storage->left = lhs.left;
286 storage->top = lhs.top;
287 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700288 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700289 storage++;
290 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700291 const int32_t top = max(lhs.top, rhs.top);
292 const int32_t bot = min(lhs.bottom, rhs.bottom);
293 if (top < bot) {
294 if (lhs.left < rhs.left) { // left-side rect
295 storage->left = lhs.left;
296 storage->top = top;
297 storage->right = rhs.left;
298 storage->bottom = bot;
299 storage++;
300 }
301 if (lhs.right > rhs.right) { // right-side rect
302 storage->left = rhs.right;
303 storage->top = top;
304 storage->right = lhs.right;
305 storage->bottom = bot;
306 storage++;
307 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700308 }
309 if (lhs.bottom > rhs.bottom) { // bottom rect
310 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700311 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700312 storage->right = lhs.right;
313 storage->bottom = lhs.bottom;
314 storage++;
315 }
316 reg.count = storage - reg.storage;
317 }
318 return reg;
319 }
320 bool isEmpty() const {
321 return count<=0;
322 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700323 private:
324 Rect storage[4];
325 ssize_t count;
326 };
327
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700328 struct region_iterator : public copybit_region_t {
329 region_iterator(const Region& region)
330 : b(region.begin()), e(region.end()) {
331 this->next = iterate;
332 }
333 private:
334 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
335 region_iterator const* me = static_cast<region_iterator const*>(self);
336 if (me->b != me->e) {
337 *reinterpret_cast<Rect*>(rect) = *me->b++;
338 return 1;
339 }
340 return 0;
341 }
342 mutable Region::const_iterator b;
343 Region::const_iterator const e;
344 };
345
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700346 void copyBlt(
347 android_native_buffer_t* dst, void* dst_vaddr,
348 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700349 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700350
351 Rect dirtyRegion;
352 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353};
354
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700355egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 EGLConfig config,
357 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700358 ANativeWindow* window)
Mathias Agopian0926f502009-05-04 14:17:04 -0700359 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700360 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700361 blitengine(0), bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362{
Mathias Agopian0926f502009-05-04 14:17:04 -0700363 hw_module_t const* pModule;
364 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
365 module = reinterpret_cast<gralloc_module_t const*>(pModule);
366
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700367 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
368 copybit_open(pModule, &blitengine);
369 }
370
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700371 pixelFormatTable = gglGetPixelFormatTable();
372
373 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700374 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700375 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
376 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700377}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700378
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700379egl_window_surface_v2_t::~egl_window_surface_v2_t() {
380 if (buffer) {
381 buffer->common.decRef(&buffer->common);
382 }
383 if (previousBuffer) {
384 previousBuffer->common.decRef(&previousBuffer->common);
385 }
386 nativeWindow->common.decRef(&nativeWindow->common);
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700387 if (blitengine) {
388 copybit_close(blitengine);
389 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700390}
391
Mathias Agopiancf81c842009-07-31 14:47:00 -0700392EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700393{
Mathias Agopian52212712009-08-11 22:34:02 -0700394 // we're intending to do software rendering
395 native_window_set_usage(nativeWindow,
396 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
397
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700398 // dequeue a buffer
Mathias Agopiancf81c842009-07-31 14:47:00 -0700399 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
400 return setError(EGL_BAD_ALLOC, EGL_FALSE);
401 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700402
403 // allocate a corresponding depth-buffer
404 width = buffer->width;
405 height = buffer->height;
406 if (depth.format) {
407 depth.width = width;
408 depth.height = height;
409 depth.stride = depth.width; // use the width here
410 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
411 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700412 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700413 }
414 }
415
416 // keep a reference on the buffer
417 buffer->common.incRef(&buffer->common);
418
Mathias Agopian0926f502009-05-04 14:17:04 -0700419 // Lock the buffer
Mathias Agopiane71212b2009-05-05 00:37:46 -0700420 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700421 // pin the buffer down
422 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
423 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700424 LOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700425 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700426 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700427 // FIXME: we should make sure we're not accessing the buffer anymore
428 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700429 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700430}
431
432void egl_window_surface_v2_t::disconnect()
433{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700434 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700435 bits = NULL;
436 unlock(buffer);
437 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700438 // enqueue the last frame
439 nativeWindow->queueBuffer(nativeWindow, buffer);
440 if (buffer) {
441 buffer->common.decRef(&buffer->common);
442 buffer = 0;
443 }
444 if (previousBuffer) {
445 previousBuffer->common.decRef(&previousBuffer->common);
446 previousBuffer = 0;
447 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448}
449
Mathias Agopian0926f502009-05-04 14:17:04 -0700450status_t egl_window_surface_v2_t::lock(
Mathias Agopiane71212b2009-05-05 00:37:46 -0700451 android_native_buffer_t* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700452{
Mathias Agopianbc492912009-11-03 20:38:08 -0800453 int err;
454 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
455 err = module->lock(module, buf->handle,
456 usage, 0, 0, buf->width, buf->height, vaddr);
457 } else {
458 sw_gralloc_handle_t const* hnd =
459 reinterpret_cast<sw_gralloc_handle_t const*>(buf->handle);
460 *vaddr = (void*)hnd->base;
461 err = NO_ERROR;
462 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700463 return err;
464}
465
466status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
467{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700468 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800469 int err = NO_ERROR;
470 if (sw_gralloc_handle_t::validate(buf->handle) < 0) {
471 err = module->unlock(module, buf->handle);
472 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700473 return err;
474}
475
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700476void egl_window_surface_v2_t::copyBlt(
477 android_native_buffer_t* dst, void* dst_vaddr,
478 android_native_buffer_t* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700479 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700480{
481 // FIXME: use copybit if possible
482 // NOTE: dst and src must be the same format
483
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700484 status_t err = NO_ERROR;
485 copybit_device_t* const copybit = blitengine;
486 if (copybit) {
487 copybit_image_t simg;
488 simg.w = src->width;
489 simg.h = src->height;
490 simg.format = src->format;
491 simg.handle = const_cast<native_handle_t*>(src->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700492
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700493 copybit_image_t dimg;
494 dimg.w = dst->width;
495 dimg.h = dst->height;
496 dimg.format = dst->format;
497 dimg.handle = const_cast<native_handle_t*>(dst->handle);
498
499 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
500 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
501 copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
502 region_iterator it(clip);
503 err = copybit->blit(copybit, &dimg, &simg, &it);
504 if (err != NO_ERROR) {
505 LOGE("copybit failed (%s)", strerror(err));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700506 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700507 }
508
509 if (!copybit || err) {
510 Region::const_iterator cur = clip.begin();
511 Region::const_iterator end = clip.end();
512
513 const size_t bpp = pixelFormatTable[src->format].size;
514 const size_t dbpr = dst->stride * bpp;
515 const size_t sbpr = src->stride * bpp;
516
517 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
518 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
519
520 while (cur != end) {
521 const Rect& r(*cur++);
522 ssize_t w = r.right - r.left;
523 ssize_t h = r.bottom - r.top;
524 if (w <= 0 || h<=0) continue;
525 size_t size = w * bpp;
526 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
527 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
528 if (dbpr==sbpr && size==sbpr) {
529 size *= h;
530 h = 1;
531 }
532 do {
533 memcpy(d, s, size);
534 d += dbpr;
535 s += sbpr;
536 } while (--h > 0);
537 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539}
540
541EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700543 if (!buffer) {
544 return setError(EGL_BAD_ACCESS, EGL_FALSE);
545 }
546
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700547 /*
548 * Handle eglSetSwapRectangleANDROID()
549 * We copyback from the front buffer
550 */
551 if (!dirtyRegion.isEmpty()) {
552 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
553 if (previousBuffer) {
554 const Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
555 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700556 void* prevBits;
557 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700558 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
559 // copy from previousBuffer to buffer
560 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700561 unlock(previousBuffer);
562 }
563 }
564 }
565 oldDirtyRegion = dirtyRegion;
566 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700568 if (previousBuffer) {
569 previousBuffer->common.decRef(&previousBuffer->common);
570 previousBuffer = 0;
571 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572
Mathias Agopian0926f502009-05-04 14:17:04 -0700573 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700574 previousBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700576 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700578 // dequeue a new buffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 nativeWindow->dequeueBuffer(nativeWindow, &buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700580
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581 // TODO: lockBuffer should rather be executed when the very first
582 // direct rendering occurs.
583 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700584
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700585 // reallocate the depth-buffer if needed
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 if ((width != buffer->width) || (height != buffer->height)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 // TODO: we probably should reset the swap rect here
588 // if the window size has changed
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589 width = buffer->width;
590 height = buffer->height;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 if (depth.data) {
592 free(depth.data);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 depth.width = width;
594 depth.height = height;
595 depth.stride = buffer->stride;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
597 if (depth.data == 0) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700598 setError(EGL_BAD_ALLOC, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 return EGL_FALSE;
600 }
601 }
602 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700603
604 // keep a reference on the buffer
605 buffer->common.incRef(&buffer->common);
606
607 // finally pin the buffer down
608 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
609 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
610 LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
611 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700612 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700613 // FIXME: we should make sure we're not accessing the buffer anymore
614 }
615
616 return EGL_TRUE;
617}
618
619EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
620 EGLint l, EGLint t, EGLint w, EGLint h)
621{
622 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 return EGL_TRUE;
624}
625
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700626EGLClientBuffer egl_window_surface_v2_t::getRenderBuffer() const
627{
628 return buffer;
629}
630
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700631EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632{
633 GGLSurface buffer;
634 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635 buffer.width = this->buffer->width;
636 buffer.height = this->buffer->height;
637 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700638 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 gl->rasterizer.procs.colorBuffer(gl, &buffer);
641 if (depth.data != gl->rasterizer.state.buffers.depth.data)
642 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 return EGL_TRUE;
645}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647{
648 GGLSurface buffer;
649 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650 buffer.width = this->buffer->width;
651 buffer.height = this->buffer->height;
652 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700653 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 gl->rasterizer.procs.readBuffer(gl, &buffer);
656 return EGL_TRUE;
657}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
660}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
663}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700664EGLint egl_window_surface_v2_t::getRefreshRate() const {
665 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700667EGLint egl_window_surface_v2_t::getSwapBehavior() const
668{
669 /*
670 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
671 * the content of the swapped buffer.
672 *
673 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
674 *
675 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
676 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
677 * is, everything outside of this area is preserved.
678 *
679 * This implementation of EGL assumes the later case.
680 *
681 */
682
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700683 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684}
685
686// ----------------------------------------------------------------------------
687
688struct egl_pixmap_surface_t : public egl_surface_t
689{
690 egl_pixmap_surface_t(
691 EGLDisplay dpy, EGLConfig config,
692 int32_t depthFormat,
693 egl_native_pixmap_t const * pixmap);
694
695 virtual ~egl_pixmap_surface_t() { }
696
Mathias Agopian0696a572009-08-20 00:12:56 -0700697 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
699 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
700 virtual EGLint getWidth() const { return nativePixmap.width; }
701 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702private:
703 egl_native_pixmap_t nativePixmap;
704};
705
706egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
707 EGLConfig config,
708 int32_t depthFormat,
709 egl_native_pixmap_t const * pixmap)
710 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
711{
712 if (depthFormat) {
713 depth.width = pixmap->width;
714 depth.height = pixmap->height;
715 depth.stride = depth.width; // use the width here
716 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
717 if (depth.data == 0) {
718 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 }
720 }
721}
722EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
723{
724 GGLSurface buffer;
725 buffer.version = sizeof(GGLSurface);
726 buffer.width = nativePixmap.width;
727 buffer.height = nativePixmap.height;
728 buffer.stride = nativePixmap.stride;
729 buffer.data = nativePixmap.data;
730 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700731
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 gl->rasterizer.procs.colorBuffer(gl, &buffer);
733 if (depth.data != gl->rasterizer.state.buffers.depth.data)
734 gl->rasterizer.procs.depthBuffer(gl, &depth);
735 return EGL_TRUE;
736}
737EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
738{
739 GGLSurface buffer;
740 buffer.version = sizeof(GGLSurface);
741 buffer.width = nativePixmap.width;
742 buffer.height = nativePixmap.height;
743 buffer.stride = nativePixmap.stride;
744 buffer.data = nativePixmap.data;
745 buffer.format = nativePixmap.format;
746 gl->rasterizer.procs.readBuffer(gl, &buffer);
747 return EGL_TRUE;
748}
749
750// ----------------------------------------------------------------------------
751
752struct egl_pbuffer_surface_t : public egl_surface_t
753{
754 egl_pbuffer_surface_t(
755 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
756 int32_t w, int32_t h, int32_t f);
757
758 virtual ~egl_pbuffer_surface_t();
759
Mathias Agopian0696a572009-08-20 00:12:56 -0700760 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
762 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
763 virtual EGLint getWidth() const { return pbuffer.width; }
764 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765private:
766 GGLSurface pbuffer;
767};
768
769egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
770 EGLConfig config, int32_t depthFormat,
771 int32_t w, int32_t h, int32_t f)
772 : egl_surface_t(dpy, config, depthFormat)
773{
774 size_t size = w*h;
775 switch (f) {
776 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
777 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
778 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800779 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 default:
781 LOGE("incompatible pixel format for pbuffer (format=%d)", f);
782 pbuffer.data = 0;
783 break;
784 }
785 pbuffer.version = sizeof(GGLSurface);
786 pbuffer.width = w;
787 pbuffer.height = h;
788 pbuffer.stride = w;
789 pbuffer.data = (GGLubyte*)malloc(size);
790 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 if (depthFormat) {
793 depth.width = pbuffer.width;
794 depth.height = pbuffer.height;
795 depth.stride = depth.width; // use the width here
796 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
797 if (depth.data == 0) {
798 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
799 return;
800 }
801 }
802}
803egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
804 free(pbuffer.data);
805}
806EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
807{
808 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
809 if (depth.data != gl->rasterizer.state.buffers.depth.data)
810 gl->rasterizer.procs.depthBuffer(gl, &depth);
811 return EGL_TRUE;
812}
813EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
814{
815 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
816 return EGL_TRUE;
817}
818
819// ----------------------------------------------------------------------------
820
821struct config_pair_t {
822 GLint key;
823 GLint value;
824};
825
826struct configs_t {
827 const config_pair_t* array;
828 int size;
829};
830
831struct config_management_t {
832 GLint key;
833 bool (*match)(GLint reqValue, GLint confValue);
834 static bool atLeast(GLint reqValue, GLint confValue) {
835 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
836 }
837 static bool exact(GLint reqValue, GLint confValue) {
838 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
839 }
840 static bool mask(GLint reqValue, GLint confValue) {
841 return (confValue & reqValue) == reqValue;
842 }
843};
844
845// ----------------------------------------------------------------------------
846
847#define VERSION_MAJOR 1
848#define VERSION_MINOR 2
849static char const * const gVendorString = "Google Inc.";
Mathias Agopian1eeffc62009-11-12 15:19:42 -0800850static char const * const gVersionString = "1.2 Android Driver 1.1.0";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700852static char const * const gExtensionsString =
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700853 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700854 // "KHR_image_pixmap "
855 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700856 "EGL_ANDROID_swap_rectangle "
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700857 "EGL_ANDROID_get_render_buffer "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700858 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859
860// ----------------------------------------------------------------------------
861
862struct extention_map_t {
863 const char * const name;
864 __eglMustCastToProperFunctionPointerType address;
865};
866
867static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700868 { "glDrawTexsOES",
869 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
870 { "glDrawTexiOES",
871 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
872 { "glDrawTexfOES",
873 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
874 { "glDrawTexxOES",
875 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
876 { "glDrawTexsvOES",
877 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
878 { "glDrawTexivOES",
879 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
880 { "glDrawTexfvOES",
881 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
882 { "glDrawTexxvOES",
883 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
884 { "glQueryMatrixxOES",
885 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
886 { "glEGLImageTargetTexture2DOES",
887 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
888 { "glEGLImageTargetRenderbufferStorageOES",
889 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
890 { "glClipPlanef",
891 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
892 { "glClipPlanex",
893 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
894 { "glBindBuffer",
895 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
896 { "glBufferData",
897 (__eglMustCastToProperFunctionPointerType)&glBufferData },
898 { "glBufferSubData",
899 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
900 { "glDeleteBuffers",
901 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
902 { "glGenBuffers",
903 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700904 { "eglCreateImageKHR",
905 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
906 { "eglDestroyImageKHR",
907 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
908 { "eglSetSwapRectangleANDROID",
909 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
910 { "eglGetRenderBufferANDROID",
911 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912};
913
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700914/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 * In the lists below, attributes names MUST be sorted.
916 * Additionally, all configs must be sorted according to
917 * the EGL specification.
918 */
919
920static config_pair_t const config_base_attribute_list[] = {
921 { EGL_STENCIL_SIZE, 0 },
922 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
923 { EGL_LEVEL, 0 },
924 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
927 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
928 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
929 { EGL_NATIVE_VISUAL_ID, 0 },
930 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
931 { EGL_SAMPLES, 0 },
932 { EGL_SAMPLE_BUFFERS, 0 },
933 { EGL_TRANSPARENT_TYPE, EGL_NONE },
934 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
935 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
936 { EGL_TRANSPARENT_RED_VALUE, 0 },
937 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
938 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
939 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700940 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700941 { EGL_LUMINANCE_SIZE, 0 },
942 { EGL_ALPHA_MASK_SIZE, 0 },
943 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700944 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700945 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946};
947
948// These configs can override the base attribute list
949// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
950
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800951// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952static config_pair_t const config_0_attribute_list[] = {
953 { EGL_BUFFER_SIZE, 16 },
954 { EGL_ALPHA_SIZE, 0 },
955 { EGL_BLUE_SIZE, 5 },
956 { EGL_GREEN_SIZE, 6 },
957 { EGL_RED_SIZE, 5 },
958 { EGL_DEPTH_SIZE, 0 },
959 { EGL_CONFIG_ID, 0 },
960 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
961};
962
963static config_pair_t const config_1_attribute_list[] = {
964 { EGL_BUFFER_SIZE, 16 },
965 { EGL_ALPHA_SIZE, 0 },
966 { EGL_BLUE_SIZE, 5 },
967 { EGL_GREEN_SIZE, 6 },
968 { EGL_RED_SIZE, 5 },
969 { EGL_DEPTH_SIZE, 16 },
970 { EGL_CONFIG_ID, 1 },
971 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
972};
973
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800974// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975static config_pair_t const config_2_attribute_list[] = {
976 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800977 { EGL_ALPHA_SIZE, 0 },
978 { EGL_BLUE_SIZE, 8 },
979 { EGL_GREEN_SIZE, 8 },
980 { EGL_RED_SIZE, 8 },
981 { EGL_DEPTH_SIZE, 0 },
982 { EGL_CONFIG_ID, 6 },
983 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
984};
985
986static config_pair_t const config_3_attribute_list[] = {
987 { EGL_BUFFER_SIZE, 32 },
988 { EGL_ALPHA_SIZE, 0 },
989 { EGL_BLUE_SIZE, 8 },
990 { EGL_GREEN_SIZE, 8 },
991 { EGL_RED_SIZE, 8 },
992 { EGL_DEPTH_SIZE, 16 },
993 { EGL_CONFIG_ID, 7 },
994 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
995};
996
997// 8888 configs
998static config_pair_t const config_4_attribute_list[] = {
999 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 { EGL_ALPHA_SIZE, 8 },
1001 { EGL_BLUE_SIZE, 8 },
1002 { EGL_GREEN_SIZE, 8 },
1003 { EGL_RED_SIZE, 8 },
1004 { EGL_DEPTH_SIZE, 0 },
1005 { EGL_CONFIG_ID, 2 },
1006 { 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 },
1017 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1018};
1019
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001020// A8 configs
1021static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 { EGL_BUFFER_SIZE, 8 },
1023 { EGL_ALPHA_SIZE, 8 },
1024 { EGL_BLUE_SIZE, 0 },
1025 { EGL_GREEN_SIZE, 0 },
1026 { EGL_RED_SIZE, 0 },
1027 { EGL_DEPTH_SIZE, 0 },
1028 { EGL_CONFIG_ID, 4 },
1029 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1030};
1031
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001032static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 { EGL_BUFFER_SIZE, 8 },
1034 { EGL_ALPHA_SIZE, 8 },
1035 { EGL_BLUE_SIZE, 0 },
1036 { EGL_GREEN_SIZE, 0 },
1037 { EGL_RED_SIZE, 0 },
1038 { EGL_DEPTH_SIZE, 16 },
1039 { EGL_CONFIG_ID, 5 },
1040 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1041};
1042
1043static configs_t const gConfigs[] = {
1044 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1045 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1046 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1047 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1048 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1049 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001050 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1051 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052};
1053
1054static config_management_t const gConfigManagement[] = {
1055 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1056 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1057 { EGL_BLUE_SIZE, config_management_t::atLeast },
1058 { EGL_GREEN_SIZE, config_management_t::atLeast },
1059 { EGL_RED_SIZE, config_management_t::atLeast },
1060 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1061 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1062 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1063 { EGL_CONFIG_ID, config_management_t::exact },
1064 { EGL_LEVEL, config_management_t::exact },
1065 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::exact },
1066 { EGL_MAX_PBUFFER_PIXELS, config_management_t::exact },
1067 { EGL_MAX_PBUFFER_WIDTH, config_management_t::exact },
1068 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
1069 { EGL_NATIVE_VISUAL_ID, config_management_t::exact },
1070 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1071 { EGL_SAMPLES, config_management_t::exact },
1072 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1073 { EGL_SURFACE_TYPE, config_management_t::mask },
1074 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1075 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1076 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1077 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1078 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1079 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1080 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1081 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001082 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1083 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1084 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1085 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1086 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087};
1088
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001091 // attributes that are not specified are simply ignored, if a particular
1092 // one needs not be ignored, it must be specified here, eg:
1093 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094};
1095
1096// ----------------------------------------------------------------------------
1097
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001098static status_t getConfigFormatInfo(EGLint configID,
1099 int32_t& pixelFormat, int32_t& depthFormat)
1100{
1101 switch(configID) {
1102 case 0:
1103 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1104 depthFormat = 0;
1105 break;
1106 case 1:
1107 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1108 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1109 break;
1110 case 2:
1111 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1112 depthFormat = 0;
1113 break;
1114 case 3:
1115 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1116 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1117 break;
1118 case 4:
1119 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1120 depthFormat = 0;
1121 break;
1122 case 5:
1123 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1124 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1125 break;
1126 case 6:
1127 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1128 depthFormat = 0;
1129 break;
1130 case 7:
1131 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1132 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1133 break;
1134 default:
1135 return NAME_NOT_FOUND;
1136 }
1137 return NO_ERROR;
1138}
1139
1140// ----------------------------------------------------------------------------
1141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142template<typename T>
1143static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1144{
1145 while (first <= last) {
1146 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001147 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001149 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 last = mid - 1;
1151 } else {
1152 return mid;
1153 }
1154 }
1155 return -1;
1156}
1157
1158static int isAttributeMatching(int i, EGLint attr, EGLint val)
1159{
1160 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001161 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 int index = binarySearch<config_pair_t>(
1163 gConfigs[i].array,
1164 0, gConfigs[i].size-1,
1165 attr);
1166 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001167 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 index = binarySearch<config_pair_t>(
1169 config_base_attribute_list,
1170 0, NELEM(config_base_attribute_list)-1,
1171 attr);
1172 }
1173 if (index >= 0) {
1174 // attribute found, check if this config could match
1175 int cfgMgtIndex = binarySearch<config_management_t>(
1176 gConfigManagement,
1177 0, NELEM(gConfigManagement)-1,
1178 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001179 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 bool match = gConfigManagement[cfgMgtIndex].match(
1181 val, configFound[index].value);
1182 if (match) {
1183 // this config matches
1184 return 1;
1185 }
1186 } else {
1187 // attribute not found. this should NEVER happen.
1188 }
1189 } else {
1190 // error, this attribute doesn't exist
1191 }
1192 return 0;
1193}
1194
1195static int makeCurrent(ogles_context_t* gl)
1196{
1197 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1198 if (gl) {
1199 egl_context_t* c = egl_context_t::context(gl);
1200 if (c->flags & egl_context_t::IS_CURRENT) {
1201 if (current != gl) {
1202 // it is an error to set a context current, if it's already
1203 // current to another thread
1204 return -1;
1205 }
1206 } else {
1207 if (current) {
1208 // mark the current context as not current, and flush
1209 glFlush();
1210 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1211 }
1212 }
1213 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1214 // The context is not current, make it current!
1215 setGlThreadSpecific(gl);
1216 c->flags |= egl_context_t::IS_CURRENT;
1217 }
1218 } else {
1219 if (current) {
1220 // mark the current context as not current, and flush
1221 glFlush();
1222 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1223 }
1224 // this thread has no context attached to it
1225 setGlThreadSpecific(0);
1226 }
1227 return 0;
1228}
1229
1230static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1231 EGLint attribute, EGLint *value)
1232{
1233 size_t numConfigs = NELEM(gConfigs);
1234 int index = (int)config;
1235 if (uint32_t(index) >= numConfigs)
1236 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1237
1238 int attrIndex;
1239 attrIndex = binarySearch<config_pair_t>(
1240 gConfigs[index].array,
1241 0, gConfigs[index].size-1,
1242 attribute);
1243 if (attrIndex>=0) {
1244 *value = gConfigs[index].array[attrIndex].value;
1245 return EGL_TRUE;
1246 }
1247
1248 attrIndex = binarySearch<config_pair_t>(
1249 config_base_attribute_list,
1250 0, NELEM(config_base_attribute_list)-1,
1251 attribute);
1252 if (attrIndex>=0) {
1253 *value = config_base_attribute_list[attrIndex].value;
1254 return EGL_TRUE;
1255 }
1256 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1257}
1258
1259static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1260 NativeWindowType window, const EGLint *attrib_list)
1261{
1262 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1263 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1264 if (window == 0)
1265 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1266
1267 EGLint surfaceType;
1268 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1269 return EGL_FALSE;
1270
1271 if (!(surfaceType & EGL_WINDOW_BIT))
1272 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1273
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001274 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001275 ANDROID_NATIVE_WINDOW_MAGIC) {
1276 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1277 }
1278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 EGLint configID;
1280 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1281 return EGL_FALSE;
1282
1283 int32_t depthFormat;
1284 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001285 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1287 }
1288
1289 // FIXME: we don't have access to the pixelFormat here just yet.
1290 // (it's possible that the surface is not fully initialized)
1291 // maybe this should be done after the page-flip
1292 //if (EGLint(info.format) != pixelFormat)
1293 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1294
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001295 egl_surface_t* surface;
1296 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001297 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298
Mathias Agopian0696a572009-08-20 00:12:56 -07001299 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 // there was a problem in the ctor, the error
1301 // flag has been set.
1302 delete surface;
1303 surface = 0;
1304 }
1305 return surface;
1306}
1307
1308static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1309 NativePixmapType pixmap, const EGLint *attrib_list)
1310{
1311 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1312 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1313 if (pixmap == 0)
1314 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1315
1316 EGLint surfaceType;
1317 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1318 return EGL_FALSE;
1319
1320 if (!(surfaceType & EGL_PIXMAP_BIT))
1321 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1322
Mathias Agopian0696a572009-08-20 00:12:56 -07001323 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1324 sizeof(egl_native_pixmap_t)) {
1325 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1326 }
1327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 EGLint configID;
1329 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1330 return EGL_FALSE;
1331
1332 int32_t depthFormat;
1333 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001334 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1336 }
1337
1338 if (pixmap->format != pixelFormat)
1339 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1340
1341 egl_surface_t* surface =
1342 new egl_pixmap_surface_t(dpy, config, depthFormat,
1343 static_cast<egl_native_pixmap_t*>(pixmap));
1344
Mathias Agopian0696a572009-08-20 00:12:56 -07001345 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 // there was a problem in the ctor, the error
1347 // flag has been set.
1348 delete surface;
1349 surface = 0;
1350 }
1351 return surface;
1352}
1353
1354static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1355 const EGLint *attrib_list)
1356{
1357 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1358 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1359
1360 EGLint surfaceType;
1361 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1362 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001363
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 if (!(surfaceType & EGL_PBUFFER_BIT))
1365 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 EGLint configID;
1368 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1369 return EGL_FALSE;
1370
1371 int32_t depthFormat;
1372 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001373 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1375 }
1376
1377 int32_t w = 0;
1378 int32_t h = 0;
1379 while (attrib_list[0]) {
1380 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1381 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1382 attrib_list+=2;
1383 }
1384
1385 egl_surface_t* surface =
1386 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1387
Mathias Agopian0696a572009-08-20 00:12:56 -07001388 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 // there was a problem in the ctor, the error
1390 // flag has been set.
1391 delete surface;
1392 surface = 0;
1393 }
1394 return surface;
1395}
1396
1397// ----------------------------------------------------------------------------
1398}; // namespace android
1399// ----------------------------------------------------------------------------
1400
1401using namespace android;
1402
1403// ----------------------------------------------------------------------------
1404// Initialization
1405// ----------------------------------------------------------------------------
1406
1407EGLDisplay eglGetDisplay(NativeDisplayType display)
1408{
1409#ifndef HAVE_ANDROID_OS
1410 // this just needs to be done once
1411 if (gGLKey == -1) {
1412 pthread_mutex_lock(&gInitMutex);
1413 if (gGLKey == -1)
1414 pthread_key_create(&gGLKey, NULL);
1415 pthread_mutex_unlock(&gInitMutex);
1416 }
1417#endif
1418 if (display == EGL_DEFAULT_DISPLAY) {
1419 EGLDisplay dpy = (EGLDisplay)1;
1420 egl_display_t& d = egl_display_t::get_display(dpy);
1421 d.type = display;
1422 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 return EGL_NO_DISPLAY;
1425}
1426
1427EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1428{
1429 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1430 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001431
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 EGLBoolean res = EGL_TRUE;
1433 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 if (android_atomic_inc(&d.initialized) == 0) {
1436 // initialize stuff here if needed
1437 //pthread_mutex_lock(&gInitMutex);
1438 //pthread_mutex_unlock(&gInitMutex);
1439 }
1440
1441 if (res == EGL_TRUE) {
1442 if (major != NULL) *major = VERSION_MAJOR;
1443 if (minor != NULL) *minor = VERSION_MINOR;
1444 }
1445 return res;
1446}
1447
1448EGLBoolean eglTerminate(EGLDisplay dpy)
1449{
1450 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1451 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1452
1453 EGLBoolean res = EGL_TRUE;
1454 egl_display_t& d = egl_display_t::get_display(dpy);
1455 if (android_atomic_dec(&d.initialized) == 1) {
1456 // TODO: destroy all resources (surfaces, contexts, etc...)
1457 //pthread_mutex_lock(&gInitMutex);
1458 //pthread_mutex_unlock(&gInitMutex);
1459 }
1460 return res;
1461}
1462
1463// ----------------------------------------------------------------------------
1464// configuration
1465// ----------------------------------------------------------------------------
1466
1467EGLBoolean eglGetConfigs( EGLDisplay dpy,
1468 EGLConfig *configs,
1469 EGLint config_size, EGLint *num_config)
1470{
1471 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1472 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1473
1474 GLint numConfigs = NELEM(gConfigs);
1475 if (!configs) {
1476 *num_config = numConfigs;
1477 return EGL_TRUE;
1478 }
1479 GLint i;
1480 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1481 *configs++ = (EGLConfig)i;
1482 }
1483 *num_config = i;
1484 return EGL_TRUE;
1485}
1486
1487EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1488 EGLConfig *configs, EGLint config_size,
1489 EGLint *num_config)
1490{
1491 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1492 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001493
1494 if (ggl_unlikely(num_config==0)) {
1495 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1496 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497
Jack Palevich749c63d2009-03-25 15:12:17 -07001498 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001499 /*
1500 * A NULL attrib_list should be treated as though it was an empty
1501 * one (terminated with EGL_NONE) as defined in
1502 * section 3.4.1 "Querying Configurations" in the EGL specification.
1503 */
1504 static const EGLint dummy = EGL_NONE;
1505 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 int numAttributes = 0;
1509 int numConfigs = NELEM(gConfigs);
1510 uint32_t possibleMatch = (1<<numConfigs)-1;
1511 while(possibleMatch && *attrib_list != EGL_NONE) {
1512 numAttributes++;
1513 EGLint attr = *attrib_list++;
1514 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001515 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 if (!(possibleMatch & (1<<i)))
1517 continue;
1518 if (isAttributeMatching(i, attr, val) == 0) {
1519 possibleMatch &= ~(1<<i);
1520 }
1521 }
1522 }
1523
1524 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001525 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1526 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 // default value
1528 if (binarySearch<config_pair_t>(
1529 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001530 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 config_defaults[j].key) < 0)
1532 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001533 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 if (!(possibleMatch & (1<<i)))
1535 continue;
1536 if (isAttributeMatching(i,
1537 config_defaults[j].key,
1538 config_defaults[j].value) == 0)
1539 {
1540 possibleMatch &= ~(1<<i);
1541 }
1542 }
1543 }
1544 }
1545
1546 // return the configurations found
1547 int n=0;
1548 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001549 if (configs) {
1550 for (int i=0 ; config_size && i<numConfigs ; i++) {
1551 if (possibleMatch & (1<<i)) {
1552 *configs++ = (EGLConfig)i;
1553 config_size--;
1554 n++;
1555 }
1556 }
1557 } else {
1558 for (int i=0 ; i<numConfigs ; i++) {
1559 if (possibleMatch & (1<<i)) {
1560 n++;
1561 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 }
1563 }
1564 }
1565 *num_config = n;
1566 return EGL_TRUE;
1567}
1568
1569EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1570 EGLint attribute, EGLint *value)
1571{
1572 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1573 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1574
1575 return getConfigAttrib(dpy, config, attribute, value);
1576}
1577
1578// ----------------------------------------------------------------------------
1579// surfaces
1580// ----------------------------------------------------------------------------
1581
1582EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1583 NativeWindowType window,
1584 const EGLint *attrib_list)
1585{
1586 return createWindowSurface(dpy, config, window, attrib_list);
1587}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1590 NativePixmapType pixmap,
1591 const EGLint *attrib_list)
1592{
1593 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1594}
1595
1596EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1597 const EGLint *attrib_list)
1598{
1599 return createPbufferSurface(dpy, config, attrib_list);
1600}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001601
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1603{
1604 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1605 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1606 if (eglSurface != EGL_NO_SURFACE) {
1607 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001608 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1610 if (surface->dpy != dpy)
1611 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001612 if (surface->ctx) {
1613 // FIXME: this surface is current check what the spec says
1614 surface->disconnect();
1615 surface->ctx = 0;
1616 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 delete surface;
1618 }
1619 return EGL_TRUE;
1620}
1621
1622EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1623 EGLint attribute, EGLint *value)
1624{
1625 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1626 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1627 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001628 if (!surface->isValid())
1629 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 if (surface->dpy != dpy)
1631 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1632
1633 EGLBoolean ret = EGL_TRUE;
1634 switch (attribute) {
1635 case EGL_CONFIG_ID:
1636 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1637 break;
1638 case EGL_WIDTH:
1639 *value = surface->getWidth();
1640 break;
1641 case EGL_HEIGHT:
1642 *value = surface->getHeight();
1643 break;
1644 case EGL_LARGEST_PBUFFER:
1645 // not modified for a window or pixmap surface
1646 break;
1647 case EGL_TEXTURE_FORMAT:
1648 *value = EGL_NO_TEXTURE;
1649 break;
1650 case EGL_TEXTURE_TARGET:
1651 *value = EGL_NO_TEXTURE;
1652 break;
1653 case EGL_MIPMAP_TEXTURE:
1654 *value = EGL_FALSE;
1655 break;
1656 case EGL_MIPMAP_LEVEL:
1657 *value = 0;
1658 break;
1659 case EGL_RENDER_BUFFER:
1660 // TODO: return the real RENDER_BUFFER here
1661 *value = EGL_BACK_BUFFER;
1662 break;
1663 case EGL_HORIZONTAL_RESOLUTION:
1664 // pixel/mm * EGL_DISPLAY_SCALING
1665 *value = surface->getHorizontalResolution();
1666 break;
1667 case EGL_VERTICAL_RESOLUTION:
1668 // pixel/mm * EGL_DISPLAY_SCALING
1669 *value = surface->getVerticalResolution();
1670 break;
1671 case EGL_PIXEL_ASPECT_RATIO: {
1672 // w/h * EGL_DISPLAY_SCALING
1673 int wr = surface->getHorizontalResolution();
1674 int hr = surface->getVerticalResolution();
1675 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1676 } break;
1677 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001678 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 break;
1680 default:
1681 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1682 }
1683 return ret;
1684}
1685
1686EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1687 EGLContext share_list, const EGLint *attrib_list)
1688{
1689 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1690 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1691
1692 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1693 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1694
1695 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1696 c->flags = egl_context_t::NEVER_CURRENT;
1697 c->dpy = dpy;
1698 c->config = config;
1699 c->read = 0;
1700 c->draw = 0;
1701 return (EGLContext)gl;
1702}
1703
1704EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1705{
1706 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1707 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1708 egl_context_t* c = egl_context_t::context(ctx);
1709 if (c->flags & egl_context_t::IS_CURRENT)
1710 setGlThreadSpecific(0);
1711 ogles_uninit((ogles_context_t*)ctx);
1712 return EGL_TRUE;
1713}
1714
1715EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1716 EGLSurface read, EGLContext ctx)
1717{
1718 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1719 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1720 if (draw) {
1721 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001722 if (!s->isValid())
1723 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 if (s->dpy != dpy)
1725 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001726 // TODO: check that draw is compatible with the context
1727 }
1728 if (read && read!=draw) {
1729 egl_surface_t* s = (egl_surface_t*)read;
1730 if (!s->isValid())
1731 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1732 if (s->dpy != dpy)
1733 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1734 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 }
1736
1737 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1740 return setError(EGL_BAD_MATCH, EGL_FALSE);
1741
1742 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1743 return setError(EGL_BAD_MATCH, EGL_FALSE);
1744
1745 if (ctx == EGL_NO_CONTEXT) {
1746 // if we're detaching, we need the current context
1747 current_ctx = (EGLContext)getGlThreadSpecific();
1748 } else {
1749 egl_context_t* c = egl_context_t::context(ctx);
1750 egl_surface_t* d = (egl_surface_t*)draw;
1751 egl_surface_t* r = (egl_surface_t*)read;
1752 if ((d && d->ctx && d->ctx != ctx) ||
1753 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001754 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1756 }
1757 }
1758
1759 ogles_context_t* gl = (ogles_context_t*)ctx;
1760 if (makeCurrent(gl) == 0) {
1761 if (ctx) {
1762 egl_context_t* c = egl_context_t::context(ctx);
1763 egl_surface_t* d = (egl_surface_t*)draw;
1764 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001765
1766 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001767 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1768 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001769 }
1770 if (c->read) {
1771 // FIXME: unlock/disconnect the read surface too
1772 }
1773
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001775 c->read = read;
1776
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001777 if (c->flags & egl_context_t::NEVER_CURRENT) {
1778 c->flags &= ~egl_context_t::NEVER_CURRENT;
1779 GLint w = 0;
1780 GLint h = 0;
1781 if (draw) {
1782 w = d->getWidth();
1783 h = d->getHeight();
1784 }
1785 ogles_surfaceport(gl, 0, 0);
1786 ogles_viewport(gl, 0, 0, w, h);
1787 ogles_scissor(gl, 0, 0, w, h);
1788 }
1789 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001790 if (d->connect() == EGL_FALSE) {
1791 return EGL_FALSE;
1792 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 d->ctx = ctx;
1794 d->bindDrawSurface(gl);
1795 }
1796 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001797 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798 r->ctx = ctx;
1799 r->bindReadSurface(gl);
1800 }
1801 } else {
1802 // if surfaces were bound to the context bound to this thread
1803 // mark then as unbound.
1804 if (current_ctx) {
1805 egl_context_t* c = egl_context_t::context(current_ctx);
1806 egl_surface_t* d = (egl_surface_t*)c->draw;
1807 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001808 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001809 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001810 d->ctx = EGL_NO_CONTEXT;
1811 d->disconnect();
1812 }
1813 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001814 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001815 r->ctx = EGL_NO_CONTEXT;
1816 // FIXME: unlock/disconnect the read surface too
1817 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 }
1819 }
1820 return EGL_TRUE;
1821 }
1822 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1823}
1824
1825EGLContext eglGetCurrentContext(void)
1826{
1827 // eglGetCurrentContext returns the current EGL rendering context,
1828 // as specified by eglMakeCurrent. If no context is current,
1829 // EGL_NO_CONTEXT is returned.
1830 return (EGLContext)getGlThreadSpecific();
1831}
1832
1833EGLSurface eglGetCurrentSurface(EGLint readdraw)
1834{
1835 // eglGetCurrentSurface returns the read or draw surface attached
1836 // to the current EGL rendering context, as specified by eglMakeCurrent.
1837 // If no context is current, EGL_NO_SURFACE is returned.
1838 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1839 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1840 egl_context_t* c = egl_context_t::context(ctx);
1841 if (readdraw == EGL_READ) {
1842 return c->read;
1843 } else if (readdraw == EGL_DRAW) {
1844 return c->draw;
1845 }
1846 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1847}
1848
1849EGLDisplay eglGetCurrentDisplay(void)
1850{
1851 // eglGetCurrentDisplay returns the current EGL display connection
1852 // for the current EGL rendering context, as specified by eglMakeCurrent.
1853 // If no context is current, EGL_NO_DISPLAY is returned.
1854 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1855 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1856 egl_context_t* c = egl_context_t::context(ctx);
1857 return c->dpy;
1858}
1859
1860EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1861 EGLint attribute, EGLint *value)
1862{
1863 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1864 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1865 egl_context_t* c = egl_context_t::context(ctx);
1866 switch (attribute) {
1867 case EGL_CONFIG_ID:
1868 // Returns the ID of the EGL frame buffer configuration with
1869 // respect to which the context was created
1870 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1871 }
1872 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1873}
1874
1875EGLBoolean eglWaitGL(void)
1876{
1877 return EGL_TRUE;
1878}
1879
1880EGLBoolean eglWaitNative(EGLint engine)
1881{
1882 return EGL_TRUE;
1883}
1884
1885EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1886{
1887 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1888 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001889
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001891 if (!d->isValid())
1892 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 if (d->dpy != dpy)
1894 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1895
1896 // post the surface
1897 d->swapBuffers();
1898
1899 // if it's bound to a context, update the buffer
1900 if (d->ctx != EGL_NO_CONTEXT) {
1901 d->bindDrawSurface((ogles_context_t*)d->ctx);
1902 // if this surface is also the read surface of the context
1903 // it is bound to, make sure to update the read buffer as well.
1904 // The EGL spec is a little unclear about this.
1905 egl_context_t* c = egl_context_t::context(d->ctx);
1906 if (c->read == draw) {
1907 d->bindReadSurface((ogles_context_t*)d->ctx);
1908 }
1909 }
1910
1911 return EGL_TRUE;
1912}
1913
1914EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1915 NativePixmapType target)
1916{
1917 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1918 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1919 // TODO: eglCopyBuffers()
1920 return EGL_FALSE;
1921}
1922
1923EGLint eglGetError(void)
1924{
1925 return getError();
1926}
1927
1928const char* eglQueryString(EGLDisplay dpy, EGLint name)
1929{
1930 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1931 return setError(EGL_BAD_DISPLAY, (const char*)0);
1932
1933 switch (name) {
1934 case EGL_VENDOR:
1935 return gVendorString;
1936 case EGL_VERSION:
1937 return gVersionString;
1938 case EGL_EXTENSIONS:
1939 return gExtensionsString;
1940 case EGL_CLIENT_APIS:
1941 return gClientApiString;
1942 }
1943 return setError(EGL_BAD_PARAMETER, (const char *)0);
1944}
1945
1946// ----------------------------------------------------------------------------
1947// EGL 1.1
1948// ----------------------------------------------------------------------------
1949
1950EGLBoolean eglSurfaceAttrib(
1951 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1952{
1953 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1954 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1955 // TODO: eglSurfaceAttrib()
1956 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1957}
1958
1959EGLBoolean eglBindTexImage(
1960 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1961{
1962 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1963 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1964 // TODO: eglBindTexImage()
1965 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1966}
1967
1968EGLBoolean eglReleaseTexImage(
1969 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1970{
1971 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1972 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1973 // TODO: eglReleaseTexImage()
1974 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1975}
1976
1977EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1978{
1979 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1980 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1981 // TODO: eglSwapInterval()
1982 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1983}
1984
1985// ----------------------------------------------------------------------------
1986// EGL 1.2
1987// ----------------------------------------------------------------------------
1988
1989EGLBoolean eglBindAPI(EGLenum api)
1990{
1991 if (api != EGL_OPENGL_ES_API)
1992 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1993 return EGL_TRUE;
1994}
1995
1996EGLenum eglQueryAPI(void)
1997{
1998 return EGL_OPENGL_ES_API;
1999}
2000
2001EGLBoolean eglWaitClient(void)
2002{
2003 glFinish();
2004 return EGL_TRUE;
2005}
2006
2007EGLBoolean eglReleaseThread(void)
2008{
2009 // TODO: eglReleaseThread()
2010 return EGL_TRUE;
2011}
2012
2013EGLSurface eglCreatePbufferFromClientBuffer(
2014 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
2015 EGLConfig config, const EGLint *attrib_list)
2016{
2017 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2018 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2019 // TODO: eglCreatePbufferFromClientBuffer()
2020 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2021}
2022
2023// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002024// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025// ----------------------------------------------------------------------------
2026
2027void (*eglGetProcAddress (const char *procname))()
2028{
2029 extention_map_t const * const map = gExtentionMap;
2030 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2031 if (!strcmp(procname, map[i].name)) {
2032 return map[i].address;
2033 }
2034 }
2035 return NULL;
2036}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002037
2038EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2039 const EGLint *attrib_list)
2040{
2041 EGLBoolean result = EGL_FALSE;
2042 return result;
2043}
2044
2045EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2046{
2047 EGLBoolean result = EGL_FALSE;
2048 return result;
2049}
2050
2051EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2052 EGLClientBuffer buffer, const EGLint *attrib_list)
2053{
2054 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2055 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2056 }
2057 if (ctx != EGL_NO_CONTEXT) {
2058 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2059 }
2060 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2061 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2062 }
2063
2064 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
2065
2066 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2067 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2068
2069 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2070 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002071
2072 switch (native_buffer->format) {
2073 case HAL_PIXEL_FORMAT_RGBA_8888:
2074 case HAL_PIXEL_FORMAT_RGBX_8888:
2075 case HAL_PIXEL_FORMAT_RGB_888:
2076 case HAL_PIXEL_FORMAT_RGB_565:
2077 case HAL_PIXEL_FORMAT_BGRA_8888:
2078 case HAL_PIXEL_FORMAT_RGBA_5551:
2079 case HAL_PIXEL_FORMAT_RGBA_4444:
2080 break;
2081 default:
2082 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2083 }
2084
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002085 native_buffer->common.incRef(&native_buffer->common);
2086 return (EGLImageKHR)native_buffer;
2087}
2088
2089EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2090{
2091 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2092 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2093 }
2094
2095 android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
2096
2097 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2098 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2099
2100 if (native_buffer->common.version != sizeof(android_native_buffer_t))
2101 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2102
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002103 native_buffer->common.decRef(&native_buffer->common);
2104
2105 return EGL_TRUE;
2106}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002107
2108// ----------------------------------------------------------------------------
2109// ANDROID extensions
2110// ----------------------------------------------------------------------------
2111
2112EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2113 EGLint left, EGLint top, EGLint width, EGLint height)
2114{
2115 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2116 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2117
2118 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002119 if (!d->isValid())
2120 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002121 if (d->dpy != dpy)
2122 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2123
2124 // post the surface
2125 d->setSwapRectangle(left, top, width, height);
2126
2127 return EGL_TRUE;
2128}
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07002129
2130EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
2131{
2132 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2133 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2134
2135 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002136 if (!d->isValid())
2137 return setError(EGL_BAD_SURFACE, (EGLClientBuffer)0);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07002138 if (d->dpy != dpy)
2139 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2140
2141 // post the surface
2142 return d->getRenderBuffer();
2143}