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