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