summaryrefslogtreecommitdiffstats
path: root/libs/gui/Surface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/gui/Surface.cpp')
-rw-r--r--libs/gui/Surface.cpp89
1 files changed, 78 insertions, 11 deletions
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index b8acad2..4b76f98 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -42,7 +42,8 @@ namespace android {
Surface::Surface(
const sp<IGraphicBufferProducer>& bufferProducer,
bool controlledByApp)
- : mGraphicBufferProducer(bufferProducer)
+ : mGraphicBufferProducer(bufferProducer),
+ mGenerationNumber(0)
{
// Initialize the ANativeWindow function pointers.
ANativeWindow::setSwapInterval = hook_setSwapInterval;
@@ -102,6 +103,18 @@ void Surface::allocateBuffers() {
reqHeight, mReqFormat, mReqUsage);
}
+status_t Surface::setGenerationNumber(uint32_t generation) {
+ status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
+ if (result == NO_ERROR) {
+ mGenerationNumber = generation;
+ }
+ return result;
+}
+
+String8 Surface::getConsumerName() const {
+ return mGraphicBufferProducer->getConsumerName();
+}
+
int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
Surface* c = getSelf(window);
return c->setSwapInterval(interval);
@@ -267,6 +280,9 @@ int Surface::cancelBuffer(android_native_buffer_t* buffer,
Mutex::Autolock lock(mMutex);
int i = getSlotFromBufferLocked(buffer);
if (i < 0) {
+ if (fenceFd >= 0) {
+ close(fenceFd);
+ }
return i;
}
sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
@@ -308,6 +324,9 @@ int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
}
int i = getSlotFromBufferLocked(buffer);
if (i < 0) {
+ if (fenceFd >= 0) {
+ close(fenceFd);
+ }
return i;
}
@@ -325,16 +344,61 @@ int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
input.setSurfaceDamage(Region::INVALID_REGION);
} else {
- // 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.
+ // 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;
+ int height = buffer->height;
+ bool rotated90 = (mTransform ^ mStickyTransform) &
+ NATIVE_WINDOW_TRANSFORM_ROT_90;
+ if (rotated90) {
+ std::swap(width, height);
+ }
+
Region flippedRegion;
for (auto rect : mDirtyRegion) {
- auto top = buffer->height - rect.bottom;
- auto bottom = buffer->height - rect.top;
- Rect flippedRect{rect.left, top, rect.right, bottom};
- flippedRegion.orSelf(flippedRect);
+ 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;
+ }
+ }
}
input.setSurfaceDamage(flippedRegion);
@@ -651,7 +715,7 @@ int Surface::disconnect(int api) {
return err;
}
-int Surface::detachNextBuffer(ANativeWindowBuffer** outBuffer,
+int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
sp<Fence>* outFence) {
ATRACE_CALL();
ALOGV("Surface::detachNextBuffer");
@@ -670,7 +734,7 @@ int Surface::detachNextBuffer(ANativeWindowBuffer** outBuffer,
return result;
}
- *outBuffer = buffer.get();
+ *outBuffer = buffer;
if (fence != NULL && fence->isValid()) {
*outFence = fence;
} else {
@@ -688,11 +752,14 @@ int Surface::attachBuffer(ANativeWindowBuffer* buffer)
Mutex::Autolock lock(mMutex);
sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
+ uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
+ graphicBuffer->mGenerationNumber = mGenerationNumber;
int32_t attachedSlot = -1;
status_t result = mGraphicBufferProducer->attachBuffer(
&attachedSlot, graphicBuffer);
if (result != NO_ERROR) {
ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
+ graphicBuffer->mGenerationNumber = priorGeneration;
return result;
}
mSlots[attachedSlot].buffer = graphicBuffer;