VirtualDisplaySurface::~VirtualDisplaySurface() {
}
-status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
+status_t VirtualDisplaySurface::beginFrame() {
if (mDisplayId < 0)
return NO_ERROR;
VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
+ "Unexpected beginFrame() in %s state", dbgStateStr());
+ mDbgState = DBG_STATE_BEGUN;
+
+ uint32_t transformHint, numPendingBuffers;
+ mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
+ &transformHint, &numPendingBuffers);
+
+ return refreshOutputBuffer();
+}
+
+status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
+ if (mDisplayId < 0)
+ return NO_ERROR;
+
+ VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
"Unexpected prepareFrame() in %s state", dbgStateStr());
mDbgState = DBG_STATE_PREPARED;
}
mDbgState = DBG_STATE_HWC;
- status_t result;
- sp<Fence> outFence;
- if (mCompositionType != COMPOSITION_GLES) {
- // Dequeue an output buffer from the sink
- uint32_t transformHint, numPendingBuffers;
- mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
- &transformHint, &numPendingBuffers);
- int sslot;
- result = dequeueBuffer(SOURCE_SINK, 0, &sslot, &outFence, false);
- if (result < 0)
- return result;
- mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
- }
-
if (mCompositionType == COMPOSITION_HWC) {
- // We just dequeued the output buffer, use it for FB as well
+ // Use the output buffer for the FB as well, though conceptually the
+ // FB is unused on this frame.
mFbProducerSlot = mOutputProducerSlot;
- mFbFence = outFence;
- } else if (mCompositionType == COMPOSITION_GLES) {
- mOutputProducerSlot = mFbProducerSlot;
- outFence = mFbFence;
- } else {
- // mFbFence and mFbProducerSlot were set in queueBuffer,
- // and mOutputProducerSlot and outFence were set above when dequeueing
- // the sink buffer.
+ mFbFence = mOutputFence;
}
if (mFbProducerSlot < 0 || mOutputProducerSlot < 0) {
mFbProducerSlot, fbBuffer.get(),
mOutputProducerSlot, outBuffer.get());
- result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
- if (result == NO_ERROR) {
- result = mHwc.setOutputBuffer(mDisplayId, outFence, outBuffer);
- }
-
- return result;
+ return mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
}
void VirtualDisplaySurface::onFrameCommitted() {
VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
+ status_t result = NO_ERROR;
mProducerUsage = usage | GRALLOC_USAGE_HW_COMPOSER;
Source source = fbSourceForCompositionType(mCompositionType);
+
if (source == SOURCE_SINK) {
- mSinkBufferWidth = w;
- mSinkBufferHeight = h;
+ // We already dequeued the output buffer. If the GLES driver wants
+ // something incompatible, we have to cancel and get a new one. This
+ // will mean that HWC will see a different output buffer between
+ // prepare and set, but since we're in GLES-only mode already it
+ // shouldn't matter.
+
+ const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
+ if ((mProducerUsage & ~buf->getUsage()) != 0 ||
+ (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
+ (w != 0 && w != mSinkBufferWidth) ||
+ (h != 0 && h != mSinkBufferHeight)) {
+ VDS_LOGV("dequeueBuffer: output buffer doesn't satisfy GLES "
+ "request, getting a new buffer");
+ result = refreshOutputBuffer();
+ if (result < 0)
+ return result;
+ }
}
- int sslot;
- status_t result = dequeueBuffer(source, format, &sslot, fence, async);
- if (result >= 0) {
- *pslot = mapSource2ProducerSlot(source, sslot);
+ if (source == SOURCE_SINK) {
+ *pslot = mOutputProducerSlot;
+ *fence = mOutputFence;
+ } else {
+ int sslot;
+ result = dequeueBuffer(source, format, &sslot, fence, async);
+ if (result >= 0) {
+ *pslot = mapSource2ProducerSlot(source, sslot);
+ }
}
return result;
}
&transform, &async, &mFbFence);
mFbProducerSlot = pslot;
+ mOutputFence = mFbFence;
}
*output = mQueueBufferOutput;
mSinkBufferWidth = 0;
mSinkBufferHeight = 0;
mFbFence = Fence::NO_FENCE;
+ mOutputFence = Fence::NO_FENCE;
mFbProducerSlot = -1;
mOutputProducerSlot = -1;
}
+status_t VirtualDisplaySurface::refreshOutputBuffer() {
+ if (mOutputProducerSlot >= 0) {
+ mSource[SOURCE_SINK]->cancelBuffer(
+ mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
+ mOutputFence);
+ }
+
+ int sslot;
+ status_t result = dequeueBuffer(SOURCE_SINK, 0, &sslot, &mOutputFence, false);
+ if (result < 0)
+ return result;
+ mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
+
+ result = mHwc.setOutputBuffer(mDisplayId, mOutputFence,
+ mProducerBuffers[mOutputProducerSlot]);
+
+ return result;
+}
+
// This slot mapping function is its own inverse, so two copies are unnecessary.
// Both are kept to make the intent clear where the function is called, and for
// the (unlikely) chance that we switch to a different mapping function.
//
// DisplaySurface interface
//
+ virtual status_t beginFrame();
virtual status_t prepareFrame(CompositionType compositionType);
virtual status_t compositionComplete();
virtual status_t advanceFrame();
int* sslot, sp<Fence>* fence, bool async);
void updateQueueBufferOutput(const QueueBufferOutput& qbo);
void resetPerFrameState();
+ status_t refreshOutputBuffer();
// Both the sink and scratch buffer pools have their own set of slots
// ("source slots", or "sslot"). We have to merge these into the single
// target buffer.
sp<Fence> mFbFence;
+ // mOutputFence is the fence HWC should wait for before writing to the
+ // output buffer.
+ sp<Fence> mOutputFence;
+
// Producer slot numbers for the buffers to use for HWC framebuffer target
// and output.
int mFbProducerSlot;
// +-----------+-------------------+-------------+
// | State | Event || Next State |
// +-----------+-------------------+-------------+
- // | IDLE | prepareFrame || PREPARED |
+ // | IDLE | beginFrame || BEGUN |
+ // | BEGUN | prepareFrame || PREPARED |
// | PREPARED | dequeueBuffer [1] || GLES |
// | PREPARED | advanceFrame [2] || HWC |
// | GLES | queueBuffer || GLES_DONE |
enum DbgState {
// no buffer dequeued, don't know anything about the next frame
DBG_STATE_IDLE,
- // no buffer dequeued, but we know the buffer source for the frame
+ // output buffer dequeued, framebuffer source not yet known
+ DBG_STATE_BEGUN,
+ // output buffer dequeued, framebuffer source known but not provided
+ // to GLES yet.
DBG_STATE_PREPARED,
// GLES driver has a buffer dequeued
DBG_STATE_GLES,