blob: 384f7b22c7bf1a3ead4382829845d41c8a05b16d [file] [log] [blame]
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001/*
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 Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Jesse Hall38efe862013-04-06 23:12:29 -070021// #define LOG_NDEBUG 0
Mathias Agopiancb496ac2017-05-22 14:21:00 -070022
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070023#include <cinttypes>
Mathias Agopiancb496ac2017-05-22 14:21:00 -070024
Jim Shargo6ccc5e82024-07-27 03:42:08 +000025#include <com_android_graphics_libgui_flags.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070026#include <ftl/enum.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070027#include <ftl/flags.h>
Dan Stoza11611f92015-03-12 15:12:44 -070028#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080029#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070030#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070031#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070032
Dominik Laskowski2f01d772022-03-23 16:01:29 -070033#include "HWComposer.h"
34#include "SurfaceFlinger.h"
35#include "VirtualDisplaySurface.h"
36
Jesse Hall24cd98e2014-07-13 14:37:16 -070037#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070038 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070039#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070040 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070041#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070042 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall38efe862013-04-06 23:12:29 -070043
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070044#define UNSUPPORTED() \
45 VDS_LOGE("%s: Invalid operation on virtual display", __func__); \
46 return INVALID_OPERATION
47
48namespace android {
Jesse Hall38efe862013-04-06 23:12:29 -070049
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020050VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, VirtualDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -070051 const sp<IGraphicBufferProducer>& sink,
52 const sp<IGraphicBufferProducer>& bqProducer,
53 const sp<IGraphicBufferConsumer>& bqConsumer,
Lloyd Pique30db6402023-06-26 18:56:51 +000054 const std::string& name)
Jim Shargo6ccc5e82024-07-27 03:42:08 +000055#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
56 : ConsumerBase(bqProducer, bqConsumer),
57#else
Dominik Laskowski075d3172018-05-24 15:50:06 -070058 : ConsumerBase(bqConsumer),
Jim Shargo6ccc5e82024-07-27 03:42:08 +000059#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Dominik Laskowski075d3172018-05-24 15:50:06 -070060 mHwc(hwc),
61 mDisplayId(displayId),
62 mDisplayName(name),
63 mSource{},
64 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
65 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
66 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
67 mProducerSlotSource(0),
68 mProducerBuffers(),
KaiChieh Chuangc5222132019-12-20 07:38:19 +080069 mProducerSlotNeedReallocation(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -070070 mQueueBufferOutput(),
71 mSinkBufferWidth(0),
72 mSinkBufferHeight(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -070073 mFbFence(Fence::NO_FENCE),
74 mOutputFence(Fence::NO_FENCE),
75 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
76 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Lloyd Pique30db6402023-06-26 18:56:51 +000077 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070078 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070079 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070080
81 resetPerFrameState();
82
83 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080084 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
85 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070086 mSinkBufferWidth = sinkWidth;
87 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080088
89 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080090 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -080091 // set by the consumer. Otherwise allow gralloc to decide the format based
92 // on usage bits.
93 int sinkUsage;
94 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
95 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
96 int sinkFormat;
97 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
98 mDefaultOutputFormat = sinkFormat;
99 } else {
100 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
101 }
102 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700103
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700104 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700105 mConsumer->setConsumerName(ConsumerBase::mName);
106 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
107 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700108 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000109 IGraphicBufferProducer::QueueBufferOutput output;
110 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Brian Lindahl439afad2022-11-14 11:16:55 -0700111
112 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
113 mHwcBufferIds[i] = UINT64_MAX;
114 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700115}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700116
117VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700118 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700119}
120
Dan Stoza71433162014-02-04 16:22:36 -0800121status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200122 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700123 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700124 }
Jesse Hall38efe862013-04-06 23:12:29 -0700125
Dan Stoza71433162014-02-04 16:22:36 -0800126 mMustRecompose = mustRecompose;
127
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700128 VDS_LOGW_IF(mDebugState != DebugState::Idle, "Unexpected %s in %s state", __func__,
129 ftl::enum_string(mDebugState).c_str());
130 mDebugState = DebugState::Begun;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700131
Jesse Hall028dc8f2013-08-20 16:35:32 -0700132 return refreshOutputBuffer();
133}
134
135status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200136 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700137 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700138 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700139
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700140 VDS_LOGW_IF(mDebugState != DebugState::Begun, "Unexpected %s in %s state", __func__,
141 ftl::enum_string(mDebugState).c_str());
142 mDebugState = DebugState::Prepared;
Jesse Hall38efe862013-04-06 23:12:29 -0700143
144 mCompositionType = compositionType;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700145 if (mForceHwcCopy && mCompositionType == CompositionType::Gpu) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400146 // Some hardware can do RGB->YUV conversion more efficiently in hardware
147 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800148 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400149 // allows the format conversion to happen there, rather than passing RGB
150 // directly to the consumer.
151 //
152 // On the other hand, when the consumer prefers RGB or can consume RGB
153 // inexpensively, this forces an unnecessary copy.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700154 mCompositionType = CompositionType::Mixed;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400155 }
156
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700157 if (mCompositionType != mDebugLastCompositionType) {
158 VDS_LOGV("%s: composition type changed to %s", __func__,
159 toString(mCompositionType).c_str());
160 mDebugLastCompositionType = mCompositionType;
Jesse Hall38efe862013-04-06 23:12:29 -0700161 }
162
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700163 if (mCompositionType != CompositionType::Gpu &&
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800164 (mOutputFormat != mDefaultOutputFormat || mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
165 // We must have just switched from GPU-only to MIXED or HWC
166 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700167 // driver; they may be suboptimal when HWC is writing to the output
168 // buffer. For example, if the output is going to a video encoder, and
169 // HWC can write directly to YUV, some hardware can skip a
170 // memory-to-memory RGB-to-YUV conversion step.
171 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800172 // If we just switched *to* GPU-only mode, we'll change the
173 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700174 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800175 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700176 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
177 refreshOutputBuffer();
178 }
179
Jesse Hall38efe862013-04-06 23:12:29 -0700180 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700181}
182
Alec Mourif97df4d2023-09-06 02:10:05 +0000183status_t VirtualDisplaySurface::advanceFrame(float hdrSdrRatio) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200184 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700185 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700186 }
Jesse Hall38efe862013-04-06 23:12:29 -0700187
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700188 if (mCompositionType == CompositionType::Hwc) {
189 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state on HWC frame",
190 __func__, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700191 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700192 VDS_LOGW_IF(mDebugState != DebugState::GpuDone,
193 "Unexpected %s in %s state on GPU/MIXED frame", __func__,
194 ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700195 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700196 mDebugState = DebugState::Hwc;
Jesse Hall38efe862013-04-06 23:12:29 -0700197
Jesse Hall14e8b012013-11-07 12:28:26 -0800198 if (mOutputProducerSlot < 0 ||
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700199 (mCompositionType != CompositionType::Hwc && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700200 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800201 // in a graphics API configuration, if the sink disappears then dequeueBuffer
202 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700203 // will soldier on. So we end up here without a buffer. There should
204 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700205 VDS_LOGE("%s: no buffer, bailing out", __func__);
Jesse Hall38efe862013-04-06 23:12:29 -0700206 return NO_MEMORY;
207 }
208
Brian Lindahl439afad2022-11-14 11:16:55 -0700209 sp<GraphicBuffer> const& fbBuffer =
210 mFbProducerSlot >= 0 ? mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
211 sp<GraphicBuffer> const& outBuffer = mProducerBuffers[mOutputProducerSlot];
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700212 VDS_LOGV("%s: fb=%d(%p) out=%d(%p)", __func__, mFbProducerSlot, fbBuffer.get(),
213 mOutputProducerSlot, outBuffer.get());
Jesse Hall38efe862013-04-06 23:12:29 -0700214
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200215 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
216 LOG_FATAL_IF(!halDisplayId);
Jesse Hallb716e572013-10-01 17:25:20 -0700217 // At this point we know the output buffer acquire fence,
218 // so update HWC state with it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200219 mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700220
Jesse Hall14e8b012013-11-07 12:28:26 -0800221 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800222 if (fbBuffer != nullptr) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700223 // assume that HWC has previously seen the buffer in this slot
224 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
225 if (fbBuffer->getId() != mHwcBufferIds[mFbProducerSlot]) {
226 mHwcBufferIds[mFbProducerSlot] = fbBuffer->getId();
227 hwcBuffer = fbBuffer; // HWC hasn't previously seen this buffer in this slot
228 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 // TODO: Correctly propagate the dataspace from GL composition
Brian Lindahl439afad2022-11-14 11:16:55 -0700230 result = mHwc.setClientTarget(*halDisplayId, mFbProducerSlot, mFbFence, hwcBuffer,
Alec Mourif97df4d2023-09-06 02:10:05 +0000231 ui::Dataspace::UNKNOWN, hdrSdrRatio);
Jesse Hall14e8b012013-11-07 12:28:26 -0800232 }
233
234 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700235}
236
Jesse Hall851cfe82013-03-20 13:44:00 -0700237void VirtualDisplaySurface::onFrameCommitted() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200238 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
239 if (!halDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700240 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 }
Jesse Hall38efe862013-04-06 23:12:29 -0700242
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700243 VDS_LOGW_IF(mDebugState != DebugState::Hwc, "Unexpected %s in %s state", __func__,
244 ftl::enum_string(mDebugState).c_str());
245 mDebugState = DebugState::Idle;
Jesse Hall38efe862013-04-06 23:12:29 -0700246
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200247 sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700248 if (mCompositionType == CompositionType::Mixed && mFbProducerSlot >= 0) {
Jesse Hall38efe862013-04-06 23:12:29 -0700249 // release the scratch buffer back to the pool
250 Mutex::Autolock lock(mMutex);
251 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700252 VDS_LOGV("%s: release scratch sslot=%d", __func__, sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
254 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800255 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700256 }
257
258 if (mOutputProducerSlot >= 0) {
259 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
260 QueueBufferOutput qbo;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700261 VDS_LOGV("%s: queue sink sslot=%d", __func__, sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800262 if (mMustRecompose) {
263 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
264 QueueBufferInput(
265 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800266 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800267 Rect(mSinkBufferWidth, mSinkBufferHeight),
268 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800270 &qbo);
271 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700272 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800273 }
274 } else {
275 // If the surface hadn't actually been updated, then we only went
276 // through the motions of updating the display to keep our state
277 // machine happy. We cancel the buffer to avoid triggering another
278 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800279 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700280 }
281 }
282
283 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700284}
285
Dan Stoza01049c82014-11-11 10:32:31 -0800286void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700287}
288
Marin Shalamanov045b7002021-01-07 16:56:24 +0100289void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
290 mQueueBufferOutput.width = newSize.width;
291 mQueueBufferOutput.height = newSize.height;
292 mSinkBufferWidth = newSize.width;
293 mSinkBufferHeight = newSize.height;
Michael Lentine47e45402014-07-18 15:34:25 -0700294}
295
Dan Stoza9e56aa02015-11-02 13:00:03 -0800296const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
297 return mFbFence;
298}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800299
Jesse Hall38efe862013-04-06 23:12:29 -0700300status_t VirtualDisplaySurface::requestBuffer(int pslot,
301 sp<GraphicBuffer>* outBuf) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200302 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700303 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700304 }
Michael Lentine47e45402014-07-18 15:34:25 -0700305
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700306 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s pslot=%d in %s state", __func__,
307 pslot, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700308
309 *outBuf = mProducerBuffers[pslot];
310 return NO_ERROR;
311}
312
Pablo Ceballosfa455352015-08-12 17:47:47 -0700313status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
314 int maxDequeuedBuffers) {
315 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
316}
317
318status_t VirtualDisplaySurface::setAsyncMode(bool async) {
319 return mSource[SOURCE_SINK]->setAsyncMode(async);
320}
321
Jesse Hall38efe862013-04-06 23:12:29 -0700322status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700323 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Alan Ding5be08652024-05-24 01:48:07 +0000324 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Jesse Hall8db92552013-08-29 16:03:50 -0700325
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600326 status_t result =
327 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
328 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700329 if (result < 0)
330 return result;
331 int pslot = mapSource2ProducerSlot(source, *sslot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700332 VDS_LOGV("%s(%s): sslot=%d pslot=%d result=%d", __func__, ftl::enum_string(source).c_str(),
333 *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700334 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700335
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800336 // reset producer slot reallocation flag
337 mProducerSlotNeedReallocation &= ~(1ULL << pslot);
338
Dan Stozafebd4f42014-04-09 16:14:51 -0700339 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700340 // This slot was previously dequeued from the other source; must
341 // re-request the buffer.
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800342 mProducerSlotNeedReallocation |= 1ULL << pslot;
343
Dan Stozafebd4f42014-04-09 16:14:51 -0700344 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700345 mProducerSlotSource |= sourceBit;
346 }
347
348 if (result & RELEASE_ALL_BUFFERS) {
349 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700350 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700351 mProducerBuffers[i].clear();
352 }
353 }
354 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700355 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
356 if (result < 0) {
357 mProducerBuffers[pslot].clear();
358 mSource[source]->cancelBuffer(*sslot, *fence);
359 return result;
360 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700361 VDS_LOGV("%s(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64, __func__,
362 ftl::enum_string(source).c_str(), pslot, mProducerBuffers[pslot].get(),
363 mProducerBuffers[pslot]->getPixelFormat(), mProducerBuffers[pslot]->getUsage());
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800364
365 // propagate reallocation to VDS consumer
366 mProducerSlotNeedReallocation |= 1ULL << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700367 }
368
369 return result;
370}
371
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600372status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
373 PixelFormat format, uint64_t usage,
374 uint64_t* outBufferAge,
375 FrameEventHistoryDelta* outTimestamps) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200376 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600377 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
378 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700379 }
Michael Lentine47e45402014-07-18 15:34:25 -0700380
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700381 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state", __func__,
382 ftl::enum_string(mDebugState).c_str());
383 mDebugState = DebugState::Gpu;
Jesse Hall38efe862013-04-06 23:12:29 -0700384
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700385 VDS_LOGV("%s %dx%d fmt=%d usage=%#" PRIx64, __func__, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700386
Jesse Hall028dc8f2013-08-20 16:35:32 -0700387 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700388 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700389
Jesse Hall38efe862013-04-06 23:12:29 -0700390 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700391
392 if (mOutputProducerSlot < 0) {
393 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800394 // in a graphics API configuration, if the sink disappears then dequeueBuffer
395 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700396 // will soldier on. So we end up here without a buffer. There should
397 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700398 VDS_LOGE("%s: no buffer, bailing out", __func__);
Mathias Agopian6da15f42013-09-25 20:40:07 -0700399 return NO_MEMORY;
400 }
401
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800402 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700403 // something incompatible, we have to cancel and get a new one. This
404 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800405 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700406 // shouldn't matter.
407
Jesse Hall1e27ba22013-09-27 09:05:09 -0700408 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700409 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700410 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800411 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700412 (w != 0 && w != mSinkBufferWidth) ||
413 (h != 0 && h != mSinkBufferHeight)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700414 VDS_LOGV("%s: dequeueing new output buffer: "
415 "want %dx%d fmt=%d use=%#" PRIx64 ", "
416 "have %dx%d fmt=%d use=%#" PRIx64,
417 __func__, w, h, format, usage, mSinkBufferWidth, mSinkBufferHeight,
418 buf->getPixelFormat(), buf->getUsage());
Jesse Hall1e27ba22013-09-27 09:05:09 -0700419 mOutputFormat = format;
420 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700421 result = refreshOutputBuffer();
422 if (result < 0)
423 return result;
424 }
Jesse Hall38efe862013-04-06 23:12:29 -0700425 }
426
Jesse Hall028dc8f2013-08-20 16:35:32 -0700427 if (source == SOURCE_SINK) {
428 *pslot = mOutputProducerSlot;
429 *fence = mOutputFence;
430 } else {
431 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700432 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700433 if (result >= 0) {
434 *pslot = mapSource2ProducerSlot(source, sslot);
435 }
Jesse Hall38efe862013-04-06 23:12:29 -0700436 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600437 if (outBufferAge) {
438 *outBufferAge = 0;
439 }
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800440
441 if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
442 result |= BUFFER_NEEDS_REALLOCATION;
443 }
444
Jesse Hall38efe862013-04-06 23:12:29 -0700445 return result;
446}
447
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700448status_t VirtualDisplaySurface::detachBuffer(int) {
449 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800450}
451
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700452status_t VirtualDisplaySurface::detachNextBuffer(sp<GraphicBuffer>*, sp<Fence>*) {
453 UNSUPPORTED();
Dan Stozad9822a32014-03-28 15:25:31 -0700454}
455
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700456status_t VirtualDisplaySurface::attachBuffer(int*, const sp<GraphicBuffer>&) {
457 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458}
459
Jesse Hall38efe862013-04-06 23:12:29 -0700460status_t VirtualDisplaySurface::queueBuffer(int pslot,
461 const QueueBufferInput& input, QueueBufferOutput* output) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200462 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700463 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700464 }
Michael Lentine47e45402014-07-18 15:34:25 -0700465
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700466 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
467 pslot, ftl::enum_string(mDebugState).c_str());
468 mDebugState = DebugState::GpuDone;
Jesse Hall38efe862013-04-06 23:12:29 -0700469
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700470 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700471
472 status_t result;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700473 if (mCompositionType == CompositionType::Mixed) {
Jesse Hall38efe862013-04-06 23:12:29 -0700474 // Queue the buffer back into the scratch pool
475 QueueBufferOutput scratchQBO;
476 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700477 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700478 if (result != NO_ERROR)
479 return result;
480
481 // Now acquire the buffer from the scratch pool -- should be the same
482 // slot and fence as we just queued.
483 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700484 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700485 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700486 if (result != NO_ERROR)
487 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700488 VDS_LOGW_IF(item.mSlot != sslot,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700489 "%s: acquired sslot %d from SCRATCH after queueing sslot %d", __func__,
490 item.mSlot, sslot);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700491 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
492 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700493
494 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700495 LOG_FATAL_IF(mCompositionType != CompositionType::Gpu,
496 "Unexpected %s in state %s for composition type %s", __func__,
497 ftl::enum_string(mDebugState).c_str(), toString(mCompositionType).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700498
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800499 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700500 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700501 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800502 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700503 Rect crop;
504 int scalingMode;
505 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800506 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700507 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700508
509 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700510 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700511 }
512
Brian Anderson3d4039d2016-09-23 16:31:30 -0700513 // This moves the frame timestamps and keeps a copy of all other fields.
514 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700515 return NO_ERROR;
516}
517
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700518status_t VirtualDisplaySurface::cancelBuffer(int pslot,
519 const sp<Fence>& fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200520 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700521 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700522 }
Michael Lentine47e45402014-07-18 15:34:25 -0700523
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700524 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
525 pslot, ftl::enum_string(mDebugState).c_str());
526 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700527 Source source = fbSourceForCompositionType(mCompositionType);
528 return mSource[source]->cancelBuffer(
529 mapProducer2SourceSlot(source, pslot), fence);
530}
531
532int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700533 switch (what) {
534 case NATIVE_WINDOW_WIDTH:
535 *value = mSinkBufferWidth;
536 break;
537 case NATIVE_WINDOW_HEIGHT:
538 *value = mSinkBufferHeight;
539 break;
540 default:
541 return mSource[SOURCE_SINK]->query(what, value);
542 }
543 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700544}
545
Dan Stozaf0eaf252014-03-21 13:05:51 -0700546status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700547 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700548 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700549 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700550 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
551 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700552 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700553 updateQueueBufferOutput(std::move(qbo));
554 // This moves the frame timestamps and keeps a copy of all other fields.
555 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700556 }
557 return result;
558}
559
Robert Carr97b9c862016-09-08 13:54:35 -0700560status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
561 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700562}
563
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700564status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>&) {
565 UNSUPPORTED();
Jesse Hall399184a2014-03-03 15:42:54 -0800566}
567
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700568void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700569 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700570 // TODO: Should we actually allocate buffers for a virtual display?
571}
572
Dan Stoza9de72932015-04-16 17:28:43 -0700573status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
574 return INVALID_OPERATION;
575}
576
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700577status_t VirtualDisplaySurface::setGenerationNumber(uint32_t) {
578 UNSUPPORTED();
Dan Stoza812ed062015-06-02 15:45:22 -0700579}
580
Dan Stozac6f30bd2015-06-08 09:32:50 -0700581String8 VirtualDisplaySurface::getConsumerName() const {
582 return String8("VirtualDisplaySurface");
583}
584
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700585status_t VirtualDisplaySurface::setSharedBufferMode(bool) {
586 UNSUPPORTED();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800587}
588
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700589status_t VirtualDisplaySurface::setAutoRefresh(bool) {
590 UNSUPPORTED();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700591}
592
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700593status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t) {
594 UNSUPPORTED();
Dan Stoza127fc632015-06-30 13:43:32 -0700595}
596
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700597status_t VirtualDisplaySurface::getLastQueuedBuffer(sp<GraphicBuffer>*, sp<Fence>*, float[16]) {
598 UNSUPPORTED();
Dan Stoza50101d02016-04-07 16:53:23 -0700599}
600
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700601status_t VirtualDisplaySurface::getUniqueId(uint64_t*) const {
602 UNSUPPORTED();
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700603}
604
Chia-I Wue2786ea2017-08-07 10:36:08 -0700605status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
606 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
607}
608
Jesse Hall38efe862013-04-06 23:12:29 -0700609void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700610 QueueBufferOutput&& qbo) {
611 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700612 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700613}
614
615void VirtualDisplaySurface::resetPerFrameState() {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700616 mCompositionType = CompositionType::Unknown;
mayank parsharb988f852014-01-10 10:27:45 +0530617 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700618 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700619 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530620 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700621}
622
Jesse Hall028dc8f2013-08-20 16:35:32 -0700623status_t VirtualDisplaySurface::refreshOutputBuffer() {
Alan Ding5be08652024-05-24 01:48:07 +0000624 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Dominik Laskowski075d3172018-05-24 15:50:06 -0700625
Jesse Hall028dc8f2013-08-20 16:35:32 -0700626 if (mOutputProducerSlot >= 0) {
627 mSource[SOURCE_SINK]->cancelBuffer(
628 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
629 mOutputFence);
630 }
631
632 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700633 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
634 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700635 if (result < 0)
636 return result;
637 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
638
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800639 // On GPU-only frames, we don't have the right output buffer acquire fence
640 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700641 // (for use in HWC prepare) but not the fence; we'll call this again with
642 // the proper fence once we have it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200643 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
644 LOG_FATAL_IF(!halDisplayId);
645 result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700646 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700647
648 return result;
649}
650
Jesse Hall38efe862013-04-06 23:12:29 -0700651// This slot mapping function is its own inverse, so two copies are unnecessary.
652// Both are kept to make the intent clear where the function is called, and for
653// the (unlikely) chance that we switch to a different mapping function.
654int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
655 if (source == SOURCE_SCRATCH) {
656 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
657 } else {
658 return sslot;
659 }
660}
661int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
662 return mapSource2ProducerSlot(source, pslot);
663}
664
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700665auto VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) -> Source {
666 return type == CompositionType::Mixed ? SOURCE_SCRATCH : SOURCE_SINK;
Jesse Hall38efe862013-04-06 23:12:29 -0700667}
668
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700669std::string VirtualDisplaySurface::toString(CompositionType type) {
670 using namespace std::literals;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700671 return type == CompositionType::Unknown ? "Unknown"s : ftl::Flags(type).string();
Jesse Hall38efe862013-04-06 23:12:29 -0700672}
673
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700674} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800675
676// TODO(b/129481165): remove the #pragma below and fix conversion issues
677#pragma clang diagnostic pop // ignored "-Wconversion"