| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 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 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 18 | #include "VirtualDisplaySurface.h" | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 19 | #include "HWComposer.h" | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 20 |  | 
 | 21 | // --------------------------------------------------------------------------- | 
 | 22 | namespace android { | 
 | 23 | // --------------------------------------------------------------------------- | 
 | 24 |  | 
| Jesse Hall | c354eff | 2013-10-25 10:44:41 -0700 | [diff] [blame] | 25 | #if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS) | 
 | 26 | static const bool sForceHwcCopy = true; | 
 | 27 | #else | 
 | 28 | static const bool sForceHwcCopy = false; | 
 | 29 | #endif | 
| Naseer Ahmed | 6a96846 | 2013-10-04 16:15:22 -0400 | [diff] [blame] | 30 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 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__) | 
 | 37 |  | 
 | 38 | static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) { | 
 | 39 |     switch (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>"; | 
 | 45 |     } | 
 | 46 | } | 
 | 47 |  | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 48 | VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 49 |         const sp<IGraphicBufferProducer>& sink, | 
 | 50 |         const sp<BufferQueue>& bq, | 
 | 51 |         const String8& name) | 
 | 52 | :   ConsumerBase(bq), | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 53 |     mHwc(hwc), | 
 | 54 |     mDisplayId(dispId), | 
 | 55 |     mDisplayName(name), | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 56 |     mOutputUsage(GRALLOC_USAGE_HW_COMPOSER), | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 57 |     mProducerSlotSource(0), | 
 | 58 |     mDbgState(DBG_STATE_IDLE), | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 59 |     mDbgLastCompositionType(COMPOSITION_UNKNOWN), | 
 | 60 |     mMustRecompose(false) | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 61 | { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 62 |     mSource[SOURCE_SINK] = sink; | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 63 |     mSource[SOURCE_SCRATCH] = bq; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 64 |  | 
 | 65 |     resetPerFrameState(); | 
 | 66 |  | 
 | 67 |     int sinkWidth, sinkHeight; | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 68 |     sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth); | 
 | 69 |     sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight); | 
 | 70 |  | 
 | 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 | 
 | 74 |     // on usage bits. | 
 | 75 |     int sinkUsage; | 
 | 76 |     sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage); | 
 | 77 |     if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { | 
 | 78 |         int sinkFormat; | 
 | 79 |         sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat); | 
 | 80 |         mDefaultOutputFormat = sinkFormat; | 
 | 81 |     } else { | 
 | 82 |         mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; | 
 | 83 |     } | 
 | 84 |     mOutputFormat = mDefaultOutputFormat; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 85 |  | 
 | 86 |     ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string()); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 87 |     mConsumer->setConsumerName(ConsumerBase::mName); | 
 | 88 |     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER); | 
 | 89 |     mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight); | 
 | 90 |     mConsumer->setDefaultMaxBufferCount(2); | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 91 | } | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 92 |  | 
 | 93 | VirtualDisplaySurface::~VirtualDisplaySurface() { | 
 | 94 | } | 
 | 95 |  | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 96 | status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 97 |     if (mDisplayId < 0) | 
 | 98 |         return NO_ERROR; | 
 | 99 |  | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 100 |     mMustRecompose = mustRecompose; | 
 | 101 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 102 |     VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE, | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 103 |             "Unexpected beginFrame() in %s state", dbgStateStr()); | 
 | 104 |     mDbgState = DBG_STATE_BEGUN; | 
 | 105 |  | 
 | 106 |     uint32_t transformHint, numPendingBuffers; | 
 | 107 |     mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight, | 
 | 108 |             &transformHint, &numPendingBuffers); | 
 | 109 |  | 
 | 110 |     return refreshOutputBuffer(); | 
 | 111 | } | 
 | 112 |  | 
 | 113 | status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) { | 
 | 114 |     if (mDisplayId < 0) | 
 | 115 |         return NO_ERROR; | 
 | 116 |  | 
 | 117 |     VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN, | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 118 |             "Unexpected prepareFrame() in %s state", dbgStateStr()); | 
 | 119 |     mDbgState = DBG_STATE_PREPARED; | 
 | 120 |  | 
 | 121 |     mCompositionType = compositionType; | 
