blob: 7e29bff5b614bca1ae82235378218b3bba60f1ea [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,
Ramakant Singhf7e81912020-04-11 16:02:43 +053054 const std::string& name, bool secure)
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),
Ramakant Singhf7e81912020-04-11 16:02:43 +053077 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv),
78 mSecure(secure),
79 mSinkUsage(0) {
Jesse Hall38efe862013-04-06 23:12:29 -070080 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070081 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070082
83 resetPerFrameState();
84
85 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080086 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
87 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070088 mSinkBufferWidth = sinkWidth;
89 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080090
91 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080092 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -080093 // set by the consumer. Otherwise allow gralloc to decide the format based
94 // on usage bits.
95 int sinkUsage;
96 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
Ramakant Singhf7e81912020-04-11 16:02:43 +053097 mSinkUsage |= (GRALLOC_USAGE_HW_COMPOSER | sinkUsage);
98 setOutputUsage(mSinkUsage);
Jesse Hall497ba0e2013-11-04 16:43:03 -080099 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
100 int sinkFormat;
101 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
102 mDefaultOutputFormat = sinkFormat;
103 } else {
104 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
105 }
106 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700107
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700108 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700109 mConsumer->setConsumerName(ConsumerBase::mName);
110 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
111 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700112 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000113 IGraphicBufferProducer::QueueBufferOutput output;
114 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Brian Lindahl439afad2022-11-14 11:16:55 -0700115
116 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
117 mHwcBufferIds[i] = UINT64_MAX;
118 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700119}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700120
121VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700122 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700123}
124
Dan Stoza71433162014-02-04 16:22:36 -0800125status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200126 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700127 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700128 }
Jesse Hall38efe862013-04-06 23:12:29 -0700129
Dan Stoza71433162014-02-04 16:22:36 -0800130 mMustRecompose = mustRecompose;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530131 //For WFD use cases we must always set the recompose flag in order
132 //to support pause/resume functionality
133 if (mOutputUsage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
134 mMustRecompose = true;
135 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700136 VDS_LOGW_IF(mDebugState != DebugState::Idle, "Unexpected %s in %s state", __func__,
137 ftl::enum_string(mDebugState).c_str());
138 mDebugState = DebugState::Begun;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700139
Jesse Hall028dc8f2013-08-20 16:35:32 -0700140 return refreshOutputBuffer();
141}
142
143status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200144 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700145 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700146 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700147
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700148 VDS_LOGW_IF(mDebugState != DebugState::Begun, "Unexpected %s in %s state", __func__,
149 ftl::enum_string(mDebugState).c_str());
150 mDebugState = DebugState::Prepared;
Jesse Hall38efe862013-04-06 23:12:29 -0700151
152 mCompositionType = compositionType;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700153 if (mForceHwcCopy && mCompositionType == CompositionType::Gpu) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400154 // Some hardware can do RGB->YUV conversion more efficiently in hardware
155 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800156 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400157 // allows the format conversion to happen there, rather than passing RGB
158 // directly to the consumer.
159 //
160 // On the other hand, when the consumer prefers RGB or can consume RGB
161 // inexpensively, this forces an unnecessary copy.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700162 mCompositionType = CompositionType::Mixed;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400163 }
164
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700165 if (mCompositionType != mDebugLastCompositionType) {
166 VDS_LOGV("%s: composition type changed to %s", __func__,
167 toString(mCompositionType).c_str());
168 mDebugLastCompositionType = mCompositionType;
Jesse Hall38efe862013-04-06 23:12:29 -0700169 }
170
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700171 if (mCompositionType != CompositionType::Gpu &&
Ramakant Singhf7e81912020-04-11 16:02:43 +0530172 (mOutputFormat != mDefaultOutputFormat || !(mOutputUsage & GRALLOC_USAGE_HW_COMPOSER))) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800173 // We must have just switched from GPU-only to MIXED or HWC
174 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700175 // driver; they may be suboptimal when HWC is writing to the output
176 // buffer. For example, if the output is going to a video encoder, and
177 // HWC can write directly to YUV, some hardware can skip a
178 // memory-to-memory RGB-to-YUV conversion step.
179 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800180 // If we just switched *to* GPU-only mode, we'll change the
181 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700182 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800183 mOutputFormat = mDefaultOutputFormat;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530184 setOutputUsage(GRALLOC_USAGE_HW_COMPOSER);
Jesse Hall1e27ba22013-09-27 09:05:09 -0700185 refreshOutputBuffer();
186 }
187
Jesse Hall38efe862013-04-06 23:12:29 -0700188 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700189}
190
Alec Mourif97df4d2023-09-06 02:10:05 +0000191status_t VirtualDisplaySurface::advanceFrame(float hdrSdrRatio) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200192 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700193 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700194 }
Jesse Hall38efe862013-04-06 23:12:29 -0700195
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700196 if (mCompositionType == CompositionType::Hwc) {
197 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state on HWC frame",
198 __func__, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700199 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700200 VDS_LOGW_IF(mDebugState != DebugState::GpuDone,
201 "Unexpected %s in %s state on GPU/MIXED frame", __func__,
202 ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700203 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700204 mDebugState = DebugState::Hwc;
Jesse Hall38efe862013-04-06 23:12:29 -0700205
Jesse Hall14e8b012013-11-07 12:28:26 -0800206 if (mOutputProducerSlot < 0 ||
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700207 (mCompositionType != CompositionType::Hwc && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700208 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800209 // in a graphics API configuration, if the sink disappears then dequeueBuffer
210 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700211 // will soldier on. So we end up here without a buffer. There should
212 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700213 VDS_LOGE("%s: no buffer, bailing out", __func__);
Jesse Hall38efe862013-04-06 23:12:29 -0700214 return NO_MEMORY;
215 }
216
Brian Lindahl439afad2022-11-14 11:16:55 -0700217 sp<GraphicBuffer> const& fbBuffer =
218 mFbProducerSlot >= 0 ? mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
219 sp<GraphicBuffer> const& outBuffer = mProducerBuffers[mOutputProducerSlot];
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700220 VDS_LOGV("%s: fb=%d(%p) out=%d(%p)", __func__, mFbProducerSlot, fbBuffer.get(),
221 mOutputProducerSlot, outBuffer.get());
Jesse Hall38efe862013-04-06 23:12:29 -0700222
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200223 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
224 LOG_FATAL_IF(!halDisplayId);
Jesse Hallb716e572013-10-01 17:25:20 -0700225 // At this point we know the output buffer acquire fence,
226 // so update HWC state with it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200227 mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700228
Jesse Hall14e8b012013-11-07 12:28:26 -0800229 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800230 if (fbBuffer != nullptr) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700231 // assume that HWC has previously seen the buffer in this slot
232 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
233 if (fbBuffer->getId() != mHwcBufferIds[mFbProducerSlot]) {
234 mHwcBufferIds[mFbProducerSlot] = fbBuffer->getId();
235 hwcBuffer = fbBuffer; // HWC hasn't previously seen this buffer in this slot
236 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800237 // TODO: Correctly propagate the dataspace from GL composition
Brian Lindahl439afad2022-11-14 11:16:55 -0700238 result = mHwc.setClientTarget(*halDisplayId, mFbProducerSlot, mFbFence, hwcBuffer,
Alec Mourif97df4d2023-09-06 02:10:05 +0000239 ui::Dataspace::UNKNOWN, hdrSdrRatio);
Jesse Hall14e8b012013-11-07 12:28:26 -0800240 }
241
242 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700243}
244
Jesse Hall851cfe82013-03-20 13:44:00 -0700245void VirtualDisplaySurface::onFrameCommitted() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200246 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
247 if (!halDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700248 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249 }
Jesse Hall38efe862013-04-06 23:12:29 -0700250
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700251 VDS_LOGW_IF(mDebugState != DebugState::Hwc, "Unexpected %s in %s state", __func__,
252 ftl::enum_string(mDebugState).c_str());
253 mDebugState = DebugState::Idle;
Jesse Hall38efe862013-04-06 23:12:29 -0700254
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200255 sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700256 if (mCompositionType == CompositionType::Mixed && mFbProducerSlot >= 0) {
Jesse Hall38efe862013-04-06 23:12:29 -0700257 // release the scratch buffer back to the pool
258 Mutex::Autolock lock(mMutex);
259 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700260 VDS_LOGV("%s: release scratch sslot=%d", __func__, sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
262 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800263 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700264 }
265
266 if (mOutputProducerSlot >= 0) {
267 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
268 QueueBufferOutput qbo;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700269 VDS_LOGV("%s: queue sink sslot=%d", __func__, sslot);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530270 if (retireFence->isValid() && mMustRecompose) {
Dan Stoza71433162014-02-04 16:22:36 -0800271 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
272 QueueBufferInput(
273 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800274 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800275 Rect(mSinkBufferWidth, mSinkBufferHeight),
276 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800278 &qbo);
279 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700280 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800281 }
282 } else {
283 // If the surface hadn't actually been updated, then we only went
284 // through the motions of updating the display to keep our state
285 // machine happy. We cancel the buffer to avoid triggering another
286 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800287 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700288 }
289 }
290
291 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700292}
293
Dan Stoza01049c82014-11-11 10:32:31 -0800294void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700295}
296
Marin Shalamanov045b7002021-01-07 16:56:24 +0100297void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
298 mQueueBufferOutput.width = newSize.width;
299 mQueueBufferOutput.height = newSize.height;
300 mSinkBufferWidth = newSize.width;
301 mSinkBufferHeight = newSize.height;
Michael Lentine47e45402014-07-18 15:34:25 -0700302}
303
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
305 return mFbFence;
306}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800307
Jesse Hall38efe862013-04-06 23:12:29 -0700308status_t VirtualDisplaySurface::requestBuffer(int pslot,
309 sp<GraphicBuffer>* outBuf) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200310 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700311 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700312 }
Michael Lentine47e45402014-07-18 15:34:25 -0700313
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700314 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s pslot=%d in %s state", __func__,
315 pslot, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700316
317 *outBuf = mProducerBuffers[pslot];
318 return NO_ERROR;
319}
320
Pablo Ceballosfa455352015-08-12 17:47:47 -0700321status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
322 int maxDequeuedBuffers) {
323 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
324}
325
326status_t VirtualDisplaySurface::setAsyncMode(bool async) {
327 return mSource[SOURCE_SINK]->setAsyncMode(async);
328}
329
Jesse Hall38efe862013-04-06 23:12:29 -0700330status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700331 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Alan Ding5be08652024-05-24 01:48:07 +0000332 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Jesse Hall8db92552013-08-29 16:03:50 -0700333
Ramakant Singhf7e81912020-04-11 16:02:43 +0530334 // Exclude video encoder usage flag from scratch buffer usage flags.
335 if (source == SOURCE_SCRATCH) {
336 usage |= GRALLOC_USAGE_HW_FB;
337 usage &= ~(GRALLOC_USAGE_HW_VIDEO_ENCODER);
Michael Bestasdc203cd2022-02-18 17:19:02 +0200338 VDS_LOGV("%s(%s): updated scratch buffer usage flags=%#" PRIx64,
339 __func__, ftl::enum_string(source).c_str(), usage);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530340 }
341
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600342 status_t result =
343 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
344 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700345 if (result < 0)
346 return result;
347 int pslot = mapSource2ProducerSlot(source, *sslot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700348 VDS_LOGV("%s(%s): sslot=%d pslot=%d result=%d", __func__, ftl::enum_string(source).c_str(),
349 *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700350 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700351
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800352 // reset producer slot reallocation flag
353 mProducerSlotNeedReallocation &= ~(1ULL << pslot);
354
Dan Stozafebd4f42014-04-09 16:14:51 -0700355 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700356 // This slot was previously dequeued from the other source; must
357 // re-request the buffer.
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800358 mProducerSlotNeedReallocation |= 1ULL << pslot;
359
Dan Stozafebd4f42014-04-09 16:14:51 -0700360 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700361 mProducerSlotSource |= sourceBit;
362 }
363
364 if (result & RELEASE_ALL_BUFFERS) {
365 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700366 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700367 mProducerBuffers[i].clear();
368 }
369 }
370 if (result & BUFFER_NEEDS_REALLOCATION) {
Ramakant Singhf7e81912020-04-11 16:02:43 +0530371 auto status = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
372 if (status < 0) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700373 mProducerBuffers[pslot].clear();
374 mSource[source]->cancelBuffer(*sslot, *fence);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530375 return status;
Jesse Hall0b63cd12014-04-29 16:14:14 -0700376 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700377 VDS_LOGV("%s(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64, __func__,
378 ftl::enum_string(source).c_str(), pslot, mProducerBuffers[pslot].get(),
379 mProducerBuffers[pslot]->getPixelFormat(), mProducerBuffers[pslot]->getUsage());
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800380
381 // propagate reallocation to VDS consumer
382 mProducerSlotNeedReallocation |= 1ULL << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700383 }
384
385 return result;
386}
387
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600388status_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) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200392 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600393 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
394 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700395 }
Michael Lentine47e45402014-07-18 15:34:25 -0700396
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700397 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state", __func__,
398 ftl::enum_string(mDebugState).c_str());
399 mDebugState = DebugState::Gpu;
Jesse Hall38efe862013-04-06 23:12:29 -0700400
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700401 VDS_LOGV("%s %dx%d fmt=%d usage=%#" PRIx64, __func__, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700402
Jesse Hall028dc8f2013-08-20 16:35:32 -0700403 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700404 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700405
Jesse Hall38efe862013-04-06 23:12:29 -0700406 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700407
408 if (mOutputProducerSlot < 0) {
409 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800410 // in a graphics API configuration, if the sink disappears then dequeueBuffer
411 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700412 // 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.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700414 VDS_LOGE("%s: no buffer, bailing out", __func__);
Mathias Agopian6da15f42013-09-25 20:40:07 -0700415 return NO_MEMORY;
416 }
417
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800418 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700419 // something incompatible, we have to cancel and get a new one. This
420 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800421 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700422 // shouldn't matter.
423
Jesse Hall1e27ba22013-09-27 09:05:09 -0700424 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700425 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700426 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800427 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700428 (w != 0 && w != mSinkBufferWidth) ||
429 (h != 0 && h != mSinkBufferHeight)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700430 VDS_LOGV("%s: dequeueing new output buffer: "
431 "want %dx%d fmt=%d use=%#" PRIx64 ", "
432 "have %dx%d fmt=%d use=%#" PRIx64,
433 __func__, w, h, format, usage, mSinkBufferWidth, mSinkBufferHeight,
434 buf->getPixelFormat(), buf->getUsage());
Jesse Hall1e27ba22013-09-27 09:05:09 -0700435 mOutputFormat = format;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530436 setOutputUsage(usage);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700437 result = refreshOutputBuffer();
438 if (result < 0)
439 return result;
440 }
Jesse Hall38efe862013-04-06 23:12:29 -0700441 }
442
Jesse Hall028dc8f2013-08-20 16:35:32 -0700443 if (source == SOURCE_SINK) {
444 *pslot = mOutputProducerSlot;
445 *fence = mOutputFence;
446 } else {
447 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700448 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700449 if (result >= 0) {
450 *pslot = mapSource2ProducerSlot(source, sslot);
451 }
Jesse Hall38efe862013-04-06 23:12:29 -0700452 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600453 if (outBufferAge) {
454 *outBufferAge = 0;
455 }
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800456
457 if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
458 result |= BUFFER_NEEDS_REALLOCATION;
459 }
460
Jesse Hall38efe862013-04-06 23:12:29 -0700461 return result;
462}
463
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700464status_t VirtualDisplaySurface::detachBuffer(int) {
465 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800466}
467
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700468status_t VirtualDisplaySurface::detachNextBuffer(sp<GraphicBuffer>*, sp<Fence>*) {
469 UNSUPPORTED();
Dan Stozad9822a32014-03-28 15:25:31 -0700470}
471
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700472status_t VirtualDisplaySurface::attachBuffer(int*, const sp<GraphicBuffer>&) {
473 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800474}
475
Jesse Hall38efe862013-04-06 23:12:29 -0700476status_t VirtualDisplaySurface::queueBuffer(int pslot,
477 const QueueBufferInput& input, QueueBufferOutput* output) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200478 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700479 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700480 }
Michael Lentine47e45402014-07-18 15:34:25 -0700481
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700482 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
483 pslot, ftl::enum_string(mDebugState).c_str());
484 mDebugState = DebugState::GpuDone;
Jesse Hall38efe862013-04-06 23:12:29 -0700485
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700486 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700487
488 status_t result;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700489 if (mCompositionType == CompositionType::Mixed) {
Jesse Hall38efe862013-04-06 23:12:29 -0700490 // Queue the buffer back into the scratch pool
491 QueueBufferOutput scratchQBO;
492 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700493 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700494 if (result != NO_ERROR)
495 return result;
496
497 // Now acquire the buffer from the scratch pool -- should be the same
498 // slot and fence as we just queued.
499 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700500 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700501 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700502 if (result != NO_ERROR)
503 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700504 VDS_LOGW_IF(item.mSlot != sslot,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700505 "%s: acquired sslot %d from SCRATCH after queueing sslot %d", __func__,
506 item.mSlot, sslot);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700507 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
508 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700509
510 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700511 LOG_FATAL_IF(mCompositionType != CompositionType::Gpu,
512 "Unexpected %s in state %s for composition type %s", __func__,
513 ftl::enum_string(mDebugState).c_str(), toString(mCompositionType).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700514
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800515 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700516 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700517 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800518 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700519 Rect crop;
520 int scalingMode;
521 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800522 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700523 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700524
525 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700526 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700527 }
528
Brian Anderson3d4039d2016-09-23 16:31:30 -0700529 // This moves the frame timestamps and keeps a copy of all other fields.
530 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700531 return NO_ERROR;
532}
533
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700534status_t VirtualDisplaySurface::cancelBuffer(int pslot,
535 const sp<Fence>& fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200536 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700537 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700538 }
Michael Lentine47e45402014-07-18 15:34:25 -0700539
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700540 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
541 pslot, ftl::enum_string(mDebugState).c_str());
542 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700543 Source source = fbSourceForCompositionType(mCompositionType);
544 return mSource[source]->cancelBuffer(
545 mapProducer2SourceSlot(source, pslot), fence);
546}
547
548int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700549 switch (what) {
550 case NATIVE_WINDOW_WIDTH:
551 *value = mSinkBufferWidth;
552 break;
553 case NATIVE_WINDOW_HEIGHT:
554 *value = mSinkBufferHeight;
555 break;
556 default:
557 return mSource[SOURCE_SINK]->query(what, value);
558 }
559 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700560}
561
Dan Stozaf0eaf252014-03-21 13:05:51 -0700562status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700563 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700564 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700565 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700566 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
567 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700568 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700569 updateQueueBufferOutput(std::move(qbo));
570 // This moves the frame timestamps and keeps a copy of all other fields.
571 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700572 }
573 return result;
574}
575
Robert Carr97b9c862016-09-08 13:54:35 -0700576status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
577 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700578}
579
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700580status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>&) {
581 UNSUPPORTED();
Jesse Hall399184a2014-03-03 15:42:54 -0800582}
583
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700584void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700585 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700586 // TODO: Should we actually allocate buffers for a virtual display?
587}
588
Dan Stoza9de72932015-04-16 17:28:43 -0700589status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
590 return INVALID_OPERATION;
591}
592
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700593status_t VirtualDisplaySurface::setGenerationNumber(uint32_t) {
594 UNSUPPORTED();
Dan Stoza812ed062015-06-02 15:45:22 -0700595}
596
Dan Stozac6f30bd2015-06-08 09:32:50 -0700597String8 VirtualDisplaySurface::getConsumerName() const {
598 return String8("VirtualDisplaySurface");
599}
600
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700601status_t VirtualDisplaySurface::setSharedBufferMode(bool) {
602 UNSUPPORTED();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800603}
604
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700605status_t VirtualDisplaySurface::setAutoRefresh(bool) {
606 UNSUPPORTED();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700607}
608
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700609status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t) {
610 UNSUPPORTED();
Dan Stoza127fc632015-06-30 13:43:32 -0700611}
612
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700613status_t VirtualDisplaySurface::getLastQueuedBuffer(sp<GraphicBuffer>*, sp<Fence>*, float[16]) {
614 UNSUPPORTED();
Dan Stoza50101d02016-04-07 16:53:23 -0700615}
616
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700617status_t VirtualDisplaySurface::getUniqueId(uint64_t*) const {
618 UNSUPPORTED();
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700619}
620
Chia-I Wue2786ea2017-08-07 10:36:08 -0700621status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
622 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
623}
624
Jesse Hall38efe862013-04-06 23:12:29 -0700625void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700626 QueueBufferOutput&& qbo) {
627 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700628 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700629}
630
631void VirtualDisplaySurface::resetPerFrameState() {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700632 mCompositionType = CompositionType::Unknown;
mayank parsharb988f852014-01-10 10:27:45 +0530633 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700634 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700635 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530636 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700637}
638
Jesse Hall028dc8f2013-08-20 16:35:32 -0700639status_t VirtualDisplaySurface::refreshOutputBuffer() {
Alan Ding5be08652024-05-24 01:48:07 +0000640 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Dominik Laskowski075d3172018-05-24 15:50:06 -0700641
Jesse Hall028dc8f2013-08-20 16:35:32 -0700642 if (mOutputProducerSlot >= 0) {
643 mSource[SOURCE_SINK]->cancelBuffer(
644 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
645 mOutputFence);
646 }
647
648 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700649 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
650 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700651 if (result < 0)
652 return result;
653 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
654
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800655 // On GPU-only frames, we don't have the right output buffer acquire fence
656 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700657 // (for use in HWC prepare) but not the fence; we'll call this again with
658 // the proper fence once we have it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200659 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
660 LOG_FATAL_IF(!halDisplayId);
661 result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700662 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700663
664 return result;
665}
666
Jesse Hall38efe862013-04-06 23:12:29 -0700667// This slot mapping function is its own inverse, so two copies are unnecessary.
668// Both are kept to make the intent clear where the function is called, and for
669// the (unlikely) chance that we switch to a different mapping function.
670int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
671 if (source == SOURCE_SCRATCH) {
672 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
673 } else {
674 return sslot;
675 }
676}
677int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
678 return mapSource2ProducerSlot(source, pslot);
679}
680
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700681auto VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) -> Source {
682 return type == CompositionType::Mixed ? SOURCE_SCRATCH : SOURCE_SINK;
Jesse Hall38efe862013-04-06 23:12:29 -0700683}
684
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700685std::string VirtualDisplaySurface::toString(CompositionType type) {
686 using namespace std::literals;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700687 return type == CompositionType::Unknown ? "Unknown"s : ftl::Flags(type).string();
Jesse Hall38efe862013-04-06 23:12:29 -0700688}
689
Ramakant Singhf7e81912020-04-11 16:02:43 +0530690/* Helper to update the output usage when the display is secure */
691
692void VirtualDisplaySurface::setOutputUsage(uint64_t /*flag*/) {
693
694 mOutputUsage = mSinkUsage;
695 if (mSecure && (mOutputUsage & GRALLOC_USAGE_HW_VIDEO_ENCODER)) {
696 /*TODO: Currently, the framework can only say whether the display
697 * and its subsequent session are secure or not. However, there is
698 * no mechanism to distinguish the different levels of security.
699 * The current solution assumes WV L3 protection.
700 */
701 mOutputUsage |= GRALLOC_USAGE_PROTECTED;
702 }
703}
704
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700705} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800706
707// TODO(b/129481165): remove the #pragma below and fix conversion issues
708#pragma clang diagnostic pop // ignored "-Wconversion"