blob: e26ab117b8143b822feb31eda67662aa913ef03e [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
Jesse Hall99c7dbb2013-03-14 14:29:29 -070022#include "VirtualDisplaySurface.h"
Mathias Agopiancb496ac2017-05-22 14:21:00 -070023
24#include <inttypes.h>
25
Jesse Hall38efe862013-04-06 23:12:29 -070026#include "HWComposer.h"
Fabien Sanglarda34ed632017-03-14 11:43:52 -070027#include "SurfaceFlinger.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070028
Dan Stoza11611f92015-03-12 15:12:44 -070029#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080030#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070031#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070032#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070033
Jesse Hall99c7dbb2013-03-14 14:29:29 -070034// ---------------------------------------------------------------------------
35namespace android {
36// ---------------------------------------------------------------------------
37
Jesse Hall24cd98e2014-07-13 14:37:16 -070038#define VDS_LOGE(msg, ...) ALOGE("[%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_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070041 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070042#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070043 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall38efe862013-04-06 23:12:29 -070044
Lloyd Pique542307f2018-10-19 13:24:08 -070045static const char* dbgCompositionTypeStr(compositionengine::DisplaySurface::CompositionType type) {
Jesse Hall38efe862013-04-06 23:12:29 -070046 switch (type) {
Lloyd Pique542307f2018-10-19 13:24:08 -070047 case compositionengine::DisplaySurface::COMPOSITION_UNKNOWN:
48 return "UNKNOWN";
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080049 case compositionengine::DisplaySurface::COMPOSITION_GPU:
50 return "GPU";
Lloyd Pique542307f2018-10-19 13:24:08 -070051 case compositionengine::DisplaySurface::COMPOSITION_HWC:
52 return "HWC";
53 case compositionengine::DisplaySurface::COMPOSITION_MIXED:
54 return "MIXED";
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080055 default:
56 return "<INVALID>";
Jesse Hall38efe862013-04-06 23:12:29 -070057 }
58}
59
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020060VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, VirtualDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -070061 const sp<IGraphicBufferProducer>& sink,
62 const sp<IGraphicBufferProducer>& bqProducer,
63 const sp<IGraphicBufferConsumer>& bqConsumer,
64 const std::string& name)
65 : ConsumerBase(bqConsumer),
66 mHwc(hwc),
67 mDisplayId(displayId),
68 mDisplayName(name),
69 mSource{},
70 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
71 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
72 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
73 mProducerSlotSource(0),
74 mProducerBuffers(),
KaiChieh Chuangc5222132019-12-20 07:38:19 +080075 mProducerSlotNeedReallocation(0),
Dominik Laskowski075d3172018-05-24 15:50:06 -070076 mQueueBufferOutput(),
77 mSinkBufferWidth(0),
78 mSinkBufferHeight(0),
79 mCompositionType(COMPOSITION_UNKNOWN),
80 mFbFence(Fence::NO_FENCE),
81 mOutputFence(Fence::NO_FENCE),
82 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
83 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
84 mDbgState(DBG_STATE_IDLE),
85 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
86 mMustRecompose(false),
87 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070088 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070089 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070090
91 resetPerFrameState();
92
93 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080094 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
95 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070096 mSinkBufferWidth = sinkWidth;
97 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080098
99 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800100 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -0800101 // set by the consumer. Otherwise allow gralloc to decide the format based
102 // on usage bits.
103 int sinkUsage;
104 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
105 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
106 int sinkFormat;
107 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
108 mDefaultOutputFormat = sinkFormat;
109 } else {
110 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
111 }
112 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700113
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700114 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700115 mConsumer->setConsumerName(ConsumerBase::mName);
116 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
117 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700118 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000119 IGraphicBufferProducer::QueueBufferOutput output;
120 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700121}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700122
123VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700124 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700125}
126
Dan Stoza71433162014-02-04 16:22:36 -0800127status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200128 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700129 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700130 }
Jesse Hall38efe862013-04-06 23:12:29 -0700131
Dan Stoza71433162014-02-04 16:22:36 -0800132 mMustRecompose = mustRecompose;
133
Jesse Hall38efe862013-04-06 23:12:29 -0700134 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700135 "Unexpected beginFrame() in %s state", dbgStateStr());
136 mDbgState = DBG_STATE_BEGUN;
137
Jesse Hall028dc8f2013-08-20 16:35:32 -0700138 return refreshOutputBuffer();
139}
140
141status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200142 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700143 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700144 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700145
146 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700147 "Unexpected prepareFrame() in %s state", dbgStateStr());
148 mDbgState = DBG_STATE_PREPARED;
149
150 mCompositionType = compositionType;
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800151 if (mForceHwcCopy && mCompositionType == COMPOSITION_GPU) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400152 // Some hardware can do RGB->YUV conversion more efficiently in hardware
153 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800154 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400155 // allows the format conversion to happen there, rather than passing RGB
156 // directly to the consumer.
157 //
158 // On the other hand, when the consumer prefers RGB or can consume RGB
159 // inexpensively, this forces an unnecessary copy.
160 mCompositionType = COMPOSITION_MIXED;
161 }
162
Jesse Hall38efe862013-04-06 23:12:29 -0700163 if (mCompositionType != mDbgLastCompositionType) {
164 VDS_LOGV("prepareFrame: composition type changed to %s",
165 dbgCompositionTypeStr(mCompositionType));
166 mDbgLastCompositionType = mCompositionType;
167 }
168
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800169 if (mCompositionType != COMPOSITION_GPU &&
170 (mOutputFormat != mDefaultOutputFormat || mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
171 // We must have just switched from GPU-only to MIXED or HWC
172 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700173 // driver; they may be suboptimal when HWC is writing to the output
174 // buffer. For example, if the output is going to a video encoder, and
175 // HWC can write directly to YUV, some hardware can skip a
176 // memory-to-memory RGB-to-YUV conversion step.
177 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800178 // If we just switched *to* GPU-only mode, we'll change the
179 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700180 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800181 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700182 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
183 refreshOutputBuffer();
184 }
185
Jesse Hall38efe862013-04-06 23:12:29 -0700186 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700187}
188
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700189status_t VirtualDisplaySurface::advanceFrame() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700191 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700192 }
Jesse Hall38efe862013-04-06 23:12:29 -0700193
194 if (mCompositionType == COMPOSITION_HWC) {
195 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
196 "Unexpected advanceFrame() in %s state on HWC frame",
197 dbgStateStr());
198 } else {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800199 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU_DONE,
200 "Unexpected advanceFrame() in %s state on GPU/MIXED frame", dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700201 }
202 mDbgState = DBG_STATE_HWC;
203
Jesse Hall14e8b012013-11-07 12:28:26 -0800204 if (mOutputProducerSlot < 0 ||
205 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700206 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800207 // in a graphics API configuration, if the sink disappears then dequeueBuffer
208 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700209 // will soldier on. So we end up here without a buffer. There should
210 // be lots of scary messages in the log just before this.
211 VDS_LOGE("advanceFrame: no buffer, bailing out");
212 return NO_MEMORY;
213 }
214
Jesse Hall14e8b012013-11-07 12:28:26 -0800215 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800216 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700217 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
218 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
219 mFbProducerSlot, fbBuffer.get(),
220 mOutputProducerSlot, outBuffer.get());
221
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
223 LOG_FATAL_IF(!halDisplayId);
Jesse Hallb716e572013-10-01 17:25:20 -0700224 // At this point we know the output buffer acquire fence,
225 // so update HWC state with it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200226 mHwc.setOutputBuffer(*halDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700227
Jesse Hall14e8b012013-11-07 12:28:26 -0800228 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800229 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800230 uint32_t hwcSlot = 0;
231 sp<GraphicBuffer> hwcBuffer;
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700232 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer, &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800233
Dan Stoza9e56aa02015-11-02 13:00:03 -0800234 // TODO: Correctly propagate the dataspace from GL composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200235 result = mHwc.setClientTarget(*halDisplayId, hwcSlot, mFbFence, hwcBuffer,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700236 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800237 }
238
239 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700240}
241
Jesse Hall851cfe82013-03-20 13:44:00 -0700242void VirtualDisplaySurface::onFrameCommitted() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200243 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
244 if (!halDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700245 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700246 }
Jesse Hall38efe862013-04-06 23:12:29 -0700247
248 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
249 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
250 mDbgState = DBG_STATE_IDLE;
251
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200252 sp<Fence> retireFence = mHwc.getPresentFence(*halDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700253 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
254 // release the scratch buffer back to the pool
255 Mutex::Autolock lock(mMutex);
256 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
257 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
259 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800260 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700261 }
262
263 if (mOutputProducerSlot >= 0) {
264 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
265 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700266 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800267 if (mMustRecompose) {
268 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
269 QueueBufferInput(
270 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800271 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800272 Rect(mSinkBufferWidth, mSinkBufferHeight),
273 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800274 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800275 &qbo);
276 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700277 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800278 }
279 } else {
280 // If the surface hadn't actually been updated, then we only went
281 // through the motions of updating the display to keep our state
282 // machine happy. We cancel the buffer to avoid triggering another
283 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700285 }
286 }
287
288 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700289}
290
Dan Stoza01049c82014-11-11 10:32:31 -0800291void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700292}
293
Marin Shalamanov045b7002021-01-07 16:56:24 +0100294void VirtualDisplaySurface::resizeBuffers(const ui::Size& newSize) {
295 mQueueBufferOutput.width = newSize.width;
296 mQueueBufferOutput.height = newSize.height;
297 mSinkBufferWidth = newSize.width;
298 mSinkBufferHeight = newSize.height;
Michael Lentine47e45402014-07-18 15:34:25 -0700299}
300
Dan Stoza9e56aa02015-11-02 13:00:03 -0800301const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
302 return mFbFence;
303}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304
Jesse Hall38efe862013-04-06 23:12:29 -0700305status_t VirtualDisplaySurface::requestBuffer(int pslot,
306 sp<GraphicBuffer>* outBuf) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200307 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700308 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700309 }
Michael Lentine47e45402014-07-18 15:34:25 -0700310
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800311 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected requestBuffer pslot=%d in %s state", pslot,
312 dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700313
314 *outBuf = mProducerBuffers[pslot];
315 return NO_ERROR;
316}
317
Pablo Ceballosfa455352015-08-12 17:47:47 -0700318status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
319 int maxDequeuedBuffers) {
320 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
321}
322
323status_t VirtualDisplaySurface::setAsyncMode(bool async) {
324 return mSource[SOURCE_SINK]->setAsyncMode(async);
325}
326
Jesse Hall38efe862013-04-06 23:12:29 -0700327status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700328 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200329 LOG_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId));
Jesse Hall8db92552013-08-29 16:03:50 -0700330
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600331 status_t result =
332 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
333 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700334 if (result < 0)
335 return result;
336 int pslot = mapSource2ProducerSlot(source, *sslot);
337 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
338 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700339 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700340
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800341 // reset producer slot reallocation flag
342 mProducerSlotNeedReallocation &= ~(1ULL << pslot);
343
Dan Stozafebd4f42014-04-09 16:14:51 -0700344 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700345 // This slot was previously dequeued from the other source; must
346 // re-request the buffer.
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800347 mProducerSlotNeedReallocation |= 1ULL << pslot;
348
Dan Stozafebd4f42014-04-09 16:14:51 -0700349 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700350 mProducerSlotSource |= sourceBit;
351 }
352
353 if (result & RELEASE_ALL_BUFFERS) {
354 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700355 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700356 mProducerBuffers[i].clear();
357 }
358 }
359 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700360 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
361 if (result < 0) {
362 mProducerBuffers[pslot].clear();
363 mSource[source]->cancelBuffer(*sslot, *fence);
364 return result;
365 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700366 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800367 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
368 mProducerBuffers[pslot]->getPixelFormat(),
369 mProducerBuffers[pslot]->getUsage());
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800370
371 // propagate reallocation to VDS consumer
372 mProducerSlotNeedReallocation |= 1ULL << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700373 }
374
375 return result;
376}
377
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600378status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
379 PixelFormat format, uint64_t usage,
380 uint64_t* outBufferAge,
381 FrameEventHistoryDelta* outTimestamps) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200382 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600383 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
384 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700385 }
Michael Lentine47e45402014-07-18 15:34:25 -0700386
Jesse Hall38efe862013-04-06 23:12:29 -0700387 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
388 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800389 mDbgState = DBG_STATE_GPU;
Jesse Hall38efe862013-04-06 23:12:29 -0700390
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700391 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700392
Jesse Hall028dc8f2013-08-20 16:35:32 -0700393 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700394 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700395
Jesse Hall38efe862013-04-06 23:12:29 -0700396 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700397
398 if (mOutputProducerSlot < 0) {
399 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800400 // in a graphics API configuration, if the sink disappears then dequeueBuffer
401 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700402 // will soldier on. So we end up here without a buffer. There should
403 // be lots of scary messages in the log just before this.
404 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
405 return NO_MEMORY;
406 }
407
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800408 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700409 // something incompatible, we have to cancel and get a new one. This
410 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800411 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700412 // shouldn't matter.
413
Jesse Hall1e27ba22013-09-27 09:05:09 -0700414 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700415 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700416 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800417 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700418 (w != 0 && w != mSinkBufferWidth) ||
419 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700420 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700421 "want %dx%d fmt=%d use=%#" PRIx64 ", "
422 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700423 w, h, format, usage,
424 mSinkBufferWidth, mSinkBufferHeight,
425 buf->getPixelFormat(), buf->getUsage());
426 mOutputFormat = format;
427 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700428 result = refreshOutputBuffer();
429 if (result < 0)
430 return result;
431 }
Jesse Hall38efe862013-04-06 23:12:29 -0700432 }
433
Jesse Hall028dc8f2013-08-20 16:35:32 -0700434 if (source == SOURCE_SINK) {
435 *pslot = mOutputProducerSlot;
436 *fence = mOutputFence;
437 } else {
438 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700439 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700440 if (result >= 0) {
441 *pslot = mapSource2ProducerSlot(source, sslot);
442 }
Jesse Hall38efe862013-04-06 23:12:29 -0700443 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600444 if (outBufferAge) {
445 *outBufferAge = 0;
446 }
KaiChieh Chuangc5222132019-12-20 07:38:19 +0800447
448 if ((mProducerSlotNeedReallocation & (1ULL << *pslot)) != 0) {
449 result |= BUFFER_NEEDS_REALLOCATION;
450 }
451
Jesse Hall38efe862013-04-06 23:12:29 -0700452 return result;
453}
454
Dan Stoza88a459a2014-03-12 09:34:36 -0700455status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
456 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
457 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458}
459
Dan Stozad9822a32014-03-28 15:25:31 -0700460status_t VirtualDisplaySurface::detachNextBuffer(
461 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
462 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
463 return INVALID_OPERATION;
464}
465
Dan Stoza88a459a2014-03-12 09:34:36 -0700466status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
467 const sp<GraphicBuffer>& /* buffer */) {
468 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
469 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800470}
471
Jesse Hall38efe862013-04-06 23:12:29 -0700472status_t VirtualDisplaySurface::queueBuffer(int pslot,
473 const QueueBufferInput& input, QueueBufferOutput* output) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200474 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentine47e45402014-07-18 15:34:25 -0700475 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700476 }
Michael Lentine47e45402014-07-18 15:34:25 -0700477
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800478 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
479 dbgStateStr());
480 mDbgState = DBG_STATE_GPU_DONE;
Jesse Hall38efe862013-04-06 23:12:29 -0700481
482 VDS_LOGV("queueBuffer pslot=%d", pslot);
483
484 status_t result;
485 if (mCompositionType == COMPOSITION_MIXED) {
486 // Queue the buffer back into the scratch pool
487 QueueBufferOutput scratchQBO;
488 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700489 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700490 if (result != NO_ERROR)
491 return result;
492
493 // Now acquire the buffer from the scratch pool -- should be the same
494 // slot and fence as we just queued.
495 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700496 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700497 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700498 if (result != NO_ERROR)
499 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700500 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700501 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700502 item.mSlot, sslot);
503 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
504 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700505
506 } else {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800507 LOG_FATAL_IF(mCompositionType != COMPOSITION_GPU,
508 "Unexpected queueBuffer in state %s for compositionType %s", dbgStateStr(),
509 dbgCompositionTypeStr(mCompositionType));
Jesse Hall38efe862013-04-06 23:12:29 -0700510
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800511 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700512 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700513 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800514 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700515 Rect crop;
516 int scalingMode;
517 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800518 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700519 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700520
521 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700522 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700523 }
524
Brian Anderson3d4039d2016-09-23 16:31:30 -0700525 // This moves the frame timestamps and keeps a copy of all other fields.
526 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700527 return NO_ERROR;
528}
529
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700530status_t VirtualDisplaySurface::cancelBuffer(int pslot,
531 const sp<Fence>& fence) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200532 if (GpuVirtualDisplayId::tryCast(mDisplayId)) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700533 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700534 }
Michael Lentine47e45402014-07-18 15:34:25 -0700535
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800536 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
537 dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700538 VDS_LOGV("cancelBuffer pslot=%d", pslot);
539 Source source = fbSourceForCompositionType(mCompositionType);
540 return mSource[source]->cancelBuffer(
541 mapProducer2SourceSlot(source, pslot), fence);
542}
543
544int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700545 switch (what) {
546 case NATIVE_WINDOW_WIDTH:
547 *value = mSinkBufferWidth;
548 break;
549 case NATIVE_WINDOW_HEIGHT:
550 *value = mSinkBufferHeight;
551 break;
552 default:
553 return mSource[SOURCE_SINK]->query(what, value);
554 }
555 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700556}
557
Dan Stozaf0eaf252014-03-21 13:05:51 -0700558status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700559 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700560 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700561 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700562 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
563 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700564 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700565 updateQueueBufferOutput(std::move(qbo));
566 // This moves the frame timestamps and keeps a copy of all other fields.
567 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700568 }
569 return result;
570}
571
Robert Carr97b9c862016-09-08 13:54:35 -0700572status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
573 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700574}
575
Jesse Hall399184a2014-03-03 15:42:54 -0800576status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
577 return INVALID_OPERATION;
578}
579
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700580void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700581 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700582 // TODO: Should we actually allocate buffers for a virtual display?
583}
584
Dan Stoza9de72932015-04-16 17:28:43 -0700585status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
586 return INVALID_OPERATION;
587}
588
Dan Stoza812ed062015-06-02 15:45:22 -0700589status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
590 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
591 return INVALID_OPERATION;
592}
593
Dan Stozac6f30bd2015-06-08 09:32:50 -0700594String8 VirtualDisplaySurface::getConsumerName() const {
595 return String8("VirtualDisplaySurface");
596}
597
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700598status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
599 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800600 return INVALID_OPERATION;
601}
602
603status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
604 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
605 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700606}
607
Dan Stoza127fc632015-06-30 13:43:32 -0700608status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
609 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
610 return INVALID_OPERATION;
611}
612
Dan Stoza50101d02016-04-07 16:53:23 -0700613status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700614 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
615 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700616 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
617 return INVALID_OPERATION;
618}
619
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700620status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
621 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
622 return INVALID_OPERATION;
623}
624
Chia-I Wue2786ea2017-08-07 10:36:08 -0700625status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
626 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
627}
628
Jesse Hall38efe862013-04-06 23:12:29 -0700629void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700630 QueueBufferOutput&& qbo) {
631 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700632 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700633}
634
635void VirtualDisplaySurface::resetPerFrameState() {
636 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530637 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700638 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700639 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530640 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700641}
642
Jesse Hall028dc8f2013-08-20 16:35:32 -0700643status_t VirtualDisplaySurface::refreshOutputBuffer() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200644 LOG_FATAL_IF(GpuVirtualDisplayId::tryCast(mDisplayId));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645
Jesse Hall028dc8f2013-08-20 16:35:32 -0700646 if (mOutputProducerSlot >= 0) {
647 mSource[SOURCE_SINK]->cancelBuffer(
648 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
649 mOutputFence);
650 }
651
652 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700653 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
654 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700655 if (result < 0)
656 return result;
657 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
658
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800659 // On GPU-only frames, we don't have the right output buffer acquire fence
660 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700661 // (for use in HWC prepare) but not the fence; we'll call this again with
662 // the proper fence once we have it.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200663 const auto halDisplayId = HalVirtualDisplayId::tryCast(mDisplayId);
664 LOG_FATAL_IF(!halDisplayId);
665 result = mHwc.setOutputBuffer(*halDisplayId, Fence::NO_FENCE,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700666 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700667
668 return result;
669}
670
Jesse Hall38efe862013-04-06 23:12:29 -0700671// This slot mapping function is its own inverse, so two copies are unnecessary.
672// Both are kept to make the intent clear where the function is called, and for
673// the (unlikely) chance that we switch to a different mapping function.
674int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
675 if (source == SOURCE_SCRATCH) {
676 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
677 } else {
678 return sslot;
679 }
680}
681int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
682 return mapSource2ProducerSlot(source, pslot);
683}
684
685VirtualDisplaySurface::Source
686VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
687 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
688}
689
690const char* VirtualDisplaySurface::dbgStateStr() const {
691 switch (mDbgState) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800692 case DBG_STATE_IDLE:
693 return "IDLE";
694 case DBG_STATE_PREPARED:
695 return "PREPARED";
696 case DBG_STATE_GPU:
697 return "GPU";
698 case DBG_STATE_GPU_DONE:
699 return "GPU_DONE";
700 case DBG_STATE_HWC:
701 return "HWC";
702 default:
703 return "INVALID";
Jesse Hall38efe862013-04-06 23:12:29 -0700704 }
705}
706
707const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
708 switch (s) {
709 case SOURCE_SINK: return "SINK";
710 case SOURCE_SCRATCH: return "SCRATCH";
711 default: return "INVALID";
712 }
713}
714
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700715// ---------------------------------------------------------------------------
716} // namespace android
717// ---------------------------------------------------------------------------
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800718
719// TODO(b/129481165): remove the #pragma below and fix conversion issues
720#pragma clang diagnostic pop // ignored "-Wconversion"