| 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 |  | 
| Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 21 | #include <gui/BufferItem.h> | 
| Pablo Ceballos | f133d00 | 2015-10-23 12:10:13 -0700 | [diff] [blame] | 22 | #include <gui/IProducerListener.h> | 
| Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 23 |  | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 24 | // --------------------------------------------------------------------------- | 
|  | 25 | namespace android { | 
|  | 26 | // --------------------------------------------------------------------------- | 
|  | 27 |  | 
| Jesse Hall | c354eff | 2013-10-25 10:44:41 -0700 | [diff] [blame] | 28 | #if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS) | 
|  | 29 | static const bool sForceHwcCopy = true; | 
|  | 30 | #else | 
|  | 31 | static const bool sForceHwcCopy = false; | 
|  | 32 | #endif | 
| Naseer Ahmed | 6a96846 | 2013-10-04 16:15:22 -0400 | [diff] [blame] | 33 |  | 
| Jesse Hall | 24cd98e | 2014-07-13 14:37:16 -0700 | [diff] [blame] | 34 | #define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \ | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 35 | mDisplayName.string(), ##__VA_ARGS__) | 
| Jesse Hall | 24cd98e | 2014-07-13 14:37:16 -0700 | [diff] [blame] | 36 | #define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \ | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 37 | mDisplayName.string(), ##__VA_ARGS__) | 
| Jesse Hall | 24cd98e | 2014-07-13 14:37:16 -0700 | [diff] [blame] | 38 | #define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \ | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 39 | mDisplayName.string(), ##__VA_ARGS__) | 
|  | 40 |  | 
|  | 41 | static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) { | 
|  | 42 | switch (type) { | 
|  | 43 | case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN"; | 
|  | 44 | case DisplaySurface::COMPOSITION_GLES:    return "GLES"; | 
|  | 45 | case DisplaySurface::COMPOSITION_HWC:     return "HWC"; | 
|  | 46 | case DisplaySurface::COMPOSITION_MIXED:   return "MIXED"; | 
|  | 47 | default:                                  return "<INVALID>"; | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 |  | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 51 | VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 52 | const sp<IGraphicBufferProducer>& sink, | 
| Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 53 | const sp<IGraphicBufferProducer>& bqProducer, | 
|  | 54 | const sp<IGraphicBufferConsumer>& bqConsumer, | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 55 | const String8& name) | 
| Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 56 | :   ConsumerBase(bqConsumer), | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 57 | mHwc(hwc), | 
|  | 58 | mDisplayId(dispId), | 
|  | 59 | mDisplayName(name), | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 60 | mSource{}, | 
|  | 61 | mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED), | 
|  | 62 | mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED), | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 63 | mOutputUsage(GRALLOC_USAGE_HW_COMPOSER), | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 64 | mProducerSlotSource(0), | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 65 | mProducerBuffers(), | 
|  | 66 | mQueueBufferOutput(), | 
|  | 67 | mSinkBufferWidth(0), | 
|  | 68 | mSinkBufferHeight(0), | 
|  | 69 | mCompositionType(COMPOSITION_UNKNOWN), | 
|  | 70 | mFbFence(Fence::NO_FENCE), | 
|  | 71 | mOutputFence(Fence::NO_FENCE), | 
|  | 72 | mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT), | 
|  | 73 | mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT), | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 74 | mDbgState(DBG_STATE_IDLE), | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 75 | mDbgLastCompositionType(COMPOSITION_UNKNOWN), | 
|  | 76 | mMustRecompose(false) | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 77 | { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 78 | mSource[SOURCE_SINK] = sink; | 
| Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 79 | mSource[SOURCE_SCRATCH] = bqProducer; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | resetPerFrameState(); | 
|  | 82 |  | 
|  | 83 | int sinkWidth, sinkHeight; | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 84 | sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth); | 
|  | 85 | sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight); | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 86 | mSinkBufferWidth = sinkWidth; | 
|  | 87 | mSinkBufferHeight = sinkHeight; | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | // Pick the buffer format to request from the sink when not rendering to it | 
|  | 90 | // with GLES. If the consumer needs CPU access, use the default format | 
|  | 91 | // set by the consumer. Otherwise allow gralloc to decide the format based | 
|  | 92 | // on usage bits. | 
|  | 93 | int sinkUsage; | 
|  | 94 | sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage); | 
|  | 95 | if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { | 
|  | 96 | int sinkFormat; | 
|  | 97 | sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat); | 
|  | 98 | mDefaultOutputFormat = sinkFormat; | 
|  | 99 | } else { | 
|  | 100 | mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; | 
|  | 101 | } | 
|  | 102 | mOutputFormat = mDefaultOutputFormat; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string()); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 105 | mConsumer->setConsumerName(ConsumerBase::mName); | 
|  | 106 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER); | 
|  | 107 | mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight); | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 108 | sink->setAsyncMode(true); | 
| Pablo Ceballos | f133d00 | 2015-10-23 12:10:13 -0700 | [diff] [blame] | 109 | IGraphicBufferProducer::QueueBufferOutput output; | 
|  | 110 | mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output); | 
| Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 111 | } | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 112 |  | 
|  | 113 | VirtualDisplaySurface::~VirtualDisplaySurface() { | 
| Pablo Ceballos | f133d00 | 2015-10-23 12:10:13 -0700 | [diff] [blame] | 114 | mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL); | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 117 | status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 118 | if (mDisplayId < 0) | 
|  | 119 | return NO_ERROR; | 
|  | 120 |  | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 121 | mMustRecompose = mustRecompose; | 
|  | 122 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 123 | VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE, | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 124 | "Unexpected beginFrame() in %s state", dbgStateStr()); | 
|  | 125 | mDbgState = DBG_STATE_BEGUN; | 
|  | 126 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 127 | return refreshOutputBuffer(); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) { | 
|  | 131 | if (mDisplayId < 0) | 
|  | 132 | return NO_ERROR; | 
|  | 133 |  | 
|  | 134 | VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN, | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 135 | "Unexpected prepareFrame() in %s state", dbgStateStr()); | 
|  | 136 | mDbgState = DBG_STATE_PREPARED; | 
|  | 137 |  | 
|  | 138 | mCompositionType = compositionType; | 
| Naseer Ahmed | 6a96846 | 2013-10-04 16:15:22 -0400 | [diff] [blame] | 139 | if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) { | 
|  | 140 | // Some hardware can do RGB->YUV conversion more efficiently in hardware | 
|  | 141 | // controlled by HWC than in hardware controlled by the video encoder. | 
|  | 142 | // Forcing GLES-composed frames to go through an extra copy by the HWC | 
|  | 143 | // allows the format conversion to happen there, rather than passing RGB | 
|  | 144 | // directly to the consumer. | 
|  | 145 | // | 
|  | 146 | // On the other hand, when the consumer prefers RGB or can consume RGB | 
|  | 147 | // inexpensively, this forces an unnecessary copy. | 
|  | 148 | mCompositionType = COMPOSITION_MIXED; | 
|  | 149 | } | 
|  | 150 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 151 | if (mCompositionType != mDbgLastCompositionType) { | 
|  | 152 | VDS_LOGV("prepareFrame: composition type changed to %s", | 
|  | 153 | dbgCompositionTypeStr(mCompositionType)); | 
|  | 154 | mDbgLastCompositionType = mCompositionType; | 
|  | 155 | } | 
|  | 156 |  | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 157 | if (mCompositionType != COMPOSITION_GLES && | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 158 | (mOutputFormat != mDefaultOutputFormat || | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 159 | mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) { | 
|  | 160 | // We must have just switched from GLES-only to MIXED or HWC | 
|  | 161 | // composition. Stop using the format and usage requested by the GLES | 
|  | 162 | // driver; they may be suboptimal when HWC is writing to the output | 
|  | 163 | // buffer. For example, if the output is going to a video encoder, and | 
|  | 164 | // HWC can write directly to YUV, some hardware can skip a | 
|  | 165 | // memory-to-memory RGB-to-YUV conversion step. | 
|  | 166 | // | 
|  | 167 | // If we just switched *to* GLES-only mode, we'll change the | 
|  | 168 | // format/usage and get a new buffer when the GLES driver calls | 
|  | 169 | // dequeueBuffer(). | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 170 | mOutputFormat = mDefaultOutputFormat; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 171 | mOutputUsage = GRALLOC_USAGE_HW_COMPOSER; | 
|  | 172 | refreshOutputBuffer(); | 
|  | 173 | } | 
|  | 174 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 175 | return NO_ERROR; | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 178 | #ifndef USE_HWC2 | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 179 | status_t VirtualDisplaySurface::compositionComplete() { | 
|  | 180 | return NO_ERROR; | 
|  | 181 | } | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 182 | #endif | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 183 |  | 
|  | 184 | status_t VirtualDisplaySurface::advanceFrame() { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 185 | if (mDisplayId < 0) | 
|  | 186 | return NO_ERROR; | 
|  | 187 |  | 
|  | 188 | if (mCompositionType == COMPOSITION_HWC) { | 
|  | 189 | VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, | 
|  | 190 | "Unexpected advanceFrame() in %s state on HWC frame", | 
|  | 191 | dbgStateStr()); | 
|  | 192 | } else { | 
|  | 193 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE, | 
|  | 194 | "Unexpected advanceFrame() in %s state on GLES/MIXED frame", | 
|  | 195 | dbgStateStr()); | 
|  | 196 | } | 
|  | 197 | mDbgState = DBG_STATE_HWC; | 
|  | 198 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 199 | if (mOutputProducerSlot < 0 || | 
|  | 200 | (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 201 | // Last chance bailout if something bad happened earlier. For example, | 
|  | 202 | // in a GLES configuration, if the sink disappears then dequeueBuffer | 
|  | 203 | // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger | 
|  | 204 | // will soldier on. So we end up here without a buffer. There should | 
|  | 205 | // be lots of scary messages in the log just before this. | 
|  | 206 | VDS_LOGE("advanceFrame: no buffer, bailing out"); | 
|  | 207 | return NO_MEMORY; | 
|  | 208 | } | 
|  | 209 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 210 | sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ? | 
|  | 211 | mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 212 | sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot]; | 
|  | 213 | VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)", | 
|  | 214 | mFbProducerSlot, fbBuffer.get(), | 
|  | 215 | mOutputProducerSlot, outBuffer.get()); | 
|  | 216 |  | 
| Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 217 | // At this point we know the output buffer acquire fence, | 
|  | 218 | // so update HWC state with it. | 
|  | 219 | mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer); | 
|  | 220 |  | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 221 | status_t result = NO_ERROR; | 
|  | 222 | if (fbBuffer != NULL) { | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 223 | #ifdef USE_HWC2 | 
|  | 224 | // TODO: Correctly propagate the dataspace from GL composition | 
|  | 225 | result = mHwc.setClientTarget(mDisplayId, mFbFence, fbBuffer, | 
|  | 226 | HAL_DATASPACE_UNKNOWN); | 
|  | 227 | #else | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 228 | result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 229 | #endif | 
| Jesse Hall | 14e8b01 | 2013-11-07 12:28:26 -0800 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
|  | 232 | return result; | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
| Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 235 | void VirtualDisplaySurface::onFrameCommitted() { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 236 | if (mDisplayId < 0) | 
|  | 237 | return; | 
|  | 238 |  | 
|  | 239 | VDS_LOGW_IF(mDbgState != DBG_STATE_HWC, | 
|  | 240 | "Unexpected onFrameCommitted() in %s state", dbgStateStr()); | 
|  | 241 | mDbgState = DBG_STATE_IDLE; | 
|  | 242 |  | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 243 | #ifdef USE_HWC2 | 
|  | 244 | sp<Fence> retireFence = mHwc.getRetireFence(mDisplayId); | 
|  | 245 | #else | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 246 | sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 247 | #endif | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 248 | if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) { | 
|  | 249 | // release the scratch buffer back to the pool | 
|  | 250 | Mutex::Autolock lock(mMutex); | 
|  | 251 | int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot); | 
|  | 252 | VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 253 | #ifdef USE_HWC2 | 
|  | 254 | addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], | 
|  | 255 | retireFence); | 
|  | 256 | #else | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 257 | addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 258 | #endif | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 259 | releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot], | 
|  | 260 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | if (mOutputProducerSlot >= 0) { | 
|  | 264 | int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot); | 
|  | 265 | QueueBufferOutput qbo; | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 266 | #ifndef USE_HWC2 | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 267 | sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 268 | #endif | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 269 | VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot); | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 270 | if (mMustRecompose) { | 
|  | 271 | status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot, | 
|  | 272 | QueueBufferInput( | 
|  | 273 | systemTime(), false /* isAutoTimestamp */, | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 274 | HAL_DATASPACE_UNKNOWN, | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 275 | Rect(mSinkBufferWidth, mSinkBufferHeight), | 
|  | 276 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */, | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 277 | #ifdef USE_HWC2 | 
|  | 278 | retireFence), | 
|  | 279 | #else | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 280 | outFence), | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 281 | #endif | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 282 | &qbo); | 
|  | 283 | if (result == NO_ERROR) { | 
|  | 284 | updateQueueBufferOutput(qbo); | 
|  | 285 | } | 
|  | 286 | } else { | 
|  | 287 | // If the surface hadn't actually been updated, then we only went | 
|  | 288 | // through the motions of updating the display to keep our state | 
|  | 289 | // machine happy. We cancel the buffer to avoid triggering another | 
|  | 290 | // re-composition and causing an infinite loop. | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 291 | #ifdef USE_HWC2 | 
|  | 292 | mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence); | 
|  | 293 | #else | 
| Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 294 | mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence); | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 295 | #endif | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 296 | } | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | resetPerFrameState(); | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 302 | void VirtualDisplaySurface::dumpAsString(String8& /* result */) const { | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 305 | void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) { | 
|  | 306 | uint32_t tmpW, tmpH, transformHint, numPendingBuffers; | 
|  | 307 | mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers); | 
|  | 308 | mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers); | 
|  | 309 |  | 
|  | 310 | mSinkBufferWidth = w; | 
|  | 311 | mSinkBufferHeight = h; | 
|  | 312 | } | 
|  | 313 |  | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 314 | const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const { | 
|  | 315 | return mFbFence; | 
|  | 316 | } | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 317 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 318 | status_t VirtualDisplaySurface::requestBuffer(int pslot, | 
|  | 319 | sp<GraphicBuffer>* outBuf) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 320 | if (mDisplayId < 0) | 
|  | 321 | return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf); | 
|  | 322 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 323 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, | 
|  | 324 | "Unexpected requestBuffer pslot=%d in %s state", | 
|  | 325 | pslot, dbgStateStr()); | 
|  | 326 |  | 
|  | 327 | *outBuf = mProducerBuffers[pslot]; | 
|  | 328 | return NO_ERROR; | 
|  | 329 | } | 
|  | 330 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 331 | status_t VirtualDisplaySurface::setMaxDequeuedBufferCount( | 
|  | 332 | int maxDequeuedBuffers) { | 
|  | 333 | return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | status_t VirtualDisplaySurface::setAsyncMode(bool async) { | 
|  | 337 | return mSource[SOURCE_SINK]->setAsyncMode(async); | 
|  | 338 | } | 
|  | 339 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 340 | status_t VirtualDisplaySurface::dequeueBuffer(Source source, | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 341 | PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 342 | LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId); | 
| Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 343 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 344 | status_t result = mSource[source]->dequeueBuffer(sslot, fence, | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 345 | mSinkBufferWidth, mSinkBufferHeight, format, usage); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 346 | if (result < 0) | 
|  | 347 | return result; | 
|  | 348 | int pslot = mapSource2ProducerSlot(source, *sslot); | 
|  | 349 | VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d", | 
|  | 350 | dbgSourceStr(source), *sslot, pslot, result); | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 351 | uint64_t sourceBit = static_cast<uint64_t>(source) << pslot; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 352 |  | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 353 | if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 354 | // This slot was previously dequeued from the other source; must | 
|  | 355 | // re-request the buffer. | 
|  | 356 | result |= BUFFER_NEEDS_REALLOCATION; | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 357 | mProducerSlotSource &= ~(1ULL << pslot); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 358 | mProducerSlotSource |= sourceBit; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | if (result & RELEASE_ALL_BUFFERS) { | 
|  | 362 | for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 363 | if ((mProducerSlotSource & (1ULL << i)) == sourceBit) | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 364 | mProducerBuffers[i].clear(); | 
|  | 365 | } | 
|  | 366 | } | 
|  | 367 | if (result & BUFFER_NEEDS_REALLOCATION) { | 
| Jesse Hall | 0b63cd1 | 2014-04-29 16:14:14 -0700 | [diff] [blame] | 368 | result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]); | 
|  | 369 | if (result < 0) { | 
|  | 370 | mProducerBuffers[pslot].clear(); | 
|  | 371 | mSource[source]->cancelBuffer(*sslot, *fence); | 
|  | 372 | return result; | 
|  | 373 | } | 
| Jesse Hall | 497ba0e | 2013-11-04 16:43:03 -0800 | [diff] [blame] | 374 | VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x", | 
|  | 375 | dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(), | 
|  | 376 | mProducerBuffers[pslot]->getPixelFormat(), | 
|  | 377 | mProducerBuffers[pslot]->getUsage()); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 378 | } | 
|  | 379 |  | 
|  | 380 | return result; | 
|  | 381 | } | 
|  | 382 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 383 | status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 384 | uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 385 | if (mDisplayId < 0) | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 386 | return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage); | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 387 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 388 | VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED, | 
|  | 389 | "Unexpected dequeueBuffer() in %s state", dbgStateStr()); | 
|  | 390 | mDbgState = DBG_STATE_GLES; | 
|  | 391 |  | 
|  | 392 | VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage); | 
|  | 393 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 394 | status_t result = NO_ERROR; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 395 | Source source = fbSourceForCompositionType(mCompositionType); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 396 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 397 | if (source == SOURCE_SINK) { | 
| Mathias Agopian | 6da15f4 | 2013-09-25 20:40:07 -0700 | [diff] [blame] | 398 |  | 
|  | 399 | if (mOutputProducerSlot < 0) { | 
|  | 400 | // Last chance bailout if something bad happened earlier. For example, | 
|  | 401 | // in a GLES configuration, if the sink disappears then dequeueBuffer | 
|  | 402 | // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger | 
|  | 403 | // will soldier on. So we end up here without a buffer. There should | 
|  | 404 | // be lots of scary messages in the log just before this. | 
|  | 405 | VDS_LOGE("dequeueBuffer: no buffer, bailing out"); | 
|  | 406 | return NO_MEMORY; | 
|  | 407 | } | 
|  | 408 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 409 | // We already dequeued the output buffer. If the GLES driver wants | 
|  | 410 | // something incompatible, we have to cancel and get a new one. This | 
|  | 411 | // will mean that HWC will see a different output buffer between | 
|  | 412 | // prepare and set, but since we're in GLES-only mode already it | 
|  | 413 | // shouldn't matter. | 
|  | 414 |  | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 415 | usage |= GRALLOC_USAGE_HW_COMPOSER; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 416 | const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot]; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 417 | if ((usage & ~buf->getUsage()) != 0 || | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 418 | (format != 0 && format != buf->getPixelFormat()) || | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 419 | (w != 0 && w != mSinkBufferWidth) || | 
|  | 420 | (h != 0 && h != mSinkBufferHeight)) { | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 421 | VDS_LOGV("dequeueBuffer: dequeueing new output buffer: " | 
|  | 422 | "want %dx%d fmt=%d use=%#x, " | 
|  | 423 | "have %dx%d fmt=%d use=%#x", | 
|  | 424 | w, h, format, usage, | 
|  | 425 | mSinkBufferWidth, mSinkBufferHeight, | 
|  | 426 | buf->getPixelFormat(), buf->getUsage()); | 
|  | 427 | mOutputFormat = format; | 
|  | 428 | mOutputUsage = usage; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 429 | result = refreshOutputBuffer(); | 
|  | 430 | if (result < 0) | 
|  | 431 | return result; | 
|  | 432 | } | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 433 | } | 
|  | 434 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 435 | if (source == SOURCE_SINK) { | 
|  | 436 | *pslot = mOutputProducerSlot; | 
|  | 437 | *fence = mOutputFence; | 
|  | 438 | } else { | 
|  | 439 | int sslot; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 440 | result = dequeueBuffer(source, format, usage, &sslot, fence); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 441 | if (result >= 0) { | 
|  | 442 | *pslot = mapSource2ProducerSlot(source, sslot); | 
|  | 443 | } | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 444 | } | 
|  | 445 | return result; | 
|  | 446 | } | 
|  | 447 |  | 
| Dan Stoza | 88a459a | 2014-03-12 09:34:36 -0700 | [diff] [blame] | 448 | status_t VirtualDisplaySurface::detachBuffer(int /* slot */) { | 
|  | 449 | VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface"); | 
|  | 450 | return INVALID_OPERATION; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 451 | } | 
|  | 452 |  | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 453 | status_t VirtualDisplaySurface::detachNextBuffer( | 
|  | 454 | sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) { | 
|  | 455 | VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface"); | 
|  | 456 | return INVALID_OPERATION; | 
|  | 457 | } | 
|  | 458 |  | 
| Dan Stoza | 88a459a | 2014-03-12 09:34:36 -0700 | [diff] [blame] | 459 | status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */, | 
|  | 460 | const sp<GraphicBuffer>& /* buffer */) { | 
|  | 461 | VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface"); | 
|  | 462 | return INVALID_OPERATION; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 463 | } | 
|  | 464 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 465 | status_t VirtualDisplaySurface::queueBuffer(int pslot, | 
|  | 466 | const QueueBufferInput& input, QueueBufferOutput* output) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 467 | if (mDisplayId < 0) | 
|  | 468 | return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output); | 
|  | 469 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 470 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, | 
|  | 471 | "Unexpected queueBuffer(pslot=%d) in %s state", pslot, | 
|  | 472 | dbgStateStr()); | 
|  | 473 | mDbgState = DBG_STATE_GLES_DONE; | 
|  | 474 |  | 
|  | 475 | VDS_LOGV("queueBuffer pslot=%d", pslot); | 
|  | 476 |  | 
|  | 477 | status_t result; | 
|  | 478 | if (mCompositionType == COMPOSITION_MIXED) { | 
|  | 479 | // Queue the buffer back into the scratch pool | 
|  | 480 | QueueBufferOutput scratchQBO; | 
|  | 481 | int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 482 | result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 483 | if (result != NO_ERROR) | 
|  | 484 | return result; | 
|  | 485 |  | 
|  | 486 | // Now acquire the buffer from the scratch pool -- should be the same | 
|  | 487 | // slot and fence as we just queued. | 
|  | 488 | Mutex::Autolock lock(mMutex); | 
| Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 489 | BufferItem item; | 
| Jesse Hall | bce7611 | 2013-07-16 13:46:20 -0700 | [diff] [blame] | 490 | result = acquireBufferLocked(&item, 0); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 491 | if (result != NO_ERROR) | 
|  | 492 | return result; | 
| Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 493 | VDS_LOGW_IF(item.mSlot != sslot, | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 494 | "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d", | 
| Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 495 | item.mSlot, sslot); | 
|  | 496 | mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot); | 
|  | 497 | mFbFence = mSlots[item.mSlot].mFence; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 498 |  | 
|  | 499 | } else { | 
|  | 500 | LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES, | 
|  | 501 | "Unexpected queueBuffer in state %s for compositionType %s", | 
|  | 502 | dbgStateStr(), dbgCompositionTypeStr(mCompositionType)); | 
|  | 503 |  | 
|  | 504 | // Extract the GLES release fence for HWC to acquire | 
|  | 505 | int64_t timestamp; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 506 | bool isAutoTimestamp; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 507 | android_dataspace dataSpace; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 508 | Rect crop; | 
|  | 509 | int scalingMode; | 
|  | 510 | uint32_t transform; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 511 | input.deflate(×tamp, &isAutoTimestamp, &dataSpace, &crop, | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 512 | &scalingMode, &transform, &mFbFence); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 513 |  | 
|  | 514 | mFbProducerSlot = pslot; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 515 | mOutputFence = mFbFence; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
|  | 518 | *output = mQueueBufferOutput; | 
|  | 519 | return NO_ERROR; | 
|  | 520 | } | 
|  | 521 |  | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 522 | status_t VirtualDisplaySurface::cancelBuffer(int pslot, | 
|  | 523 | const sp<Fence>& fence) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 524 | if (mDisplayId < 0) | 
| Michael Lentine | fd9d183 | 2014-07-30 15:39:17 -0700 | [diff] [blame] | 525 | return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence); | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 526 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 527 | VDS_LOGW_IF(mDbgState != DBG_STATE_GLES, | 
|  | 528 | "Unexpected cancelBuffer(pslot=%d) in %s state", pslot, | 
|  | 529 | dbgStateStr()); | 
|  | 530 | VDS_LOGV("cancelBuffer pslot=%d", pslot); | 
|  | 531 | Source source = fbSourceForCompositionType(mCompositionType); | 
|  | 532 | return mSource[source]->cancelBuffer( | 
|  | 533 | mapProducer2SourceSlot(source, pslot), fence); | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | int VirtualDisplaySurface::query(int what, int* value) { | 
| Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 537 | switch (what) { | 
|  | 538 | case NATIVE_WINDOW_WIDTH: | 
|  | 539 | *value = mSinkBufferWidth; | 
|  | 540 | break; | 
|  | 541 | case NATIVE_WINDOW_HEIGHT: | 
|  | 542 | *value = mSinkBufferHeight; | 
|  | 543 | break; | 
|  | 544 | default: | 
|  | 545 | return mSource[SOURCE_SINK]->query(what, value); | 
|  | 546 | } | 
|  | 547 | return NO_ERROR; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 548 | } | 
|  | 549 |  | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 550 | status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener, | 
| Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 551 | int api, bool producerControlledByApp, | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 552 | QueueBufferOutput* output) { | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 553 | QueueBufferOutput qbo; | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 554 | status_t result = mSource[SOURCE_SINK]->connect(listener, api, | 
|  | 555 | producerControlledByApp, &qbo); | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 556 | if (result == NO_ERROR) { | 
|  | 557 | updateQueueBufferOutput(qbo); | 
|  | 558 | *output = mQueueBufferOutput; | 
|  | 559 | } | 
|  | 560 | return result; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | status_t VirtualDisplaySurface::disconnect(int api) { | 
|  | 564 | return mSource[SOURCE_SINK]->disconnect(api); | 
|  | 565 | } | 
|  | 566 |  | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 567 | status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) { | 
|  | 568 | return INVALID_OPERATION; | 
|  | 569 | } | 
|  | 570 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 571 | void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */, | 
|  | 572 | uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) { | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 573 | // TODO: Should we actually allocate buffers for a virtual display? | 
|  | 574 | } | 
|  | 575 |  | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 576 | status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) { | 
|  | 577 | return INVALID_OPERATION; | 
|  | 578 | } | 
|  | 579 |  | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 580 | status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) { | 
|  | 581 | ALOGE("setGenerationNumber not supported on VirtualDisplaySurface"); | 
|  | 582 | return INVALID_OPERATION; | 
|  | 583 | } | 
|  | 584 |  | 
| Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 585 | String8 VirtualDisplaySurface::getConsumerName() const { | 
|  | 586 | return String8("VirtualDisplaySurface"); | 
|  | 587 | } | 
|  | 588 |  | 
| Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 589 | uint64_t VirtualDisplaySurface::getNextFrameNumber() const { | 
|  | 590 | return 0; | 
|  | 591 | } | 
|  | 592 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 593 | status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) { | 
|  | 594 | ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface"); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 595 | return INVALID_OPERATION; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) { | 
|  | 599 | ALOGE("setAutoRefresh not supported on VirtualDisplaySurface"); | 
|  | 600 | return INVALID_OPERATION; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 601 | } | 
|  | 602 |  | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 603 | status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) { | 
|  | 604 | ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface"); | 
|  | 605 | return INVALID_OPERATION; | 
|  | 606 | } | 
|  | 607 |  | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 608 | status_t VirtualDisplaySurface::getLastQueuedBuffer( | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 609 | sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/, | 
|  | 610 | float[16] /* outTransformMatrix*/) { | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 611 | ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface"); | 
|  | 612 | return INVALID_OPERATION; | 
|  | 613 | } | 
|  | 614 |  | 
| Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 615 | status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const { | 
|  | 616 | ALOGE("getUniqueId not supported on VirtualDisplaySurface"); | 
|  | 617 | return INVALID_OPERATION; | 
|  | 618 | } | 
|  | 619 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 620 | void VirtualDisplaySurface::updateQueueBufferOutput( | 
|  | 621 | const QueueBufferOutput& qbo) { | 
|  | 622 | uint32_t w, h, transformHint, numPendingBuffers; | 
|  | 623 | qbo.deflate(&w, &h, &transformHint, &numPendingBuffers); | 
|  | 624 | mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers); | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | void VirtualDisplaySurface::resetPerFrameState() { | 
|  | 628 | mCompositionType = COMPOSITION_UNKNOWN; | 
| mayank parshar | b988f85 | 2014-01-10 10:27:45 +0530 | [diff] [blame] | 629 | mFbFence = Fence::NO_FENCE; | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 630 | mOutputFence = Fence::NO_FENCE; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 631 | mOutputProducerSlot = -1; | 
| mayank parshar | fdfde88 | 2014-01-23 15:08:31 +0530 | [diff] [blame] | 632 | mFbProducerSlot = -1; | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 633 | } | 
|  | 634 |  | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 635 | status_t VirtualDisplaySurface::refreshOutputBuffer() { | 
|  | 636 | if (mOutputProducerSlot >= 0) { | 
|  | 637 | mSource[SOURCE_SINK]->cancelBuffer( | 
|  | 638 | mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot), | 
|  | 639 | mOutputFence); | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | int sslot; | 
| Jesse Hall | 1e27ba2 | 2013-09-27 09:05:09 -0700 | [diff] [blame] | 643 | status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage, | 
|  | 644 | &sslot, &mOutputFence); | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 645 | if (result < 0) | 
|  | 646 | return result; | 
|  | 647 | mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot); | 
|  | 648 |  | 
| Jesse Hall | b716e57 | 2013-10-01 17:25:20 -0700 | [diff] [blame] | 649 | // On GLES-only frames, we don't have the right output buffer acquire fence | 
|  | 650 | // until after GLES calls queueBuffer(). So here we just set the buffer | 
|  | 651 | // (for use in HWC prepare) but not the fence; we'll call this again with | 
|  | 652 | // the proper fence once we have it. | 
|  | 653 | result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE, | 
| Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 654 | mProducerBuffers[mOutputProducerSlot]); | 
|  | 655 |  | 
|  | 656 | return result; | 
|  | 657 | } | 
|  | 658 |  | 
| Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 659 | // This slot mapping function is its own inverse, so two copies are unnecessary. | 
|  | 660 | // Both are kept to make the intent clear where the function is called, and for | 
|  | 661 | // the (unlikely) chance that we switch to a different mapping function. | 
|  | 662 | int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) { | 
|  | 663 | if (source == SOURCE_SCRATCH) { | 
|  | 664 | return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1; | 
|  | 665 | } else { | 
|  | 666 | return sslot; | 
|  | 667 | } | 
|  | 668 | } | 
|  | 669 | int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) { | 
|  | 670 | return mapSource2ProducerSlot(source, pslot); | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | VirtualDisplaySurface::Source | 
|  | 674 | VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) { | 
|  | 675 | return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | const char* VirtualDisplaySurface::dbgStateStr() const { | 
|  | 679 | switch (mDbgState) { | 
|  | 680 | case DBG_STATE_IDLE:      return "IDLE"; | 
|  | 681 | case DBG_STATE_PREPARED:  return "PREPARED"; | 
|  | 682 | case DBG_STATE_GLES:      return "GLES"; | 
|  | 683 | case DBG_STATE_GLES_DONE: return "GLES_DONE"; | 
|  | 684 | case DBG_STATE_HWC:       return "HWC"; | 
|  | 685 | default:                  return "INVALID"; | 
|  | 686 | } | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | const char* VirtualDisplaySurface::dbgSourceStr(Source s) { | 
|  | 690 | switch (s) { | 
|  | 691 | case SOURCE_SINK:    return "SINK"; | 
|  | 692 | case SOURCE_SCRATCH: return "SCRATCH"; | 
|  | 693 | default:             return "INVALID"; | 
|  | 694 | } | 
|  | 695 | } | 
|  | 696 |  | 
| Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 697 | // --------------------------------------------------------------------------- | 
|  | 698 | } // namespace android | 
|  | 699 | // --------------------------------------------------------------------------- |