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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "SurfaceTexture"
|
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>
|
|
|
|
|
|
|
|
#include <gui/SurfaceTexture.h>
|
|
|
|
|
2011-04-12 04:19:55 +00:00
|
|
|
#include <hardware/hardware.h>
|
|
|
|
|
2011-11-18 01:48:35 +00:00
|
|
|
#include <private/gui/ComposerService.h>
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
#include <surfaceflinger/ISurfaceComposer.h>
|
|
|
|
#include <surfaceflinger/SurfaceComposerClient.h>
|
2011-01-13 02:30:40 +00:00
|
|
|
#include <surfaceflinger/IGraphicBufferAlloc.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>
|
2010-12-20 19:27:26 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// This compile option causes SurfaceTexture to return the buffer that is currently
|
|
|
|
// attached to the GL texture from dequeueBuffer when no other buffers are
|
|
|
|
// available. It requires the drivers (Gralloc, GL, OMX IL, and Camera) to do
|
|
|
|
// implicit cross-process synchronization to prevent the buffer from being
|
|
|
|
// written to before the buffer has (a) been detached from the GL texture and
|
|
|
|
// (b) all GL reads from the buffer have completed.
|
2011-11-15 03:17:37 +00:00
|
|
|
#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
|
|
|
|
#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true
|
|
|
|
#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
|
|
|
|
#else
|
|
|
|
#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false
|
|
|
|
#endif
|
2011-08-17 22:42:04 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
|
|
|
|
// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
|
|
|
|
// waiting for the GL reads for the buffer being dequeued to complete before
|
|
|
|
// allowing the buffer to be dequeued.
|
|
|
|
#ifdef USE_FENCE_SYNC
|
|
|
|
#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
|
|
|
|
#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-09-17 00:30:26 +00:00
|
|
|
// Macros for including the SurfaceTexture name in log messages
|
2011-10-20 10:56:00 +00:00
|
|
|
#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
|
2011-12-20 16:23:08 +00:00
|
|
|
#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
|
2012-01-04 20:05:49 +00:00
|
|
|
#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
|
2012-01-05 23:22:43 +00:00
|
|
|
#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
|
2012-01-06 19:20:56 +00:00
|
|
|
#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
|
2011-08-17 22:42:04 +00:00
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
namespace android {
|
|
|
|
|
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 float mtxRot180[16] = {
|
|
|
|
-1, 0, 0, 0,
|
|
|
|
0, -1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
1, 1, 0, 1,
|
|
|
|
};
|
|
|
|
static float mtxRot270[16] = {
|
|
|
|
0, -1, 0, 0,
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 1, 0, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mtxMul(float out[16], const float a[16], const float b[16]);
|
|
|
|
|
2011-09-17 00:30:26 +00:00
|
|
|
// Get an ID that's unique within this process.
|
|
|
|
static int32_t createProcessUniqueId() {
|
|
|
|
static volatile int32_t globalCounter = 0;
|
|
|
|
return android_atomic_inc(&globalCounter);
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:31 +00:00
|
|
|
SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
|
2011-11-14 22:51:01 +00:00
|
|
|
GLenum texTarget, bool useFenceSync) :
|
2011-04-01 02:10:24 +00:00
|
|
|
mDefaultWidth(1),
|
|
|
|
mDefaultHeight(1),
|
|
|
|
mPixelFormat(PIXEL_FORMAT_RGBA_8888),
|
2011-05-03 02:51:12 +00:00
|
|
|
mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
|
|
|
|
mClientBufferCount(0),
|
|
|
|
mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
|
2011-02-18 19:02:42 +00:00
|
|
|
mCurrentTexture(INVALID_BUFFER_SLOT),
|
|
|
|
mCurrentTransform(0),
|
|
|
|
mCurrentTimestamp(0),
|
|
|
|
mNextTransform(0),
|
2011-07-13 22:24:42 +00:00
|
|
|
mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
|
2011-04-22 01:52:51 +00:00
|
|
|
mTexName(tex),
|
2011-06-24 04:21:47 +00:00
|
|
|
mSynchronousMode(false),
|
2011-07-14 02:12:20 +00:00
|
|
|
mAllowSynchronousMode(allowSynchronousMode),
|
2011-07-19 19:08:33 +00:00
|
|
|
mConnectedApi(NO_CONNECTED_API),
|
2011-09-28 19:13:31 +00:00
|
|
|
mAbandoned(false),
|
2011-11-14 22:51:01 +00:00
|
|
|
#ifdef USE_FENCE_SYNC
|
|
|
|
mUseFenceSync(useFenceSync),
|
|
|
|
#else
|
|
|
|
mUseFenceSync(false),
|
|
|
|
#endif
|
2011-11-10 00:23:41 +00:00
|
|
|
mTexTarget(texTarget),
|
|
|
|
mFrameCounter(0) {
|
2011-09-17 00:30:26 +00:00
|
|
|
// Choose a name using the PID and a process-unique ID.
|
|
|
|
mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
|
|
|
|
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("SurfaceTexture");
|
2011-01-13 02:30:40 +00:00
|
|
|
sp<ISurfaceComposer> composer(ComposerService::getComposerService());
|
|
|
|
mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
|
2011-04-28 01:57:33 +00:00
|
|
|
mNextCrop.makeInvalid();
|
2011-09-17 00:30:26 +00:00
|
|
|
memcpy(mCurrentTransformMatrix, mtxIdentity,
|
|
|
|
sizeof(mCurrentTransformMatrix));
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceTexture::~SurfaceTexture() {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("~SurfaceTexture");
|
2011-08-10 22:28:58 +00:00
|
|
|
freeAllBuffersLocked();
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
|
|
|
|
if (bufferCount > NUM_BUFFER_SLOTS)
|
|
|
|
return BAD_VALUE;
|
|
|
|
|
|
|
|
// special-case, nothing to do
|
|
|
|
if (bufferCount == mBufferCount)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
if (!mClientBufferCount &&
|
|
|
|
bufferCount >= mBufferCount) {
|
|
|
|
// easy, we just have more buffers
|
|
|
|
mBufferCount = bufferCount;
|
|
|
|
mServerBufferCount = bufferCount;
|
|
|
|
mDequeueCondition.signal();
|
|
|
|
} else {
|
|
|
|
// we're here because we're either
|
|
|
|
// - reducing the number of available buffers
|
|
|
|
// - or there is a client-buffer-count in effect
|
|
|
|
|
|
|
|
// less than 2 buffers is never allowed
|
|
|
|
if (bufferCount < 2)
|
|
|
|
return BAD_VALUE;
|
|
|
|
|
|
|
|
// when there is non client-buffer-count in effect, the client is not
|
|
|
|
// allowed to dequeue more than one buffer at a time,
|
|
|
|
// so the next time they dequeue a buffer, we know that they don't
|
|
|
|
// own one. the actual resizing will happen during the next
|
|
|
|
// dequeueBuffer.
|
|
|
|
|
|
|
|
mServerBufferCount = bufferCount;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return setBufferCountServerLocked(bufferCount);
|
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
status_t SurfaceTexture::setBufferCount(int bufferCount) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setBufferCount: count=%d", bufferCount);
|
2011-04-22 01:52:51 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-02-27 22:10:20 +00:00
|
|
|
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-06-24 16:56:27 +00:00
|
|
|
if (bufferCount > NUM_BUFFER_SLOTS) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setBufferCount: bufferCount larger than slots available");
|
2011-06-24 16:56:27 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
// Error out if the user has dequeued buffers
|
|
|
|
for (int i=0 ; i<mBufferCount ; i++) {
|
|
|
|
if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setBufferCount: client owns some buffers");
|
2011-05-03 02:51:12 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2011-07-30 23:00:11 +00:00
|
|
|
const int minBufferSlots = mSynchronousMode ?
|
|
|
|
MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
|
2011-05-03 02:51:12 +00:00
|
|
|
if (bufferCount == 0) {
|
|
|
|
mClientBufferCount = 0;
|
|
|
|
bufferCount = (mServerBufferCount >= minBufferSlots) ?
|
|
|
|
mServerBufferCount : minBufferSlots;
|
|
|
|
return setBufferCountServerLocked(bufferCount);
|
|
|
|
}
|
|
|
|
|
2011-07-30 23:00:11 +00:00
|
|
|
if (bufferCount < minBufferSlots) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
|
2011-07-30 23:00:11 +00:00
|
|
|
"minimum (%d)", bufferCount, minBufferSlots);
|
2011-02-27 22:10:20 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
// here we're guaranteed that the client doesn't have dequeued buffers
|
|
|
|
// and will release all of its buffer references.
|
2011-08-10 22:28:58 +00:00
|
|
|
freeAllBuffersLocked();
|
2010-12-20 19:27:26 +00:00
|
|
|
mBufferCount = bufferCount;
|
2011-05-03 02:51:12 +00:00
|
|
|
mClientBufferCount = bufferCount;
|
2011-01-09 21:25:39 +00:00
|
|
|
mCurrentTexture = INVALID_BUFFER_SLOT;
|
2011-04-22 01:52:51 +00:00
|
|
|
mQueue.clear();
|
|
|
|
mDequeueCondition.signal();
|
2010-12-20 19:27:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-01 02:10:24 +00:00
|
|
|
status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
|
|
|
|
{
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
|
2011-07-26 02:56:08 +00:00
|
|
|
if (!w || !h) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
|
|
|
|
w, h);
|
2011-07-26 02:56:08 +00:00
|
|
|
return BAD_VALUE;
|
2011-04-01 02:10:24 +00:00
|
|
|
}
|
2011-07-26 02:56:08 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mDefaultWidth = w;
|
|
|
|
mDefaultHeight = h;
|
2011-04-01 02:10:24 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-07-19 19:08:33 +00:00
|
|
|
status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("requestBuffer: slot=%d", slot);
|
2010-12-20 19:27:26 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
if (slot < 0 || mBufferCount <= slot) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
|
2011-07-19 19:08:33 +00:00
|
|
|
mBufferCount, slot);
|
|
|
|
return BAD_VALUE;
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
2011-07-19 19:08:33 +00:00
|
|
|
mSlots[slot].mRequestBufferCalled = true;
|
|
|
|
*buf = mSlots[slot].mGraphicBuffer;
|
|
|
|
return NO_ERROR;
|
2011-04-26 03:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
|
|
|
|
uint32_t format, uint32_t usage) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
|
2011-04-26 03:22:14 +00:00
|
|
|
|
2011-06-24 16:56:27 +00:00
|
|
|
if ((w && !h) || (!w && h)) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
|
2011-04-26 03:22:14 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
status_t returnFlags(OK);
|
2011-11-14 22:51:01 +00:00
|
|
|
EGLDisplay dpy = EGL_NO_DISPLAY;
|
|
|
|
EGLSyncKHR fence = EGL_NO_SYNC_KHR;
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
{ // Scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
int found = -1;
|
|
|
|
int foundSync = -1;
|
|
|
|
int dequeuedCount = 0;
|
|
|
|
bool tryAgain = true;
|
|
|
|
while (tryAgain) {
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// We need to wait for the FIFO to drain if the number of buffer
|
|
|
|
// needs to change.
|
|
|
|
//
|
|
|
|
// The condition "number of buffers needs to change" is true if
|
|
|
|
// - the client doesn't care about how many buffers there are
|
|
|
|
// - AND the actual number of buffer is different from what was
|
|
|
|
// set in the last setBufferCountServer()
|
|
|
|
// - OR -
|
|
|
|
// setBufferCountServer() was set to a value incompatible with
|
|
|
|
// the synchronization mode (for instance because the sync mode
|
|
|
|
// changed since)
|
|
|
|
//
|
|
|
|
// As long as this condition is true AND the FIFO is not empty, we
|
|
|
|
// wait on mDequeueCondition.
|
|
|
|
|
|
|
|
const int minBufferCountNeeded = mSynchronousMode ?
|
|
|
|
MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
|
|
|
|
|
|
|
|
const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
|
|
|
|
((mServerBufferCount != mBufferCount) ||
|
|
|
|
(mServerBufferCount < minBufferCountNeeded));
|
|
|
|
|
|
|
|
if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
|
|
|
|
// wait for the FIFO to drain
|
|
|
|
mDequeueCondition.wait(mMutex);
|
|
|
|
// NOTE: we continue here because we need to reevaluate our
|
|
|
|
// whole state (eg: we could be abandoned or disconnected)
|
|
|
|
continue;
|
2011-04-22 01:52:51 +00:00
|
|
|
}
|
2011-08-17 22:42:04 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
if (numberOfBuffersNeedsToChange) {
|
|
|
|
// here we're guaranteed that mQueue is empty
|
|
|
|
freeAllBuffersLocked();
|
|
|
|
mBufferCount = mServerBufferCount;
|
|
|
|
if (mBufferCount < minBufferCountNeeded)
|
|
|
|
mBufferCount = minBufferCountNeeded;
|
|
|
|
mCurrentTexture = INVALID_BUFFER_SLOT;
|
|
|
|
returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
|
|
|
|
}
|
2011-08-17 22:42:04 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// look for a free buffer to give to the client
|
|
|
|
found = INVALID_BUFFER_SLOT;
|
|
|
|
foundSync = INVALID_BUFFER_SLOT;
|
|
|
|
dequeuedCount = 0;
|
|
|
|
for (int i = 0; i < mBufferCount; i++) {
|
|
|
|
const int state = mSlots[i].mBufferState;
|
|
|
|
if (state == BufferSlot::DEQUEUED) {
|
|
|
|
dequeuedCount++;
|
2011-08-17 22:42:04 +00:00
|
|
|
}
|
2011-11-14 22:51:01 +00:00
|
|
|
|
|
|
|
// if buffer is FREE it CANNOT be current
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
|
2011-11-14 22:51:01 +00:00
|
|
|
"dequeueBuffer: buffer %d is both FREE and current!",
|
|
|
|
i);
|
|
|
|
|
|
|
|
if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
|
|
|
|
if (state == BufferSlot::FREE || i == mCurrentTexture) {
|
2011-11-10 00:23:41 +00:00
|
|
|
foundSync = i;
|
2011-11-14 22:51:01 +00:00
|
|
|
if (i != mCurrentTexture) {
|
|
|
|
found = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state == BufferSlot::FREE) {
|
|
|
|
/* We return the oldest of the free buffers to avoid
|
|
|
|
* stalling the producer if possible. This is because
|
|
|
|
* the consumer may still have pending reads of the
|
|
|
|
* buffers in flight.
|
|
|
|
*/
|
|
|
|
bool isOlder = mSlots[i].mFrameNumber <
|
|
|
|
mSlots[found].mFrameNumber;
|
|
|
|
if (found < 0 || isOlder) {
|
|
|
|
foundSync = i;
|
|
|
|
found = i;
|
|
|
|
}
|
2011-11-10 00:23:41 +00:00
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// clients are not allowed to dequeue more than one buffer
|
|
|
|
// if they didn't set a buffer count.
|
|
|
|
if (!mClientBufferCount && dequeuedCount) {
|
|
|
|
ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
|
|
|
|
"setting the buffer count");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// See whether a buffer has been queued since the last
|
|
|
|
// setBufferCount so we know whether to perform the
|
|
|
|
// MIN_UNDEQUEUED_BUFFERS check below.
|
|
|
|
bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
|
|
|
|
if (bufferHasBeenQueued) {
|
|
|
|
// make sure the client is not trying to dequeue more buffers
|
|
|
|
// than allowed.
|
|
|
|
const int avail = mBufferCount - (dequeuedCount+1);
|
|
|
|
if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
|
|
|
|
ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
|
|
|
|
"(dequeued=%d)",
|
|
|
|
MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
|
|
|
|
dequeuedCount);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're in synchronous mode and didn't find a buffer, we need to
|
|
|
|
// wait for some buffers to be consumed
|
|
|
|
tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
|
|
|
|
if (tryAgain) {
|
|
|
|
mDequeueCondition.wait(mMutex);
|
2011-05-24 01:44:04 +00:00
|
|
|
}
|
2011-05-03 02:51:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
|
|
|
|
// foundSync guaranteed to be != INVALID_BUFFER_SLOT
|
|
|
|
found = foundSync;
|
2011-04-26 03:22:14 +00:00
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
if (found == INVALID_BUFFER_SLOT) {
|
|
|
|
// This should not happen.
|
|
|
|
ST_LOGE("dequeueBuffer: no available buffer slots");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
const int buf = found;
|
|
|
|
*outBuf = found;
|
2011-04-26 03:22:14 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
const bool useDefaultSize = !w && !h;
|
|
|
|
if (useDefaultSize) {
|
|
|
|
// use the default size
|
|
|
|
w = mDefaultWidth;
|
|
|
|
h = mDefaultHeight;
|
|
|
|
}
|
2011-04-01 02:10:24 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
const bool updateFormat = (format != 0);
|
|
|
|
if (!updateFormat) {
|
|
|
|
// keep the current (or default) format
|
|
|
|
format = mPixelFormat;
|
|
|
|
}
|
2011-04-01 02:10:24 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
// buffer is now in DEQUEUED (but can also be current at the same time,
|
|
|
|
// if we're in synchronous mode)
|
|
|
|
mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
|
|
|
|
|
|
|
|
const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
|
|
|
|
if ((buffer == NULL) ||
|
|
|
|
(uint32_t(buffer->width) != w) ||
|
|
|
|
(uint32_t(buffer->height) != h) ||
|
|
|
|
(uint32_t(buffer->format) != format) ||
|
|
|
|
((uint32_t(buffer->usage) & usage) != usage))
|
|
|
|
{
|
|
|
|
usage |= GraphicBuffer::USAGE_HW_TEXTURE;
|
|
|
|
status_t error;
|
|
|
|
sp<GraphicBuffer> graphicBuffer(
|
|
|
|
mGraphicBufferAlloc->createGraphicBuffer(
|
|
|
|
w, h, format, usage, &error));
|
|
|
|
if (graphicBuffer == 0) {
|
|
|
|
ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
|
|
|
|
"failed");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (updateFormat) {
|
|
|
|
mPixelFormat = format;
|
|
|
|
}
|
|
|
|
mSlots[buf].mGraphicBuffer = graphicBuffer;
|
|
|
|
mSlots[buf].mRequestBufferCalled = false;
|
|
|
|
mSlots[buf].mFence = EGL_NO_SYNC_KHR;
|
|
|
|
if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
|
|
|
|
eglDestroyImageKHR(mSlots[buf].mEglDisplay,
|
|
|
|
mSlots[buf].mEglImage);
|
|
|
|
mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
|
|
|
|
mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
if (mCurrentTexture == buf) {
|
|
|
|
// The current texture no longer references the buffer in this slot
|
|
|
|
// since we just allocated a new buffer.
|
|
|
|
mCurrentTexture = INVALID_BUFFER_SLOT;
|
|
|
|
}
|
|
|
|
returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
dpy = mSlots[buf].mEglDisplay;
|
|
|
|
fence = mSlots[buf].mFence;
|
|
|
|
mSlots[buf].mFence = EGL_NO_SYNC_KHR;
|
2011-04-01 02:10:24 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
if (fence != EGL_NO_SYNC_KHR) {
|
|
|
|
EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
|
|
|
|
// If something goes wrong, log the error, but return the buffer without
|
|
|
|
// synchronizing access to it. It's too late at this point to abort the
|
|
|
|
// dequeue operation.
|
|
|
|
if (result == EGL_FALSE) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
|
2011-11-14 22:51:01 +00:00
|
|
|
} else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("dequeueBuffer: timeout waiting for fence");
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
2011-11-14 22:51:01 +00:00
|
|
|
eglDestroySyncKHR(dpy, fence);
|
2011-04-01 02:10:24 +00:00
|
|
|
}
|
2011-11-14 22:51:01 +00:00
|
|
|
|
2011-12-09 23:07:44 +00:00
|
|
|
ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
|
|
|
|
mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
|
2011-11-14 22:51:01 +00:00
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
return returnFlags;
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 01:52:51 +00:00
|
|
|
status_t SurfaceTexture::setSynchronousMode(bool enabled) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setSynchronousMode: enabled=%d", enabled);
|
2011-04-22 01:52:51 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-05-03 02:51:12 +00:00
|
|
|
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
status_t err = OK;
|
2011-06-24 04:21:47 +00:00
|
|
|
if (!mAllowSynchronousMode && enabled)
|
|
|
|
return err;
|
|
|
|
|
2011-05-03 02:51:12 +00:00
|
|
|
if (!enabled) {
|
|
|
|
// going to asynchronous mode, drain the queue
|
2011-08-11 00:35:09 +00:00
|
|
|
err = drainQueueLocked();
|
|
|
|
if (err != NO_ERROR)
|
|
|
|
return err;
|
2011-05-03 02:51:12 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 01:52:51 +00:00
|
|
|
if (mSynchronousMode != enabled) {
|
2011-05-03 02:51:12 +00:00
|
|
|
// - if we're going to asynchronous mode, the queue is guaranteed to be
|
|
|
|
// empty here
|
|
|
|
// - if the client set the number of buffers, we're guaranteed that
|
|
|
|
// we have at least 3 (because we don't allow less)
|
2011-04-22 01:52:51 +00:00
|
|
|
mSynchronousMode = enabled;
|
|
|
|
mDequeueCondition.signal();
|
|
|
|
}
|
2011-05-03 02:51:12 +00:00
|
|
|
return err;
|
2011-04-22 01:52:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-19 22:24:46 +00:00
|
|
|
status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
|
|
|
|
uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
|
2011-05-11 22:05:29 +00:00
|
|
|
|
|
|
|
sp<FrameAvailableListener> listener;
|
|
|
|
|
|
|
|
{ // scope for the lock
|
2011-05-19 20:33:00 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-05-19 20:33:00 +00:00
|
|
|
if (buf < 0 || buf >= mBufferCount) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
|
2011-05-19 20:33:00 +00:00
|
|
|
mBufferCount, buf);
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("queueBuffer: slot %d is not owned by the client "
|
|
|
|
"(state=%d)", buf, mSlots[buf].mBufferState);
|
2011-05-19 20:33:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
} else if (buf == mCurrentTexture) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("queueBuffer: slot %d is current!", buf);
|
2011-05-19 20:33:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
} else if (!mSlots[buf].mRequestBufferCalled) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
|
2011-05-19 20:33:00 +00:00
|
|
|
"buffer", buf);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-05-11 22:05:29 +00:00
|
|
|
|
2011-05-19 20:33:00 +00:00
|
|
|
if (mSynchronousMode) {
|
2011-06-27 01:27:47 +00:00
|
|
|
// In synchronous mode we queue all buffers in a FIFO.
|
2011-04-22 01:52:51 +00:00
|
|
|
mQueue.push_back(buf);
|
2011-06-27 01:27:47 +00:00
|
|
|
|
|
|
|
// Synchronous mode always signals that an additional frame should
|
|
|
|
// be consumed.
|
|
|
|
listener = mFrameAvailableListener;
|
2011-04-22 01:52:51 +00:00
|
|
|
} else {
|
2011-06-27 01:27:47 +00:00
|
|
|
// In asynchronous mode we only keep the most recent buffer.
|
2011-05-19 20:33:00 +00:00
|
|
|
if (mQueue.empty()) {
|
|
|
|
mQueue.push_back(buf);
|
2011-06-27 01:27:47 +00:00
|
|
|
|
|
|
|
// Asynchronous mode only signals that a frame should be
|
|
|
|
// consumed if no previous frame was pending. If a frame were
|
|
|
|
// pending then the consumer would have already been notified.
|
|
|
|
listener = mFrameAvailableListener;
|
2011-05-19 20:33:00 +00:00
|
|
|
} else {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
// buffer currently queued is freed
|
|
|
|
mSlots[*front].mBufferState = BufferSlot::FREE;
|
|
|
|
// and we record the new buffer index in the queued list
|
|
|
|
*front = buf;
|
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:33:00 +00:00
|
|
|
mSlots[buf].mBufferState = BufferSlot::QUEUED;
|
|
|
|
mSlots[buf].mCrop = mNextCrop;
|
|
|
|
mSlots[buf].mTransform = mNextTransform;
|
2011-07-13 22:24:42 +00:00
|
|
|
mSlots[buf].mScalingMode = mNextScalingMode;
|
2011-05-19 20:33:00 +00:00
|
|
|
mSlots[buf].mTimestamp = timestamp;
|
2011-11-10 00:23:41 +00:00
|
|
|
mFrameCounter++;
|
|
|
|
mSlots[buf].mFrameNumber = mFrameCounter;
|
|
|
|
|
2011-05-19 20:33:00 +00:00
|
|
|
mDequeueCondition.signal();
|
2011-08-17 19:45:40 +00:00
|
|
|
|
|
|
|
*outWidth = mDefaultWidth;
|
|
|
|
*outHeight = mDefaultHeight;
|
|
|
|
*outTransform = 0;
|
2011-05-11 22:05:29 +00:00
|
|
|
} // scope for the lock
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2011-05-11 22:05:29 +00:00
|
|
|
// call back without lock held
|
|
|
|
if (listener != 0) {
|
|
|
|
listener->onFrameAvailable();
|
2011-01-13 22:43:36 +00:00
|
|
|
}
|
2010-12-20 19:27:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTexture::cancelBuffer(int buf) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("cancelBuffer: slot=%d", buf);
|
2010-12-20 19:27:26 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
|
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-22 01:52:51 +00:00
|
|
|
if (buf < 0 || buf >= mBufferCount) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
|
2011-04-22 01:52:51 +00:00
|
|
|
mBufferCount, buf);
|
2010-12-20 19:27:26 +00:00
|
|
|
return;
|
2011-04-22 01:52:51 +00:00
|
|
|
} else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
|
2011-04-22 01:52:51 +00:00
|
|
|
buf, mSlots[buf].mBufferState);
|
2010-12-20 19:27:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
mSlots[buf].mBufferState = BufferSlot::FREE;
|
2011-11-10 00:23:41 +00:00
|
|
|
mSlots[buf].mFrameNumber = 0;
|
2011-04-22 01:52:51 +00:00
|
|
|
mDequeueCondition.signal();
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 00:33:17 +00:00
|
|
|
status_t SurfaceTexture::setCrop(const Rect& crop) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
|
|
|
|
crop.bottom);
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-01-10 00:33:17 +00:00
|
|
|
mNextCrop = crop;
|
2010-12-20 19:27:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SurfaceTexture::setTransform(uint32_t transform) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setTransform: xform=%#x", transform);
|
2010-12-20 19:27:26 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-01-10 00:33:17 +00:00
|
|
|
mNextTransform = transform;
|
2010-12-20 19:27:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-08-09 02:14:03 +00:00
|
|
|
status_t SurfaceTexture::connect(int api,
|
|
|
|
uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("connect: api=%d", api);
|
2011-07-14 02:12:20 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
|
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("connect: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2011-07-14 02:12:20 +00:00
|
|
|
int err = NO_ERROR;
|
|
|
|
switch (api) {
|
|
|
|
case NATIVE_WINDOW_API_EGL:
|
|
|
|
case NATIVE_WINDOW_API_CPU:
|
|
|
|
case NATIVE_WINDOW_API_MEDIA:
|
|
|
|
case NATIVE_WINDOW_API_CAMERA:
|
|
|
|
if (mConnectedApi != NO_CONNECTED_API) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("connect: already connected (cur=%d, req=%d)",
|
2011-07-14 00:39:11 +00:00
|
|
|
mConnectedApi, api);
|
2011-07-14 02:12:20 +00:00
|
|
|
err = -EINVAL;
|
|
|
|
} else {
|
|
|
|
mConnectedApi = api;
|
2011-08-09 02:14:03 +00:00
|
|
|
*outWidth = mDefaultWidth;
|
|
|
|
*outHeight = mDefaultHeight;
|
|
|
|
*outTransform = 0;
|
2011-07-14 02:12:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SurfaceTexture::disconnect(int api) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("disconnect: api=%d", api);
|
2011-07-14 02:12:20 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
|
|
|
|
if (mAbandoned) {
|
2011-11-18 22:30:20 +00:00
|
|
|
// it is not really an error to disconnect after the surface
|
|
|
|
// has been abandoned, it should just be a no-op.
|
|
|
|
return NO_ERROR;
|
2011-07-19 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 02:12:20 +00:00
|
|
|
int err = NO_ERROR;
|
|
|
|
switch (api) {
|
|
|
|
case NATIVE_WINDOW_API_EGL:
|
|
|
|
case NATIVE_WINDOW_API_CPU:
|
|
|
|
case NATIVE_WINDOW_API_MEDIA:
|
|
|
|
case NATIVE_WINDOW_API_CAMERA:
|
|
|
|
if (mConnectedApi == api) {
|
2011-08-11 00:35:09 +00:00
|
|
|
drainQueueAndFreeBuffersLocked();
|
2011-07-14 02:12:20 +00:00
|
|
|
mConnectedApi = NO_CONNECTED_API;
|
2011-08-26 00:03:30 +00:00
|
|
|
mNextCrop.makeInvalid();
|
|
|
|
mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
|
|
|
|
mNextTransform = 0;
|
2011-08-11 00:35:09 +00:00
|
|
|
mDequeueCondition.signal();
|
2011-07-14 02:12:20 +00:00
|
|
|
} else {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
|
2011-07-14 00:39:11 +00:00
|
|
|
mConnectedApi, api);
|
2011-07-14 02:12:20 +00:00
|
|
|
err = -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGE("disconnect: unknown API %d", api);
|
2011-07-14 02:12:20 +00:00
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-07-13 22:24:42 +00:00
|
|
|
status_t SurfaceTexture::setScalingMode(int mode) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setScalingMode: mode=%d", mode);
|
2011-07-13 22:24:42 +00:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_FREEZE:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
|
|
|
|
break;
|
|
|
|
default:
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGE("unknown scaling mode: %d", mode);
|
2011-07-13 22:24:42 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mNextScalingMode = mode;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
status_t SurfaceTexture::updateTexImage() {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("updateTexImage");
|
2010-12-20 19:27:26 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2011-08-09 02:35:15 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
|
2011-08-11 00:35:09 +00:00
|
|
|
return NO_INIT;
|
2011-08-09 02:35:15 +00:00
|
|
|
}
|
|
|
|
|
2011-06-27 22:41:52 +00:00
|
|
|
// In asynchronous mode the list is guaranteed to be one buffer
|
|
|
|
// deep, while in synchronous mode we use the oldest buffer.
|
2011-04-22 01:52:51 +00:00
|
|
|
if (!mQueue.empty()) {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
2011-06-27 22:41:52 +00:00
|
|
|
int buf = *front;
|
2011-04-22 01:52:51 +00:00
|
|
|
|
2011-01-10 00:33:17 +00:00
|
|
|
// Update the GL texture object.
|
2011-04-22 01:52:51 +00:00
|
|
|
EGLImageKHR image = mSlots[buf].mEglImage;
|
2011-11-14 22:51:01 +00:00
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
2010-12-20 19:27:26 +00:00
|
|
|
if (image == EGL_NO_IMAGE_KHR) {
|
2011-08-09 02:35:15 +00:00
|
|
|
if (mSlots[buf].mGraphicBuffer == 0) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("buffer at slot %d is null", buf);
|
2011-08-11 00:35:09 +00:00
|
|
|
return BAD_VALUE;
|
2011-08-09 02:35:15 +00:00
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
image = createImage(dpy, mSlots[buf].mGraphicBuffer);
|
|
|
|
mSlots[buf].mEglImage = image;
|
|
|
|
mSlots[buf].mEglDisplay = dpy;
|
2011-04-26 21:57:40 +00:00
|
|
|
if (image == EGL_NO_IMAGE_KHR) {
|
|
|
|
// NOTE: if dpy was invalid, createImage() is guaranteed to
|
|
|
|
// fail. so we'd end up here.
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
2011-01-26 19:52:02 +00:00
|
|
|
|
|
|
|
GLint error;
|
|
|
|
while ((error = glGetError()) != GL_NO_ERROR) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
|
2011-01-26 19:52:02 +00:00
|
|
|
}
|
2011-04-12 04:19:55 +00:00
|
|
|
|
2011-09-28 19:13:31 +00:00
|
|
|
glBindTexture(mTexTarget, mTexName);
|
|
|
|
glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
|
2011-04-12 04:19:55 +00:00
|
|
|
|
2011-01-26 19:52:02 +00:00
|
|
|
bool failed = false;
|
|
|
|
while ((error = glGetError()) != GL_NO_ERROR) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("error binding external texture image %p (slot %d): %#04x",
|
2011-04-22 01:52:51 +00:00
|
|
|
image, buf, error);
|
2011-01-26 19:52:02 +00:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
if (failed) {
|
2010-12-20 19:27:26 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-01-13 02:30:40 +00:00
|
|
|
|
2011-11-14 22:51:01 +00:00
|
|
|
if (mCurrentTexture != INVALID_BUFFER_SLOT) {
|
|
|
|
if (mUseFenceSync) {
|
|
|
|
EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
|
|
|
|
NULL);
|
|
|
|
if (fence == EGL_NO_SYNC_KHR) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("updateTexImage: error creating fence: %#x",
|
2011-11-14 22:51:01 +00:00
|
|
|
eglGetError());
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
glFlush();
|
|
|
|
mSlots[mCurrentTexture].mFence = fence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
|
|
|
|
mCurrentTexture,
|
|
|
|
mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
|
|
|
|
buf, mSlots[buf].mGraphicBuffer->handle);
|
2011-10-19 20:36:31 +00:00
|
|
|
|
2011-04-22 01:52:51 +00:00
|
|
|
if (mCurrentTexture != INVALID_BUFFER_SLOT) {
|
2011-06-27 22:41:52 +00:00
|
|
|
// The current buffer becomes FREE if it was still in the queued
|
2011-04-22 01:52:51 +00:00
|
|
|
// state. If it has already been given to the client
|
|
|
|
// (synchronous mode), then it stays in DEQUEUED state.
|
2011-11-14 22:51:01 +00:00
|
|
|
if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
|
2011-04-22 01:52:51 +00:00
|
|
|
mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
|
2011-11-14 22:51:01 +00:00
|
|
|
}
|
2011-04-22 01:52:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-13 02:30:40 +00:00
|
|
|
// Update the SurfaceTexture state.
|
2011-04-22 01:52:51 +00:00
|
|
|
mCurrentTexture = buf;
|
|
|
|
mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
|
2011-05-19 20:33:00 +00:00
|
|
|
mCurrentCrop = mSlots[buf].mCrop;
|
|
|
|
mCurrentTransform = mSlots[buf].mTransform;
|
2011-07-13 22:24:42 +00:00
|
|
|
mCurrentScalingMode = mSlots[buf].mScalingMode;
|
2011-05-19 20:33:00 +00:00
|
|
|
mCurrentTimestamp = mSlots[buf].mTimestamp;
|
2011-06-13 00:03:06 +00:00
|
|
|
computeCurrentTransformMatrix();
|
2011-06-27 22:41:52 +00:00
|
|
|
|
|
|
|
// Now that we've passed the point at which failures can happen,
|
|
|
|
// it's safe to remove the buffer from the front of the queue.
|
|
|
|
mQueue.erase(front);
|
2011-04-22 01:52:51 +00:00
|
|
|
mDequeueCondition.signal();
|
2011-04-12 04:19:55 +00:00
|
|
|
} else {
|
|
|
|
// We always bind the texture even if we don't update its contents.
|
2011-09-28 19:13:31 +00:00
|
|
|
glBindTexture(mTexTarget, mTexName);
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
2011-06-27 22:41:52 +00:00
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-12 04:19:55 +00:00
|
|
|
bool SurfaceTexture::isExternalFormat(uint32_t format)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum SurfaceTexture::getCurrentTextureTarget() const {
|
2011-09-28 19:13:31 +00:00
|
|
|
return mTexTarget;
|
2011-04-12 04:19:55 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 00:33:17 +00:00
|
|
|
void SurfaceTexture::getTransformMatrix(float mtx[16]) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-06-13 00:03:06 +00:00
|
|
|
memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTexture::computeCurrentTransformMatrix() {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("computeCurrentTransformMatrix");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
|
2011-01-14 21:53:31 +00:00
|
|
|
float tx, ty, sx, sy;
|
|
|
|
if (!mCurrentCrop.isEmpty()) {
|
2011-03-11 00:24:46 +00:00
|
|
|
// In order to prevent bilinear sampling at the of the crop rectangle we
|
|
|
|
// may need to shrink it by 2 texels in each direction. Normally this
|
|
|
|
// would just need to take 1/2 a texel off each end, but because the
|
|
|
|
// chroma channels will likely be subsampled we need to chop off a whole
|
|
|
|
// texel. This will cause artifacts if someone does nearest sampling
|
|
|
|
// with 1:1 pixel:texel ratio, but it's impossible to simultaneously
|
|
|
|
// accomodate the bilinear and nearest sampling uses.
|
|
|
|
//
|
|
|
|
// If nearest sampling turns out to be a desirable usage of these
|
|
|
|
// textures then we could add the ability to switch a SurfaceTexture to
|
|
|
|
// nearest-mode. Preferably, however, the image producers (video
|
|
|
|
// decoder, camera, etc.) would simply not use a crop rectangle (or at
|
|
|
|
// least not tell the framework about it) so that the GPU can do the
|
|
|
|
// correct edge behavior.
|
|
|
|
int xshrink = 0, yshrink = 0;
|
|
|
|
if (mCurrentCrop.left > 0) {
|
|
|
|
tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
|
|
|
|
xshrink++;
|
|
|
|
} else {
|
|
|
|
tx = 0.0f;
|
|
|
|
}
|
2011-04-01 02:10:24 +00:00
|
|
|
if (mCurrentCrop.right < int32_t(buf->getWidth())) {
|
2011-03-11 00:24:46 +00:00
|
|
|
xshrink++;
|
|
|
|
}
|
2011-04-01 02:10:24 +00:00
|
|
|
if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
|
2011-03-11 00:24:46 +00:00
|
|
|
ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
|
|
|
|
float(buf->getHeight());
|
|
|
|
yshrink++;
|
|
|
|
} else {
|
|
|
|
ty = 0.0f;
|
|
|
|
}
|
|
|
|
if (mCurrentCrop.top > 0) {
|
|
|
|
yshrink++;
|
|
|
|
}
|
|
|
|
sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
|
|
|
|
sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
|
2011-01-14 21:53:31 +00:00
|
|
|
} else {
|
|
|
|
tx = 0.0f;
|
|
|
|
ty = 0.0f;
|
|
|
|
sx = 1.0f;
|
|
|
|
sy = 1.0f;
|
|
|
|
}
|
2011-01-10 00:33:17 +00:00
|
|
|
float crop[16] = {
|
2011-01-14 21:53:31 +00:00
|
|
|
sx, 0, 0, 0,
|
|
|
|
0, sy, 0, 0,
|
2011-01-10 00:33:17 +00:00
|
|
|
0, 0, 1, 0,
|
2011-03-11 00:24:46 +00:00
|
|
|
tx, ty, 0, 1,
|
2011-01-10 00:33:17 +00:00
|
|
|
};
|
|
|
|
|
2011-01-14 21:53:31 +00:00
|
|
|
float mtxBeforeFlipV[16];
|
|
|
|
mtxMul(mtxBeforeFlipV, crop, xform);
|
|
|
|
|
|
|
|
// SurfaceFlinger expects the top of its window textures to be at a Y
|
|
|
|
// coordinate of 0, so SurfaceTexture must behave the same way. We don't
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2011-02-18 19:02:42 +00:00
|
|
|
nsecs_t SurfaceTexture::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;
|
|
|
|
}
|
|
|
|
|
2011-01-13 22:43:36 +00:00
|
|
|
void SurfaceTexture::setFrameAvailableListener(
|
2011-06-24 16:56:27 +00:00
|
|
|
const sp<FrameAvailableListener>& listener) {
|
2011-10-19 20:36:31 +00:00
|
|
|
ST_LOGV("setFrameAvailableListener");
|
2011-01-13 22:43:36 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-06-24 16:56:27 +00:00
|
|
|
mFrameAvailableListener = listener;
|
2011-01-13 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-11 00:35:09 +00:00
|
|
|
void SurfaceTexture::freeBufferLocked(int i) {
|
|
|
|
mSlots[i].mGraphicBuffer = 0;
|
|
|
|
mSlots[i].mBufferState = BufferSlot::FREE;
|
2011-11-10 00:23:41 +00:00
|
|
|
mSlots[i].mFrameNumber = 0;
|
2011-08-11 00:35:09 +00:00
|
|
|
if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
|
|
|
|
eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
|
|
|
|
mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
|
|
|
|
mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 22:28:58 +00:00
|
|
|
void SurfaceTexture::freeAllBuffersLocked() {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW_IF(!mQueue.isEmpty(),
|
2011-08-10 22:28:58 +00:00
|
|
|
"freeAllBuffersLocked called but mQueue is not empty");
|
2011-08-17 22:42:04 +00:00
|
|
|
mCurrentTexture = INVALID_BUFFER_SLOT;
|
2010-12-20 19:27:26 +00:00
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
2011-08-11 00:35:09 +00:00
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW_IF(!mQueue.isEmpty(),
|
2011-08-11 00:35:09 +00:00
|
|
|
"freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
|
|
|
|
int head = -1;
|
|
|
|
if (!mQueue.empty()) {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
head = *front;
|
|
|
|
}
|
2011-08-17 22:42:04 +00:00
|
|
|
mCurrentTexture = INVALID_BUFFER_SLOT;
|
2011-08-11 00:35:09 +00:00
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
if (i != head) {
|
|
|
|
freeBufferLocked(i);
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 00:35:09 +00:00
|
|
|
status_t SurfaceTexture::drainQueueLocked() {
|
2011-08-10 23:33:23 +00:00
|
|
|
while (mSynchronousMode && !mQueue.isEmpty()) {
|
|
|
|
mDequeueCondition.wait(mMutex);
|
2011-08-11 00:35:09 +00:00
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
|
2011-08-11 00:35:09 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
if (mConnectedApi == NO_CONNECTED_API) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
|
2011-08-11 00:35:09 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2011-08-10 23:33:23 +00:00
|
|
|
}
|
2011-08-11 00:35:09 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
|
|
|
|
status_t err = drainQueueLocked();
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
if (mSynchronousMode) {
|
|
|
|
freeAllBuffersLocked();
|
|
|
|
} else {
|
|
|
|
freeAllBuffersExceptHeadLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
2011-08-10 23:33:23 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
|
|
|
|
const sp<GraphicBuffer>& graphicBuffer) {
|
|
|
|
EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
|
|
|
|
EGLint attrs[] = {
|
|
|
|
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
|
|
|
|
EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
|
2011-04-26 21:57:40 +00:00
|
|
|
if (image == EGL_NO_IMAGE_KHR) {
|
|
|
|
EGLint error = eglGetError();
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("error creating EGLImage: %#x", error);
|
2010-12-20 19:27:26 +00:00
|
|
|
}
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2011-04-12 04:19:55 +00:00
|
|
|
sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentTextureBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect SurfaceTexture::getCurrentCrop() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentCrop;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SurfaceTexture::getCurrentTransform() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentTransform;
|
|
|
|
}
|
|
|
|
|
2011-07-13 22:24:42 +00:00
|
|
|
uint32_t SurfaceTexture::getCurrentScalingMode() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mCurrentScalingMode;
|
|
|
|
}
|
|
|
|
|
2011-11-20 02:04:43 +00:00
|
|
|
bool SurfaceTexture::isSynchronousMode() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mSynchronousMode;
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:20:59 +00:00
|
|
|
int SurfaceTexture::query(int what, int* outValue)
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-07-19 19:08:33 +00:00
|
|
|
|
|
|
|
if (mAbandoned) {
|
2011-09-17 00:30:26 +00:00
|
|
|
ST_LOGE("query: SurfaceTexture has been abandoned!");
|
2011-07-19 19:08:33 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2011-04-20 21:20:59 +00:00
|
|
|
int value;
|
|
|
|
switch (what) {
|
|
|
|
case NATIVE_WINDOW_WIDTH:
|
|
|
|
value = mDefaultWidth;
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_HEIGHT:
|
|
|
|
value = mDefaultHeight;
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_FORMAT:
|
|
|
|
value = mPixelFormat;
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
|
|
|
|
value = mSynchronousMode ?
|
|
|
|
(MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
outValue[0] = value;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2011-04-12 04:19:55 +00:00
|
|
|
|
2011-07-19 19:08:33 +00:00
|
|
|
void SurfaceTexture::abandon() {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2011-08-11 00:35:09 +00:00
|
|
|
mQueue.clear();
|
2011-07-19 19:08:33 +00:00
|
|
|
mAbandoned = true;
|
2011-08-03 22:18:36 +00:00
|
|
|
mCurrentTextureBuf.clear();
|
2011-08-11 00:35:09 +00:00
|
|
|
freeAllBuffersLocked();
|
2011-07-19 19:08:33 +00:00
|
|
|
mDequeueCondition.signal();
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:30:26 +00:00
|
|
|
void SurfaceTexture::setName(const String8& name) {
|
|
|
|
mName = name;
|
|
|
|
}
|
|
|
|
|
2011-05-10 02:08:33 +00:00
|
|
|
void SurfaceTexture::dump(String8& result) const
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
dump(result, "", buffer, 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTexture::dump(String8& result, const char* prefix,
|
|
|
|
char* buffer, size_t SIZE) const
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
snprintf(buffer, SIZE,
|
|
|
|
"%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
|
|
|
|
"mPixelFormat=%d, mTexName=%d\n",
|
2011-09-17 00:30:26 +00:00
|
|
|
prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
|
|
|
|
mDefaultHeight, mPixelFormat, mTexName);
|
2011-05-10 02:08:33 +00:00
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
String8 fifo;
|
|
|
|
int fifoSize = 0;
|
|
|
|
Fifo::const_iterator i(mQueue.begin());
|
|
|
|
while (i != mQueue.end()) {
|
|
|
|
snprintf(buffer, SIZE, "%02d ", *i++);
|
|
|
|
fifoSize++;
|
|
|
|
fifo.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE,
|
2011-07-20 00:58:43 +00:00
|
|
|
"%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
|
2011-05-10 02:08:33 +00:00
|
|
|
"%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
|
|
|
|
,
|
|
|
|
prefix, mCurrentCrop.left,
|
|
|
|
mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
|
2011-07-20 00:58:43 +00:00
|
|
|
mCurrentTransform, mCurrentTexture,
|
2011-09-17 00:30:26 +00:00
|
|
|
prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
|
|
|
|
mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
|
2011-05-10 02:08:33 +00:00
|
|
|
);
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char * operator()(int state) const {
|
|
|
|
switch (state) {
|
|
|
|
case BufferSlot::DEQUEUED: return "DEQUEUED";
|
|
|
|
case BufferSlot::QUEUED: return "QUEUED";
|
|
|
|
case BufferSlot::FREE: return "FREE";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} stateName;
|
|
|
|
|
|
|
|
for (int i=0 ; i<mBufferCount ; i++) {
|
|
|
|
const BufferSlot& slot(mSlots[i]);
|
|
|
|
snprintf(buffer, SIZE,
|
2011-08-08 23:02:13 +00:00
|
|
|
"%s%s[%02d] "
|
|
|
|
"state=%-8s, crop=[%d,%d,%d,%d], "
|
2011-08-09 22:48:43 +00:00
|
|
|
"transform=0x%02x, timestamp=%lld",
|
2011-08-08 23:02:13 +00:00
|
|
|
prefix, (i==mCurrentTexture)?">":" ", i,
|
|
|
|
stateName(slot.mBufferState),
|
2011-09-17 00:30:26 +00:00
|
|
|
slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
|
|
|
|
slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
|
2011-05-10 02:08:33 +00:00
|
|
|
);
|
|
|
|
result.append(buffer);
|
2011-08-09 22:48:43 +00:00
|
|
|
|
|
|
|
const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
|
|
|
|
if (buf != NULL) {
|
|
|
|
snprintf(buffer, SIZE,
|
|
|
|
", %p [%4ux%4u:%4u,%3X]",
|
2011-09-17 00:30:26 +00:00
|
|
|
buf->handle, buf->width, buf->height, buf->stride,
|
|
|
|
buf->format);
|
2011-08-09 22:48:43 +00:00
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
result.append("\n");
|
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];
|
|
|
|
}
|
|
|
|
|
2010-12-20 19:27:26 +00:00
|
|
|
}; // namespace android
|