2011-12-21 23:03:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <GLES2/gl2.h>
|
2013-10-29 00:03:12 +04:00
|
|
|
#include <nsTArray.h>
|
2011-12-21 23:03:26 +04:00
|
|
|
#include "AndroidGraphicBuffer.h"
|
2012-01-19 05:41:28 +04:00
|
|
|
#include "AndroidBridge.h"
|
2015-08-05 00:47:28 +03:00
|
|
|
#include "GLTypes.h"
|
2012-01-19 05:41:28 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AndroidGraphicBuffer" , ## args)
|
|
|
|
|
|
|
|
#define EGL_NATIVE_BUFFER_ANDROID 0x3140
|
|
|
|
#define EGL_IMAGE_PRESERVED_KHR 0x30D2
|
|
|
|
|
2012-03-14 08:15:11 +04:00
|
|
|
typedef void *EGLContext;
|
|
|
|
typedef void *EGLDisplay;
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef uint32_t EGLenum;
|
|
|
|
typedef int32_t EGLint;
|
|
|
|
typedef uint32_t EGLBoolean;
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
#define EGL_TRUE 1
|
|
|
|
#define EGL_FALSE 0
|
|
|
|
#define EGL_NONE 0x3038
|
|
|
|
#define EGL_DEFAULT_DISPLAY (void*)0
|
|
|
|
|
2012-01-13 10:23:02 +04:00
|
|
|
#define ANDROID_LIBUI_PATH "libui.so"
|
|
|
|
#define ANDROID_GLES_PATH "libGLESv2.so"
|
|
|
|
#define ANDROID_EGL_PATH "libEGL.so"
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
|
2012-01-19 05:41:28 +04:00
|
|
|
typedef struct ARect {
|
2011-12-21 23:03:26 +04:00
|
|
|
int32_t left;
|
|
|
|
int32_t top;
|
|
|
|
int32_t right;
|
|
|
|
int32_t bottom;
|
2012-01-19 05:41:28 +04:00
|
|
|
} ARect;
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
static bool gTryRealloc = true;
|
|
|
|
|
|
|
|
static class GLFunctions
|
|
|
|
{
|
|
|
|
public:
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr GLFunctions() : fGetDisplay(nullptr),
|
2013-08-12 18:58:36 +04:00
|
|
|
fEGLGetError(nullptr),
|
|
|
|
fCreateImageKHR(nullptr),
|
|
|
|
fDestroyImageKHR(nullptr),
|
|
|
|
fImageTargetTexture2DOES(nullptr),
|
|
|
|
fBindTexture(nullptr),
|
|
|
|
fGLGetError(nullptr),
|
|
|
|
fGraphicBufferCtor(nullptr),
|
|
|
|
fGraphicBufferDtor(nullptr),
|
|
|
|
fGraphicBufferLock(nullptr),
|
|
|
|
fGraphicBufferLockRect(nullptr),
|
|
|
|
fGraphicBufferUnlock(nullptr),
|
|
|
|
fGraphicBufferGetNativeBuffer(nullptr),
|
|
|
|
fGraphicBufferReallocate(nullptr),
|
|
|
|
mInitialized(false)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef void (*pfnGraphicBufferCtor)(void*, uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
|
2011-12-21 23:03:26 +04:00
|
|
|
pfnGraphicBufferCtor fGraphicBufferCtor;
|
|
|
|
|
|
|
|
typedef void (*pfnGraphicBufferDtor)(void*);
|
|
|
|
pfnGraphicBufferDtor fGraphicBufferDtor;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef int (*pfnGraphicBufferLock)(void*, uint32_t usage, unsigned char **addr);
|
2011-12-21 23:03:26 +04:00
|
|
|
pfnGraphicBufferLock fGraphicBufferLock;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef int (*pfnGraphicBufferLockRect)(void*, uint32_t usage, const ARect&, unsigned char **addr);
|
2011-12-21 23:03:26 +04:00
|
|
|
pfnGraphicBufferLockRect fGraphicBufferLockRect;
|
|
|
|
|
|
|
|
typedef int (*pfnGraphicBufferUnlock)(void*);
|
|
|
|
pfnGraphicBufferUnlock fGraphicBufferUnlock;
|
|
|
|
|
|
|
|
typedef void* (*pfnGraphicBufferGetNativeBuffer)(void*);
|
|
|
|
pfnGraphicBufferGetNativeBuffer fGraphicBufferGetNativeBuffer;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef int (*pfnGraphicBufferReallocate)(void*, uint32_t w, uint32_t h, uint32_t format);
|
2011-12-21 23:03:26 +04:00
|
|
|
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 {
|
|
|
|
|
2012-01-05 06:51:11 +04:00
|
|
|
static void clearGLError()
|
|
|
|
{
|
|
|
|
while (glGetError() != GL_NO_ERROR);
|
|
|
|
}
|
|
|
|
|
2011-12-21 23:03:26 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidGraphicBuffer::AndroidGraphicBuffer(uint32_t width, uint32_t height, uint32_t usage,
|
2011-12-21 23:03:26 +04:00
|
|
|
gfxImageFormat format) :
|
|
|
|
mWidth(width)
|
|
|
|
, mHeight(height)
|
|
|
|
, mUsage(usage)
|
|
|
|
, mFormat(format)
|
|
|
|
, mHandle(0)
|
|
|
|
, mEGLImage(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AndroidGraphicBuffer::~AndroidGraphicBuffer()
|
|
|
|
{
|
|
|
|
DestroyBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AndroidGraphicBuffer::DestroyBuffer()
|
|
|
|
{
|
2012-03-16 10:21:54 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2011-12-21 23:03:26 +04:00
|
|
|
if (mEGLImage) {
|
|
|
|
if (sGLFunctions.EnsureInitialized()) {
|
2012-03-16 10:21:54 +04:00
|
|
|
sGLFunctions.fDestroyImageKHR(sGLFunctions.fGetDisplay(EGL_DEFAULT_DISPLAY), mEGLImage);
|
2013-10-11 00:42:52 +04:00
|
|
|
mEGLImage = nullptr;
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-16 10:21:54 +04:00
|
|
|
#endif
|
2013-10-11 00:42:52 +04:00
|
|
|
mEGLImage = nullptr;
|
2011-12-21 23:03:26 +04:00
|
|
|
|
2012-03-16 10:21:54 +04:00
|
|
|
if (mHandle) {
|
|
|
|
if (sGLFunctions.EnsureInitialized()) {
|
|
|
|
sGLFunctions.fGraphicBufferDtor(mHandle);
|
|
|
|
}
|
|
|
|
free(mHandle);
|
2013-10-11 00:42:52 +04:00
|
|
|
mHandle = nullptr;
|
2012-03-16 10:21:54 +04:00
|
|
|
}
|
|
|
|
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-01-05 06:53:09 +04:00
|
|
|
AndroidGraphicBuffer::EnsureBufferCreated()
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
if (!mHandle) {
|
2012-03-16 10:21:54 +04:00
|
|
|
mHandle = malloc(GRAPHIC_BUFFER_SIZE);
|
2012-01-05 06:53:09 +04:00
|
|
|
sGLFunctions.fGraphicBufferCtor(mHandle, mWidth, mHeight, GetAndroidFormat(mFormat), GetAndroidUsage(mUsage));
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AndroidGraphicBuffer::EnsureInitialized()
|
|
|
|
{
|
|
|
|
if (!sGLFunctions.EnsureInitialized()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-05 06:53:09 +04:00
|
|
|
EnsureBufferCreated();
|
2011-12-21 23:03:26 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
int
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidGraphicBuffer::Lock(uint32_t aUsage, unsigned char **bits)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return true;
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
return sGLFunctions.fGraphicBufferLock(mHandle, GetAndroidUsage(aUsage), bits);
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
int
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidGraphicBuffer::Lock(uint32_t aUsage, const nsIntRect& aRect, unsigned char **bits)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
2012-01-19 05:41:28 +04:00
|
|
|
ARect rect;
|
2011-12-21 23:03:26 +04:00
|
|
|
rect.left = aRect.x;
|
|
|
|
rect.top = aRect.y;
|
|
|
|
rect.right = aRect.x + aRect.width;
|
|
|
|
rect.bottom = aRect.y + aRect.height;
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
return sGLFunctions.fGraphicBufferLockRect(mHandle, GetAndroidUsage(aUsage), rect, bits);
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
int
|
2011-12-21 23:03:26 +04:00
|
|
|
AndroidGraphicBuffer::Unlock()
|
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
2012-01-18 00:00:56 +04:00
|
|
|
return sGLFunctions.fGraphicBufferUnlock(mHandle);
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
AndroidGraphicBuffer::Reallocate(uint32_t aWidth, uint32_t aHeight, gfxImageFormat aFormat)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
2012-01-05 06:53:09 +04:00
|
|
|
mWidth = aWidth;
|
|
|
|
mHeight = aHeight;
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
2011-12-21 23:03:26 +04:00
|
|
|
// 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();
|
2012-01-05 06:53:09 +04:00
|
|
|
EnsureBufferCreated();
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
gTryRealloc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
|
|
|
AndroidGraphicBuffer::GetAndroidUsage(uint32_t aUsage)
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = 0;
|
2011-12-21 23:03:26 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2011-12-21 23:03:26 +04:00
|
|
|
AndroidGraphicBuffer::GetAndroidFormat(gfxImageFormat aFormat)
|
|
|
|
{
|
|
|
|
switch (aFormat) {
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
2011-12-21 23:03:26 +04:00
|
|
|
return HAL_PIXEL_FORMAT_RGBX_8888;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2011-12-21 23:03:26 +04:00
|
|
|
return HAL_PIXEL_FORMAT_RGB_565;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AndroidGraphicBuffer::EnsureEGLImage()
|
|
|
|
{
|
|
|
|
if (mEGLImage)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
2012-03-16 10:21:54 +04:00
|
|
|
EGLint eglImgAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE };
|
2012-03-13 00:28:02 +04:00
|
|
|
void* nativeBuffer = sGLFunctions.fGraphicBufferGetNativeBuffer(mHandle);
|
2012-03-16 10:21:54 +04:00
|
|
|
|
|
|
|
mEGLImage = sGLFunctions.fCreateImageKHR(sGLFunctions.fGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)nativeBuffer, eglImgAttrs);
|
2013-10-11 00:42:52 +04:00
|
|
|
return mEGLImage != nullptr;
|
2011-12-21 23:03:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-03-16 10:21:54 +04:00
|
|
|
AndroidGraphicBuffer::Bind()
|
2011-12-21 23:03:26 +04:00
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!EnsureEGLImage()) {
|
|
|
|
LOG("No valid EGLImage!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-05 06:51:11 +04:00
|
|
|
clearGLError();
|
2012-03-16 10:21:54 +04:00
|
|
|
sGLFunctions.fImageTargetTexture2DOES(GL_TEXTURE_2D, mEGLImage);
|
2011-12-21 23:03:26 +04:00
|
|
|
return ensureNoGLError("glEGLImageTargetTexture2DOES");
|
|
|
|
}
|
|
|
|
|
2013-10-29 00:03:12 +04:00
|
|
|
// Build whitelist to check for board type.
|
|
|
|
static void InitWhiteList(nsTArray<nsString>& list)
|
|
|
|
{
|
|
|
|
nsString ele;
|
|
|
|
ele.AssignASCII("droid2"); // Motorola Droid 2
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("GT-I9100"); // Samsung Galaxy SII
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("herring"); // Samsung Nexus S
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("omap4sdp"); // Amazon Kindle Fire
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("SGH-I897"); // Samsung Galaxy S
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("sgh-i997"); // Samsung Infuse 4G
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("sgh-t839"); // Samsung Sidekick 4G
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("shadow"); // Motorola Droid X
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("spyder"); // Motorola Razr
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("targa"); // Motorola Droid Bionic
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("tuna"); // Galaxy Nexus
|
|
|
|
list.AppendElement(ele);
|
|
|
|
ele.AssignASCII("venus2"); // Motorla Droid Pro
|
|
|
|
list.AppendElement(ele);
|
|
|
|
}
|
2012-01-19 05:41:28 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
AndroidGraphicBuffer::IsBlacklisted()
|
|
|
|
{
|
|
|
|
nsAutoString board;
|
|
|
|
if (!AndroidBridge::Bridge()->GetStaticStringField("android/os/Build", "BOARD", board))
|
|
|
|
return true;
|
|
|
|
|
2012-07-27 07:13:51 +04:00
|
|
|
NS_ConvertUTF16toUTF8 boardUtf8(board);
|
2012-01-19 05:41:28 +04:00
|
|
|
|
|
|
|
if (Preferences::GetBool("direct-texture.force.enabled", false)) {
|
2012-07-27 07:13:51 +04:00
|
|
|
LOG("allowing board '%s' due to prefs override", boardUtf8.get());
|
2012-01-19 05:41:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Preferences::GetBool("direct-texture.force.disabled", false)) {
|
2012-07-27 07:13:51 +04:00
|
|
|
LOG("disallowing board '%s' due to prefs override", boardUtf8.get());
|
2012-01-19 05:41:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-29 00:03:12 +04:00
|
|
|
static nsTArray<nsString> sListAllowed;
|
|
|
|
if (sListAllowed.Length() == 0) {
|
|
|
|
InitWhiteList(sListAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = -1;
|
|
|
|
if ((i = sListAllowed.BinaryIndexOf(board)) >= 0) {
|
|
|
|
nsString name = sListAllowed.ElementAt(i);
|
|
|
|
LOG("allowing board '%s' based on '%s'\n", boardUtf8.get(), NS_ConvertUTF16toUTF8(name).get());
|
|
|
|
return false;
|
2012-01-19 05:41:28 +04:00
|
|
|
}
|
|
|
|
|
2012-07-27 07:13:51 +04:00
|
|
|
LOG("disallowing board: %s\n", boardUtf8.get());
|
2012-01-19 05:41:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-21 23:03:26 +04:00
|
|
|
} /* mozilla */
|