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