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