blob: d62075ec65ddccbeb577ea8745c9d0b862f221c6 [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,
Lloyd Pique30db6402023-06-26 18:56:51 +000053 const std::string& name)
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),
Lloyd Pique30db6402023-06-26 18:56:51 +000072 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070073 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070074 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070075
76 resetPerFrameState();
77
78 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080079 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
80 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070081 mSinkBufferWidth = sinkWidth;
82 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080083
84 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080085 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -080086 // set by the consumer. Otherwise allow gralloc to decide the format based
87 // on usage bits.
88 int sinkUsage;
89 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
90 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
91 int sinkFormat;
92 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
93 mDefaultOutputFormat = sinkFormat;
94 } else {
95 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
96 }
97 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -070098
Dominik Laskowskibf170d92018-04-19 15:08:05 -070099 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700100 mConsumer->setConsumerName(ConsumerBase::mName);
101 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
102 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700103 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000104 IGraphicBufferProducer::QueueBufferOutput output;
105 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Brian Lindahl439afad2022-11-14 11:16:55 -0700106
107 for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
108 mHwcBufferIds[i] = UINT64_MAX;
109 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700110}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700111
112VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700113 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700114}
115
Dan Stoza71433162014-02-04 16:22:36 -0800116status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200117 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700118 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700119 }
Jesse Hall38efe862013-04-06 23:12:29 -0700120
Dan Stoza71433162014-02-04 16:22:36 -0800121 mMustRecompose = mustRecompose;
122
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700123 VDS_LOGW_IF(mDebugState != DebugState::Idle, "Unexpected %s in %s state", __func__,
124 ftl::enum_string(mDebugState).c_str());
125 mDebugState = DebugState::Begun;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700126
Jesse Hall028dc8f2013-08-20 16:35:32 -0700127 return refreshOutputBuffer();
128}
129
130status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200131 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700132 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700133 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700134
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700135 VDS_LOGW_IF(mDebugState != DebugState::Begun, "Unexpected %s in %s state", __func__,
136 ftl::enum_string(mDebugState).c_str());
137 mDebugState = DebugState::Prepared;
Jesse Hall38efe862013-04-06 23:12:29 -0700138
139 mCompositionType = compositionType;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700140 if (mForceHwcCopy && mCompositionType == CompositionType::Gpu) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400141 // Some hardware can do RGB->YUV conversion more efficiently in hardware
142 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800143 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400144 // allows the format conversion to happen there, rather than passing RGB
145 // directly to the consumer.
146 //
147 // On the other hand, when the consumer prefers RGB or can consume RGB
148 // inexpensively, this forces an unnecessary copy.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700149 mCompositionType = CompositionType::Mixed;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400150 }
151
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700152 if (mCompositionType != mDebugLastCompositionType) {
153 VDS_LOGV("%s: composition type changed to %s", __func__,
154 toString(mCompositionType).c_str());
155 mDebugLastCompositionType = mCompositionType;
Jesse Hall38efe862013-04-06 23:12:29 -0700156 }
157
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700158 if (mCompositionType != CompositionType::Gpu &&
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800159 (mOutputFormat != mDefaultOutputFormat || mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
160 // We must have just switched from GPU-only to MIXED or HWC
161 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700162 // driver; they may be suboptimal when HWC is writing to the output
163 // buffer. For example, if the output is going to a video encoder, and
164 // HWC can write directly to YUV, some hardware can skip a
165 // memory-to-memory RGB-to-YUV conversion step.
166 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800167 // If we just switched *to* GPU-only mode, we'll change the
168 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700169 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800170 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700171 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
172 refreshOutputBuffer();
173 }
174
Jesse Hall38efe862013-04-06 23:12:29 -0700175 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700176}
177
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700178status_t VirtualDisplaySurface::advanceFrame() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200179 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700180 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700181 }
Jesse Hall38efe862013-04-06 23:12:29 -0700182
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700183 if (mCompositionType == CompositionType::Hwc) {
184 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state on HWC frame",
185 __func__, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700186 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700187 VDS_LOGW_IF(mDebugState != DebugState::GpuDone,
188 "Unexpected %s in %s state on GPU/MIXED frame", __func__,
189 ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700190 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700191 mDebugState = DebugState::Hwc;
Jesse Hall38efe862013-04-06 23:12:29 -0700192
Jesse Hall14e8b012013-11-07 12:28:26 -0800193 if (mOutputProducerSlot < 0 ||
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700194 (mCompositionType != CompositionType::Hwc && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700195 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800196 // in a graphics API configuration, if the sink disappears then dequeueBuffer
197 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700198 // will soldier on. So we end up here without a buffer. There should
199 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700200 VDS_LOGE("%s: no buffer, bailing out", __func__);
Jesse Hall38efe862013-04-06 23:12:29 -0700201 return NO_MEMORY;
202 }
203
Brian Lindahl439afad2022-11-14 11:16:55 -0700204 sp<GraphicBuffer> const& fbBuffer =
205 mFbProducerSlot >= 0 ? mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
206 sp<GraphicBuffer> const& outBuffer = mProducerBuffers[mOutputProducerSlot];
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700207 VDS_LOGV("%s: fb=%d(%p) out=%d(%p)", __func__, mFbProducerSlot, fbBuffer.get(),
208 mOutputProducerSlot, outBuffer.get());
Jesse Hall38efe862013-04-06 23:12:29 -0700209
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200210 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
211 LOG_FATAL_IF(!halDisplayId);
Jesse Hallb716e572013-10-01 17:25:20 -0700212 // At this point we know the output buffer acquire fence,
213 // so update HWC state with it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200214 mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700215
Jesse Hall14e8b012013-11-07 12:28:26 -0800216 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800217 if (fbBuffer != nullptr) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700218 // assume that HWC has previously seen the buffer in this slot
219 sp<GraphicBuffer> hwcBuffer = sp<GraphicBuffer>(nullptr);
220 if (fbBuffer->getId() != mHwcBufferIds[mFbProducerSlot]) {
221 mHwcBufferIds[mFbProducerSlot] = fbBuffer->getId();
222 hwcBuffer = fbBuffer; // HWC hasn't previously seen this buffer in this slot
223 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 // TODO: Correctly propagate the dataspace from GL composition
Brian Lindahl439afad2022-11-14 11:16:55 -0700225 result = mHwc.setClientTarget(*halDisplayId, mFbProducerSlot, mFbFence, hwcBuffer,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700226 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800227 }
228
229 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700230}
231
Jesse Hall851cfe82013-03-20 13:44:00 -0700232void VirtualDisplaySurface::onFrameCommitted() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200233 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
234 if (!halDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700235 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700236 }
Jesse Hall38efe862013-04-06 23:12:29 -0700237
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700238 VDS_LOGW_IF(mDebugState != DebugState::Hwc, "Unexpected %s in %s state", __func__,
239 ftl::enum_string(mDebugState).c_str());
240 mDebugState = DebugState::Idle;
Jesse Hall38efe862013-04-06 23:12:29 -0700241
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200242 sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700243 if (mCompositionType == CompositionType::Mixed && mFbProducerSlot >= 0) {
Jesse Hall38efe862013-04-06 23:12:29 -0700244 // release the scratch buffer back to the pool
245 Mutex::Autolock lock(mMutex);
246 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700247 VDS_LOGV("%s: release scratch sslot=%d", __func__, sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800248 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
249 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800250 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700251 }
252
253 if (mOutputProducerSlot >= 0) {
254 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
255 QueueBufferOutput qbo;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700256 VDS_LOGV("%s: queue sink sslot=%d", __func__, sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800257 if (mMustRecompose) {
258 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
259 QueueBufferInput(
260 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800261 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800262 Rect(mSinkBufferWidth, mSinkBufferHeight),
263 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800265 &qbo);
266 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700267 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800268 }
269 } else {
270 // If the surface hadn't actually been updated, then we only went
271 // through the motions of updating the display to keep our state
272 // machine happy. We cancel the buffer to avoid triggering another
273 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800274 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700275 }
276 }
277
278 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700279}
280
Dan Stoza01049c82014-11-11 10:32:31 -0800281void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700282}
283
Marin Shalamanov045b7002021-01-07 16:56:24 +0100284void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
285 mQueueBufferOutput.width = newSize.width;
286 mQueueBufferOutput.height = newSize.height;
287 mSinkBufferWidth = newSize.width;
288 mSinkBufferHeight = newSize.height;
Michael Lentine47e45402014-07-18 15:34:25 -0700289}
290
Dan Stoza9e56aa02015-11-02 13:00:03 -0800291const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
292 return mFbFence;
293}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800294
Jesse Hall38efe862013-04-06 23:12:29 -0700295status_t VirtualDisplaySurface::requestBuffer(int pslot,
296 sp<GraphicBuffer>* outBuf) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200297 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700298 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700299 }
Michael Lentine47e45402014-07-18 15:34:25 -0700300
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700301 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s pslot=%d in %s state", __func__,
302 pslot, ftl::enum_string(mDebugState).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700303
304 *outBuf = mProducerBuffers[pslot];
305 return NO_ERROR;
306}
307
Pablo Ceballosfa455352015-08-12 17:47:47 -0700308status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
309 int maxDequeuedBuffers) {
310 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
311}
312
313status_t VirtualDisplaySurface::setAsyncMode(bool async) {
314 return mSource[SOURCE_SINK]->setAsyncMode(async);
315}
316
Jesse Hall38efe862013-04-06 23:12:29 -0700317status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700318 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
ning xu952f9952021-10-29 02:44:49 +0000319 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Jesse Hall8db92552013-08-29 16:03:50 -0700320
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600321 status_t result =
322 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
323 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700324 if (result < 0)
325 return result;
326 int pslot = mapSource2ProducerSlot(source, *sslot);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700327 VDS_LOGV("%s(%s): sslot=%d pslot=%d result=%d", __func__, ftl::enum_string(source).c_str(),
328 *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700329 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700330
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800331 // reset producer slot reallocation flag
332 mProducerSlotNeedReallocation &= ~(1ULL << pslot);
333
Dan Stozafebd4f42014-04-09 16:14:51 -0700334 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700335 // This slot was previously dequeued from the other source; must
336 // re-request the buffer.
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800337 mProducerSlotNeedReallocation |= 1ULL << pslot;
338
Dan Stozafebd4f42014-04-09 16:14:51 -0700339 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700340 mProducerSlotSource |= sourceBit;
341 }
342
343 if (result & RELEASE_ALL_BUFFERS) {
344 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700345 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700346 mProducerBuffers[i].clear();
347 }
348 }
349 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700350 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
351 if (result < 0) {
352 mProducerBuffers[pslot].clear();
353 mSource[source]->cancelBuffer(*sslot, *fence);
354 return result;
355 }
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700356 VDS_LOGV("%s(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64, __func__,
357 ftl::enum_string(source).c_str(), pslot, mProducerBuffers[pslot].get(),
358 mProducerBuffers[pslot]->getPixelFormat(), mProducerBuffers[pslot]->getUsage());
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800359
360 // propagate reallocation to VDS consumer
361 mProducerSlotNeedReallocation |= 1ULL << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700362 }
363
364 return result;
365}
366
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600367status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
368 PixelFormat format, uint64_t usage,
369 uint64_t* outBufferAge,
370 FrameEventHistoryDelta* outTimestamps) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200371 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600372 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
373 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700374 }
Michael Lentine47e45402014-07-18 15:34:25 -0700375
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700376 VDS_LOGW_IF(mDebugState != DebugState::Prepared, "Unexpected %s in %s state", __func__,
377 ftl::enum_string(mDebugState).c_str());
378 mDebugState = DebugState::Gpu;
Jesse Hall38efe862013-04-06 23:12:29 -0700379
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700380 VDS_LOGV("%s %dx%d fmt=%d usage=%#" PRIx64, __func__, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700381
Jesse Hall028dc8f2013-08-20 16:35:32 -0700382 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700383 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700384
Jesse Hall38efe862013-04-06 23:12:29 -0700385 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700386
387 if (mOutputProducerSlot < 0) {
388 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800389 // in a graphics API configuration, if the sink disappears then dequeueBuffer
390 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700391 // will soldier on. So we end up here without a buffer. There should
392 // be lots of scary messages in the log just before this.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700393 VDS_LOGE("%s: no buffer, bailing out", __func__);
Mathias Agopian6da15f42013-09-25 20:40:07 -0700394 return NO_MEMORY;
395 }
396
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800397 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 // something incompatible, we have to cancel and get a new one. This
399 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800400 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700401 // shouldn't matter.
402
Jesse Hall1e27ba22013-09-27 09:05:09 -0700403 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700404 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700405 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800406 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700407 (w != 0 && w != mSinkBufferWidth) ||
408 (h != 0 && h != mSinkBufferHeight)) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700409 VDS_LOGV("%s: dequeueing new output buffer: "
410 "want %dx%d fmt=%d use=%#" PRIx64 ", "
411 "have %dx%d fmt=%d use=%#" PRIx64,
412 __func__, w, h, format, usage, mSinkBufferWidth, mSinkBufferHeight,
413 buf->getPixelFormat(), buf->getUsage());
Jesse Hall1e27ba22013-09-27 09:05:09 -0700414 mOutputFormat = format;
415 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700416 result = refreshOutputBuffer();
417 if (result < 0)
418 return result;
419 }
Jesse Hall38efe862013-04-06 23:12:29 -0700420 }
421
Jesse Hall028dc8f2013-08-20 16:35:32 -0700422 if (source == SOURCE_SINK) {
423 *pslot = mOutputProducerSlot;
424 *fence = mOutputFence;
425 } else {
426 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700427 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700428 if (result >= 0) {
429 *pslot = mapSource2ProducerSlot(source, sslot);
430 }
Jesse Hall38efe862013-04-06 23:12:29 -0700431 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600432 if (outBufferAge) {
433 *outBufferAge = 0;
434 }
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800435
436 if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
437 result |= BUFFER_NEEDS_REALLOCATION;
438 }
439
Jesse Hall38efe862013-04-06 23:12:29 -0700440 return result;
441}
442
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700443status_t VirtualDisplaySurface::detachBuffer(int) {
444 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800445}
446
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700447status_t VirtualDisplaySurface::detachNextBuffer(sp<GraphicBuffer>*, sp<Fence>*) {
448 UNSUPPORTED();
Dan Stozad9822a32014-03-28 15:25:31 -0700449}
450
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700451status_t VirtualDisplaySurface::attachBuffer(int*, const sp<GraphicBuffer>&) {
452 UNSUPPORTED();
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453}
454
Jesse Hall38efe862013-04-06 23:12:29 -0700455status_t VirtualDisplaySurface::queueBuffer(int pslot,
456 const QueueBufferInput& input, QueueBufferOutput* output) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200457 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700458 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700459 }
Michael Lentine47e45402014-07-18 15:34:25 -0700460
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700461 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
462 pslot, ftl::enum_string(mDebugState).c_str());
463 mDebugState = DebugState::GpuDone;
Jesse Hall38efe862013-04-06 23:12:29 -0700464
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700465 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700466
467 status_t result;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700468 if (mCompositionType == CompositionType::Mixed) {
Jesse Hall38efe862013-04-06 23:12:29 -0700469 // Queue the buffer back into the scratch pool
470 QueueBufferOutput scratchQBO;
471 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700472 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700473 if (result != NO_ERROR)
474 return result;
475
476 // Now acquire the buffer from the scratch pool -- should be the same
477 // slot and fence as we just queued.
478 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700479 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700480 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700481 if (result != NO_ERROR)
482 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700483 VDS_LOGW_IF(item.mSlot != sslot,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700484 "%s: acquired sslot %d from SCRATCH after queueing sslot %d", __func__,
485 item.mSlot, sslot);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700486 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
487 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700488
489 } else {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700490 LOG_FATAL_IF(mCompositionType != CompositionType::Gpu,
491 "Unexpected %s in state %s for composition type %s", __func__,
492 ftl::enum_string(mDebugState).c_str(), toString(mCompositionType).c_str());
Jesse Hall38efe862013-04-06 23:12:29 -0700493
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800494 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700495 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700496 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800497 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700498 Rect crop;
499 int scalingMode;
500 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800501 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700502 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700503
504 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700505 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700506 }
507
Brian Anderson3d4039d2016-09-23 16:31:30 -0700508 // This moves the frame timestamps and keeps a copy of all other fields.
509 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700510 return NO_ERROR;
511}
512
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700513status_t VirtualDisplaySurface::cancelBuffer(int pslot,
514 const sp<Fence>& fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200515 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700516 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700517 }
Michael Lentine47e45402014-07-18 15:34:25 -0700518
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700519 VDS_LOGW_IF(mDebugState != DebugState::Gpu, "Unexpected %s(pslot=%d) in %s state", __func__,
520 pslot, ftl::enum_string(mDebugState).c_str());
521 VDS_LOGV("%s pslot=%d", __func__, pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700522 Source source = fbSourceForCompositionType(mCompositionType);
523 return mSource[source]->cancelBuffer(
524 mapProducer2SourceSlot(source, pslot), fence);
525}
526
527int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700528 switch (what) {
529 case NATIVE_WINDOW_WIDTH:
530 *value = mSinkBufferWidth;
531 break;
532 case NATIVE_WINDOW_HEIGHT:
533 *value = mSinkBufferHeight;
534 break;
535 default:
536 return mSource[SOURCE_SINK]->query(what, value);
537 }
538 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700539}
540
Dan Stozaf0eaf252014-03-21 13:05:51 -0700541status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700542 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700543 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700544 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700545 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
546 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700547 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700548 updateQueueBufferOutput(std::move(qbo));
549 // This moves the frame timestamps and keeps a copy of all other fields.
550 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700551 }
552 return result;
553}
554
Robert Carr97b9c862016-09-08 13:54:35 -0700555status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
556 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700557}
558
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700559status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>&) {
560 UNSUPPORTED();
Jesse Hall399184a2014-03-03 15:42:54 -0800561}
562
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700563void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700564 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700565 // TODO: Should we actually allocate buffers for a virtual display?
566}
567
Dan Stoza9de72932015-04-16 17:28:43 -0700568status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
569 return INVALID_OPERATION;
570}
571
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700572status_t VirtualDisplaySurface::setGenerationNumber(uint32_t) {
573 UNSUPPORTED();
Dan Stoza812ed062015-06-02 15:45:22 -0700574}
575
Dan Stozac6f30bd2015-06-08 09:32:50 -0700576String8 VirtualDisplaySurface::getConsumerName() const {
577 return String8("VirtualDisplaySurface");
578}
579
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700580status_t VirtualDisplaySurface::setSharedBufferMode(bool) {
581 UNSUPPORTED();
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800582}
583
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700584status_t VirtualDisplaySurface::setAutoRefresh(bool) {
585 UNSUPPORTED();
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700586}
587
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700588status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t) {
589 UNSUPPORTED();
Dan Stoza127fc632015-06-30 13:43:32 -0700590}
591
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700592status_t VirtualDisplaySurface::getLastQueuedBuffer(sp<GraphicBuffer>*, sp<Fence>*, float[16]) {
593 UNSUPPORTED();
Dan Stoza50101d02016-04-07 16:53:23 -0700594}
595
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700596status_t VirtualDisplaySurface::getUniqueId(uint64_t*) const {
597 UNSUPPORTED();
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700598}
599
Chia-I Wue2786ea2017-08-07 10:36:08 -0700600status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
601 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
602}
603
Jesse Hall38efe862013-04-06 23:12:29 -0700604void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700605 QueueBufferOutput&& qbo) {
606 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700607 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700608}
609
610void VirtualDisplaySurface::resetPerFrameState() {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700611 mCompositionType = CompositionType::Unknown;
mayank parsharb988f852014-01-10 10:27:45 +0530612 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700613 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700614 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530615 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700616}
617
Jesse Hall028dc8f2013-08-20 16:35:32 -0700618status_t VirtualDisplaySurface::refreshOutputBuffer() {
ning xu952f9952021-10-29 02:44:49 +0000619 LOG_ALWAYS_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId).has_value());
Dominik Laskowski075d3172018-05-24 15:50:06 -0700620
Jesse Hall028dc8f2013-08-20 16:35:32 -0700621 if (mOutputProducerSlot >= 0) {
622 mSource[SOURCE_SINK]->cancelBuffer(
623 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
624 mOutputFence);
625 }
626
627 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700628 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
629 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700630 if (result < 0)
631 return result;
632 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
633
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800634 // On GPU-only frames, we don't have the right output buffer acquire fence
635 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700636 // (for use in HWC prepare) but not the fence; we'll call this again with
637 // the proper fence once we have it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200638 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
639 LOG_FATAL_IF(!halDisplayId);
640 result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700641 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700642
643 return result;
644}
645
Jesse Hall38efe862013-04-06 23:12:29 -0700646// This slot mapping function is its own inverse, so two copies are unnecessary.
647// Both are kept to make the intent clear where the function is called, and for
648// the (unlikely) chance that we switch to a different mapping function.
649int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
650 if (source == SOURCE_SCRATCH) {
651 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
652 } else {
653 return sslot;
654 }
655}
656int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
657 return mapSource2ProducerSlot(source, pslot);
658}
659
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700660auto VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) -> Source {
661 return type == CompositionType::Mixed ? SOURCE_SCRATCH : SOURCE_SINK;
Jesse Hall38efe862013-04-06 23:12:29 -0700662}
663
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700664std::string VirtualDisplaySurface::toString(CompositionType type) {
665 using namespace std::literals;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700666 return type == CompositionType::Unknown ? "Unknown"s : ftl::Flags(type).string();
Jesse Hall38efe862013-04-06 23:12:29 -0700667}
668
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700669} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800670
671// TODO(b/129481165): remove the #pragma below and fix conversion issues
672#pragma clang diagnostic pop // ignored "-Wconversion"