| Naseer Ahmed | 6a96846 | 2013-10-04 16:15:22 -0400 | [diff] [blame] | 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. | 
 | 128 |         // | 
 | 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; | 
 | 132 |     } | 
 | 133 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 134 |     if (mCompositionType != mDbgLastCompositionType) { | 
 | 135 |         VDS_LOGV("prepareFrame: composition type changed to %s", | 
 | 136 |                 dbgCompositionTypeStr(mCompositionType)); | 
 | 137 |         mDbgLastCompositionType = mCompositionType; | 
 | 138 |     } | 
 | 139 |  | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 140 |     if (mCompositionType != COMPOSITION_GLES && | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 141 |             (mOutputFormat != mDefaultOutputFormat || | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 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. | 
 | 149 |         // | 
 | 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 | 
 | 152 |         // dequeueBuffer(). | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 153 |         mOutputFormat = mDefaultOutputFormat; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 154 |         mOutputUsage = GRALLOC_USAGE_HW_COMPOSER; | 
 | 155 |         refreshOutputBuffer(); | 
 | 156 |     } | 
 | 157 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 158 |     return NO_ERROR; | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 159 | } | 
 | 160 |  | 
 | 161 | status_t VirtualDisplaySurface::compositionComplete() { | 
 | 162 |     return NO_ERROR; | 
 | 163 | } | 
 | 164 |  | 
 | 165 | status_t VirtualDisplaySurface::advanceFrame() { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 166 |     if (mDisplayId < 0) | 
 | 167 |         return NO_ERROR; | 
 | 168 |  | 
 | 169 |     if (mCompositionType == COMPOSITION_HWC) { | 
 | 170 |         VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, | 
 | 171 |                 "Unexpected advanceFrame() in %s state on HWC frame", | 
 | 172 |                 dbgStateStr()); | 
 | 173 |     } else { | 
 | 174 |         VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE, | 
 | 175 |                 "Unexpected advanceFrame() in %s state on GLES/MIXED frame", | 
 | 176 |                 dbgStateStr()); | 
 | 177 |     } | 
 | 178 |     mDbgState = DBG_STATE_HWC; | 
 | 179 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 180 |     if (mOutputProducerSlot < 0 || | 
 | 181 |             (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 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"); | 
 | 188 |         return NO_MEMORY; | 
 | 189 |     } | 
 | 190 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 191 |     sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ? | 
 | 192 |             mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 193 |     sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot]; | 
 | 194 |     VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)", | 
 | 195 |             mFbProducerSlot, fbBuffer.get(), | 
 | 196 |             mOutputProducerSlot, outBuffer.get()); | 
 | 197 |  | 
| Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 198 |     // At this point we know the output buffer acquire fence, | 
 | 199 |     // so update HWC state with it. | 
 | 200 |     mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer); | 
 | 201 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 202 |     status_t result = NO_ERROR; | 
 | 203 |     if (fbBuffer != NULL) { | 
 | 204 |         result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer); | 
 | 205 |     } | 
 | 206 |  | 
 | 207 |     return result; | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 208 | } | 
 | 209 |  | 
| Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 210 | void VirtualDisplaySurface::onFrameCommitted() { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 211 |     if (mDisplayId < 0) | 
 | 212 |         return; | 
 | 213 |  | 
 | 214 |     VDS_LOGW_IF(mDbgState != DBG_STATE_HWC, | 
 | 215 |             "Unexpected onFrameCommitted() in %s state", dbgStateStr()); | 
 | 216 |     mDbgState = DBG_STATE_IDLE; | 
 | 217 |  | 
 | 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); | 
 | 227 |     } | 
 | 228 |  | 
 | 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); | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 234 |         if (mMustRecompose) { | 
 | 235 |             status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot, | 
 | 236 |                     QueueBufferInput( | 
 | 237 |                         systemTime(), false /* isAutoTimestamp */, | 
 | 238 |                         Rect(mSinkBufferWidth, mSinkBufferHeight), | 
 | 239 |                         NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */, | 
 | 240 |                         true /* async*/, | 
 | 241 |                         outFence), | 
 | 242 |                     &qbo); | 
 | 243 |             if (result == NO_ERROR) { | 
 | 244 |                 updateQueueBufferOutput(qbo); | 
 | 245 |             } | 
 | 246 |         } else { | 
 | 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); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 252 |         } | 
 | 253 |     } | 
 | 254 |  | 
 | 255 |     resetPerFrameState(); | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 256 | } | 
 | 257 |  | 
 | 258 | void VirtualDisplaySurface::dump(String8& result) const { | 
 | 259 | } | 
 | 260 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 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()); | 
 | 266 |  | 
 | 267 |     *outBuf = mProducerBuffers[pslot]; | 
 | 268 |     return NO_ERROR; | 
 | 269 | } | 
 | 270 |  | 
 | 271 | status_t VirtualDisplaySurface::setBufferCount(int bufferCount) { | 
 | 272 |     return mSource[SOURCE_SINK]->setBufferCount(bufferCount); | 
 | 273 | } | 
 | 274 |  | 
 | 275 | status_t VirtualDisplaySurface::dequeueBuffer(Source source, | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 276 |         uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) { | 
| Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 277 |     // Don't let a slow consumer block us | 
 | 278 |     bool async = (source == SOURCE_SINK); | 
 | 279 |  | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 280 |     status_t result = mSource[source]->dequeueBuffer(sslot, fence, async, | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 281 |             mSinkBufferWidth, mSinkBufferHeight, format, usage); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 282 |     if (result < 0) | 
 | 283 |         return result; | 
 | 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; | 
 | 288 |  | 
 | 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; | 
 | 295 |     } | 
 | 296 |  | 
 | 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(); | 
 | 301 |         } | 
 | 302 |     } | 
 | 303 |     if (result & BUFFER_NEEDS_REALLOCATION) { | 
 | 304 |         mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]); | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 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()); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 309 |     } | 
 | 310 |  | 
 | 311 |     return result; | 
 | 312 | } | 
 | 313 |  | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 314 | status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async, | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 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; | 
 | 319 |  | 
| Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 320 |     VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)"); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 321 |     VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage); | 
 | 322 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 323 |     status_t result = NO_ERROR; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 324 |     Source source = fbSourceForCompositionType(mCompositionType); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 325 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 326 |     if (source == SOURCE_SINK) { | 
| Mathias Agopian | 6da15f4 | 2013-09-25 20:40:07 -0700 | [diff] [blame] | 327 |  | 
 | 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"); | 
 | 335 |             return NO_MEMORY; | 
 | 336 |         } | 
 | 337 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 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 | 
 | 342 |         // shouldn't matter. | 
 | 343 |  | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 344 |         usage |= GRALLOC_USAGE_HW_COMPOSER; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 345 |         const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot]; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 346 |         if ((usage & ~buf->getUsage()) != 0 || | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 347 |                 (format != 0 && format != (uint32_t)buf->getPixelFormat()) || | 
 | 348 |                 (w != 0 && w != mSinkBufferWidth) || | 
 | 349 |                 (h != 0 && h != mSinkBufferHeight)) { | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 350 |             VDS_LOGV("dequeueBuffer: dequeueing new output buffer: " | 
 | 351 |                     "want %dx%d fmt=%d use=%#x, " | 
 | 352 |                     "have %dx%d fmt=%d use=%#x", | 
 | 353 |                     w, h, format, usage, | 
 | 354 |                     mSinkBufferWidth, mSinkBufferHeight, | 
 | 355 |                     buf->getPixelFormat(), buf->getUsage()); | 
 | 356 |             mOutputFormat = format; | 
 | 357 |             mOutputUsage = usage; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 358 |             result = refreshOutputBuffer(); | 
 | 359 |             if (result < 0) | 
 | 360 |                 return result; | 
 | 361 |         } | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 362 |     } | 
 | 363 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 364 |     if (source == SOURCE_SINK) { | 
 | 365 |         *pslot = mOutputProducerSlot; | 
 | 366 |         *fence = mOutputFence; | 
 | 367 |     } else { | 
 | 368 |         int sslot; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 369 |         result = dequeueBuffer(source, format, usage, &sslot, fence); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 370 |         if (result >= 0) { | 
 | 371 |             *pslot = mapSource2ProducerSlot(source, sslot); | 
 | 372 |         } | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 373 |     } | 
 | 374 |     return result; | 
 | 375 | } | 
 | 376 |  | 
| Dan Stoza | 88a459a | 2014-03-12 09:34:36 -0700 | [diff] [blame^] | 377 | status_t VirtualDisplaySurface::detachBuffer(int /* slot */) { | 
 | 378 |     VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface"); | 
 | 379 |     return INVALID_OPERATION; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 380 | } | 
 | 381 |  | 
