1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <dlfcn.h>
#include <android/log.h>
#include <GLES2/gl2.h>
#include "AndroidGraphicBuffer.h"
#include "AndroidBridge.h"
#include "mozilla/Preferences.h"
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AndroidGraphicBuffer" , ## args)
#define EGL_NATIVE_BUFFER_ANDROID 0x3140
#define EGL_IMAGE_PRESERVED_KHR 0x30D2
typedef void *EGLContext;
typedef void *EGLDisplay;
typedef uint32_t EGLenum;
typedef int32_t EGLint;
typedef uint32_t EGLBoolean;
typedef gfxASurface::gfxImageFormat gfxImageFormat;
#define EGL_TRUE 1
#define EGL_FALSE 0
#define EGL_NONE 0x3038
#define EGL_NO_CONTEXT (EGLContext)0
#define EGL_DEFAULT_DISPLAY (void*)0
#define ANDROID_LIBUI_PATH "libui.so"
#define ANDROID_GLES_PATH "libGLESv2.so"
#define ANDROID_EGL_PATH "libEGL.so"
// Really I have no idea, but this should be big enough
#define GRAPHIC_BUFFER_SIZE 1024
enum {
/* buffer is never read in software */
GRALLOC_USAGE_SW_READ_NEVER = 0x00000000,
/* buffer is rarely read in software */
GRALLOC_USAGE_SW_READ_RARELY = 0x00000002,
/* buffer is often read in software */
GRALLOC_USAGE_SW_READ_OFTEN = 0x00000003,
/* mask for the software read values */
GRALLOC_USAGE_SW_READ_MASK = 0x0000000F,
/* buffer is never written in software */
GRALLOC_USAGE_SW_WRITE_NEVER = 0x00000000,
/* buffer is never written in software */
GRALLOC_USAGE_SW_WRITE_RARELY = 0x00000020,
/* buffer is never written in software */
GRALLOC_USAGE_SW_WRITE_OFTEN = 0x00000030,
/* mask for the software write values */
GRALLOC_USAGE_SW_WRITE_MASK = 0x000000F0,
/* buffer will be used as an OpenGL ES texture */
GRALLOC_USAGE_HW_TEXTURE = 0x00000100,
/* buffer will be used as an OpenGL ES render target */
GRALLOC_USAGE_HW_RENDER = 0x00000200,
/* buffer will be used by the 2D hardware blitter */
GRALLOC_USAGE_HW_2D = 0x00000400,
/* buffer will be used with the framebuffer device */
GRALLOC_USAGE_HW_FB = 0x00001000,
/* mask for the software usage bit-mask */
GRALLOC_USAGE_HW_MASK = 0x00001F00,
};
enum {
HAL_PIXEL_FORMAT_RGBA_8888 = 1,
HAL_PIXEL_FORMAT_RGBX_8888 = 2,
HAL_PIXEL_FORMAT_RGB_888 = 3,
HAL_PIXEL_FORMAT_RGB_565 = 4,
HAL_PIXEL_FORMAT_BGRA_8888 = 5,
HAL_PIXEL_FORMAT_RGBA_5551 = 6,
HAL_PIXEL_FORMAT_RGBA_4444 = 7,
};
typedef struct ARect {
int32_t left;
int32_t top;
int32_t right;
int32_t bottom;
} ARect;
static bool gTryRealloc = true;
static class GLFunctions
{
public:
GLFunctions() : mInitialized(false)
{
}
typedef EGLDisplay (* pfnGetDisplay)(void *display_id);
pfnGetDisplay fGetDisplay;
typedef EGLint (* pfnEGLGetError)(void);
pfnEGLGetError fEGLGetError;
typedef EGLImageKHR (* pfnCreateImageKHR)(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list);
pfnCreateImageKHR fCreateImageKHR;
typedef EGLBoolean (* pfnDestroyImageKHR)(EGLDisplay dpy, EGLImageKHR image);
pfnDestroyImageKHR fDestroyImageKHR;
typedef void (* pfnImageTargetTexture2DOES)(GLenum target, EGLImageKHR image);
pfnImageTargetTexture2DOES fImageTargetTexture2DOES;
typedef void (* pfnBindTexture)(GLenum target, GLuint texture);
pfnBindTexture fBindTexture;
typedef GLenum (* pfnGLGetError)();
pfnGLGetError fGLGetError;
typedef void (*pfnGraphicBufferCtor)(void*, uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
pfnGraphicBufferCtor fGraphicBufferCtor;
typedef void (*pfnGraphicBufferDtor)(void*);
pfnGraphicBufferDtor fGraphicBufferDtor;
typedef int (*pfnGraphicBufferLock)(void*, uint32_t usage, unsigned char **addr);
pfnGraphicBufferLock fGraphicBufferLock;
typedef int (*pfnGraphicBufferLockRect)(void*, uint32_t usage, const ARect&, unsigned char **addr);
pfnGraphicBufferLockRect fGraphicBufferLockRect;
typedef int (*pfnGraphicBufferUnlock)(void*);
pfnGraphicBufferUnlock fGraphicBufferUnlock;
typedef void* (*pfnGraphicBufferGetNativeBuffer)(void*);
pfnGraphicBufferGetNativeBuffer fGraphicBufferGetNativeBuffer;
typedef int (*pfnGraphicBufferReallocate)(void*, uint32_t w, uint32_t h, uint32_t format);
pfnGraphicBufferReallocate fGraphicBufferReallocate;
bool EnsureInitialized()
{
if (mInitialized) {
return true;
}
void *handle = dlopen(ANDROID_EGL_PATH, RTLD_LAZY);
if (!handle) {
LOG("Couldn't load EGL library");
return false;
}
fGetDisplay = (pfnGetDisplay)dlsym(handle, "eglGetDisplay");
fEGLGetError = (pfnEGLGetError)dlsym(handle, "eglGetError");
fCreateImageKHR = (pfnCreateImageKHR)dlsym(handle, "eglCreateImageKHR");
fDestroyImageKHR = (pfnDestroyImageKHR)dlsym(handle, "eglDestroyImageKHR");
if (!fGetDisplay || !fEGLGetError || !fCreateImageKHR || !fDestroyImageKHR) {
LOG("Failed to find some EGL functions");
return false;
}
handle = dlopen(ANDROID_GLES_PATH, RTLD_LAZY);
if (!handle) {
LOG("Couldn't load GL library");
return false;
}
fImageTargetTexture2DOES = (pfnImageTargetTexture2DOES)dlsym(handle, "glEGLImageTargetTexture2DOES");
fBindTexture = (pfnBindTexture)dlsym(handle, "glBindTexture");
fGLGetError = (pfnGLGetError)dlsym(handle, "glGetError");
if (!fImageTargetTexture2DOES || !fBindTexture || !fGLGetError) {
LOG("Failed to find some GL functions");
return false;
}
handle = dlopen(ANDROID_LIBUI_PATH, RTLD_LAZY);
if (!handle) {
LOG("Couldn't load libui.so");
return false;
}
fGraphicBufferCtor = (pfnGraphicBufferCtor)dlsym(handle, "_ZN7android13GraphicBufferC1Ejjij");
fGraphicBufferDtor = (pfnGraphicBufferDtor)dlsym(handle, "_ZN7android13GraphicBufferD1Ev");
fGraphicBufferLock = (pfnGraphicBufferLock)dlsym(handle, "_ZN7android13GraphicBuffer4lockEjPPv");
fGraphicBufferLockRect = (pfnGraphicBufferLockRect)dlsym(handle, "_ZN7android13GraphicBuffer4lockEjRKNS_4RectEPPv");
fGraphicBufferUnlock = (pfnGraphicBufferUnlock)dlsym(handle, "_ZN7android13GraphicBuffer6unlockEv");
fGraphicBufferGetNativeBuffer = (pfnGraphicBufferGetNativeBuffer)dlsym(handle, "_ZNK7android13GraphicBuffer15getNativeBufferEv");
fGraphicBufferReallocate = (pfnGraphicBufferReallocate)dlsym(handle, "_ZN7android13GraphicBuffer10reallocateEjjij");
if (!fGraphicBufferCtor || !fGraphicBufferDtor || !fGraphicBufferLock ||
!fGraphicBufferUnlock || !fGraphicBufferGetNativeBuffer) {
LOG("Failed to lookup some GraphicBuffer functions");
return false;
}
mInitialized = true;
return true;
}
private:
bool mInitialized;
} sGLFunctions;
namespace mozilla {
static void clearGLError()
{
while (glGetError() != GL_NO_ERROR);
}
static bool ensureNoGLError(const char* name)
{
bool result = true;
GLuint error;
while ((error = glGetError()) != GL_NO_ERROR) {
LOG("GL error [%s]: %40x\n", name, error);
result = false;
}
return result;
}
AndroidGraphicBuffer::AndroidGraphicBuffer(uint32_t width, uint32_t height, uint32_t usage,
gfxImageFormat format) :
mWidth(width)
, mHeight(height)
, mUsage(usage)
, mFormat(format)
, mHandle(0)
, mEGLImage(0)
{
}
AndroidGraphicBuffer::~AndroidGraphicBuffer()
{
DestroyBuffer();
}
void
AndroidGraphicBuffer::DestroyBuffer()
{
/**
* XXX: eglDestroyImageKHR crashes sometimes due to refcount badness (I think)
*
* If you look at egl.cpp (https://github.com/android/platform_frameworks_base/blob/master/opengl/libagl/egl.cpp#L2002)
* you can see that eglCreateImageKHR just refs the native buffer, and eglDestroyImageKHR
* just unrefs it. Somehow the ref count gets messed up and things are already destroyed
* by the time eglDestroyImageKHR gets called. For now, at least, just not calling
* eglDestroyImageKHR should be fine since we do free the GraphicBuffer below.
*
* Bug 712716
*/
#if 0
if (mEGLImage) {
if (sGLFunctions.EnsureInitialized()) {
sGLFunctions.fDestroyImageKHR(sGLFunctions.fGetDisplay(EGL_DEFAULT_DISPLAY), mEGLImage);
mEGLImage = NULL;
}
}
#endif
mEGLImage = NULL;
if (mHandle) {
if (sGLFunctions.EnsureInitialized()) {
sGLFunctions.fGraphicBufferDtor(mHandle);
}
free(mHandle);
mHandle = NULL;
}
}
bool
AndroidGraphicBuffer::EnsureBufferCreated()
{
if (!mHandle) {
mHandle = malloc(GRAPHIC_BUFFER_SIZE);
sGLFunctions.fGraphicBufferCtor(mHandle, mWidth, mHeight, GetAndroidFormat(mFormat), GetAndroidUsage(mUsage));
}
return true;
}
bool
AndroidGraphicBuffer::EnsureInitialized()
{
if (!sGLFunctions.EnsureInitialized()) {
return false;
}
EnsureBufferCreated();
return true;
}
int
AndroidGraphicBuffer::Lock(uint32_t aUsage, unsigned char **bits)
{
if (!EnsureInitialized())
return true;
return sGLFunctions.fGraphicBufferLock(mHandle, GetAndroidUsage(aUsage), bits);
}
int
AndroidGraphicBuffer::Lock(uint32_t aUsage, const nsIntRect& aRect, unsigned char **bits)
{
if (!EnsureInitialized())
return false;
ARect rect;
rect.left = aRect.x;
rect.top = aRect.y;
rect.right = aRect.x + aRect.width;
rect.bottom = aRect.y + aRect.height;
return sGLFunctions.fGraphicBufferLockRect(mHandle, GetAndroidUsage(aUsage), rect, bits);
}
int
AndroidGraphicBuffer::Unlock()
{
if (!EnsureInitialized())
return false;
return sGLFunctions.fGraphicBufferUnlock(mHandle);
}
bool
AndroidGraphicBuffer::Reallocate(uint32_t aWidth, uint32_t aHeight, gfxImageFormat aFormat)
{
if (!EnsureInitialized())
return false;
mWidth = aWidth;
mHeight = aHeight;
mFormat = aFormat;
// Sometimes GraphicBuffer::reallocate just doesn't work. In those cases we'll just allocate a brand
// new buffer. If reallocate fails once, never try it again.
if (!gTryRealloc || sGLFunctions.fGraphicBufferReallocate(mHandle, aWidth, aHeight, GetAndroidFormat(aFormat)) != 0) {
DestroyBuffer();
EnsureBufferCreated();
gTryRealloc = false;
}
return true;
}
uint32_t
AndroidGraphicBuffer::GetAndroidUsage(uint32_t aUsage)
{
uint32_t flags = 0;
if (aUsage & UsageSoftwareRead) {
flags |= GRALLOC_USAGE_SW_READ_OFTEN;
}
if (aUsage & UsageSoftwareWrite) {
flags |= GRALLOC_USAGE_SW_WRITE_OFTEN;
}
if (aUsage & UsageTexture) {
flags |= GRALLOC_USAGE_HW_TEXTURE;
}
if (aUsage & UsageTarget) {
flags |= GRALLOC_USAGE_HW_RENDER;
}
if (aUsage & Usage2D) {
flags |= GRALLOC_USAGE_HW_2D;
}
return flags;
}
uint32_t
AndroidGraphicBuffer::GetAndroidFormat(gfxImageFormat aFormat)
{
switch (aFormat) {
case gfxImageFormat::ImageFormatRGB24:
return HAL_PIXEL_FORMAT_RGBX_8888;
case gfxImageFormat::ImageFormatRGB16_565:
return HAL_PIXEL_FORMAT_RGB_565;
default:
return 0;
}
}
bool
AndroidGraphicBuffer::EnsureEGLImage()
{
if (mEGLImage)
return true;
if (!EnsureInitialized())
return false;
EGLint eglImgAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE };
void* nativeBuffer = sGLFunctions.fGraphicBufferGetNativeBuffer(mHandle);
mEGLImage = sGLFunctions.fCreateImageKHR(sGLFunctions.fGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)nativeBuffer, eglImgAttrs);
return mEGLImage != NULL;
}
bool
AndroidGraphicBuffer::Bind()
{
if (!EnsureInitialized())
return false;
if (!EnsureEGLImage()) {
LOG("No valid EGLImage!");
return false;
}
clearGLError();
sGLFunctions.fImageTargetTexture2DOES(GL_TEXTURE_2D, mEGLImage);
return ensureNoGLError("glEGLImageTargetTexture2DOES");
}
static const char* const sAllowedBoards[] = {
"venus2", // Motorola Droid Pro
"tuna", // Galaxy Nexus
"omap4sdp", // Amazon Kindle Fire
"droid2", // Motorola Droid 2
"targa", // Motorola Droid Bionic
"spyder", // Motorola Razr
"shadow", // Motorola Droid X
"SGH-I897", // Samsung Galaxy S
"GT-I9100", // Samsung Galaxy SII
"sgh-i997", // Samsung Infuse 4G
"herring", // Samsung Nexus S
"sgh-t839", // Samsung Sidekick 4G
NULL
};
bool
AndroidGraphicBuffer::IsBlacklisted()
{
nsAutoString board;
if (!AndroidBridge::Bridge()->GetStaticStringField("android/os/Build", "BOARD", board))
return true;
NS_ConvertUTF16toUTF8 boardUtf8(board);
if (Preferences::GetBool("direct-texture.force.enabled", false)) {
LOG("allowing board '%s' due to prefs override", boardUtf8.get());
return false;
}
if (Preferences::GetBool("direct-texture.force.disabled", false)) {
LOG("disallowing board '%s' due to prefs override", boardUtf8.get());
return true;
}
// FIXME: (Bug 722605) use something better than a linear search
for (int i = 0; sAllowedBoards[i]; i++) {
if (board.Find(sAllowedBoards[i]) >= 0) {
LOG("allowing board '%s' based on '%s'\n", boardUtf8.get(), sAllowedBoards[i]);
return false;
}
}
LOG("disallowing board: %s\n", boardUtf8.get());
return true;
}
} /* mozilla */
|