blob: 56b0ea61e3d5d02a7446e42ecf3ba15fdebceda9 [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
Jesse Hall38efe862013-04-06 23:12:29 -070017// #define LOG_NDEBUG 0
Jesse Hall99c7dbb2013-03-14 14:29:29 -070018#include "VirtualDisplaySurface.h"
Mathias Agopiancb496ac2017-05-22 14:21:00 -070019
20#include <inttypes.h>
21
Jesse Hall38efe862013-04-06 23:12:29 -070022#include "HWComposer.h"
Fabien Sanglarda34ed632017-03-14 11:43:52 -070023#include "SurfaceFlinger.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070024
Dan Stoza11611f92015-03-12 15:12:44 -070025#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080026#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070027#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070028#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070029
Jesse Hall99c7dbb2013-03-14 14:29:29 -070030// ---------------------------------------------------------------------------
31namespace android {
32// ---------------------------------------------------------------------------
33
Jesse Hall24cd98e2014-07-13 14:37:16 -070034#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070035 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070036#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%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_LOGV(msg, ...) ALOGV("[%s] " msg, \
Dominik Laskowskibf170d92018-04-19 15:08:05 -070039 mDisplayName.c_str(), ##__VA_ARGS__)
Jesse Hall38efe862013-04-06 23:12:29 -070040
Lloyd Pique542307f2018-10-19 13:24:08 -070041static const char* dbgCompositionTypeStr(compositionengine::DisplaySurface::CompositionType type) {
Jesse Hall38efe862013-04-06 23:12:29 -070042 switch (type) {
Lloyd Pique542307f2018-10-19 13:24:08 -070043 case compositionengine::DisplaySurface::COMPOSITION_UNKNOWN:
44 return "UNKNOWN";
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080045 case compositionengine::DisplaySurface::COMPOSITION_GPU:
46 return "GPU";
Lloyd Pique542307f2018-10-19 13:24:08 -070047 case compositionengine::DisplaySurface::COMPOSITION_HWC:
48 return "HWC";
49 case compositionengine::DisplaySurface::COMPOSITION_MIXED:
50 return "MIXED";
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080051 default:
52 return "<INVALID>";
Jesse Hall38efe862013-04-06 23:12:29 -070053 }
54}
55
Dominik Laskowski075d3172018-05-24 15:50:06 -070056VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc,
57 const std::optional<DisplayId>& displayId,
58 const sp<IGraphicBufferProducer>& sink,
59 const sp<IGraphicBufferProducer>& bqProducer,
60 const sp<IGraphicBufferConsumer>& bqConsumer,
61 const std::string& name)
62 : ConsumerBase(bqConsumer),
63 mHwc(hwc),
64 mDisplayId(displayId),
65 mDisplayName(name),
66 mSource{},
67 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
68 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
69 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
70 mProducerSlotSource(0),
71 mProducerBuffers(),
72 mQueueBufferOutput(),
73 mSinkBufferWidth(0),
74 mSinkBufferHeight(0),
75 mCompositionType(COMPOSITION_UNKNOWN),
76 mFbFence(Fence::NO_FENCE),
77 mOutputFence(Fence::NO_FENCE),
78 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
79 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
80 mDbgState(DBG_STATE_IDLE),
81 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
82 mMustRecompose(false),
83 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070084 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070085 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070086
87 resetPerFrameState();
88
89 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080090 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
91 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070092 mSinkBufferWidth = sinkWidth;
93 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080094
95 // Pick the buffer format to request from the sink when not rendering to it
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080096 // with GPU. If the consumer needs CPU access, use the default format
Jesse Hall497ba0e2013-11-04 16:43:03 -080097 // set by the consumer. Otherwise allow gralloc to decide the format based
98 // on usage bits.
99 int sinkUsage;
100 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
101 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
102 int sinkFormat;
103 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
104 mDefaultOutputFormat = sinkFormat;
105 } else {
106 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
107 }
108 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700109
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700110 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700111 mConsumer->setConsumerName(ConsumerBase::mName);
112 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
113 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700114 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000115 IGraphicBufferProducer::QueueBufferOutput output;
116 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700117}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700118
119VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700120 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700121}
122
Dan Stoza71433162014-02-04 16:22:36 -0800123status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700124 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700125 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700126 }
Jesse Hall38efe862013-04-06 23:12:29 -0700127
Dan Stoza71433162014-02-04 16:22:36 -0800128 mMustRecompose = mustRecompose;
129
Jesse Hall38efe862013-04-06 23:12:29 -0700130 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700131 "Unexpected beginFrame() in %s state", dbgStateStr());
132 mDbgState = DBG_STATE_BEGUN;
133
Jesse Hall028dc8f2013-08-20 16:35:32 -0700134 return refreshOutputBuffer();
135}
136
137status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700138 if (!mDisplayId) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700139 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700140 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700141
142 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700143 "Unexpected prepareFrame() in %s state", dbgStateStr());
144 mDbgState = DBG_STATE_PREPARED;
145
146 mCompositionType = compositionType;
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800147 if (mForceHwcCopy && mCompositionType == COMPOSITION_GPU) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400148 // Some hardware can do RGB->YUV conversion more efficiently in hardware
149 // controlled by HWC than in hardware controlled by the video encoder.
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800150 // Forcing GPU-composed frames to go through an extra copy by the HWC
Naseer Ahmed6a968462013-10-04 16:15:22 -0400151 // allows the format conversion to happen there, rather than passing RGB
152 // directly to the consumer.
153 //
154 // On the other hand, when the consumer prefers RGB or can consume RGB
155 // inexpensively, this forces an unnecessary copy.
156 mCompositionType = COMPOSITION_MIXED;
157 }
158
Jesse Hall38efe862013-04-06 23:12:29 -0700159 if (mCompositionType != mDbgLastCompositionType) {
160 VDS_LOGV("prepareFrame: composition type changed to %s",
161 dbgCompositionTypeStr(mCompositionType));
162 mDbgLastCompositionType = mCompositionType;
163 }
164
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800165 if (mCompositionType != COMPOSITION_GPU &&
166 (mOutputFormat != mDefaultOutputFormat || mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
167 // We must have just switched from GPU-only to MIXED or HWC
168 // composition. Stop using the format and usage requested by the GPU
Jesse Hall1e27ba22013-09-27 09:05:09 -0700169 // driver; they may be suboptimal when HWC is writing to the output
170 // buffer. For example, if the output is going to a video encoder, and
171 // HWC can write directly to YUV, some hardware can skip a
172 // memory-to-memory RGB-to-YUV conversion step.
173 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800174 // If we just switched *to* GPU-only mode, we'll change the
175 // format/usage and get a new buffer when the GPU driver calls
Jesse Hall1e27ba22013-09-27 09:05:09 -0700176 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800177 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700178 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
179 refreshOutputBuffer();
180 }
181
Jesse Hall38efe862013-04-06 23:12:29 -0700182 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700183}
184
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700185status_t VirtualDisplaySurface::advanceFrame() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700186 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700187 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700188 }
Jesse Hall38efe862013-04-06 23:12:29 -0700189
190 if (mCompositionType == COMPOSITION_HWC) {
191 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
192 "Unexpected advanceFrame() in %s state on HWC frame",
193 dbgStateStr());
194 } else {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800195 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU_DONE,
196 "Unexpected advanceFrame() in %s state on GPU/MIXED frame", dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700197 }
198 mDbgState = DBG_STATE_HWC;
199
Jesse Hall14e8b012013-11-07 12:28:26 -0800200 if (mOutputProducerSlot < 0 ||
201 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700202 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800203 // in a graphics API configuration, if the sink disappears then dequeueBuffer
204 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Jesse Hall38efe862013-04-06 23:12:29 -0700205 // will soldier on. So we end up here without a buffer. There should
206 // be lots of scary messages in the log just before this.
207 VDS_LOGE("advanceFrame: no buffer, bailing out");
208 return NO_MEMORY;
209 }
210
Jesse Hall14e8b012013-11-07 12:28:26 -0800211 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800212 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700213 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
214 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
215 mFbProducerSlot, fbBuffer.get(),
216 mOutputProducerSlot, outBuffer.get());
217
Jesse Hallb716e572013-10-01 17:25:20 -0700218 // At this point we know the output buffer acquire fence,
219 // so update HWC state with it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700220 mHwc.setOutputBuffer(*mDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700221
Jesse Hall14e8b012013-11-07 12:28:26 -0800222 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800223 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800224 uint32_t hwcSlot = 0;
225 sp<GraphicBuffer> hwcBuffer;
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700226 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer, &hwcSlot, &hwcBuffer);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800227
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228 // TODO: Correctly propagate the dataspace from GL composition
Dominik Laskowski075d3172018-05-24 15:50:06 -0700229 result = mHwc.setClientTarget(*mDisplayId, hwcSlot, mFbFence, hwcBuffer,
230 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800231 }
232
233 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700234}
235
Jesse Hall851cfe82013-03-20 13:44:00 -0700236void VirtualDisplaySurface::onFrameCommitted() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700237 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700238 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700239 }
Jesse Hall38efe862013-04-06 23:12:29 -0700240
241 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
242 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
243 mDbgState = DBG_STATE_IDLE;
244
Dominik Laskowski075d3172018-05-24 15:50:06 -0700245 sp<Fence> retireFence = mHwc.getPresentFence(*mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700246 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
247 // release the scratch buffer back to the pool
248 Mutex::Autolock lock(mMutex);
249 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
250 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
252 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800253 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700254 }
255
256 if (mOutputProducerSlot >= 0) {
257 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
258 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700259 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800260 if (mMustRecompose) {
261 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
262 QueueBufferInput(
263 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800264 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800265 Rect(mSinkBufferWidth, mSinkBufferHeight),
266 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800267 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800268 &qbo);
269 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700270 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800271 }
272 } else {
273 // If the surface hadn't actually been updated, then we only went
274 // through the motions of updating the display to keep our state
275 // machine happy. We cancel the buffer to avoid triggering another
276 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700278 }
279 }
280
281 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700282}
283
Dan Stoza01049c82014-11-11 10:32:31 -0800284void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700285}
286
Michael Lentine47e45402014-07-18 15:34:25 -0700287void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700288 mQueueBufferOutput.width = w;
289 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700290 mSinkBufferWidth = w;
291 mSinkBufferHeight = h;
292}
293
Dan Stoza9e56aa02015-11-02 13:00:03 -0800294const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
295 return mFbFence;
296}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800297
Jesse Hall38efe862013-04-06 23:12:29 -0700298status_t VirtualDisplaySurface::requestBuffer(int pslot,
299 sp<GraphicBuffer>* outBuf) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700301 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700302 }
Michael Lentine47e45402014-07-18 15:34:25 -0700303
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800304 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected requestBuffer pslot=%d in %s state", pslot,
305 dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700306
307 *outBuf = mProducerBuffers[pslot];
308 return NO_ERROR;
309}
310
Pablo Ceballosfa455352015-08-12 17:47:47 -0700311status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
312 int maxDequeuedBuffers) {
313 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
314}
315
316status_t VirtualDisplaySurface::setAsyncMode(bool async) {
317 return mSource[SOURCE_SINK]->setAsyncMode(async);
318}
319
Jesse Hall38efe862013-04-06 23:12:29 -0700320status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700321 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322 LOG_FATAL_IF(!mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700323
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600324 status_t result =
325 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
326 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700327 if (result < 0)
328 return result;
329 int pslot = mapSource2ProducerSlot(source, *sslot);
330 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
331 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700332 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700333
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.
337 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700338 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700339 mProducerSlotSource |= sourceBit;
340 }
341
342 if (result & RELEASE_ALL_BUFFERS) {
343 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700344 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700345 mProducerBuffers[i].clear();
346 }
347 }
348 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700349 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
350 if (result < 0) {
351 mProducerBuffers[pslot].clear();
352 mSource[source]->cancelBuffer(*sslot, *fence);
353 return result;
354 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700355 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800356 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
357 mProducerBuffers[pslot]->getPixelFormat(),
358 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700359 }
360
361 return result;
362}
363
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600364status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
365 PixelFormat format, uint64_t usage,
366 uint64_t* outBufferAge,
367 FrameEventHistoryDelta* outTimestamps) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700368 if (!mDisplayId) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600369 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
370 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700371 }
Michael Lentine47e45402014-07-18 15:34:25 -0700372
Jesse Hall38efe862013-04-06 23:12:29 -0700373 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
374 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800375 mDbgState = DBG_STATE_GPU;
Jesse Hall38efe862013-04-06 23:12:29 -0700376
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700377 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700378
Jesse Hall028dc8f2013-08-20 16:35:32 -0700379 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700380 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700381
Jesse Hall38efe862013-04-06 23:12:29 -0700382 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700383
384 if (mOutputProducerSlot < 0) {
385 // Last chance bailout if something bad happened earlier. For example,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800386 // in a graphics API configuration, if the sink disappears then dequeueBuffer
387 // will fail, the GPU driver won't queue a buffer, but SurfaceFlinger
Mathias Agopian6da15f42013-09-25 20:40:07 -0700388 // will soldier on. So we end up here without a buffer. There should
389 // be lots of scary messages in the log just before this.
390 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
391 return NO_MEMORY;
392 }
393
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800394 // We already dequeued the output buffer. If the GPU driver wants
Jesse Hall028dc8f2013-08-20 16:35:32 -0700395 // something incompatible, we have to cancel and get a new one. This
396 // will mean that HWC will see a different output buffer between
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800397 // prepare and set, but since we're in GPU-only mode already it
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 // shouldn't matter.
399
Jesse Hall1e27ba22013-09-27 09:05:09 -0700400 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700401 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700402 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800403 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700404 (w != 0 && w != mSinkBufferWidth) ||
405 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700406 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700407 "want %dx%d fmt=%d use=%#" PRIx64 ", "
408 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700409 w, h, format, usage,
410 mSinkBufferWidth, mSinkBufferHeight,
411 buf->getPixelFormat(), buf->getUsage());
412 mOutputFormat = format;
413 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700414 result = refreshOutputBuffer();
415 if (result < 0)
416 return result;
417 }
Jesse Hall38efe862013-04-06 23:12:29 -0700418 }
419
Jesse Hall028dc8f2013-08-20 16:35:32 -0700420 if (source == SOURCE_SINK) {
421 *pslot = mOutputProducerSlot;
422 *fence = mOutputFence;
423 } else {
424 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700425 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700426 if (result >= 0) {
427 *pslot = mapSource2ProducerSlot(source, sslot);
428 }
Jesse Hall38efe862013-04-06 23:12:29 -0700429 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600430 if (outBufferAge) {
431 *outBufferAge = 0;
432 }
Jesse Hall38efe862013-04-06 23:12:29 -0700433 return result;
434}
435
Dan Stoza88a459a2014-03-12 09:34:36 -0700436status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
437 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
438 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800439}
440
Dan Stozad9822a32014-03-28 15:25:31 -0700441status_t VirtualDisplaySurface::detachNextBuffer(
442 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
443 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
444 return INVALID_OPERATION;
445}
446
Dan Stoza88a459a2014-03-12 09:34:36 -0700447status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
448 const sp<GraphicBuffer>& /* buffer */) {
449 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
450 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800451}
452
Jesse Hall38efe862013-04-06 23:12:29 -0700453status_t VirtualDisplaySurface::queueBuffer(int pslot,
454 const QueueBufferInput& input, QueueBufferOutput* output) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700455 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700456 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700457 }
Michael Lentine47e45402014-07-18 15:34:25 -0700458
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800459 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
460 dbgStateStr());
461 mDbgState = DBG_STATE_GPU_DONE;
Jesse Hall38efe862013-04-06 23:12:29 -0700462
463 VDS_LOGV("queueBuffer pslot=%d", pslot);
464
465 status_t result;
466 if (mCompositionType == COMPOSITION_MIXED) {
467 // Queue the buffer back into the scratch pool
468 QueueBufferOutput scratchQBO;
469 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700470 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700471 if (result != NO_ERROR)
472 return result;
473
474 // Now acquire the buffer from the scratch pool -- should be the same
475 // slot and fence as we just queued.
476 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700477 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700478 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700479 if (result != NO_ERROR)
480 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700481 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700482 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700483 item.mSlot, sslot);
484 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
485 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700486
487 } else {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800488 LOG_FATAL_IF(mCompositionType != COMPOSITION_GPU,
489 "Unexpected queueBuffer in state %s for compositionType %s", dbgStateStr(),
490 dbgCompositionTypeStr(mCompositionType));
Jesse Hall38efe862013-04-06 23:12:29 -0700491
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800492 // Extract the GPU release fence for HWC to acquire
Jesse Hall38efe862013-04-06 23:12:29 -0700493 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700494 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800495 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700496 Rect crop;
497 int scalingMode;
498 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700500 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700501
502 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700503 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700504 }
505
Brian Anderson3d4039d2016-09-23 16:31:30 -0700506 // This moves the frame timestamps and keeps a copy of all other fields.
507 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700508 return NO_ERROR;
509}
510
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700511status_t VirtualDisplaySurface::cancelBuffer(int pslot,
512 const sp<Fence>& fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700513 if (!mDisplayId) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700514 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700515 }
Michael Lentine47e45402014-07-18 15:34:25 -0700516
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800517 VDS_LOGW_IF(mDbgState != DBG_STATE_GPU, "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
518 dbgStateStr());
Jesse Hall38efe862013-04-06 23:12:29 -0700519 VDS_LOGV("cancelBuffer pslot=%d", pslot);
520 Source source = fbSourceForCompositionType(mCompositionType);
521 return mSource[source]->cancelBuffer(
522 mapProducer2SourceSlot(source, pslot), fence);
523}
524
525int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700526 switch (what) {
527 case NATIVE_WINDOW_WIDTH:
528 *value = mSinkBufferWidth;
529 break;
530 case NATIVE_WINDOW_HEIGHT:
531 *value = mSinkBufferHeight;
532 break;
533 default:
534 return mSource[SOURCE_SINK]->query(what, value);
535 }
536 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700537}
538
Dan Stozaf0eaf252014-03-21 13:05:51 -0700539status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700540 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700541 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700542 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700543 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
544 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700545 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700546 updateQueueBufferOutput(std::move(qbo));
547 // This moves the frame timestamps and keeps a copy of all other fields.
548 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700549 }
550 return result;
551}
552
Robert Carr97b9c862016-09-08 13:54:35 -0700553status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
554 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700555}
556
Jesse Hall399184a2014-03-03 15:42:54 -0800557status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
558 return INVALID_OPERATION;
559}
560
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700561void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700562 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700563 // TODO: Should we actually allocate buffers for a virtual display?
564}
565
Dan Stoza9de72932015-04-16 17:28:43 -0700566status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
567 return INVALID_OPERATION;
568}
569
Dan Stoza812ed062015-06-02 15:45:22 -0700570status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
571 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
572 return INVALID_OPERATION;
573}
574
Dan Stozac6f30bd2015-06-08 09:32:50 -0700575String8 VirtualDisplaySurface::getConsumerName() const {
576 return String8("VirtualDisplaySurface");
577}
578
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700579status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
580 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800581 return INVALID_OPERATION;
582}
583
584status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
585 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
586 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700587}
588
Dan Stoza127fc632015-06-30 13:43:32 -0700589status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
590 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
591 return INVALID_OPERATION;
592}
593
Dan Stoza50101d02016-04-07 16:53:23 -0700594status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700595 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
596 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700597 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
598 return INVALID_OPERATION;
599}
600
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700601status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
602 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
603 return INVALID_OPERATION;
604}
605
Chia-I Wue2786ea2017-08-07 10:36:08 -0700606status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
607 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
608}
609
Jesse Hall38efe862013-04-06 23:12:29 -0700610void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700611 QueueBufferOutput&& qbo) {
612 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700613 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700614}
615
616void VirtualDisplaySurface::resetPerFrameState() {
617 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530618 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700619 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700620 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530621 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700622}
623
Jesse Hall028dc8f2013-08-20 16:35:32 -0700624status_t VirtualDisplaySurface::refreshOutputBuffer() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700625 LOG_FATAL_IF(!mDisplayId);
626
Jesse Hall028dc8f2013-08-20 16:35:32 -0700627 if (mOutputProducerSlot >= 0) {
628 mSource[SOURCE_SINK]->cancelBuffer(
629 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
630 mOutputFence);
631 }
632
633 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700634 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
635 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700636 if (result < 0)
637 return result;
638 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
639
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800640 // On GPU-only frames, we don't have the right output buffer acquire fence
641 // until after GPU calls queueBuffer(). So here we just set the buffer
Jesse Hallb716e572013-10-01 17:25:20 -0700642 // (for use in HWC prepare) but not the fence; we'll call this again with
643 // the proper fence once we have it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700644 result = mHwc.setOutputBuffer(*mDisplayId, Fence::NO_FENCE,
645 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700646
647 return result;
648}
649
Jesse Hall38efe862013-04-06 23:12:29 -0700650// This slot mapping function is its own inverse, so two copies are unnecessary.
651// Both are kept to make the intent clear where the function is called, and for
652// the (unlikely) chance that we switch to a different mapping function.
653int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
654 if (source == SOURCE_SCRATCH) {
655 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
656 } else {
657 return sslot;
658 }
659}
660int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
661 return mapSource2ProducerSlot(source, pslot);
662}
663
664VirtualDisplaySurface::Source
665VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
666 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
667}
668
669const char* VirtualDisplaySurface::dbgStateStr() const {
670 switch (mDbgState) {
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800671 case DBG_STATE_IDLE:
672 return "IDLE";
673 case DBG_STATE_PREPARED:
674 return "PREPARED";
675 case DBG_STATE_GPU:
676 return "GPU";
677 case DBG_STATE_GPU_DONE:
678 return "GPU_DONE";
679 case DBG_STATE_HWC:
680 return "HWC";
681 default:
682 return "INVALID";
Jesse Hall38efe862013-04-06 23:12:29 -0700683 }
684}
685
686const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
687 switch (s) {
688 case SOURCE_SINK: return "SINK";
689 case SOURCE_SCRATCH: return "SCRATCH";
690 default: return "INVALID";
691 }
692}
693
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700694// ---------------------------------------------------------------------------
695} // namespace android
696// ---------------------------------------------------------------------------