VirtualDisplaySurface.cpp revision 29a3e90879fd96404c971e7187cd0e05927bbce0
149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes/* 249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Copyright 2013 The Android Open Source Project 349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Licensed under the Apache License, Version 2.0 (the "License"); 549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * you may not use this file except in compliance with the License. 649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * You may obtain a copy of the License at 749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * http://www.apache.org/licenses/LICENSE-2.0 949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 1049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Unless required by applicable law or agreed to in writing, software 1149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * distributed under the License is distributed on an "AS IS" BASIS, 1249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * See the License for the specific language governing permissions and 1449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * limitations under the License. 1549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes */ 1649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas// #define LOG_NDEBUG 0 1849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes#include "VirtualDisplaySurface.h" 19ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas#include "HWComposer.h" 208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas 211f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes// --------------------------------------------------------------------------- 221f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesnamespace android { 231f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes// --------------------------------------------------------------------------- 2449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 2549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS) 263de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasstatic const bool sForceHwcCopy = true; 273de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikas#else 283de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasstatic const bool sForceHwcCopy = false; 294c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes#endif 301f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 311f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes#define VDS_LOGE(msg, ...) ALOGE("[%s] "msg, \ 3249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDisplayName.string(), ##__VA_ARGS__) 3349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] "msg, \ 3449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDisplayName.string(), ##__VA_ARGS__) 358e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas#define VDS_LOGV(msg, ...) ALOGV("[%s] "msg, \ 3649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDisplayName.string(), ##__VA_ARGS__) 371f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 381f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesstatic const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) { 391f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes switch (type) { 401f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN"; 411f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes case DisplaySurface::COMPOSITION_GLES: return "GLES"; 421f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes case DisplaySurface::COMPOSITION_HWC: return "HWC"; 431f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes case DisplaySurface::COMPOSITION_MIXED: return "MIXED"; 441f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes default: return "<INVALID>"; 451f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 464c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes} 474c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes 484c5457da0e82b0090f03655545772ebb8cf4a978Chris BanesVirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, 491f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes const sp<IGraphicBufferProducer>& sink, 504c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes const sp<IGraphicBufferProducer>& bqProducer, 511f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes const sp<IGraphicBufferConsumer>& bqConsumer, 521f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes const String8& name) 531f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes: ConsumerBase(bqConsumer), 5449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mHwc(hwc), 5549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDisplayId(dispId), 5649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDisplayName(name), 5749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mOutputUsage(GRALLOC_USAGE_HW_COMPOSER), 5849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mProducerSlotSource(0), 5949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDbgState(DBG_STATE_IDLE), 6049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mDbgLastCompositionType(COMPOSITION_UNKNOWN), 6149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mMustRecompose(false) 621f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes{ 631f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes mSource[SOURCE_SINK] = sink; 641f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes mSource[SOURCE_SCRATCH] = bqProducer; 651f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 661f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes resetPerFrameState(); 671f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 681f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes int sinkWidth, sinkHeight; 691f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth); 704c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight); 711f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 721f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes // Pick the buffer format to request from the sink when not rendering to it 731f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes // with GLES. If the consumer needs CPU access, use the default format 741f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes // set by the consumer. Otherwise allow gralloc to decide the format based 751f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes // on usage bits. 761f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes int sinkUsage; 771f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage); 781f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { 791f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes int sinkFormat; 804c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat); 811f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes mDefaultOutputFormat = sinkFormat; 824c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes } else { 834c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 844c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes } 854c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mOutputFormat = mDefaultOutputFormat; 864c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes 874c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string()); 884c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mConsumer->setConsumerName(ConsumerBase::mName); 894c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER); 904c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight); 914c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes mConsumer->setDefaultMaxBufferCount(2); 924c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes} 934c5457da0e82b0090f03655545772ebb8cf4a978Chris Banes 944c5457da0e82b0090f03655545772ebb8cf4a978Chris BanesVirtualDisplaySurface::~VirtualDisplaySurface() { 951f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes} 961f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 971f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesstatus_t VirtualDisplaySurface::beginFrame(bool mustRecompose) { 9849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes if (mDisplayId < 0) 99 return NO_ERROR; 100 101 mMustRecompose = mustRecompose; 102 103 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE, 104 "Unexpected beginFrame() in %s state", dbgStateStr()); 105 mDbgState = DBG_STATE_BEGUN; 106 107 uint32_t transformHint, numPendingBuffers; 108 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight, 109 &transformHint, &numPendingBuffers); 110 111 return refreshOutputBuffer(); 112} 113 114status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) { 115 if (mDisplayId < 0) 116 return NO_ERROR; 117 118 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN, 119 "Unexpected prepareFrame() in %s state", dbgStateStr()); 120 mDbgState = DBG_STATE_PREPARED; 121 122 mCompositionType = compositionType; 123 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) { 124 // Some hardware can do RGB->YUV conversion more efficiently in hardware 125 // controlled by HWC than in hardware controlled by the video encoder. 126 // Forcing GLES-composed frames to go through an extra copy by the HWC 127 // allows the format conversion to happen there, rather than passing RGB 128 // directly to the consumer. 129 // 130 // On the other hand, when the consumer prefers RGB or can consume RGB 131 // inexpensively, this forces an unnecessary copy. 132 mCompositionType = COMPOSITION_MIXED; 133 } 134 135 if (mCompositionType != mDbgLastCompositionType) { 136 VDS_LOGV("prepareFrame: composition type changed to %s", 137 dbgCompositionTypeStr(mCompositionType)); 138 mDbgLastCompositionType = mCompositionType; 139 } 140 141 if (mCompositionType != COMPOSITION_GLES && 142 (mOutputFormat != mDefaultOutputFormat || 143 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) { 144 // We must have just switched from GLES-only to MIXED or HWC 145 // composition. Stop using the format and usage requested by the GLES 146 // driver; they may be suboptimal when HWC is writing to the output 147 // buffer. For example, if the output is going to a video encoder, and 148 // HWC can write directly to YUV, some hardware can skip a 149 // memory-to-memory RGB-to-YUV conversion step. 150 // 151 // If we just switched *to* GLES-only mode, we'll change the 152 // format/usage and get a new buffer when the GLES driver calls 153 // dequeueBuffer(). 154 mOutputFormat = mDefaultOutputFormat; 155 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER; 156 refreshOutputBuffer(); 157 } 158 159 return NO_ERROR; 160} 161 162status_t VirtualDisplaySurface::compositionComplete() { 163 return NO_ERROR; 164} 165 166status_t VirtualDisplaySurface::advanceFrame() { 167 if (mDisplayId < 0) 168 return NO_ERROR; 169 170 if (mCompositionType == COMPOSITION_HWC) { 171 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, 172 "Unexpected advanceFrame() in %s state on HWC frame", 173 dbgStateStr()); 174 } else { 175 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE, 176 "Unexpected advanceFrame() in %s state on GLES/MIXED frame", 177 dbgStateStr()); 178 } 179 mDbgState = DBG_STATE_HWC; 180 181 if (mOutputProducerSlot < 0 || 182 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) { 183 // Last chance bailout if something bad happened earlier. For example, 184 // in a GLES configuration, if the sink disappears then dequeueBuffer 185 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger 186 // will soldier on. So we end up here without a buffer. There should 187 // be lots of scary messages in the log just before this. 188 VDS_LOGE("advanceFrame: no buffer, bailing out"); 189 return NO_MEMORY; 190 } 191 192 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ? 193 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL); 194 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot]; 195 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)", 196 mFbProducerSlot, fbBuffer.get(), 197 mOutputProducerSlot, outBuffer.get()); 198 199 // At this point we know the output buffer acquire fence, 200 // so update HWC state with it. 201 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer); 202 203 status_t result = NO_ERROR; 204 if (fbBuffer != NULL) { 205 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer); 206 } 207 208 return result; 209} 210 211void VirtualDisplaySurface::onFrameCommitted() { 212 if (mDisplayId < 0) 213 return; 214 215 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC, 216 "Unexpected onFrameCommitted() in %s state", dbgStateStr()); 217 mDbgState = DBG_STATE_IDLE; 218 219 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId); 220 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) { 221 // release the scratch buffer back to the pool 222 Mutex::Autolock lock(mMutex); 223 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot); 224 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot); 225 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence); 226 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot], 227 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); 228 } 229 230 if (mOutputProducerSlot >= 0) { 231 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot); 232 QueueBufferOutput qbo; 233 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId); 234 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot); 235 if (mMustRecompose) { 236 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot, 237 QueueBufferInput( 238 systemTime(), false /* isAutoTimestamp */, 239 Rect(mSinkBufferWidth, mSinkBufferHeight), 240 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */, 241 true /* async*/, 242 outFence), 243 &qbo); 244 if (result == NO_ERROR) { 245 updateQueueBufferOutput(qbo); 246 } 247 } else { 248 // If the surface hadn't actually been updated, then we only went 249 // through the motions of updating the display to keep our state 250 // machine happy. We cancel the buffer to avoid triggering another 251 // re-composition and causing an infinite loop. 252 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence); 253 } 254 } 255 256 resetPerFrameState(); 257} 258 259void VirtualDisplaySurface::dump(String8& /* result */) const { 260} 261 262status_t VirtualDisplaySurface::requestBuffer(int pslot, 263 sp<GraphicBuffer>* outBuf) { 264 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, 265 "Unexpected requestBuffer pslot=%d in %s state", 266 pslot, dbgStateStr()); 267 268 *outBuf = mProducerBuffers[pslot]; 269 return NO_ERROR; 270} 271 272status_t VirtualDisplaySurface::setBufferCount(int bufferCount) { 273 return mSource[SOURCE_SINK]->setBufferCount(bufferCount); 274} 275 276status_t VirtualDisplaySurface::dequeueBuffer(Source source, 277 uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) { 278 // Don't let a slow consumer block us 279 bool async = (source == SOURCE_SINK); 280 281 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async, 282 mSinkBufferWidth, mSinkBufferHeight, format, usage); 283 if (result < 0) 284 return result; 285 int pslot = mapSource2ProducerSlot(source, *sslot); 286 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d", 287 dbgSourceStr(source), *sslot, pslot, result); 288 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot; 289 290 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) { 291 // This slot was previously dequeued from the other source; must 292 // re-request the buffer. 293 result |= BUFFER_NEEDS_REALLOCATION; 294 mProducerSlotSource &= ~(1ULL << pslot); 295 mProducerSlotSource |= sourceBit; 296 } 297 298 if (result & RELEASE_ALL_BUFFERS) { 299 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { 300 if ((mProducerSlotSource & (1ULL << i)) == sourceBit) 301 mProducerBuffers[i].clear(); 302 } 303 } 304 if (result & BUFFER_NEEDS_REALLOCATION) { 305 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]); 306 if (result < 0) { 307 mProducerBuffers[pslot].clear(); 308 mSource[source]->cancelBuffer(*sslot, *fence); 309 return result; 310 } 311 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x", 312 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(), 313 mProducerBuffers[pslot]->getPixelFormat(), 314 mProducerBuffers[pslot]->getUsage()); 315 } 316 317 return result; 318} 319 320status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async, 321 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { 322 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, 323 "Unexpected dequeueBuffer() in %s state", dbgStateStr()); 324 mDbgState = DBG_STATE_GLES; 325 326 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)"); 327 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage); 328 329 status_t result = NO_ERROR; 330 Source source = fbSourceForCompositionType(mCompositionType); 331 332 if (source == SOURCE_SINK) { 333 334 if (mOutputProducerSlot < 0) { 335 // Last chance bailout if something bad happened earlier. For example, 336 // in a GLES configuration, if the sink disappears then dequeueBuffer 337 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger 338 // will soldier on. So we end up here without a buffer. There should 339 // be lots of scary messages in the log just before this. 340 VDS_LOGE("dequeueBuffer: no buffer, bailing out"); 341 return NO_MEMORY; 342 } 343 344 // We already dequeued the output buffer. If the GLES driver wants 345 // something incompatible, we have to cancel and get a new one. This 346 // will mean that HWC will see a different output buffer between 347 // prepare and set, but since we're in GLES-only mode already it 348 // shouldn't matter. 349 350 usage |= GRALLOC_USAGE_HW_COMPOSER; 351 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot]; 352 if ((usage & ~buf->getUsage()) != 0 || 353 (format != 0 && format != (uint32_t)buf->getPixelFormat()) || 354 (w != 0 && w != mSinkBufferWidth) || 355 (h != 0 && h != mSinkBufferHeight)) { 356 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: " 357 "want %dx%d fmt=%d use=%#x, " 358 "have %dx%d fmt=%d use=%#x", 359 w, h, format, usage, 360 mSinkBufferWidth, mSinkBufferHeight, 361 buf->getPixelFormat(), buf->getUsage()); 362 mOutputFormat = format; 363 mOutputUsage = usage; 364 result = refreshOutputBuffer(); 365 if (result < 0) 366 return result; 367 } 368 } 369 370 if (source == SOURCE_SINK) { 371 *pslot = mOutputProducerSlot; 372 *fence = mOutputFence; 373 } else { 374 int sslot; 375 result = dequeueBuffer(source, format, usage, &sslot, fence); 376 if (result >= 0) { 377 *pslot = mapSource2ProducerSlot(source, sslot); 378 } 379 } 380 return result; 381} 382 383status_t VirtualDisplaySurface::detachBuffer(int /* slot */) { 384 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface"); 385 return INVALID_OPERATION; 386} 387 388status_t VirtualDisplaySurface::detachNextBuffer( 389 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) { 390 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface"); 391 return INVALID_OPERATION; 392} 393 394status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */, 395 const sp<GraphicBuffer>& /* buffer */) { 396 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface"); 397 return INVALID_OPERATION; 398} 399 400status_t VirtualDisplaySurface::queueBuffer(int pslot, 401 const QueueBufferInput& input, QueueBufferOutput* output) { 402 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, 403 "Unexpected queueBuffer(pslot=%d) in %s state", pslot, 404 dbgStateStr()); 405 mDbgState = DBG_STATE_GLES_DONE; 406 407 VDS_LOGV("queueBuffer pslot=%d", pslot); 408 409 status_t result; 410 if (mCompositionType == COMPOSITION_MIXED) { 411 // Queue the buffer back into the scratch pool 412 QueueBufferOutput scratchQBO; 413 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot); 414 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO); 415 if (result != NO_ERROR) 416 return result; 417 418 // Now acquire the buffer from the scratch pool -- should be the same 419 // slot and fence as we just queued. 420 Mutex::Autolock lock(mMutex); 421 BufferQueue::BufferItem item; 422 result = acquireBufferLocked(&item, 0); 423 if (result != NO_ERROR) 424 return result; 425 VDS_LOGW_IF(item.mBuf != sslot, 426 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d", 427 item.mBuf, sslot); 428 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf); 429 mFbFence = mSlots[item.mBuf].mFence; 430 431 } else { 432 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES, 433 "Unexpected queueBuffer in state %s for compositionType %s", 434 dbgStateStr(), dbgCompositionTypeStr(mCompositionType)); 435 436 // Extract the GLES release fence for HWC to acquire 437 int64_t timestamp; 438 bool isAutoTimestamp; 439 Rect crop; 440 int scalingMode; 441 uint32_t transform; 442 bool async; 443 input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, 444 &transform, &async, &mFbFence); 445 446 mFbProducerSlot = pslot; 447 mOutputFence = mFbFence; 448 } 449 450 *output = mQueueBufferOutput; 451 return NO_ERROR; 452} 453 454void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) { 455 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, 456 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot, 457 dbgStateStr()); 458 VDS_LOGV("cancelBuffer pslot=%d", pslot); 459 Source source = fbSourceForCompositionType(mCompositionType); 460 return mSource[source]->cancelBuffer( 461 mapProducer2SourceSlot(source, pslot), fence); 462} 463 464int VirtualDisplaySurface::query(int what, int* value) { 465 return mSource[SOURCE_SINK]->query(what, value); 466} 467 468status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener, 469 int api, bool producerControlledByApp, 470 QueueBufferOutput* output) { 471 QueueBufferOutput qbo; 472 status_t result = mSource[SOURCE_SINK]->connect(listener, api, 473 producerControlledByApp, &qbo); 474 if (result == NO_ERROR) { 475 updateQueueBufferOutput(qbo); 476 *output = mQueueBufferOutput; 477 } 478 return result; 479} 480 481status_t VirtualDisplaySurface::disconnect(int api) { 482 return mSource[SOURCE_SINK]->disconnect(api); 483} 484 485status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) { 486 return INVALID_OPERATION; 487} 488 489void VirtualDisplaySurface::allocateBuffers(bool /* async */, 490 uint32_t /* width */, uint32_t /* height */, uint32_t /* format */, 491 uint32_t /* usage */) { 492 // TODO: Should we actually allocate buffers for a virtual display? 493} 494 495void VirtualDisplaySurface::updateQueueBufferOutput( 496 const QueueBufferOutput& qbo) { 497 uint32_t w, h, transformHint, numPendingBuffers; 498 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers); 499 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers); 500} 501 502void VirtualDisplaySurface::resetPerFrameState() { 503 mCompositionType = COMPOSITION_UNKNOWN; 504 mSinkBufferWidth = 0; 505 mSinkBufferHeight = 0; 506 mFbFence = Fence::NO_FENCE; 507 mOutputFence = Fence::NO_FENCE; 508 mOutputProducerSlot = -1; 509 mFbProducerSlot = -1; 510} 511 512status_t VirtualDisplaySurface::refreshOutputBuffer() { 513 if (mOutputProducerSlot >= 0) { 514 mSource[SOURCE_SINK]->cancelBuffer( 515 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot), 516 mOutputFence); 517 } 518 519 int sslot; 520 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage, 521 &sslot, &mOutputFence); 522 if (result < 0) 523 return result; 524 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot); 525 526 // On GLES-only frames, we don't have the right output buffer acquire fence 527 // until after GLES calls queueBuffer(). So here we just set the buffer 528 // (for use in HWC prepare) but not the fence; we'll call this again with 529 // the proper fence once we have it. 530 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE, 531 mProducerBuffers[mOutputProducerSlot]); 532 533 return result; 534} 535 536// This slot mapping function is its own inverse, so two copies are unnecessary. 537// Both are kept to make the intent clear where the function is called, and for 538// the (unlikely) chance that we switch to a different mapping function. 539int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) { 540 if (source == SOURCE_SCRATCH) { 541 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1; 542 } else { 543 return sslot; 544 } 545} 546int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) { 547 return mapSource2ProducerSlot(source, pslot); 548} 549 550VirtualDisplaySurface::Source 551VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) { 552 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK; 553} 554 555const char* VirtualDisplaySurface::dbgStateStr() const { 556 switch (mDbgState) { 557 case DBG_STATE_IDLE: return "IDLE"; 558 case DBG_STATE_PREPARED: return "PREPARED"; 559 case DBG_STATE_GLES: return "GLES"; 560 case DBG_STATE_GLES_DONE: return "GLES_DONE"; 561 case DBG_STATE_HWC: return "HWC"; 562 default: return "INVALID"; 563 } 564} 565 566const char* VirtualDisplaySurface::dbgSourceStr(Source s) { 567 switch (s) { 568 case SOURCE_SINK: return "SINK"; 569 case SOURCE_SCRATCH: return "SCRATCH"; 570 default: return "INVALID"; 571 } 572} 573 574// --------------------------------------------------------------------------- 575} // namespace android 576// --------------------------------------------------------------------------- 577