blob: c111a27f5103cb066f3ebc97a2a15fdc3870f834 [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
Jesse Hallffe1f192013-03-22 15:13:48 -070051VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070052 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070053 const sp<IGraphicBufferProducer>& bqProducer,
54 const sp<IGraphicBufferConsumer>& bqConsumer,
Dominik Laskowskibf170d92018-04-19 15:08:05 -070055 const std::string& name)
Dan Stozab9b08832014-03-13 11:55:57 -070056: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070057 mHwc(hwc),
58 mDisplayId(dispId),
59 mDisplayName(name),
Dan Stoza9e56aa02015-11-02 13:00:03 -080060 mSource{},
61 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
62 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070063 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070064 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mProducerBuffers(),
66 mQueueBufferOutput(),
67 mSinkBufferWidth(0),
68 mSinkBufferHeight(0),
69 mCompositionType(COMPOSITION_UNKNOWN),
70 mFbFence(Fence::NO_FENCE),
71 mOutputFence(Fence::NO_FENCE),
72 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
73 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070074 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080075 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
Fabien Sanglarda34ed632017-03-14 11:43:52 -070076 mMustRecompose(false),
77 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv)
Jesse Hallffe1f192013-03-22 15:13:48 -070078{
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) {
Jesse Hall38efe862013-04-06 23:12:29 -0700119 if (mDisplayId < 0)
120 return NO_ERROR;
121
Dan Stoza71433162014-02-04 16:22:36 -0800122 mMustRecompose = mustRecompose;
123
Jesse Hall38efe862013-04-06 23:12:29 -0700124 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700125 "Unexpected beginFrame() in %s state", dbgStateStr());
126 mDbgState = DBG_STATE_BEGUN;
127
Jesse Hall028dc8f2013-08-20 16:35:32 -0700128 return refreshOutputBuffer();
129}
130
131status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
132 if (mDisplayId < 0)
133 return NO_ERROR;
134
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() {
Jesse Hall38efe862013-04-06 23:12:29 -0700180 if (mDisplayId < 0)
181 return NO_ERROR;
182
183 if (mCompositionType == COMPOSITION_HWC) {
184 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
185 "Unexpected advanceFrame() in %s state on HWC frame",
186 dbgStateStr());
187 } else {
188 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
189 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
190 dbgStateStr());
191 }
192 mDbgState = DBG_STATE_HWC;
193
Jesse Hall14e8b012013-11-07 12:28:26 -0800194 if (mOutputProducerSlot < 0 ||
195 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700196 // Last chance bailout if something bad happened earlier. For example,
197 // in a GLES configuration, if the sink disappears then dequeueBuffer
198 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
199 // will soldier on. So we end up here without a buffer. There should
200 // be lots of scary messages in the log just before this.
201 VDS_LOGE("advanceFrame: no buffer, bailing out");
202 return NO_MEMORY;
203 }
204
Jesse Hall14e8b012013-11-07 12:28:26 -0800205 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
Peiyong Lin566a3b42018-01-09 18:22:43 -0800206 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700207 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
208 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
209 mFbProducerSlot, fbBuffer.get(),
210 mOutputProducerSlot, outBuffer.get());
211
Jesse Hallb716e572013-10-01 17:25:20 -0700212 // At this point we know the output buffer acquire fence,
213 // so update HWC state with it.
214 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
215
Jesse Hall14e8b012013-11-07 12:28:26 -0800216 status_t result = NO_ERROR;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800217 if (fbBuffer != nullptr) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800218 uint32_t hwcSlot = 0;
219 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800220 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800221 &hwcSlot, &hwcBuffer);
222
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223 // TODO: Correctly propagate the dataspace from GL composition
Chia-I Wu06d63de2017-01-04 14:58:51 +0800224 result = mHwc.setClientTarget(mDisplayId, hwcSlot, mFbFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700225 hwcBuffer, ui::Dataspace::UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800226 }
227
228 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700229}
230
Jesse Hall851cfe82013-03-20 13:44:00 -0700231void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700232 if (mDisplayId < 0)
233 return;
234
235 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
236 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
237 mDbgState = DBG_STATE_IDLE;
238
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800239 sp<Fence> retireFence = mHwc.getPresentFence(mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700240 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
241 // release the scratch buffer back to the pool
242 Mutex::Autolock lock(mMutex);
243 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
244 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
246 retireFence);
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800247 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot]);
Jesse Hall38efe862013-04-06 23:12:29 -0700248 }
249
250 if (mOutputProducerSlot >= 0) {
251 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
252 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700253 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800254 if (mMustRecompose) {
255 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
256 QueueBufferInput(
257 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800258 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800259 Rect(mSinkBufferWidth, mSinkBufferHeight),
260 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800262 &qbo);
263 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700264 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800265 }
266 } else {
267 // If the surface hadn't actually been updated, then we only went
268 // through the motions of updating the display to keep our state
269 // machine happy. We cancel the buffer to avoid triggering another
270 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800271 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700272 }
273 }
274
275 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700276}
277
Dan Stoza01049c82014-11-11 10:32:31 -0800278void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700279}
280
Michael Lentine47e45402014-07-18 15:34:25 -0700281void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700282 mQueueBufferOutput.width = w;
283 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700284 mSinkBufferWidth = w;
285 mSinkBufferHeight = h;
286}
287
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
289 return mFbFence;
290}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800291
Jesse Hall38efe862013-04-06 23:12:29 -0700292status_t VirtualDisplaySurface::requestBuffer(int pslot,
293 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700294 if (mDisplayId < 0)
295 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
296
Jesse Hall38efe862013-04-06 23:12:29 -0700297 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
298 "Unexpected requestBuffer pslot=%d in %s state",
299 pslot, dbgStateStr());
300
301 *outBuf = mProducerBuffers[pslot];
302 return NO_ERROR;
303}
304
Pablo Ceballosfa455352015-08-12 17:47:47 -0700305status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
306 int maxDequeuedBuffers) {
307 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
308}
309
310status_t VirtualDisplaySurface::setAsyncMode(bool async) {
311 return mSource[SOURCE_SINK]->setAsyncMode(async);
312}
313
Jesse Hall38efe862013-04-06 23:12:29 -0700314status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700315 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700316 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700317
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600318 status_t result =
319 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
320 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700321 if (result < 0)
322 return result;
323 int pslot = mapSource2ProducerSlot(source, *sslot);
324 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
325 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700326 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700327
Dan Stozafebd4f42014-04-09 16:14:51 -0700328 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700329 // This slot was previously dequeued from the other source; must
330 // re-request the buffer.
331 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700332 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700333 mProducerSlotSource |= sourceBit;
334 }
335
336 if (result & RELEASE_ALL_BUFFERS) {
337 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700338 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700339 mProducerBuffers[i].clear();
340 }
341 }
342 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700343 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
344 if (result < 0) {
345 mProducerBuffers[pslot].clear();
346 mSource[source]->cancelBuffer(*sslot, *fence);
347 return result;
348 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700349 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800350 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
351 mProducerBuffers[pslot]->getPixelFormat(),
352 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700353 }
354
355 return result;
356}
357
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600358status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
359 PixelFormat format, uint64_t usage,
360 uint64_t* outBufferAge,
361 FrameEventHistoryDelta* outTimestamps) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700362 if (mDisplayId < 0) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600363 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
364 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700365 }
Michael Lentine47e45402014-07-18 15:34:25 -0700366
Jesse Hall38efe862013-04-06 23:12:29 -0700367 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
368 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
369 mDbgState = DBG_STATE_GLES;
370
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700371 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700372
Jesse Hall028dc8f2013-08-20 16:35:32 -0700373 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700374 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700375
Jesse Hall38efe862013-04-06 23:12:29 -0700376 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700377
378 if (mOutputProducerSlot < 0) {
379 // Last chance bailout if something bad happened earlier. For example,
380 // in a GLES configuration, if the sink disappears then dequeueBuffer
381 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
382 // will soldier on. So we end up here without a buffer. There should
383 // be lots of scary messages in the log just before this.
384 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
385 return NO_MEMORY;
386 }
387
Jesse Hall028dc8f2013-08-20 16:35:32 -0700388 // We already dequeued the output buffer. If the GLES driver wants
389 // something incompatible, we have to cancel and get a new one. This
390 // will mean that HWC will see a different output buffer between
391 // prepare and set, but since we're in GLES-only mode already it
392 // shouldn't matter.
393
Jesse Hall1e27ba22013-09-27 09:05:09 -0700394 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700395 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700396 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800397 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 (w != 0 && w != mSinkBufferWidth) ||
399 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700400 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700401 "want %dx%d fmt=%d use=%#" PRIx64 ", "
402 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700403 w, h, format, usage,
404 mSinkBufferWidth, mSinkBufferHeight,
405 buf->getPixelFormat(), buf->getUsage());
406 mOutputFormat = format;
407 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700408 result = refreshOutputBuffer();
409 if (result < 0)
410 return result;
411 }
Jesse Hall38efe862013-04-06 23:12:29 -0700412 }
413
Jesse Hall028dc8f2013-08-20 16:35:32 -0700414 if (source == SOURCE_SINK) {
415 *pslot = mOutputProducerSlot;
416 *fence = mOutputFence;
417 } else {
418 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700419 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700420 if (result >= 0) {
421 *pslot = mapSource2ProducerSlot(source, sslot);
422 }
Jesse Hall38efe862013-04-06 23:12:29 -0700423 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600424 if (outBufferAge) {
425 *outBufferAge = 0;
426 }
Jesse Hall38efe862013-04-06 23:12:29 -0700427 return result;
428}
429
Dan Stoza88a459a2014-03-12 09:34:36 -0700430status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
431 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
432 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800433}
434
Dan Stozad9822a32014-03-28 15:25:31 -0700435status_t VirtualDisplaySurface::detachNextBuffer(
436 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
437 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
438 return INVALID_OPERATION;
439}
440
Dan Stoza88a459a2014-03-12 09:34:36 -0700441status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
442 const sp<GraphicBuffer>& /* buffer */) {
443 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
444 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800445}
446
Jesse Hall38efe862013-04-06 23:12:29 -0700447status_t VirtualDisplaySurface::queueBuffer(int pslot,
448 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700449 if (mDisplayId < 0)
450 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
451
Jesse Hall38efe862013-04-06 23:12:29 -0700452 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
453 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
454 dbgStateStr());
455 mDbgState = DBG_STATE_GLES_DONE;
456
457 VDS_LOGV("queueBuffer pslot=%d", pslot);
458
459 status_t result;
460 if (mCompositionType == COMPOSITION_MIXED) {
461 // Queue the buffer back into the scratch pool
462 QueueBufferOutput scratchQBO;
463 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700464 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700465 if (result != NO_ERROR)
466 return result;
467
468 // Now acquire the buffer from the scratch pool -- should be the same
469 // slot and fence as we just queued.
470 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700471 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700472 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700473 if (result != NO_ERROR)
474 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700475 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700476 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700477 item.mSlot, sslot);
478 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
479 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700480
481 } else {
482 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
483 "Unexpected queueBuffer in state %s for compositionType %s",
484 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
485
486 // Extract the GLES release fence for HWC to acquire
487 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700488 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800489 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700490 Rect crop;
491 int scalingMode;
492 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800493 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700494 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700495
496 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700497 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700498 }
499
Brian Anderson3d4039d2016-09-23 16:31:30 -0700500 // This moves the frame timestamps and keeps a copy of all other fields.
501 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700502 return NO_ERROR;
503}
504
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700505status_t VirtualDisplaySurface::cancelBuffer(int pslot,
506 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700507 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700508 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700509
Jesse Hall38efe862013-04-06 23:12:29 -0700510 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
511 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
512 dbgStateStr());
513 VDS_LOGV("cancelBuffer pslot=%d", pslot);
514 Source source = fbSourceForCompositionType(mCompositionType);
515 return mSource[source]->cancelBuffer(
516 mapProducer2SourceSlot(source, pslot), fence);
517}
518
519int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700520 switch (what) {
521 case NATIVE_WINDOW_WIDTH:
522 *value = mSinkBufferWidth;
523 break;
524 case NATIVE_WINDOW_HEIGHT:
525 *value = mSinkBufferHeight;
526 break;
527 default:
528 return mSource[SOURCE_SINK]->query(what, value);
529 }
530 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700531}
532
Dan Stozaf0eaf252014-03-21 13:05:51 -0700533status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700534 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700535 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700536 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700537 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
538 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700539 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700540 updateQueueBufferOutput(std::move(qbo));
541 // This moves the frame timestamps and keeps a copy of all other fields.
542 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700543 }
544 return result;
545}
546
Robert Carr97b9c862016-09-08 13:54:35 -0700547status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
548 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700549}
550
Jesse Hall399184a2014-03-03 15:42:54 -0800551status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
552 return INVALID_OPERATION;
553}
554
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700555void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700556 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700557 // TODO: Should we actually allocate buffers for a virtual display?
558}
559
Dan Stoza9de72932015-04-16 17:28:43 -0700560status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
561 return INVALID_OPERATION;
562}
563
Dan Stoza812ed062015-06-02 15:45:22 -0700564status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
565 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
566 return INVALID_OPERATION;
567}
568
Dan Stozac6f30bd2015-06-08 09:32:50 -0700569String8 VirtualDisplaySurface::getConsumerName() const {
570 return String8("VirtualDisplaySurface");
571}
572
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700573status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
574 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800575 return INVALID_OPERATION;
576}
577
578status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
579 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
580 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700581}
582
Dan Stoza127fc632015-06-30 13:43:32 -0700583status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
584 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
585 return INVALID_OPERATION;
586}
587
Dan Stoza50101d02016-04-07 16:53:23 -0700588status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700589 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
590 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700591 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
592 return INVALID_OPERATION;
593}
594
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700595status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
596 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
597 return INVALID_OPERATION;
598}
599
Chia-I Wue2786ea2017-08-07 10:36:08 -0700600status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
601 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
602}
603
Jesse Hall38efe862013-04-06 23:12:29 -0700604void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700605 QueueBufferOutput&& qbo) {
606 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700607 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700608}
609
610void VirtualDisplaySurface::resetPerFrameState() {
611 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530612 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700613 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700614 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530615 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700616}
617
Jesse Hall028dc8f2013-08-20 16:35:32 -0700618status_t VirtualDisplaySurface::refreshOutputBuffer() {
619 if (mOutputProducerSlot >= 0) {
620 mSource[SOURCE_SINK]->cancelBuffer(
621 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
622 mOutputFence);
623 }
624
625 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700626 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
627 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700628 if (result < 0)
629 return result;
630 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
631
Jesse Hallb716e572013-10-01 17:25:20 -0700632 // On GLES-only frames, we don't have the right output buffer acquire fence
633 // until after GLES calls queueBuffer(). So here we just set the buffer
634 // (for use in HWC prepare) but not the fence; we'll call this again with
635 // the proper fence once we have it.
636 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700637 mProducerBuffers[mOutputProducerSlot]);
638
639 return result;
640}
641
Jesse Hall38efe862013-04-06 23:12:29 -0700642// This slot mapping function is its own inverse, so two copies are unnecessary.
643// Both are kept to make the intent clear where the function is called, and for
644// the (unlikely) chance that we switch to a different mapping function.
645int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
646 if (source == SOURCE_SCRATCH) {
647 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
648 } else {
649 return sslot;
650 }
651}
652int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
653 return mapSource2ProducerSlot(source, pslot);
654}
655
656VirtualDisplaySurface::Source
657VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
658 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
659}
660
661const char* VirtualDisplaySurface::dbgStateStr() const {
662 switch (mDbgState) {
663 case DBG_STATE_IDLE: return "IDLE";
664 case DBG_STATE_PREPARED: return "PREPARED";
665 case DBG_STATE_GLES: return "GLES";
666 case DBG_STATE_GLES_DONE: return "GLES_DONE";
667 case DBG_STATE_HWC: return "HWC";
668 default: return "INVALID";
669 }
670}
671
672const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
673 switch (s) {
674 case SOURCE_SINK: return "SINK";
675 case SOURCE_SCRATCH: return "SCRATCH";
676 default: return "INVALID";
677 }
678}
679
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700680// ---------------------------------------------------------------------------
681} // namespace android
682// ---------------------------------------------------------------------------