blob: be437054ce43e66e4b2ee8e2b543d9945e56018f [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>
Jesse Hall3aa75f92016-05-20 10:47:07 -070019#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <errno.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/ioctl.h>
26#include <sys/types.h>
27#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mark Salyzyn7823e122016-09-29 08:08:05 -070030#include <log/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/threads.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080033#include <ui/ANativeObjectBase.h>
Jamie Gennisd8e812c2012-06-13 16:32:25 -070034#include <ui/Fence.h>
Dan Stoza204240a2016-01-08 10:52:16 -080035#include <ui/GraphicBufferMapper.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38#include <EGL/egl.h>
39#include <EGL/eglext.h>
40#include <GLES/gl.h>
41#include <GLES/glext.h>
42
43#include <pixelflinger/format.h>
44#include <pixelflinger/pixelflinger.h>
45
46#include "context.h"
47#include "state.h"
48#include "texture.h"
49#include "matrix.h"
50
51#undef NELEM
52#define NELEM(x) (sizeof(x)/sizeof(*(x)))
53
Mathias Agopian5f2165f2012-02-24 18:25:41 -080054// ----------------------------------------------------------------------------
Mathias Agopian4b9511c2011-11-13 23:52:47 -080055
56EGLBoolean EGLAPI eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
57 EGLint left, EGLint top, EGLint width, EGLint height);
58
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070059
60typedef struct egl_native_pixmap_t
61{
62 int32_t version; /* must be 32 */
63 int32_t width;
64 int32_t height;
65 int32_t stride;
66 uint8_t* data;
67 uint8_t format;
68 uint8_t rfu[3];
69 union {
70 uint32_t compressedFormat;
71 int32_t vstride;
72 };
73 int32_t reserved;
74} egl_native_pixmap_t;
75
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ----------------------------------------------------------------------------
78namespace android {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080// ----------------------------------------------------------------------------
81
82const unsigned int NUM_DISPLAYS = 1;
83
Chih-Hung Hsieh1e02a622017-10-26 15:18:12 -070084#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Chih-Hung Hsieh1e02a622017-10-26 15:18:12 -070086#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER;
88static pthread_key_t gEGLErrorKey = -1;
Elliott Hughes6071da72015-08-12 15:27:47 -070089#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090namespace gl {
91pthread_key_t gGLKey = -1;
92}; // namespace gl
93#endif
94
95template<typename T>
96static T setError(GLint error, T returnValue) {
97 if (ggl_unlikely(gEGLErrorKey == -1)) {
98 pthread_mutex_lock(&gErrorKeyMutex);
99 if (gEGLErrorKey == -1)
100 pthread_key_create(&gEGLErrorKey, NULL);
101 pthread_mutex_unlock(&gErrorKeyMutex);
102 }
Colin Cross444839b2014-01-24 14:35:39 -0800103 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)error);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 return returnValue;
105}
106
107static GLint getError() {
108 if (ggl_unlikely(gEGLErrorKey == -1))
109 return EGL_SUCCESS;
Colin Cross444839b2014-01-24 14:35:39 -0800110 GLint error = (GLint)(uintptr_t)pthread_getspecific(gEGLErrorKey);
Jamie Gennis2076f352011-01-30 15:59:36 -0800111 if (error == 0) {
112 // The TLS key has been created by another thread, but the value for
113 // this thread has not been initialized.
114 return EGL_SUCCESS;
115 }
Colin Cross444839b2014-01-24 14:35:39 -0800116 pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)EGL_SUCCESS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 return error;
118}
119
120// ----------------------------------------------------------------------------
121
122struct egl_display_t
123{
124 egl_display_t() : type(0), initialized(0) { }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 static egl_display_t& get_display(EGLDisplay dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 static EGLBoolean is_valid(EGLDisplay dpy) {
129 return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
130 }
131
Jesse Hall3aa75f92016-05-20 10:47:07 -0700132 NativeDisplayType type;
133 std::atomic_size_t initialized;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134};
135
136static egl_display_t gDisplays[NUM_DISPLAYS];
137
138egl_display_t& egl_display_t::get_display(EGLDisplay dpy) {
139 return gDisplays[uintptr_t(dpy)-1U];
140}
141
142struct egl_context_t {
143 enum {
144 IS_CURRENT = 0x00010000,
145 NEVER_CURRENT = 0x00020000
146 };
147 uint32_t flags;
148 EGLDisplay dpy;
149 EGLConfig config;
150 EGLSurface read;
151 EGLSurface draw;
152
153 static inline egl_context_t* context(EGLContext ctx) {
154 ogles_context_t* const gl = static_cast<ogles_context_t*>(ctx);
155 return static_cast<egl_context_t*>(gl->rasterizer.base);
156 }
157};
158
159// ----------------------------------------------------------------------------
160
161struct egl_surface_t
162{
163 enum {
164 PAGE_FLIP = 0x00000001,
165 MAGIC = 0x31415265
166 };
167
168 uint32_t magic;
169 EGLDisplay dpy;
170 EGLConfig config;
171 EGLContext ctx;
Jesse Hall78141e32013-03-07 09:56:26 -0800172 bool zombie;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
174 egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
175 virtual ~egl_surface_t();
Mathias Agopian0696a572009-08-20 00:12:56 -0700176 bool isValid() const;
177 virtual bool initCheck() const = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl) = 0;
180 virtual EGLBoolean bindReadSurface(ogles_context_t* gl) = 0;
Mathias Agopiancf81c842009-07-31 14:47:00 -0700181 virtual EGLBoolean connect() { return EGL_TRUE; }
Mathias Agopiane71212b2009-05-05 00:37:46 -0700182 virtual void disconnect() {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 virtual EGLint getWidth() const = 0;
184 virtual EGLint getHeight() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
186 virtual EGLint getHorizontalResolution() const;
187 virtual EGLint getVerticalResolution() const;
188 virtual EGLint getRefreshRate() const;
189 virtual EGLint getSwapBehavior() const;
190 virtual EGLBoolean swapBuffers();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700191 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192protected:
193 GGLSurface depth;
194};
195
196egl_surface_t::egl_surface_t(EGLDisplay dpy,
197 EGLConfig config,
198 int32_t depthFormat)
Jesse Hall78141e32013-03-07 09:56:26 -0800199 : magic(MAGIC), dpy(dpy), config(config), ctx(0), zombie(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
201 depth.version = sizeof(GGLSurface);
202 depth.data = 0;
203 depth.format = depthFormat;
204}
205egl_surface_t::~egl_surface_t()
206{
207 magic = 0;
208 free(depth.data);
209}
Mathias Agopian0696a572009-08-20 00:12:56 -0700210bool egl_surface_t::isValid() const {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000211 ALOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
Mathias Agopian0696a572009-08-20 00:12:56 -0700212 return magic == MAGIC;
213}
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215EGLBoolean egl_surface_t::swapBuffers() {
216 return EGL_FALSE;
217}
218EGLint egl_surface_t::getHorizontalResolution() const {
219 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
220}
221EGLint egl_surface_t::getVerticalResolution() const {
222 return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
223}
224EGLint egl_surface_t::getRefreshRate() const {
225 return (60 * EGL_DISPLAY_SCALING);
226}
227EGLint egl_surface_t::getSwapBehavior() const {
228 return EGL_BUFFER_PRESERVED;
229}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700230EGLBoolean egl_surface_t::setSwapRectangle(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700231 EGLint /*l*/, EGLint /*t*/, EGLint /*w*/, EGLint /*h*/)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700232{
233 return EGL_FALSE;
234}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236// ----------------------------------------------------------------------------
237
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238struct egl_window_surface_v2_t : public egl_surface_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 egl_window_surface_v2_t(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 EGLDisplay dpy, EGLConfig config,
242 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700243 ANativeWindow* window);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244
Mathias Agopian0696a572009-08-20 00:12:56 -0700245 ~egl_window_surface_v2_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246
Mathias Agopian0696a572009-08-20 00:12:56 -0700247 virtual bool initCheck() const { return true; } // TODO: report failure if ctor fails
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 virtual EGLBoolean swapBuffers();
249 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
250 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700251 virtual EGLBoolean connect();
Mathias Agopiane71212b2009-05-05 00:37:46 -0700252 virtual void disconnect();
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700253 virtual EGLint getWidth() const { return width; }
254 virtual EGLint getHeight() const { return height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 virtual EGLint getHorizontalResolution() const;
256 virtual EGLint getVerticalResolution() const;
257 virtual EGLint getRefreshRate() const;
258 virtual EGLint getSwapBehavior() const;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700259 virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261private:
Iliyan Malchev697526b2011-05-01 11:33:26 -0700262 status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
263 status_t unlock(ANativeWindowBuffer* buf);
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700264 ANativeWindow* nativeWindow;
Iliyan Malchev697526b2011-05-01 11:33:26 -0700265 ANativeWindowBuffer* buffer;
266 ANativeWindowBuffer* previousBuffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700267 int width;
268 int height;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700269 void* bits;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700270 GGLFormat const* pixelFormatTable;
271
272 struct Rect {
273 inline Rect() { };
274 inline Rect(int32_t w, int32_t h)
275 : left(0), top(0), right(w), bottom(h) { }
276 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
277 : left(l), top(t), right(r), bottom(b) { }
278 Rect& andSelf(const Rect& r) {
279 left = max(left, r.left);
280 top = max(top, r.top);
281 right = min(right, r.right);
282 bottom = min(bottom, r.bottom);
283 return *this;
284 }
285 bool isEmpty() const {
286 return (left>=right || top>=bottom);
287 }
288 void dump(char const* what) {
Steve Block9d453682011-12-20 16:23:08 +0000289 ALOGD("%s { %5d, %5d, w=%5d, h=%5d }",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700290 what, left, top, right-left, bottom-top);
291 }
292
293 int32_t left;
294 int32_t top;
295 int32_t right;
296 int32_t bottom;
297 };
298
299 struct Region {
300 inline Region() : count(0) { }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700301 typedef Rect const* const_iterator;
302 const_iterator begin() const { return storage; }
303 const_iterator end() const { return storage+count; }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700304 static Region subtract(const Rect& lhs, const Rect& rhs) {
305 Region reg;
306 Rect* storage = reg.storage;
307 if (!lhs.isEmpty()) {
308 if (lhs.top < rhs.top) { // top rect
309 storage->left = lhs.left;
310 storage->top = lhs.top;
311 storage->right = lhs.right;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700312 storage->bottom = rhs.top;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700313 storage++;
314 }
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700315 const int32_t top = max(lhs.top, rhs.top);
316 const int32_t bot = min(lhs.bottom, rhs.bottom);
317 if (top < bot) {
318 if (lhs.left < rhs.left) { // left-side rect
319 storage->left = lhs.left;
320 storage->top = top;
321 storage->right = rhs.left;
322 storage->bottom = bot;
323 storage++;
324 }
325 if (lhs.right > rhs.right) { // right-side rect
326 storage->left = rhs.right;
327 storage->top = top;
328 storage->right = lhs.right;
329 storage->bottom = bot;
330 storage++;
331 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700332 }
333 if (lhs.bottom > rhs.bottom) { // bottom rect
334 storage->left = lhs.left;
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700335 storage->top = rhs.bottom;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700336 storage->right = lhs.right;
337 storage->bottom = lhs.bottom;
338 storage++;
339 }
340 reg.count = storage - reg.storage;
341 }
342 return reg;
343 }
344 bool isEmpty() const {
345 return count<=0;
346 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700347 private:
348 Rect storage[4];
349 ssize_t count;
350 };
351
352 void copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700353 ANativeWindowBuffer* dst, void* dst_vaddr,
354 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700355 const Region& clip);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700356
357 Rect dirtyRegion;
358 Rect oldDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359};
360
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700361egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 EGLConfig config,
363 int32_t depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700364 ANativeWindow* window)
Dan Stoza204240a2016-01-08 10:52:16 -0800365 : egl_surface_t(dpy, config, depthFormat),
366 nativeWindow(window), buffer(0), previousBuffer(0), bits(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
Mathias Agopian69e43b72011-05-11 13:41:09 -0700368
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700369 pixelFormatTable = gglGetPixelFormatTable();
Dan Stoza204240a2016-01-08 10:52:16 -0800370
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700371 // keep a reference on the window
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700372 nativeWindow->common.incRef(&nativeWindow->common);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700373 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
374 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
Mathias Agopiane71212b2009-05-05 00:37:46 -0700375}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700376
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700377egl_window_surface_v2_t::~egl_window_surface_v2_t() {
378 if (buffer) {
379 buffer->common.decRef(&buffer->common);
380 }
381 if (previousBuffer) {
382 previousBuffer->common.decRef(&previousBuffer->common);
383 }
384 nativeWindow->common.decRef(&nativeWindow->common);
385}
386
Mathias Agopiancf81c842009-07-31 14:47:00 -0700387EGLBoolean egl_window_surface_v2_t::connect()
Mathias Agopiane71212b2009-05-05 00:37:46 -0700388{
Mathias Agopian52212712009-08-11 22:34:02 -0700389 // we're intending to do software rendering
390 native_window_set_usage(nativeWindow,
391 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
392
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700393 // dequeue a buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700394 int fenceFd = -1;
395 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer,
396 &fenceFd) != NO_ERROR) {
397 return setError(EGL_BAD_ALLOC, EGL_FALSE);
398 }
399
400 // wait for the buffer
401 sp<Fence> fence(new Fence(fenceFd));
402 if (fence->wait(Fence::TIMEOUT_NEVER) != NO_ERROR) {
403 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700404 return setError(EGL_BAD_ALLOC, EGL_FALSE);
405 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700406
407 // allocate a corresponding depth-buffer
408 width = buffer->width;
409 height = buffer->height;
410 if (depth.format) {
411 depth.width = width;
412 depth.height = height;
413 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700414 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
415 static_cast<uint64_t>(depth.height) * 2;
416 if (depth.stride < 0 || depth.height > INT_MAX ||
417 allocSize > UINT32_MAX) {
418 return setError(EGL_BAD_ALLOC, EGL_FALSE);
419 }
420 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700421 if (depth.data == 0) {
Mathias Agopiancf81c842009-07-31 14:47:00 -0700422 return setError(EGL_BAD_ALLOC, EGL_FALSE);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700423 }
424 }
425
426 // keep a reference on the buffer
427 buffer->common.incRef(&buffer->common);
428
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700429 // pin the buffer down
430 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
431 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000432 ALOGE("connect() failed to lock buffer %p (%ux%u)",
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700433 buffer, buffer->width, buffer->height);
Mathias Agopiancf81c842009-07-31 14:47:00 -0700434 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700435 // FIXME: we should make sure we're not accessing the buffer anymore
436 }
Mathias Agopiancf81c842009-07-31 14:47:00 -0700437 return EGL_TRUE;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700438}
439
440void egl_window_surface_v2_t::disconnect()
441{
Mathias Agopian9648c1a2009-06-03 19:00:53 -0700442 if (buffer && bits) {
Mathias Agopiane71212b2009-05-05 00:37:46 -0700443 bits = NULL;
444 unlock(buffer);
445 }
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700446 if (buffer) {
Jesse Hall5f555562013-03-07 15:14:18 -0800447 nativeWindow->cancelBuffer(nativeWindow, buffer, -1);
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700448 buffer->common.decRef(&buffer->common);
449 buffer = 0;
450 }
451 if (previousBuffer) {
452 previousBuffer->common.decRef(&previousBuffer->common);
453 previousBuffer = 0;
454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455}
456
Mathias Agopian0926f502009-05-04 14:17:04 -0700457status_t egl_window_surface_v2_t::lock(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700458 ANativeWindowBuffer* buf, int usage, void** vaddr)
Mathias Agopian0926f502009-05-04 14:17:04 -0700459{
Dan Stoza204240a2016-01-08 10:52:16 -0800460 auto& mapper = GraphicBufferMapper::get();
461 return mapper.lock(buf->handle, usage,
462 android::Rect(buf->width, buf->height), vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700463}
464
Iliyan Malchev697526b2011-05-01 11:33:26 -0700465status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
Mathias Agopian0926f502009-05-04 14:17:04 -0700466{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700467 if (!buf) return BAD_VALUE;
Dan Stoza204240a2016-01-08 10:52:16 -0800468 auto& mapper = GraphicBufferMapper::get();
469 return mapper.unlock(buf->handle);
Mathias Agopian0926f502009-05-04 14:17:04 -0700470}
471
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700472void egl_window_surface_v2_t::copyBlt(
Iliyan Malchev697526b2011-05-01 11:33:26 -0700473 ANativeWindowBuffer* dst, void* dst_vaddr,
474 ANativeWindowBuffer* src, void const* src_vaddr,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700475 const Region& clip)
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700476{
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700477 // NOTE: dst and src must be the same format
478
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700479 Region::const_iterator cur = clip.begin();
480 Region::const_iterator end = clip.end();
Mathias Agopian0926f502009-05-04 14:17:04 -0700481
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700482 const size_t bpp = pixelFormatTable[src->format].size;
483 const size_t dbpr = dst->stride * bpp;
484 const size_t sbpr = src->stride * bpp;
485
486 uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
487 uint8_t * const dst_bits = (uint8_t *)dst_vaddr;
488
489 while (cur != end) {
490 const Rect& r(*cur++);
491 ssize_t w = r.right - r.left;
492 ssize_t h = r.bottom - r.top;
493 if (w <= 0 || h<=0) continue;
494 size_t size = w * bpp;
495 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
496 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
497 if (dbpr==sbpr && size==sbpr) {
498 size *= h;
499 h = 1;
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700500 }
Mathias Agopian9cdb01d2011-04-28 19:50:21 -0700501 do {
502 memcpy(d, s, size);
503 d += dbpr;
504 s += sbpr;
505 } while (--h > 0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700506 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700507}
508
509EGLBoolean egl_window_surface_v2_t::swapBuffers()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510{
Mathias Agopiancf81c842009-07-31 14:47:00 -0700511 if (!buffer) {
512 return setError(EGL_BAD_ACCESS, EGL_FALSE);
513 }
514
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700515 /*
516 * Handle eglSetSwapRectangleANDROID()
517 * We copyback from the front buffer
518 */
519 if (!dirtyRegion.isEmpty()) {
520 dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
521 if (previousBuffer) {
Kristian Monsen72c384e2010-10-27 17:59:09 +0100522 // This was const Region copyBack, but that causes an
523 // internal compile error on simulator builds
524 /*const*/ Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700525 if (!copyBack.isEmpty()) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700526 void* prevBits;
527 if (lock(previousBuffer,
Mathias Agopian240c9fe2009-06-25 15:39:25 -0700528 GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
529 // copy from previousBuffer to buffer
530 copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700531 unlock(previousBuffer);
532 }
533 }
534 }
535 oldDirtyRegion = dirtyRegion;
536 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700538 if (previousBuffer) {
539 previousBuffer->common.decRef(&previousBuffer->common);
540 previousBuffer = 0;
541 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700542
Mathias Agopian0926f502009-05-04 14:17:04 -0700543 unlock(buffer);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700544 previousBuffer = buffer;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700545 nativeWindow->queueBuffer(nativeWindow, buffer, -1);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700546 buffer = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700548 // dequeue a new buffer
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700549 int fenceFd = -1;
550 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &fenceFd) == NO_ERROR) {
551 sp<Fence> fence(new Fence(fenceFd));
552 if (fence->wait(Fence::TIMEOUT_NEVER)) {
553 nativeWindow->cancelBuffer(nativeWindow, buffer, fenceFd);
554 return setError(EGL_BAD_ALLOC, EGL_FALSE);
555 }
Mathias Agopian031213e2010-08-18 16:07:34 -0700556
557 // reallocate the depth-buffer if needed
558 if ((width != buffer->width) || (height != buffer->height)) {
559 // TODO: we probably should reset the swap rect here
560 // if the window size has changed
561 width = buffer->width;
562 height = buffer->height;
563 if (depth.data) {
564 free(depth.data);
565 depth.width = width;
566 depth.height = height;
567 depth.stride = buffer->stride;
Michael Lentine95303882015-05-28 17:43:06 -0700568 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
569 static_cast<uint64_t>(depth.height) * 2;
570 if (depth.stride < 0 || depth.height > INT_MAX ||
571 allocSize > UINT32_MAX) {
572 setError(EGL_BAD_ALLOC, EGL_FALSE);
573 return EGL_FALSE;
574 }
575 depth.data = (GGLubyte*)malloc(allocSize);
Mathias Agopian031213e2010-08-18 16:07:34 -0700576 if (depth.data == 0) {
577 setError(EGL_BAD_ALLOC, EGL_FALSE);
578 return EGL_FALSE;
579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 }
581 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700582
Mathias Agopian031213e2010-08-18 16:07:34 -0700583 // keep a reference on the buffer
584 buffer->common.incRef(&buffer->common);
585
586 // finally pin the buffer down
587 if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
588 GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000589 ALOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
Mathias Agopian031213e2010-08-18 16:07:34 -0700590 buffer, buffer->width, buffer->height);
591 return setError(EGL_BAD_ACCESS, EGL_FALSE);
592 // FIXME: we should make sure we're not accessing the buffer anymore
593 }
594 } else {
595 return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700596 }
597
598 return EGL_TRUE;
599}
600
601EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
602 EGLint l, EGLint t, EGLint w, EGLint h)
603{
604 dirtyRegion = Rect(l, t, l+w, t+h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 return EGL_TRUE;
606}
607
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700608EGLBoolean egl_window_surface_v2_t::bindDrawSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609{
610 GGLSurface buffer;
611 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 buffer.width = this->buffer->width;
613 buffer.height = this->buffer->height;
614 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700615 buffer.data = (GGLubyte*)bits;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 gl->rasterizer.procs.colorBuffer(gl, &buffer);
618 if (depth.data != gl->rasterizer.state.buffers.depth.data)
619 gl->rasterizer.procs.depthBuffer(gl, &depth);
Mathias Agopian0a3139a2009-06-10 16:01:54 -0700620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 return EGL_TRUE;
622}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700623EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624{
625 GGLSurface buffer;
626 buffer.version = sizeof(GGLSurface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627 buffer.width = this->buffer->width;
628 buffer.height = this->buffer->height;
629 buffer.stride = this->buffer->stride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700630 buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700631 buffer.format = this->buffer->format;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 gl->rasterizer.procs.readBuffer(gl, &buffer);
633 return EGL_TRUE;
634}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635EGLint egl_window_surface_v2_t::getHorizontalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
637}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638EGLint egl_window_surface_v2_t::getVerticalResolution() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
640}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641EGLint egl_window_surface_v2_t::getRefreshRate() const {
642 return (60 * EGL_DISPLAY_SCALING); // FIXME
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643}
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700644EGLint egl_window_surface_v2_t::getSwapBehavior() const
645{
646 /*
647 * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
648 * the content of the swapped buffer.
649 *
650 * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
651 *
652 * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
653 * only applies to the area specified by eglSetSwapRectangleANDROID(), that
654 * is, everything outside of this area is preserved.
655 *
656 * This implementation of EGL assumes the later case.
657 *
658 */
659
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660 return EGL_BUFFER_DESTROYED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661}
662
663// ----------------------------------------------------------------------------
664
665struct egl_pixmap_surface_t : public egl_surface_t
666{
667 egl_pixmap_surface_t(
668 EGLDisplay dpy, EGLConfig config,
669 int32_t depthFormat,
670 egl_native_pixmap_t const * pixmap);
671
672 virtual ~egl_pixmap_surface_t() { }
673
Mathias Agopian0696a572009-08-20 00:12:56 -0700674 virtual bool initCheck() const { return !depth.format || depth.data!=0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
676 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
677 virtual EGLint getWidth() const { return nativePixmap.width; }
678 virtual EGLint getHeight() const { return nativePixmap.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679private:
680 egl_native_pixmap_t nativePixmap;
681};
682
683egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
684 EGLConfig config,
685 int32_t depthFormat,
686 egl_native_pixmap_t const * pixmap)
687 : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
688{
689 if (depthFormat) {
690 depth.width = pixmap->width;
691 depth.height = pixmap->height;
692 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700693 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
694 static_cast<uint64_t>(depth.height) * 2;
695 if (depth.stride < 0 || depth.height > INT_MAX ||
696 allocSize > UINT32_MAX) {
697 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
698 return;
699 }
700 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 if (depth.data == 0) {
702 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 }
704 }
705}
706EGLBoolean egl_pixmap_surface_t::bindDrawSurface(ogles_context_t* gl)
707{
708 GGLSurface buffer;
709 buffer.version = sizeof(GGLSurface);
710 buffer.width = nativePixmap.width;
711 buffer.height = nativePixmap.height;
712 buffer.stride = nativePixmap.stride;
713 buffer.data = nativePixmap.data;
714 buffer.format = nativePixmap.format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 gl->rasterizer.procs.colorBuffer(gl, &buffer);
717 if (depth.data != gl->rasterizer.state.buffers.depth.data)
718 gl->rasterizer.procs.depthBuffer(gl, &depth);
719 return EGL_TRUE;
720}
721EGLBoolean egl_pixmap_surface_t::bindReadSurface(ogles_context_t* gl)
722{
723 GGLSurface buffer;
724 buffer.version = sizeof(GGLSurface);
725 buffer.width = nativePixmap.width;
726 buffer.height = nativePixmap.height;
727 buffer.stride = nativePixmap.stride;
728 buffer.data = nativePixmap.data;
729 buffer.format = nativePixmap.format;
730 gl->rasterizer.procs.readBuffer(gl, &buffer);
731 return EGL_TRUE;
732}
733
734// ----------------------------------------------------------------------------
735
736struct egl_pbuffer_surface_t : public egl_surface_t
737{
738 egl_pbuffer_surface_t(
739 EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
740 int32_t w, int32_t h, int32_t f);
741
742 virtual ~egl_pbuffer_surface_t();
743
Mathias Agopian0696a572009-08-20 00:12:56 -0700744 virtual bool initCheck() const { return pbuffer.data != 0; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 virtual EGLBoolean bindDrawSurface(ogles_context_t* gl);
746 virtual EGLBoolean bindReadSurface(ogles_context_t* gl);
747 virtual EGLint getWidth() const { return pbuffer.width; }
748 virtual EGLint getHeight() const { return pbuffer.height; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749private:
750 GGLSurface pbuffer;
751};
752
753egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
754 EGLConfig config, int32_t depthFormat,
755 int32_t w, int32_t h, int32_t f)
756 : egl_surface_t(dpy, config, depthFormat)
757{
758 size_t size = w*h;
759 switch (f) {
760 case GGL_PIXEL_FORMAT_A_8: size *= 1; break;
761 case GGL_PIXEL_FORMAT_RGB_565: size *= 2; break;
762 case GGL_PIXEL_FORMAT_RGBA_8888: size *= 4; break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800763 case GGL_PIXEL_FORMAT_RGBX_8888: size *= 4; break;
Amit Pundir90c75a22015-04-18 20:37:58 +0530764 case GGL_PIXEL_FORMAT_BGRA_8888: size *= 4; break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 default:
Steve Blocke6f43dd2012-01-06 19:20:56 +0000766 ALOGE("incompatible pixel format for pbuffer (format=%d)", f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 pbuffer.data = 0;
768 break;
769 }
770 pbuffer.version = sizeof(GGLSurface);
771 pbuffer.width = w;
772 pbuffer.height = h;
773 pbuffer.stride = w;
774 pbuffer.data = (GGLubyte*)malloc(size);
775 pbuffer.format = f;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 if (depthFormat) {
778 depth.width = pbuffer.width;
779 depth.height = pbuffer.height;
780 depth.stride = depth.width; // use the width here
Michael Lentine95303882015-05-28 17:43:06 -0700781 uint64_t allocSize = static_cast<uint64_t>(depth.stride) *
782 static_cast<uint64_t>(depth.height) * 2;
783 if (depth.stride < 0 || depth.height > INT_MAX ||
784 allocSize > UINT32_MAX) {
785 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
786 return;
787 }
788 depth.data = (GGLubyte*)malloc(allocSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 if (depth.data == 0) {
790 setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
791 return;
792 }
793 }
794}
795egl_pbuffer_surface_t::~egl_pbuffer_surface_t() {
796 free(pbuffer.data);
797}
798EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(ogles_context_t* gl)
799{
800 gl->rasterizer.procs.colorBuffer(gl, &pbuffer);
801 if (depth.data != gl->rasterizer.state.buffers.depth.data)
802 gl->rasterizer.procs.depthBuffer(gl, &depth);
803 return EGL_TRUE;
804}
805EGLBoolean egl_pbuffer_surface_t::bindReadSurface(ogles_context_t* gl)
806{
807 gl->rasterizer.procs.readBuffer(gl, &pbuffer);
808 return EGL_TRUE;
809}
810
811// ----------------------------------------------------------------------------
812
813struct config_pair_t {
814 GLint key;
815 GLint value;
816};
817
818struct configs_t {
819 const config_pair_t* array;
820 int size;
821};
822
823struct config_management_t {
824 GLint key;
825 bool (*match)(GLint reqValue, GLint confValue);
826 static bool atLeast(GLint reqValue, GLint confValue) {
827 return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
828 }
829 static bool exact(GLint reqValue, GLint confValue) {
830 return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
831 }
832 static bool mask(GLint reqValue, GLint confValue) {
833 return (confValue & reqValue) == reqValue;
834 }
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700835 static bool ignore(GLint /*reqValue*/, GLint /*confValue*/) {
Mathias Agopian63971672010-10-25 15:51:24 -0700836 return true;
837 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838};
839
840// ----------------------------------------------------------------------------
841
842#define VERSION_MAJOR 1
843#define VERSION_MINOR 2
844static char const * const gVendorString = "Google Inc.";
Mathias Agopian141550b2010-10-19 14:47:08 -0700845static char const * const gVersionString = "1.2 Android Driver 1.2.0";
Mathias Agopiancc2b1562012-05-21 14:01:37 -0700846static char const * const gClientApiString = "OpenGL_ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700847static char const * const gExtensionsString =
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700848 "EGL_KHR_fence_sync "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700849 "EGL_KHR_image_base "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700850 // "KHR_image_pixmap "
851 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700852 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700853 ;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854
855// ----------------------------------------------------------------------------
856
857struct extention_map_t {
858 const char * const name;
859 __eglMustCastToProperFunctionPointerType address;
860};
861
862static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700863 { "glDrawTexsOES",
864 (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
865 { "glDrawTexiOES",
866 (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
867 { "glDrawTexfOES",
868 (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
869 { "glDrawTexxOES",
870 (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
871 { "glDrawTexsvOES",
872 (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
873 { "glDrawTexivOES",
874 (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
875 { "glDrawTexfvOES",
876 (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
877 { "glDrawTexxvOES",
878 (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
879 { "glQueryMatrixxOES",
880 (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
881 { "glEGLImageTargetTexture2DOES",
882 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
883 { "glEGLImageTargetRenderbufferStorageOES",
884 (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
885 { "glClipPlanef",
886 (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
887 { "glClipPlanex",
888 (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
889 { "glBindBuffer",
890 (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
891 { "glBufferData",
892 (__eglMustCastToProperFunctionPointerType)&glBufferData },
893 { "glBufferSubData",
894 (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
895 { "glDeleteBuffers",
896 (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
897 { "glGenBuffers",
898 (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700899 { "eglCreateImageKHR",
900 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
901 { "eglDestroyImageKHR",
902 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jesse Hall83e7c8c2012-05-22 10:42:56 -0700903 { "eglCreateSyncKHR",
904 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
905 { "eglDestroySyncKHR",
906 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
907 { "eglClientWaitSyncKHR",
908 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
909 { "eglGetSyncAttribKHR",
910 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700911 { "eglSetSwapRectangleANDROID",
912 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913};
914
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700915/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 * In the lists below, attributes names MUST be sorted.
917 * Additionally, all configs must be sorted according to
918 * the EGL specification.
919 */
920
921static config_pair_t const config_base_attribute_list[] = {
922 { EGL_STENCIL_SIZE, 0 },
923 { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG },
924 { EGL_LEVEL, 0 },
925 { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS },
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700926 { EGL_MAX_PBUFFER_PIXELS,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS },
928 { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS },
929 { EGL_NATIVE_RENDERABLE, EGL_TRUE },
930 { EGL_NATIVE_VISUAL_ID, 0 },
931 { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGB_565 },
932 { EGL_SAMPLES, 0 },
933 { EGL_SAMPLE_BUFFERS, 0 },
934 { EGL_TRANSPARENT_TYPE, EGL_NONE },
935 { EGL_TRANSPARENT_BLUE_VALUE, 0 },
936 { EGL_TRANSPARENT_GREEN_VALUE, 0 },
937 { EGL_TRANSPARENT_RED_VALUE, 0 },
938 { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE },
939 { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE },
940 { EGL_MIN_SWAP_INTERVAL, 1 },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700941 { EGL_MAX_SWAP_INTERVAL, 1 },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700942 { EGL_LUMINANCE_SIZE, 0 },
943 { EGL_ALPHA_MASK_SIZE, 0 },
944 { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER },
Mathias Agopian56fa2752009-09-27 20:18:16 -0700945 { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT },
Mathias Agopian0985f6a2009-10-19 14:46:27 -0700946 { EGL_CONFORMANT, 0 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947};
948
949// These configs can override the base attribute list
950// NOTE: when adding a config here, don't forget to update eglCreate*Surface()
951
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800952// 565 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953static config_pair_t const config_0_attribute_list[] = {
954 { EGL_BUFFER_SIZE, 16 },
955 { EGL_ALPHA_SIZE, 0 },
956 { EGL_BLUE_SIZE, 5 },
957 { EGL_GREEN_SIZE, 6 },
958 { EGL_RED_SIZE, 5 },
959 { EGL_DEPTH_SIZE, 0 },
960 { EGL_CONFIG_ID, 0 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700961 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
963};
964
965static config_pair_t const config_1_attribute_list[] = {
966 { EGL_BUFFER_SIZE, 16 },
967 { EGL_ALPHA_SIZE, 0 },
968 { EGL_BLUE_SIZE, 5 },
969 { EGL_GREEN_SIZE, 6 },
970 { EGL_RED_SIZE, 5 },
971 { EGL_DEPTH_SIZE, 16 },
972 { EGL_CONFIG_ID, 1 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700973 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
975};
976
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800977// RGB 888 configs
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978static config_pair_t const config_2_attribute_list[] = {
979 { EGL_BUFFER_SIZE, 32 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800980 { EGL_ALPHA_SIZE, 0 },
981 { EGL_BLUE_SIZE, 8 },
982 { EGL_GREEN_SIZE, 8 },
983 { EGL_RED_SIZE, 8 },
984 { EGL_DEPTH_SIZE, 0 },
985 { EGL_CONFIG_ID, 6 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700986 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800987 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
988};
989
990static config_pair_t const config_3_attribute_list[] = {
991 { EGL_BUFFER_SIZE, 32 },
992 { EGL_ALPHA_SIZE, 0 },
993 { EGL_BLUE_SIZE, 8 },
994 { EGL_GREEN_SIZE, 8 },
995 { EGL_RED_SIZE, 8 },
996 { EGL_DEPTH_SIZE, 16 },
997 { EGL_CONFIG_ID, 7 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -0700998 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -0800999 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1000};
1001
1002// 8888 configs
1003static config_pair_t const config_4_attribute_list[] = {
1004 { EGL_BUFFER_SIZE, 32 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 { EGL_ALPHA_SIZE, 8 },
1006 { EGL_BLUE_SIZE, 8 },
1007 { EGL_GREEN_SIZE, 8 },
1008 { EGL_RED_SIZE, 8 },
1009 { EGL_DEPTH_SIZE, 0 },
1010 { EGL_CONFIG_ID, 2 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001011 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1013};
1014
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001015static config_pair_t const config_5_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 { EGL_BUFFER_SIZE, 32 },
1017 { EGL_ALPHA_SIZE, 8 },
1018 { EGL_BLUE_SIZE, 8 },
1019 { EGL_GREEN_SIZE, 8 },
1020 { EGL_RED_SIZE, 8 },
1021 { EGL_DEPTH_SIZE, 16 },
1022 { EGL_CONFIG_ID, 3 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001023 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1025};
1026
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001027// A8 configs
1028static config_pair_t const config_6_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 { EGL_BUFFER_SIZE, 8 },
1030 { EGL_ALPHA_SIZE, 8 },
1031 { EGL_BLUE_SIZE, 0 },
1032 { EGL_GREEN_SIZE, 0 },
1033 { EGL_RED_SIZE, 0 },
1034 { EGL_DEPTH_SIZE, 0 },
1035 { EGL_CONFIG_ID, 4 },
Mathias Agopiand8e5ceb2010-10-20 17:21:43 -07001036 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1038};
1039
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001040static config_pair_t const config_7_attribute_list[] = {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 { EGL_BUFFER_SIZE, 8 },
1042 { EGL_ALPHA_SIZE, 8 },
1043 { EGL_BLUE_SIZE, 0 },
1044 { EGL_GREEN_SIZE, 0 },
1045 { EGL_RED_SIZE, 0 },
1046 { EGL_DEPTH_SIZE, 16 },
1047 { EGL_CONFIG_ID, 5 },
Mathias Agopian6af358e2010-10-21 15:58:25 -07001048 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1050};
1051
Amit Pundir90c75a22015-04-18 20:37:58 +05301052// BGRA 8888 config
1053static config_pair_t const config_8_attribute_list[] = {
1054 { EGL_BUFFER_SIZE, 32 },
1055 { EGL_ALPHA_SIZE, 8 },
1056 { EGL_BLUE_SIZE, 8 },
1057 { EGL_GREEN_SIZE, 8 },
1058 { EGL_RED_SIZE, 8 },
1059 { EGL_DEPTH_SIZE, 0 },
1060 { EGL_CONFIG_ID, 8 },
1061 { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_BGRA_8888 },
1062 { EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
1063};
1064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065static configs_t const gConfigs[] = {
1066 { config_0_attribute_list, NELEM(config_0_attribute_list) },
1067 { config_1_attribute_list, NELEM(config_1_attribute_list) },
1068 { config_2_attribute_list, NELEM(config_2_attribute_list) },
1069 { config_3_attribute_list, NELEM(config_3_attribute_list) },
1070 { config_4_attribute_list, NELEM(config_4_attribute_list) },
1071 { config_5_attribute_list, NELEM(config_5_attribute_list) },
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001072 { config_6_attribute_list, NELEM(config_6_attribute_list) },
1073 { config_7_attribute_list, NELEM(config_7_attribute_list) },
Amit Pundir90c75a22015-04-18 20:37:58 +05301074 { config_8_attribute_list, NELEM(config_8_attribute_list) },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075};
1076
1077static config_management_t const gConfigManagement[] = {
1078 { EGL_BUFFER_SIZE, config_management_t::atLeast },
1079 { EGL_ALPHA_SIZE, config_management_t::atLeast },
1080 { EGL_BLUE_SIZE, config_management_t::atLeast },
1081 { EGL_GREEN_SIZE, config_management_t::atLeast },
1082 { EGL_RED_SIZE, config_management_t::atLeast },
1083 { EGL_DEPTH_SIZE, config_management_t::atLeast },
1084 { EGL_STENCIL_SIZE, config_management_t::atLeast },
1085 { EGL_CONFIG_CAVEAT, config_management_t::exact },
1086 { EGL_CONFIG_ID, config_management_t::exact },
1087 { EGL_LEVEL, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001088 { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore },
1089 { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore },
1090 { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 { EGL_NATIVE_RENDERABLE, config_management_t::exact },
Mathias Agopian63971672010-10-25 15:51:24 -07001092 { EGL_NATIVE_VISUAL_ID, config_management_t::ignore },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact },
1094 { EGL_SAMPLES, config_management_t::exact },
1095 { EGL_SAMPLE_BUFFERS, config_management_t::exact },
1096 { EGL_SURFACE_TYPE, config_management_t::mask },
1097 { EGL_TRANSPARENT_TYPE, config_management_t::exact },
1098 { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact },
1099 { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact },
1100 { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact },
1101 { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact },
1102 { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact },
1103 { EGL_MIN_SWAP_INTERVAL, config_management_t::exact },
1104 { EGL_MAX_SWAP_INTERVAL, config_management_t::exact },
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001105 { EGL_LUMINANCE_SIZE, config_management_t::atLeast },
1106 { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast },
1107 { EGL_COLOR_BUFFER_TYPE, config_management_t::exact },
1108 { EGL_RENDERABLE_TYPE, config_management_t::mask },
1109 { EGL_CONFORMANT, config_management_t::mask }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110};
1111
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113static config_pair_t const config_defaults[] = {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001114 // attributes that are not specified are simply ignored, if a particular
1115 // one needs not be ignored, it must be specified here, eg:
1116 // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117};
1118
1119// ----------------------------------------------------------------------------
1120
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001121static status_t getConfigFormatInfo(EGLint configID,
1122 int32_t& pixelFormat, int32_t& depthFormat)
1123{
1124 switch(configID) {
1125 case 0:
1126 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1127 depthFormat = 0;
1128 break;
1129 case 1:
1130 pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
1131 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1132 break;
1133 case 2:
1134 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1135 depthFormat = 0;
1136 break;
1137 case 3:
1138 pixelFormat = GGL_PIXEL_FORMAT_RGBX_8888;
1139 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1140 break;
1141 case 4:
1142 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1143 depthFormat = 0;
1144 break;
1145 case 5:
1146 pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
1147 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1148 break;
1149 case 6:
1150 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1151 depthFormat = 0;
1152 break;
1153 case 7:
1154 pixelFormat = GGL_PIXEL_FORMAT_A_8;
1155 depthFormat = GGL_PIXEL_FORMAT_Z_16;
1156 break;
Amit Pundir90c75a22015-04-18 20:37:58 +05301157 case 8:
1158 pixelFormat = GGL_PIXEL_FORMAT_BGRA_8888;
1159 depthFormat = 0;
1160 break;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001161 default:
1162 return NAME_NOT_FOUND;
1163 }
1164 return NO_ERROR;
1165}
1166
1167// ----------------------------------------------------------------------------
1168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169template<typename T>
1170static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
1171{
1172 while (first <= last) {
1173 int mid = (first + last) / 2;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001174 if (key > sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 first = mid + 1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001176 } else if (key < sortedArray[mid].key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 last = mid - 1;
1178 } else {
1179 return mid;
1180 }
1181 }
1182 return -1;
1183}
1184
1185static int isAttributeMatching(int i, EGLint attr, EGLint val)
1186{
1187 // look for the attribute in all of our configs
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001188 config_pair_t const* configFound = gConfigs[i].array;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 int index = binarySearch<config_pair_t>(
1190 gConfigs[i].array,
1191 0, gConfigs[i].size-1,
1192 attr);
1193 if (index < 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001194 configFound = config_base_attribute_list;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 index = binarySearch<config_pair_t>(
1196 config_base_attribute_list,
1197 0, NELEM(config_base_attribute_list)-1,
1198 attr);
1199 }
1200 if (index >= 0) {
1201 // attribute found, check if this config could match
1202 int cfgMgtIndex = binarySearch<config_management_t>(
1203 gConfigManagement,
1204 0, NELEM(gConfigManagement)-1,
1205 attr);
Christoffer Gurell97640b92009-10-12 11:57:27 +02001206 if (cfgMgtIndex >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 bool match = gConfigManagement[cfgMgtIndex].match(
1208 val, configFound[index].value);
1209 if (match) {
1210 // this config matches
1211 return 1;
1212 }
1213 } else {
1214 // attribute not found. this should NEVER happen.
1215 }
1216 } else {
1217 // error, this attribute doesn't exist
1218 }
1219 return 0;
1220}
1221
1222static int makeCurrent(ogles_context_t* gl)
1223{
1224 ogles_context_t* current = (ogles_context_t*)getGlThreadSpecific();
1225 if (gl) {
1226 egl_context_t* c = egl_context_t::context(gl);
1227 if (c->flags & egl_context_t::IS_CURRENT) {
1228 if (current != gl) {
1229 // it is an error to set a context current, if it's already
1230 // current to another thread
1231 return -1;
1232 }
1233 } else {
1234 if (current) {
1235 // mark the current context as not current, and flush
1236 glFlush();
1237 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1238 }
1239 }
1240 if (!(c->flags & egl_context_t::IS_CURRENT)) {
1241 // The context is not current, make it current!
1242 setGlThreadSpecific(gl);
1243 c->flags |= egl_context_t::IS_CURRENT;
1244 }
1245 } else {
1246 if (current) {
1247 // mark the current context as not current, and flush
1248 glFlush();
1249 egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
1250 }
1251 // this thread has no context attached to it
1252 setGlThreadSpecific(0);
1253 }
1254 return 0;
1255}
1256
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001257static EGLBoolean getConfigAttrib(EGLDisplay /*dpy*/, EGLConfig config,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 EGLint attribute, EGLint *value)
1259{
1260 size_t numConfigs = NELEM(gConfigs);
Colin Cross444839b2014-01-24 14:35:39 -08001261 int index = (int)(uintptr_t)config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 if (uint32_t(index) >= numConfigs)
1263 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1264
1265 int attrIndex;
1266 attrIndex = binarySearch<config_pair_t>(
1267 gConfigs[index].array,
1268 0, gConfigs[index].size-1,
1269 attribute);
1270 if (attrIndex>=0) {
1271 *value = gConfigs[index].array[attrIndex].value;
1272 return EGL_TRUE;
1273 }
1274
1275 attrIndex = binarySearch<config_pair_t>(
1276 config_base_attribute_list,
1277 0, NELEM(config_base_attribute_list)-1,
1278 attribute);
1279 if (attrIndex>=0) {
1280 *value = config_base_attribute_list[attrIndex].value;
1281 return EGL_TRUE;
1282 }
1283 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1284}
1285
1286static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001287 NativeWindowType window, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
1289 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1290 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1291 if (window == 0)
1292 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1293
1294 EGLint surfaceType;
1295 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1296 return EGL_FALSE;
1297
1298 if (!(surfaceType & EGL_WINDOW_BIT))
1299 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1300
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001301 if (static_cast<ANativeWindow*>(window)->common.magic !=
Mathias Agopian0696a572009-08-20 00:12:56 -07001302 ANDROID_NATIVE_WINDOW_MAGIC) {
1303 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1304 }
1305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 EGLint configID;
1307 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1308 return EGL_FALSE;
1309
1310 int32_t depthFormat;
1311 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001312 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1314 }
1315
1316 // FIXME: we don't have access to the pixelFormat here just yet.
1317 // (it's possible that the surface is not fully initialized)
1318 // maybe this should be done after the page-flip
1319 //if (EGLint(info.format) != pixelFormat)
1320 // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1321
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001322 egl_surface_t* surface;
1323 surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -07001324 static_cast<ANativeWindow*>(window));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325
Mathias Agopian0696a572009-08-20 00:12:56 -07001326 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 // there was a problem in the ctor, the error
1328 // flag has been set.
1329 delete surface;
1330 surface = 0;
1331 }
1332 return surface;
1333}
1334
1335static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001336 NativePixmapType pixmap, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337{
1338 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1339 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1340 if (pixmap == 0)
1341 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1342
1343 EGLint surfaceType;
1344 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1345 return EGL_FALSE;
1346
1347 if (!(surfaceType & EGL_PIXMAP_BIT))
1348 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1349
Mathias Agopian0696a572009-08-20 00:12:56 -07001350 if (static_cast<egl_native_pixmap_t*>(pixmap)->version !=
1351 sizeof(egl_native_pixmap_t)) {
1352 return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
1353 }
1354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 EGLint configID;
1356 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1357 return EGL_FALSE;
1358
1359 int32_t depthFormat;
1360 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001361 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1363 }
1364
1365 if (pixmap->format != pixelFormat)
1366 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1367
1368 egl_surface_t* surface =
1369 new egl_pixmap_surface_t(dpy, config, depthFormat,
1370 static_cast<egl_native_pixmap_t*>(pixmap));
1371
Mathias Agopian0696a572009-08-20 00:12:56 -07001372 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 // there was a problem in the ctor, the error
1374 // flag has been set.
1375 delete surface;
1376 surface = 0;
1377 }
1378 return surface;
1379}
1380
1381static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
1382 const EGLint *attrib_list)
1383{
1384 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1385 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1386
1387 EGLint surfaceType;
1388 if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
1389 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 if (!(surfaceType & EGL_PBUFFER_BIT))
1392 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 EGLint configID;
1395 if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
1396 return EGL_FALSE;
1397
1398 int32_t depthFormat;
1399 int32_t pixelFormat;
Mathias Agopian8b6b95a2009-11-03 16:17:55 -08001400 if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
1402 }
1403
1404 int32_t w = 0;
1405 int32_t h = 0;
Jesse Hall2e8ca9d2015-08-21 07:41:46 -07001406 while (attrib_list[0] != EGL_NONE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1];
1408 if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
1409 attrib_list+=2;
1410 }
1411
1412 egl_surface_t* surface =
1413 new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
1414
Mathias Agopian0696a572009-08-20 00:12:56 -07001415 if (!surface->initCheck()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 // there was a problem in the ctor, the error
1417 // flag has been set.
1418 delete surface;
1419 surface = 0;
1420 }
1421 return surface;
1422}
1423
1424// ----------------------------------------------------------------------------
1425}; // namespace android
1426// ----------------------------------------------------------------------------
1427
1428using namespace android;
1429
1430// ----------------------------------------------------------------------------
1431// Initialization
1432// ----------------------------------------------------------------------------
1433
1434EGLDisplay eglGetDisplay(NativeDisplayType display)
1435{
Elliott Hughes6071da72015-08-12 15:27:47 -07001436#ifndef __ANDROID__
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 // this just needs to be done once
1438 if (gGLKey == -1) {
1439 pthread_mutex_lock(&gInitMutex);
1440 if (gGLKey == -1)
1441 pthread_key_create(&gGLKey, NULL);
1442 pthread_mutex_unlock(&gInitMutex);
1443 }
1444#endif
1445 if (display == EGL_DEFAULT_DISPLAY) {
1446 EGLDisplay dpy = (EGLDisplay)1;
1447 egl_display_t& d = egl_display_t::get_display(dpy);
1448 d.type = display;
1449 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001450 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 return EGL_NO_DISPLAY;
1452}
1453
1454EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
1455{
1456 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1457 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 EGLBoolean res = EGL_TRUE;
1460 egl_display_t& d = egl_display_t::get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001461
Jesse Hall3aa75f92016-05-20 10:47:07 -07001462 if (d.initialized.fetch_add(1, std::memory_order_acquire) == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 // initialize stuff here if needed
1464 //pthread_mutex_lock(&gInitMutex);
1465 //pthread_mutex_unlock(&gInitMutex);
1466 }
1467
1468 if (res == EGL_TRUE) {
1469 if (major != NULL) *major = VERSION_MAJOR;
1470 if (minor != NULL) *minor = VERSION_MINOR;
1471 }
1472 return res;
1473}
1474
1475EGLBoolean eglTerminate(EGLDisplay dpy)
1476{
1477 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1478 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1479
1480 EGLBoolean res = EGL_TRUE;
1481 egl_display_t& d = egl_display_t::get_display(dpy);
Jesse Hall3aa75f92016-05-20 10:47:07 -07001482 if (d.initialized.fetch_sub(1, std::memory_order_release) == 1) {
1483 std::atomic_thread_fence(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 // TODO: destroy all resources (surfaces, contexts, etc...)
1485 //pthread_mutex_lock(&gInitMutex);
1486 //pthread_mutex_unlock(&gInitMutex);
1487 }
1488 return res;
1489}
1490
1491// ----------------------------------------------------------------------------
1492// configuration
1493// ----------------------------------------------------------------------------
1494
1495EGLBoolean eglGetConfigs( EGLDisplay dpy,
1496 EGLConfig *configs,
1497 EGLint config_size, EGLint *num_config)
1498{
1499 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1500 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1501
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001502 if (ggl_unlikely(num_config==NULL))
1503 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 GLint numConfigs = NELEM(gConfigs);
1506 if (!configs) {
1507 *num_config = numConfigs;
1508 return EGL_TRUE;
1509 }
1510 GLint i;
1511 for (i=0 ; i<numConfigs && i<config_size ; i++) {
Colin Cross444839b2014-01-24 14:35:39 -08001512 *configs++ = (EGLConfig)(uintptr_t)i;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 }
1514 *num_config = i;
1515 return EGL_TRUE;
1516}
1517
1518EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1519 EGLConfig *configs, EGLint config_size,
1520 EGLint *num_config)
1521{
1522 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1523 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pauli Nieminend8cf2ce2012-01-04 16:25:10 +02001524
1525 if (ggl_unlikely(num_config==NULL)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001526 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1527 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528
Jack Palevich749c63d2009-03-25 15:12:17 -07001529 if (ggl_unlikely(attrib_list==0)) {
Mathias Agopian04aed212010-05-17 14:45:43 -07001530 /*
1531 * A NULL attrib_list should be treated as though it was an empty
1532 * one (terminated with EGL_NONE) as defined in
1533 * section 3.4.1 "Querying Configurations" in the EGL specification.
1534 */
1535 static const EGLint dummy = EGL_NONE;
1536 attrib_list = &dummy;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539 int numAttributes = 0;
1540 int numConfigs = NELEM(gConfigs);
1541 uint32_t possibleMatch = (1<<numConfigs)-1;
1542 while(possibleMatch && *attrib_list != EGL_NONE) {
1543 numAttributes++;
1544 EGLint attr = *attrib_list++;
1545 EGLint val = *attrib_list++;
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001546 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 if (!(possibleMatch & (1<<i)))
1548 continue;
1549 if (isAttributeMatching(i, attr, val) == 0) {
1550 possibleMatch &= ~(1<<i);
1551 }
1552 }
1553 }
1554
1555 // now, handle the attributes which have a useful default value
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001556 for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
1557 // see if this attribute was specified, if not, apply its
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 // default value
1559 if (binarySearch<config_pair_t>(
1560 (config_pair_t const*)attrib_list,
Mathias Agopiandacd7a32009-07-09 17:33:15 -07001561 0, numAttributes-1,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 config_defaults[j].key) < 0)
1563 {
Mathias Agopian0985f6a2009-10-19 14:46:27 -07001564 for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 if (!(possibleMatch & (1<<i)))
1566 continue;
1567 if (isAttributeMatching(i,
1568 config_defaults[j].key,
1569 config_defaults[j].value) == 0)
1570 {
1571 possibleMatch &= ~(1<<i);
1572 }
1573 }
1574 }
1575 }
1576
1577 // return the configurations found
1578 int n=0;
1579 if (possibleMatch) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001580 if (configs) {
1581 for (int i=0 ; config_size && i<numConfigs ; i++) {
1582 if (possibleMatch & (1<<i)) {
Colin Cross444839b2014-01-24 14:35:39 -08001583 *configs++ = (EGLConfig)(uintptr_t)i;
Jack Palevich749c63d2009-03-25 15:12:17 -07001584 config_size--;
1585 n++;
1586 }
1587 }
1588 } else {
1589 for (int i=0 ; i<numConfigs ; i++) {
1590 if (possibleMatch & (1<<i)) {
1591 n++;
1592 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593 }
1594 }
1595 }
1596 *num_config = n;
1597 return EGL_TRUE;
1598}
1599
1600EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1601 EGLint attribute, EGLint *value)
1602{
1603 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1604 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1605
1606 return getConfigAttrib(dpy, config, attribute, value);
1607}
1608
1609// ----------------------------------------------------------------------------
1610// surfaces
1611// ----------------------------------------------------------------------------
1612
1613EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1614 NativeWindowType window,
1615 const EGLint *attrib_list)
1616{
1617 return createWindowSurface(dpy, config, window, attrib_list);
1618}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001619
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1621 NativePixmapType pixmap,
1622 const EGLint *attrib_list)
1623{
1624 return createPixmapSurface(dpy, config, pixmap, attrib_list);
1625}
1626
1627EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1628 const EGLint *attrib_list)
1629{
1630 return createPbufferSurface(dpy, config, attrib_list);
1631}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001632
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
1634{
1635 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1636 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1637 if (eglSurface != EGL_NO_SURFACE) {
1638 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
Mathias Agopian0696a572009-08-20 00:12:56 -07001639 if (!surface->isValid())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1641 if (surface->dpy != dpy)
1642 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopiane71212b2009-05-05 00:37:46 -07001643 if (surface->ctx) {
Jesse Hall78141e32013-03-07 09:56:26 -08001644 // defer disconnect/delete until no longer current
1645 surface->zombie = true;
1646 } else {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001647 surface->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001648 delete surface;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 }
1651 return EGL_TRUE;
1652}
1653
1654EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
1655 EGLint attribute, EGLint *value)
1656{
1657 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1658 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1659 egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
Mathias Agopian0696a572009-08-20 00:12:56 -07001660 if (!surface->isValid())
1661 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 if (surface->dpy != dpy)
1663 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1664
1665 EGLBoolean ret = EGL_TRUE;
1666 switch (attribute) {
1667 case EGL_CONFIG_ID:
1668 ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
1669 break;
1670 case EGL_WIDTH:
1671 *value = surface->getWidth();
1672 break;
1673 case EGL_HEIGHT:
1674 *value = surface->getHeight();
1675 break;
1676 case EGL_LARGEST_PBUFFER:
1677 // not modified for a window or pixmap surface
1678 break;
1679 case EGL_TEXTURE_FORMAT:
1680 *value = EGL_NO_TEXTURE;
1681 break;
1682 case EGL_TEXTURE_TARGET:
1683 *value = EGL_NO_TEXTURE;
1684 break;
1685 case EGL_MIPMAP_TEXTURE:
1686 *value = EGL_FALSE;
1687 break;
1688 case EGL_MIPMAP_LEVEL:
1689 *value = 0;
1690 break;
1691 case EGL_RENDER_BUFFER:
1692 // TODO: return the real RENDER_BUFFER here
1693 *value = EGL_BACK_BUFFER;
1694 break;
1695 case EGL_HORIZONTAL_RESOLUTION:
1696 // pixel/mm * EGL_DISPLAY_SCALING
1697 *value = surface->getHorizontalResolution();
1698 break;
1699 case EGL_VERTICAL_RESOLUTION:
1700 // pixel/mm * EGL_DISPLAY_SCALING
1701 *value = surface->getVerticalResolution();
1702 break;
1703 case EGL_PIXEL_ASPECT_RATIO: {
1704 // w/h * EGL_DISPLAY_SCALING
1705 int wr = surface->getHorizontalResolution();
1706 int hr = surface->getVerticalResolution();
1707 *value = (wr * EGL_DISPLAY_SCALING) / hr;
1708 } break;
1709 case EGL_SWAP_BEHAVIOR:
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001710 *value = surface->getSwapBehavior();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 break;
1712 default:
1713 ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1714 }
1715 return ret;
1716}
1717
1718EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001719 EGLContext /*share_list*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720{
1721 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1722 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
1723
1724 ogles_context_t* gl = ogles_init(sizeof(egl_context_t));
1725 if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1726
1727 egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
1728 c->flags = egl_context_t::NEVER_CURRENT;
1729 c->dpy = dpy;
1730 c->config = config;
1731 c->read = 0;
1732 c->draw = 0;
1733 return (EGLContext)gl;
1734}
1735
1736EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1737{
1738 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1739 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1740 egl_context_t* c = egl_context_t::context(ctx);
1741 if (c->flags & egl_context_t::IS_CURRENT)
1742 setGlThreadSpecific(0);
1743 ogles_uninit((ogles_context_t*)ctx);
1744 return EGL_TRUE;
1745}
1746
1747EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1748 EGLSurface read, EGLContext ctx)
1749{
1750 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1751 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1752 if (draw) {
1753 egl_surface_t* s = (egl_surface_t*)draw;
Mathias Agopian0696a572009-08-20 00:12:56 -07001754 if (!s->isValid())
1755 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756 if (s->dpy != dpy)
1757 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian0696a572009-08-20 00:12:56 -07001758 // TODO: check that draw is compatible with the context
1759 }
1760 if (read && read!=draw) {
1761 egl_surface_t* s = (egl_surface_t*)read;
1762 if (!s->isValid())
1763 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1764 if (s->dpy != dpy)
1765 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1766 // TODO: check that read is compatible with the context
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 }
1768
1769 EGLContext current_ctx = EGL_NO_CONTEXT;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1772 return setError(EGL_BAD_MATCH, EGL_FALSE);
1773
1774 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1775 return setError(EGL_BAD_MATCH, EGL_FALSE);
1776
1777 if (ctx == EGL_NO_CONTEXT) {
1778 // if we're detaching, we need the current context
1779 current_ctx = (EGLContext)getGlThreadSpecific();
1780 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 egl_surface_t* d = (egl_surface_t*)draw;
1782 egl_surface_t* r = (egl_surface_t*)read;
1783 if ((d && d->ctx && d->ctx != ctx) ||
1784 (r && r->ctx && r->ctx != ctx)) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001785 // one of the surface is bound to a context in another thread
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1787 }
1788 }
1789
1790 ogles_context_t* gl = (ogles_context_t*)ctx;
1791 if (makeCurrent(gl) == 0) {
1792 if (ctx) {
1793 egl_context_t* c = egl_context_t::context(ctx);
1794 egl_surface_t* d = (egl_surface_t*)draw;
1795 egl_surface_t* r = (egl_surface_t*)read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001796
1797 if (c->draw) {
Mathias Agopian0696a572009-08-20 00:12:56 -07001798 egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
1799 s->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001800 s->ctx = EGL_NO_CONTEXT;
1801 if (s->zombie)
1802 delete s;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001803 }
1804 if (c->read) {
1805 // FIXME: unlock/disconnect the read surface too
1806 }
1807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001808 c->draw = draw;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001809 c->read = read;
1810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 if (c->flags & egl_context_t::NEVER_CURRENT) {
1812 c->flags &= ~egl_context_t::NEVER_CURRENT;
1813 GLint w = 0;
1814 GLint h = 0;
1815 if (draw) {
1816 w = d->getWidth();
1817 h = d->getHeight();
1818 }
1819 ogles_surfaceport(gl, 0, 0);
1820 ogles_viewport(gl, 0, 0, w, h);
1821 ogles_scissor(gl, 0, 0, w, h);
1822 }
1823 if (d) {
Mathias Agopiancf81c842009-07-31 14:47:00 -07001824 if (d->connect() == EGL_FALSE) {
1825 return EGL_FALSE;
1826 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827 d->ctx = ctx;
1828 d->bindDrawSurface(gl);
1829 }
1830 if (r) {
Mathias Agopiane71212b2009-05-05 00:37:46 -07001831 // FIXME: lock/connect the read surface too
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 r->ctx = ctx;
1833 r->bindReadSurface(gl);
1834 }
1835 } else {
1836 // if surfaces were bound to the context bound to this thread
1837 // mark then as unbound.
1838 if (current_ctx) {
1839 egl_context_t* c = egl_context_t::context(current_ctx);
1840 egl_surface_t* d = (egl_surface_t*)c->draw;
1841 egl_surface_t* r = (egl_surface_t*)c->read;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001842 if (d) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001843 c->draw = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001844 d->disconnect();
Jesse Hall78141e32013-03-07 09:56:26 -08001845 d->ctx = EGL_NO_CONTEXT;
1846 if (d->zombie)
1847 delete d;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001848 }
1849 if (r) {
Mathias Agopian9648c1a2009-06-03 19:00:53 -07001850 c->read = 0;
Mathias Agopiane71212b2009-05-05 00:37:46 -07001851 r->ctx = EGL_NO_CONTEXT;
1852 // FIXME: unlock/disconnect the read surface too
1853 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 }
1855 }
1856 return EGL_TRUE;
1857 }
1858 return setError(EGL_BAD_ACCESS, EGL_FALSE);
1859}
1860
1861EGLContext eglGetCurrentContext(void)
1862{
1863 // eglGetCurrentContext returns the current EGL rendering context,
1864 // as specified by eglMakeCurrent. If no context is current,
1865 // EGL_NO_CONTEXT is returned.
1866 return (EGLContext)getGlThreadSpecific();
1867}
1868
1869EGLSurface eglGetCurrentSurface(EGLint readdraw)
1870{
1871 // eglGetCurrentSurface returns the read or draw surface attached
1872 // to the current EGL rendering context, as specified by eglMakeCurrent.
1873 // If no context is current, EGL_NO_SURFACE is returned.
1874 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1875 if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
1876 egl_context_t* c = egl_context_t::context(ctx);
1877 if (readdraw == EGL_READ) {
1878 return c->read;
1879 } else if (readdraw == EGL_DRAW) {
1880 return c->draw;
1881 }
1882 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1883}
1884
1885EGLDisplay eglGetCurrentDisplay(void)
1886{
1887 // eglGetCurrentDisplay returns the current EGL display connection
1888 // for the current EGL rendering context, as specified by eglMakeCurrent.
1889 // If no context is current, EGL_NO_DISPLAY is returned.
1890 EGLContext ctx = (EGLContext)getGlThreadSpecific();
1891 if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
1892 egl_context_t* c = egl_context_t::context(ctx);
1893 return c->dpy;
1894}
1895
1896EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1897 EGLint attribute, EGLint *value)
1898{
1899 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1900 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1901 egl_context_t* c = egl_context_t::context(ctx);
1902 switch (attribute) {
1903 case EGL_CONFIG_ID:
1904 // Returns the ID of the EGL frame buffer configuration with
1905 // respect to which the context was created
1906 return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
1907 }
1908 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1909}
1910
1911EGLBoolean eglWaitGL(void)
1912{
1913 return EGL_TRUE;
1914}
1915
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001916EGLBoolean eglWaitNative(EGLint /*engine*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917{
1918 return EGL_TRUE;
1919}
1920
1921EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1922{
1923 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1924 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07001927 if (!d->isValid())
1928 return setError(EGL_BAD_SURFACE, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929 if (d->dpy != dpy)
1930 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1931
1932 // post the surface
1933 d->swapBuffers();
1934
1935 // if it's bound to a context, update the buffer
1936 if (d->ctx != EGL_NO_CONTEXT) {
1937 d->bindDrawSurface((ogles_context_t*)d->ctx);
1938 // if this surface is also the read surface of the context
1939 // it is bound to, make sure to update the read buffer as well.
1940 // The EGL spec is a little unclear about this.
1941 egl_context_t* c = egl_context_t::context(d->ctx);
1942 if (c->read == draw) {
1943 d->bindReadSurface((ogles_context_t*)d->ctx);
1944 }
1945 }
1946
1947 return EGL_TRUE;
1948}
1949
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001950EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface /*surface*/,
1951 NativePixmapType /*target*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952{
1953 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1954 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1955 // TODO: eglCopyBuffers()
1956 return EGL_FALSE;
1957}
1958
1959EGLint eglGetError(void)
1960{
1961 return getError();
1962}
1963
1964const char* eglQueryString(EGLDisplay dpy, EGLint name)
1965{
1966 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1967 return setError(EGL_BAD_DISPLAY, (const char*)0);
1968
1969 switch (name) {
1970 case EGL_VENDOR:
1971 return gVendorString;
1972 case EGL_VERSION:
1973 return gVersionString;
1974 case EGL_EXTENSIONS:
1975 return gExtensionsString;
1976 case EGL_CLIENT_APIS:
1977 return gClientApiString;
1978 }
1979 return setError(EGL_BAD_PARAMETER, (const char *)0);
1980}
1981
1982// ----------------------------------------------------------------------------
1983// EGL 1.1
1984// ----------------------------------------------------------------------------
1985
1986EGLBoolean eglSurfaceAttrib(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001987 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*attribute*/, EGLint /*value*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001988{
1989 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1990 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1991 // TODO: eglSurfaceAttrib()
1992 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1993}
1994
1995EGLBoolean eglBindTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001996 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997{
1998 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
1999 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2000 // TODO: eglBindTexImage()
2001 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2002}
2003
2004EGLBoolean eglReleaseTexImage(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002005 EGLDisplay dpy, EGLSurface /*surface*/, EGLint /*buffer*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002006{
2007 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2008 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2009 // TODO: eglReleaseTexImage()
2010 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2011}
2012
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002013EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint /*interval*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002014{
2015 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2016 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2017 // TODO: eglSwapInterval()
Ari Hirvonen551dc262010-10-01 19:00:54 +03002018 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019}
2020
2021// ----------------------------------------------------------------------------
2022// EGL 1.2
2023// ----------------------------------------------------------------------------
2024
2025EGLBoolean eglBindAPI(EGLenum api)
2026{
2027 if (api != EGL_OPENGL_ES_API)
2028 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2029 return EGL_TRUE;
2030}
2031
2032EGLenum eglQueryAPI(void)
2033{
2034 return EGL_OPENGL_ES_API;
2035}
2036
2037EGLBoolean eglWaitClient(void)
2038{
2039 glFinish();
2040 return EGL_TRUE;
2041}
2042
2043EGLBoolean eglReleaseThread(void)
2044{
2045 // TODO: eglReleaseThread()
2046 return EGL_TRUE;
2047}
2048
2049EGLSurface eglCreatePbufferFromClientBuffer(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002050 EGLDisplay dpy, EGLenum /*buftype*/, EGLClientBuffer /*buffer*/,
2051 EGLConfig /*config*/, const EGLint* /*attrib_list*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002052{
2053 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2054 return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
2055 // TODO: eglCreatePbufferFromClientBuffer()
2056 return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
2057}
2058
2059// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002060// EGL_EGLEXT_VERSION 3
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002061// ----------------------------------------------------------------------------
2062
2063void (*eglGetProcAddress (const char *procname))()
2064{
2065 extention_map_t const * const map = gExtentionMap;
2066 for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
2067 if (!strcmp(procname, map[i].name)) {
2068 return map[i].address;
2069 }
2070 }
2071 return NULL;
2072}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002073
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002074EGLBoolean eglLockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/,
2075 const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002076{
2077 EGLBoolean result = EGL_FALSE;
2078 return result;
2079}
2080
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002081EGLBoolean eglUnlockSurfaceKHR(EGLDisplay /*dpy*/, EGLSurface /*surface*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002082{
2083 EGLBoolean result = EGL_FALSE;
2084 return result;
2085}
2086
2087EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002088 EGLClientBuffer buffer, const EGLint* /*attrib_list*/)
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002089{
2090 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2091 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
2092 }
2093 if (ctx != EGL_NO_CONTEXT) {
2094 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
2095 }
2096 if (target != EGL_NATIVE_BUFFER_ANDROID) {
2097 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2098 }
2099
Iliyan Malchev697526b2011-05-01 11:33:26 -07002100 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002101
2102 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2103 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2104
Iliyan Malchev697526b2011-05-01 11:33:26 -07002105 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002106 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
Mathias Agopian8dccb262010-02-04 17:04:53 -08002107
2108 switch (native_buffer->format) {
2109 case HAL_PIXEL_FORMAT_RGBA_8888:
2110 case HAL_PIXEL_FORMAT_RGBX_8888:
2111 case HAL_PIXEL_FORMAT_RGB_888:
2112 case HAL_PIXEL_FORMAT_RGB_565:
2113 case HAL_PIXEL_FORMAT_BGRA_8888:
Mathias Agopian8dccb262010-02-04 17:04:53 -08002114 break;
2115 default:
2116 return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
2117 }
2118
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002119 native_buffer->common.incRef(&native_buffer->common);
2120 return (EGLImageKHR)native_buffer;
2121}
2122
2123EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2124{
2125 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2126 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2127 }
2128
Iliyan Malchev697526b2011-05-01 11:33:26 -07002129 ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002130
2131 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
2132 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2133
Iliyan Malchev697526b2011-05-01 11:33:26 -07002134 if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002135 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2136
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002137 native_buffer->common.decRef(&native_buffer->common);
2138
2139 return EGL_TRUE;
2140}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002141
2142// ----------------------------------------------------------------------------
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002143// EGL_KHR_fence_sync
2144// ----------------------------------------------------------------------------
2145
2146#define FENCE_SYNC_HANDLE ((EGLSyncKHR)0xFE4CE)
2147
2148EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
2149 const EGLint *attrib_list)
2150{
2151 if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
2152 return setError(EGL_BAD_DISPLAY, EGL_NO_SYNC_KHR);
2153 }
2154
2155 if (type != EGL_SYNC_FENCE_KHR ||
2156 (attrib_list != NULL && attrib_list[0] != EGL_NONE)) {
2157 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
2158 }
2159
2160 if (eglGetCurrentContext() == EGL_NO_CONTEXT) {
2161 return setError(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
2162 }
2163
2164 // AGL is synchronous; nothing to do here.
2165
2166 return FENCE_SYNC_HANDLE;
2167}
2168
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002169EGLBoolean eglDestroySyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002170{
2171 if (sync != FENCE_SYNC_HANDLE) {
2172 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2173 }
2174
2175 return EGL_TRUE;
2176}
2177
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002178EGLint eglClientWaitSyncKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync, EGLint /*flags*/,
2179 EGLTimeKHR /*timeout*/)
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002180{
2181 if (sync != FENCE_SYNC_HANDLE) {
2182 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2183 }
2184
2185 return EGL_CONDITION_SATISFIED_KHR;
2186}
2187
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07002188EGLBoolean eglGetSyncAttribKHR(EGLDisplay /*dpy*/, EGLSyncKHR sync,
Jesse Hall83e7c8c2012-05-22 10:42:56 -07002189 EGLint attribute, EGLint *value)
2190{
2191 if (sync != FENCE_SYNC_HANDLE) {
2192 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2193 }
2194
2195 switch (attribute) {
2196 case EGL_SYNC_TYPE_KHR:
2197 *value = EGL_SYNC_FENCE_KHR;
2198 return EGL_TRUE;
2199 case EGL_SYNC_STATUS_KHR:
2200 *value = EGL_SIGNALED_KHR;
2201 return EGL_TRUE;
2202 case EGL_SYNC_CONDITION_KHR:
2203 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
2204 return EGL_TRUE;
2205 default:
2206 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
2207 }
2208}
2209
2210// ----------------------------------------------------------------------------
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002211// ANDROID extensions
2212// ----------------------------------------------------------------------------
2213
2214EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2215 EGLint left, EGLint top, EGLint width, EGLint height)
2216{
2217 if (egl_display_t::is_valid(dpy) == EGL_FALSE)
2218 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2219
2220 egl_surface_t* d = static_cast<egl_surface_t*>(draw);
Mathias Agopian0696a572009-08-20 00:12:56 -07002221 if (!d->isValid())
2222 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002223 if (d->dpy != dpy)
2224 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2225
2226 // post the surface
2227 d->setSwapRectangle(left, top, width, height);
2228
2229 return EGL_TRUE;
2230}