VirtualDisplaySurface.cpp revision 7143316af216fa92c31a60d4407b707637382da1
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<BufferQueue>& bq,
51        const String8& name)
52:   ConsumerBase(bq),
53    mHwc(hwc),
54    mDisplayId(dispId),
55    mDisplayName(name),
56    mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
57    mProducerSlotSource(0),
58    mDbgState(DBG_STATE_IDLE),
59    mDbgLastCompositionType(COMPOSITION_UNKNOWN),
60    mMustRecompose(false)
61{
62    mSource[SOURCE_SINK] = sink;
63    mSource[SOURCE_SCRATCH] = bq;
64
65    resetPerFrameState();
66
67    int sinkWidth, sinkHeight;
68    sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
69    sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
70
71    // Pick the buffer format to request from the sink when not rendering to it
72    // with GLES. If the consumer needs CPU access, use the default format
73    // set by the consumer. Otherwise allow gralloc to decide the format based
74    // on usage bits.
75    int sinkUsage;
76    sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
77    if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
78        int sinkFormat;
79        sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
80        mDefaultOutputFormat = sinkFormat;
81    } else {
82        mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
83    }
84    mOutputFormat = mDefaultOutputFormat;
85
86    ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
87    mConsumer->setConsumerName(ConsumerBase::mName);
88    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
89    mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
90    mConsumer->setDefaultMaxBufferCount(2);
91}
92
93VirtualDisplaySurface::~VirtualDisplaySurface() {
94}
95
96status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
97    if (mDisplayId < 0)
98        return NO_ERROR;
99
100    mMustRecompose = mustRecompose;
101
102    VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
103            "Unexpected beginFrame() in %s state", dbgStateStr());
104    mDbgState = DBG_STATE_BEGUN;
105
106    uint32_t transformHint, numPendingBuffers;
107    mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
108            &transformHint, &numPendingBuffers);
109
110    return refreshOutputBuffer();
111}
112
113status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
114    if (mDisplayId < 0)
115        return NO_ERROR;
116
117    VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
118            "Unexpected prepareFrame() in %s state", dbgStateStr());
119    mDbgState = DBG_STATE_PREPARED;
120
121    mCompositionType = compositionType;
122    if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
123        // Some hardware can do RGB->YUV conversion more efficiently in hardware
124        // controlled by HWC than in hardware controlled by the video encoder.
125        // Forcing GLES-composed frames to go through an extra copy by the HWC
126        // allows the format conversion to happen there, rather than passing RGB
127        // directly to the consumer.
128        //
129        // On the other hand, when the consumer prefers RGB or can consume RGB
130        // inexpensively, this forces an unnecessary copy.
131        mCompositionType = COMPOSITION_MIXED;
132    }
133
134    if (mCompositionType != mDbgLastCompositionType) {
135        VDS_LOGV("prepareFrame: composition type changed to %s",
136                dbgCompositionTypeStr(mCompositionType));
137        mDbgLastCompositionType = mCompositionType;
138    }
139
140    if (mCompositionType != COMPOSITION_GLES &&
141            (mOutputFormat != mDefaultOutputFormat ||
142             mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
143        // We must have just switched from GLES-only to MIXED or HWC
144        // composition. Stop using the format and usage requested by the GLES
145        // driver; they may be suboptimal when HWC is writing to the output
146        // buffer. For example, if the output is going to a video encoder, and
147        // HWC can write directly to YUV, some hardware can skip a
148        // memory-to-memory RGB-to-YUV conversion step.
149        //
150        // If we just switched *to* GLES-only mode, we'll change the
151        // format/usage and get a new buffer when the GLES driver calls
152        // dequeueBuffer().
153        mOutputFormat = mDefaultOutputFormat;
154        mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
155        refreshOutputBuffer();
156    }
157
158    return NO_ERROR;
159}
160
161status_t VirtualDisplaySurface::compositionComplete() {
162    return NO_ERROR;
163}
164
165status_t VirtualDisplaySurface::advanceFrame() {
166    if (mDisplayId < 0)
167        return NO_ERROR;
168
169    if (mCompositionType == COMPOSITION_HWC) {
170        VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
171                "Unexpected advanceFrame() in %s state on HWC frame",
172                dbgStateStr());
173    } else {
174        VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
175                "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
176                dbgStateStr());
177    }
178    mDbgState = DBG_STATE_HWC;
179
180    if (mOutputProducerSlot < 0 ||
181            (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
182        // Last chance bailout if something bad happened earlier. For example,
183        // in a GLES configuration, if the sink disappears then dequeueBuffer
184        // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
185        // will soldier on. So we end up here without a buffer. There should
186        // be lots of scary messages in the log just before this.
187        VDS_LOGE("advanceFrame: no buffer, bailing out");
188        return NO_MEMORY;
189    }
190
191    sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
192            mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
193    sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
194    VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
195            mFbProducerSlot, fbBuffer.get(),
196            mOutputProducerSlot, outBuffer.get());
197
198    // At this point we know the output buffer acquire fence,
199    // so update HWC state with it.
200    mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
201
202    status_t result = NO_ERROR;
203    if (fbBuffer != NULL) {
204        result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
205    }
206
207    return result;
208}
209
210void VirtualDisplaySurface::onFrameCommitted() {
211    if (mDisplayId < 0)
212        return;
213
214    VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
215            "Unexpected onFrameCommitted() in %s state", dbgStateStr());
216    mDbgState = DBG_STATE_IDLE;
217
218    sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
219    if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
220        // release the scratch buffer back to the pool
221        Mutex::Autolock lock(mMutex);
222        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
223        VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
224        addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
225        releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
226                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
227    }
228
229    if (mOutputProducerSlot >= 0) {
230        int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
231        QueueBufferOutput qbo;
232        sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
233        VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
234        if (mMustRecompose) {
235            status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
236                    QueueBufferInput(
237                        systemTime(), false /* isAutoTimestamp */,
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::dump(String8& result) const {
259}
260
261status_t VirtualDisplaySurface::requestBuffer(int pslot,
262        sp<GraphicBuffer>* outBuf) {
263    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
264            "Unexpected requestBuffer pslot=%d in %s state",
265            pslot, dbgStateStr());
266
267    *outBuf = mProducerBuffers[pslot];
268    return NO_ERROR;
269}
270
271status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
272    return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
273}
274
275status_t VirtualDisplaySurface::dequeueBuffer(Source source,
276        uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
277    // Don't let a slow consumer block us
278    bool async = (source == SOURCE_SINK);
279
280    status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
281            mSinkBufferWidth, mSinkBufferHeight, format, usage);
282    if (result < 0)
283        return result;
284    int pslot = mapSource2ProducerSlot(source, *sslot);
285    VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
286            dbgSourceStr(source), *sslot, pslot, result);
287    uint32_t sourceBit = static_cast<uint32_t>(source) << pslot;
288
289    if ((mProducerSlotSource & (1u << pslot)) != sourceBit) {
290        // This slot was previously dequeued from the other source; must
291        // re-request the buffer.
292        result |= BUFFER_NEEDS_REALLOCATION;
293        mProducerSlotSource &= ~(1u << pslot);
294        mProducerSlotSource |= sourceBit;
295    }
296
297    if (result & RELEASE_ALL_BUFFERS) {
298        for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
299            if ((mProducerSlotSource & (1u << i)) == sourceBit)
300                mProducerBuffers[i].clear();
301        }
302    }
303    if (result & BUFFER_NEEDS_REALLOCATION) {
304        mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
305        VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
306                dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
307                mProducerBuffers[pslot]->getPixelFormat(),
308                mProducerBuffers[pslot]->getUsage());
309    }
310
311    return result;
312}
313
314status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
315        uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
316    VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
317            "Unexpected dequeueBuffer() in %s state", dbgStateStr());
318    mDbgState = DBG_STATE_GLES;
319
320    VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
321    VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
322
323    status_t result = NO_ERROR;
324    Source source = fbSourceForCompositionType(mCompositionType);
325
326    if (source == SOURCE_SINK) {
327
328        if (mOutputProducerSlot < 0) {
329            // Last chance bailout if something bad happened earlier. For example,
330            // in a GLES configuration, if the sink disappears then dequeueBuffer
331            // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
332            // will soldier on. So we end up here without a buffer. There should
333            // be lots of scary messages in the log just before this.
334            VDS_LOGE("dequeueBuffer: no buffer, bailing out");
335            return NO_MEMORY;
336        }
337
338        // We already dequeued the output buffer. If the GLES driver wants
339        // something incompatible, we have to cancel and get a new one. This
340        // will mean that HWC will see a different output buffer between
341        // prepare and set, but since we're in GLES-only mode already it
342        // shouldn't matter.
343
344        usage |= GRALLOC_USAGE_HW_COMPOSER;
345        const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
346        if ((usage & ~buf->getUsage()) != 0 ||
347                (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
348                (w != 0 && w != mSinkBufferWidth) ||
349                (h != 0 && h != mSinkBufferHeight)) {
350            VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
351                    "want %dx%d fmt=%d use=%#x, "
352                    "have %dx%d fmt=%d use=%#x",
353                    w, h, format, usage,
354                    mSinkBufferWidth, mSinkBufferHeight,
355                    buf->getPixelFormat(), buf->getUsage());
356            mOutputFormat = format;
357            mOutputUsage = usage;
358            result = refreshOutputBuffer();
359            if (result < 0)
360                return result;
361        }
362    }
363
364    if (source == SOURCE_SINK) {
365        *pslot = mOutputProducerSlot;
366        *fence = mOutputFence;
367    } else {
368        int sslot;
369        result = dequeueBuffer(source, format, usage, &sslot, fence);
370        if (result >= 0) {
371            *pslot = mapSource2ProducerSlot(source, sslot);
372        }
373    }
374    return result;
375}
376
377status_t VirtualDisplaySurface::queueBuffer(int pslot,
378        const QueueBufferInput& input, QueueBufferOutput* output) {
379    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
380            "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
381            dbgStateStr());
382    mDbgState = DBG_STATE_GLES_DONE;
383
384    VDS_LOGV("queueBuffer pslot=%d", pslot);
385
386    status_t result;
387    if (mCompositionType == COMPOSITION_MIXED) {
388        // Queue the buffer back into the scratch pool
389        QueueBufferOutput scratchQBO;
390        int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
391        result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
392        if (result != NO_ERROR)
393            return result;
394
395        // Now acquire the buffer from the scratch pool -- should be the same
396        // slot and fence as we just queued.
397        Mutex::Autolock lock(mMutex);
398        BufferQueue::BufferItem item;
399        result = acquireBufferLocked(&item, 0);
400        if (result != NO_ERROR)
401            return result;
402        VDS_LOGW_IF(item.mBuf != sslot,
403                "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
404                item.mBuf, sslot);
405        mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
406        mFbFence = mSlots[item.mBuf].mFence;
407
408    } else {
409        LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
410                "Unexpected queueBuffer in state %s for compositionType %s",
411                dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
412
413        // Extract the GLES release fence for HWC to acquire
414        int64_t timestamp;
415        bool isAutoTimestamp;
416        Rect crop;
417        int scalingMode;
418        uint32_t transform;
419        bool async;
420        input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
421                &transform, &async, &mFbFence);
422
423        mFbProducerSlot = pslot;
424        mOutputFence = mFbFence;
425    }
426
427    *output = mQueueBufferOutput;
428    return NO_ERROR;
429}
430
431void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
432    VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
433            "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
434            dbgStateStr());
435    VDS_LOGV("cancelBuffer pslot=%d", pslot);
436    Source source = fbSourceForCompositionType(mCompositionType);
437    return mSource[source]->cancelBuffer(
438            mapProducer2SourceSlot(source, pslot), fence);
439}
440
441int VirtualDisplaySurface::query(int what, int* value) {
442    return mSource[SOURCE_SINK]->query(what, value);
443}
444
445status_t VirtualDisplaySurface::connect(const sp<IBinder>& token,
446        int api, bool producerControlledByApp,
447        QueueBufferOutput* output) {
448    QueueBufferOutput qbo;
449    status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo);
450    if (result == NO_ERROR) {
451        updateQueueBufferOutput(qbo);
452        *output = mQueueBufferOutput;
453    }
454    return result;
455}
456
457status_t VirtualDisplaySurface::disconnect(int api) {
458    return mSource[SOURCE_SINK]->disconnect(api);
459}
460
461void VirtualDisplaySurface::updateQueueBufferOutput(
462        const QueueBufferOutput& qbo) {
463    uint32_t w, h, transformHint, numPendingBuffers;
464    qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
465    mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
466}
467
468void VirtualDisplaySurface::resetPerFrameState() {
469    mCompositionType = COMPOSITION_UNKNOWN;
470    mSinkBufferWidth = 0;
471    mSinkBufferHeight = 0;
472    mFbFence = Fence::NO_FENCE;
473    mOutputFence = Fence::NO_FENCE;
474    mOutputProducerSlot = -1;
475    mFbProducerSlot = -1;
476}
477
478status_t VirtualDisplaySurface::refreshOutputBuffer() {
479    if (mOutputProducerSlot >= 0) {
480        mSource[SOURCE_SINK]->cancelBuffer(
481                mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
482                mOutputFence);
483    }
484
485    int sslot;
486    status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
487            &sslot, &mOutputFence);
488    if (result < 0)
489        return result;
490    mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
491
492    // On GLES-only frames, we don't have the right output buffer acquire fence
493    // until after GLES calls queueBuffer(). So here we just set the buffer
494    // (for use in HWC prepare) but not the fence; we'll call this again with
495    // the proper fence once we have it.
496    result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
497            mProducerBuffers[mOutputProducerSlot]);
498
499    return result;
500}
501
502// This slot mapping function is its own inverse, so two copies are unnecessary.
503// Both are kept to make the intent clear where the function is called, and for
504// the (unlikely) chance that we switch to a different mapping function.
505int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
506    if (source == SOURCE_SCRATCH) {
507        return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
508    } else {
509        return sslot;
510    }
511}
512int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
513    return mapSource2ProducerSlot(source, pslot);
514}
515
516VirtualDisplaySurface::Source
517VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
518    return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
519}
520
521const char* VirtualDisplaySurface::dbgStateStr() const {
522    switch (mDbgState) {
523        case DBG_STATE_IDLE:      return "IDLE";
524        case DBG_STATE_PREPARED:  return "PREPARED";
525        case DBG_STATE_GLES:      return "GLES";
526        case DBG_STATE_GLES_DONE: return "GLES_DONE";
527        case DBG_STATE_HWC:       return "HWC";
528        default:                  return "INVALID";
529    }
530}
531
532const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
533    switch (s) {
534        case SOURCE_SINK:    return "SINK";
535        case SOURCE_SCRATCH: return "SCRATCH";
536        default:             return "INVALID";
537    }
538}
539
540// ---------------------------------------------------------------------------
541} // namespace android
542// ---------------------------------------------------------------------------
543