| Dan Stoza | 88a459a | 2014-03-12 09:34:36 -0700 | [diff] [blame^] | 382 | status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */, | 
 | 383 |         const sp<GraphicBuffer>& /* buffer */) { | 
 | 384 |     VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface"); | 
 | 385 |     return INVALID_OPERATION; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 386 | } | 
 | 387 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 388 | status_t VirtualDisplaySurface::queueBuffer(int pslot, | 
 | 389 |         const QueueBufferInput& input, QueueBufferOutput* output) { | 
 | 390 |     VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, | 
 | 391 |             "Unexpected queueBuffer(pslot=%d) in %s state", pslot, | 
 | 392 |             dbgStateStr()); | 
 | 393 |     mDbgState = DBG_STATE_GLES_DONE; | 
 | 394 |  | 
 | 395 |     VDS_LOGV("queueBuffer pslot=%d", pslot); | 
 | 396 |  | 
 | 397 |     status_t result; | 
 | 398 |     if (mCompositionType == COMPOSITION_MIXED) { | 
 | 399 |         // Queue the buffer back into the scratch pool | 
 | 400 |         QueueBufferOutput scratchQBO; | 
 | 401 |         int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 402 |         result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 403 |         if (result != NO_ERROR) | 
 | 404 |             return result; | 
 | 405 |  | 
 | 406 |         // Now acquire the buffer from the scratch pool -- should be the same | 
 | 407 |         // slot and fence as we just queued. | 
 | 408 |         Mutex::Autolock lock(mMutex); | 
 | 409 |         BufferQueue::BufferItem item; | 
| Jesse Hall | bce7611 | 2013-07-16 13:46:20 -0700 | [diff] [blame] | 410 |         result = acquireBufferLocked(&item, 0); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 411 |         if (result != NO_ERROR) | 
 | 412 |             return result; | 
 | 413 |         VDS_LOGW_IF(item.mBuf != sslot, | 
 | 414 |                 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d", | 
 | 415 |                 item.mBuf, sslot); | 
 | 416 |         mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf); | 
 | 417 |         mFbFence = mSlots[item.mBuf].mFence; | 
 | 418 |  | 
 | 419 |     } else { | 
 | 420 |         LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES, | 
 | 421 |                 "Unexpected queueBuffer in state %s for compositionType %s", | 
 | 422 |                 dbgStateStr(), dbgCompositionTypeStr(mCompositionType)); | 
 | 423 |  | 
 | 424 |         // Extract the GLES release fence for HWC to acquire | 
 | 425 |         int64_t timestamp; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 426 |         bool isAutoTimestamp; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 427 |         Rect crop; | 
 | 428 |         int scalingMode; | 
 | 429 |         uint32_t transform; | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 430 |         bool async; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 431 |         input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, | 
 | 432 |                 &transform, &async, &mFbFence); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 433 |  | 
 | 434 |         mFbProducerSlot = pslot; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 435 |         mOutputFence = mFbFence; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 436 |     } | 
 | 437 |  | 
 | 438 |     *output = mQueueBufferOutput; | 
 | 439 |     return NO_ERROR; | 
 | 440 | } | 
 | 441 |  | 
 | 442 | void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) { | 
 | 443 |     VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, | 
 | 444 |             "Unexpected cancelBuffer(pslot=%d) in %s state", pslot, | 
 | 445 |             dbgStateStr()); | 
 | 446 |     VDS_LOGV("cancelBuffer pslot=%d", pslot); | 
 | 447 |     Source source = fbSourceForCompositionType(mCompositionType); | 
 | 448 |     return mSource[source]->cancelBuffer( | 
 | 449 |             mapProducer2SourceSlot(source, pslot), fence); | 
 | 450 | } | 
 | 451 |  | 
 | 452 | int VirtualDisplaySurface::query(int what, int* value) { | 
 | 453 |     return mSource[SOURCE_SINK]->query(what, value); | 
 | 454 | } | 
 | 455 |  | 
| Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 456 | status_t VirtualDisplaySurface::connect(const sp<IBinder>& token, | 
 | 457 |         int api, bool producerControlledByApp, | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 458 |         QueueBufferOutput* output) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 459 |     QueueBufferOutput qbo; | 
| Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 460 |     status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 461 |     if (result == NO_ERROR) { | 
 | 462 |         updateQueueBufferOutput(qbo); | 
 | 463 |         *output = mQueueBufferOutput; | 
 | 464 |     } | 
 | 465 |     return result; | 
 | 466 | } | 
 | 467 |  | 
 | 468 | status_t VirtualDisplaySurface::disconnect(int api) { | 
 | 469 |     return mSource[SOURCE_SINK]->disconnect(api); | 
 | 470 | } | 
 | 471 |  | 
 | 472 | void VirtualDisplaySurface::updateQueueBufferOutput( | 
 | 473 |         const QueueBufferOutput& qbo) { | 
 | 474 |     uint32_t w, h, transformHint, numPendingBuffers; | 
 | 475 |     qbo.deflate(&w, &h, &transformHint, &numPendingBuffers); | 
 | 476 |     mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers); | 
 | 477 | } | 
 | 478 |  | 
 | 479 | void VirtualDisplaySurface::resetPerFrameState() { | 
 | 480 |     mCompositionType = COMPOSITION_UNKNOWN; | 
 | 481 |     mSinkBufferWidth = 0; | 
 | 482 |     mSinkBufferHeight = 0; | 
| mayank parshar | b988f85 | 2014-01-10 10:27:45 +0530 | [diff] [blame] | 483 |     mFbFence = Fence::NO_FENCE; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 484 |     mOutputFence = Fence::NO_FENCE; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 485 |     mOutputProducerSlot = -1; | 
| mayank parshar | fdfde88 | 2014-01-23 15:08:31 +0530 | [diff] [blame] | 486 |     mFbProducerSlot = -1; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 487 | } | 
 | 488 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 489 | status_t VirtualDisplaySurface::refreshOutputBuffer() { | 
 | 490 |     if (mOutputProducerSlot >= 0) { | 
 | 491 |         mSource[SOURCE_SINK]->cancelBuffer( | 
 | 492 |                 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot), | 
 | 493 |                 mOutputFence); | 
 | 494 |     } | 
 | 495 |  | 
 | 496 |     int sslot; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 497 |     status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage, | 
 | 498 |             &sslot, &mOutputFence); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 499 |     if (result < 0) | 
 | 500 |         return result; | 
 | 501 |     mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot); | 
 | 502 |  | 
| Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 503 |     // On GLES-only frames, we don't have the right output buffer acquire fence | 
 | 504 |     // until after GLES calls queueBuffer(). So here we just set the buffer | 
 | 505 |     // (for use in HWC prepare) but not the fence; we'll call this again with | 
 | 506 |     // the proper fence once we have it. | 
 | 507 |     result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE, | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 508 |             mProducerBuffers[mOutputProducerSlot]); | 
 | 509 |  | 
 | 510 |     return result; | 
 | 511 | } | 
 | 512 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 513 | // This slot mapping function is its own inverse, so two copies are unnecessary. | 
 | 514 | // Both are kept to make the intent clear where the function is called, and for | 
 | 515 | // the (unlikely) chance that we switch to a different mapping function. | 
 | 516 | int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) { | 
 | 517 |     if (source == SOURCE_SCRATCH) { | 
 | 518 |         return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1; | 
 | 519 |     } else { | 
 | 520 |         return sslot; | 
 | 521 |     } | 
 | 522 | } | 
 | 523 | int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) { | 
 | 524 |     return mapSource2ProducerSlot(source, pslot); | 
 | 525 | } | 
 | 526 |  | 
 | 527 | VirtualDisplaySurface::Source | 
 | 528 | VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) { | 
 | 529 |     return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK; | 
 | 530 | } | 
 | 531 |  | 
 | 532 | const char* VirtualDisplaySurface::dbgStateStr() const { | 
 | 533 |     switch (mDbgState) { | 
 | 534 |         case DBG_STATE_IDLE:      return "IDLE"; | 
 | 535 |         case DBG_STATE_PREPARED:  return "PREPARED"; | 
 | 536 |         case DBG_STATE_GLES:      return "GLES"; | 
 | 537 |         case DBG_STATE_GLES_DONE: return "GLES_DONE"; | 
 | 538 |         case DBG_STATE_HWC:       return "HWC"; | 
 | 539 |         default:                  return "INVALID"; | 
 | 540 |     } | 
 | 541 | } | 
 | 542 |  | 
 | 543 | const char* VirtualDisplaySurface::dbgSourceStr(Source s) { | 
 | 544 |     switch (s) { | 
 | 545 |         case SOURCE_SINK:    return "SINK"; | 
 | 546 |         case SOURCE_SCRATCH: return "SCRATCH"; | 
 | 547 |         default:             return "INVALID"; | 
 | 548 |     } | 
 | 549 | } | 
 | 550 |  | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 551 | // --------------------------------------------------------------------------- | 
 | 552 | } // namespace android | 
 | 553 | // --------------------------------------------------------------------------- |