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