2009-03-04 03:31:44 +00:00
|
|
|
/*
|
2013-02-15 01:11:02 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2009-03-04 03:31:44 +00:00
|
|
|
*
|
|
|
|
* 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 "Surface"
|
2013-02-15 01:11:02 +00:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
//#define LOG_NDEBUG 0
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2012-03-23 21:15:44 +00:00
|
|
|
#include <android/native_window.h>
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <binder/Parcel.h>
|
2010-02-10 01:46:37 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/Trace.h>
|
2014-07-30 00:53:53 +00:00
|
|
|
#include <utils/NativeHandle.h>
|
2011-04-20 21:20:59 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <ui/Fence.h>
|
2015-03-17 23:23:42 +00:00
|
|
|
#include <ui/Region.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2014-03-21 20:05:51 +00:00
|
|
|
#include <gui/IProducerListener.h>
|
2012-02-26 02:48:35 +00:00
|
|
|
#include <gui/ISurfaceComposer.h>
|
|
|
|
#include <gui/SurfaceComposerClient.h>
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <gui/GLConsumer.h>
|
|
|
|
#include <gui/Surface.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <private/gui/ComposerService.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
namespace android {
|
2009-04-16 23:19:50 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
Surface::Surface(
|
2013-07-17 05:56:09 +00:00
|
|
|
const sp<IGraphicBufferProducer>& bufferProducer,
|
|
|
|
bool controlledByApp)
|
2015-06-02 22:45:22 +00:00
|
|
|
: mGraphicBufferProducer(bufferProducer),
|
|
|
|
mGenerationNumber(0)
|
2009-04-16 23:19:50 +00:00
|
|
|
{
|
2013-02-15 01:11:02 +00:00
|
|
|
// Initialize the ANativeWindow function pointers.
|
|
|
|
ANativeWindow::setSwapInterval = hook_setSwapInterval;
|
|
|
|
ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
|
|
|
|
ANativeWindow::cancelBuffer = hook_cancelBuffer;
|
|
|
|
ANativeWindow::queueBuffer = hook_queueBuffer;
|
|
|
|
ANativeWindow::query = hook_query;
|
|
|
|
ANativeWindow::perform = hook_perform;
|
|
|
|
|
|
|
|
ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
|
|
|
|
ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
|
|
|
|
ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
|
|
|
|
ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
|
|
|
|
|
|
|
|
const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
|
|
|
|
const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
|
|
|
|
|
|
|
|
mReqWidth = 0;
|
|
|
|
mReqHeight = 0;
|
|
|
|
mReqFormat = 0;
|
|
|
|
mReqUsage = 0;
|
|
|
|
mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
|
2015-02-20 00:10:43 +00:00
|
|
|
mDataSpace = HAL_DATASPACE_UNKNOWN;
|
2013-02-15 01:11:02 +00:00
|
|
|
mCrop.clear();
|
|
|
|
mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
|
|
|
|
mTransform = 0;
|
2014-06-27 22:51:55 +00:00
|
|
|
mStickyTransform = 0;
|
2013-02-15 01:11:02 +00:00
|
|
|
mDefaultWidth = 0;
|
|
|
|
mDefaultHeight = 0;
|
|
|
|
mUserWidth = 0;
|
|
|
|
mUserHeight = 0;
|
|
|
|
mTransformHint = 0;
|
|
|
|
mConsumerRunningBehind = false;
|
|
|
|
mConnectedToCpu = false;
|
2013-08-21 18:53:37 +00:00
|
|
|
mProducerControlledByApp = controlledByApp;
|
2013-07-19 05:10:56 +00:00
|
|
|
mSwapIntervalZero = false;
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:45:09 +00:00
|
|
|
Surface::~Surface() {
|
|
|
|
if (mConnectedToCpu) {
|
|
|
|
Surface::disconnect(NATIVE_WINDOW_API_CPU);
|
|
|
|
}
|
2009-04-16 23:19:50 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
|
|
|
|
return mGraphicBufferProducer;
|
|
|
|
}
|
|
|
|
|
2014-03-17 08:46:53 +00:00
|
|
|
void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
|
|
|
|
mGraphicBufferProducer->setSidebandStream(stream);
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:13:57 +00:00
|
|
|
void Surface::allocateBuffers() {
|
|
|
|
uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
|
|
|
|
uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
|
2014-12-09 04:42:40 +00:00
|
|
|
mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, reqWidth,
|
|
|
|
reqHeight, mReqFormat, mReqUsage);
|
2014-06-20 20:13:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 22:45:22 +00:00
|
|
|
status_t Surface::setGenerationNumber(uint32_t generation) {
|
|
|
|
status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
|
|
|
|
if (result == NO_ERROR) {
|
|
|
|
mGenerationNumber = generation;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-08 16:32:50 +00:00
|
|
|
String8 Surface::getConsumerName() const {
|
|
|
|
return mGraphicBufferProducer->getConsumerName();
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->setSwapInterval(interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_dequeueBuffer(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer** buffer, int* fenceFd) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->dequeueBuffer(buffer, fenceFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_cancelBuffer(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer* buffer, int fenceFd) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->cancelBuffer(buffer, fenceFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_queueBuffer(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer* buffer, int fenceFd) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->queueBuffer(buffer, fenceFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer** buffer) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
ANativeWindowBuffer* buf;
|
|
|
|
int fenceFd = -1;
|
|
|
|
int result = c->dequeueBuffer(&buf, &fenceFd);
|
|
|
|
sp<Fence> fence(new Fence(fenceFd));
|
2013-05-17 01:03:22 +00:00
|
|
|
int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
|
2013-02-15 01:11:02 +00:00
|
|
|
if (waitResult != OK) {
|
|
|
|
ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
|
|
|
|
waitResult);
|
|
|
|
c->cancelBuffer(buf, -1);
|
|
|
|
return waitResult;
|
2009-04-16 23:19:50 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
*buffer = buf;
|
|
|
|
return result;
|
2009-04-16 23:19:50 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer* buffer) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->cancelBuffer(buffer, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer* buffer) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->lockBuffer_DEPRECATED(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
|
|
|
|
ANativeWindowBuffer* buffer) {
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->queueBuffer(buffer, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_query(const ANativeWindow* window,
|
|
|
|
int what, int* value) {
|
|
|
|
const Surface* c = getSelf(window);
|
|
|
|
return c->query(what, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, operation);
|
|
|
|
Surface* c = getSelf(window);
|
|
|
|
return c->perform(operation, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::setSwapInterval(int interval) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
// EGL specification states:
|
|
|
|
// interval is silently clamped to minimum and maximum implementation
|
|
|
|
// dependent values before being stored.
|
|
|
|
|
|
|
|
if (interval < minSwapInterval)
|
|
|
|
interval = minSwapInterval;
|
|
|
|
|
|
|
|
if (interval > maxSwapInterval)
|
|
|
|
interval = maxSwapInterval;
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
mSwapIntervalZero = (interval == 0);
|
2013-02-15 01:11:02 +00:00
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
return NO_ERROR;
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 22:48:40 +00:00
|
|
|
int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
|
2013-02-15 01:11:02 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::dequeueBuffer");
|
2014-03-01 02:44:21 +00:00
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t reqWidth;
|
|
|
|
uint32_t reqHeight;
|
2014-03-01 02:44:21 +00:00
|
|
|
bool swapIntervalZero;
|
2014-11-18 18:24:03 +00:00
|
|
|
PixelFormat reqFormat;
|
2014-03-01 02:44:21 +00:00
|
|
|
uint32_t reqUsage;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
reqWidth = mReqWidth ? mReqWidth : mUserWidth;
|
|
|
|
reqHeight = mReqHeight ? mReqHeight : mUserHeight;
|
2014-03-01 02:44:21 +00:00
|
|
|
|
|
|
|
swapIntervalZero = mSwapIntervalZero;
|
|
|
|
reqFormat = mReqFormat;
|
|
|
|
reqUsage = mReqUsage;
|
|
|
|
} // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int buf = -1;
|
|
|
|
sp<Fence> fence;
|
2014-03-01 02:44:21 +00:00
|
|
|
status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
|
2014-11-18 18:24:03 +00:00
|
|
|
reqWidth, reqHeight, reqFormat, reqUsage);
|
2014-03-01 02:44:21 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
if (result < 0) {
|
2014-03-01 02:44:21 +00:00
|
|
|
ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
|
2014-11-18 18:24:03 +00:00
|
|
|
"failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat,
|
|
|
|
reqUsage, result);
|
2013-02-15 01:11:02 +00:00
|
|
|
return result;
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
2014-03-01 02:44:21 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
|
2013-08-01 22:48:40 +00:00
|
|
|
|
|
|
|
// this should never happen
|
|
|
|
ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
|
|
|
|
freeAllBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
|
|
|
|
result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
|
|
|
|
if (result != NO_ERROR) {
|
2013-08-01 22:48:40 +00:00
|
|
|
ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
|
2014-10-02 18:09:03 +00:00
|
|
|
mGraphicBufferProducer->cancelBuffer(buf, fence);
|
2013-02-15 01:11:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fence->isValid()) {
|
|
|
|
*fenceFd = fence->dup();
|
|
|
|
if (*fenceFd == -1) {
|
|
|
|
ALOGE("dequeueBuffer: error duping fence: %d", errno);
|
|
|
|
// dup() should never fail; something is badly wrong. Soldier on
|
|
|
|
// and hope for the best; the worst that should happen is some
|
|
|
|
// visible corruption that lasts until the next frame.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*fenceFd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer = gbuf.get();
|
|
|
|
return OK;
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::cancelBuffer(android_native_buffer_t* buffer,
|
|
|
|
int fenceFd) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::cancelBuffer");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
int i = getSlotFromBufferLocked(buffer);
|
|
|
|
if (i < 0) {
|
2015-04-30 13:15:33 +00:00
|
|
|
if (fenceFd >= 0) {
|
|
|
|
close(fenceFd);
|
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
|
|
|
|
mGraphicBufferProducer->cancelBuffer(i, fence);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::getSlotFromBufferLocked(
|
|
|
|
android_native_buffer_t* buffer) const {
|
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
if (mSlots[i].buffer != NULL &&
|
|
|
|
mSlots[i].buffer->handle == buffer->handle) {
|
|
|
|
return i;
|
|
|
|
}
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
|
|
|
|
return BAD_VALUE;
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 02:02:20 +00:00
|
|
|
int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGV("Surface::lockBuffer");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return OK;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::queueBuffer");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
int64_t timestamp;
|
2013-08-16 21:55:39 +00:00
|
|
|
bool isAutoTimestamp = false;
|
2013-02-15 01:11:02 +00:00
|
|
|
if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
|
2013-08-02 22:31:45 +00:00
|
|
|
timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
|
2013-08-16 21:55:39 +00:00
|
|
|
isAutoTimestamp = true;
|
2013-08-02 22:31:45 +00:00
|
|
|
ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
|
|
|
|
timestamp / 1000000.f);
|
2013-02-15 01:11:02 +00:00
|
|
|
} else {
|
|
|
|
timestamp = mTimestamp;
|
|
|
|
}
|
|
|
|
int i = getSlotFromBufferLocked(buffer);
|
|
|
|
if (i < 0) {
|
2015-04-30 13:15:33 +00:00
|
|
|
if (fenceFd >= 0) {
|
|
|
|
close(fenceFd);
|
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure the crop rectangle is entirely inside the buffer.
|
|
|
|
Rect crop;
|
|
|
|
mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
|
|
|
|
|
|
|
|
sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
|
|
|
|
IGraphicBufferProducer::QueueBufferOutput output;
|
2013-08-16 21:55:39 +00:00
|
|
|
IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
|
2015-02-20 00:10:43 +00:00
|
|
|
mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
|
|
|
|
mSwapIntervalZero, fence, mStickyTransform);
|
2015-03-17 23:23:42 +00:00
|
|
|
|
2015-04-21 23:42:49 +00:00
|
|
|
if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
|
2015-03-17 23:23:42 +00:00
|
|
|
input.setSurfaceDamage(Region::INVALID_REGION);
|
|
|
|
} else {
|
2015-06-25 23:10:18 +00:00
|
|
|
// Here we do two things:
|
|
|
|
// 1) The surface damage was specified using the OpenGL ES convention of
|
|
|
|
// the origin being in the bottom-left corner. Here we flip to the
|
|
|
|
// convention that the rest of the system uses (top-left corner) by
|
|
|
|
// subtracting all top/bottom coordinates from the buffer height.
|
|
|
|
// 2) If the buffer is coming in rotated (for example, because the EGL
|
|
|
|
// implementation is reacting to the transform hint coming back from
|
|
|
|
// SurfaceFlinger), the surface damage needs to be rotated the
|
|
|
|
// opposite direction, since it was generated assuming an unrotated
|
|
|
|
// buffer (the app doesn't know that the EGL implementation is
|
|
|
|
// reacting to the transform hint behind its back). The
|
|
|
|
// transformations in the switch statement below apply those
|
|
|
|
// complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
|
|
|
|
|
|
|
|
int width = buffer->width;
|
2015-05-26 20:22:27 +00:00
|
|
|
int height = buffer->height;
|
2015-06-25 23:10:18 +00:00
|
|
|
bool rotated90 = (mTransform ^ mStickyTransform) &
|
|
|
|
NATIVE_WINDOW_TRANSFORM_ROT_90;
|
|
|
|
if (rotated90) {
|
|
|
|
std::swap(width, height);
|
2015-05-26 20:22:27 +00:00
|
|
|
}
|
2015-06-25 23:10:18 +00:00
|
|
|
|
2015-03-17 23:23:42 +00:00
|
|
|
Region flippedRegion;
|
|
|
|
for (auto rect : mDirtyRegion) {
|
2015-06-25 23:10:18 +00:00
|
|
|
int left = rect.left;
|
|
|
|
int right = rect.right;
|
|
|
|
int top = height - rect.bottom; // Flip from OpenGL convention
|
|
|
|
int bottom = height - rect.top; // Flip from OpenGL convention
|
|
|
|
switch (mTransform ^ mStickyTransform) {
|
|
|
|
case NATIVE_WINDOW_TRANSFORM_ROT_90: {
|
|
|
|
// Rotate 270 degrees
|
|
|
|
Rect flippedRect{top, width - right, bottom, width - left};
|
|
|
|
flippedRegion.orSelf(flippedRect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NATIVE_WINDOW_TRANSFORM_ROT_180: {
|
|
|
|
// Rotate 180 degrees
|
|
|
|
Rect flippedRect{width - right, height - bottom,
|
|
|
|
width - left, height - top};
|
|
|
|
flippedRegion.orSelf(flippedRect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NATIVE_WINDOW_TRANSFORM_ROT_270: {
|
|
|
|
// Rotate 90 degrees
|
|
|
|
Rect flippedRect{height - bottom, left,
|
|
|
|
height - top, right};
|
|
|
|
flippedRegion.orSelf(flippedRect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
Rect flippedRect{left, top, right, bottom};
|
|
|
|
flippedRegion.orSelf(flippedRect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 23:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input.setSurfaceDamage(flippedRegion);
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
|
|
|
|
if (err != OK) {
|
|
|
|
ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
|
2011-07-15 22:10:10 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
uint32_t numPendingBuffers = 0;
|
2014-06-27 22:51:55 +00:00
|
|
|
uint32_t hint = 0;
|
|
|
|
output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
|
2013-02-15 01:11:02 +00:00
|
|
|
&numPendingBuffers);
|
|
|
|
|
2014-06-27 22:51:55 +00:00
|
|
|
// Disable transform hint if sticky transform is set.
|
|
|
|
if (mStickyTransform == 0) {
|
|
|
|
mTransformHint = hint;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
mConsumerRunningBehind = (numPendingBuffers >= 2);
|
|
|
|
|
2015-04-21 23:42:49 +00:00
|
|
|
if (!mConnectedToCpu) {
|
|
|
|
// Clear surface damage back to full-buffer
|
|
|
|
mDirtyRegion = Region::INVALID_REGION;
|
|
|
|
}
|
2015-03-17 23:23:42 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
return err;
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
2009-04-16 23:19:50 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::query(int what, int* value) const {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::query");
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
switch (what) {
|
|
|
|
case NATIVE_WINDOW_FORMAT:
|
|
|
|
if (mReqFormat) {
|
2014-11-18 18:24:03 +00:00
|
|
|
*value = static_cast<int>(mReqFormat);
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
|
|
|
|
sp<ISurfaceComposer> composer(
|
|
|
|
ComposerService::getComposerService());
|
|
|
|
if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
|
|
|
|
*value = 1;
|
|
|
|
} else {
|
|
|
|
*value = 0;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
case NATIVE_WINDOW_CONCRETE_TYPE:
|
|
|
|
*value = NATIVE_WINDOW_SURFACE;
|
|
|
|
return NO_ERROR;
|
|
|
|
case NATIVE_WINDOW_DEFAULT_WIDTH:
|
2014-11-18 18:24:03 +00:00
|
|
|
*value = static_cast<int>(
|
|
|
|
mUserWidth ? mUserWidth : mDefaultWidth);
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
case NATIVE_WINDOW_DEFAULT_HEIGHT:
|
2014-11-18 18:24:03 +00:00
|
|
|
*value = static_cast<int>(
|
|
|
|
mUserHeight ? mUserHeight : mDefaultHeight);
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
case NATIVE_WINDOW_TRANSFORM_HINT:
|
2014-11-18 18:24:03 +00:00
|
|
|
*value = static_cast<int>(mTransformHint);
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
|
|
|
|
status_t err = NO_ERROR;
|
|
|
|
if (!mConsumerRunningBehind) {
|
|
|
|
*value = 0;
|
|
|
|
} else {
|
|
|
|
err = mGraphicBufferProducer->query(what, value);
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
mConsumerRunningBehind = *value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2011-07-15 22:10:10 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
return mGraphicBufferProducer->query(what, value);
|
2011-07-15 22:10:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::perform(int operation, va_list args)
|
2011-07-15 22:10:10 +00:00
|
|
|
{
|
2013-02-15 01:11:02 +00:00
|
|
|
int res = NO_ERROR;
|
|
|
|
switch (operation) {
|
|
|
|
case NATIVE_WINDOW_CONNECT:
|
|
|
|
// deprecated. must return NO_ERROR.
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_DISCONNECT:
|
|
|
|
// deprecated. must return NO_ERROR.
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_USAGE:
|
|
|
|
res = dispatchSetUsage(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_CROP:
|
|
|
|
res = dispatchSetCrop(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFER_COUNT:
|
|
|
|
res = dispatchSetBufferCount(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
|
|
|
|
res = dispatchSetBuffersGeometry(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
|
|
|
|
res = dispatchSetBuffersTransform(args);
|
|
|
|
break;
|
2014-06-27 22:51:55 +00:00
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
|
|
|
|
res = dispatchSetBuffersStickyTransform(args);
|
|
|
|
break;
|
2013-02-15 01:11:02 +00:00
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
|
|
|
|
res = dispatchSetBuffersTimestamp(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
|
|
|
|
res = dispatchSetBuffersDimensions(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
|
|
|
|
res = dispatchSetBuffersUserDimensions(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
|
|
|
|
res = dispatchSetBuffersFormat(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_LOCK:
|
|
|
|
res = dispatchLock(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_UNLOCK_AND_POST:
|
|
|
|
res = dispatchUnlockAndPost(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_SET_SCALING_MODE:
|
|
|
|
res = dispatchSetScalingMode(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_API_CONNECT:
|
|
|
|
res = dispatchConnect(args);
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_API_DISCONNECT:
|
|
|
|
res = dispatchDisconnect(args);
|
|
|
|
break;
|
2014-07-30 00:53:53 +00:00
|
|
|
case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
|
|
|
|
res = dispatchSetSidebandStream(args);
|
|
|
|
break;
|
2015-02-20 00:10:43 +00:00
|
|
|
case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
|
|
|
|
res = dispatchSetBuffersDataSpace(args);
|
|
|
|
break;
|
2015-03-17 23:23:42 +00:00
|
|
|
case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
|
|
|
|
res = dispatchSetSurfaceDamage(args);
|
|
|
|
break;
|
2013-02-15 01:11:02 +00:00
|
|
|
default:
|
|
|
|
res = NAME_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
2009-04-17 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchConnect(va_list args) {
|
|
|
|
int api = va_arg(args, int);
|
|
|
|
return connect(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchDisconnect(va_list args) {
|
|
|
|
int api = va_arg(args, int);
|
|
|
|
return disconnect(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchSetUsage(va_list args) {
|
|
|
|
int usage = va_arg(args, int);
|
2014-11-18 18:24:03 +00:00
|
|
|
return setUsage(static_cast<uint32_t>(usage));
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchSetCrop(va_list args) {
|
|
|
|
android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
|
|
|
|
return setCrop(reinterpret_cast<Rect const*>(rect));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchSetBufferCount(va_list args) {
|
|
|
|
size_t bufferCount = va_arg(args, size_t);
|
2014-11-18 18:24:03 +00:00
|
|
|
return setBufferCount(static_cast<int32_t>(bufferCount));
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchSetBuffersGeometry(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t width = va_arg(args, uint32_t);
|
|
|
|
uint32_t height = va_arg(args, uint32_t);
|
|
|
|
PixelFormat format = va_arg(args, PixelFormat);
|
|
|
|
int err = setBuffersDimensions(width, height);
|
2013-02-15 01:11:02 +00:00
|
|
|
if (err != 0) {
|
|
|
|
return err;
|
2010-06-09 02:54:15 +00:00
|
|
|
}
|
2014-11-18 18:24:03 +00:00
|
|
|
return setBuffersFormat(format);
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
2011-07-15 22:10:10 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchSetBuffersDimensions(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t width = va_arg(args, uint32_t);
|
|
|
|
uint32_t height = va_arg(args, uint32_t);
|
|
|
|
return setBuffersDimensions(width, height);
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
2010-06-09 02:54:15 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchSetBuffersUserDimensions(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t width = va_arg(args, uint32_t);
|
|
|
|
uint32_t height = va_arg(args, uint32_t);
|
|
|
|
return setBuffersUserDimensions(width, height);
|
2010-06-09 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchSetBuffersFormat(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
PixelFormat format = va_arg(args, PixelFormat);
|
|
|
|
return setBuffersFormat(format);
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
2010-07-16 00:29:15 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchSetScalingMode(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
int mode = va_arg(args, int);
|
|
|
|
return setScalingMode(mode);
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchSetBuffersTransform(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t transform = va_arg(args, uint32_t);
|
2013-02-15 01:11:02 +00:00
|
|
|
return setBuffersTransform(transform);
|
|
|
|
}
|
|
|
|
|
2014-06-27 22:51:55 +00:00
|
|
|
int Surface::dispatchSetBuffersStickyTransform(va_list args) {
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t transform = va_arg(args, uint32_t);
|
2014-06-27 22:51:55 +00:00
|
|
|
return setBuffersStickyTransform(transform);
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::dispatchSetBuffersTimestamp(va_list args) {
|
|
|
|
int64_t timestamp = va_arg(args, int64_t);
|
|
|
|
return setBuffersTimestamp(timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::dispatchLock(va_list args) {
|
|
|
|
ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
|
|
|
|
ARect* inOutDirtyBounds = va_arg(args, ARect*);
|
|
|
|
return lock(outBuffer, inOutDirtyBounds);
|
|
|
|
}
|
|
|
|
|
2013-11-13 02:02:20 +00:00
|
|
|
int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
|
2013-02-15 01:11:02 +00:00
|
|
|
return unlockAndPost();
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:53:53 +00:00
|
|
|
int Surface::dispatchSetSidebandStream(va_list args) {
|
|
|
|
native_handle_t* sH = va_arg(args, native_handle_t*);
|
|
|
|
sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
|
|
|
|
setSidebandStream(sidebandHandle);
|
|
|
|
return OK;
|
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
|
2015-02-20 00:10:43 +00:00
|
|
|
int Surface::dispatchSetBuffersDataSpace(va_list args) {
|
|
|
|
android_dataspace dataspace =
|
|
|
|
static_cast<android_dataspace>(va_arg(args, int));
|
|
|
|
return setBuffersDataSpace(dataspace);
|
|
|
|
}
|
|
|
|
|
2015-03-17 23:23:42 +00:00
|
|
|
int Surface::dispatchSetSurfaceDamage(va_list args) {
|
|
|
|
android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
|
|
|
|
size_t numRects = va_arg(args, size_t);
|
|
|
|
setSurfaceDamage(rects, numRects);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::connect(int api) {
|
2015-03-03 06:12:37 +00:00
|
|
|
static sp<IProducerListener> listener = new DummyProducerListener();
|
|
|
|
return connect(api, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::connect(int api, const sp<IProducerListener>& listener) {
|
2013-02-15 01:11:02 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::connect");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
IGraphicBufferProducer::QueueBufferOutput output;
|
2014-03-21 20:05:51 +00:00
|
|
|
int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
|
2013-02-15 01:11:02 +00:00
|
|
|
if (err == NO_ERROR) {
|
|
|
|
uint32_t numPendingBuffers = 0;
|
2014-06-27 22:51:55 +00:00
|
|
|
uint32_t hint = 0;
|
|
|
|
output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
|
2013-02-15 01:11:02 +00:00
|
|
|
&numPendingBuffers);
|
2014-06-27 22:51:55 +00:00
|
|
|
|
|
|
|
// Disable transform hint if sticky transform is set.
|
|
|
|
if (mStickyTransform == 0) {
|
|
|
|
mTransformHint = hint;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
mConsumerRunningBehind = (numPendingBuffers >= 2);
|
2010-07-16 00:29:15 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
if (!err && api == NATIVE_WINDOW_API_CPU) {
|
|
|
|
mConnectedToCpu = true;
|
2015-03-17 23:23:42 +00:00
|
|
|
// Clear the dirty region in case we're switching from a non-CPU API
|
|
|
|
mDirtyRegion.clear();
|
|
|
|
} else if (!err) {
|
|
|
|
// Initialize the dirty region for tracking surface damage
|
|
|
|
mDirtyRegion = Region::INVALID_REGION;
|
2010-07-16 00:29:15 +00:00
|
|
|
}
|
2015-03-17 23:23:42 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
return err;
|
2010-07-16 00:29:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:45 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::disconnect(int api) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::disconnect");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
freeAllBuffers();
|
|
|
|
int err = mGraphicBufferProducer->disconnect(api);
|
|
|
|
if (!err) {
|
|
|
|
mReqFormat = 0;
|
|
|
|
mReqWidth = 0;
|
|
|
|
mReqHeight = 0;
|
|
|
|
mReqUsage = 0;
|
|
|
|
mCrop.clear();
|
|
|
|
mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
|
|
|
|
mTransform = 0;
|
2014-06-27 22:51:55 +00:00
|
|
|
mStickyTransform = 0;
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
if (api == NATIVE_WINDOW_API_CPU) {
|
|
|
|
mConnectedToCpu = false;
|
2010-07-16 00:29:15 +00:00
|
|
|
}
|
2010-06-05 01:26:32 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
return err;
|
2010-06-05 01:26:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:06:01 +00:00
|
|
|
int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
|
2015-03-11 18:55:01 +00:00
|
|
|
sp<Fence>* outFence) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::detachNextBuffer");
|
|
|
|
|
|
|
|
if (outBuffer == NULL || outFence == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
sp<GraphicBuffer> buffer(NULL);
|
|
|
|
sp<Fence> fence(NULL);
|
|
|
|
status_t result = mGraphicBufferProducer->detachNextBuffer(
|
|
|
|
&buffer, &fence);
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:06:01 +00:00
|
|
|
*outBuffer = buffer;
|
2015-03-11 18:55:01 +00:00
|
|
|
if (fence != NULL && fence->isValid()) {
|
|
|
|
*outFence = fence;
|
|
|
|
} else {
|
|
|
|
*outFence = Fence::NO_FENCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::attachBuffer(ANativeWindowBuffer* buffer)
|
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::attachBuffer");
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
|
2015-06-02 22:45:22 +00:00
|
|
|
uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
|
|
|
|
graphicBuffer->mGenerationNumber = mGenerationNumber;
|
2015-03-11 18:55:01 +00:00
|
|
|
int32_t attachedSlot = -1;
|
|
|
|
status_t result = mGraphicBufferProducer->attachBuffer(
|
|
|
|
&attachedSlot, graphicBuffer);
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
|
2015-06-02 22:45:22 +00:00
|
|
|
graphicBuffer->mGenerationNumber = priorGeneration;
|
2015-03-11 18:55:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
mSlots[attachedSlot].buffer = graphicBuffer;
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::setUsage(uint32_t reqUsage)
|
2009-04-17 03:04:08 +00:00
|
|
|
{
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGV("Surface::setUsage");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mReqUsage = reqUsage;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Surface::setCrop(Rect const* rect)
|
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
2011-04-20 21:20:59 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
Rect realRect;
|
|
|
|
if (rect == NULL || rect->isEmpty()) {
|
|
|
|
realRect.clear();
|
|
|
|
} else {
|
|
|
|
realRect = *rect;
|
2010-05-26 00:51:34 +00:00
|
|
|
}
|
2013-02-15 01:11:02 +00:00
|
|
|
|
|
|
|
ALOGV("Surface::setCrop rect=[%d %d %d %d]",
|
|
|
|
realRect.left, realRect.top, realRect.right, realRect.bottom);
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mCrop = realRect;
|
|
|
|
return NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::setBufferCount(int bufferCount)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2013-02-15 01:11:02 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setBufferCount");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
|
|
|
|
ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
|
|
|
|
bufferCount, strerror(-err));
|
|
|
|
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
freeAllBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
|
2013-02-15 01:11:02 +00:00
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setBuffersDimensions");
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
if ((width && !height) || (!width && height))
|
2013-02-15 01:11:02 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2014-11-18 18:24:03 +00:00
|
|
|
mReqWidth = width;
|
|
|
|
mReqHeight = height;
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
|
2013-02-15 01:11:02 +00:00
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setBuffersUserDimensions");
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
if ((width && !height) || (!width && height))
|
2013-02-15 01:11:02 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2014-11-18 18:24:03 +00:00
|
|
|
mUserWidth = width;
|
|
|
|
mUserHeight = height;
|
2013-02-15 01:11:02 +00:00
|
|
|
return NO_ERROR;
|
2011-06-22 22:52:53 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
int Surface::setBuffersFormat(PixelFormat format)
|
2013-02-15 01:11:02 +00:00
|
|
|
{
|
|
|
|
ALOGV("Surface::setBuffersFormat");
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mReqFormat = format;
|
|
|
|
return NO_ERROR;
|
2010-05-26 00:51:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::setScalingMode(int mode)
|
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setScalingMode(%d)", mode);
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_FREEZE:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ALOGE("unknown scaling mode: %d", mode);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mScalingMode = mode;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
int Surface::setBuffersTransform(uint32_t transform)
|
2013-02-15 01:11:02 +00:00
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setBuffersTransform");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mTransform = transform;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
int Surface::setBuffersStickyTransform(uint32_t transform)
|
2014-06-27 22:51:55 +00:00
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setBuffersStickyTransform");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mStickyTransform = transform;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
int Surface::setBuffersTimestamp(int64_t timestamp)
|
|
|
|
{
|
|
|
|
ALOGV("Surface::setBuffersTimestamp");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mTimestamp = timestamp;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:10:43 +00:00
|
|
|
int Surface::setBuffersDataSpace(android_dataspace dataSpace)
|
|
|
|
{
|
|
|
|
ALOGV("Surface::setBuffersDataSpace");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mDataSpace = dataSpace;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
void Surface::freeAllBuffers() {
|
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
mSlots[i].buffer = 0;
|
2011-03-14 22:00:06 +00:00
|
|
|
}
|
2011-02-18 19:02:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:23:42 +00:00
|
|
|
void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
ALOGV("Surface::setSurfaceDamage");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2015-04-21 23:42:49 +00:00
|
|
|
if (mConnectedToCpu || numRects == 0) {
|
2015-03-17 23:23:42 +00:00
|
|
|
mDirtyRegion = Region::INVALID_REGION;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDirtyRegion.clear();
|
|
|
|
for (size_t r = 0; r < numRects; ++r) {
|
|
|
|
// We intentionally flip top and bottom here, since because they're
|
|
|
|
// specified with a bottom-left origin, top > bottom, which fails
|
|
|
|
// validation in the Region class. We will fix this up when we flip to a
|
|
|
|
// top-left origin in queueBuffer.
|
|
|
|
Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
|
|
|
|
mDirtyRegion.orSelf(rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// the lock/unlock APIs must be used from the same thread
|
|
|
|
|
|
|
|
static status_t copyBlt(
|
|
|
|
const sp<GraphicBuffer>& dst,
|
|
|
|
const sp<GraphicBuffer>& src,
|
|
|
|
const Region& reg)
|
|
|
|
{
|
|
|
|
// src and dst with, height and format must be identical. no verification
|
|
|
|
// is done here.
|
|
|
|
status_t err;
|
2014-11-18 18:24:03 +00:00
|
|
|
uint8_t* src_bits = NULL;
|
|
|
|
err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
|
|
|
|
reinterpret_cast<void**>(&src_bits));
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
|
|
|
|
|
|
|
|
uint8_t* dst_bits = NULL;
|
2014-11-18 18:24:03 +00:00
|
|
|
err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
|
|
|
|
reinterpret_cast<void**>(&dst_bits));
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
|
|
|
|
|
|
|
|
Region::const_iterator head(reg.begin());
|
|
|
|
Region::const_iterator tail(reg.end());
|
|
|
|
if (head != tail && src_bits && dst_bits) {
|
|
|
|
const size_t bpp = bytesPerPixel(src->format);
|
2014-11-18 18:24:03 +00:00
|
|
|
const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
|
|
|
|
const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
|
2013-02-15 01:11:02 +00:00
|
|
|
|
|
|
|
while (head != tail) {
|
|
|
|
const Rect& r(*head++);
|
2014-11-18 18:24:03 +00:00
|
|
|
int32_t h = r.height();
|
2013-02-15 01:11:02 +00:00
|
|
|
if (h <= 0) continue;
|
2014-11-18 18:24:03 +00:00
|
|
|
size_t size = static_cast<uint32_t>(r.width()) * bpp;
|
|
|
|
uint8_t const * s = src_bits +
|
|
|
|
static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
|
|
|
|
uint8_t * d = dst_bits +
|
|
|
|
static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
|
2013-02-15 01:11:02 +00:00
|
|
|
if (dbpr==sbpr && size==sbpr) {
|
2014-11-18 18:24:03 +00:00
|
|
|
size *= static_cast<size_t>(h);
|
2013-02-15 01:11:02 +00:00
|
|
|
h = 1;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
memcpy(d, s, size);
|
|
|
|
d += dbpr;
|
|
|
|
s += sbpr;
|
|
|
|
} while (--h > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_bits)
|
|
|
|
src->unlock();
|
|
|
|
|
|
|
|
if (dst_bits)
|
|
|
|
dst->unlock();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2010-05-22 00:24:35 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2010-03-11 23:06:54 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
status_t Surface::lock(
|
|
|
|
ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
|
|
|
|
{
|
|
|
|
if (mLockedBuffer != 0) {
|
|
|
|
ALOGE("Surface::lock failed, already locked");
|
|
|
|
return INVALID_OPERATION;
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
2011-08-24 04:09:41 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
if (!mConnectedToCpu) {
|
|
|
|
int err = Surface::connect(NATIVE_WINDOW_API_CPU);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
// we're intending to do software rendering from this point
|
|
|
|
setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
|
2011-08-24 04:09:41 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
ANativeWindowBuffer* out;
|
|
|
|
int fenceFd = -1;
|
|
|
|
status_t err = dequeueBuffer(&out, &fenceFd);
|
|
|
|
ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
|
|
|
|
const Rect bounds(backBuffer->width, backBuffer->height);
|
|
|
|
|
|
|
|
Region newDirtyRegion;
|
|
|
|
if (inOutDirtyBounds) {
|
|
|
|
newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
|
|
|
|
newDirtyRegion.andSelf(bounds);
|
|
|
|
} else {
|
|
|
|
newDirtyRegion.set(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out if we can copy the frontbuffer back
|
2012-08-10 02:00:37 +00:00
|
|
|
int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
|
2013-02-15 01:11:02 +00:00
|
|
|
const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
|
|
|
|
const bool canCopyBack = (frontBuffer != 0 &&
|
|
|
|
backBuffer->width == frontBuffer->width &&
|
|
|
|
backBuffer->height == frontBuffer->height &&
|
|
|
|
backBuffer->format == frontBuffer->format);
|
|
|
|
|
|
|
|
if (canCopyBack) {
|
2012-08-10 02:00:37 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
Region oldDirtyRegion;
|
2015-01-23 08:30:34 +00:00
|
|
|
if(mSlots[backBufferSlot].dirtyRegion.isEmpty()) {
|
|
|
|
oldDirtyRegion.set(bounds);
|
|
|
|
} else {
|
|
|
|
for(int i = 0 ; i < NUM_BUFFER_SLOTS; i++ ) {
|
|
|
|
if(i != backBufferSlot && !mSlots[i].dirtyRegion.isEmpty())
|
|
|
|
oldDirtyRegion.orSelf(mSlots[i].dirtyRegion);
|
|
|
|
}
|
2012-08-10 02:00:37 +00:00
|
|
|
}
|
|
|
|
const Region copyback(oldDirtyRegion.subtract(newDirtyRegion));
|
2013-02-15 01:11:02 +00:00
|
|
|
if (!copyback.isEmpty())
|
|
|
|
copyBlt(backBuffer, frontBuffer, copyback);
|
|
|
|
} else {
|
|
|
|
// if we can't copy-back anything, modify the user's dirty
|
|
|
|
// region to make sure they redraw the whole buffer
|
|
|
|
newDirtyRegion.set(bounds);
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
|
|
|
|
mSlots[i].dirtyRegion.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-08-10 02:00:37 +00:00
|
|
|
mSlots[backBufferSlot].dirtyRegion = newDirtyRegion;
|
2013-02-15 01:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inOutDirtyBounds) {
|
|
|
|
*inOutDirtyBounds = newDirtyRegion.getBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* vaddr;
|
2014-04-01 12:30:53 +00:00
|
|
|
status_t res = backBuffer->lockAsync(
|
2013-02-15 01:11:02 +00:00
|
|
|
GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
|
2014-04-01 12:30:53 +00:00
|
|
|
newDirtyRegion.bounds(), &vaddr, fenceFd);
|
2013-02-15 01:11:02 +00:00
|
|
|
|
|
|
|
ALOGW_IF(res, "failed locking buffer (handle = %p)",
|
|
|
|
backBuffer->handle);
|
|
|
|
|
|
|
|
if (res != 0) {
|
|
|
|
err = INVALID_OPERATION;
|
|
|
|
} else {
|
|
|
|
mLockedBuffer = backBuffer;
|
|
|
|
outBuffer->width = backBuffer->width;
|
|
|
|
outBuffer->height = backBuffer->height;
|
|
|
|
outBuffer->stride = backBuffer->stride;
|
|
|
|
outBuffer->format = backBuffer->format;
|
|
|
|
outBuffer->bits = vaddr;
|
|
|
|
}
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
return err;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
fix [2068105] implement queueBuffer/lockBuffer/dequeueBuffer properly
Rewrote SurfaceFlinger's buffer management from the ground-up.
The design now support an arbitrary number of buffers per surface, however the current implementation is limited to four. Currently only 2 buffers are used in practice.
The main new feature is to be able to dequeue all buffers at once (very important when there are only two).
A client can dequeue all buffers until there are none available, it can lock all buffers except the last one that is used for composition. The client will block then, until a new buffer is enqueued.
The current implementation requires that buffers are locked in the same order they are dequeued and enqueued in the same order they are locked. Only one buffer can be locked at a time.
eg. Allowed sequence: DQ, DQ, LOCK, Q, LOCK, Q
eg. Forbidden sequence: DQ, DQ, LOCK, LOCK, Q, Q
2009-09-07 23:32:45 +00:00
|
|
|
|
2013-02-15 01:11:02 +00:00
|
|
|
status_t Surface::unlockAndPost()
|
|
|
|
{
|
|
|
|
if (mLockedBuffer == 0) {
|
|
|
|
ALOGE("Surface::unlockAndPost failed, no locked buffer");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:30:53 +00:00
|
|
|
int fd = -1;
|
|
|
|
status_t err = mLockedBuffer->unlockAsync(&fd);
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
|
|
|
|
|
2014-04-01 12:30:53 +00:00
|
|
|
err = queueBuffer(mLockedBuffer.get(), fd);
|
2013-02-15 01:11:02 +00:00
|
|
|
ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
|
|
|
|
mLockedBuffer->handle, strerror(-err));
|
|
|
|
|
|
|
|
mPostedBuffer = mLockedBuffer;
|
|
|
|
mLockedBuffer = 0;
|
|
|
|
return err;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|