2010-12-20 19:27:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
#define LOG_TAG "GLConsumer"
|
2012-02-24 03:27:23 +00:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
2011-01-09 21:24:09 +00:00
|
|
|
//#define LOG_NDEBUG 0
|
2010-12-20 19:27:26 +00:00
|
|
|
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#include <GLES2/gl2ext.h>
|
2013-07-24 00:28:53 +00:00
|
|
|
#include <cutils/compiler.h>
|
2010-12-20 19:27:26 +00:00
|
|
|
|
2012-08-08 01:03:04 +00:00
|
|
|
#include <hardware/hardware.h>
|
|
|
|
|
2013-03-29 00:44:13 +00:00
|
|
|
#include <gui/GLConsumer.h>
|
2012-02-26 02:48:35 +00:00
|
|
|
#include <gui/IGraphicBufferAlloc.h>
|
|
|
|
#include <gui/ISurfaceComposer.h>
|
|
|
|
#include <gui/SurfaceComposerClient.h>
|
2011-11-18 01:48:35 +00:00
|
|
|
|
2012-02-26 02:48:35 +00:00
|
|
|
#include <private/gui/ComposerService.h>
|
2013-03-29 00:44:13 +00:00
|
|
|
#include <private/gui/SyncFeatures.h>
|
2010-12-20 19:27:26 +00:00
|
|
|
|
|
|
|
#include <utils/Log.h>
|
2011-05-10 02:08:33 +00:00
|
|
|
#include <utils/String8.h>
|
2012-02-24 03:27:23 +00:00
|
|
|
#include <utils/Trace.h>
|
2010-12-20 19:27:26 +00:00
|
|
|
|
2013-09-24 00:22:10 +00:00
|
|
|
EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
|
|
|
|
#define CROP_EXT_STR "EGL_ANDROID_image_crop"
|
|
|
|
|
2012-12-11 23:21:45 +00:00
|
|
|
namespace android {
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
// Macros for including the GLConsumer name in log messages
|
2014-09-09 01:53:39 +00:00
|
|
|
#define ST_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
|
2011-08-17 22:42:04 +00:00
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
static const struct {
|
|
|
|
size_t width, height;
|
|
|
|
char const* bits;
|
2013-08-07 20:35:20 +00:00
|
|
|
} kDebugData = { 15, 12,
|
|
|
|
"___________________________________XX_XX_______X_X_____X_X____X_XXXXXXX_X____XXXXXXXXXXX__"
|
|
|
|
"___XX_XXX_XX_______XXXXXXX_________X___X_________X_____X__________________________________"
|
|
|
|
};
|
2013-07-24 00:28:53 +00:00
|
|
|
|
2011-01-10 00:33:17 +00:00
|
|
|
// Transform matrices
|
|
|
|
static float mtxIdentity[16] = {
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1,
|
|
|
|
};
|
|
|
|
static float mtxFlipH[16] = {
|
|
|
|
-1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
1, 0, 0, 1,
|
|
|
|
};
|
|
|
|
static float mtxFlipV[16] = {
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, -1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 1, 0, 1,
|
|
|
|
};
|
|
|
|
static float mtxRot90[16] = {
|
|
|
|
0, 1, 0, 0,
|
|
|
|
-1, 0, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
1, 0, 0, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mtxMul(float out[16], const float a[16], const float b[16]);
|
|
|
|
|
2013-08-09 00:46:48 +00:00
|
|
|
Mutex GLConsumer::sStaticInitLock;
|
|
|
|
sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
|
2011-09-17 00:30:26 +00:00
|
|
|
|
2013-09-24 00:22:10 +00:00
|
|
|
static bool hasEglAndroidImageCropImpl() {
|
|
|
|
EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
|
|
|
|
size_t cropExtLen = strlen(CROP_EXT_STR);
|
|
|
|
size_t extsLen = strlen(exts);
|
|
|
|
bool equal = !strcmp(CROP_EXT_STR, exts);
|
|
|
|
bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
|
|
|
|
bool atEnd = (cropExtLen+1) < extsLen &&
|
|
|
|
!strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
|
|
|
|
bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
|
|
|
|
return equal || atStart || atEnd || inMiddle;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasEglAndroidImageCrop() {
|
|
|
|
// Only compute whether the extension is present once the first time this
|
|
|
|
// function is called.
|
|
|
|
static bool hasIt = hasEglAndroidImageCropImpl();
|
|
|
|
return hasIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEglImageCroppable(const Rect& crop) {
|
|
|
|
return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:24:32 +00:00
|
|
|
GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
|
|
|
|
uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
|
2013-07-17 05:56:09 +00:00
|
|
|
ConsumerBase(bq, isControlledByApp),
|
2011-02-18 19:02:42 +00:00
|
|
|
mCurrentTransform(0),
|
2013-04-22 09:24:02 +00:00
|
|
|
mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
|
2012-12-20 22:05:45 +00:00
|
|
|
mCurrentFence(Fence::NO_FENCE),
|
2011-02-18 19:02:42 +00:00
|
|
|
mCurrentTimestamp(0),
|
2013-09-19 20:36:07 +00:00
|
|
|
mCurrentFrameNumber(0),
|
2013-04-22 09:24:02 +00:00
|
|
|
mDefaultWidth(1),
|
|
|
|
mDefaultHeight(1),
|
2012-05-08 23:56:34 +00:00
|
|
|
mFilteringEnabled(true),
|
2011-04-22 01:52:51 +00:00
|
|
|
mTexName(tex),
|
2011-11-14 22:51:01 +00:00
|
|
|
mUseFenceSync(useFenceSync),
|
2012-01-22 23:26:27 +00:00
|
|
|
mTexTarget(texTarget),
|
2012-03-20 01:33:05 +00:00
|
|
|
mEglDisplay(EGL_NO_DISPLAY),
|
|
|
|
mEglContext(EGL_NO_CONTEXT),
|
2012-03-29 02:05:54 +00:00
|
|
|
mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
|
|
|
|
mAttached(true)
|
2012-01-22 23:26:27 +00:00
|
|
|
{
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGV("GLConsumer");
|
2012-02-23 22:35:13 +00:00
|
|
|
|
2011-09-17 00:30:26 +00:00
|
|
|
memcpy(mCurrentTransformMatrix, mtxIdentity,
|
|
|
|
sizeof(mCurrentTransformMatrix));
|
2012-03-15 21:01:24 +00:00
|
|
|
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 21:21:45 +00:00
|
|
|
GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
|
|
|
|
bool useFenceSync, bool isControlledByApp) :
|
|
|
|
ConsumerBase(bq, isControlledByApp),
|
|
|
|
mCurrentTransform(0),
|
|
|
|
mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
|
|
|
|
mCurrentFence(Fence::NO_FENCE),
|
|
|
|
mCurrentTimestamp(0),
|
|
|
|
mCurrentFrameNumber(0),
|
|
|
|
mDefaultWidth(1),
|
|
|
|
mDefaultHeight(1),
|
|
|
|
mFilteringEnabled(true),
|
|
|
|
mTexName(-1),
|
|
|
|
mUseFenceSync(useFenceSync),
|
|
|
|
mTexTarget(texTarget),
|
|
|
|
mEglDisplay(EGL_NO_DISPLAY),
|
|
|
|
mEglContext(EGL_NO_CONTEXT),
|
|
|
|
mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
|
|
|
|
mAttached(false)
|
|
|
|
{
|
|
|
|
ST_LOGV("GLConsumer");
|
|
|
|
|
|
|
|
memcpy(mCurrentTransformMatrix, mtxIdentity,
|
|
|
|
sizeof(mCurrentTransformMatrix));
|
|
|
|
|
|
|
|
mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
|
2011-05-03 02:51:12 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setDefaultMaxBufferCount(bufferCount);
|
2011-05-03 02:51:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
|
2011-04-01 02:10:24 +00:00
|
|
|
{
|
2012-02-23 22:35:13 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-04-03 22:54:58 +00:00
|
|
|
mDefaultWidth = w;
|
|
|
|
mDefaultHeight = h;
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setDefaultBufferSize(w, h);
|
2011-04-01 02:10:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::updateTexImage() {
|
2012-12-05 00:51:15 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ST_LOGV("updateTexImage");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("updateTexImage: GLConsumer is abandoned!");
|
2012-12-05 00:51:15 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the EGL state is the same as in previous calls.
|
|
|
|
status_t err = checkAndUpdateEglStateLocked();
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferQueue::BufferItem item;
|
|
|
|
|
|
|
|
// Acquire the next buffer.
|
|
|
|
// In asynchronous mode the list is guaranteed to be one buffer
|
|
|
|
// deep, while in synchronous mode we use the oldest buffer.
|
2013-06-28 20:52:40 +00:00
|
|
|
err = acquireBufferLocked(&item, 0);
|
2012-12-05 00:51:15 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
|
|
|
|
// We always bind the texture even if we don't update its contents.
|
|
|
|
ST_LOGV("updateTexImage: no buffers were available");
|
|
|
|
glBindTexture(mTexTarget, mTexName);
|
|
|
|
err = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
ST_LOGE("updateTexImage: acquire failed: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the previous buffer.
|
2013-07-24 00:28:53 +00:00
|
|
|
err = updateAndReleaseLocked(item);
|
2012-12-05 00:51:15 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
// We always bind the texture.
|
|
|
|
glBindTexture(mTexTarget, mTexName);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-12-11 23:21:45 +00:00
|
|
|
// Bind the new buffer to the GL texture, and wait until it's ready.
|
|
|
|
return bindTextureImageLocked();
|
2012-05-24 00:56:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
|
|
|
|
status_t GLConsumer::releaseTexImage() {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ST_LOGV("releaseTexImage");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("releaseTexImage: GLConsumer is abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the EGL state is the same as in previous calls.
|
2013-08-09 01:16:21 +00:00
|
|
|
status_t err = NO_ERROR;
|
|
|
|
|
|
|
|
if (mAttached) {
|
|
|
|
err = checkAndUpdateEglStateLocked(true);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if we're detached, no need to validate EGL's state -- we won't use it.
|
2013-07-24 00:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the GLConsumer state.
|
|
|
|
int buf = mCurrentTexture;
|
|
|
|
if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
|
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
ST_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
|
2013-07-24 00:28:53 +00:00
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
if (mAttached) {
|
|
|
|
// Do whatever sync ops we need to do before releasing the slot.
|
|
|
|
err = syncForReleaseLocked(mEglDisplay);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ST_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if we're detached, we just use the fence that was created in detachFromContext()
|
|
|
|
// so... basically, nothing more to do here.
|
2013-07-24 00:28:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
|
2013-07-24 00:28:53 +00:00
|
|
|
if (err < NO_ERROR) {
|
|
|
|
ST_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
if (mReleasedTexImage == NULL) {
|
|
|
|
mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
|
|
|
|
}
|
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTextureImage = mReleasedTexImage;
|
2013-07-24 00:28:53 +00:00
|
|
|
mCurrentCrop.makeInvalid();
|
|
|
|
mCurrentTransform = 0;
|
|
|
|
mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
|
|
|
|
mCurrentTimestamp = 0;
|
|
|
|
mCurrentFence = Fence::NO_FENCE;
|
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
if (mAttached) {
|
2014-07-12 02:08:04 +00:00
|
|
|
// This binds a dummy buffer (mReleasedTexImage).
|
|
|
|
status_t err = bindTextureImageLocked();
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
2013-08-09 01:16:21 +00:00
|
|
|
} else {
|
|
|
|
// detached, don't touch the texture (and we may not even have an
|
|
|
|
// EGLDisplay here.
|
|
|
|
}
|
2013-07-24 00:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:46:48 +00:00
|
|
|
sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
|
|
|
|
Mutex::Autolock _l(sStaticInitLock);
|
|
|
|
if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
|
|
|
|
// The first time, create the debug texture in case the application
|
|
|
|
// continues to use it.
|
|
|
|
sp<GraphicBuffer> buffer = new GraphicBuffer(
|
|
|
|
kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
|
|
|
|
GraphicBuffer::USAGE_SW_WRITE_RARELY);
|
|
|
|
uint32_t* bits;
|
|
|
|
buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
|
|
|
|
size_t w = buffer->getStride();
|
|
|
|
size_t h = buffer->getHeight();
|
|
|
|
memset(bits, 0, w*h*4);
|
|
|
|
for (size_t y=0 ; y<kDebugData.height ; y++) {
|
|
|
|
for (size_t x=0 ; x<kDebugData.width ; x++) {
|
|
|
|
bits[x] = (kDebugData.bits[y*kDebugData.width+x] == 'X') ? 0xFF000000 : 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
bits += w;
|
|
|
|
}
|
|
|
|
buffer->unlock();
|
|
|
|
sReleasedTexImageBuffer = buffer;
|
|
|
|
}
|
|
|
|
return sReleasedTexImageBuffer;
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
|
|
|
|
nsecs_t presentWhen) {
|
|
|
|
status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
|
2012-08-08 01:03:04 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
// If item->mGraphicBuffer is not null, this buffer has not been acquired
|
|
|
|
// before, so any prior EglImage created is using a stale buffer. This
|
|
|
|
// replaces any old EglImage with a new one (using the new buffer).
|
|
|
|
if (item->mGraphicBuffer != NULL) {
|
|
|
|
int slot = item->mBuf;
|
|
|
|
mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
|
2012-08-08 01:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t GLConsumer::releaseBufferLocked(int buf,
|
|
|
|
sp<GraphicBuffer> graphicBuffer,
|
|
|
|
EGLDisplay display, EGLSyncKHR eglFence) {
|
|
|
|
// release the buffer if it hasn't already been discarded by the
|
|
|
|
// BufferQueue. This can happen, for example, when the producer of this
|
|
|
|
// buffer has reallocated the original buffer slot after this buffer
|
|
|
|
// was acquired.
|
|
|
|
status_t err = ConsumerBase::releaseBufferLocked(
|
|
|
|
buf, graphicBuffer, display, eglFence);
|
2012-09-21 18:55:35 +00:00
|
|
|
mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
|
2012-08-08 01:03:04 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
|
2012-12-05 00:51:15 +00:00
|
|
|
{
|
2012-03-27 03:37:15 +00:00
|
|
|
status_t err = NO_ERROR;
|
|
|
|
|
2014-04-04 22:37:08 +00:00
|
|
|
int buf = item.mBuf;
|
|
|
|
|
2012-03-29 02:05:54 +00:00
|
|
|
if (!mAttached) {
|
2013-07-24 00:28:53 +00:00
|
|
|
ST_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
|
2012-03-29 02:05:54 +00:00
|
|
|
"ES context");
|
2014-04-04 22:37:08 +00:00
|
|
|
releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
|
|
|
|
mEglDisplay, EGL_NO_SYNC_KHR);
|
2012-03-29 02:05:54 +00:00
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2012-12-05 00:51:15 +00:00
|
|
|
// Confirm state.
|
|
|
|
err = checkAndUpdateEglStateLocked();
|
|
|
|
if (err != NO_ERROR) {
|
2014-04-04 22:37:08 +00:00
|
|
|
releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
|
|
|
|
mEglDisplay, EGL_NO_SYNC_KHR);
|
2012-12-05 00:51:15 +00:00
|
|
|
return err;
|
2012-03-20 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
// Ensure we have a valid EglImageKHR for the slot, creating an EglImage
|
|
|
|
// if nessessary, for the gralloc buffer currently in the slot in
|
|
|
|
// ConsumerBase.
|
2012-12-05 00:51:15 +00:00
|
|
|
// We may have to do this even when item.mGraphicBuffer == NULL (which
|
2014-07-12 02:08:04 +00:00
|
|
|
// means the buffer was previously acquired).
|
|
|
|
err = mEglSlots[buf].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ST_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
|
|
|
|
mEglDisplay, buf);
|
|
|
|
releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
|
|
|
|
mEglDisplay, EGL_NO_SYNC_KHR);
|
|
|
|
return UNKNOWN_ERROR;
|
2012-03-20 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 00:51:15 +00:00
|
|
|
// Do whatever sync ops we need to do before releasing the old slot.
|
|
|
|
err = syncForReleaseLocked(mEglDisplay);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
// Release the buffer we just acquired. It's not safe to
|
|
|
|
// release the old buffer, so instead we just drop the new frame.
|
2013-05-03 21:50:50 +00:00
|
|
|
// As we are still under lock since acquireBuffer, it is safe to
|
|
|
|
// release by slot.
|
|
|
|
releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
|
|
|
|
mEglDisplay, EGL_NO_SYNC_KHR);
|
2012-12-05 00:51:15 +00:00
|
|
|
return err;
|
|
|
|
}
|
2012-03-20 01:33:05 +00:00
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
ST_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTexture, mCurrentTextureImage != NULL ?
|
|
|
|
mCurrentTextureImage->graphicBufferHandle() : 0,
|
2012-12-05 00:51:15 +00:00
|
|
|
buf, mSlots[buf].mGraphicBuffer->handle);
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-12-05 00:51:15 +00:00
|
|
|
// release old buffer
|
|
|
|
if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t status = releaseBufferLocked(
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
|
|
|
|
mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
|
2013-07-24 00:28:53 +00:00
|
|
|
if (status < NO_ERROR) {
|
|
|
|
ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
|
2012-12-05 00:51:15 +00:00
|
|
|
strerror(-status), status);
|
|
|
|
err = status;
|
|
|
|
// keep going, with error raised [?]
|
2012-05-24 00:56:42 +00:00
|
|
|
}
|
2012-12-05 00:51:15 +00:00
|
|
|
}
|
2012-05-24 00:56:42 +00:00
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
// Update the GLConsumer state.
|
2012-12-05 00:51:15 +00:00
|
|
|
mCurrentTexture = buf;
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTextureImage = mEglSlots[buf].mEglImage;
|
2012-12-05 00:51:15 +00:00
|
|
|
mCurrentCrop = item.mCrop;
|
|
|
|
mCurrentTransform = item.mTransform;
|
|
|
|
mCurrentScalingMode = item.mScalingMode;
|
|
|
|
mCurrentTimestamp = item.mTimestamp;
|
|
|
|
mCurrentFence = item.mFence;
|
2013-09-19 20:36:07 +00:00
|
|
|
mCurrentFrameNumber = item.mFrameNumber;
|
2012-12-05 00:51:15 +00:00
|
|
|
|
|
|
|
computeCurrentTransformMatrixLocked();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::bindTextureImageLocked() {
|
2012-12-05 00:51:15 +00:00
|
|
|
if (mEglDisplay == EGL_NO_DISPLAY) {
|
|
|
|
ALOGE("bindTextureImage: invalid display");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLint error;
|
|
|
|
while ((error = glGetError()) != GL_NO_ERROR) {
|
|
|
|
ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(mTexTarget, mTexName);
|
2014-07-12 02:08:04 +00:00
|
|
|
if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
|
|
|
|
mCurrentTextureImage == NULL) {
|
|
|
|
ST_LOGE("bindTextureImage: no currently-bound texture");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-01-26 19:52:02 +00:00
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
|
2014-08-26 03:16:37 +00:00
|
|
|
mCurrentCrop);
|
2014-07-12 02:08:04 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ST_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
|
|
|
|
mEglDisplay, mCurrentTexture);
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
mCurrentTextureImage->bindToTextureTarget(mTexTarget);
|
|
|
|
|
2014-08-26 03:16:37 +00:00
|
|
|
// In the rare case that the display is terminated and then initialized
|
|
|
|
// again, we can't detect that the display changed (it didn't), but the
|
|
|
|
// image is invalid. In this case, repeat the exact same steps while
|
|
|
|
// forcing the creation of a new image.
|
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) {
|
|
|
|
glBindTexture(mTexTarget, mTexName);
|
|
|
|
status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
|
|
|
|
mCurrentCrop,
|
|
|
|
true);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ST_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
|
|
|
|
mEglDisplay, mCurrentTexture);
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
mCurrentTextureImage->bindToTextureTarget(mTexTarget);
|
|
|
|
if ((error = glGetError()) != GL_NO_ERROR) {
|
|
|
|
ST_LOGE("bindTextureImage: error binding external image: %#04x", error);
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
2012-12-05 00:51:15 +00:00
|
|
|
}
|
2012-12-11 23:21:45 +00:00
|
|
|
|
|
|
|
// Wait for the new buffer to be ready.
|
|
|
|
return doGLFenceWaitLocked();
|
2012-12-05 00:51:15 +00:00
|
|
|
}
|
2011-01-13 02:30:40 +00:00
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
|
2012-12-05 00:51:15 +00:00
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
|
|
|
EGLContext ctx = eglGetCurrentContext();
|
2011-11-14 22:51:01 +00:00
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
if (!contextCheck) {
|
|
|
|
// if this is the first time we're called, mEglDisplay/mEglContext have
|
|
|
|
// never been set, so don't error out (below).
|
|
|
|
if (mEglDisplay == EGL_NO_DISPLAY) {
|
|
|
|
mEglDisplay = dpy;
|
|
|
|
}
|
2014-08-06 19:15:15 +00:00
|
|
|
if (mEglContext == EGL_NO_CONTEXT) {
|
2013-08-09 01:16:21 +00:00
|
|
|
mEglContext = ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
|
2012-12-05 00:51:15 +00:00
|
|
|
ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2013-08-09 01:16:21 +00:00
|
|
|
if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
|
2012-12-05 00:51:15 +00:00
|
|
|
ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
|
|
|
|
return INVALID_OPERATION;
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
2011-06-27 22:41:52 +00:00
|
|
|
|
2012-12-05 00:51:15 +00:00
|
|
|
mEglDisplay = dpy;
|
|
|
|
mEglContext = ctx;
|
|
|
|
return NO_ERROR;
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 18:37:21 +00:00
|
|
|
void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
|
|
|
|
if (fence->isValid() &&
|
|
|
|
mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t err = addReleaseFence(mCurrentTexture,
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTextureImage->graphicBuffer(), fence);
|
2013-03-20 18:37:21 +00:00
|
|
|
if (err != OK) {
|
|
|
|
ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
}
|
2012-06-14 21:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::detachFromContext() {
|
2012-03-29 02:05:54 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ST_LOGV("detachFromContext");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("detachFromContext: abandoned GLConsumer");
|
2012-03-29 02:05:54 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAttached) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("detachFromContext: GLConsumer is not attached to a "
|
2012-03-29 02:05:54 +00:00
|
|
|
"context");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
|
|
|
EGLContext ctx = eglGetCurrentContext();
|
|
|
|
|
|
|
|
if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
|
|
|
|
ST_LOGE("detachFromContext: invalid current EGLDisplay");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
|
|
|
|
ST_LOGE("detachFromContext: invalid current EGLContext");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
|
|
|
|
status_t err = syncForReleaseLocked(dpy);
|
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
glDeleteTextures(1, &mTexName);
|
|
|
|
}
|
|
|
|
|
|
|
|
mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
mEglContext = EGL_NO_CONTEXT;
|
|
|
|
mAttached = false;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:24:32 +00:00
|
|
|
status_t GLConsumer::attachToContext(uint32_t tex) {
|
2012-03-29 02:05:54 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ST_LOGV("attachToContext");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("attachToContext: abandoned GLConsumer");
|
2012-03-29 02:05:54 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAttached) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("attachToContext: GLConsumer is already attached to a "
|
2012-03-29 02:05:54 +00:00
|
|
|
"context");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
|
|
|
EGLContext ctx = eglGetCurrentContext();
|
|
|
|
|
|
|
|
if (dpy == EGL_NO_DISPLAY) {
|
|
|
|
ST_LOGE("attachToContext: invalid current EGLDisplay");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx == EGL_NO_CONTEXT) {
|
|
|
|
ST_LOGE("attachToContext: invalid current EGLContext");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to bind the texture regardless of whether there's a current
|
|
|
|
// buffer.
|
2013-08-08 04:24:32 +00:00
|
|
|
glBindTexture(mTexTarget, GLuint(tex));
|
2012-03-29 02:05:54 +00:00
|
|
|
|
|
|
|
mEglDisplay = dpy;
|
|
|
|
mEglContext = ctx;
|
|
|
|
mTexName = tex;
|
|
|
|
mAttached = true;
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
if (mCurrentTextureImage != NULL) {
|
|
|
|
// This may wait for a buffer a second time. This is likely required if
|
|
|
|
// this is a different context, since otherwise the wait could be skipped
|
|
|
|
// by bouncing through another context. For the same context the extra
|
|
|
|
// wait is redundant.
|
|
|
|
status_t err = bindTextureImageLocked();
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
2012-12-05 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
return OK;
|
2012-12-05 00:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
|
2012-03-29 02:05:54 +00:00
|
|
|
ST_LOGV("syncForReleaseLocked");
|
|
|
|
|
2012-09-06 21:54:19 +00:00
|
|
|
if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
|
2013-03-29 00:44:13 +00:00
|
|
|
if (SyncFeatures::getInstance().useNativeFenceSync()) {
|
2012-09-06 21:54:19 +00:00
|
|
|
EGLSyncKHR sync = eglCreateSyncKHR(dpy,
|
|
|
|
EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
|
|
|
|
if (sync == EGL_NO_SYNC_KHR) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
|
|
|
|
eglGetError());
|
2012-03-29 02:05:54 +00:00
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
2012-09-06 21:54:19 +00:00
|
|
|
glFlush();
|
|
|
|
int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
|
2012-09-10 21:49:42 +00:00
|
|
|
eglDestroySyncKHR(dpy, sync);
|
2012-09-06 21:54:19 +00:00
|
|
|
if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
|
|
|
|
"fd: %#x", eglGetError());
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
sp<Fence> fence(new Fence(fenceFd));
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t err = addReleaseFenceLocked(mCurrentTexture,
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTextureImage->graphicBuffer(), fence);
|
2012-09-06 21:54:19 +00:00
|
|
|
if (err != OK) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: error adding release fence: "
|
|
|
|
"%s (%d)", strerror(-err), err);
|
|
|
|
return err;
|
|
|
|
}
|
2013-03-29 00:44:13 +00:00
|
|
|
} else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
|
2012-09-06 21:54:19 +00:00
|
|
|
EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
|
|
|
|
if (fence != EGL_NO_SYNC_KHR) {
|
|
|
|
// There is already a fence for the current slot. We need to
|
|
|
|
// wait on that before replacing it with another fence to
|
|
|
|
// ensure that all outstanding buffer accesses have completed
|
|
|
|
// before the producer accesses it.
|
|
|
|
EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
|
|
|
|
if (result == EGL_FALSE) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: error waiting for previous "
|
|
|
|
"fence: %#x", eglGetError());
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
} else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
|
|
|
|
"fence");
|
|
|
|
return TIMED_OUT;
|
|
|
|
}
|
|
|
|
eglDestroySyncKHR(dpy, fence);
|
|
|
|
}
|
2012-03-29 02:05:54 +00:00
|
|
|
|
2012-09-06 21:54:19 +00:00
|
|
|
// Create a fence for the outstanding accesses in the current
|
|
|
|
// OpenGL ES context.
|
|
|
|
fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
|
|
|
|
if (fence == EGL_NO_SYNC_KHR) {
|
|
|
|
ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
|
|
|
|
eglGetError());
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
glFlush();
|
|
|
|
mEglSlots[mCurrentTexture].mEglFence = fence;
|
2012-03-29 02:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
bool GLConsumer::isExternalFormat(uint32_t format)
|
2011-04-12 04:19:55 +00:00
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
// supported YUV formats
|
|
|
|
case HAL_PIXEL_FORMAT_YV12:
|
|
|
|
// Legacy/deprecated YUV formats
|
|
|
|
case HAL_PIXEL_FORMAT_YCbCr_422_SP:
|
|
|
|
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
|
|
|
|
case HAL_PIXEL_FORMAT_YCbCr_422_I:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any OEM format needs to be considered
|
|
|
|
if (format>=0x100 && format<=0x1FF)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:24:32 +00:00
|
|
|
uint32_t GLConsumer::getCurrentTextureTarget() const {
|
2011-09-28 19:13:31 +00:00
|
|
|
return mTexTarget;
|
2011-04-12 04:19:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::getTransformMatrix(float mtx[16]) {
|
2011-01-10 00:33:17 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-06-13 00:03:06 +00:00
|
|
|
memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::setFilteringEnabled(bool enabled) {
|
2012-05-08 23:56:34 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-09-25 02:26:11 +00:00
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
|
2012-09-25 02:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-08 23:56:34 +00:00
|
|
|
bool needsRecompute = mFilteringEnabled != enabled;
|
|
|
|
mFilteringEnabled = enabled;
|
2012-09-25 02:26:11 +00:00
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
if (needsRecompute && mCurrentTextureImage==NULL) {
|
|
|
|
ST_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
|
2012-09-25 02:26:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
if (needsRecompute && mCurrentTextureImage != NULL) {
|
2012-09-25 02:26:11 +00:00
|
|
|
computeCurrentTransformMatrixLocked();
|
2012-05-08 23:56:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::computeCurrentTransformMatrixLocked() {
|
2012-09-25 02:26:11 +00:00
|
|
|
ST_LOGV("computeCurrentTransformMatrixLocked");
|
2011-01-10 00:33:17 +00:00
|
|
|
|
2011-01-14 21:53:31 +00:00
|
|
|
float xform[16];
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
xform[i] = mtxIdentity[i];
|
|
|
|
}
|
|
|
|
if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
|
|
|
|
float result[16];
|
|
|
|
mtxMul(result, xform, mtxFlipH);
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
xform[i] = result[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
|
|
|
|
float result[16];
|
|
|
|
mtxMul(result, xform, mtxFlipV);
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
xform[i] = result[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
|
|
|
|
float result[16];
|
|
|
|
mtxMul(result, xform, mtxRot90);
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
xform[i] = result[i];
|
|
|
|
}
|
2011-01-10 00:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
sp<GraphicBuffer> buf = (mCurrentTextureImage == NULL) ?
|
|
|
|
NULL : mCurrentTextureImage->graphicBuffer();
|
2012-09-25 02:26:11 +00:00
|
|
|
|
|
|
|
if (buf == NULL) {
|
2014-07-12 02:08:04 +00:00
|
|
|
ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureImage is NULL");
|
2012-09-25 02:26:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 00:22:10 +00:00
|
|
|
float mtxBeforeFlipV[16];
|
|
|
|
if (!isEglImageCroppable(mCurrentCrop)) {
|
|
|
|
Rect cropRect = mCurrentCrop;
|
|
|
|
float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
|
|
|
|
float bufferWidth = buf->getWidth();
|
|
|
|
float bufferHeight = buf->getHeight();
|
|
|
|
if (!cropRect.isEmpty()) {
|
|
|
|
float shrinkAmount = 0.0f;
|
|
|
|
if (mFilteringEnabled) {
|
|
|
|
// In order to prevent bilinear sampling beyond the edge of the
|
|
|
|
// crop rectangle we may need to shrink it by 2 texels in each
|
|
|
|
// dimension. Normally this would just need to take 1/2 a texel
|
|
|
|
// off each end, but because the chroma channels of YUV420 images
|
|
|
|
// are subsampled we may need to shrink the crop region by a whole
|
|
|
|
// texel on each side.
|
|
|
|
switch (buf->getPixelFormat()) {
|
|
|
|
case PIXEL_FORMAT_RGBA_8888:
|
|
|
|
case PIXEL_FORMAT_RGBX_8888:
|
|
|
|
case PIXEL_FORMAT_RGB_888:
|
|
|
|
case PIXEL_FORMAT_RGB_565:
|
|
|
|
case PIXEL_FORMAT_BGRA_8888:
|
|
|
|
// We know there's no subsampling of any channels, so we
|
|
|
|
// only need to shrink by a half a pixel.
|
|
|
|
shrinkAmount = 0.5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// If we don't recognize the format, we must assume the
|
|
|
|
// worst case (that we care about), which is YUV420.
|
|
|
|
shrinkAmount = 1.0;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-08 23:56:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 00:22:10 +00:00
|
|
|
// Only shrink the dimensions that are not the size of the buffer.
|
|
|
|
if (cropRect.width() < bufferWidth) {
|
|
|
|
tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
|
|
|
|
sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
|
|
|
|
bufferWidth;
|
|
|
|
}
|
|
|
|
if (cropRect.height() < bufferHeight) {
|
|
|
|
ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
|
|
|
|
bufferHeight;
|
|
|
|
sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
|
|
|
|
bufferHeight;
|
|
|
|
}
|
2012-05-08 23:56:34 +00:00
|
|
|
}
|
2013-09-24 00:22:10 +00:00
|
|
|
float crop[16] = {
|
|
|
|
sx, 0, 0, 0,
|
|
|
|
0, sy, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
tx, ty, 0, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
mtxMul(mtxBeforeFlipV, crop, xform);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
mtxBeforeFlipV[i] = xform[i];
|
2012-05-08 23:56:34 +00:00
|
|
|
}
|
2011-01-14 21:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SurfaceFlinger expects the top of its window textures to be at a Y
|
2012-12-18 17:49:45 +00:00
|
|
|
// coordinate of 0, so GLConsumer must behave the same way. We don't
|
2011-01-14 21:53:31 +00:00
|
|
|
// want to expose this to applications, however, so we must add an
|
|
|
|
// additional vertical flip to the transform after all the other transforms.
|
2011-06-13 00:03:06 +00:00
|
|
|
mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
|
2011-01-10 00:33:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
nsecs_t GLConsumer::getTimestamp() {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("getTimestamp");
|
2011-02-18 19:02:42 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentTimestamp;
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:36:07 +00:00
|
|
|
nsecs_t GLConsumer::getFrameNumber() {
|
|
|
|
ST_LOGV("getFrameNumber");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentFrameNumber;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
|
2011-04-12 04:19:55 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2014-07-12 02:08:04 +00:00
|
|
|
return (mCurrentTextureImage == NULL) ?
|
|
|
|
NULL : mCurrentTextureImage->graphicBuffer();
|
2011-04-12 04:19:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
Rect GLConsumer::getCurrentCrop() const {
|
2011-04-12 04:19:55 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-04-03 22:54:58 +00:00
|
|
|
|
|
|
|
Rect outCrop = mCurrentCrop;
|
|
|
|
if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
|
|
|
|
int32_t newWidth = mCurrentCrop.width();
|
|
|
|
int32_t newHeight = mCurrentCrop.height();
|
|
|
|
|
|
|
|
if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
|
|
|
|
newWidth = newHeight * mDefaultWidth / mDefaultHeight;
|
|
|
|
ST_LOGV("too wide: newWidth = %d", newWidth);
|
|
|
|
} else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
|
|
|
|
newHeight = newWidth * mDefaultHeight / mDefaultWidth;
|
|
|
|
ST_LOGV("too tall: newHeight = %d", newHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The crop is too wide
|
|
|
|
if (newWidth < mCurrentCrop.width()) {
|
|
|
|
int32_t dw = (newWidth - mCurrentCrop.width())/2;
|
|
|
|
outCrop.left -=dw;
|
|
|
|
outCrop.right += dw;
|
|
|
|
// The crop is too tall
|
|
|
|
} else if (newHeight < mCurrentCrop.height()) {
|
|
|
|
int32_t dh = (newHeight - mCurrentCrop.height())/2;
|
|
|
|
outCrop.top -= dh;
|
|
|
|
outCrop.bottom += dh;
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
|
|
|
|
outCrop.left, outCrop.top,
|
|
|
|
outCrop.right,outCrop.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return outCrop;
|
2011-04-12 04:19:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
uint32_t GLConsumer::getCurrentTransform() const {
|
2011-04-12 04:19:55 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentTransform;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
uint32_t GLConsumer::getCurrentScalingMode() const {
|
2011-07-13 22:24:42 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentScalingMode;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
sp<Fence> GLConsumer::getCurrentFence() const {
|
2012-06-29 22:21:18 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentFence;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::doGLFenceWait() const {
|
2012-09-10 00:48:42 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-09-17 23:58:17 +00:00
|
|
|
return doGLFenceWaitLocked();
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::doGLFenceWaitLocked() const {
|
2012-09-10 00:48:42 +00:00
|
|
|
|
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
|
|
|
EGLContext ctx = eglGetCurrentContext();
|
|
|
|
|
|
|
|
if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
|
|
|
|
ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
|
|
|
|
ST_LOGE("doGLFenceWait: invalid current EGLContext");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2012-12-20 22:05:45 +00:00
|
|
|
if (mCurrentFence->isValid()) {
|
2013-03-29 00:44:13 +00:00
|
|
|
if (SyncFeatures::getInstance().useWaitSync()) {
|
2012-09-10 00:48:42 +00:00
|
|
|
// Create an EGLSyncKHR from the current fence.
|
|
|
|
int fenceFd = mCurrentFence->dup();
|
|
|
|
if (fenceFd == -1) {
|
|
|
|
ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
EGLint attribs[] = {
|
|
|
|
EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
EGLSyncKHR sync = eglCreateSyncKHR(dpy,
|
|
|
|
EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
|
|
|
|
if (sync == EGL_NO_SYNC_KHR) {
|
|
|
|
close(fenceFd);
|
|
|
|
ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
|
|
|
|
eglGetError());
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: The spec draft is inconsistent as to whether this should
|
|
|
|
// return an EGLint or void. Ignore the return value for now, as
|
|
|
|
// it's not strictly needed.
|
2013-03-28 00:32:41 +00:00
|
|
|
eglWaitSyncKHR(dpy, sync, 0);
|
2012-09-10 00:48:42 +00:00
|
|
|
EGLint eglErr = eglGetError();
|
|
|
|
eglDestroySyncKHR(dpy, sync);
|
|
|
|
if (eglErr != EGL_SUCCESS) {
|
|
|
|
ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
|
|
|
|
eglErr);
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-17 01:03:22 +00:00
|
|
|
status_t err = mCurrentFence->waitForever(
|
2012-12-18 17:49:45 +00:00
|
|
|
"GLConsumer::doGLFenceWaitLocked");
|
2012-09-10 00:48:42 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::freeBufferLocked(int slotIndex) {
|
2012-03-15 21:01:24 +00:00
|
|
|
ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
|
2012-03-27 03:37:15 +00:00
|
|
|
if (slotIndex == mCurrentTexture) {
|
|
|
|
mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
|
|
|
|
}
|
2014-07-12 02:08:04 +00:00
|
|
|
mEglSlots[slotIndex].mEglImage.clear();
|
2012-08-08 01:03:04 +00:00
|
|
|
ConsumerBase::freeBufferLocked(slotIndex);
|
2012-03-15 21:01:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::abandonLocked() {
|
2012-08-08 01:03:04 +00:00
|
|
|
ST_LOGV("abandonLocked");
|
2014-07-12 02:08:04 +00:00
|
|
|
mCurrentTextureImage.clear();
|
2012-08-08 01:03:04 +00:00
|
|
|
ConsumerBase::abandonLocked();
|
2011-07-19 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
void GLConsumer::setName(const String8& name) {
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
2011-09-17 00:30:26 +00:00
|
|
|
mName = name;
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->setConsumerName(name);
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
|
2012-02-23 22:35:13 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setDefaultBufferFormat(defaultFormat);
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
|
2012-02-23 22:35:13 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-04-13 22:16:31 +00:00
|
|
|
usage |= DEFAULT_USAGE_FLAGS;
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setConsumerUsageBits(usage);
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
status_t GLConsumer::setTransformHint(uint32_t hint) {
|
2012-02-23 22:35:13 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setTransformHint(hint);
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
void GLConsumer::dumpLocked(String8& result, const char* prefix) const
|
2011-05-10 02:08:33 +00:00
|
|
|
{
|
2013-04-22 14:55:35 +00:00
|
|
|
result.appendFormat(
|
2012-08-08 01:03:04 +00:00
|
|
|
"%smTexName=%d mCurrentTexture=%d\n"
|
|
|
|
"%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
|
|
|
|
prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
|
|
|
|
mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
|
|
|
|
mCurrentTransform);
|
2011-05-10 02:08:33 +00:00
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
ConsumerBase::dumpLocked(result, prefix);
|
2011-05-10 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 00:33:17 +00:00
|
|
|
static void mtxMul(float out[16], const float a[16], const float b[16]) {
|
|
|
|
out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
|
|
|
|
out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
|
|
|
|
out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
|
|
|
|
out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
|
|
|
|
|
|
|
|
out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
|
|
|
|
out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
|
|
|
|
out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
|
|
|
|
out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
|
|
|
|
|
|
|
|
out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
|
|
|
|
out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
|
|
|
|
out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
|
|
|
|
out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
|
|
|
|
|
|
|
|
out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
|
|
|
|
out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
|
|
|
|
out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
|
|
|
|
out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:08:04 +00:00
|
|
|
GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
|
|
|
|
mGraphicBuffer(graphicBuffer),
|
|
|
|
mEglImage(EGL_NO_IMAGE_KHR),
|
|
|
|
mEglDisplay(EGL_NO_DISPLAY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
GLConsumer::EglImage::~EglImage() {
|
|
|
|
if (mEglImage != EGL_NO_IMAGE_KHR) {
|
|
|
|
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
|
|
|
|
ALOGE("~EglImage: eglDestroyImageKHR failed");
|
|
|
|
}
|
2014-10-02 19:10:07 +00:00
|
|
|
eglTerminate(mEglDisplay);
|
2014-07-12 02:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
|
2014-08-26 03:16:37 +00:00
|
|
|
const Rect& cropRect,
|
|
|
|
bool forceCreation) {
|
2014-07-12 02:08:04 +00:00
|
|
|
// If there's an image and it's no longer valid, destroy it.
|
|
|
|
bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
|
|
|
|
bool displayInvalid = mEglDisplay != eglDisplay;
|
|
|
|
bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
|
2014-08-26 03:16:37 +00:00
|
|
|
if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
|
2014-07-12 02:08:04 +00:00
|
|
|
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
|
|
|
|
ALOGE("createIfNeeded: eglDestroyImageKHR failed");
|
|
|
|
}
|
2014-10-02 19:10:07 +00:00
|
|
|
eglTerminate(mEglDisplay);
|
2014-07-12 02:08:04 +00:00
|
|
|
mEglImage = EGL_NO_IMAGE_KHR;
|
|
|
|
mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no image, create one.
|
|
|
|
if (mEglImage == EGL_NO_IMAGE_KHR) {
|
|
|
|
mEglDisplay = eglDisplay;
|
|
|
|
mCropRect = cropRect;
|
|
|
|
mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if we can't create a valid image.
|
|
|
|
if (mEglImage == EGL_NO_IMAGE_KHR) {
|
|
|
|
mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
mCropRect.makeInvalid();
|
|
|
|
const sp<GraphicBuffer>& buffer = mGraphicBuffer;
|
|
|
|
ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
|
|
|
|
buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
|
|
|
|
buffer->getUsage(), buffer->getPixelFormat());
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
|
|
|
|
glEGLImageTargetTexture2DOES(texTarget, (GLeglImageOES)mEglImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
|
|
|
|
const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
|
|
|
|
EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
|
|
|
|
EGLint attrs[] = {
|
|
|
|
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
|
|
|
|
EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
|
|
|
|
EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
|
|
|
|
EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
|
|
|
|
EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
if (!crop.isValid()) {
|
|
|
|
// No crop rect to set, so terminate the attrib array before the crop.
|
|
|
|
attrs[2] = EGL_NONE;
|
|
|
|
} else if (!isEglImageCroppable(crop)) {
|
|
|
|
// The crop rect is not at the origin, so we can't set the crop on the
|
|
|
|
// EGLImage because that's not allowed by the EGL_ANDROID_image_crop
|
|
|
|
// extension. In the future we can add a layered extension that
|
|
|
|
// removes this restriction if there is hardware that can support it.
|
|
|
|
attrs[2] = EGL_NONE;
|
|
|
|
}
|
2014-10-02 19:10:07 +00:00
|
|
|
eglInitialize(dpy, 0, 0);
|
2014-07-12 02:08:04 +00:00
|
|
|
EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
|
|
|
|
EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
|
|
|
|
if (image == EGL_NO_IMAGE_KHR) {
|
|
|
|
EGLint error = eglGetError();
|
|
|
|
ALOGE("error creating EGLImage: %#x", error);
|
2014-10-02 19:10:07 +00:00
|
|
|
eglTerminate(dpy);
|
2014-07-12 02:08:04 +00:00
|
|
|
}
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
}; // namespace android
|