blob: 71e5d1dc81130ce15cf853ee82125422333d0854 [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
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070025#include <ftl/enum.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070026#include <ftl/flags.h>
Dan Stoza11611f92015-03-12 15:12:44 -070027#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080028#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070029#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070030#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070031
Dominik Laskowski2f01d772022-03-23 16:01:29 -070032#include "HWComposer.h"
33#include "SurfaceFlinger.h"
34#include "VirtualDisplaySurface.h"
35
Jesse Hall24cd98e2014-07-13 14:37:16 -070036#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070037 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070038#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070039 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070040#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070041 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall38efe862013-04-06 23:12:29 -070042
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070043#define UNSUPPORTED() \
44 VDS_LOGE("%s: Invalid operation on virtual display", __func__); \
45 return INVALID_OPERATION
46
47namespace android {
Jesse Hall38efe862013-04-06 23:12:29 -070048
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020049VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, VirtualDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -070050 const sp<IGraphicBufferProducer>& sink,
51 const sp<IGraphicBufferProducer>& bqProducer,
52 const sp<IGraphicBufferConsumer>& bqConsumer,
Ramakant Singhf7e81912020-04-11 16:02:43 +053053 const std::string& name, bool secure)
Dominik Laskowski075d3172018-05-24 15:50:06 -070054 : 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 Chuangc5222132019-12-20 07:38:19 +080064 mProducerSlotNeedReallocation(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -070065 mQueueBufferOutput(),
66 mSinkBufferWidth(0),
67 mSinkBufferHeight(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -070068 mFbFence(Fence::NO_FENCE),
69 mOutputFence(Fence::NO_FENCE),
70 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
71 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Ramakant Singhf7e81912020-04-11 16:02:43 +053072 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv),
73 mSecure(secure),
74 mSinkUsage(0) {
Jesse Hall38efe862013-04-06 23:12:29 -070075 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070076 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070077
78 resetPerFrameState();
79
80 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080081 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
82 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070083 mSinkBufferWidth = sinkWidth;
84 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080085
86 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080087 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -080088 // set by the consumer. Otherwise allow gralloc to decide the format based
89 // on usage bits.
90 int sinkUsage;
91 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
Ramakant Singhf7e81912020-04-11 16:02:43 +053092 mSinkUsage |= (GRALLOC_USAGE_HW_COMPOSER | sinkUsage);
93 setOutputUsage(mSinkUsage);
Jesse Hall497ba0e2013-11-04 16:43:03 -080094 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
95 int sinkFormat;
96 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
97 mDefaultOutputFormat = sinkFormat;
98 } else {
99 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
100 }
101 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700102
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700103 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700104 mConsumer->setConsumerName(ConsumerBase::mName);
105 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
106 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700107 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000108 IGraphicBufferProducer::QueueBufferOutput output;
109 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Brian Lindahl439afad2022-11-14 11:16:55 -0700110
111 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
112 mHwcBufferIds[i] = UINT64_MAX;
113 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700114}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700115
116VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700117 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700118}
119
Dan Stoza71433162014-02-04 16:22:36 -0800120status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200121 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700122 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700123 }
Jesse Hall38efe862013-04-06 23:12:29 -0700124
Dan Stoza71433162014-02-04 16:22:36 -0800125 mMustRecompose = mustRecompose;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530126 //For WFD use cases we must always set the recompose flag in order
127 //to support pause/resume functionality
128 if (mOutputUsage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
129 mMustRecompose = true;
130 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700131 VDS_LOGW_IF(mDebugState != DebugState::Idle, "Unexpected %s in %s state", __func__,
132 ftl::enum_string(mDebugState).c_str());
133 mDebugState = DebugState::Begun;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700134
Jesse Hall028dc8f2013-08-20 16:35:32 -0700135 return refreshOutputBuffer();
136}
137
138status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200139 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700140 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700141 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700142
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700143 VDS_LOGW_IF(mDebugState != DebugState::Begun, "Unexpected %s in %s state", __func__,
144 ftl::enum_string(mDebugState).c_str());
145 mDebugState = DebugState::Prepared;
Jesse Hall38efe862013-04-06 23:12:29 -0700146
147 mCompositionType = compositionType;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700148 if (mForceHwcCopy && mCompositionType == CompositionType::Gpu) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400149 // Some hardware can do RGB->YUV conversion more efficiently in hardware
150 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800151 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400152 // allows the format conversion to happen there, rather than passing RGB
153 // directly to the consumer.
154 //
155 // On the other hand, when the consumer prefers RGB or can consume RGB
156 // inexpensively, this forces an unnecessary copy.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700157 mCompositionType = CompositionType::Mixed;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400158 }
159
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700160 if (mCompositionType != mDebugLastCompositionType) {
161 VDS_LOGV("%s: composition type changed to %s", __func__,
162 toString(mCompositionType).c_str());
163 mDebugLastCompositionType = mCompositionType;
Jesse Hall38efe862013-04-06 23:12:29 -0700164 }
165
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700166 if (mCompositionType != CompositionType::Gpu &&
Ramakant Singhf7e81912020-04-11 16:02:43 +0530167 (mOutputFormat != mDefaultOutputFormat || !(mOutputUsage & GRALLOC_USAGE_HW_COMPOSER))) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800168 // We must have just switched from GPU-only to MIXED or HWC
169 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700170 // driver; they may be suboptimal when HWC is writing to the output
171 // buffer. For example, if the output is going to a video encoder, and
172 // HWC can write directly to YUV, some hardware can skip a
173 // memory-to-memory RGB-to-YUV conversion step.
174 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800175 // If we just switched *to* GPU-only mode, we'll change the
176 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700177 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800178 mOutputFormat = mDefaultOutputFormat;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530179 setOutputUsage(GRALLOC_USAGE_HW_COMPOSER);
Jesse Hall1e27ba22013-09-27 09:05:09 -0700180 refreshOutputBuffer();
181 }
182
Jesse Hall38efe862013-04-06 23:12:29 -0700183 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700184}
185
Alec Mourif97df4d2023-09-06 02:10:05 +0000186status_t VirtualDisplaySurface::advanceFrame(float hdrSdrRatio) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200187 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700188 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700189 }
Jesse Hall38efe862013-04-06 23:12:29 -0700190
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700191 if (mCompositionType == CompositionType::Hwc) {
192 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state on HWC frame",
193 __func__, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700194 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700195 VDS_LOGW_IF(mDebugState != DebugState::GpuDone,
196 "Unexpected %s in %s state on GPU/MIXED frame", __func__,
197 ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700198 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700199 mDebugState = DebugState::Hwc;
Jesse Hall38efe862013-04-06 23:12:29 -0700200
Jesse Hall14e8b012013-11-07 12:28:26 -0800201 if (mOutputProducerSlot < 0 ||
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700202 (mCompositionType != CompositionType::Hwc && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700203 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800204 // in a graphics API configuration, if the sink disappears then dequeueBuffer
205 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700206 // will soldier on. So we end up here without a buffer. There should
207 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700208 VDS_LOGE("%s: no buffer, bailing out", __func__);
Jesse Hall38efe862013-04-06 23:12:29 -0700209 return NO_MEMORY;
210 }
211
Brian Lindahl439afad2022-11-14 11:16:55 -0700212 sp<GraphicBuffer> const& fbBuffer =
213 mFbProducerSlot >= 0 ? mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
214 sp<GraphicBuffer> const& outBuffer = mProducerBuffers[mOutputProducerSlot];
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700215 VDS_LOGV("%s: fb=%d(%p) out=%d(%p)", __func__, mFbProducerSlot, fbBuffer.get(),
216 mOutputProducerSlot, outBuffer.get());
Jesse Hall38efe862013-04-06 23:12:29 -0700217
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200218 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
219 LOG_FATAL_IF(!halDisplayId);
Jesse Hallb716e572013-10-01 17:25:20 -0700220 // At this point we know the output buffer acquire fence,
221 // so update HWC state with it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700223
Jesse Hall14e8b012013-11-07 12:28:26 -0800224 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800225 if (fbBuffer != nullptr) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700226 // assume that HWC has previously seen the buffer in this slot
227 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
228 if (fbBuffer->getId() != mHwcBufferIds[mFbProducerSlot]) {
229 mHwcBufferIds[mFbProducerSlot] = fbBuffer->getId();
230 hwcBuffer = fbBuffer; // HWC hasn't previously seen this buffer in this slot
231 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800232 // TODO: Correctly propagate the dataspace from GL composition
Brian Lindahl439afad2022-11-14 11:16:55 -0700233 result = mHwc.setClientTarget(*halDisplayId, mFbProducerSlot, mFbFence, hwcBuffer,
Alec Mourif97df4d2023-09-06 02:10:05 +0000234 ui::Dataspace::UNKNOWN, hdrSdrRatio);
Jesse Hall14e8b012013-11-07 12:28:26 -0800235 }
236
237 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700238}
239
Jesse Hall851cfe82013-03-20 13:44:00 -0700240void VirtualDisplaySurface::onFrameCommitted() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200241 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
242 if (!halDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700243 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700244 }
Jesse Hall38efe862013-04-06 23:12:29 -0700245
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700246 VDS_LOGW_IF(mDebugState != DebugState::Hwc, "Unexpected %s in %s state", __func__,
247 ftl::enum_string(mDebugState).c_str());
248 mDebugState = DebugState::Idle;
Jesse Hall38efe862013-04-06 23:12:29 -0700249
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200250 sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700251 if (mCompositionType == CompositionType::Mixed && mFbProducerSlot >= 0) {
Jesse Hall38efe862013-04-06 23:12:29 -0700252 // release the scratch buffer back to the pool
253 Mutex::Autolock lock(mMutex);
254 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700255 VDS_LOGV("%s: release scratch sslot=%d", __func__, sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
257 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800258 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700259 }
260
261 if (mOutputProducerSlot >= 0) {
262 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
263 QueueBufferOutput qbo;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700264 VDS_LOGV("%s: queue sink sslot=%d", __func__, sslot);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530265 if (retireFence->isValid() && mMustRecompose) {
Dan Stoza71433162014-02-04 16:22:36 -0800266 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
267 QueueBufferInput(
268 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800269 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800270 Rect(mSinkBufferWidth, mSinkBufferHeight),
271 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800273 &qbo);
274 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700275 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800276 }
277 } else {
278 // If the surface hadn't actually been updated, then we only went
279 // through the motions of updating the display to keep our state
280 // machine happy. We cancel the buffer to avoid triggering another
281 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800282 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700283 }
284 }
285
286 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700287}
288
Dan Stoza01049c82014-11-11 10:32:31 -0800289void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700290}
291
Marin Shalamanov045b7002021-01-07 16:56:24 +0100292void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
293 mQueueBufferOutput.width = newSize.width;
294 mQueueBufferOutput.height = newSize.height;
295 mSinkBufferWidth = newSize.width;
296 mSinkBufferHeight = newSize.height;
Michael Lentine47e45402014-07-18 15:34:25 -0700297}
298
Dan Stoza9e56aa02015-11-02 13:00:03 -0800299const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
300 return mFbFence;
301}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800302
Jesse Hall38efe862013-04-06 23:12:29 -0700303status_t VirtualDisplaySurface::requestBuffer(int pslot,
304 sp<GraphicBuffer>* outBuf) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200305 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700306 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700307 }
Michael Lentine47e45402014-07-18 15:34:25 -0700308
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700309 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s pslot=%d in %s state", __func__,
310 pslot, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700311
312 *outBuf = mProducerBuffers[pslot];
313 return NO_ERROR;
314}
315
Pablo Ceballosfa455352015-08-12 17:47:47 -0700316status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
317 int maxDequeuedBuffers) {
318 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
319}
320
321status_t VirtualDisplaySurface::setAsyncMode(bool async) {
322 return mSource[SOURCE_SINK]->setAsyncMode(async);
323}
324
Jesse Hall38efe862013-04-06 23:12:29 -0700325status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700326 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Alan Ding5be08652024-05-24 01:48:07 +0000327 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Jesse Hall8db92552013-08-29 16:03:50 -0700328
Ramakant Singhf7e81912020-04-11 16:02:43 +0530329 // Exclude video encoder usage flag from scratch buffer usage flags.
330 if (source == SOURCE_SCRATCH) {
331 usage |= GRALLOC_USAGE_HW_FB;
332 usage &= ~(GRALLOC_USAGE_HW_VIDEO_ENCODER);
Michael Bestasdc203cd2022-02-18 17:19:02 +0200333 VDS_LOGV("%s(%s): updated scratch buffer usage flags=%#" PRIx64,
334 __func__, ftl::enum_string(source).c_str(), usage);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530335 }
336
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600337 status_t result =
338 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
339 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700340 if (result < 0)
341 return result;
342 int pslot = mapSource2ProducerSlot(source, *sslot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700343 VDS_LOGV("%s(%s): sslot=%d pslot=%d result=%d", __func__, ftl::enum_string(source).c_str(),
344 *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700345 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700346
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800347 // reset producer slot reallocation flag
348 mProducerSlotNeedReallocation &= ~(1ULL << pslot);
349
Dan Stozafebd4f42014-04-09 16:14:51 -0700350 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700351 // This slot was previously dequeued from the other source; must
352 // re-request the buffer.
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800353 mProducerSlotNeedReallocation |= 1ULL << pslot;
354
Dan Stozafebd4f42014-04-09 16:14:51 -0700355 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700356 mProducerSlotSource |= sourceBit;
357 }
358
359 if (result & RELEASE_ALL_BUFFERS) {
360 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700361 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700362 mProducerBuffers[i].clear();
363 }
364 }
365 if (result & BUFFER_NEEDS_REALLOCATION) {
Ramakant Singhf7e81912020-04-11 16:02:43 +0530366 auto status = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
367 if (status < 0) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700368 mProducerBuffers[pslot].clear();
369 mSource[source]->cancelBuffer(*sslot, *fence);
Ramakant Singhf7e81912020-04-11 16:02:43 +0530370 return status;
Jesse Hall0b63cd12014-04-29 16:14:14 -0700371 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700372 VDS_LOGV("%s(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64, __func__,
373 ftl::enum_string(source).c_str(), pslot, mProducerBuffers[pslot].get(),
374 mProducerBuffers[pslot]->getPixelFormat(), mProducerBuffers[pslot]->getUsage());
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800375
376 // propagate reallocation to VDS consumer
377 mProducerSlotNeedReallocation |= 1ULL << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700378 }
379
380 return result;
381}
382
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600383status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
384 PixelFormat format, uint64_t usage,
385 uint64_t* outBufferAge,
386 FrameEventHistoryDelta* outTimestamps) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200387 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600388 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
389 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700390 }
Michael Lentine47e45402014-07-18 15:34:25 -0700391
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700392 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state", __func__,
393 ftl::enum_string(mDebugState).c_str());
394 mDebugState = DebugState::Gpu;
Jesse Hall38efe862013-04-06 23:12:29 -0700395
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700396 VDS_LOGV("%s %dx%d fmt=%d usage=%#" PRIx64, __func__, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700397
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700399 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700400
Jesse Hall38efe862013-04-06 23:12:29 -0700401 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700402
403 if (mOutputProducerSlot < 0) {
404 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800405 // in a graphics API configuration, if the sink disappears then dequeueBuffer
406 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700407 // will soldier on. So we end up here without a buffer. There should
408 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700409 VDS_LOGE("%s: no buffer, bailing out", __func__);
Mathias Agopian6da15f42013-09-25 20:40:07 -0700410 return NO_MEMORY;
411 }
412
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800413 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700414 // something incompatible, we have to cancel and get a new one. This
415 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800416 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700417 // shouldn't matter.
418
Jesse Hall1e27ba22013-09-27 09:05:09 -0700419 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700420 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700421 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800422 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700423 (w != 0 && w != mSinkBufferWidth) ||
424 (h != 0 && h != mSinkBufferHeight)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700425 VDS_LOGV("%s: dequeueing new output buffer: "
426 "want %dx%d fmt=%d use=%#" PRIx64 ", "
427 "have %dx%d fmt=%d use=%#" PRIx64,
428 __func__, w, h, format, usage, mSinkBufferWidth, mSinkBufferHeight,
429 buf->getPixelFormat(), buf->getUsage());
Jesse Hall1e27ba22013-09-27 09:05:09 -0700430 mOutputFormat = format;
Ramakant Singhf7e81912020-04-11 16:02:43 +0530431 setOutputUsage(usage);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700432 result = refreshOutputBuffer();
433 if (result < 0)
434 return result;
435 }
Jesse Hall38efe862013-04-06 23:12:29 -0700436 }
437
Jesse Hall028dc8f2013-08-20 16:35:32 -0700438 if (source == SOURCE_SINK) {
439 *pslot = mOutputProducerSlot;
440 *fence = mOutputFence;
441 } else {
442 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700443 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700444 if (result >= 0) {
445 *pslot = mapSource2ProducerSlot(source, sslot);
446 }
Jesse Hall38efe862013-04-06 23:12:29 -0700447 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600448 if (outBufferAge) {
449 *outBufferAge = 0;
450 }
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800451
452 if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
453 result |= BUFFER_NEEDS_REALLOCATION;
454 }
455
Jesse Hall38efe862013-04-06 23:12:29 -0700456 return result;
457}
458
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700459status_t VirtualDisplaySurface::detachBuffer(int) {
460 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800461}
462
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700463status_t VirtualDisplaySurface::detachNextBuffer(sp<GraphicBuffer>*, sp<Fence>*) {
464 UNSUPPORTED();
Dan Stozad9822a32014-03-28 15:25:31 -0700465}
466
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700467status_t VirtualDisplaySurface::attachBuffer(int*, const sp<GraphicBuffer>&) {
468 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800469}
470
Jesse Hall38efe862013-04-06 23:12:29 -0700471status_t VirtualDisplaySurface::queueBuffer(int pslot,
472 const QueueBufferInput& input, QueueBufferOutput* output) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200473 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700474 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700475 }
Michael Lentine47e45402014-07-18 15:34:25 -0700476
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700477 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
478 pslot, ftl::enum_string(mDebugState).c_str());
479 mDebugState = DebugState::GpuDone;
Jesse Hall38efe862013-04-06 23:12:29 -0700480
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700481 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700482
483 status_t result;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700484 if (mCompositionType == CompositionType::Mixed) {
Jesse Hall38efe862013-04-06 23:12:29 -0700485 // Queue the buffer back into the scratch pool
486 QueueBufferOutput scratchQBO;
487 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700488 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700489 if (result != NO_ERROR)
490 return result;
491
492 // Now acquire the buffer from the scratch pool -- should be the same
493 // slot and fence as we just queued.
494 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700495 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700496 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700497 if (result != NO_ERROR)
498 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700499 VDS_LOGW_IF(item.mSlot != sslot,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700500 "%s: acquired sslot %d from SCRATCH after queueing sslot %d", __func__,
501 item.mSlot, sslot);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700502 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
503 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700504
505 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700506 LOG_FATAL_IF(mCompositionType != CompositionType::Gpu,
507 "Unexpected %s in state %s for composition type %s", __func__,
508 ftl::enum_string(mDebugState).c_str(), toString(mCompositionType).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700509
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800510 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700511 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700512 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800513 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700514 Rect crop;
515 int scalingMode;
516 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800517 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700518 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700519
520 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700521 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700522 }
523
Brian Anderson3d4039d2016-09-23 16:31:30 -0700524 // This moves the frame timestamps and keeps a copy of all other fields.
525 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700526 return NO_ERROR;
527}
528
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700529status_t VirtualDisplaySurface::cancelBuffer(int pslot,
530 const sp<Fence>& fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200531 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700532 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700533 }
Michael Lentine47e45402014-07-18 15:34:25 -0700534
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700535 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
536 pslot, ftl::enum_string(mDebugState).c_str());
537 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700538 Source source = fbSourceForCompositionType(mCompositionType);
539 return mSource[source]->cancelBuffer(
540 mapProducer2SourceSlot(source, pslot), fence);
541}
542
543int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700544 switch (what) {
545 case NATIVE_WINDOW_WIDTH:
546 *value = mSinkBufferWidth;
547 break;
548 case NATIVE_WINDOW_HEIGHT:
549 *value = mSinkBufferHeight;
550 break;
551 default:
552 return mSource[SOURCE_SINK]->query(what, value);
553 }
554 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700555}
556
Dan Stozaf0eaf252014-03-21 13:05:51 -0700557status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700558 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700559 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700560 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700561 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
562 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700563 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700564 updateQueueBufferOutput(std::move(qbo));
565 // This moves the frame timestamps and keeps a copy of all other fields.
566 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700567 }
568 return result;
569}
570
Robert Carr97b9c862016-09-08 13:54:35 -0700571status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
572 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700573}
574
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700575status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>&) {
576 UNSUPPORTED();
Jesse Hall399184a2014-03-03 15:42:54 -0800577}
578
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700579void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700580 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700581 // TODO: Should we actually allocate buffers for a virtual display?
582}
583
Dan Stoza9de72932015-04-16 17:28:43 -0700584status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
585 return INVALID_OPERATION;
586}
587
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700588status_t VirtualDisplaySurface::setGenerationNumber(uint32_t) {
589 UNSUPPORTED();
Dan Stoza812ed062015-06-02 15:45:22 -0700590}
591
Dan Stozac6f30bd2015-06-08 09:32:50 -0700592String8 VirtualDisplaySurface::getConsumerName() const {
593 return String8("VirtualDisplaySurface");
594}
595
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700596status_t VirtualDisplaySurface::setSharedBufferMode(bool) {
597 UNSUPPORTED();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800598}
599
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700600status_t VirtualDisplaySurface::setAutoRefresh(bool) {
601 UNSUPPORTED();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700602}
603
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700604status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t) {
605 UNSUPPORTED();
Dan Stoza127fc632015-06-30 13:43:32 -0700606}
607
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700608status_t VirtualDisplaySurface::getLastQueuedBuffer(sp<GraphicBuffer>*, sp<Fence>*, float[16]) {
609 UNSUPPORTED();
Dan Stoza50101d02016-04-07 16:53:23 -0700610}
611
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700612status_t VirtualDisplaySurface::getUniqueId(uint64_t*) const {
613 UNSUPPORTED();
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700614}
615
Chia-I Wue2786ea2017-08-07 10:36:08 -0700616status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
617 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
618}
619
Jesse Hall38efe862013-04-06 23:12:29 -0700620void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700621 QueueBufferOutput&& qbo) {
622 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700623 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700624}
625
626void VirtualDisplaySurface::resetPerFrameState() {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700627 mCompositionType = CompositionType::Unknown;
mayank parsharb988f852014-01-10 10:27:45 +0530628 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700629 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700630 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530631 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700632}
633
Jesse Hall028dc8f2013-08-20 16:35:32 -0700634status_t VirtualDisplaySurface::refreshOutputBuffer() {
Alan Ding5be08652024-05-24 01:48:07 +0000635 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Dominik Laskowski075d3172018-05-24 15:50:06 -0700636
Jesse Hall028dc8f2013-08-20 16:35:32 -0700637 if (mOutputProducerSlot >= 0) {
638 mSource[SOURCE_SINK]->cancelBuffer(
639 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
640 mOutputFence);
641 }
642
643 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700644 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
645 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700646 if (result < 0)
647 return result;
648 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
649
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800650 // On GPU-only frames, we don't have the right output buffer acquire fence
651 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700652 // (for use in HWC prepare) but not the fence; we'll call this again with
653 // the proper fence once we have it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200654 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
655 LOG_FATAL_IF(!halDisplayId);
656 result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700658
659 return result;
660}
661
Jesse Hall38efe862013-04-06 23:12:29 -0700662// This slot mapping function is its own inverse, so two copies are unnecessary.
663// Both are kept to make the intent clear where the function is called, and for
664// the (unlikely) chance that we switch to a different mapping function.
665int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
666 if (source == SOURCE_SCRATCH) {
667 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
668 } else {
669 return sslot;
670 }
671}
672int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
673 return mapSource2ProducerSlot(source, pslot);
674}
675
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700676auto VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) -> Source {
677 return type == CompositionType::Mixed ? SOURCE_SCRATCH : SOURCE_SINK;
Jesse Hall38efe862013-04-06 23:12:29 -0700678}
679
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700680std::string VirtualDisplaySurface::toString(CompositionType type) {
681 using namespace std::literals;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700682 return type == CompositionType::Unknown ? "Unknown"s : ftl::Flags(type).string();
Jesse Hall38efe862013-04-06 23:12:29 -0700683}
684
Ramakant Singhf7e81912020-04-11 16:02:43 +0530685/* Helper to update the output usage when the display is secure */
686
687void VirtualDisplaySurface::setOutputUsage(uint64_t /*flag*/) {
688
689 mOutputUsage = mSinkUsage;
690 if (mSecure && (mOutputUsage & GRALLOC_USAGE_HW_VIDEO_ENCODER)) {
691 /*TODO: Currently, the framework can only say whether the display
692 * and its subsequent session are secure or not. However, there is
693 * no mechanism to distinguish the different levels of security.
694 * The current solution assumes WV L3 protection.
695 */
696 mOutputUsage |= GRALLOC_USAGE_PROTECTED;
697 }
698}
699
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700700} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800701
702// TODO(b/129481165): remove the #pragma below and fix conversion issues
703#pragma clang diagnostic pop // ignored "-Wconversion"