2 * Copyright 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // #define LOG_NDEBUG 0
18 #include "VirtualDisplaySurface.h"
19 #include "HWComposer.h"
21 // ---------------------------------------------------------------------------
23 // ---------------------------------------------------------------------------
25 #if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
26 static const bool sForceHwcCopy = true;
28 static const bool sForceHwcCopy = false;
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__)
38 static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType 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>";
48 VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
49 const sp<IGraphicBufferProducer>& sink,
50 const sp<BufferQueue>& bq,
56 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
57 mProducerSlotSource(0),
58 mDbgState(DBG_STATE_IDLE),
59 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
62 mSource[SOURCE_SINK] = sink;
63 mSource[SOURCE_SCRATCH] = bq;
67 int sinkWidth, sinkHeight;
68 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
69 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
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
76 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
77 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
79 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
80 mDefaultOutputFormat = sinkFormat;
82 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
84 mOutputFormat = mDefaultOutputFormat;
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);
93 VirtualDisplaySurface::~VirtualDisplaySurface() {
96 status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
100 mMustRecompose = mustRecompose;
102 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
103 "Unexpected beginFrame() in %s state", dbgStateStr());
104 mDbgState = DBG_STATE_BEGUN;
106 uint32_t transformHint, numPendingBuffers;
107 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
108 &transformHint, &numPendingBuffers);
110 return refreshOutputBuffer();
113 status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
117 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
118 "Unexpected prepareFrame() in %s state", dbgStateStr());
119 mDbgState = DBG_STATE_PREPARED;
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.
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;
134 if (mCompositionType != mDbgLastCompositionType) {
135 VDS_LOGV("prepareFrame: composition type changed to %s",
136 dbgCompositionTypeStr(mCompositionType));
137 mDbgLastCompositionType = mCompositionType;
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.
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
153 mOutputFormat = mDefaultOutputFormat;
154 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
155 refreshOutputBuffer();
161 status_t VirtualDisplaySurface::compositionComplete() {
165 status_t VirtualDisplaySurface::advanceFrame() {
169 if (mCompositionType == COMPOSITION_HWC) {
170 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
171 "Unexpected advanceFrame() in %s state on HWC frame",
174 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
175 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
178 mDbgState = DBG_STATE_HWC;
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");
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());
198 // At this point we know the output buffer acquire fence,
199 // so update HWC state with it.
200 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
202 status_t result = NO_ERROR;
203 if (fbBuffer != NULL) {
204 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
210 void VirtualDisplaySurface::onFrameCommitted() {
214 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
215 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
216 mDbgState = DBG_STATE_IDLE;
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);
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,
237 systemTime(), false /* isAutoTimestamp */,
238 Rect(mSinkBufferWidth, mSinkBufferHeight),
239 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
243 if (result == NO_ERROR) {
244 updateQueueBufferOutput(qbo);
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);
255 resetPerFrameState();
258 void VirtualDisplaySurface::dump(String8& result) const {
261 status_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());
267 *outBuf = mProducerBuffers[pslot];
271 status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
272 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
275 status_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);
280 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
281 mSinkBufferWidth, mSinkBufferHeight, format, usage);
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;
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;
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();
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());
314 status_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;
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);
323 status_t result = NO_ERROR;
324 Source source = fbSourceForCompositionType(mCompositionType);
326 if (source == SOURCE_SINK) {
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");
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
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",
354 mSinkBufferWidth, mSinkBufferHeight,
355 buf->getPixelFormat(), buf->getUsage());
356 mOutputFormat = format;
357 mOutputUsage = usage;
358 result = refreshOutputBuffer();
364 if (source == SOURCE_SINK) {
365 *pslot = mOutputProducerSlot;
366 *fence = mOutputFence;
369 result = dequeueBuffer(source, format, usage, &sslot, fence);
371 *pslot = mapSource2ProducerSlot(source, sslot);
377 status_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,
382 mDbgState = DBG_STATE_GLES_DONE;
384 VDS_LOGV("queueBuffer pslot=%d", pslot);
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)
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)
402 VDS_LOGW_IF(item.mBuf != sslot,
403 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
405 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
406 mFbFence = mSlots[item.mBuf].mFence;
409 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
410 "Unexpected queueBuffer in state %s for compositionType %s",
411 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
413 // Extract the GLES release fence for HWC to acquire
415 bool isAutoTimestamp;
420 input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode,
421 &transform, &async, &mFbFence);
423 mFbProducerSlot = pslot;
424 mOutputFence = mFbFence;
427 *output = mQueueBufferOutput;
431 void 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,
435 VDS_LOGV("cancelBuffer pslot=%d", pslot);
436 Source source = fbSourceForCompositionType(mCompositionType);
437 return mSource[source]->cancelBuffer(
438 mapProducer2SourceSlot(source, pslot), fence);
441 int VirtualDisplaySurface::query(int what, int* value) {
442 return mSource[SOURCE_SINK]->query(what, value);
445 status_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;
457 status_t VirtualDisplaySurface::disconnect(int api) {
458 return mSource[SOURCE_SINK]->disconnect(api);
461 void 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);
468 void 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;
478 status_t VirtualDisplaySurface::refreshOutputBuffer() {
479 if (mOutputProducerSlot >= 0) {
480 mSource[SOURCE_SINK]->cancelBuffer(
481 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
486 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
487 &sslot, &mOutputFence);
490 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
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]);
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.
505 int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
506 if (source == SOURCE_SCRATCH) {
507 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
512 int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
513 return mapSource2ProducerSlot(source, pslot);
516 VirtualDisplaySurface::Source
517 VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
518 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
521 const char* VirtualDisplaySurface::dbgStateStr() const {
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";
532 const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
534 case SOURCE_SINK: return "SINK";
535 case SOURCE_SCRATCH: return "SCRATCH";
536 default: return "INVALID";
540 // ---------------------------------------------------------------------------
541 } // namespace android
542 // ---------------------------------------------------------------------------