VirtualDisplaySurface.cpp revision 82c6bcc9705eabcaf5b9e45bc81867b0e2d61a02
1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18#include "VirtualDisplaySurface.h"
19#include "HWComposer.h"
20
21// ---------------------------------------------------------------------------
22namespace android {
23// ---------------------------------------------------------------------------
24
25#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
26static const bool sForceHwcCopy = true;
27#else
28static const bool sForceHwcCopy = false;
29#endif
30
31#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
32        mDisplayName.string(), ##__VA_ARGS__)
33#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
34        mDisplayName.string(), ##__VA_ARGS__)
35#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
36        mDisplayName.string(), ##__VA_ARGS__)
37
38static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
39    switch (type) {
40        case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
41        case DisplaySurface::COMPOSITION_GLES:    return "GLES";
42        case DisplaySurface::COMPOSITION_HWC:     return "HWC";
43        case DisplaySurface::COMPOSITION_MIXED:   return "MIXED";
44        default:                                  return "<INVALID>";
45    }
46}
47
48VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
49        const sp<IGraphicBufferProducer>& sink,
50        const sp<IGraphicBufferProducer>& bqProducer,
51        const sp<IGraphicBufferConsumer>& bqConsumer,
52        const String8& name)
53:   ConsumerBase(bqConsumer),
54    mHwc(hwc),
55    mDisplayId(dispId),
56    mDisplayName(name),
57    mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
58    mProducerSlotSource(0),
59    mDbgState(DBG_STATE_IDLE),
60    mDbgLastCompositionType(COMPOSITION_UNKNOWN),
61    mMustRecompose(false)
62{
63    mSource[SOURCE_SINK] = sink;
64    mSource[SOURCE_SCRATCH] = bqProducer;
65
66    resetPerFrameState();
67
68    int sinkWidth, sinkHeight;
69    sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
70    sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
71    mSinkBufferWidth = sinkWidth;
72    mSinkBufferHeight = sinkHeight;
73
74    // Pick the buffer format to request from the sink when not rendering to it
75    // with GLES. If the consumer needs CPU access, use the default format
76    // set by the consumer. Otherwise allow gralloc to decide the format based
77    // on usage bits.
78    int sinkUsage;
79    sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
80    if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
81        int sinkFormat;
82        sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
83        mDefaultOutputFormat = sinkFormat;
84    } else {
85        mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
86    }
87    mOutputFormat = mDefaultOutputFormat;
88
89    ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
90    mConsumer->setConsumerName(ConsumerBase::mName);
91    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
92    mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
93    mConsumer->setDefaultMaxBufferCount(2);
94}
95
96VirtualDisplaySurface::~VirtualDisplaySurface() {
97}
98
99status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
100    if (mDisplayId < 0)
101        return NO_ERROR;
102
103    mMustRecompose = mustRecompose;
104
105    VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
106            "Unexpected beginFrame() in %s state", dbgStateStr());
107    mDbgState = DBG_STATE_BEGUN;
108
109    return refreshOutputBuffer();
110}
111
112status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
113    if (mDisplayId < 0)
114        return NO_ERROR;
115
116    VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
117            "Unexpected prepareFrame() in %s state", dbgStateStr());
118    mDbgState = DBG_STATE_PREPARED;
119
120    mCompositionType = compositionType;
121    if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
122        // Some hardware can do RGB->YUV conversion more efficiently in hardware
123        // controlled by HWC than in hardware controlled by the video encoder.
124        // Forcing GLES-composed frames to go through an extra copy by the HWC
125        // allows the format conversion to happen there, rather than passing RGB
126        // directly to the consumer.
127        //
128        // On the other hand, when the consumer prefers RGB or can consume RGB
129        // inexpensively, this forces an unnecessary copy.
130        mCompositionType = COMPOSITION_MIXED;
131    }
132
133    if (mCompositionType != mDbgLastCompositionType) {
134        VDS_LOGV("prepareFrame: composition type changed to %s",
135                dbgCompositionTypeStr(mCompositionType));
136        mDbgLastCompositionType = mCompositionType;
137    }
138
139    if (mCompositionType != COMPOSITION_GLES &&
140            (mOutputFormat != mDefaultOutputFormat ||
141             mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
142        // We must have just switched from GLES-only to MIXED or HWC
143        // composition. Stop using the format and usage requested by the GLES
144        // driver; they may be suboptimal when HWC is writing to the output
145        // buffer. For example, if the output is going to a video encoder, and
146        // HWC can write directly to YUV, some hardware can skip a
147        // memory-to-memory RGB-to-YUV conversion step.
148        //
149        // If we just switched *to* GLES-only mode, we'll change the
150        // format/usage and get a new buffer when the GLES driver calls
151        // dequeueBuffer().
152        mOutputFormat = mDefaultOutputFormat;
153        mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
154        refreshOutputBuffer();
155    }
156
157    return NO_ERROR;
158}
159
160status_t VirtualDisplaySurface::compositionComplete() {
161    return NO_ERROR;
162}
163
164status_t VirtualDisplaySurface::advanceFrame() {
165    if (mDisplayId < 0)
166        return NO_ERROR;
167
168    if (mCompositionType == COMPOSITION_HWC) {
169        VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
170                "Unexpected advanceFrame() in %s state on HWC frame",
171                dbgStateStr());
172    } else {
173        VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
174                "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
175                dbgStateStr());
176    }
177    mDbgState = DBG_STATE_HWC;
178
179    if (mOutputProducerSlot < 0 ||
180            (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
181        // Last chance bailout if something bad happened earlier. For example,
182        // in a GLES configuration, if the sink disappears then dequeueBuffer
183        // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
184        // will soldier on. So we end up here without a buffer. There should
185        // be lots of scary messages in the log just before this.
186        VDS_LOGE("advanceFrame: no buffer, bailing out");
187        return NO_MEMORY;
188    }
189
190    sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
191            mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
192    sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
193    VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
194            mFbProducerSlot, fbBuffer.get(),
195            mOutputProducerSlot, outBuffer.get());
196
197    // At this point we know the output buffer acquire fence,
198    // so update HWC state with it.
199    mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
200
201    status_t result = NO_ERROR;
202    if (fbBuffer != NULL) {
203        result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
204    }
205
206    return result;
207}
208
209void VirtualDisplaySurface::onFrameCommitted() {
210    if (mDisplayId < 0)
211        return;
212
213    VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
214            "Unexpected onFrameCommitted() in %s state", dbgStateStr());
215    mDbgState = DBG_STATE_IDLE;
216
217    sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
218    if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
219        // release the scratch buffer back to the pool
220        Mutex::Autolock lock(mMutex);
221        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
222        VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
223        addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
224        releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
225                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
226    }
227
228    if (mOutputProducerSlot >= 0) {
229        int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
230        QueueBufferOutput qbo;
231        sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
232        VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
233        if (mMustRecompose) {
234            status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
235                    QueueBufferInput(
236                        systemTime(), false /* isAutoTimestamp */,
237                        HAL_DATASPACE_UNKNOWN,
238                        Rect(mSinkBufferWidth, mSinkBufferHeight),
239                        NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
240                        true /* async*/,
241                        outFence),
242                    &qbo);
243            if (result == NO_ERROR) {
244                updateQueueBufferOutput(qbo);
245            }
246        } else {
247            // If the surface hadn't actually been updated, then we only went
248            // through the motions of updating the display to keep our state
249            // machine happy. We cancel the buffer to avoid triggering another
250            // re-composition and causing an infinite loop.
251            mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
252        }
253    }
254
255    resetPerFrameState();
256}
257
258void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
259}
260
261void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
262    uint32_t tmpW, tmpH, transformHint, numPendingBuffers;
263    mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers);
264    mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers);
265
266    mSinkBufferWidth = w;
267    mSinkBufferHeight = h;
268}
269
270status_t VirtualDisplaySurface::requestBuffer(int pslot,
271        sp<GraphicBuffer>* outBuf) {
272    if (mDisplayId < 0)
273        return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
274
275    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
276            "Unexpected requestBuffer pslot=%d in %s state",
277            pslot, dbgStateStr());
278
279    *outBuf = mProducerBuffers[pslot];
280    return NO_ERROR;
281}
282
283status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
284    return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
285}
286
287status_t VirtualDisplaySurface::dequeueBuffer(Source source,
288        PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
289    LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
290    // Don't let a slow consumer block us
291    bool async = (source == SOURCE_SINK);
292
293    status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
294            mSinkBufferWidth, mSinkBufferHeight, format, usage);
295    if (result < 0)
296        return result;
297    int pslot = mapSource2ProducerSlot(source, *sslot);
298    VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
299            dbgSourceStr(source), *sslot, pslot, result);
300    uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
301
302    if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
303        // This slot was previously dequeued from the other source; must
304        // re-request the buffer.
305        result |= BUFFER_NEEDS_REALLOCATION;
306        mProducerSlotSource &= ~(1ULL << pslot);
307        mProducerSlotSource |= sourceBit;
308    }
309
310    if (result & RELEASE_ALL_BUFFERS) {
311        for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
312            if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
313                mProducerBuffers[i].clear();
314        }
315    }
316    if (result & BUFFER_NEEDS_REALLOCATION) {
317        result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
318        if (result < 0) {
319            mProducerBuffers[pslot].clear();
320            mSource[source]->cancelBuffer(*sslot, *fence);
321            return result;
322        }
323        VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
324                dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
325                mProducerBuffers[pslot]->getPixelFormat(),
326                mProducerBuffers[pslot]->getUsage());
327    }
328
329    return result;
330}
331
332status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
333        uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) {
334    if (mDisplayId < 0)
335        return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, async, w, h, format, usage);
336
337    VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
338            "Unexpected dequeueBuffer() in %s state", dbgStateStr());
339    mDbgState = DBG_STATE_GLES;
340
341    VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
342    VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
343
344    status_t result = NO_ERROR;
345    Source source = fbSourceForCompositionType(mCompositionType);
346
347    if (source == SOURCE_SINK) {
348
349        if (mOutputProducerSlot < 0) {
350            // Last chance bailout if something bad happened earlier. For example,
351            // in a GLES configuration, if the sink disappears then dequeueBuffer
352            // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
353            // will soldier on. So we end up here without a buffer. There should
354            // be lots of scary messages in the log just before this.
355            VDS_LOGE("dequeueBuffer: no buffer, bailing out");
356            return NO_MEMORY;
357        }
358
359        // We already dequeued the output buffer. If the GLES driver wants
360        // something incompatible, we have to cancel and get a new one. This
361        // will mean that HWC will see a different output buffer between
362        // prepare and set, but since we're in GLES-only mode already it
363        // shouldn't matter.
364
365        usage |= GRALLOC_USAGE_HW_COMPOSER;
366        const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
367        if ((usage & ~buf->getUsage()) != 0 ||
368                (format != 0 && format != buf->getPixelFormat()) ||
369                (w != 0 && w != mSinkBufferWidth) ||
370                (h != 0 && h != mSinkBufferHeight)) {
371            VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
372                    "want %dx%d fmt=%d use=%#x, "
373                    "have %dx%d fmt=%d use=%#x",
374                    w, h, format, usage,
375                    mSinkBufferWidth, mSinkBufferHeight,
376                    buf->getPixelFormat(), buf->getUsage());
377            mOutputFormat = format;
378            mOutputUsage = usage;
379            result = refreshOutputBuffer();
380            if (result < 0)
381                return result;
382        }
383    }
384
385    if (source == SOURCE_SINK) {
386        *pslot = mOutputProducerSlot;
387        *fence = mOutputFence;
388    } else {
389        int sslot;
390        result = dequeueBuffer(source, format, usage, &sslot, fence);
391        if (result >= 0) {
392            *pslot = mapSource2ProducerSlot(source, sslot);
393        }
394    }
395    return result;
396}
397
398status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
399    VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
400    return INVALID_OPERATION;
401}
402
403status_t VirtualDisplaySurface::detachNextBuffer(
404        sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
405    VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
406    return INVALID_OPERATION;
407}
408
409status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
410        const sp<GraphicBuffer>& /* buffer */) {
411    VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
412    return INVALID_OPERATION;
413}
414
415status_t VirtualDisplaySurface::queueBuffer(int pslot,
416        const QueueBufferInput& input, QueueBufferOutput* output) {
417    if (mDisplayId < 0)
418        return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
419
420    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
421            "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
422            dbgStateStr());
423    mDbgState = DBG_STATE_GLES_DONE;
424
425    VDS_LOGV("queueBuffer pslot=%d", pslot);
426
427    status_t result;
428    if (mCompositionType == COMPOSITION_MIXED) {
429        // Queue the buffer back into the scratch pool
430        QueueBufferOutput scratchQBO;
431        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
432        result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
433        if (result != NO_ERROR)
434            return result;
435
436        // Now acquire the buffer from the scratch pool -- should be the same
437        // slot and fence as we just queued.
438        Mutex::Autolock lock(mMutex);
439        BufferQueue::BufferItem item;
440        result = acquireBufferLocked(&item, 0);
441        if (result != NO_ERROR)
442            return result;
443        VDS_LOGW_IF(item.mBuf != sslot,
444                "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
445                item.mBuf, sslot);
446        mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
447        mFbFence = mSlots[item.mBuf].mFence;
448
449    } else {
450        LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
451                "Unexpected queueBuffer in state %s for compositionType %s",
452                dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
453
454        // Extract the GLES release fence for HWC to acquire
455        int64_t timestamp;
456        bool isAutoTimestamp;
457        android_dataspace dataSpace;
458        Rect crop;
459        int scalingMode;
460        uint32_t transform;
461        bool async;
462        input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
463                &scalingMode, &transform, &async, &mFbFence);
464
465        mFbProducerSlot = pslot;
466        mOutputFence = mFbFence;
467    }
468
469    *output = mQueueBufferOutput;
470    return NO_ERROR;
471}
472
473void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
474    if (mDisplayId < 0)
475        return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
476
477    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
478            "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
479            dbgStateStr());
480    VDS_LOGV("cancelBuffer pslot=%d", pslot);
481    Source source = fbSourceForCompositionType(mCompositionType);
482    return mSource[source]->cancelBuffer(
483            mapProducer2SourceSlot(source, pslot), fence);
484}
485
486int VirtualDisplaySurface::query(int what, int* value) {
487    switch (what) {
488        case NATIVE_WINDOW_WIDTH:
489            *value = mSinkBufferWidth;
490            break;
491        case NATIVE_WINDOW_HEIGHT:
492            *value = mSinkBufferHeight;
493            break;
494        default:
495            return mSource[SOURCE_SINK]->query(what, value);
496    }
497    return NO_ERROR;
498}
499
500status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
501        int api, bool producerControlledByApp,
502        QueueBufferOutput* output) {
503    QueueBufferOutput qbo;
504    status_t result = mSource[SOURCE_SINK]->connect(listener, api,
505            producerControlledByApp, &qbo);
506    if (result == NO_ERROR) {
507        updateQueueBufferOutput(qbo);
508        *output = mQueueBufferOutput;
509    }
510    return result;
511}
512
513status_t VirtualDisplaySurface::disconnect(int api) {
514    return mSource[SOURCE_SINK]->disconnect(api);
515}
516
517status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
518    return INVALID_OPERATION;
519}
520
521void VirtualDisplaySurface::allocateBuffers(bool /* async */,
522        uint32_t /* width */, uint32_t /* height */, PixelFormat /* format */,
523        uint32_t /* usage */) {
524    // TODO: Should we actually allocate buffers for a virtual display?
525}
526
527void VirtualDisplaySurface::updateQueueBufferOutput(
528        const QueueBufferOutput& qbo) {
529    uint32_t w, h, transformHint, numPendingBuffers;
530    qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
531    mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
532}
533
534void VirtualDisplaySurface::resetPerFrameState() {
535    mCompositionType = COMPOSITION_UNKNOWN;
536    mFbFence = Fence::NO_FENCE;
537    mOutputFence = Fence::NO_FENCE;
538    mOutputProducerSlot = -1;
539    mFbProducerSlot = -1;
540}
541
542status_t VirtualDisplaySurface::refreshOutputBuffer() {
543    if (mOutputProducerSlot >= 0) {
544        mSource[SOURCE_SINK]->cancelBuffer(
545                mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
546                mOutputFence);
547    }
548
549    int sslot;
550    status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
551            &sslot, &mOutputFence);
552    if (result < 0)
553        return result;
554    mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
555
556    // On GLES-only frames, we don't have the right output buffer acquire fence
557    // until after GLES calls queueBuffer(). So here we just set the buffer
558    // (for use in HWC prepare) but not the fence; we'll call this again with
559    // the proper fence once we have it.
560    result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
561            mProducerBuffers[mOutputProducerSlot]);
562
563    return result;
564}
565
566// This slot mapping function is its own inverse, so two copies are unnecessary.
567// Both are kept to make the intent clear where the function is called, and for
568// the (unlikely) chance that we switch to a different mapping function.
569int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
570    if (source == SOURCE_SCRATCH) {
571        return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
572    } else {
573        return sslot;
574    }
575}
576int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
577    return mapSource2ProducerSlot(source, pslot);
578}
579
580VirtualDisplaySurface::Source
581VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
582    return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
583}
584
585const char* VirtualDisplaySurface::dbgStateStr() const {
586    switch (mDbgState) {
587        case DBG_STATE_IDLE:      return "IDLE";
588        case DBG_STATE_PREPARED:  return "PREPARED";
589        case DBG_STATE_GLES:      return "GLES";
590        case DBG_STATE_GLES_DONE: return "GLES_DONE";
591        case DBG_STATE_HWC:       return "HWC";
592        default:                  return "INVALID";
593    }
594}
595
596const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
597    switch (s) {
598        case SOURCE_SINK:    return "SINK";
599        case SOURCE_SCRATCH: return "SCRATCH";
600        default:             return "INVALID";
601    }
602}
603
604// ---------------------------------------------------------------------------
605} // namespace android
606// ---------------------------------------------------------------------------
607