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