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