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