blob: 34f1cecfdebec053151825884d9cc561d650fa00 [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, \
Jesse Hall38efe862013-04-06 23:12:29 -070035 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070036#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070037 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070038#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070039 mDisplayName.string(), ##__VA_ARGS__)
40
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,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055 const String8& 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
105 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
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;
111 mSource[SOURCE_SCRATCH]->connect(NULL, 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
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000179#ifndef USE_HWC2
180status_t VirtualDisplaySurface::compositionComplete() {
181 return NO_ERROR;
182}
183#endif
184
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700185status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700186 if (mDisplayId < 0)
187 return NO_ERROR;
188
189 if (mCompositionType == COMPOSITION_HWC) {
190 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
191 "Unexpected advanceFrame() in %s state on HWC frame",
192 dbgStateStr());
193 } else {
194 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
195 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
196 dbgStateStr());
197 }
198 mDbgState = DBG_STATE_HWC;
199
Jesse Hall14e8b012013-11-07 12:28:26 -0800200 if (mOutputProducerSlot < 0 ||
201 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700202 // Last chance bailout if something bad happened earlier. For example,
203 // in a GLES configuration, if the sink disappears then dequeueBuffer
204 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
205 // will soldier on. So we end up here without a buffer. There should
206 // be lots of scary messages in the log just before this.
207 VDS_LOGE("advanceFrame: no buffer, bailing out");
208 return NO_MEMORY;
209 }
210
Jesse Hall14e8b012013-11-07 12:28:26 -0800211 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
212 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700213 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
214 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
215 mFbProducerSlot, fbBuffer.get(),
216 mOutputProducerSlot, outBuffer.get());
217
Jesse Hallb716e572013-10-01 17:25:20 -0700218 // At this point we know the output buffer acquire fence,
219 // so update HWC state with it.
220 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
221
Jesse Hall14e8b012013-11-07 12:28:26 -0800222 status_t result = NO_ERROR;
223 if (fbBuffer != NULL) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000224#ifdef USE_HWC2
Chia-I Wu06d63de2017-01-04 14:58:51 +0800225 uint32_t hwcSlot = 0;
226 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800227 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800228 &hwcSlot, &hwcBuffer);
229
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230 // TODO: Correctly propagate the dataspace from GL composition
Chia-I Wu06d63de2017-01-04 14:58:51 +0800231 result = mHwc.setClientTarget(mDisplayId, hwcSlot, mFbFence,
232 hwcBuffer, HAL_DATASPACE_UNKNOWN);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000233#else
234 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
235#endif
Jesse Hall14e8b012013-11-07 12:28:26 -0800236 }
237
238 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700239}
240
Jesse Hall851cfe82013-03-20 13:44:00 -0700241void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700242 if (mDisplayId < 0)
243 return;
244
245 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
246 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
247 mDbgState = DBG_STATE_IDLE;
248
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000249#ifdef USE_HWC2
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800250 sp<Fence> retireFence = mHwc.getPresentFence(mDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000251#else
252 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
253#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700254 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
255 // release the scratch buffer back to the pool
256 Mutex::Autolock lock(mMutex);
257 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
258 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000259#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
261 retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000262#else
263 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
264#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700265 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
266 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
267 }
268
269 if (mOutputProducerSlot >= 0) {
270 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
271 QueueBufferOutput qbo;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000272#ifndef USE_HWC2
273 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
274#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700275 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800276 if (mMustRecompose) {
277 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
278 QueueBufferInput(
279 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800280 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800281 Rect(mSinkBufferWidth, mSinkBufferHeight),
282 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000283#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 retireFence),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000285#else
286 outFence),
287#endif
Dan Stoza71433162014-02-04 16:22:36 -0800288 &qbo);
289 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700290 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800291 }
292 } else {
293 // If the surface hadn't actually been updated, then we only went
294 // through the motions of updating the display to keep our state
295 // machine happy. We cancel the buffer to avoid triggering another
296 // re-composition and causing an infinite loop.
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000297#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800298 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000299#else
300 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
301#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700302 }
303 }
304
305 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700306}
307
Dan Stoza01049c82014-11-11 10:32:31 -0800308void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700309}
310
Michael Lentine47e45402014-07-18 15:34:25 -0700311void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700312 mQueueBufferOutput.width = w;
313 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700314 mSinkBufferWidth = w;
315 mSinkBufferHeight = h;
316}
317
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
319 return mFbFence;
320}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800321
Jesse Hall38efe862013-04-06 23:12:29 -0700322status_t VirtualDisplaySurface::requestBuffer(int pslot,
323 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700324 if (mDisplayId < 0)
325 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
326
Jesse Hall38efe862013-04-06 23:12:29 -0700327 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
328 "Unexpected requestBuffer pslot=%d in %s state",
329 pslot, dbgStateStr());
330
331 *outBuf = mProducerBuffers[pslot];
332 return NO_ERROR;
333}
334
Pablo Ceballosfa455352015-08-12 17:47:47 -0700335status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
336 int maxDequeuedBuffers) {
337 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
338}
339
340status_t VirtualDisplaySurface::setAsyncMode(bool async) {
341 return mSource[SOURCE_SINK]->setAsyncMode(async);
342}
343
Jesse Hall38efe862013-04-06 23:12:29 -0700344status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700345 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700346 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700347
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600348 status_t result =
349 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
350 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700351 if (result < 0)
352 return result;
353 int pslot = mapSource2ProducerSlot(source, *sslot);
354 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
355 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700356 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700357
Dan Stozafebd4f42014-04-09 16:14:51 -0700358 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700359 // This slot was previously dequeued from the other source; must
360 // re-request the buffer.
361 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700362 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700363 mProducerSlotSource |= sourceBit;
364 }
365
366 if (result & RELEASE_ALL_BUFFERS) {
367 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700368 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700369 mProducerBuffers[i].clear();
370 }
371 }
372 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700373 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
374 if (result < 0) {
375 mProducerBuffers[pslot].clear();
376 mSource[source]->cancelBuffer(*sslot, *fence);
377 return result;
378 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700379 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800380 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
381 mProducerBuffers[pslot]->getPixelFormat(),
382 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700383 }
384
385 return result;
386}
387
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600388status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
389 PixelFormat format, uint64_t usage,
390 uint64_t* outBufferAge,
391 FrameEventHistoryDelta* outTimestamps) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700392 if (mDisplayId < 0) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600393 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
394 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700395 }
Michael Lentine47e45402014-07-18 15:34:25 -0700396
Jesse Hall38efe862013-04-06 23:12:29 -0700397 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
398 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
399 mDbgState = DBG_STATE_GLES;
400
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700401 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700402
Jesse Hall028dc8f2013-08-20 16:35:32 -0700403 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700404 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700405
Jesse Hall38efe862013-04-06 23:12:29 -0700406 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700407
408 if (mOutputProducerSlot < 0) {
409 // Last chance bailout if something bad happened earlier. For example,
410 // in a GLES configuration, if the sink disappears then dequeueBuffer
411 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
412 // will soldier on. So we end up here without a buffer. There should
413 // be lots of scary messages in the log just before this.
414 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
415 return NO_MEMORY;
416 }
417
Jesse Hall028dc8f2013-08-20 16:35:32 -0700418 // We already dequeued the output buffer. If the GLES driver wants
419 // something incompatible, we have to cancel and get a new one. This
420 // will mean that HWC will see a different output buffer between
421 // prepare and set, but since we're in GLES-only mode already it
422 // shouldn't matter.
423
Jesse Hall1e27ba22013-09-27 09:05:09 -0700424 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700425 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700426 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800427 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700428 (w != 0 && w != mSinkBufferWidth) ||
429 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700430 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700431 "want %dx%d fmt=%d use=%#" PRIx64 ", "
432 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700433 w, h, format, usage,
434 mSinkBufferWidth, mSinkBufferHeight,
435 buf->getPixelFormat(), buf->getUsage());
436 mOutputFormat = format;
437 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700438 result = refreshOutputBuffer();
439 if (result < 0)
440 return result;
441 }
Jesse Hall38efe862013-04-06 23:12:29 -0700442 }
443
Jesse Hall028dc8f2013-08-20 16:35:32 -0700444 if (source == SOURCE_SINK) {
445 *pslot = mOutputProducerSlot;
446 *fence = mOutputFence;
447 } else {
448 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700449 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700450 if (result >= 0) {
451 *pslot = mapSource2ProducerSlot(source, sslot);
452 }
Jesse Hall38efe862013-04-06 23:12:29 -0700453 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600454 if (outBufferAge) {
455 *outBufferAge = 0;
456 }
Jesse Hall38efe862013-04-06 23:12:29 -0700457 return result;
458}
459
Dan Stoza88a459a2014-03-12 09:34:36 -0700460status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
461 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
462 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800463}
464
Dan Stozad9822a32014-03-28 15:25:31 -0700465status_t VirtualDisplaySurface::detachNextBuffer(
466 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
467 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
468 return INVALID_OPERATION;
469}
470
Dan Stoza88a459a2014-03-12 09:34:36 -0700471status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
472 const sp<GraphicBuffer>& /* buffer */) {
473 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
474 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800475}
476
Jesse Hall38efe862013-04-06 23:12:29 -0700477status_t VirtualDisplaySurface::queueBuffer(int pslot,
478 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700479 if (mDisplayId < 0)
480 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
481
Jesse Hall38efe862013-04-06 23:12:29 -0700482 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
483 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
484 dbgStateStr());
485 mDbgState = DBG_STATE_GLES_DONE;
486
487 VDS_LOGV("queueBuffer pslot=%d", pslot);
488
489 status_t result;
490 if (mCompositionType == COMPOSITION_MIXED) {
491 // Queue the buffer back into the scratch pool
492 QueueBufferOutput scratchQBO;
493 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700494 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700495 if (result != NO_ERROR)
496 return result;
497
498 // Now acquire the buffer from the scratch pool -- should be the same
499 // slot and fence as we just queued.
500 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700501 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700502 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700503 if (result != NO_ERROR)
504 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700505 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700506 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700507 item.mSlot, sslot);
508 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
509 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700510
511 } else {
512 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
513 "Unexpected queueBuffer in state %s for compositionType %s",
514 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
515
516 // Extract the GLES release fence for HWC to acquire
517 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700518 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800519 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700520 Rect crop;
521 int scalingMode;
522 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800523 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700524 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700525
526 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700527 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700528 }
529
Brian Anderson3d4039d2016-09-23 16:31:30 -0700530 // This moves the frame timestamps and keeps a copy of all other fields.
531 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700532 return NO_ERROR;
533}
534
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700535status_t VirtualDisplaySurface::cancelBuffer(int pslot,
536 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700537 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700538 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700539
Jesse Hall38efe862013-04-06 23:12:29 -0700540 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
541 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
542 dbgStateStr());
543 VDS_LOGV("cancelBuffer pslot=%d", pslot);
544 Source source = fbSourceForCompositionType(mCompositionType);
545 return mSource[source]->cancelBuffer(
546 mapProducer2SourceSlot(source, pslot), fence);
547}
548
549int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700550 switch (what) {
551 case NATIVE_WINDOW_WIDTH:
552 *value = mSinkBufferWidth;
553 break;
554 case NATIVE_WINDOW_HEIGHT:
555 *value = mSinkBufferHeight;
556 break;
557 default:
558 return mSource[SOURCE_SINK]->query(what, value);
559 }
560 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700561}
562
Dan Stozaf0eaf252014-03-21 13:05:51 -0700563status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700564 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700565 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700566 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700567 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
568 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700569 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700570 updateQueueBufferOutput(std::move(qbo));
571 // This moves the frame timestamps and keeps a copy of all other fields.
572 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700573 }
574 return result;
575}
576
Robert Carr97b9c862016-09-08 13:54:35 -0700577status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
578 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700579}
580
Jesse Hall399184a2014-03-03 15:42:54 -0800581status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
582 return INVALID_OPERATION;
583}
584
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700585void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700586 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700587 // TODO: Should we actually allocate buffers for a virtual display?
588}
589
Dan Stoza9de72932015-04-16 17:28:43 -0700590status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
591 return INVALID_OPERATION;
592}
593
Dan Stoza812ed062015-06-02 15:45:22 -0700594status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
595 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
596 return INVALID_OPERATION;
597}
598
Dan Stozac6f30bd2015-06-08 09:32:50 -0700599String8 VirtualDisplaySurface::getConsumerName() const {
600 return String8("VirtualDisplaySurface");
601}
602
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700603status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
604 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800605 return INVALID_OPERATION;
606}
607
608status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
609 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
610 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700611}
612
Dan Stoza127fc632015-06-30 13:43:32 -0700613status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
614 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
615 return INVALID_OPERATION;
616}
617
Dan Stoza50101d02016-04-07 16:53:23 -0700618status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700619 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
620 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700621 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
622 return INVALID_OPERATION;
623}
624
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700625status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
626 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
627 return INVALID_OPERATION;
628}
629
Jesse Hall38efe862013-04-06 23:12:29 -0700630void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700631 QueueBufferOutput&& qbo) {
632 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700633 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700634}
635
636void VirtualDisplaySurface::resetPerFrameState() {
637 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530638 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700639 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700640 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530641 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700642}
643
Jesse Hall028dc8f2013-08-20 16:35:32 -0700644status_t VirtualDisplaySurface::refreshOutputBuffer() {
645 if (mOutputProducerSlot >= 0) {
646 mSource[SOURCE_SINK]->cancelBuffer(
647 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
648 mOutputFence);
649 }
650
651 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700652 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
653 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700654 if (result < 0)
655 return result;
656 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
657
Jesse Hallb716e572013-10-01 17:25:20 -0700658 // On GLES-only frames, we don't have the right output buffer acquire fence
659 // until after GLES calls queueBuffer(). So here we just set the buffer
660 // (for use in HWC prepare) but not the fence; we'll call this again with
661 // the proper fence once we have it.
662 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700663 mProducerBuffers[mOutputProducerSlot]);
664
665 return result;
666}
667
Jesse Hall38efe862013-04-06 23:12:29 -0700668// This slot mapping function is its own inverse, so two copies are unnecessary.
669// Both are kept to make the intent clear where the function is called, and for
670// the (unlikely) chance that we switch to a different mapping function.
671int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
672 if (source == SOURCE_SCRATCH) {
673 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
674 } else {
675 return sslot;
676 }
677}
678int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
679 return mapSource2ProducerSlot(source, pslot);
680}
681
682VirtualDisplaySurface::Source
683VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
684 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
685}
686
687const char* VirtualDisplaySurface::dbgStateStr() const {
688 switch (mDbgState) {
689 case DBG_STATE_IDLE: return "IDLE";
690 case DBG_STATE_PREPARED: return "PREPARED";
691 case DBG_STATE_GLES: return "GLES";
692 case DBG_STATE_GLES_DONE: return "GLES_DONE";
693 case DBG_STATE_HWC: return "HWC";
694 default: return "INVALID";
695 }
696}
697
698const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
699 switch (s) {
700 case SOURCE_SINK: return "SINK";
701 case SOURCE_SCRATCH: return "SCRATCH";
702 default: return "INVALID";
703 }
704}
705
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700706// ---------------------------------------------------------------------------
707} // namespace android
708// ---------------------------------------------------------------------------