blob: 5f3fcd6e88981f892613c89b172ae661419c8c14 [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);
Jesse Hallffe1f192013-03-22 15:13:48 -0700110}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700111
112VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700113 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700114}
115
Dan Stoza71433162014-02-04 16:22:36 -0800116status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700117 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700118 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700119 }
Jesse Hall38efe862013-04-06 23:12:29 -0700120
Dan Stoza71433162014-02-04 16:22:36 -0800121 mMustRecompose = mustRecompose;
122
Jesse Hall38efe862013-04-06 23:12:29 -0700123 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700124 "Unexpected beginFrame() in %s state", dbgStateStr());
125 mDbgState = DBG_STATE_BEGUN;
126
Jesse Hall028dc8f2013-08-20 16:35:32 -0700127 return refreshOutputBuffer();
128}
129
130status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700131 if (!mDisplayId) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700132 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700133 }
Jesse Hall028dc8f2013-08-20 16:35:32 -0700134
135 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700136 "Unexpected prepareFrame() in %s state", dbgStateStr());
137 mDbgState = DBG_STATE_PREPARED;
138
139 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700140 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400141 // Some hardware can do RGB->YUV conversion more efficiently in hardware
142 // controlled by HWC than in hardware controlled by the video encoder.
143 // Forcing GLES-composed frames to go through an extra copy by the HWC
144 // allows the format conversion to happen there, rather than passing RGB
145 // directly to the consumer.
146 //
147 // On the other hand, when the consumer prefers RGB or can consume RGB
148 // inexpensively, this forces an unnecessary copy.
149 mCompositionType = COMPOSITION_MIXED;
150 }
151
Jesse Hall38efe862013-04-06 23:12:29 -0700152 if (mCompositionType != mDbgLastCompositionType) {
153 VDS_LOGV("prepareFrame: composition type changed to %s",
154 dbgCompositionTypeStr(mCompositionType));
155 mDbgLastCompositionType = mCompositionType;
156 }
157
Jesse Hall1e27ba22013-09-27 09:05:09 -0700158 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800159 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700160 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
161 // We must have just switched from GLES-only to MIXED or HWC
162 // composition. Stop using the format and usage requested by the GLES
163 // driver; they may be suboptimal when HWC is writing to the output
164 // buffer. For example, if the output is going to a video encoder, and
165 // HWC can write directly to YUV, some hardware can skip a
166 // memory-to-memory RGB-to-YUV conversion step.
167 //
168 // If we just switched *to* GLES-only mode, we'll change the
169 // format/usage and get a new buffer when the GLES driver calls
170 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800171 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700172 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
173 refreshOutputBuffer();
174 }
175
Jesse Hall38efe862013-04-06 23:12:29 -0700176 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700177}
178
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700179status_t VirtualDisplaySurface::advanceFrame() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700180 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700181 return NO_ERROR;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700182 }
Jesse Hall38efe862013-04-06 23:12:29 -0700183
184 if (mCompositionType == COMPOSITION_HWC) {
185 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
186 "Unexpected advanceFrame() in %s state on HWC frame",
187 dbgStateStr());
188 } else {
189 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
190 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
191 dbgStateStr());
192 }
193 mDbgState = DBG_STATE_HWC;
194
Jesse Hall14e8b012013-11-07 12:28:26 -0800195 if (mOutputProducerSlot < 0 ||
196 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700197 // Last chance bailout if something bad happened earlier. For example,
198 // in a GLES configuration, if the sink disappears then dequeueBuffer
199 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
200 // will soldier on. So we end up here without a buffer. There should
201 // be lots of scary messages in the log just before this.
202 VDS_LOGE("advanceFrame: no buffer, bailing out");
203 return NO_MEMORY;
204 }
205
Jesse Hall14e8b012013-11-07 12:28:26 -0800206 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800207 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700208 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
209 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
210 mFbProducerSlot, fbBuffer.get(),
211 mOutputProducerSlot, outBuffer.get());
212
Jesse Hallb716e572013-10-01 17:25:20 -0700213 // At this point we know the output buffer acquire fence,
214 // so update HWC state with it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215 mHwc.setOutputBuffer(*mDisplayId, mOutputFence, outBuffer);
Jesse Hallb716e572013-10-01 17:25:20 -0700216
Jesse Hall14e8b012013-11-07 12:28:26 -0800217 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800218 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800219 uint32_t hwcSlot = 0;
220 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800221 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800222 &hwcSlot, &hwcBuffer);
223
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 // TODO: Correctly propagate the dataspace from GL composition
Dominik Laskowski075d3172018-05-24 15:50:06 -0700225 result = mHwc.setClientTarget(*mDisplayId, hwcSlot, mFbFence, hwcBuffer,
226 ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800227 }
228
229 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700230}
231
Jesse Hall851cfe82013-03-20 13:44:00 -0700232void VirtualDisplaySurface::onFrameCommitted() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700233 if (!mDisplayId) {
Jesse Hall38efe862013-04-06 23:12:29 -0700234 return;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700235 }
Jesse Hall38efe862013-04-06 23:12:29 -0700236
237 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
238 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
239 mDbgState = DBG_STATE_IDLE;
240
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 sp<Fence> retireFence = mHwc.getPresentFence(*mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700242 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
243 // release the scratch buffer back to the pool
244 Mutex::Autolock lock(mMutex);
245 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
246 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
248 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800249 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700250 }
251
252 if (mOutputProducerSlot >= 0) {
253 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
254 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700255 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800256 if (mMustRecompose) {
257 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
258 QueueBufferInput(
259 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800260 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800261 Rect(mSinkBufferWidth, mSinkBufferHeight),
262 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800263 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800264 &qbo);
265 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700266 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800267 }
268 } else {
269 // If the surface hadn't actually been updated, then we only went
270 // through the motions of updating the display to keep our state
271 // machine happy. We cancel the buffer to avoid triggering another
272 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800273 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700274 }
275 }
276
277 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700278}
279
Dan Stoza01049c82014-11-11 10:32:31 -0800280void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700281}
282
Michael Lentine47e45402014-07-18 15:34:25 -0700283void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700284 mQueueBufferOutput.width = w;
285 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700286 mSinkBufferWidth = w;
287 mSinkBufferHeight = h;
288}
289
Dan Stoza9e56aa02015-11-02 13:00:03 -0800290const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
291 return mFbFence;
292}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800293
Jesse Hall38efe862013-04-06 23:12:29 -0700294status_t VirtualDisplaySurface::requestBuffer(int pslot,
295 sp<GraphicBuffer>* outBuf) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700297 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 }
Michael Lentine47e45402014-07-18 15:34:25 -0700299
Jesse Hall38efe862013-04-06 23:12:29 -0700300 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
301 "Unexpected requestBuffer pslot=%d in %s state",
302 pslot, dbgStateStr());
303
304 *outBuf = mProducerBuffers[pslot];
305 return NO_ERROR;
306}
307
Pablo Ceballosfa455352015-08-12 17:47:47 -0700308status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
309 int maxDequeuedBuffers) {
310 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
311}
312
313status_t VirtualDisplaySurface::setAsyncMode(bool async) {
314 return mSource[SOURCE_SINK]->setAsyncMode(async);
315}
316
Jesse Hall38efe862013-04-06 23:12:29 -0700317status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700318 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 LOG_FATAL_IF(!mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700320
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600321 status_t result =
322 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
323 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700324 if (result < 0)
325 return result;
326 int pslot = mapSource2ProducerSlot(source, *sslot);
327 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
328 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700329 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700330
Dan Stozafebd4f42014-04-09 16:14:51 -0700331 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700332 // This slot was previously dequeued from the other source; must
333 // re-request the buffer.
334 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700335 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700336 mProducerSlotSource |= sourceBit;
337 }
338
339 if (result & RELEASE_ALL_BUFFERS) {
340 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700341 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700342 mProducerBuffers[i].clear();
343 }
344 }
345 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700346 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
347 if (result < 0) {
348 mProducerBuffers[pslot].clear();
349 mSource[source]->cancelBuffer(*sslot, *fence);
350 return result;
351 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700352 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800353 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
354 mProducerBuffers[pslot]->getPixelFormat(),
355 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700356 }
357
358 return result;
359}
360
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600361status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
362 PixelFormat format, uint64_t usage,
363 uint64_t* outBufferAge,
364 FrameEventHistoryDelta* outTimestamps) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365 if (!mDisplayId) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600366 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
367 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700368 }
Michael Lentine47e45402014-07-18 15:34:25 -0700369
Jesse Hall38efe862013-04-06 23:12:29 -0700370 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
371 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
372 mDbgState = DBG_STATE_GLES;
373
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700374 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700375
Jesse Hall028dc8f2013-08-20 16:35:32 -0700376 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700377 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700378
Jesse Hall38efe862013-04-06 23:12:29 -0700379 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700380
381 if (mOutputProducerSlot < 0) {
382 // Last chance bailout if something bad happened earlier. For example,
383 // in a GLES configuration, if the sink disappears then dequeueBuffer
384 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
385 // will soldier on. So we end up here without a buffer. There should
386 // be lots of scary messages in the log just before this.
387 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
388 return NO_MEMORY;
389 }
390
Jesse Hall028dc8f2013-08-20 16:35:32 -0700391 // We already dequeued the output buffer. If the GLES driver wants
392 // something incompatible, we have to cancel and get a new one. This
393 // will mean that HWC will see a different output buffer between
394 // prepare and set, but since we're in GLES-only mode already it
395 // shouldn't matter.
396
Jesse Hall1e27ba22013-09-27 09:05:09 -0700397 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700399 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800400 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700401 (w != 0 && w != mSinkBufferWidth) ||
402 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700403 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700404 "want %dx%d fmt=%d use=%#" PRIx64 ", "
405 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700406 w, h, format, usage,
407 mSinkBufferWidth, mSinkBufferHeight,
408 buf->getPixelFormat(), buf->getUsage());
409 mOutputFormat = format;
410 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700411 result = refreshOutputBuffer();
412 if (result < 0)
413 return result;
414 }
Jesse Hall38efe862013-04-06 23:12:29 -0700415 }
416
Jesse Hall028dc8f2013-08-20 16:35:32 -0700417 if (source == SOURCE_SINK) {
418 *pslot = mOutputProducerSlot;
419 *fence = mOutputFence;
420 } else {
421 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700422 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700423 if (result >= 0) {
424 *pslot = mapSource2ProducerSlot(source, sslot);
425 }
Jesse Hall38efe862013-04-06 23:12:29 -0700426 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600427 if (outBufferAge) {
428 *outBufferAge = 0;
429 }
Jesse Hall38efe862013-04-06 23:12:29 -0700430 return result;
431}
432
Dan Stoza88a459a2014-03-12 09:34:36 -0700433status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
434 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
435 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800436}
437
Dan Stozad9822a32014-03-28 15:25:31 -0700438status_t VirtualDisplaySurface::detachNextBuffer(
439 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
440 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
441 return INVALID_OPERATION;
442}
443
Dan Stoza88a459a2014-03-12 09:34:36 -0700444status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
445 const sp<GraphicBuffer>& /* buffer */) {
446 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
447 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800448}
449
Jesse Hall38efe862013-04-06 23:12:29 -0700450status_t VirtualDisplaySurface::queueBuffer(int pslot,
451 const QueueBufferInput& input, QueueBufferOutput* output) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700452 if (!mDisplayId) {
Michael Lentine47e45402014-07-18 15:34:25 -0700453 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700454 }
Michael Lentine47e45402014-07-18 15:34:25 -0700455
Jesse Hall38efe862013-04-06 23:12:29 -0700456 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
457 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
458 dbgStateStr());
459 mDbgState = DBG_STATE_GLES_DONE;
460
461 VDS_LOGV("queueBuffer pslot=%d", pslot);
462
463 status_t result;
464 if (mCompositionType == COMPOSITION_MIXED) {
465 // Queue the buffer back into the scratch pool
466 QueueBufferOutput scratchQBO;
467 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700468 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700469 if (result != NO_ERROR)
470 return result;
471
472 // Now acquire the buffer from the scratch pool -- should be the same
473 // slot and fence as we just queued.
474 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700475 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700476 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700477 if (result != NO_ERROR)
478 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700479 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700480 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700481 item.mSlot, sslot);
482 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
483 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700484
485 } else {
486 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
487 "Unexpected queueBuffer in state %s for compositionType %s",
488 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
489
490 // Extract the GLES release fence for HWC to acquire
491 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700492 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800493 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700494 Rect crop;
495 int scalingMode;
496 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800497 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700498 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700499
500 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700501 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700502 }
503
Brian Anderson3d4039d2016-09-23 16:31:30 -0700504 // This moves the frame timestamps and keeps a copy of all other fields.
505 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700506 return NO_ERROR;
507}
508
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700509status_t VirtualDisplaySurface::cancelBuffer(int pslot,
510 const sp<Fence>& fence) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700511 if (!mDisplayId) {
Michael Lentinefd9d1832014-07-30 15:39:17 -0700512 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700513 }
Michael Lentine47e45402014-07-18 15:34:25 -0700514
Jesse Hall38efe862013-04-06 23:12:29 -0700515 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
516 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
517 dbgStateStr());
518 VDS_LOGV("cancelBuffer pslot=%d", pslot);
519 Source source = fbSourceForCompositionType(mCompositionType);
520 return mSource[source]->cancelBuffer(
521 mapProducer2SourceSlot(source, pslot), fence);
522}
523
524int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700525 switch (what) {
526 case NATIVE_WINDOW_WIDTH:
527 *value = mSinkBufferWidth;
528 break;
529 case NATIVE_WINDOW_HEIGHT:
530 *value = mSinkBufferHeight;
531 break;
532 default:
533 return mSource[SOURCE_SINK]->query(what, value);
534 }
535 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700536}
537
Dan Stozaf0eaf252014-03-21 13:05:51 -0700538status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700539 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700540 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700541 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700542 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
543 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700544 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700545 updateQueueBufferOutput(std::move(qbo));
546 // This moves the frame timestamps and keeps a copy of all other fields.
547 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700548 }
549 return result;
550}
551
Robert Carr97b9c862016-09-08 13:54:35 -0700552status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
553 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700554}
555
Jesse Hall399184a2014-03-03 15:42:54 -0800556status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
557 return INVALID_OPERATION;
558}
559
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700560void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700561 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700562 // TODO: Should we actually allocate buffers for a virtual display?
563}
564
Dan Stoza9de72932015-04-16 17:28:43 -0700565status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
566 return INVALID_OPERATION;
567}
568
Dan Stoza812ed062015-06-02 15:45:22 -0700569status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
570 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
571 return INVALID_OPERATION;
572}
573
Dan Stozac6f30bd2015-06-08 09:32:50 -0700574String8 VirtualDisplaySurface::getConsumerName() const {
575 return String8("VirtualDisplaySurface");
576}
577
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700578status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
579 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800580 return INVALID_OPERATION;
581}
582
583status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
584 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
585 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700586}
587
Dan Stoza127fc632015-06-30 13:43:32 -0700588status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
589 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
590 return INVALID_OPERATION;
591}
592
Dan Stoza50101d02016-04-07 16:53:23 -0700593status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700594 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
595 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700596 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
597 return INVALID_OPERATION;
598}
599
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700600status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
601 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
602 return INVALID_OPERATION;
603}
604
Chia-I Wue2786ea2017-08-07 10:36:08 -0700605status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
606 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
607}
608
Jesse Hall38efe862013-04-06 23:12:29 -0700609void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700610 QueueBufferOutput&& qbo) {
611 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700612 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700613}
614
615void VirtualDisplaySurface::resetPerFrameState() {
616 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530617 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700618 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700619 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530620 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700621}
622
Jesse Hall028dc8f2013-08-20 16:35:32 -0700623status_t VirtualDisplaySurface::refreshOutputBuffer() {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700624 LOG_FATAL_IF(!mDisplayId);
625
Jesse Hall028dc8f2013-08-20 16:35:32 -0700626 if (mOutputProducerSlot >= 0) {
627 mSource[SOURCE_SINK]->cancelBuffer(
628 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
629 mOutputFence);
630 }
631
632 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700633 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
634 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700635 if (result < 0)
636 return result;
637 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
638
Jesse Hallb716e572013-10-01 17:25:20 -0700639 // On GLES-only frames, we don't have the right output buffer acquire fence
640 // until after GLES calls queueBuffer(). So here we just set the buffer
641 // (for use in HWC prepare) but not the fence; we'll call this again with
642 // the proper fence once we have it.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700643 result = mHwc.setOutputBuffer(*mDisplayId, Fence::NO_FENCE,
644 mProducerBuffers[mOutputProducerSlot]);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700645
646 return result;
647}
648
Jesse Hall38efe862013-04-06 23:12:29 -0700649// This slot mapping function is its own inverse, so two copies are unnecessary.
650// Both are kept to make the intent clear where the function is called, and for
651// the (unlikely) chance that we switch to a different mapping function.
652int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
653 if (source == SOURCE_SCRATCH) {
654 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
655 } else {
656 return sslot;
657 }
658}
659int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
660 return mapSource2ProducerSlot(source, pslot);
661}
662
663VirtualDisplaySurface::Source
664VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
665 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
666}
667
668const char* VirtualDisplaySurface::dbgStateStr() const {
669 switch (mDbgState) {
670 case DBG_STATE_IDLE: return "IDLE";
671 case DBG_STATE_PREPARED: return "PREPARED";
672 case DBG_STATE_GLES: return "GLES";
673 case DBG_STATE_GLES_DONE: return "GLES_DONE";
674 case DBG_STATE_HWC: return "HWC";
675 default: return "INVALID";
676 }
677}
678
679const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
680 switch (s) {
681 case SOURCE_SINK: return "SINK";
682 case SOURCE_SCRATCH: return "SCRATCH";
683 default: return "INVALID";
684 }
685}
686
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700687// ---------------------------------------------------------------------------
688} // namespace android
689// ---------------------------------------------------------------------------