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 | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 25 | #define VDS_LOGE(msg, ...) ALOGE("[%s] "msg, \ |
| 26 | mDisplayName.string(), ##__VA_ARGS__) |
| 27 | #define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] "msg, \ |
| 28 | mDisplayName.string(), ##__VA_ARGS__) |
| 29 | #define VDS_LOGV(msg, ...) ALOGV("[%s] "msg, \ |
| 30 | mDisplayName.string(), ##__VA_ARGS__) |
| 31 | |
| 32 | static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) { |
| 33 | switch (type) { |
| 34 | case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN"; |
| 35 | case DisplaySurface::COMPOSITION_GLES: return "GLES"; |
| 36 | case DisplaySurface::COMPOSITION_HWC: return "HWC"; |
| 37 | case DisplaySurface::COMPOSITION_MIXED: return "MIXED"; |
| 38 | default: return "<INVALID>"; |
| 39 | } |
| 40 | } |
| 41 | |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 42 | VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 43 | const sp<IGraphicBufferProducer>& sink, |
| 44 | const sp<BufferQueue>& bq, |
| 45 | const String8& name) |
| 46 | : ConsumerBase(bq), |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 47 | mHwc(hwc), |
| 48 | mDisplayId(dispId), |
| 49 | mDisplayName(name), |
| 50 | mProducerUsage(GRALLOC_USAGE_HW_COMPOSER), |
| 51 | mProducerSlotSource(0), |
| 52 | mDbgState(DBG_STATE_IDLE), |
| 53 | mDbgLastCompositionType(COMPOSITION_UNKNOWN) |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 54 | { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 55 | mSource[SOURCE_SINK] = sink; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 56 | mSource[SOURCE_SCRATCH] = bq; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 57 | |
| 58 | resetPerFrameState(); |
| 59 | |
| 60 | int sinkWidth, sinkHeight; |
| 61 | mSource[SOURCE_SINK]->query(NATIVE_WINDOW_WIDTH, &sinkWidth); |
| 62 | mSource[SOURCE_SINK]->query(NATIVE_WINDOW_HEIGHT, &sinkHeight); |
| 63 | |
| 64 | ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string()); |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 65 | mConsumer->setConsumerName(ConsumerBase::mName); |
| 66 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER); |
| 67 | mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight); |
| 68 | mConsumer->setDefaultMaxBufferCount(2); |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 69 | } |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 70 | |
| 71 | VirtualDisplaySurface::~VirtualDisplaySurface() { |
| 72 | } |
| 73 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 74 | status_t VirtualDisplaySurface::beginFrame() { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 75 | if (mDisplayId < 0) |
| 76 | return NO_ERROR; |
| 77 | |
| 78 | VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE, |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 79 | "Unexpected beginFrame() in %s state", dbgStateStr()); |
| 80 | mDbgState = DBG_STATE_BEGUN; |
| 81 | |
| 82 | uint32_t transformHint, numPendingBuffers; |
| 83 | mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight, |
| 84 | &transformHint, &numPendingBuffers); |
| 85 | |
| 86 | return refreshOutputBuffer(); |
| 87 | } |
| 88 | |
| 89 | status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) { |
| 90 | if (mDisplayId < 0) |
| 91 | return NO_ERROR; |
| 92 | |
| 93 | VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN, |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 94 | "Unexpected prepareFrame() in %s state", dbgStateStr()); |
| 95 | mDbgState = DBG_STATE_PREPARED; |
| 96 | |
| 97 | mCompositionType = compositionType; |
| 98 | |
| 99 | if (mCompositionType != mDbgLastCompositionType) { |
| 100 | VDS_LOGV("prepareFrame: composition type changed to %s", |
| 101 | dbgCompositionTypeStr(mCompositionType)); |
| 102 | mDbgLastCompositionType = mCompositionType; |
| 103 | } |
| 104 | |
| 105 | return NO_ERROR; |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | status_t VirtualDisplaySurface::compositionComplete() { |
| 109 | return NO_ERROR; |
| 110 | } |
| 111 | |
| 112 | status_t VirtualDisplaySurface::advanceFrame() { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 113 | if (mDisplayId < 0) |
| 114 | return NO_ERROR; |
| 115 | |
| 116 | if (mCompositionType == COMPOSITION_HWC) { |
| 117 | VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, |
| 118 | "Unexpected advanceFrame() in %s state on HWC frame", |
| 119 | dbgStateStr()); |
| 120 | } else { |
| 121 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE, |
| 122 | "Unexpected advanceFrame() in %s state on GLES/MIXED frame", |
| 123 | dbgStateStr()); |
| 124 | } |
| 125 | mDbgState = DBG_STATE_HWC; |
| 126 | |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 127 | if (mCompositionType == COMPOSITION_HWC) { |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 128 | // Use the output buffer for the FB as well, though conceptually the |
| 129 | // FB is unused on this frame. |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 130 | mFbProducerSlot = mOutputProducerSlot; |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 131 | mFbFence = mOutputFence; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (mFbProducerSlot < 0 || mOutputProducerSlot < 0) { |
| 135 | // Last chance bailout if something bad happened earlier. For example, |
| 136 | // in a GLES configuration, if the sink disappears then dequeueBuffer |
| 137 | // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger |
| 138 | // will soldier on. So we end up here without a buffer. There should |
| 139 | // be lots of scary messages in the log just before this. |
| 140 | VDS_LOGE("advanceFrame: no buffer, bailing out"); |
| 141 | return NO_MEMORY; |
| 142 | } |
| 143 | |
| 144 | sp<GraphicBuffer> fbBuffer = mProducerBuffers[mFbProducerSlot]; |
| 145 | sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot]; |
| 146 | VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)", |
| 147 | mFbProducerSlot, fbBuffer.get(), |
| 148 | mOutputProducerSlot, outBuffer.get()); |
| 149 | |
Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 150 | // At this point we know the output buffer acquire fence, |
| 151 | // so update HWC state with it. |
| 152 | mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer); |
| 153 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 154 | return mHwc.fbPost(mDisplayId, mFbFence, fbBuffer); |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 157 | void VirtualDisplaySurface::onFrameCommitted() { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 158 | if (mDisplayId < 0) |
| 159 | return; |
| 160 | |
| 161 | VDS_LOGW_IF(mDbgState != DBG_STATE_HWC, |
| 162 | "Unexpected onFrameCommitted() in %s state", dbgStateStr()); |
| 163 | mDbgState = DBG_STATE_IDLE; |
| 164 | |
| 165 | sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId); |
| 166 | if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) { |
| 167 | // release the scratch buffer back to the pool |
| 168 | Mutex::Autolock lock(mMutex); |
| 169 | int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot); |
| 170 | VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot); |
| 171 | addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence); |
| 172 | releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot], |
| 173 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); |
| 174 | } |
| 175 | |
| 176 | if (mOutputProducerSlot >= 0) { |
| 177 | int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot); |
| 178 | QueueBufferOutput qbo; |
| 179 | sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId); |
| 180 | VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot); |
| 181 | status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot, |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 182 | QueueBufferInput( |
| 183 | systemTime(), false /* isAutoTimestamp */, |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 184 | Rect(mSinkBufferWidth, mSinkBufferHeight), |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 185 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */, |
| 186 | true /* async*/, |
| 187 | outFence), |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 188 | &qbo); |
| 189 | if (result == NO_ERROR) { |
| 190 | updateQueueBufferOutput(qbo); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | resetPerFrameState(); |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void VirtualDisplaySurface::dump(String8& result) const { |
| 198 | } |
| 199 | |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 200 | status_t VirtualDisplaySurface::requestBuffer(int pslot, |
| 201 | sp<GraphicBuffer>* outBuf) { |
| 202 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, |
| 203 | "Unexpected requestBuffer pslot=%d in %s state", |
| 204 | pslot, dbgStateStr()); |
| 205 | |
| 206 | *outBuf = mProducerBuffers[pslot]; |
| 207 | return NO_ERROR; |
| 208 | } |
| 209 | |
| 210 | status_t VirtualDisplaySurface::setBufferCount(int bufferCount) { |
| 211 | return mSource[SOURCE_SINK]->setBufferCount(bufferCount); |
| 212 | } |
| 213 | |
| 214 | status_t VirtualDisplaySurface::dequeueBuffer(Source source, |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 215 | uint32_t format, int* sslot, sp<Fence>* fence) { |
| 216 | // Don't let a slow consumer block us |
| 217 | bool async = (source == SOURCE_SINK); |
| 218 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 219 | status_t result = mSource[source]->dequeueBuffer(sslot, fence, async, |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 220 | mSinkBufferWidth, mSinkBufferHeight, format, mProducerUsage); |
| 221 | if (result < 0) |
| 222 | return result; |
| 223 | int pslot = mapSource2ProducerSlot(source, *sslot); |
| 224 | VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d", |
| 225 | dbgSourceStr(source), *sslot, pslot, result); |
| 226 | uint32_t sourceBit = static_cast<uint32_t>(source) << pslot; |
| 227 | |
| 228 | if ((mProducerSlotSource & (1u << pslot)) != sourceBit) { |
| 229 | // This slot was previously dequeued from the other source; must |
| 230 | // re-request the buffer. |
| 231 | result |= BUFFER_NEEDS_REALLOCATION; |
| 232 | mProducerSlotSource &= ~(1u << pslot); |
| 233 | mProducerSlotSource |= sourceBit; |
| 234 | } |
| 235 | |
| 236 | if (result & RELEASE_ALL_BUFFERS) { |
| 237 | for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 238 | if ((mProducerSlotSource & (1u << i)) == sourceBit) |
| 239 | mProducerBuffers[i].clear(); |
| 240 | } |
| 241 | } |
| 242 | if (result & BUFFER_NEEDS_REALLOCATION) { |
| 243 | mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]); |
| 244 | VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p", |
| 245 | dbgSourceStr(source), pslot, mProducerBuffers[pslot].get()); |
| 246 | } |
| 247 | |
| 248 | return result; |
| 249 | } |
| 250 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 251 | status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async, |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 252 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
| 253 | VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, |
| 254 | "Unexpected dequeueBuffer() in %s state", dbgStateStr()); |
| 255 | mDbgState = DBG_STATE_GLES; |
| 256 | |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 257 | VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)"); |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 258 | VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage); |
| 259 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 260 | status_t result = NO_ERROR; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 261 | mProducerUsage = usage | GRALLOC_USAGE_HW_COMPOSER; |
| 262 | Source source = fbSourceForCompositionType(mCompositionType); |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 263 | |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 264 | if (source == SOURCE_SINK) { |
Mathias Agopian | 6da15f4 | 2013-09-25 20:40:07 -0700 | [diff] [blame] | 265 | |
| 266 | if (mOutputProducerSlot < 0) { |
| 267 | // Last chance bailout if something bad happened earlier. For example, |
| 268 | // in a GLES configuration, if the sink disappears then dequeueBuffer |
| 269 | // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger |
| 270 | // will soldier on. So we end up here without a buffer. There should |
| 271 | // be lots of scary messages in the log just before this. |
| 272 | VDS_LOGE("dequeueBuffer: no buffer, bailing out"); |
| 273 | return NO_MEMORY; |
| 274 | } |
| 275 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 276 | // We already dequeued the output buffer. If the GLES driver wants |
| 277 | // something incompatible, we have to cancel and get a new one. This |
| 278 | // will mean that HWC will see a different output buffer between |
| 279 | // prepare and set, but since we're in GLES-only mode already it |
| 280 | // shouldn't matter. |
| 281 | |
| 282 | const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot]; |
| 283 | if ((mProducerUsage & ~buf->getUsage()) != 0 || |
| 284 | (format != 0 && format != (uint32_t)buf->getPixelFormat()) || |
| 285 | (w != 0 && w != mSinkBufferWidth) || |
| 286 | (h != 0 && h != mSinkBufferHeight)) { |
| 287 | VDS_LOGV("dequeueBuffer: output buffer doesn't satisfy GLES " |
| 288 | "request, getting a new buffer"); |
| 289 | result = refreshOutputBuffer(); |
| 290 | if (result < 0) |
| 291 | return result; |
| 292 | } |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 295 | if (source == SOURCE_SINK) { |
| 296 | *pslot = mOutputProducerSlot; |
| 297 | *fence = mOutputFence; |
| 298 | } else { |
| 299 | int sslot; |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 300 | result = dequeueBuffer(source, format, &sslot, fence); |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 301 | if (result >= 0) { |
| 302 | *pslot = mapSource2ProducerSlot(source, sslot); |
| 303 | } |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 304 | } |
| 305 | return result; |
| 306 | } |
| 307 | |
| 308 | status_t VirtualDisplaySurface::queueBuffer(int pslot, |
| 309 | const QueueBufferInput& input, QueueBufferOutput* output) { |
| 310 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, |
| 311 | "Unexpected queueBuffer(pslot=%d) in %s state", pslot, |
| 312 | dbgStateStr()); |
| 313 | mDbgState = DBG_STATE_GLES_DONE; |
| 314 | |
| 315 | VDS_LOGV("queueBuffer pslot=%d", pslot); |
| 316 | |
| 317 | status_t result; |
| 318 | if (mCompositionType == COMPOSITION_MIXED) { |
| 319 | // Queue the buffer back into the scratch pool |
| 320 | QueueBufferOutput scratchQBO; |
| 321 | int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot); |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 322 | result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO); |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 323 | if (result != NO_ERROR) |
| 324 | return result; |
| 325 | |
| 326 | // Now acquire the buffer from the scratch pool -- should be the same |
| 327 | // slot and fence as we just queued. |
| 328 | Mutex::Autolock lock(mMutex); |
| 329 | BufferQueue::BufferItem item; |
Jesse Hall | bce7611 | 2013-07-16 13:46:20 -0700 | [diff] [blame] | 330 | result = acquireBufferLocked(&item, 0); |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 331 | if (result != NO_ERROR) |
| 332 | return result; |
| 333 | VDS_LOGW_IF(item.mBuf != sslot, |
| 334 | "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d", |
| 335 | item.mBuf, sslot); |
| 336 | mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf); |
| 337 | mFbFence = mSlots[item.mBuf].mFence; |
| 338 | |
| 339 | } else { |
| 340 | LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES, |
| 341 | "Unexpected queueBuffer in state %s for compositionType %s", |
| 342 | dbgStateStr(), dbgCompositionTypeStr(mCompositionType)); |
| 343 | |
| 344 | // Extract the GLES release fence for HWC to acquire |
| 345 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 346 | bool isAutoTimestamp; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 347 | Rect crop; |
| 348 | int scalingMode; |
| 349 | uint32_t transform; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 350 | bool async; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 351 | input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, |
| 352 | &transform, &async, &mFbFence); |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 353 | |
| 354 | mFbProducerSlot = pslot; |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 355 | mOutputFence = mFbFence; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | *output = mQueueBufferOutput; |
| 359 | return NO_ERROR; |
| 360 | } |
| 361 | |
| 362 | void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) { |
| 363 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, |
| 364 | "Unexpected cancelBuffer(pslot=%d) in %s state", pslot, |
| 365 | dbgStateStr()); |
| 366 | VDS_LOGV("cancelBuffer pslot=%d", pslot); |
| 367 | Source source = fbSourceForCompositionType(mCompositionType); |
| 368 | return mSource[source]->cancelBuffer( |
| 369 | mapProducer2SourceSlot(source, pslot), fence); |
| 370 | } |
| 371 | |
| 372 | int VirtualDisplaySurface::query(int what, int* value) { |
| 373 | return mSource[SOURCE_SINK]->query(what, value); |
| 374 | } |
| 375 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 376 | status_t VirtualDisplaySurface::connect(const sp<IBinder>& token, |
| 377 | int api, bool producerControlledByApp, |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 378 | QueueBufferOutput* output) { |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 379 | QueueBufferOutput qbo; |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 380 | status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo); |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 381 | if (result == NO_ERROR) { |
| 382 | updateQueueBufferOutput(qbo); |
| 383 | *output = mQueueBufferOutput; |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | status_t VirtualDisplaySurface::disconnect(int api) { |
| 389 | return mSource[SOURCE_SINK]->disconnect(api); |
| 390 | } |
| 391 | |
| 392 | void VirtualDisplaySurface::updateQueueBufferOutput( |
| 393 | const QueueBufferOutput& qbo) { |
| 394 | uint32_t w, h, transformHint, numPendingBuffers; |
| 395 | qbo.deflate(&w, &h, &transformHint, &numPendingBuffers); |
| 396 | mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers); |
| 397 | } |
| 398 | |
| 399 | void VirtualDisplaySurface::resetPerFrameState() { |
| 400 | mCompositionType = COMPOSITION_UNKNOWN; |
| 401 | mSinkBufferWidth = 0; |
| 402 | mSinkBufferHeight = 0; |
| 403 | mFbFence = Fence::NO_FENCE; |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 404 | mOutputFence = Fence::NO_FENCE; |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 405 | mFbProducerSlot = -1; |
| 406 | mOutputProducerSlot = -1; |
| 407 | } |
| 408 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 409 | status_t VirtualDisplaySurface::refreshOutputBuffer() { |
| 410 | if (mOutputProducerSlot >= 0) { |
| 411 | mSource[SOURCE_SINK]->cancelBuffer( |
| 412 | mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot), |
| 413 | mOutputFence); |
| 414 | } |
| 415 | |
| 416 | int sslot; |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 417 | status_t result = dequeueBuffer(SOURCE_SINK, 0, &sslot, &mOutputFence); |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 418 | if (result < 0) |
| 419 | return result; |
| 420 | mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot); |
| 421 | |
Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 422 | // On GLES-only frames, we don't have the right output buffer acquire fence |
| 423 | // until after GLES calls queueBuffer(). So here we just set the buffer |
| 424 | // (for use in HWC prepare) but not the fence; we'll call this again with |
| 425 | // the proper fence once we have it. |
| 426 | result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE, |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 427 | mProducerBuffers[mOutputProducerSlot]); |
| 428 | |
| 429 | return result; |
| 430 | } |
| 431 | |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 432 | // This slot mapping function is its own inverse, so two copies are unnecessary. |
| 433 | // Both are kept to make the intent clear where the function is called, and for |
| 434 | // the (unlikely) chance that we switch to a different mapping function. |
| 435 | int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) { |
| 436 | if (source == SOURCE_SCRATCH) { |
| 437 | return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1; |
| 438 | } else { |
| 439 | return sslot; |
| 440 | } |
| 441 | } |
| 442 | int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) { |
| 443 | return mapSource2ProducerSlot(source, pslot); |
| 444 | } |
| 445 | |
| 446 | VirtualDisplaySurface::Source |
| 447 | VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) { |
| 448 | return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK; |
| 449 | } |
| 450 | |
| 451 | const char* VirtualDisplaySurface::dbgStateStr() const { |
| 452 | switch (mDbgState) { |
| 453 | case DBG_STATE_IDLE: return "IDLE"; |
| 454 | case DBG_STATE_PREPARED: return "PREPARED"; |
| 455 | case DBG_STATE_GLES: return "GLES"; |
| 456 | case DBG_STATE_GLES_DONE: return "GLES_DONE"; |
| 457 | case DBG_STATE_HWC: return "HWC"; |
| 458 | default: return "INVALID"; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | const char* VirtualDisplaySurface::dbgSourceStr(Source s) { |
| 463 | switch (s) { |
| 464 | case SOURCE_SINK: return "SINK"; |
| 465 | case SOURCE_SCRATCH: return "SCRATCH"; |
| 466 | default: return "INVALID"; |
| 467 | } |
| 468 | } |
| 469 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 470 | // --------------------------------------------------------------------------- |
| 471 | } // namespace android |
| 472 | // --------------------------------------------------------------------------- |