blob: 27d3dc560920e74acd724edd02a0f3af895baa28 [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
41static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
42 switch (type) {
43 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
44 case DisplaySurface::COMPOSITION_GLES: return "GLES";
45 case DisplaySurface::COMPOSITION_HWC: return "HWC";
46 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
47 default: return "<INVALID>";
48 }
49}
50
Dominik Laskowski075d3172018-05-24 15:50:06 -070051VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc,
52 const std::optional<DisplayId>& displayId,
53 const sp<IGraphicBufferProducer>& sink,
54 const sp<IGraphicBufferProducer>& bqProducer,
55 const sp<IGraphicBufferConsumer>& bqConsumer,
56 const std::string& name)
57 : ConsumerBase(bqConsumer),
58 mHwc(hwc),
59 mDisplayId(displayId),
60 mDisplayName(name),
61 mSource{},
62 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
63 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
64 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
65 mProducerSlotSource(0),
66 mProducerBuffers(),
67 mQueueBufferOutput(),
68 mSinkBufferWidth(0),
69 mSinkBufferHeight(0),
70 mCompositionType(COMPOSITION_UNKNOWN),
71 mFbFence(Fence::NO_FENCE),
72 mOutputFence(Fence::NO_FENCE),
73 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
74 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
75 mDbgState(DBG_STATE_IDLE),
76 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
77 mMustRecompose(false),
78 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv) {
Jesse Hall38efe862013-04-06 23:12:29 -070079 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070080 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070081
82 resetPerFrameState();
83
84 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080085 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
86 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070087 mSinkBufferWidth = sinkWidth;
88 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080089
90 // Pick the buffer format to request from the sink when not rendering to it
91 // with GLES. If the consumer needs CPU access, use the default format
92 // set by the consumer. Otherwise allow gralloc to decide the format based
93 // on usage bits.
94 int sinkUsage;
95 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
96 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
97 int sinkFormat;
98 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
99 mDefaultOutputFormat = sinkFormat;
100 } else {
101 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
102 }
103 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700104
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700105 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.c_str());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700106 mConsumer->setConsumerName(ConsumerBase::mName);
107 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
108 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700109 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -0700110 IGraphicBufferProducer::QueueBufferOutput output;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800111 mSource[SOURCE_SCRATCH]->connect(nullptr, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700112}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700113
114VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700115 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700116}
117
Dan Stoza71433162014-02-04 16:22:36 -0800118status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700119 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700120 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700121 }
Jesse Hall38efe862013-04-06 23:12:29 -0700122
Dan Stoza71433162014-02-04 16:22:36 -0800123 mMustRecompose = mustRecompose;
124
Jesse Hall38efe862013-04-06 23:12:29 -0700125 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700126 "Unexpected beginFrame() in %s state", dbgStateStr());
127 mDbgState = DBG_STATE_BEGUN;
128
Jesse Hall028dc8f2013-08-20 16:35:32 -0700129 return refreshOutputBuffer();
130}
131
132status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700133 if (!mDisplayId) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700134 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700135 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700136
137 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700138 "Unexpected prepareFrame() in %s state", dbgStateStr());
139 mDbgState = DBG_STATE_PREPARED;
140
141 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700142 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400143 // Some hardware can do RGB->YUV conversion more efficiently in hardware
144 // controlled by HWC than in hardware controlled by the video encoder.
145 // Forcing GLES-composed frames to go through an extra copy by the HWC
146 // allows the format conversion to happen there, rather than passing RGB
147 // directly to the consumer.
148 //
149 // On the other hand, when the consumer prefers RGB or can consume RGB
150 // inexpensively, this forces an unnecessary copy.
151 mCompositionType = COMPOSITION_MIXED;
152 }
153
Jesse Hall38efe862013-04-06 23:12:29 -0700154 if (mCompositionType != mDbgLastCompositionType) {
155 VDS_LOGV("prepareFrame: composition type changed to %s",
156 dbgCompositionTypeStr(mCompositionType));
157 mDbgLastCompositionType = mCompositionType;
158 }
159
Jesse Hall1e27ba22013-09-27 09:05:09 -0700160 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800161 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700162 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
163 // We must have just switched from GLES-only to MIXED or HWC
164 // composition. Stop using the format and usage requested by the GLES
165 // driver; they may be suboptimal when HWC is writing to the output
166 // buffer. For example, if the output is going to a video encoder, and
167 // HWC can write directly to YUV, some hardware can skip a
168 // memory-to-memory RGB-to-YUV conversion step.
169 //
170 // If we just switched *to* GLES-only mode, we'll change the
171 // format/usage and get a new buffer when the GLES driver calls
172 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800173 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700174 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
175 refreshOutputBuffer();
176 }
177
Jesse Hall38efe862013-04-06 23:12:29 -0700178 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700179}
180
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700181status_t VirtualDisplaySurface::advanceFrame() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700182 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700183 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700184 }
Jesse Hall38efe862013-04-06 23:12:29 -0700185
186 if (mCompositionType == COMPOSITION_HWC) {
187 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
188 "Unexpected advanceFrame() in %s state on HWC frame",
189 dbgStateStr());
190 } else {
191 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
192 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
193 dbgStateStr());
194 }
195 mDbgState = DBG_STATE_HWC;
196
Jesse Hall14e8b012013-11-07 12:28:26 -0800197 if (mOutputProducerSlot < 0 ||
198 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700199 // Last chance bailout if something bad happened earlier. For example,
200 // in a GLES configuration, if the sink disappears then dequeueBuffer
201 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
202 // will soldier on. So we end up here without a buffer. There should
203 // be lots of scary messages in the log just before this.
204 VDS_LOGE("advanceFrame: no buffer, bailing out");
205 return NO_MEMORY;
206 }
207
Jesse Hall14e8b012013-11-07 12:28:26 -0800208 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800209 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700210 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
211 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
212 mFbProducerSlot, fbBuffer.get(),
213 mOutputProducerSlot, outBuffer.get());
214
Jesse Hallb716e572013-10-01 17:25:20 -0700215 // At this point we know the output buffer acquire fence,
216 // so update HWC state with it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700217 mHwc.setOutputBuffer(*mDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700218
Jesse Hall14e8b012013-11-07 12:28:26 -0800219 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800220 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800221 uint32_t hwcSlot = 0;
222 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800223 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800224 &hwcSlot, &hwcBuffer);
225
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 // TODO: Correctly propagate the dataspace from GL composition
Dominik Laskowski075d3172018-05-24 15:50:06 -0700227 result = mHwc.setClientTarget(*mDisplayId, hwcSlot, mFbFence, hwcBuffer,
228 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800229 }
230
231 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700232}
233
Jesse Hall851cfe82013-03-20 13:44:00 -0700234void VirtualDisplaySurface::onFrameCommitted() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700235 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700236 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700237 }
Jesse Hall38efe862013-04-06 23:12:29 -0700238
239 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
240 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
241 mDbgState = DBG_STATE_IDLE;
242
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243 sp<Fence> retireFence = mHwc.getPresentFence(*mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700244 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
245 // release the scratch buffer back to the pool
246 Mutex::Autolock lock(mMutex);
247 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
248 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800249 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
250 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800251 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700252 }
253
254 if (mOutputProducerSlot >= 0) {
255 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
256 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700257 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800258 if (mMustRecompose) {
259 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
260 QueueBufferInput(
261 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800262 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800263 Rect(mSinkBufferWidth, mSinkBufferHeight),
264 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800266 &qbo);
267 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700268 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800269 }
270 } else {
271 // If the surface hadn't actually been updated, then we only went
272 // through the motions of updating the display to keep our state
273 // machine happy. We cancel the buffer to avoid triggering another
274 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800275 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700276 }
277 }
278
279 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700280}
281
Dan Stoza01049c82014-11-11 10:32:31 -0800282void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700283}
284
Michael Lentine47e45402014-07-18 15:34:25 -0700285void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700286 mQueueBufferOutput.width = w;
287 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700288 mSinkBufferWidth = w;
289 mSinkBufferHeight = h;
290}
291
Dan Stoza9e56aa02015-11-02 13:00:03 -0800292const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
293 return mFbFence;
294}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295
Jesse Hall38efe862013-04-06 23:12:29 -0700296status_t VirtualDisplaySurface::requestBuffer(int pslot,
297 sp<GraphicBuffer>* outBuf) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700299 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 }
Michael Lentine47e45402014-07-18 15:34:25 -0700301
Jesse Hall38efe862013-04-06 23:12:29 -0700302 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
303 "Unexpected requestBuffer pslot=%d in %s state",
304 pslot, dbgStateStr());
305
306 *outBuf = mProducerBuffers[pslot];
307 return NO_ERROR;
308}
309
Pablo Ceballosfa455352015-08-12 17:47:47 -0700310status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
311 int maxDequeuedBuffers) {
312 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
313}
314
315status_t VirtualDisplaySurface::setAsyncMode(bool async) {
316 return mSource[SOURCE_SINK]->setAsyncMode(async);
317}
318
Jesse Hall38efe862013-04-06 23:12:29 -0700319status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700320 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321 LOG_FATAL_IF(!mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700322
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600323 status_t result =
324 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
325 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700326 if (result < 0)
327 return result;
328 int pslot = mapSource2ProducerSlot(source, *sslot);
329 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
330 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700331 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700332
Dan Stozafebd4f42014-04-09 16:14:51 -0700333 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700334 // This slot was previously dequeued from the other source; must
335 // re-request the buffer.
336 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700337 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700338 mProducerSlotSource |= sourceBit;
339 }
340
341 if (result & RELEASE_ALL_BUFFERS) {
342 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700343 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700344 mProducerBuffers[i].clear();
345 }
346 }
347 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700348 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
349 if (result < 0) {
350 mProducerBuffers[pslot].clear();
351 mSource[source]->cancelBuffer(*sslot, *fence);
352 return result;
353 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700354 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800355 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
356 mProducerBuffers[pslot]->getPixelFormat(),
357 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700358 }
359
360 return result;
361}
362
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600363status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
364 PixelFormat format, uint64_t usage,
365 uint64_t* outBufferAge,
366 FrameEventHistoryDelta* outTimestamps) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700367 if (!mDisplayId) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600368 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
369 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700370 }
Michael Lentine47e45402014-07-18 15:34:25 -0700371
Jesse Hall38efe862013-04-06 23:12:29 -0700372 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
373 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
374 mDbgState = DBG_STATE_GLES;
375
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700376 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700377
Jesse Hall028dc8f2013-08-20 16:35:32 -0700378 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700379 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700380
Jesse Hall38efe862013-04-06 23:12:29 -0700381 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700382
383 if (mOutputProducerSlot < 0) {
384 // Last chance bailout if something bad happened earlier. For example,
385 // in a GLES configuration, if the sink disappears then dequeueBuffer
386 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
387 // will soldier on. So we end up here without a buffer. There should
388 // be lots of scary messages in the log just before this.
389 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
390 return NO_MEMORY;
391 }
392
Jesse Hall028dc8f2013-08-20 16:35:32 -0700393 // We already dequeued the output buffer. If the GLES driver wants
394 // something incompatible, we have to cancel and get a new one. This
395 // will mean that HWC will see a different output buffer between
396 // prepare and set, but since we're in GLES-only mode already it
397 // shouldn't matter.
398
Jesse Hall1e27ba22013-09-27 09:05:09 -0700399 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700400 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700401 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800402 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700403 (w != 0 && w != mSinkBufferWidth) ||
404 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700405 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700406 "want %dx%d fmt=%d use=%#" PRIx64 ", "
407 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700408 w, h, format, usage,
409 mSinkBufferWidth, mSinkBufferHeight,
410 buf->getPixelFormat(), buf->getUsage());
411 mOutputFormat = format;
412 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700413 result = refreshOutputBuffer();
414 if (result < 0)
415 return result;
416 }
Jesse Hall38efe862013-04-06 23:12:29 -0700417 }
418
Jesse Hall028dc8f2013-08-20 16:35:32 -0700419 if (source == SOURCE_SINK) {
420 *pslot = mOutputProducerSlot;
421 *fence = mOutputFence;
422 } else {
423 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700424 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700425 if (result >= 0) {
426 *pslot = mapSource2ProducerSlot(source, sslot);
427 }
Jesse Hall38efe862013-04-06 23:12:29 -0700428 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600429 if (outBufferAge) {
430 *outBufferAge = 0;
431 }
Jesse Hall38efe862013-04-06 23:12:29 -0700432 return result;
433}
434
Dan Stoza88a459a2014-03-12 09:34:36 -0700435status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
436 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
437 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800438}
439
Dan Stozad9822a32014-03-28 15:25:31 -0700440status_t VirtualDisplaySurface::detachNextBuffer(
441 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
442 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
443 return INVALID_OPERATION;
444}
445
Dan Stoza88a459a2014-03-12 09:34:36 -0700446status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
447 const sp<GraphicBuffer>& /* buffer */) {
448 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
449 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800450}
451
Jesse Hall38efe862013-04-06 23:12:29 -0700452status_t VirtualDisplaySurface::queueBuffer(int pslot,
453 const QueueBufferInput& input, QueueBufferOutput* output) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700454 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700455 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700456 }
Michael Lentine47e45402014-07-18 15:34:25 -0700457
Jesse Hall38efe862013-04-06 23:12:29 -0700458 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
459 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
460 dbgStateStr());
461 mDbgState = DBG_STATE_GLES_DONE;
462
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 {
488 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
489 "Unexpected queueBuffer in state %s for compositionType %s",
490 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
491
492 // Extract the GLES release fence for HWC to acquire
493 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
Jesse Hall38efe862013-04-06 23:12:29 -0700517 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
518 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
519 dbgStateStr());
520 VDS_LOGV("cancelBuffer pslot=%d", pslot);
521 Source source = fbSourceForCompositionType(mCompositionType);
522 return mSource[source]->cancelBuffer(
523 mapProducer2SourceSlot(source, pslot), fence);
524}
525
526int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700527 switch (what) {
528 case NATIVE_WINDOW_WIDTH:
529 *value = mSinkBufferWidth;
530 break;
531 case NATIVE_WINDOW_HEIGHT:
532 *value = mSinkBufferHeight;
533 break;
534 default:
535 return mSource[SOURCE_SINK]->query(what, value);
536 }
537 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700538}
539
Dan Stozaf0eaf252014-03-21 13:05:51 -0700540status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700541 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700542 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700543 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700544 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
545 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700546 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700547 updateQueueBufferOutput(std::move(qbo));
548 // This moves the frame timestamps and keeps a copy of all other fields.
549 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700550 }
551 return result;
552}
553
Robert Carr97b9c862016-09-08 13:54:35 -0700554status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
555 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700556}
557
Jesse Hall399184a2014-03-03 15:42:54 -0800558status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
559 return INVALID_OPERATION;
560}
561
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700562void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700563 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700564 // TODO: Should we actually allocate buffers for a virtual display?
565}
566
Dan Stoza9de72932015-04-16 17:28:43 -0700567status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
568 return INVALID_OPERATION;
569}
570
Dan Stoza812ed062015-06-02 15:45:22 -0700571status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
572 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
573 return INVALID_OPERATION;
574}
575
Dan Stozac6f30bd2015-06-08 09:32:50 -0700576String8 VirtualDisplaySurface::getConsumerName() const {
577 return String8("VirtualDisplaySurface");
578}
579
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700580status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
581 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800582 return INVALID_OPERATION;
583}
584
585status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
586 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
587 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700588}
589
Dan Stoza127fc632015-06-30 13:43:32 -0700590status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
591 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
592 return INVALID_OPERATION;
593}
594
Dan Stoza50101d02016-04-07 16:53:23 -0700595status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700596 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
597 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700598 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
599 return INVALID_OPERATION;
600}
601
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700602status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
603 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
604 return INVALID_OPERATION;
605}
606
Chia-I Wue2786ea2017-08-07 10:36:08 -0700607status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
608 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
609}
610
Jesse Hall38efe862013-04-06 23:12:29 -0700611void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700612 QueueBufferOutput&& qbo) {
613 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700614 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700615}
616
617void VirtualDisplaySurface::resetPerFrameState() {
618 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530619 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700620 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700621 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530622 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700623}
624
Jesse Hall028dc8f2013-08-20 16:35:32 -0700625status_t VirtualDisplaySurface::refreshOutputBuffer() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700626 LOG_FATAL_IF(!mDisplayId);
627
Jesse Hall028dc8f2013-08-20 16:35:32 -0700628 if (mOutputProducerSlot >= 0) {
629 mSource[SOURCE_SINK]->cancelBuffer(
630 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
631 mOutputFence);
632 }
633
634 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700635 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
636 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700637 if (result < 0)
638 return result;
639 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
640
Jesse Hallb716e572013-10-01 17:25:20 -0700641 // On GLES-only frames, we don't have the right output buffer acquire fence
642 // until after GLES calls queueBuffer(). So here we just set the buffer
643 // (for use in HWC prepare) but not the fence; we'll call this again with
644 // the proper fence once we have it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645 result = mHwc.setOutputBuffer(*mDisplayId, Fence::NO_FENCE,
646 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700647
648 return result;
649}
650
Jesse Hall38efe862013-04-06 23:12:29 -0700651// This slot mapping function is its own inverse, so two copies are unnecessary.
652// Both are kept to make the intent clear where the function is called, and for
653// the (unlikely) chance that we switch to a different mapping function.
654int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
655 if (source == SOURCE_SCRATCH) {
656 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
657 } else {
658 return sslot;
659 }
660}
661int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
662 return mapSource2ProducerSlot(source, pslot);
663}
664
665VirtualDisplaySurface::Source
666VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
667 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
668}
669
670const char* VirtualDisplaySurface::dbgStateStr() const {
671 switch (mDbgState) {
672 case DBG_STATE_IDLE: return "IDLE";
673 case DBG_STATE_PREPARED: return "PREPARED";
674 case DBG_STATE_GLES: return "GLES";
675 case DBG_STATE_GLES_DONE: return "GLES_DONE";
676 case DBG_STATE_HWC: return "HWC";
677 default: return "INVALID";
678 }
679}
680
681const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
682 switch (s) {
683 case SOURCE_SINK: return "SINK";
684 case SOURCE_SCRATCH: return "SCRATCH";
685 default: return "INVALID";
686 }
687}
688
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700689// ---------------------------------------------------------------------------
690} // namespace android
691// ---------------------------------------------------------------------------