BufferQueue: Add StreamSplitter
Adds a StreamSplitter class, that takes one IGraphicBufferConsumer
interface and multiple IGraphicBufferProducer interfaces and
implements a one-to-many broadcast of GraphicBuffers (while managing
fences correctly).
Change-Id: I38ecdf3e311ac521bc781c30dde0cc382a4376a3
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk
index 04a8a8d..ca94aa3 100644
--- a/libs/gui/Android.mk
+++ b/libs/gui/Android.mk
@@ -30,6 +30,7 @@
Sensor.cpp \
SensorEventQueue.cpp \
SensorManager.cpp \
+ StreamSplitter.cpp \
Surface.cpp \
SurfaceControl.cpp \
SurfaceComposerClient.cpp \
diff --git a/libs/gui/BufferQueueConsumer.cpp b/libs/gui/BufferQueueConsumer.cpp
index 995ed5e..e811ee0 100644
--- a/libs/gui/BufferQueueConsumer.cpp
+++ b/libs/gui/BufferQueueConsumer.cpp
@@ -243,11 +243,27 @@
mSlots[*outSlot].mGraphicBuffer = buffer;
mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
mSlots[*outSlot].mAttachedByConsumer = true;
- mSlots[*outSlot].mAcquireCalled = true;
mSlots[*outSlot].mNeedsCleanupOnRelease = false;
mSlots[*outSlot].mFence = Fence::NO_FENCE;
mSlots[*outSlot].mFrameNumber = 0;
+ // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
+ // GraphicBuffer pointer on the next acquireBuffer call, which decreases
+ // Binder traffic by not un/flattening the GraphicBuffer. However, it
+ // requires that the consumer maintain a cached copy of the slot <--> buffer
+ // mappings, which is why the consumer doesn't need the valid pointer on
+ // acquire.
+ //
+ // The StreamSplitter is one of the primary users of the attach/detach
+ // logic, and while it is running, all buffers it acquires are immediately
+ // detached, and all buffers it eventually releases are ones that were
+ // attached (as opposed to having been obtained from acquireBuffer), so it
+ // doesn't make sense to maintain the slot/buffer mappings, which would
+ // become invalid for every buffer during detach/attach. By setting this to
+ // false, the valid GraphicBuffer pointer will always be sent with acquire
+ // for attached buffers.
+ mSlots[*outSlot].mAcquireCalled = false;
+
return NO_ERROR;
}
diff --git a/libs/gui/BufferQueueCore.cpp b/libs/gui/BufferQueueCore.cpp
index 71ba4e7..593b6f1 100644
--- a/libs/gui/BufferQueueCore.cpp
+++ b/libs/gui/BufferQueueCore.cpp
@@ -191,7 +191,7 @@
mSlots[slot].mNeedsCleanupOnRelease = true;
}
mSlots[slot].mBufferState = BufferSlot::FREE;
- mSlots[slot].mFrameNumber = 0;
+ mSlots[slot].mFrameNumber = UINT32_MAX;
mSlots[slot].mAcquireCalled = false;
// Destroy fence as BufferQueue now takes ownership
diff --git a/libs/gui/IGraphicBufferConsumer.cpp b/libs/gui/IGraphicBufferConsumer.cpp
index 1b19626..9ef2544 100644
--- a/libs/gui/IGraphicBufferConsumer.cpp
+++ b/libs/gui/IGraphicBufferConsumer.cpp
@@ -14,12 +14,6 @@
* limitations under the License.
*/
-#define EGL_EGLEXT_PROTOTYPES
-
-#include <EGL/egl.h>
-#include <EGL/eglext.h>
-
-
#include <stdint.h>
#include <sys/types.h>
diff --git a/libs/gui/StreamSplitter.cpp b/libs/gui/StreamSplitter.cpp
new file mode 100644
index 0000000..83e08fb
--- /dev/null
+++ b/libs/gui/StreamSplitter.cpp
@@ -0,0 +1,281 @@
+/*
+ * Copyright 2014 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 "StreamSplitter"
+#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+//#define LOG_NDEBUG 0
+
+#include <gui/IGraphicBufferConsumer.h>
+#include <gui/IGraphicBufferProducer.h>
+#include <gui/StreamSplitter.h>
+
+#include <ui/GraphicBuffer.h>
+
+#include <binder/ProcessState.h>
+
+#include <utils/Trace.h>
+
+namespace android {
+
+status_t StreamSplitter::createSplitter(
+ const sp<IGraphicBufferConsumer>& inputQueue,
+ sp<StreamSplitter>* outSplitter) {
+ if (inputQueue == NULL) {
+ ALOGE("createSplitter: inputQueue must not be NULL");
+ return BAD_VALUE;
+ }
+ if (outSplitter == NULL) {
+ ALOGE("createSplitter: outSplitter must not be NULL");
+ return BAD_VALUE;
+ }
+
+ sp<StreamSplitter> splitter(new StreamSplitter(inputQueue));
+ status_t status = splitter->mInput->consumerConnect(splitter, false);
+ if (status == NO_ERROR) {
+ splitter->mInput->setConsumerName(String8("StreamSplitter"));
+ *outSplitter = splitter;
+ }
+ return status;
+}
+
+StreamSplitter::StreamSplitter(const sp<IGraphicBufferConsumer>& inputQueue)
+ : mIsAbandoned(false), mMutex(), mReleaseCondition(),
+ mOutstandingBuffers(0), mInput(inputQueue), mOutputs(), mBuffers() {}
+
+StreamSplitter::~StreamSplitter() {
+ mInput->consumerDisconnect();
+ Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin();
+ for (; output != mOutputs.end(); ++output) {
+ (*output)->disconnect(NATIVE_WINDOW_API_CPU);
+ }
+
+ if (mBuffers.size() > 0) {
+ ALOGE("%d buffers still being tracked", mBuffers.size());
+ }
+}
+
+status_t StreamSplitter::addOutput(
+ const sp<IGraphicBufferProducer>& outputQueue) {
+ if (outputQueue == NULL) {
+ ALOGE("addOutput: outputQueue must not be NULL");
+ return BAD_VALUE;
+ }
+
+ Mutex::Autolock lock(mMutex);
+
+ IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
+ sp<OutputListener> listener(new OutputListener(this, outputQueue));
+ outputQueue->asBinder()->linkToDeath(listener);
+ status_t status = outputQueue->connect(listener, NATIVE_WINDOW_API_CPU,
+ /* producerControlledByApp */ false, &queueBufferOutput);
+ if (status != NO_ERROR) {
+ ALOGE("addOutput: failed to connect (%d)", status);
+ return status;
+ }
+
+ mOutputs.push_back(outputQueue);
+
+ return NO_ERROR;
+}
+
+void StreamSplitter::setName(const String8 &name) {
+ Mutex::Autolock lock(mMutex);
+ mInput->setConsumerName(name);
+}
+
+void StreamSplitter::onFrameAvailable() {
+ ATRACE_CALL();
+ Mutex::Autolock lock(mMutex);
+
+ // The current policy is that if any one consumer is consuming buffers too
+ // slowly, the splitter will stall the rest of the outputs by not acquiring
+ // any more buffers from the input. This will cause back pressure on the
+ // input queue, slowing down its producer.
+
+ // If there are too many outstanding buffers, we block until a buffer is
+ // released back to the input in onBufferReleased
+ while (mOutstandingBuffers >= MAX_OUTSTANDING_BUFFERS) {
+ mReleaseCondition.wait(mMutex);
+
+ // If the splitter is abandoned while we are waiting, the release
+ // condition variable will be broadcast, and we should just return
+ // without attempting to do anything more (since the input queue will
+ // also be abandoned).
+ if (mIsAbandoned) {
+ return;
+ }
+ }
+ ++mOutstandingBuffers;
+
+ // Acquire and detach the buffer from the input
+ IGraphicBufferConsumer::BufferItem bufferItem;
+ status_t status = mInput->acquireBuffer(&bufferItem, /* presentWhen */ 0);
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "acquiring buffer from input failed (%d)", status);
+
+ ALOGV("acquired buffer %#llx from input",
+ bufferItem.mGraphicBuffer->getId());
+
+ status = mInput->detachBuffer(bufferItem.mBuf);
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "detaching buffer from input failed (%d)", status);
+
+ // Initialize our reference count for this buffer
+ mBuffers.add(bufferItem.mGraphicBuffer->getId(),
+ new BufferTracker(bufferItem.mGraphicBuffer));
+
+ IGraphicBufferProducer::QueueBufferInput queueInput(
+ bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
+ bufferItem.mCrop, bufferItem.mScalingMode,
+ bufferItem.mTransform, bufferItem.mIsDroppable,
+ bufferItem.mFence);
+
+ // Attach and queue the buffer to each of the outputs
+ Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin();
+ for (; output != mOutputs.end(); ++output) {
+ int slot;
+ status = (*output)->attachBuffer(&slot, bufferItem.mGraphicBuffer);
+ if (status == NO_INIT) {
+ // If we just discovered that this output has been abandoned, note
+ // that, increment the release count so that we still release this
+ // buffer eventually, and move on to the next output
+ onAbandonedLocked();
+ mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())->
+ incrementReleaseCountLocked();
+ continue;
+ } else {
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "attaching buffer to output failed (%d)", status);
+ }
+
+ IGraphicBufferProducer::QueueBufferOutput queueOutput;
+ status = (*output)->queueBuffer(slot, queueInput, &queueOutput);
+ if (status == NO_INIT) {
+ // If we just discovered that this output has been abandoned, note
+ // that, increment the release count so that we still release this
+ // buffer eventually, and move on to the next output
+ onAbandonedLocked();
+ mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())->
+ incrementReleaseCountLocked();
+ continue;
+ } else {
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "queueing buffer to output failed (%d)", status);
+ }
+
+ ALOGV("queued buffer %#llx to output %p",
+ bufferItem.mGraphicBuffer->getId(), output->get());
+ }
+}
+
+void StreamSplitter::onBufferReleasedByOutput(
+ const sp<IGraphicBufferProducer>& from) {
+ ATRACE_CALL();
+ Mutex::Autolock lock(mMutex);
+
+ sp<GraphicBuffer> buffer;
+ sp<Fence> fence;
+ status_t status = from->detachNextBuffer(&buffer, &fence);
+ if (status == NO_INIT) {
+ // If we just discovered that this output has been abandoned, note that,
+ // but we can't do anything else, since buffer is invalid
+ onAbandonedLocked();
+ return;
+ } else {
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "detaching buffer from output failed (%d)", status);
+ }
+
+ ALOGV("detached buffer %#llx from output %p", buffer->getId(), from.get());
+
+ const sp<BufferTracker>& tracker = mBuffers.editValueFor(buffer->getId());
+
+ // Merge the release fence of the incoming buffer so that the fence we send
+ // back to the input includes all of the outputs' fences
+ tracker->mergeFence(fence);
+
+ // Check to see if this is the last outstanding reference to this buffer
+ size_t releaseCount = tracker->incrementReleaseCountLocked();
+ ALOGV("buffer %#llx reference count %d (of %d)", buffer->getId(),
+ releaseCount, mOutputs.size());
+ if (releaseCount < mOutputs.size()) {
+ return;
+ }
+
+ // If we've been abandoned, we can't return the buffer to the input, so just
+ // stop tracking it and move on
+ if (mIsAbandoned) {
+ mBuffers.removeItem(buffer->getId());
+ return;
+ }
+
+ // Attach and release the buffer back to the input
+ int consumerSlot;
+ status = mInput->attachBuffer(&consumerSlot, tracker->getBuffer());
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "attaching buffer to input failed (%d)", status);
+
+ status = mInput->releaseBuffer(consumerSlot, /* frameNumber */ 0,
+ EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker->getMergedFence());
+ LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
+ "releasing buffer to input failed (%d)", status);
+
+ ALOGV("released buffer %#llx to input", buffer->getId());
+
+ // We no longer need to track the buffer once it has been returned to the
+ // input
+ mBuffers.removeItem(buffer->getId());
+
+ // Notify any waiting onFrameAvailable calls
+ --mOutstandingBuffers;
+ mReleaseCondition.signal();
+}
+
+void StreamSplitter::onAbandonedLocked() {
+ ALOGE("one of my outputs has abandoned me");
+ if (!mIsAbandoned) {
+ mInput->consumerDisconnect();
+ }
+ mIsAbandoned = true;
+ mReleaseCondition.broadcast();
+}
+
+StreamSplitter::OutputListener::OutputListener(
+ const sp<StreamSplitter>& splitter,
+ const sp<IGraphicBufferProducer>& output)
+ : mSplitter(splitter), mOutput(output) {}
+
+StreamSplitter::OutputListener::~OutputListener() {}
+
+void StreamSplitter::OutputListener::onBufferReleased() {
+ mSplitter->onBufferReleasedByOutput(mOutput);
+}
+
+void StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
+ Mutex::Autolock lock(mSplitter->mMutex);
+ mSplitter->onAbandonedLocked();
+}
+
+StreamSplitter::BufferTracker::BufferTracker(const sp<GraphicBuffer>& buffer)
+ : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mReleaseCount(0) {}
+
+StreamSplitter::BufferTracker::~BufferTracker() {}
+
+void StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
+ mMergedFence = Fence::merge(String8("StreamSplitter"), mMergedFence, with);
+}
+
+} // namespace android
diff --git a/libs/gui/tests/Android.mk b/libs/gui/tests/Android.mk
index 2eeb5c7..e460290 100644
--- a/libs/gui/tests/Android.mk
+++ b/libs/gui/tests/Android.mk
@@ -14,6 +14,7 @@
IGraphicBufferProducer_test.cpp \
MultiTextureConsumer_test.cpp \
SRGB_test.cpp \
+ StreamSplitter_test.cpp \
SurfaceTextureClient_test.cpp \
SurfaceTextureFBO_test.cpp \
SurfaceTextureGLThreadToGL_test.cpp \
diff --git a/libs/gui/tests/BufferQueue_test.cpp b/libs/gui/tests/BufferQueue_test.cpp
index fe8f8b1..c781366 100644
--- a/libs/gui/tests/BufferQueue_test.cpp
+++ b/libs/gui/tests/BufferQueue_test.cpp
@@ -301,7 +301,7 @@
ASSERT_EQ(BAD_VALUE, mConsumer->attachBuffer(&newSlot, NULL));
ASSERT_EQ(OK, mConsumer->attachBuffer(&newSlot, item.mGraphicBuffer));
- ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mBuf, 0, EGL_NO_DISPLAY,
+ ASSERT_EQ(OK, mConsumer->releaseBuffer(newSlot, 0, EGL_NO_DISPLAY,
EGL_NO_SYNC_KHR, Fence::NO_FENCE));
ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
diff --git a/libs/gui/tests/StreamSplitter_test.cpp b/libs/gui/tests/StreamSplitter_test.cpp
new file mode 100644
index 0000000..32ec90d
--- /dev/null
+++ b/libs/gui/tests/StreamSplitter_test.cpp
@@ -0,0 +1,246 @@
+/*
+ * Copyright 2014 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 "StreamSplitter_test"
+//#define LOG_NDEBUG 0
+
+#include <gui/BufferQueue.h>
+#include <gui/IConsumerListener.h>
+#include <gui/ISurfaceComposer.h>
+#include <gui/StreamSplitter.h>
+#include <private/gui/ComposerService.h>
+
+#include <gtest/gtest.h>
+
+namespace android {
+
+class StreamSplitterTest : public ::testing::Test {
+
+protected:
+ StreamSplitterTest() {
+ const ::testing::TestInfo* const testInfo =
+ ::testing::UnitTest::GetInstance()->current_test_info();
+ ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
+ testInfo->name());
+ }
+
+ ~StreamSplitterTest() {
+ const ::testing::TestInfo* const testInfo =
+ ::testing::UnitTest::GetInstance()->current_test_info();
+ ALOGV("End test: %s.%s", testInfo->test_case_name(),
+ testInfo->name());
+ }
+};
+
+struct DummyListener : public BnConsumerListener {
+ virtual void onFrameAvailable() {}
+ virtual void onBuffersReleased() {}
+ virtual void onSidebandStreamChanged() {}
+};
+
+class CountedAllocator : public BnGraphicBufferAlloc {
+public:
+ CountedAllocator() : mAllocCount(0) {
+ sp<ISurfaceComposer> composer(ComposerService::getComposerService());
+ mAllocator = composer->createGraphicBufferAlloc();
+ }
+
+ virtual ~CountedAllocator() {}
+
+ virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
+ PixelFormat format, uint32_t usage, status_t* error) {
+ ++mAllocCount;
+ sp<GraphicBuffer> buffer = mAllocator->createGraphicBuffer(w, h, format,
+ usage, error);
+ return buffer;
+ }
+
+ int getAllocCount() const { return mAllocCount; }
+
+private:
+ sp<IGraphicBufferAlloc> mAllocator;
+ int mAllocCount;
+};
+
+TEST_F(StreamSplitterTest, OneInputOneOutput) {
+ sp<CountedAllocator> allocator(new CountedAllocator);
+
+ sp<IGraphicBufferProducer> inputProducer;
+ sp<IGraphicBufferConsumer> inputConsumer;
+ BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator);
+
+ sp<IGraphicBufferProducer> outputProducer;
+ sp<IGraphicBufferConsumer> outputConsumer;
+ BufferQueue::createBufferQueue(&outputProducer, &outputConsumer, allocator);
+ ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false));
+
+ sp<StreamSplitter> splitter;
+ status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
+ ASSERT_EQ(OK, status);
+ ASSERT_EQ(OK, splitter->addOutput(outputProducer));
+
+ IGraphicBufferProducer::QueueBufferOutput qbOutput;
+ ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
+ NATIVE_WINDOW_API_CPU, false, &qbOutput));
+
+ int slot;
+ sp<Fence> fence;
+ sp<GraphicBuffer> buffer;
+ ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
+ inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
+ GRALLOC_USAGE_SW_WRITE_OFTEN));
+ ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
+
+ uint32_t* dataIn;
+ ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
+ reinterpret_cast<void**>(&dataIn)));
+ *dataIn = 0x12345678;
+ ASSERT_EQ(OK, buffer->unlock());
+
+ IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
+ Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
+ Fence::NO_FENCE);
+ ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
+
+ IGraphicBufferConsumer::BufferItem item;
+ ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0));
+
+ uint32_t* dataOut;
+ ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
+ reinterpret_cast<void**>(&dataOut)));
+ ASSERT_EQ(*dataOut, 0x12345678);
+ ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
+
+ ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mBuf, item.mFrameNumber,
+ EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE));
+
+ ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
+ inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
+ GRALLOC_USAGE_SW_WRITE_OFTEN));
+
+ ASSERT_EQ(1, allocator->getAllocCount());
+}
+
+TEST_F(StreamSplitterTest, OneInputMultipleOutputs) {
+ const int NUM_OUTPUTS = 4;
+ sp<CountedAllocator> allocator(new CountedAllocator);
+
+ sp<IGraphicBufferProducer> inputProducer;
+ sp<IGraphicBufferConsumer> inputConsumer;
+ BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator);
+
+ sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {};
+ sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {};
+ for (int output = 0; output < NUM_OUTPUTS; ++output) {
+ BufferQueue::createBufferQueue(&outputProducers[output],
+ &outputConsumers[output], allocator);
+ ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(
+ new DummyListener, false));
+ }
+
+ sp<StreamSplitter> splitter;
+ status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
+ ASSERT_EQ(OK, status);
+ for (int output = 0; output < NUM_OUTPUTS; ++output) {
+ ASSERT_EQ(OK, splitter->addOutput(outputProducers[output]));
+ }
+
+ IGraphicBufferProducer::QueueBufferOutput qbOutput;
+ ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
+ NATIVE_WINDOW_API_CPU, false, &qbOutput));
+
+ int slot;
+ sp<Fence> fence;
+ sp<GraphicBuffer> buffer;
+ ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
+ inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
+ GRALLOC_USAGE_SW_WRITE_OFTEN));
+ ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
+
+ uint32_t* dataIn;
+ ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
+ reinterpret_cast<void**>(&dataIn)));
+ *dataIn = 0x12345678;
+ ASSERT_EQ(OK, buffer->unlock());
+
+ IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
+ Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
+ Fence::NO_FENCE);
+ ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
+
+ for (int output = 0; output < NUM_OUTPUTS; ++output) {
+ IGraphicBufferConsumer::BufferItem item;
+ ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0));
+
+ uint32_t* dataOut;
+ ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
+ reinterpret_cast<void**>(&dataOut)));
+ ASSERT_EQ(*dataOut, 0x12345678);
+ ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
+
+ ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mBuf,
+ item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
+ Fence::NO_FENCE));
+ }
+
+ ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
+ inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
+ GRALLOC_USAGE_SW_WRITE_OFTEN));
+
+ ASSERT_EQ(1, allocator->getAllocCount());
+}
+
+TEST_F(StreamSplitterTest, OutputAbandonment) {
+ sp<IGraphicBufferProducer> inputProducer;
+ sp<IGraphicBufferConsumer> inputConsumer;
+ BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
+
+ sp<IGraphicBufferProducer> outputProducer;
+ sp<IGraphicBufferConsumer> outputConsumer;
+ BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
+ ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false));
+
+ sp<StreamSplitter> splitter;
+ status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
+ ASSERT_EQ(OK, status);
+ ASSERT_EQ(OK, splitter->addOutput(outputProducer));
+
+ IGraphicBufferProducer::QueueBufferOutput qbOutput;
+ ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
+ NATIVE_WINDOW_API_CPU, false, &qbOutput));
+
+ int slot;
+ sp<Fence> fence;
+ sp<GraphicBuffer> buffer;
+ ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
+ inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
+ GRALLOC_USAGE_SW_WRITE_OFTEN));
+ ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
+
+ // Abandon the output
+ outputConsumer->consumerDisconnect();
+
+ IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
+ Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
+ Fence::NO_FENCE);
+ ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
+
+ // Input should be abandoned
+ ASSERT_EQ(NO_INIT, inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0,
+ 0, GRALLOC_USAGE_SW_WRITE_OFTEN));
+}
+
+} // namespace android