blob: 232c84ffac36679ab36b9b6678fdee39a372e347 [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>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080033#include <ui/ANativeObjectBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35#include <EGL/egl.h>
36#include <EGL/eglext.h>
37#include <GLES/gl.h>
38#include <GLES/glext.h>
39
40#include <pixelflinger/format.h>
41#include <pixelflinger/pixelflinger.h>
42
43#include "context.h"
44#include "state.h"
45#include "texture.h"
46#include "matrix.h"
47
48#undef NELEM
49#define NELEM(x) (sizeof(x)/sizeof(*(x)))
50
Mathias Agopian5f2165f2012-02-24 18:25:41 -080051// ----------------------------------------------------------------------------
Mathias Agopian4b9511c2011-11-13 23:52:47 -080052
53EGLBoolean EGLAPI eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
54 EGLint left, EGLint top, EGLint width, EGLint height);
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056// ----------------------------------------------------------------------------
57namespace android {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059// ----------------------------------------------------------------------------
60
61const unsigned int NUM_DISPLAYS = 1;
62
63static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
64static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
65static pthread_key_t gEGLErrorKey = -1;
66#ifndef HAVE_ANDROID_OS
67namespace gl {
68pthread_key_t gGLKey = -1;
69}; // namespace gl
70#endif
71
72template<typename T>
73static T setError(GLint error, T returnValue) {
74 if (ggl_unlikely(gEGLErrorKey == -1)) {
75 pthread_mutex_lock(&gErrorKeyMutex);
76 if (gEGLErrorKey == -1)
77 pthread_key_create(&gEGLErrorKey, NULL);
78 pthread_mutex_unlock(&gErrorKeyMutex);
79 }
80 pthread_setspecific(gEGLErrorKey, (void*)error);
81 return returnValue;
82}
83
84static GLint getError() {
85 if (ggl_unlikely(gEGLErrorKey == -1))
86 return EGL_SUCCESS;
87 GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -080088 if (error == 0) {
89 // The TLS key has been created by another thread, but the value for
90 // this thread has not been initialized.
91 return EGL_SUCCESS;
92 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
94 return error;
95}
96
97// ----------------------------------------------------------------------------
98
99struct egl_display_t
100{
101 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 static EGLBoolean is_valid(EGLDisplay dpy) {
106 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
107 }
108
109 NativeDisplayType type;
110 volatile int32_t initialized;
111};
112
113static egl_display_t gDisplays[NUM_DISPLAYS];
114
115egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
116 return gDisplays[uintptr_t(dpy)-1U];
117}
118
119struct egl_context_t {
120 enum {
121 IS_CURRENT = 0x00010000,
122 NEVER_CURRENT = 0x00020000
123 };
124 uint32_t flags;
125 EGLDisplay dpy;
126 EGLConfig config;
127 EGLSurface read;
128 EGLSurface draw;
129
130 static inline egl_context_t* context(EGLContext ctx) {
131 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
132 return static_cast<egl_context_t*>(gl->rasterizer.base);
133 }
134};
135
136// ----------------------------------------------------------------------------
137
138struct egl_surface_t
139{
140 enum {
141 PAGE_FLIP = 0x00000001,
142 MAGIC = 0x31415265
143 };
144
145 uint32_t magic;
146 EGLDisplay dpy;
147 EGLConfig config;
148 EGLContext ctx;
149
150 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
151 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700152 bool isValid() const;
153 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
156 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700157 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700158 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 virtual EGLint getWidth() const = 0;
160 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
162 virtual EGLint getHorizontalResolution() const;
163 virtual EGLint getVerticalResolution() const;
164 virtual EGLint getRefreshRate() const;
165 virtual EGLint getSwapBehavior() const;
166 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700167 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700168 virtual EGLClientBuffer getRenderBuffer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169protected:
170 GGLSurface depth;
171};
172
173egl_surface_t::egl_surface_t(EGLDisplay dpy,
174 EGLConfig config,
175 int32_t depthFormat)
176 : magic(MAGIC), dpy(dpy), config(config), ctx(0)
177{
178 depth.version = sizeof(GGLSurface);
179 depth.data = 0;
180 depth.format = depthFormat;
181}
182egl_surface_t::~egl_surface_t()
183{
184 magic = 0;
185 free(depth.data);
186}
Mathias Agopian0696a572009-08-20 00:12:56 -0700187bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000188 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700189 return magic == MAGIC;
190}
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192EGLBoolean egl_surface_t::swapBuffers() {
193 return EGL_FALSE;
194}
195EGLint egl_surface_t::getHorizontalResolution() const {
196 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
197}
198EGLint egl_surface_t::getVerticalResolution() const {
199 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
200}
201EGLint egl_surface_t::getRefreshRate() const {
202 return (60 * EGL_DISPLAY_SCALING);
203}
204EGLint egl_surface_t::getSwapBehavior() const {
205 return EGL_BUFFER_PRESERVED;
206}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700207EGLBoolean egl_surface_t::setSwapRectangle(
208 EGLint l, EGLint t, EGLint w, EGLint h)
209{
210 return EGL_FALSE;
211}
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700212EGLClientBuffer egl_surface_t::getRenderBuffer() const {
213 return 0;
214}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215
216// ----------------------------------------------------------------------------
217
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 EGLDisplay dpy, EGLConfig config,
222 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700223 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224
Mathias Agopian0696a572009-08-20 00:12:56 -0700225 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
Mathias Agopian0696a572009-08-20 00:12:56 -0700227 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 virtual EGLBoolean swapBuffers();
229 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
230 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700231 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700232 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700233 virtual EGLint getWidth() const { return width; }
234 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 virtual EGLint getHorizontalResolution() const;
236 virtual EGLint getVerticalResolution() const;
237 virtual EGLint getRefreshRate() const;
238 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700239 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700240 virtual EGLClientBuffer getRenderBuffer() const;
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700243 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
244 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700245 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700246 ANativeWindowBuffer* buffer;
247 ANativeWindowBuffer* previousBuffer;
Mathias Agopian0926f502009-05-04 14:17:04 -0700248 gralloc_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 int width;
250 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700251 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700252 GGLFormat const* pixelFormatTable;
253
254 struct Rect {
255 inline Rect() { };
256 inline Rect(int32_t w, int32_t h)
257 : left(0), top(0), right(w), bottom(h) { }
258 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
259 : left(l), top(t), right(r), bottom(b) { }
260 Rect& andSelf(const Rect& r) {
261 left = max(left, r.left);
262 top = max(top, r.top);
263 right = min(right, r.right);
264 bottom = min(bottom, r.bottom);
265 return *this;
266 }
267 bool isEmpty() const {
268 return (left>=right || top>=bottom);
269 }
270 void dump(char const* what) {
Steve Block9d453682011-12-20 16:23:08 +0000271 ALOGD("%s { %5d, %5d, w=%5d, h=%5d }",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700272 what, left, top, right-left, bottom-top);
273 }
274
275 int32_t left;
276 int32_t top;
277 int32_t right;
278 int32_t bottom;
279 };
280
281 struct Region {
282 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700283 typedef Rect const* const_iterator;
284 const_iterator begin() const { return storage; }
285 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700286 static Region subtract(const Rect& lhs, const Rect& rhs) {
287 Region reg;
288 Rect* storage = reg.storage;
289 if (!lhs.isEmpty()) {
290 if (lhs.top < rhs.top) { // top rect
291 storage->left = lhs.left;
292 storage->top = lhs.top;
293 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700294 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700295 storage++;
296 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700297 const int32_t top = max(lhs.top, rhs.top);
298 const int32_t bot = min(lhs.bottom, rhs.bottom);
299 if (top < bot) {
300 if (lhs.left < rhs.left) { // left-side rect
301 storage->left = lhs.left;
302 storage->top = top;
303 storage->right = rhs.left;
304 storage->bottom = bot;
305 storage++;
306 }
307 if (lhs.right > rhs.right) { // right-side rect
308 storage->left = rhs.right;
309 storage->top = top;
310 storage->right = lhs.right;
311 storage->bottom = bot;
312 storage++;
313 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700314 }
315 if (lhs.bottom > rhs.bottom) { // bottom rect
316 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700317 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700318 storage->right = lhs.right;
319 storage->bottom = lhs.bottom;
320 storage++;
321 }
322 reg.count = storage - reg.storage;
323 }
324 return reg;
325 }
326 bool isEmpty() const {
327 return count<=0;
328 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700329 private:
330 Rect storage[4];
331 ssize_t count;
332 };
333
334 void copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700335 ANativeWindowBuffer* dst, void* dst_vaddr,
336 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700337 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700338
339 Rect dirtyRegion;
340 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341};
342
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700343egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 EGLConfig config,
345 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700346 ANativeWindow* window)
Mathias Agopian0926f502009-05-04 14:17:04 -0700347 : egl_surface_t(dpy, config, depthFormat),
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700348 nativeWindow(window), buffer(0), previousBuffer(0), module(0),
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700349 bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700351 hw_module_t const* pModule;
352 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
353 module = reinterpret_cast<gralloc_module_t const*>(pModule);
354
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700355 pixelFormatTable = gglGetPixelFormatTable();
356
357 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700358 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700359 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
360 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700361}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700362
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700363egl_window_surface_v2_t::~egl_window_surface_v2_t() {
364 if (buffer) {
365 buffer->common.decRef(&buffer->common);
366 }
367 if (previousBuffer) {
368 previousBuffer->common.decRef(&previousBuffer->common);
369 }
370 nativeWindow->common.decRef(&nativeWindow->common);
371}
372
Mathias Agopiancf81c842009-07-31 14:47:00 -0700373EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700374{
Mathias Agopian52212712009-08-11 22:34:02 -0700375 // we're intending to do software rendering
376 native_window_set_usage(nativeWindow,
377 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
378
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700379 // dequeue a buffer
Mathias Agopiancf81c842009-07-31 14:47:00 -0700380 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
381 return setError(EGL_BAD_ALLOC, EGL_FALSE);
382 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700383
384 // allocate a corresponding depth-buffer
385 width = buffer->width;
386 height = buffer->height;
387 if (depth.format) {
388 depth.width = width;
389 depth.height = height;
390 depth.stride = depth.width; // use the width here
391 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
392 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700393 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700394 }
395 }
396
397 // keep a reference on the buffer
398 buffer->common.incRef(&buffer->common);
399
Mathias Agopian0926f502009-05-04 14:17:04 -0700400 // Lock the buffer
Mathias Agopiane71212b2009-05-05 00:37:46 -0700401 nativeWindow->lockBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700402 // pin the buffer down
403 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
404 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000405 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700406 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700407 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700408 // FIXME: we should make sure we're not accessing the buffer anymore
409 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700410 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700411}
412
413void egl_window_surface_v2_t::disconnect()
414{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700415 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700416 bits = NULL;
417 unlock(buffer);
418 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700419 // enqueue the last frame
420 nativeWindow->queueBuffer(nativeWindow, buffer);
421 if (buffer) {
422 buffer->common.decRef(&buffer->common);
423 buffer = 0;
424 }
425 if (previousBuffer) {
426 previousBuffer->common.decRef(&previousBuffer->common);
427 previousBuffer = 0;
428 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
Mathias Agopian0926f502009-05-04 14:17:04 -0700431status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700432 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700433{
Mathias Agopianbc492912009-11-03 20:38:08 -0800434 int err;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800435
436 err = module->lock(module, buf->handle,
437 usage, 0, 0, buf->width, buf->height, vaddr);
438
Mathias Agopian0926f502009-05-04 14:17:04 -0700439 return err;
440}
441
Iliyan Malchev697526b2011-05-01 11:33:26 -0700442status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700443{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700444 if (!buf) return BAD_VALUE;
Mathias Agopianbc492912009-11-03 20:38:08 -0800445 int err = NO_ERROR;
Mathias Agopian695b66f2010-12-08 17:44:07 -0800446
447 err = module->unlock(module, buf->handle);
448
Mathias Agopian0926f502009-05-04 14:17:04 -0700449 return err;
450}
451
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700452void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700453 ANativeWindowBuffer* dst, void* dst_vaddr,
454 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700455 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700456{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700457 // NOTE: dst and src must be the same format
458
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700459 Region::const_iterator cur = clip.begin();
460 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700461
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700462 const size_t bpp = pixelFormatTable[src->format].size;
463 const size_t dbpr = dst->stride * bpp;
464 const size_t sbpr = src->stride * bpp;
465
466 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
467 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
468
469 while (cur != end) {
470 const Rect& r(*cur++);
471 ssize_t w = r.right - r.left;
472 ssize_t h = r.bottom - r.top;
473 if (w <= 0 || h<=0) continue;
474 size_t size = w * bpp;
475 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
476 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
477 if (dbpr==sbpr && size==sbpr) {
478 size *= h;
479 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700480 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700481 do {
482 memcpy(d, s, size);
483 d += dbpr;
484 s += sbpr;
485 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700486 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700487}
488
489EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700491 if (!buffer) {
492 return setError(EGL_BAD_ACCESS, EGL_FALSE);
493 }
494
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700495 /*
496 * Handle eglSetSwapRectangleANDROID()
497 * We copyback from the front buffer
498 */
499 if (!dirtyRegion.isEmpty()) {
500 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
501 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100502 // This was const Region copyBack, but that causes an
503 // internal compile error on simulator builds
504 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700505 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700506 void* prevBits;
507 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700508 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
509 // copy from previousBuffer to buffer
510 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700511 unlock(previousBuffer);
512 }
513 }
514 }
515 oldDirtyRegion = dirtyRegion;
516 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700518 if (previousBuffer) {
519 previousBuffer->common.decRef(&previousBuffer->common);
520 previousBuffer = 0;
521 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700522
Mathias Agopian0926f502009-05-04 14:17:04 -0700523 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700524 previousBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525 nativeWindow->queueBuffer(nativeWindow, buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700526 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700528 // dequeue a new buffer
Mathias Agopian031213e2010-08-18 16:07:34 -0700529 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
530
531 // TODO: lockBuffer should rather be executed when the very first
532 // direct rendering occurs.
533 nativeWindow->lockBuffer(nativeWindow, buffer);
534
535 // reallocate the depth-buffer if needed
536 if ((width != buffer->width) || (height != buffer->height)) {
537 // TODO: we probably should reset the swap rect here
538 // if the window size has changed
539 width = buffer->width;
540 height = buffer->height;
541 if (depth.data) {
542 free(depth.data);
543 depth.width = width;
544 depth.height = height;
545 depth.stride = buffer->stride;
546 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
547 if (depth.data == 0) {
548 setError(EGL_BAD_ALLOC, EGL_FALSE);
549 return EGL_FALSE;
550 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
552 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700553
Mathias Agopian031213e2010-08-18 16:07:34 -0700554 // keep a reference on the buffer
555 buffer->common.incRef(&buffer->common);
556
557 // finally pin the buffer down
558 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
559 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000560 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700561 buffer, buffer->width, buffer->height);
562 return setError(EGL_BAD_ACCESS, EGL_FALSE);
563 // FIXME: we should make sure we're not accessing the buffer anymore
564 }
565 } else {
566 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700567 }
568
569 return EGL_TRUE;
570}
571
572EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
573 EGLint l, EGLint t, EGLint w, EGLint h)
574{
575 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 return EGL_TRUE;
577}
578
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700579EGLClientBuffer egl_window_surface_v2_t::getRenderBuffer() const
580{
581 return buffer;
582}
583
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700584EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585{
586 GGLSurface buffer;
587 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 buffer.width = this->buffer->width;
589 buffer.height = this->buffer->height;
590 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700591 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700592 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 gl->rasterizer.procs.colorBuffer(gl, &buffer);
594 if (depth.data != gl->rasterizer.state.buffers.depth.data)
595 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 return EGL_TRUE;
598}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600{
601 GGLSurface buffer;
602 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 buffer.width = this->buffer->width;
604 buffer.height = this->buffer->height;
605 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700606 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 gl->rasterizer.procs.readBuffer(gl, &buffer);
609 return EGL_TRUE;
610}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
613}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617EGLint egl_window_surface_v2_t::getRefreshRate() const {
618 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700620EGLint egl_window_surface_v2_t::getSwapBehavior() const
621{
622 /*
623 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
624 * the content of the swapped buffer.
625 *
626 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
627 *
628 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
629 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
630 * is, everything outside of this area is preserved.
631 *
632 * This implementation of EGL assumes the later case.
633 *
634 */
635
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637}
638
639// ----------------------------------------------------------------------------
640
641struct egl_pixmap_surface_t : public egl_surface_t
642{
643 egl_pixmap_surface_t(
644 EGLDisplay dpy, EGLConfig config,
645 int32_t depthFormat,
646 egl_native_pixmap_t const * pixmap);
647
648 virtual ~egl_pixmap_surface_t() { }
649
Mathias Agopian0696a572009-08-20 00:12:56 -0700650 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
652 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
653 virtual EGLint getWidth() const { return nativePixmap.width; }
654 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655private:
656 egl_native_pixmap_t nativePixmap;
657};
658
659egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
660 EGLConfig config,
661 int32_t depthFormat,
662 egl_native_pixmap_t const * pixmap)
663 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
664{
665 if (depthFormat) {
666 depth.width = pixmap->width;
667 depth.height = pixmap->height;
668 depth.stride = depth.width; // use the width here
669 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
670 if (depth.data == 0) {
671 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673 }
674}
675EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
676{
677 GGLSurface buffer;
678 buffer.version = sizeof(GGLSurface);
679 buffer.width = nativePixmap.width;
680 buffer.height = nativePixmap.height;
681 buffer.stride = nativePixmap.stride;
682 buffer.data = nativePixmap.data;
683 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 gl->rasterizer.procs.colorBuffer(gl, &buffer);
686 if (depth.data != gl->rasterizer.state.buffers.depth.data)
687 gl->rasterizer.procs.depthBuffer(gl, &depth);
688 return EGL_TRUE;
689}
690EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
691{
692 GGLSurface buffer;
693 buffer.version = sizeof(GGLSurface);
694 buffer.width = nativePixmap.width;
695 buffer.height = nativePixmap.height;
696 buffer.stride = nativePixmap.stride;
697 buffer.data = nativePixmap.data;
698 buffer.format = nativePixmap.format;
699 gl->rasterizer.procs.readBuffer(gl, &buffer);
700 return EGL_TRUE;
701}
702
703// ----------------------------------------------------------------------------
704
705struct egl_pbuffer_surface_t : public egl_surface_t
706{
707 egl_pbuffer_surface_t(
708 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
709 int32_t w, int32_t h, int32_t f);
710
711 virtual ~egl_pbuffer_surface_t();
712
Mathias Agopian0696a572009-08-20 00:12:56 -0700713 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
715 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
716 virtual EGLint getWidth() const { return pbuffer.width; }
717 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718private:
719 GGLSurface pbuffer;
720};
721
722egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
723 EGLConfig config, int32_t depthFormat,
724 int32_t w, int32_t h, int32_t f)
725 : egl_surface_t(dpy, config, depthFormat)
726{
727 size_t size = w*h;
728 switch (f) {
729 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
730 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
731 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800732 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000734 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 pbuffer.data = 0;
736 break;
737 }
738 pbuffer.version = sizeof(GGLSurface);
739 pbuffer.width = w;
740 pbuffer.height = h;
741 pbuffer.stride = w;
742 pbuffer.data = (GGLubyte*)malloc(size);
743 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 if (depthFormat) {
746 depth.width = pbuffer.width;
747 depth.height = pbuffer.height;
748 depth.stride = depth.width; // use the width here
749 depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2);
750 if (depth.data == 0) {
751 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
752 return;
753 }
754 }
755}
756egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
757 free(pbuffer.data);
758}
759EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
760{
761 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
762 if (depth.data != gl->rasterizer.state.buffers.depth.data)
763 gl->rasterizer.procs.depthBuffer(gl, &depth);
764 return EGL_TRUE;
765}
766EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
767{
768 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
769 return EGL_TRUE;
770}
771
772// ----------------------------------------------------------------------------
773
774struct config_pair_t {
775 GLint key;
776 GLint value;
777};
778
779struct configs_t {
780 const config_pair_t* array;
781 int size;
782};
783
784struct config_management_t {
785 GLint key;
786 bool (*match)(GLint reqValue, GLint confValue);
787 static bool atLeast(GLint reqValue, GLint confValue) {
788 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
789 }
790 static bool exact(GLint reqValue, GLint confValue) {
791 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
792 }
793 static bool mask(GLint reqValue, GLint confValue) {
794 return (confValue & reqValue) == reqValue;
795 }
Mathias Agopian63971672010-10-25 15:51:24 -0700796 static bool ignore(GLint reqValue, GLint confValue) {
797 return true;
798 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799};
800
801// ----------------------------------------------------------------------------
802
803#define VERSION_MAJOR 1
804#define VERSION_MINOR 2
805static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700806static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700807static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700809 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700810 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 // "KHR_image_pixmap "
812 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700813 "EGL_ANDROID_swap_rectangle "
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700814 "EGL_ANDROID_get_render_buffer "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700815 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816
817// ----------------------------------------------------------------------------
818
819struct extention_map_t {
820 const char * const name;
821 __eglMustCastToProperFunctionPointerType address;
822};
823
824static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700825 { "glDrawTexsOES",
826 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
827 { "glDrawTexiOES",
828 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
829 { "glDrawTexfOES",
830 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
831 { "glDrawTexxOES",
832 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
833 { "glDrawTexsvOES",
834 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
835 { "glDrawTexivOES",
836 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
837 { "glDrawTexfvOES",
838 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
839 { "glDrawTexxvOES",
840 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
841 { "glQueryMatrixxOES",
842 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
843 { "glEGLImageTargetTexture2DOES",
844 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
845 { "glEGLImageTargetRenderbufferStorageOES",
846 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
847 { "glClipPlanef",
848 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
849 { "glClipPlanex",
850 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
851 { "glBindBuffer",
852 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
853 { "glBufferData",
854 (__eglMustCastToProperFunctionPointerType)&glBufferData },
855 { "glBufferSubData",
856 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
857 { "glDeleteBuffers",
858 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
859 { "glGenBuffers",
860 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700861 { "eglCreateImageKHR",
862 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
863 { "eglDestroyImageKHR",
864 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700865 { "eglCreateSyncKHR",
866 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
867 { "eglDestroySyncKHR",
868 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
869 { "eglClientWaitSyncKHR",
870 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
871 { "eglGetSyncAttribKHR",
872 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700873 { "eglSetSwapRectangleANDROID",
874 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -0700875 { "eglGetRenderBufferANDROID",
876 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877};
878
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700879/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 * In the lists below, attributes names MUST be sorted.
881 * Additionally, all configs must be sorted according to
882 * the EGL specification.
883 */
884
885static config_pair_t const config_base_attribute_list[] = {
886 { EGL_STENCIL_SIZE, 0 },
887 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
888 { EGL_LEVEL, 0 },
889 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700890 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
892 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
893 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
894 { EGL_NATIVE_VISUAL_ID, 0 },
895 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
896 { EGL_SAMPLES, 0 },
897 { EGL_SAMPLE_BUFFERS, 0 },
898 { EGL_TRANSPARENT_TYPE, EGL_NONE },
899 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
900 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
901 { EGL_TRANSPARENT_RED_VALUE, 0 },
902 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
903 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
904 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700905 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700906 { EGL_LUMINANCE_SIZE, 0 },
907 { EGL_ALPHA_MASK_SIZE, 0 },
908 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700909 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700910 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911};
912
913// These configs can override the base attribute list
914// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
915
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800916// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917static config_pair_t const config_0_attribute_list[] = {
918 { EGL_BUFFER_SIZE, 16 },
919 { EGL_ALPHA_SIZE, 0 },
920 { EGL_BLUE_SIZE, 5 },
921 { EGL_GREEN_SIZE, 6 },
922 { EGL_RED_SIZE, 5 },
923 { EGL_DEPTH_SIZE, 0 },
924 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700925 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
927};
928
929static config_pair_t const config_1_attribute_list[] = {
930 { EGL_BUFFER_SIZE, 16 },
931 { EGL_ALPHA_SIZE, 0 },
932 { EGL_BLUE_SIZE, 5 },
933 { EGL_GREEN_SIZE, 6 },
934 { EGL_RED_SIZE, 5 },
935 { EGL_DEPTH_SIZE, 16 },
936 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700937 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
939};
940
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800941// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942static config_pair_t const config_2_attribute_list[] = {
943 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800944 { EGL_ALPHA_SIZE, 0 },
945 { EGL_BLUE_SIZE, 8 },
946 { EGL_GREEN_SIZE, 8 },
947 { EGL_RED_SIZE, 8 },
948 { EGL_DEPTH_SIZE, 0 },
949 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700950 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800951 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
952};
953
954static config_pair_t const config_3_attribute_list[] = {
955 { EGL_BUFFER_SIZE, 32 },
956 { EGL_ALPHA_SIZE, 0 },
957 { EGL_BLUE_SIZE, 8 },
958 { EGL_GREEN_SIZE, 8 },
959 { EGL_RED_SIZE, 8 },
960 { EGL_DEPTH_SIZE, 16 },
961 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700962 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800963 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
964};
965
966// 8888 configs
967static config_pair_t const config_4_attribute_list[] = {
968 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 { EGL_ALPHA_SIZE, 8 },
970 { EGL_BLUE_SIZE, 8 },
971 { EGL_GREEN_SIZE, 8 },
972 { EGL_RED_SIZE, 8 },
973 { EGL_DEPTH_SIZE, 0 },
974 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -0700975 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
977};
978
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800979static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 { EGL_BUFFER_SIZE, 32 },
981 { EGL_ALPHA_SIZE, 8 },
982 { EGL_BLUE_SIZE, 8 },
983 { EGL_GREEN_SIZE, 8 },
984 { EGL_RED_SIZE, 8 },
985 { EGL_DEPTH_SIZE, 16 },
986 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700987 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
989};
990
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800991// A8 configs
992static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 { EGL_BUFFER_SIZE, 8 },
994 { EGL_ALPHA_SIZE, 8 },
995 { EGL_BLUE_SIZE, 0 },
996 { EGL_GREEN_SIZE, 0 },
997 { EGL_RED_SIZE, 0 },
998 { EGL_DEPTH_SIZE, 0 },
999 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001000 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1002};
1003
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001004static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 { EGL_BUFFER_SIZE, 8 },
1006 { EGL_ALPHA_SIZE, 8 },
1007 { EGL_BLUE_SIZE, 0 },
1008 { EGL_GREEN_SIZE, 0 },
1009 { EGL_RED_SIZE, 0 },
1010 { EGL_DEPTH_SIZE, 16 },
1011 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001012 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1014};
1015
1016static configs_t const gConfigs[] = {
1017 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1018 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1019 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1020 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1021 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1022 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001023 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1024 { config_7_attribute_list, NELEM(config_7_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025};
1026
1027static config_management_t const gConfigManagement[] = {
1028 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1029 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1030 { EGL_BLUE_SIZE, config_management_t::atLeast },
1031 { EGL_GREEN_SIZE, config_management_t::atLeast },
1032 { EGL_RED_SIZE, config_management_t::atLeast },
1033 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1034 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1035 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1036 { EGL_CONFIG_ID, config_management_t::exact },
1037 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001038 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1039 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1040 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001042 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1044 { EGL_SAMPLES, config_management_t::exact },
1045 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1046 { EGL_SURFACE_TYPE, config_management_t::mask },
1047 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1048 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1049 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1050 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1051 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1052 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1053 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1054 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001055 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1056 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1057 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1058 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1059 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060};
1061
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001064 // attributes that are not specified are simply ignored, if a particular
1065 // one needs not be ignored, it must be specified here, eg:
1066 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067};
1068
1069// ----------------------------------------------------------------------------
1070
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001071static status_t getConfigFormatInfo(EGLint configID,
1072 int32_t& pixelFormat, int32_t& depthFormat)
1073{
1074 switch(configID) {
1075 case 0:
1076 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1077 depthFormat = 0;
1078 break;
1079 case 1:
1080 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1081 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1082 break;
1083 case 2:
1084 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1085 depthFormat = 0;
1086 break;
1087 case 3:
1088 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1089 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1090 break;
1091 case 4:
1092 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1093 depthFormat = 0;
1094 break;
1095 case 5:
1096 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1097 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1098 break;
1099 case 6:
1100 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1101 depthFormat = 0;
1102 break;
1103 case 7:
1104 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1105 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1106 break;
1107 default:
1108 return NAME_NOT_FOUND;
1109 }
1110 return NO_ERROR;
1111}
1112
1113// ----------------------------------------------------------------------------
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115template<typename T>
1116static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1117{
1118 while (first <= last) {
1119 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001120 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001122 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 last = mid - 1;
1124 } else {
1125 return mid;
1126 }
1127 }
1128 return -1;
1129}
1130
1131static int isAttributeMatching(int i, EGLint attr, EGLint val)
1132{
1133 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001134 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 int index = binarySearch<config_pair_t>(
1136 gConfigs[i].array,
1137 0, gConfigs[i].size-1,
1138 attr);
1139 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001140 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 index = binarySearch<config_pair_t>(
1142 config_base_attribute_list,
1143 0, NELEM(config_base_attribute_list)-1,
1144 attr);
1145 }
1146 if (index >= 0) {
1147 // attribute found, check if this config could match
1148 int cfgMgtIndex = binarySearch<config_management_t>(
1149 gConfigManagement,
1150 0, NELEM(gConfigManagement)-1,
1151 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001152 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 bool match = gConfigManagement[cfgMgtIndex].match(
1154 val, configFound[index].value);
1155 if (match) {
1156 // this config matches
1157 return 1;
1158 }
1159 } else {
1160 // attribute not found. this should NEVER happen.
1161 }
1162 } else {
1163 // error, this attribute doesn't exist
1164 }
1165 return 0;
1166}
1167
1168static int makeCurrent(ogles_context_t* gl)
1169{
1170 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1171 if (gl) {
1172 egl_context_t* c = egl_context_t::context(gl);
1173 if (c->flags & egl_context_t::IS_CURRENT) {
1174 if (current != gl) {
1175 // it is an error to set a context current, if it's already
1176 // current to another thread
1177 return -1;
1178 }
1179 } else {
1180 if (current) {
1181 // mark the current context as not current, and flush
1182 glFlush();
1183 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1184 }
1185 }
1186 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1187 // The context is not current, make it current!
1188 setGlThreadSpecific(gl);
1189 c->flags |= egl_context_t::IS_CURRENT;
1190 }
1191 } else {
1192 if (current) {
1193 // mark the current context as not current, and flush
1194 glFlush();
1195 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1196 }
1197 // this thread has no context attached to it
1198 setGlThreadSpecific(0);
1199 }
1200 return 0;
1201}
1202
1203static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
1204 EGLint attribute, EGLint *value)
1205{
1206 size_t numConfigs = NELEM(gConfigs);
1207 int index = (int)config;
1208 if (uint32_t(index) >= numConfigs)
1209 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1210
1211 int attrIndex;
1212 attrIndex = binarySearch<config_pair_t>(
1213 gConfigs[index].array,
1214 0, gConfigs[index].size-1,
1215 attribute);
1216 if (attrIndex>=0) {
1217 *value = gConfigs[index].array[attrIndex].value;
1218 return EGL_TRUE;
1219 }
1220
1221 attrIndex = binarySearch<config_pair_t>(
1222 config_base_attribute_list,
1223 0, NELEM(config_base_attribute_list)-1,
1224 attribute);
1225 if (attrIndex>=0) {
1226 *value = config_base_attribute_list[attrIndex].value;
1227 return EGL_TRUE;
1228 }
1229 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1230}
1231
1232static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
1233 NativeWindowType window, const EGLint *attrib_list)
1234{
1235 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1236 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1237 if (window == 0)
1238 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1239
1240 EGLint surfaceType;
1241 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1242 return EGL_FALSE;
1243
1244 if (!(surfaceType & EGL_WINDOW_BIT))
1245 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1246
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001247 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001248 ANDROID_NATIVE_WINDOW_MAGIC) {
1249 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1250 }
1251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 EGLint configID;
1253 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1254 return EGL_FALSE;
1255
1256 int32_t depthFormat;
1257 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001258 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1260 }
1261
1262 // FIXME: we don't have access to the pixelFormat here just yet.
1263 // (it's possible that the surface is not fully initialized)
1264 // maybe this should be done after the page-flip
1265 //if (EGLint(info.format) != pixelFormat)
1266 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1267
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001268 egl_surface_t* surface;
1269 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001270 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271
Mathias Agopian0696a572009-08-20 00:12:56 -07001272 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 // there was a problem in the ctor, the error
1274 // flag has been set.
1275 delete surface;
1276 surface = 0;
1277 }
1278 return surface;
1279}
1280
1281static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
1282 NativePixmapType pixmap, const EGLint *attrib_list)
1283{
1284 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1285 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1286 if (pixmap == 0)
1287 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1288
1289 EGLint surfaceType;
1290 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1291 return EGL_FALSE;
1292
1293 if (!(surfaceType & EGL_PIXMAP_BIT))
1294 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1295
Mathias Agopian0696a572009-08-20 00:12:56 -07001296 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1297 sizeof(egl_native_pixmap_t)) {
1298 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1299 }
1300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 EGLint configID;
1302 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1303 return EGL_FALSE;
1304
1305 int32_t depthFormat;
1306 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001307 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1309 }
1310
1311 if (pixmap->format != pixelFormat)
1312 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1313
1314 egl_surface_t* surface =
1315 new egl_pixmap_surface_t(dpy, config, depthFormat,
1316 static_cast<egl_native_pixmap_t*>(pixmap));
1317
Mathias Agopian0696a572009-08-20 00:12:56 -07001318 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 // there was a problem in the ctor, the error
1320 // flag has been set.
1321 delete surface;
1322 surface = 0;
1323 }
1324 return surface;
1325}
1326
1327static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1328 const EGLint *attrib_list)
1329{
1330 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1331 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1332
1333 EGLint surfaceType;
1334 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1335 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 if (!(surfaceType & EGL_PBUFFER_BIT))
1338 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 EGLint configID;
1341 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1342 return EGL_FALSE;
1343
1344 int32_t depthFormat;
1345 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001346 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1348 }
1349
1350 int32_t w = 0;
1351 int32_t h = 0;
1352 while (attrib_list[0]) {
1353 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1354 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1355 attrib_list+=2;
1356 }
1357
1358 egl_surface_t* surface =
1359 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1360
Mathias Agopian0696a572009-08-20 00:12:56 -07001361 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 // there was a problem in the ctor, the error
1363 // flag has been set.
1364 delete surface;
1365 surface = 0;
1366 }
1367 return surface;
1368}
1369
1370// ----------------------------------------------------------------------------
1371}; // namespace android
1372// ----------------------------------------------------------------------------
1373
1374using namespace android;
1375
1376// ----------------------------------------------------------------------------
1377// Initialization
1378// ----------------------------------------------------------------------------
1379
1380EGLDisplay eglGetDisplay(NativeDisplayType display)
1381{
1382#ifndef HAVE_ANDROID_OS
1383 // this just needs to be done once
1384 if (gGLKey == -1) {
1385 pthread_mutex_lock(&gInitMutex);
1386 if (gGLKey == -1)
1387 pthread_key_create(&gGLKey, NULL);
1388 pthread_mutex_unlock(&gInitMutex);
1389 }
1390#endif
1391 if (display == EGL_DEFAULT_DISPLAY) {
1392 EGLDisplay dpy = (EGLDisplay)1;
1393 egl_display_t& d = egl_display_t::get_display(dpy);
1394 d.type = display;
1395 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 return EGL_NO_DISPLAY;
1398}
1399
1400EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1401{
1402 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1403 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 EGLBoolean res = EGL_TRUE;
1406 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001407
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 if (android_atomic_inc(&d.initialized) == 0) {
1409 // initialize stuff here if needed
1410 //pthread_mutex_lock(&gInitMutex);
1411 //pthread_mutex_unlock(&gInitMutex);
1412 }
1413
1414 if (res == EGL_TRUE) {
1415 if (major != NULL) *major = VERSION_MAJOR;
1416 if (minor != NULL) *minor = VERSION_MINOR;
1417 }
1418 return res;
1419}
1420
1421EGLBoolean eglTerminate(EGLDisplay dpy)
1422{
1423 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1424 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1425
1426 EGLBoolean res = EGL_TRUE;
1427 egl_display_t& d = egl_display_t::get_display(dpy);
1428 if (android_atomic_dec(&d.initialized) == 1) {
1429 // TODO: destroy all resources (surfaces, contexts, etc...)
1430 //pthread_mutex_lock(&gInitMutex);
1431 //pthread_mutex_unlock(&gInitMutex);
1432 }
1433 return res;
1434}
1435
1436// ----------------------------------------------------------------------------
1437// configuration
1438// ----------------------------------------------------------------------------
1439
1440EGLBoolean eglGetConfigs( EGLDisplay dpy,
1441 EGLConfig *configs,
1442 EGLint config_size, EGLint *num_config)
1443{
1444 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1445 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1446
1447 GLint numConfigs = NELEM(gConfigs);
1448 if (!configs) {
1449 *num_config = numConfigs;
1450 return EGL_TRUE;
1451 }
1452 GLint i;
1453 for (i=0 ; i<numConfigs && i<config_size ; i++) {
1454 *configs++ = (EGLConfig)i;
1455 }
1456 *num_config = i;
1457 return EGL_TRUE;
1458}
1459
1460EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1461 EGLConfig *configs, EGLint config_size,
1462 EGLint *num_config)
1463{
1464 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1465 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Jack Palevich749c63d2009-03-25 15:12:17 -07001466
1467 if (ggl_unlikely(num_config==0)) {
1468 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470
Jack Palevich749c63d2009-03-25 15:12:17 -07001471 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001472 /*
1473 * A NULL attrib_list should be treated as though it was an empty
1474 * one (terminated with EGL_NONE) as defined in
1475 * section 3.4.1 "Querying Configurations" in the EGL specification.
1476 */
1477 static const EGLint dummy = EGL_NONE;
1478 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 int numAttributes = 0;
1482 int numConfigs = NELEM(gConfigs);
1483 uint32_t possibleMatch = (1<<numConfigs)-1;
1484 while(possibleMatch && *attrib_list != EGL_NONE) {
1485 numAttributes++;
1486 EGLint attr = *attrib_list++;
1487 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001488 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 if (!(possibleMatch & (1<<i)))
1490 continue;
1491 if (isAttributeMatching(i, attr, val) == 0) {
1492 possibleMatch &= ~(1<<i);
1493 }
1494 }
1495 }
1496
1497 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001498 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1499 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 // default value
1501 if (binarySearch<config_pair_t>(
1502 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001503 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 config_defaults[j].key) < 0)
1505 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001506 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 if (!(possibleMatch & (1<<i)))
1508 continue;
1509 if (isAttributeMatching(i,
1510 config_defaults[j].key,
1511 config_defaults[j].value) == 0)
1512 {
1513 possibleMatch &= ~(1<<i);
1514 }
1515 }
1516 }
1517 }
1518
1519 // return the configurations found
1520 int n=0;
1521 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001522 if (configs) {
1523 for (int i=0 ; config_size && i<numConfigs ; i++) {
1524 if (possibleMatch & (1<<i)) {
1525 *configs++ = (EGLConfig)i;
1526 config_size--;
1527 n++;
1528 }
1529 }
1530 } else {
1531 for (int i=0 ; i<numConfigs ; i++) {
1532 if (possibleMatch & (1<<i)) {
1533 n++;
1534 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 }
1536 }
1537 }
1538 *num_config = n;
1539 return EGL_TRUE;
1540}
1541
1542EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1543 EGLint attribute, EGLint *value)
1544{
1545 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1546 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1547
1548 return getConfigAttrib(dpy, config, attribute, value);
1549}
1550
1551// ----------------------------------------------------------------------------
1552// surfaces
1553// ----------------------------------------------------------------------------
1554
1555EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1556 NativeWindowType window,
1557 const EGLint *attrib_list)
1558{
1559 return createWindowSurface(dpy, config, window, attrib_list);
1560}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1563 NativePixmapType pixmap,
1564 const EGLint *attrib_list)
1565{
1566 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1567}
1568
1569EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1570 const EGLint *attrib_list)
1571{
1572 return createPbufferSurface(dpy, config, attrib_list);
1573}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1576{
1577 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1578 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1579 if (eglSurface != EGL_NO_SURFACE) {
1580 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001581 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1583 if (surface->dpy != dpy)
1584 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001585 if (surface->ctx) {
1586 // FIXME: this surface is current check what the spec says
1587 surface->disconnect();
1588 surface->ctx = 0;
1589 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 delete surface;
1591 }
1592 return EGL_TRUE;
1593}
1594
1595EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1596 EGLint attribute, EGLint *value)
1597{
1598 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1599 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1600 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001601 if (!surface->isValid())
1602 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 if (surface->dpy != dpy)
1604 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1605
1606 EGLBoolean ret = EGL_TRUE;
1607 switch (attribute) {
1608 case EGL_CONFIG_ID:
1609 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1610 break;
1611 case EGL_WIDTH:
1612 *value = surface->getWidth();
1613 break;
1614 case EGL_HEIGHT:
1615 *value = surface->getHeight();
1616 break;
1617 case EGL_LARGEST_PBUFFER:
1618 // not modified for a window or pixmap surface
1619 break;
1620 case EGL_TEXTURE_FORMAT:
1621 *value = EGL_NO_TEXTURE;
1622 break;
1623 case EGL_TEXTURE_TARGET:
1624 *value = EGL_NO_TEXTURE;
1625 break;
1626 case EGL_MIPMAP_TEXTURE:
1627 *value = EGL_FALSE;
1628 break;
1629 case EGL_MIPMAP_LEVEL:
1630 *value = 0;
1631 break;
1632 case EGL_RENDER_BUFFER:
1633 // TODO: return the real RENDER_BUFFER here
1634 *value = EGL_BACK_BUFFER;
1635 break;
1636 case EGL_HORIZONTAL_RESOLUTION:
1637 // pixel/mm * EGL_DISPLAY_SCALING
1638 *value = surface->getHorizontalResolution();
1639 break;
1640 case EGL_VERTICAL_RESOLUTION:
1641 // pixel/mm * EGL_DISPLAY_SCALING
1642 *value = surface->getVerticalResolution();
1643 break;
1644 case EGL_PIXEL_ASPECT_RATIO: {
1645 // w/h * EGL_DISPLAY_SCALING
1646 int wr = surface->getHorizontalResolution();
1647 int hr = surface->getVerticalResolution();
1648 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1649 } break;
1650 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001651 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 break;
1653 default:
1654 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1655 }
1656 return ret;
1657}
1658
1659EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1660 EGLContext share_list, const EGLint *attrib_list)
1661{
1662 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1663 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1664
1665 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1666 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1667
1668 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1669 c->flags = egl_context_t::NEVER_CURRENT;
1670 c->dpy = dpy;
1671 c->config = config;
1672 c->read = 0;
1673 c->draw = 0;
1674 return (EGLContext)gl;
1675}
1676
1677EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1678{
1679 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1680 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1681 egl_context_t* c = egl_context_t::context(ctx);
1682 if (c->flags & egl_context_t::IS_CURRENT)
1683 setGlThreadSpecific(0);
1684 ogles_uninit((ogles_context_t*)ctx);
1685 return EGL_TRUE;
1686}
1687
1688EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1689 EGLSurface read, EGLContext ctx)
1690{
1691 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1692 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1693 if (draw) {
1694 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001695 if (!s->isValid())
1696 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 if (s->dpy != dpy)
1698 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001699 // TODO: check that draw is compatible with the context
1700 }
1701 if (read && read!=draw) {
1702 egl_surface_t* s = (egl_surface_t*)read;
1703 if (!s->isValid())
1704 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1705 if (s->dpy != dpy)
1706 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1707 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 }
1709
1710 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1713 return setError(EGL_BAD_MATCH, EGL_FALSE);
1714
1715 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1716 return setError(EGL_BAD_MATCH, EGL_FALSE);
1717
1718 if (ctx == EGL_NO_CONTEXT) {
1719 // if we're detaching, we need the current context
1720 current_ctx = (EGLContext)getGlThreadSpecific();
1721 } else {
1722 egl_context_t* c = egl_context_t::context(ctx);
1723 egl_surface_t* d = (egl_surface_t*)draw;
1724 egl_surface_t* r = (egl_surface_t*)read;
1725 if ((d && d->ctx && d->ctx != ctx) ||
1726 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001727 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1729 }
1730 }
1731
1732 ogles_context_t* gl = (ogles_context_t*)ctx;
1733 if (makeCurrent(gl) == 0) {
1734 if (ctx) {
1735 egl_context_t* c = egl_context_t::context(ctx);
1736 egl_surface_t* d = (egl_surface_t*)draw;
1737 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001738
1739 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001740 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1741 s->disconnect();
Mathias Agopiane71212b2009-05-05 00:37:46 -07001742 }
1743 if (c->read) {
1744 // FIXME: unlock/disconnect the read surface too
1745 }
1746
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001748 c->read = read;
1749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 if (c->flags & egl_context_t::NEVER_CURRENT) {
1751 c->flags &= ~egl_context_t::NEVER_CURRENT;
1752 GLint w = 0;
1753 GLint h = 0;
1754 if (draw) {
1755 w = d->getWidth();
1756 h = d->getHeight();
1757 }
1758 ogles_surfaceport(gl, 0, 0);
1759 ogles_viewport(gl, 0, 0, w, h);
1760 ogles_scissor(gl, 0, 0, w, h);
1761 }
1762 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001763 if (d->connect() == EGL_FALSE) {
1764 return EGL_FALSE;
1765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 d->ctx = ctx;
1767 d->bindDrawSurface(gl);
1768 }
1769 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001770 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 r->ctx = ctx;
1772 r->bindReadSurface(gl);
1773 }
1774 } else {
1775 // if surfaces were bound to the context bound to this thread
1776 // mark then as unbound.
1777 if (current_ctx) {
1778 egl_context_t* c = egl_context_t::context(current_ctx);
1779 egl_surface_t* d = (egl_surface_t*)c->draw;
1780 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001781 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001782 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001783 d->ctx = EGL_NO_CONTEXT;
1784 d->disconnect();
1785 }
1786 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001787 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001788 r->ctx = EGL_NO_CONTEXT;
1789 // FIXME: unlock/disconnect the read surface too
1790 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 }
1792 }
1793 return EGL_TRUE;
1794 }
1795 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1796}
1797
1798EGLContext eglGetCurrentContext(void)
1799{
1800 // eglGetCurrentContext returns the current EGL rendering context,
1801 // as specified by eglMakeCurrent. If no context is current,
1802 // EGL_NO_CONTEXT is returned.
1803 return (EGLContext)getGlThreadSpecific();
1804}
1805
1806EGLSurface eglGetCurrentSurface(EGLint readdraw)
1807{
1808 // eglGetCurrentSurface returns the read or draw surface attached
1809 // to the current EGL rendering context, as specified by eglMakeCurrent.
1810 // If no context is current, EGL_NO_SURFACE is returned.
1811 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1812 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1813 egl_context_t* c = egl_context_t::context(ctx);
1814 if (readdraw == EGL_READ) {
1815 return c->read;
1816 } else if (readdraw == EGL_DRAW) {
1817 return c->draw;
1818 }
1819 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1820}
1821
1822EGLDisplay eglGetCurrentDisplay(void)
1823{
1824 // eglGetCurrentDisplay returns the current EGL display connection
1825 // for the current EGL rendering context, as specified by eglMakeCurrent.
1826 // If no context is current, EGL_NO_DISPLAY is returned.
1827 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1828 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1829 egl_context_t* c = egl_context_t::context(ctx);
1830 return c->dpy;
1831}
1832
1833EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1834 EGLint attribute, EGLint *value)
1835{
1836 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1837 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1838 egl_context_t* c = egl_context_t::context(ctx);
1839 switch (attribute) {
1840 case EGL_CONFIG_ID:
1841 // Returns the ID of the EGL frame buffer configuration with
1842 // respect to which the context was created
1843 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1844 }
1845 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1846}
1847
1848EGLBoolean eglWaitGL(void)
1849{
1850 return EGL_TRUE;
1851}
1852
1853EGLBoolean eglWaitNative(EGLint engine)
1854{
1855 return EGL_TRUE;
1856}
1857
1858EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1859{
1860 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1861 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001862
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001864 if (!d->isValid())
1865 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 if (d->dpy != dpy)
1867 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1868
1869 // post the surface
1870 d->swapBuffers();
1871
1872 // if it's bound to a context, update the buffer
1873 if (d->ctx != EGL_NO_CONTEXT) {
1874 d->bindDrawSurface((ogles_context_t*)d->ctx);
1875 // if this surface is also the read surface of the context
1876 // it is bound to, make sure to update the read buffer as well.
1877 // The EGL spec is a little unclear about this.
1878 egl_context_t* c = egl_context_t::context(d->ctx);
1879 if (c->read == draw) {
1880 d->bindReadSurface((ogles_context_t*)d->ctx);
1881 }
1882 }
1883
1884 return EGL_TRUE;
1885}
1886
1887EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1888 NativePixmapType target)
1889{
1890 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1891 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1892 // TODO: eglCopyBuffers()
1893 return EGL_FALSE;
1894}
1895
1896EGLint eglGetError(void)
1897{
1898 return getError();
1899}
1900
1901const char* eglQueryString(EGLDisplay dpy, EGLint name)
1902{
1903 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1904 return setError(EGL_BAD_DISPLAY, (const char*)0);
1905
1906 switch (name) {
1907 case EGL_VENDOR:
1908 return gVendorString;
1909 case EGL_VERSION:
1910 return gVersionString;
1911 case EGL_EXTENSIONS:
1912 return gExtensionsString;
1913 case EGL_CLIENT_APIS:
1914 return gClientApiString;
1915 }
1916 return setError(EGL_BAD_PARAMETER, (const char *)0);
1917}
1918
1919// ----------------------------------------------------------------------------
1920// EGL 1.1
1921// ----------------------------------------------------------------------------
1922
1923EGLBoolean eglSurfaceAttrib(
1924 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1925{
1926 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1927 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1928 // TODO: eglSurfaceAttrib()
1929 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1930}
1931
1932EGLBoolean eglBindTexImage(
1933 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1934{
1935 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1936 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1937 // TODO: eglBindTexImage()
1938 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1939}
1940
1941EGLBoolean eglReleaseTexImage(
1942 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1943{
1944 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1945 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1946 // TODO: eglReleaseTexImage()
1947 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1948}
1949
1950EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1951{
1952 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1953 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1954 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03001955 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956}
1957
1958// ----------------------------------------------------------------------------
1959// EGL 1.2
1960// ----------------------------------------------------------------------------
1961
1962EGLBoolean eglBindAPI(EGLenum api)
1963{
1964 if (api != EGL_OPENGL_ES_API)
1965 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1966 return EGL_TRUE;
1967}
1968
1969EGLenum eglQueryAPI(void)
1970{
1971 return EGL_OPENGL_ES_API;
1972}
1973
1974EGLBoolean eglWaitClient(void)
1975{
1976 glFinish();
1977 return EGL_TRUE;
1978}
1979
1980EGLBoolean eglReleaseThread(void)
1981{
1982 // TODO: eglReleaseThread()
1983 return EGL_TRUE;
1984}
1985
1986EGLSurface eglCreatePbufferFromClientBuffer(
1987 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1988 EGLConfig config, const EGLint *attrib_list)
1989{
1990 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1991 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1992 // TODO: eglCreatePbufferFromClientBuffer()
1993 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1994}
1995
1996// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001997// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001998// ----------------------------------------------------------------------------
1999
2000void (*eglGetProcAddress (const char *procname))()
2001{
2002 extention_map_t const * const map = gExtentionMap;
2003 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2004 if (!strcmp(procname, map[i].name)) {
2005 return map[i].address;
2006 }
2007 }
2008 return NULL;
2009}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002010
2011EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
2012 const EGLint *attrib_list)
2013{
2014 EGLBoolean result = EGL_FALSE;
2015 return result;
2016}
2017
2018EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
2019{
2020 EGLBoolean result = EGL_FALSE;
2021 return result;
2022}
2023
2024EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2025 EGLClientBuffer buffer, const EGLint *attrib_list)
2026{
2027 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2028 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2029 }
2030 if (ctx != EGL_NO_CONTEXT) {
2031 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2032 }
2033 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2034 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2035 }
2036
Iliyan Malchev697526b2011-05-01 11:33:26 -07002037 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002038
2039 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2040 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2041
Iliyan Malchev697526b2011-05-01 11:33:26 -07002042 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002043 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002044
2045 switch (native_buffer->format) {
2046 case HAL_PIXEL_FORMAT_RGBA_8888:
2047 case HAL_PIXEL_FORMAT_RGBX_8888:
2048 case HAL_PIXEL_FORMAT_RGB_888:
2049 case HAL_PIXEL_FORMAT_RGB_565:
2050 case HAL_PIXEL_FORMAT_BGRA_8888:
2051 case HAL_PIXEL_FORMAT_RGBA_5551:
2052 case HAL_PIXEL_FORMAT_RGBA_4444:
2053 break;
2054 default:
2055 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2056 }
2057
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002058 native_buffer->common.incRef(&native_buffer->common);
2059 return (EGLImageKHR)native_buffer;
2060}
2061
2062EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2063{
2064 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2065 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2066 }
2067
Iliyan Malchev697526b2011-05-01 11:33:26 -07002068 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002069
2070 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2071 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2072
Iliyan Malchev697526b2011-05-01 11:33:26 -07002073 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002074 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2075
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002076 native_buffer->common.decRef(&native_buffer->common);
2077
2078 return EGL_TRUE;
2079}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002080
2081// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002082// EGL_KHR_fence_sync
2083// ----------------------------------------------------------------------------
2084
2085#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2086
2087EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2088 const EGLint *attrib_list)
2089{
2090 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2091 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2092 }
2093
2094 if (type != EGL_SYNC_FENCE_KHR ||
2095 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2096 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2097 }
2098
2099 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2100 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2101 }
2102
2103 // AGL is synchronous; nothing to do here.
2104
2105 return FENCE_SYNC_HANDLE;
2106}
2107
2108EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2109{
2110 if (sync != FENCE_SYNC_HANDLE) {
2111 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2112 }
2113
2114 return EGL_TRUE;
2115}
2116
2117EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags,
2118 EGLTimeKHR timeout)
2119{
2120 if (sync != FENCE_SYNC_HANDLE) {
2121 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2122 }
2123
2124 return EGL_CONDITION_SATISFIED_KHR;
2125}
2126
2127EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
2128 EGLint attribute, EGLint *value)
2129{
2130 if (sync != FENCE_SYNC_HANDLE) {
2131 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2132 }
2133
2134 switch (attribute) {
2135 case EGL_SYNC_TYPE_KHR:
2136 *value = EGL_SYNC_FENCE_KHR;
2137 return EGL_TRUE;
2138 case EGL_SYNC_STATUS_KHR:
2139 *value = EGL_SIGNALED_KHR;
2140 return EGL_TRUE;
2141 case EGL_SYNC_CONDITION_KHR:
2142 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2143 return EGL_TRUE;
2144 default:
2145 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2146 }
2147}
2148
2149// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002150// ANDROID extensions
2151// ----------------------------------------------------------------------------
2152
2153EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2154 EGLint left, EGLint top, EGLint width, EGLint height)
2155{
2156 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2157 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2158
2159 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002160 if (!d->isValid())
2161 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002162 if (d->dpy != dpy)
2163 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2164
2165 // post the surface
2166 d->setSwapRectangle(left, top, width, height);
2167
2168 return EGL_TRUE;
2169}
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -07002170
2171EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
2172{
2173 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2174 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2175
2176 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
2177 if (!d->isValid())
2178 return setError(EGL_BAD_SURFACE, (EGLClientBuffer)0);
2179 if (d->dpy != dpy)
2180 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer)0);
2181
2182 // post the surface
2183 return d->getRenderBuffer();
2184}