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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 // #define LOG_NDEBUG 0
22 #include "VirtualDisplaySurface.h"
23 
24 #include <inttypes.h>
25 
26 #include "HWComposer.h"
27 #include "SurfaceFlinger.h"
28 
29 #include <gui/BufferItem.h>
30 #include <gui/BufferQueue.h>
31 #include <gui/IProducerListener.h>
32 #include <system/window.h>
33 
34 // ---------------------------------------------------------------------------
35 namespace android {
36 // ---------------------------------------------------------------------------
37 
38 #define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
39         mDisplayName.c_str(), ##__VA_ARGS__)
40 #define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
41         mDisplayName.c_str(), ##__VA_ARGS__)
42 #define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
43         mDisplayName.c_str(), ##__VA_ARGS__)
44 
dbgCompositionTypeStr(compositionengine::DisplaySurface::CompositionType type)45 static const char* dbgCompositionTypeStr(compositionengine::DisplaySurface::CompositionType type) {
46     switch (type) {
47         case compositionengine::DisplaySurface::COMPOSITION_UNKNOWN:
48             return "UNKNOWN";
49         case compositionengine::DisplaySurface::COMPOSITION_GPU:
50             return "GPU";
51         case compositionengine::DisplaySurface::COMPOSITION_HWC:
52             return "HWC";
53         case compositionengine::DisplaySurface::COMPOSITION_MIXED:
54             return "MIXED";
55         default:
56             return "<INVALID>";
57     }
58 }
59 
VirtualDisplaySurface(HWComposer & hwc,VirtualDisplayId displayId,const sp<IGraphicBufferProducer> & sink,const sp<IGraphicBufferProducer> & bqProducer,const sp<IGraphicBufferConsumer> & bqConsumer,const std::string & name)60 VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, VirtualDisplayId displayId,
61                                              const sp<IGraphicBufferProducer>& sink,
62                                              const sp<IGraphicBufferProducer>& bqProducer,
63                                              const sp<IGraphicBufferConsumer>& bqConsumer,
64                                              const std::string& name)
65       : ConsumerBase(bqConsumer),
66         mHwc(hwc),
67         mDisplayId(displayId),
68         mDisplayName(name),
69         mSource{},
70         mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
71         mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
72         mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
73         mProducerSlotSource(0),
74         mProducerBuffers(),
75         mProducerSlotNeedReallocation(0),
76         mQueueBufferOutput(),
77         mSinkBufferWidth(0),
78         mSinkBufferHeight(0),
79         mCompositionType(COMPOSITION_UNKNOWN),
80         mFbFence(Fence::NO_FENCE),
81         mOutputFence(Fence::NO_FENCE),
82         mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
83         mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
84         mDbgState(DBG_STATE_IDLE),
85         mDbgLastCompositionType(COMPOSITION_UNKNOWN),
86         mMustRecompose(false),
87         mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
88     mSource[SOURCE_SINK] = sink;
89     mSource[SOURCE_SCRATCH] = bqProducer;
90 
91     resetPerFrameState();
92 
93     int sinkWidth, sinkHeight;
94     sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
95     sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
96     mSinkBufferWidth = sinkWidth;
97     mSinkBufferHeight = sinkHeight;
98 
99     // Pick the buffer format to request from the sink when not rendering to it
100     // with GPU. If the consumer needs CPU access, use the default format
101     // set by the consumer. Otherwise allow gralloc to decide the format based
102     // on usage bits.
103     int sinkUsage;
104     sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
105     if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
106         int sinkFormat;
107         sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
108         mDefaultOutputFormat = sinkFormat;
109     } else {
110         mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
111     }
112     mOutputFormat = mDefaultOutputFormat;
113 
114     ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
115     mConsumer->setConsumerName(ConsumerBase::mName);
116     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
117     mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
118     sink->setAsyncMode(true);
119     IGraphicBufferProducer::QueueBufferOutput output;
120     mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
121 }
122 
~VirtualDisplaySurface()123 VirtualDisplaySurface::~VirtualDisplaySurface() {
124     mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
125 }
126 
beginFrame(bool mustRecompose)127 status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
128     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
129         return NO_ERROR;
130     }
131 
132     mMustRecompose = mustRecompose;
133 
134     VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
135             "Unexpected beginFrame() in %s state", dbgStateStr());
136     mDbgState = DBG_STATE_BEGUN;
137 
138     return refreshOutputBuffer();
139 }
140 
prepareFrame(CompositionType compositionType)141 status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
142     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
143         return NO_ERROR;
144     }
145 
146     VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
147             "Unexpected prepareFrame() in %s state", dbgStateStr());
148     mDbgState = DBG_STATE_PREPARED;
149 
150     mCompositionType = compositionType;
151     if (mForceHwcCopy && mCompositionType == COMPOSITION_GPU) {
152         // Some hardware can do RGB->YUV conversion more efficiently in hardware
153         // controlled by HWC than in hardware controlled by the video encoder.
154         // Forcing GPU-composed frames to go through an extra copy by the HWC
155         // allows the format conversion to happen there, rather than passing RGB
156         // directly to the consumer.
157         //
158         // On the other hand, when the consumer prefers RGB or can consume RGB
159         // inexpensively, this forces an unnecessary copy.
160         mCompositionType = COMPOSITION_MIXED;
161     }
162 
163     if (mCompositionType != mDbgLastCompositionType) {
164         VDS_LOGV("prepareFrame: composition type changed to %s",
165                 dbgCompositionTypeStr(mCompositionType));
166         mDbgLastCompositionType = mCompositionType;
167     }
168 
169     if (mCompositionType != COMPOSITION_GPU &&
170         (mOutputFormat != mDefaultOutputFormat || mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
171         // We must have just switched from GPU-only to MIXED or HWC
172         // composition. Stop using the format and usage requested by the GPU
173         // driver; they may be suboptimal when HWC is writing to the output
174         // buffer. For example, if the output is going to a video encoder, and
175         // HWC can write directly to YUV, some hardware can skip a
176         // memory-to-memory RGB-to-YUV conversion step.
177         //
178         // If we just switched *to* GPU-only mode, we'll change the
179         // format/usage and get a new buffer when the GPU driver calls
180         // dequeueBuffer().
181         mOutputFormat = mDefaultOutputFormat;
182         mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
183         refreshOutputBuffer();
184     }
185 
186     return NO_ERROR;
187 }
188 
advanceFrame()189 status_t VirtualDisplaySurface::advanceFrame() {
190     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
191         return NO_ERROR;
192     }
193 
194     if (mCompositionType == COMPOSITION_HWC) {
195         VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
196                 "Unexpected advanceFrame() in %s state on HWC frame",
197                 dbgStateStr());
198     } else {
199         VDS_LOGW_IF(mDbgState != DBG_STATE_GPU_DONE,
200                     "Unexpected advanceFrame() in %s state on GPU/MIXED frame", dbgStateStr());
201     }
202     mDbgState = DBG_STATE_HWC;
203 
204     if (mOutputProducerSlot < 0 ||
205             (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
206         // Last chance bailout if something bad happened earlier. For example,
207         // in a graphics API configuration, if the sink disappears then dequeueBuffer
208         // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
209         // will soldier on. So we end up here without a buffer. There should
210         // be lots of scary messages in the log just before this.
211         VDS_LOGE("advanceFrame: no buffer, bailing out");
212         return NO_MEMORY;
213     }
214 
215     sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
216             mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
217     sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
218     VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
219             mFbProducerSlot, fbBuffer.get(),
220             mOutputProducerSlot, outBuffer.get());
221 
222     const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
223     LOG_FATAL_IF(!halDisplayId);
224     // At this point we know the output buffer acquire fence,
225     // so update HWC state with it.
226     mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
227 
228     status_t result = NO_ERROR;
229     if (fbBuffer != nullptr) {
230         uint32_t hwcSlot = 0;
231         sp<GraphicBuffer> hwcBuffer;
232         mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer, &hwcSlot, &hwcBuffer);
233 
234         // TODO: Correctly propagate the dataspace from GL composition
235         result = mHwc.setClientTarget(*halDisplayId, hwcSlot, mFbFence, hwcBuffer,
236                                       ui::Dataspace::UNKNOWN);
237     }
238 
239     return result;
240 }
241 
onFrameCommitted()242 void VirtualDisplaySurface::onFrameCommitted() {
243     const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
244     if (!halDisplayId) {
245         return;
246     }
247 
248     VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
249             "Unexpected onFrameCommitted() in %s state", dbgStateStr());
250     mDbgState = DBG_STATE_IDLE;
251 
252     sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
253     if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
254         // release the scratch buffer back to the pool
255         Mutex::Autolock lock(mMutex);
256         int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
257         VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
258         addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
259                 retireFence);
260         releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
261     }
262 
263     if (mOutputProducerSlot >= 0) {
264         int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
265         QueueBufferOutput qbo;
266         VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
267         if (mMustRecompose) {
268             status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
269                     QueueBufferInput(
270                         systemTime(), false /* isAutoTimestamp */,
271                         HAL_DATASPACE_UNKNOWN,
272                         Rect(mSinkBufferWidth, mSinkBufferHeight),
273                         NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
274                         retireFence),
275                     &qbo);
276             if (result == NO_ERROR) {
277                 updateQueueBufferOutput(std::move(qbo));
278             }
279         } else {
280             // If the surface hadn't actually been updated, then we only went
281             // through the motions of updating the display to keep our state
282             // machine happy. We cancel the buffer to avoid triggering another
283             // re-composition and causing an infinite loop.
284             mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
285         }
286     }
287 
288     resetPerFrameState();
289 }
290 
dumpAsString(String8 &) const291 void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
292 }
293 
resizeBuffers(const ui::Size & newSize)294 void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
295     mQueueBufferOutput.width = newSize.width;
296     mQueueBufferOutput.height = newSize.height;
297     mSinkBufferWidth = newSize.width;
298     mSinkBufferHeight = newSize.height;
299 }
300 
getClientTargetAcquireFence() const301 const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
302     return mFbFence;
303 }
304 
requestBuffer(int pslot,sp<GraphicBuffer> * outBuf)305 status_t VirtualDisplaySurface::requestBuffer(int pslot,
306         sp<GraphicBuffer>* outBuf) {
307     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
308         return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
309     }
310 
311     VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected requestBuffer pslot=%d in %s state", pslot,
312                 dbgStateStr());
313 
314     *outBuf = mProducerBuffers[pslot];
315     return NO_ERROR;
316 }
317 
setMaxDequeuedBufferCount(int maxDequeuedBuffers)318 status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
319         int maxDequeuedBuffers) {
320     return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
321 }
322 
setAsyncMode(bool async)323 status_t VirtualDisplaySurface::setAsyncMode(bool async) {
324     return mSource[SOURCE_SINK]->setAsyncMode(async);
325 }
326 
dequeueBuffer(Source source,PixelFormat format,uint64_t usage,int * sslot,sp<Fence> * fence)327 status_t VirtualDisplaySurface::dequeueBuffer(Source source,
328         PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
329     LOG_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId));
330 
331     status_t result =
332             mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
333                                            format, usage, nullptr, nullptr);
334     if (result < 0)
335         return result;
336     int pslot = mapSource2ProducerSlot(source, *sslot);
337     VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
338             dbgSourceStr(source), *sslot, pslot, result);
339     uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
340 
341     // reset producer slot reallocation flag
342     mProducerSlotNeedReallocation &= ~(1ULL << pslot);
343 
344     if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
345         // This slot was previously dequeued from the other source; must
346         // re-request the buffer.
347         mProducerSlotNeedReallocation |= 1ULL << pslot;
348 
349         mProducerSlotSource &= ~(1ULL << pslot);
350         mProducerSlotSource |= sourceBit;
351     }
352 
353     if (result & RELEASE_ALL_BUFFERS) {
354         for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
355             if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
356                 mProducerBuffers[i].clear();
357         }
358     }
359     if (result & BUFFER_NEEDS_REALLOCATION) {
360         result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
361         if (result < 0) {
362             mProducerBuffers[pslot].clear();
363             mSource[source]->cancelBuffer(*sslot, *fence);
364             return result;
365         }
366         VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
367                 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
368                 mProducerBuffers[pslot]->getPixelFormat(),
369                 mProducerBuffers[pslot]->getUsage());
370 
371         // propagate reallocation to VDS consumer
372         mProducerSlotNeedReallocation |= 1ULL << pslot;
373     }
374 
375     return result;
376 }
377 
dequeueBuffer(int * pslot,sp<Fence> * fence,uint32_t w,uint32_t h,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)378 status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
379                                               PixelFormat format, uint64_t usage,
380                                               uint64_t* outBufferAge,
381                                               FrameEventHistoryDelta* outTimestamps) {
382     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
383         return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
384                                                    outTimestamps);
385     }
386 
387     VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
388             "Unexpected dequeueBuffer() in %s state", dbgStateStr());
389     mDbgState = DBG_STATE_GPU;
390 
391     VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
392 
393     status_t result = NO_ERROR;
394     Source source = fbSourceForCompositionType(mCompositionType);
395 
396     if (source == SOURCE_SINK) {
397 
398         if (mOutputProducerSlot < 0) {
399             // Last chance bailout if something bad happened earlier. For example,
400             // in a graphics API configuration, if the sink disappears then dequeueBuffer
401             // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
402             // will soldier on. So we end up here without a buffer. There should
403             // be lots of scary messages in the log just before this.
404             VDS_LOGE("dequeueBuffer: no buffer, bailing out");
405             return NO_MEMORY;
406         }
407 
408         // We already dequeued the output buffer. If the GPU driver wants
409         // something incompatible, we have to cancel and get a new one. This
410         // will mean that HWC will see a different output buffer between
411         // prepare and set, but since we're in GPU-only mode already it
412         // shouldn't matter.
413 
414         usage |= GRALLOC_USAGE_HW_COMPOSER;
415         const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
416         if ((usage & ~buf->getUsage()) != 0 ||
417                 (format != 0 && format != buf->getPixelFormat()) ||
418                 (w != 0 && w != mSinkBufferWidth) ||
419                 (h != 0 && h != mSinkBufferHeight)) {
420             VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
421                     "want %dx%d fmt=%d use=%#" PRIx64 ", "
422                     "have %dx%d fmt=%d use=%#" PRIx64,
423                     w, h, format, usage,
424                     mSinkBufferWidth, mSinkBufferHeight,
425                     buf->getPixelFormat(), buf->getUsage());
426             mOutputFormat = format;
427             mOutputUsage = usage;
428             result = refreshOutputBuffer();
429             if (result < 0)
430                 return result;
431         }
432     }
433 
434     if (source == SOURCE_SINK) {
435         *pslot = mOutputProducerSlot;
436         *fence = mOutputFence;
437     } else {
438         int sslot;
439         result = dequeueBuffer(source, format, usage, &sslot, fence);
440         if (result >= 0) {
441             *pslot = mapSource2ProducerSlot(source, sslot);
442         }
443     }
444     if (outBufferAge) {
445         *outBufferAge = 0;
446     }
447 
448     if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
449         result |= BUFFER_NEEDS_REALLOCATION;
450     }
451 
452     return result;
453 }
454 
detachBuffer(int)455 status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
456     VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
457     return INVALID_OPERATION;
458 }
459 
detachNextBuffer(sp<GraphicBuffer> *,sp<Fence> *)460 status_t VirtualDisplaySurface::detachNextBuffer(
461         sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
462     VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
463     return INVALID_OPERATION;
464 }
465 
attachBuffer(int *,const sp<GraphicBuffer> &)466 status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
467         const sp<GraphicBuffer>& /* buffer */) {
468     VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
469     return INVALID_OPERATION;
470 }
471 
queueBuffer(int pslot,const QueueBufferInput & input,QueueBufferOutput * output)472 status_t VirtualDisplaySurface::queueBuffer(int pslot,
473         const QueueBufferInput& input, QueueBufferOutput* output) {
474     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
475         return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
476     }
477 
478     VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
479                 dbgStateStr());
480     mDbgState = DBG_STATE_GPU_DONE;
481 
482     VDS_LOGV("queueBuffer pslot=%d", pslot);
483 
484     status_t result;
485     if (mCompositionType == COMPOSITION_MIXED) {
486         // Queue the buffer back into the scratch pool
487         QueueBufferOutput scratchQBO;
488         int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
489         result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
490         if (result != NO_ERROR)
491             return result;
492 
493         // Now acquire the buffer from the scratch pool -- should be the same
494         // slot and fence as we just queued.
495         Mutex::Autolock lock(mMutex);
496         BufferItem item;
497         result = acquireBufferLocked(&item, 0);
498         if (result != NO_ERROR)
499             return result;
500         VDS_LOGW_IF(item.mSlot != sslot,
501                 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
502                 item.mSlot, sslot);
503         mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
504         mFbFence = mSlots[item.mSlot].mFence;
505 
506     } else {
507         LOG_FATAL_IF(mCompositionType != COMPOSITION_GPU,
508                      "Unexpected queueBuffer in state %s for compositionType %s", dbgStateStr(),
509                      dbgCompositionTypeStr(mCompositionType));
510 
511         // Extract the GPU release fence for HWC to acquire
512         int64_t timestamp;
513         bool isAutoTimestamp;
514         android_dataspace dataSpace;
515         Rect crop;
516         int scalingMode;
517         uint32_t transform;
518         input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
519                 &scalingMode, &transform, &mFbFence);
520 
521         mFbProducerSlot = pslot;
522         mOutputFence = mFbFence;
523     }
524 
525     // This moves the frame timestamps and keeps a copy of all other fields.
526     *output = std::move(mQueueBufferOutput);
527     return NO_ERROR;
528 }
529 
cancelBuffer(int pslot,const sp<Fence> & fence)530 status_t VirtualDisplaySurface::cancelBuffer(int pslot,
531         const sp<Fence>& fence) {
532     if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
533         return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
534     }
535 
536     VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
537                 dbgStateStr());
538     VDS_LOGV("cancelBuffer pslot=%d", pslot);
539     Source source = fbSourceForCompositionType(mCompositionType);
540     return mSource[source]->cancelBuffer(
541             mapProducer2SourceSlot(source, pslot), fence);
542 }
543 
query(int what,int * value)544 int VirtualDisplaySurface::query(int what, int* value) {
545     switch (what) {
546         case NATIVE_WINDOW_WIDTH:
547             *value = mSinkBufferWidth;
548             break;
549         case NATIVE_WINDOW_HEIGHT:
550             *value = mSinkBufferHeight;
551             break;
552         default:
553             return mSource[SOURCE_SINK]->query(what, value);
554     }
555     return NO_ERROR;
556 }
557 
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)558 status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
559         int api, bool producerControlledByApp,
560         QueueBufferOutput* output) {
561     QueueBufferOutput qbo;
562     status_t result = mSource[SOURCE_SINK]->connect(listener, api,
563             producerControlledByApp, &qbo);
564     if (result == NO_ERROR) {
565         updateQueueBufferOutput(std::move(qbo));
566         // This moves the frame timestamps and keeps a copy of all other fields.
567         *output = std::move(mQueueBufferOutput);
568     }
569     return result;
570 }
571 
disconnect(int api,DisconnectMode mode)572 status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
573     return mSource[SOURCE_SINK]->disconnect(api, mode);
574 }
575 
setSidebandStream(const sp<NativeHandle> &)576 status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
577     return INVALID_OPERATION;
578 }
579 
allocateBuffers(uint32_t,uint32_t,PixelFormat,uint64_t)580 void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
581         uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
582     // TODO: Should we actually allocate buffers for a virtual display?
583 }
584 
allowAllocation(bool)585 status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
586     return INVALID_OPERATION;
587 }
588 
setGenerationNumber(uint32_t)589 status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
590     ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
591     return INVALID_OPERATION;
592 }
593 
getConsumerName() const594 String8 VirtualDisplaySurface::getConsumerName() const {
595     return String8("VirtualDisplaySurface");
596 }
597 
setSharedBufferMode(bool)598 status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
599     ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
600     return INVALID_OPERATION;
601 }
602 
setAutoRefresh(bool)603 status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
604     ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
605     return INVALID_OPERATION;
606 }
607 
setDequeueTimeout(nsecs_t)608 status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
609     ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
610     return INVALID_OPERATION;
611 }
612 
getLastQueuedBuffer(sp<GraphicBuffer> *,sp<Fence> *,float[16])613 status_t VirtualDisplaySurface::getLastQueuedBuffer(
614         sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
615         float[16] /* outTransformMatrix*/) {
616     ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
617     return INVALID_OPERATION;
618 }
619 
getUniqueId(uint64_t *) const620 status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
621     ALOGE("getUniqueId not supported on VirtualDisplaySurface");
622     return INVALID_OPERATION;
623 }
624 
getConsumerUsage(uint64_t * outUsage) const625 status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
626     return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
627 }
628 
updateQueueBufferOutput(QueueBufferOutput && qbo)629 void VirtualDisplaySurface::updateQueueBufferOutput(
630         QueueBufferOutput&& qbo) {
631     mQueueBufferOutput = std::move(qbo);
632     mQueueBufferOutput.transformHint = 0;
633 }
634 
resetPerFrameState()635 void VirtualDisplaySurface::resetPerFrameState() {
636     mCompositionType = COMPOSITION_UNKNOWN;
637     mFbFence = Fence::NO_FENCE;
638     mOutputFence = Fence::NO_FENCE;
639     mOutputProducerSlot = -1;
640     mFbProducerSlot = -1;
641 }
642 
refreshOutputBuffer()643 status_t VirtualDisplaySurface::refreshOutputBuffer() {
644     LOG_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId));
645 
646     if (mOutputProducerSlot >= 0) {
647         mSource[SOURCE_SINK]->cancelBuffer(
648                 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
649                 mOutputFence);
650     }
651 
652     int sslot;
653     status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
654             &sslot, &mOutputFence);
655     if (result < 0)
656         return result;
657     mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
658 
659     // On GPU-only frames, we don't have the right output buffer acquire fence
660     // until after GPU calls queueBuffer(). So here we just set the buffer
661     // (for use in HWC prepare) but not the fence; we'll call this again with
662     // the proper fence once we have it.
663     const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
664     LOG_FATAL_IF(!halDisplayId);
665     result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
666                                   mProducerBuffers[mOutputProducerSlot]);
667 
668     return result;
669 }
670 
671 // This slot mapping function is its own inverse, so two copies are unnecessary.
672 // Both are kept to make the intent clear where the function is called, and for
673 // the (unlikely) chance that we switch to a different mapping function.
mapSource2ProducerSlot(Source source,int sslot)674 int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
675     if (source == SOURCE_SCRATCH) {
676         return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
677     } else {
678         return sslot;
679     }
680 }
mapProducer2SourceSlot(Source source,int pslot)681 int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
682     return mapSource2ProducerSlot(source, pslot);
683 }
684 
685 VirtualDisplaySurface::Source
fbSourceForCompositionType(CompositionType type)686 VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
687     return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
688 }
689 
dbgStateStr() const690 const char* VirtualDisplaySurface::dbgStateStr() const {
691     switch (mDbgState) {
692         case DBG_STATE_IDLE:
693             return "IDLE";
694         case DBG_STATE_PREPARED:
695             return "PREPARED";
696         case DBG_STATE_GPU:
697             return "GPU";
698         case DBG_STATE_GPU_DONE:
699             return "GPU_DONE";
700         case DBG_STATE_HWC:
701             return "HWC";
702         default:
703             return "INVALID";
704     }
705 }
706 
dbgSourceStr(Source s)707 const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
708     switch (s) {
709         case SOURCE_SINK:    return "SINK";
710         case SOURCE_SCRATCH: return "SCRATCH";
711         default:             return "INVALID";
712     }
713 }
714 
715 // ---------------------------------------------------------------------------
716 } // namespace android
717 // ---------------------------------------------------------------------------
718 
719 // TODO(b/129481165): remove the #pragma below and fix conversion issues
720 #pragma clang diagnostic pop // ignored "-Wconversion"
721