blob: 1c2853a8572384da89e80138cea1d773dc6e2c60 [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";
45 case compositionengine::DisplaySurface::COMPOSITION_GLES:
46 return "GLES";
47 case compositionengine::DisplaySurface::COMPOSITION_HWC:
48 return "HWC";
49 case compositionengine::DisplaySurface::COMPOSITION_MIXED:
50 return "MIXED";
Jesse Hall38efe862013-04-06 23:12:29 -070051 default: return "<INVALID>";
52 }
53}
54
Dominik Laskowski075d3172018-05-24 15:50:06 -070055VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc,
56 const std::optional<DisplayId>& displayId,
57 const sp<IGraphicBufferProducer>& sink,
58 const sp<IGraphicBufferProducer>& bqProducer,
59 const sp<IGraphicBufferConsumer>& bqConsumer,
60 const std::string& name)
61 : ConsumerBase(bqConsumer),
62 mHwc(hwc),
63 mDisplayId(displayId),
64 mDisplayName(name),
65 mSource{},
66 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
67 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
68 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
69 mProducerSlotSource(0),
70 mProducerBuffers(),
71 mQueueBufferOutput(),
72 mSinkBufferWidth(0),
73 mSinkBufferHeight(0),
74 mCompositionType(COMPOSITION_UNKNOWN),
75 mFbFence(Fence::NO_FENCE),
76 mOutputFence(Fence::NO_FENCE),
77 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
78 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
79 mDbgState(DBG_STATE_IDLE),
80 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
81 mMustRecompose(false),
82 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070083 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070084 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070085
86 resetPerFrameState();
87
88 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080089 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
90 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070091 mSinkBufferWidth = sinkWidth;
92 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080093
94 // Pick the buffer format to request from the sink when not rendering to it
95 // with GLES. If the consumer needs CPU access, use the default format
96 // set by the consumer. Otherwise allow gralloc to decide the format based
97 // on usage bits.
98 int sinkUsage;
99 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
100 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
101 int sinkFormat;
102 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
103 mDefaultOutputFormat = sinkFormat;
104 } else {
105 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
106 }
107 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700108
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700109 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700110 mConsumer->setConsumerName(ConsumerBase::mName);
111 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
112 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700113 sink->setAsyncMode(true);
Alec Mouri8d4f90a2018-11-14 22:33:24 +0000114 IGraphicBufferProducer::QueueBufferOutput output;
115 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700116}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700117
118VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700119 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700120}
121
Dan Stoza71433162014-02-04 16:22:36 -0800122status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700123 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700124 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700125 }
Jesse Hall38efe862013-04-06 23:12:29 -0700126
Dan Stoza71433162014-02-04 16:22:36 -0800127 mMustRecompose = mustRecompose;
128
Jesse Hall38efe862013-04-06 23:12:29 -0700129 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700130 "Unexpected beginFrame() in %s state", dbgStateStr());
131 mDbgState = DBG_STATE_BEGUN;
132
Jesse Hall028dc8f2013-08-20 16:35:32 -0700133 return refreshOutputBuffer();
134}
135
136status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700137 if (!mDisplayId) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700138 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700139 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700140
141 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700142 "Unexpected prepareFrame() in %s state", dbgStateStr());
143 mDbgState = DBG_STATE_PREPARED;
144
145 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700146 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400147 // Some hardware can do RGB->YUV conversion more efficiently in hardware
148 // controlled by HWC than in hardware controlled by the video encoder.
149 // Forcing GLES-composed frames to go through an extra copy by the HWC
150 // allows the format conversion to happen there, rather than passing RGB
151 // directly to the consumer.
152 //
153 // On the other hand, when the consumer prefers RGB or can consume RGB
154 // inexpensively, this forces an unnecessary copy.
155 mCompositionType = COMPOSITION_MIXED;
156 }
157
Jesse Hall38efe862013-04-06 23:12:29 -0700158 if (mCompositionType != mDbgLastCompositionType) {
159 VDS_LOGV("prepareFrame: composition type changed to %s",
160 dbgCompositionTypeStr(mCompositionType));
161 mDbgLastCompositionType = mCompositionType;
162 }
163
Jesse Hall1e27ba22013-09-27 09:05:09 -0700164 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800165 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700166 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
167 // We must have just switched from GLES-only to MIXED or HWC
168 // composition. Stop using the format and usage requested by the GLES
169 // 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 //
174 // If we just switched *to* GLES-only mode, we'll change the
175 // format/usage and get a new buffer when the GLES driver calls
176 // 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 {
195 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
196 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
197 dbgStateStr());
198 }
199 mDbgState = DBG_STATE_HWC;
200
Jesse Hall14e8b012013-11-07 12:28:26 -0800201 if (mOutputProducerSlot < 0 ||
202 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700203 // Last chance bailout if something bad happened earlier. For example,
204 // in a GLES configuration, if the sink disappears then dequeueBuffer
205 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
206 // will soldier on. So we end up here without a buffer. There should
207 // be lots of scary messages in the log just before this.
208 VDS_LOGE("advanceFrame: no buffer, bailing out");
209 return NO_MEMORY;
210 }
211
Jesse Hall14e8b012013-11-07 12:28:26 -0800212 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800213 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700214 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
215 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
216 mFbProducerSlot, fbBuffer.get(),
217 mOutputProducerSlot, outBuffer.get());
218
Jesse Hallb716e572013-10-01 17:25:20 -0700219 // At this point we know the output buffer acquire fence,
220 // so update HWC state with it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700221 mHwc.setOutputBuffer(*mDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700222
Jesse Hall14e8b012013-11-07 12:28:26 -0800223 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800224 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800225 uint32_t hwcSlot = 0;
226 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800227 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800228 &hwcSlot, &hwcBuffer);
229
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230 // TODO: Correctly propagate the dataspace from GL composition
Dominik Laskowski075d3172018-05-24 15:50:06 -0700231 result = mHwc.setClientTarget(*mDisplayId, hwcSlot, mFbFence, hwcBuffer,
232 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800233 }
234
235 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700236}
237
Jesse Hall851cfe82013-03-20 13:44:00 -0700238void VirtualDisplaySurface::onFrameCommitted() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700239 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700240 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 }
Jesse Hall38efe862013-04-06 23:12:29 -0700242
243 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
244 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
245 mDbgState = DBG_STATE_IDLE;
246
Dominik Laskowski075d3172018-05-24 15:50:06 -0700247 sp<Fence> retireFence = mHwc.getPresentFence(*mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700248 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
249 // release the scratch buffer back to the pool
250 Mutex::Autolock lock(mMutex);
251 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
252 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
254 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800255 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700256 }
257
258 if (mOutputProducerSlot >= 0) {
259 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
260 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700261 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800262 if (mMustRecompose) {
263 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
264 QueueBufferInput(
265 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800266 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800267 Rect(mSinkBufferWidth, mSinkBufferHeight),
268 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800270 &qbo);
271 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700272 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800273 }
274 } else {
275 // If the surface hadn't actually been updated, then we only went
276 // through the motions of updating the display to keep our state
277 // machine happy. We cancel the buffer to avoid triggering another
278 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800279 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700280 }
281 }
282
283 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700284}
285
Dan Stoza01049c82014-11-11 10:32:31 -0800286void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700287}
288
Michael Lentine47e45402014-07-18 15:34:25 -0700289void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700290 mQueueBufferOutput.width = w;
291 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700292 mSinkBufferWidth = w;
293 mSinkBufferHeight = h;
294}
295
Dan Stoza9e56aa02015-11-02 13:00:03 -0800296const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
297 return mFbFence;
298}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800299
Jesse Hall38efe862013-04-06 23:12:29 -0700300status_t VirtualDisplaySurface::requestBuffer(int pslot,
301 sp<GraphicBuffer>* outBuf) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700302 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700303 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700304 }
Michael Lentine47e45402014-07-18 15:34:25 -0700305
Jesse Hall38efe862013-04-06 23:12:29 -0700306 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
307 "Unexpected requestBuffer pslot=%d in %s state",
308 pslot, dbgStateStr());
309
310 *outBuf = mProducerBuffers[pslot];
311 return NO_ERROR;
312}
313
Pablo Ceballosfa455352015-08-12 17:47:47 -0700314status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
315 int maxDequeuedBuffers) {
316 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
317}
318
319status_t VirtualDisplaySurface::setAsyncMode(bool async) {
320 return mSource[SOURCE_SINK]->setAsyncMode(async);
321}
322
Jesse Hall38efe862013-04-06 23:12:29 -0700323status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700324 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700325 LOG_FATAL_IF(!mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700326
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600327 status_t result =
328 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
329 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700330 if (result < 0)
331 return result;
332 int pslot = mapSource2ProducerSlot(source, *sslot);
333 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
334 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700335 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700336
Dan Stozafebd4f42014-04-09 16:14:51 -0700337 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700338 // This slot was previously dequeued from the other source; must
339 // re-request the buffer.
340 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700341 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700342 mProducerSlotSource |= sourceBit;
343 }
344
345 if (result & RELEASE_ALL_BUFFERS) {
346 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700347 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700348 mProducerBuffers[i].clear();
349 }
350 }
351 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700352 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
353 if (result < 0) {
354 mProducerBuffers[pslot].clear();
355 mSource[source]->cancelBuffer(*sslot, *fence);
356 return result;
357 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700358 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800359 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
360 mProducerBuffers[pslot]->getPixelFormat(),
361 mProducerBuffers[pslot]->getUsage());
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) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700371 if (!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
Jesse Hall38efe862013-04-06 23:12:29 -0700376 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
377 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
378 mDbgState = DBG_STATE_GLES;
379
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700380 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, 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,
389 // in a GLES configuration, if the sink disappears then dequeueBuffer
390 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
391 // 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.
393 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
394 return NO_MEMORY;
395 }
396
Jesse Hall028dc8f2013-08-20 16:35:32 -0700397 // We already dequeued the output buffer. If the GLES driver wants
398 // something incompatible, we have to cancel and get a new one. This
399 // will mean that HWC will see a different output buffer between
400 // prepare and set, but since we're in GLES-only mode already it
401 // 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)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700409 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700410 "want %dx%d fmt=%d use=%#" PRIx64 ", "
411 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700412 w, h, format, usage,
413 mSinkBufferWidth, mSinkBufferHeight,
414 buf->getPixelFormat(), buf->getUsage());
415 mOutputFormat = format;
416 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700417 result = refreshOutputBuffer();
418 if (result < 0)
419 return result;
420 }
Jesse Hall38efe862013-04-06 23:12:29 -0700421 }
422
Jesse Hall028dc8f2013-08-20 16:35:32 -0700423 if (source == SOURCE_SINK) {
424 *pslot = mOutputProducerSlot;
425 *fence = mOutputFence;
426 } else {
427 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700428 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700429 if (result >= 0) {
430 *pslot = mapSource2ProducerSlot(source, sslot);
431 }
Jesse Hall38efe862013-04-06 23:12:29 -0700432 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600433 if (outBufferAge) {
434 *outBufferAge = 0;
435 }
Jesse Hall38efe862013-04-06 23:12:29 -0700436 return result;
437}
438
Dan Stoza88a459a2014-03-12 09:34:36 -0700439status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
440 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
441 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800442}
443
Dan Stozad9822a32014-03-28 15:25:31 -0700444status_t VirtualDisplaySurface::detachNextBuffer(
445 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
446 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
447 return INVALID_OPERATION;
448}
449
Dan Stoza88a459a2014-03-12 09:34:36 -0700450status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
451 const sp<GraphicBuffer>& /* buffer */) {
452 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
453 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800454}
455
Jesse Hall38efe862013-04-06 23:12:29 -0700456status_t VirtualDisplaySurface::queueBuffer(int pslot,
457 const QueueBufferInput& input, QueueBufferOutput* output) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700458 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700459 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700460 }
Michael Lentine47e45402014-07-18 15:34:25 -0700461
Jesse Hall38efe862013-04-06 23:12:29 -0700462 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
463 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
464 dbgStateStr());
465 mDbgState = DBG_STATE_GLES_DONE;
466
467 VDS_LOGV("queueBuffer pslot=%d", pslot);
468
469 status_t result;
470 if (mCompositionType == COMPOSITION_MIXED) {
471 // Queue the buffer back into the scratch pool
472 QueueBufferOutput scratchQBO;
473 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700474 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700475 if (result != NO_ERROR)
476 return result;
477
478 // Now acquire the buffer from the scratch pool -- should be the same
479 // slot and fence as we just queued.
480 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700481 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700482 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700483 if (result != NO_ERROR)
484 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700485 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700486 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700487 item.mSlot, sslot);
488 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
489 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700490
491 } else {
492 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
493 "Unexpected queueBuffer in state %s for compositionType %s",
494 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
495
496 // Extract the GLES release fence for HWC to acquire
497 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700498 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700500 Rect crop;
501 int scalingMode;
502 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800503 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700504 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700505
506 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700507 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700508 }
509
Brian Anderson3d4039d2016-09-23 16:31:30 -0700510 // This moves the frame timestamps and keeps a copy of all other fields.
511 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700512 return NO_ERROR;
513}
514
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700515status_t VirtualDisplaySurface::cancelBuffer(int pslot,
516 const sp<Fence>& fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700517 if (!mDisplayId) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700518 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700519 }
Michael Lentine47e45402014-07-18 15:34:25 -0700520
Jesse Hall38efe862013-04-06 23:12:29 -0700521 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
522 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
523 dbgStateStr());
524 VDS_LOGV("cancelBuffer pslot=%d", pslot);
525 Source source = fbSourceForCompositionType(mCompositionType);
526 return mSource[source]->cancelBuffer(
527 mapProducer2SourceSlot(source, pslot), fence);
528}
529
530int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700531 switch (what) {
532 case NATIVE_WINDOW_WIDTH:
533 *value = mSinkBufferWidth;
534 break;
535 case NATIVE_WINDOW_HEIGHT:
536 *value = mSinkBufferHeight;
537 break;
538 default:
539 return mSource[SOURCE_SINK]->query(what, value);
540 }
541 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700542}
543
Dan Stozaf0eaf252014-03-21 13:05:51 -0700544status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700545 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700546 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700547 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700548 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
549 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700550 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700551 updateQueueBufferOutput(std::move(qbo));
552 // This moves the frame timestamps and keeps a copy of all other fields.
553 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700554 }
555 return result;
556}
557
Robert Carr97b9c862016-09-08 13:54:35 -0700558status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
559 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700560}
561
Jesse Hall399184a2014-03-03 15:42:54 -0800562status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
563 return INVALID_OPERATION;
564}
565
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700566void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700567 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700568 // TODO: Should we actually allocate buffers for a virtual display?
569}
570
Dan Stoza9de72932015-04-16 17:28:43 -0700571status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
572 return INVALID_OPERATION;
573}
574
Dan Stoza812ed062015-06-02 15:45:22 -0700575status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
576 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
577 return INVALID_OPERATION;
578}
579
Dan Stozac6f30bd2015-06-08 09:32:50 -0700580String8 VirtualDisplaySurface::getConsumerName() const {
581 return String8("VirtualDisplaySurface");
582}
583
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700584status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
585 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800586 return INVALID_OPERATION;
587}
588
589status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
590 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
591 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700592}
593
Dan Stoza127fc632015-06-30 13:43:32 -0700594status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
595 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
596 return INVALID_OPERATION;
597}
598
Dan Stoza50101d02016-04-07 16:53:23 -0700599status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700600 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
601 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700602 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
603 return INVALID_OPERATION;
604}
605
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700606status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
607 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
608 return INVALID_OPERATION;
609}
610
Chia-I Wue2786ea2017-08-07 10:36:08 -0700611status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
612 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
613}
614
Jesse Hall38efe862013-04-06 23:12:29 -0700615void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700616 QueueBufferOutput&& qbo) {
617 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700618 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700619}
620
621void VirtualDisplaySurface::resetPerFrameState() {
622 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530623 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700624 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700625 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530626 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700627}
628
Jesse Hall028dc8f2013-08-20 16:35:32 -0700629status_t VirtualDisplaySurface::refreshOutputBuffer() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700630 LOG_FATAL_IF(!mDisplayId);
631
Jesse Hall028dc8f2013-08-20 16:35:32 -0700632 if (mOutputProducerSlot >= 0) {
633 mSource[SOURCE_SINK]->cancelBuffer(
634 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
635 mOutputFence);
636 }
637
638 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700639 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
640 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700641 if (result < 0)
642 return result;
643 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
644
Jesse Hallb716e572013-10-01 17:25:20 -0700645 // On GLES-only frames, we don't have the right output buffer acquire fence
646 // until after GLES calls queueBuffer(). So here we just set the buffer
647 // (for use in HWC prepare) but not the fence; we'll call this again with
648 // the proper fence once we have it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700649 result = mHwc.setOutputBuffer(*mDisplayId, Fence::NO_FENCE,
650 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700651
652 return result;
653}
654
Jesse Hall38efe862013-04-06 23:12:29 -0700655// This slot mapping function is its own inverse, so two copies are unnecessary.
656// Both are kept to make the intent clear where the function is called, and for
657// the (unlikely) chance that we switch to a different mapping function.
658int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
659 if (source == SOURCE_SCRATCH) {
660 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
661 } else {
662 return sslot;
663 }
664}
665int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
666 return mapSource2ProducerSlot(source, pslot);
667}
668
669VirtualDisplaySurface::Source
670VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
671 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
672}
673
674const char* VirtualDisplaySurface::dbgStateStr() const {
675 switch (mDbgState) {
676 case DBG_STATE_IDLE: return "IDLE";
677 case DBG_STATE_PREPARED: return "PREPARED";
678 case DBG_STATE_GLES: return "GLES";
679 case DBG_STATE_GLES_DONE: return "GLES_DONE";
680 case DBG_STATE_HWC: return "HWC";
681 default: return "INVALID";
682 }
683}
684
685const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
686 switch (s) {
687 case SOURCE_SINK: return "SINK";
688 case SOURCE_SCRATCH: return "SCRATCH";
689 default: return "INVALID";
690 }
691}
692
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700693// ---------------------------------------------------------------------------
694} // namespace android
695// ---------------------------------------------------------------------------