blob: 6e843d9f33da9b4a1902d9ff9ccba22451277343 [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"
Jesse Hall38efe862013-04-06 23:12:29 -070019#include "HWComposer.h"
Fabien Sanglarda34ed632017-03-14 11:43:52 -070020#include "SurfaceFlinger.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070021
Dan Stoza11611f92015-03-12 15:12:44 -070022#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080023#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070024#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070025#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070026
Jesse Hall99c7dbb2013-03-14 14:29:29 -070027// ---------------------------------------------------------------------------
28namespace android {
29// ---------------------------------------------------------------------------
30
Jesse Hall24cd98e2014-07-13 14:37:16 -070031#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070032 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070033#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070034 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070035#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070036 mDisplayName.string(), ##__VA_ARGS__)
37
38static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
39 switch (type) {
40 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
41 case DisplaySurface::COMPOSITION_GLES: return "GLES";
42 case DisplaySurface::COMPOSITION_HWC: return "HWC";
43 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
44 default: return "<INVALID>";
45 }
46}
47
Jesse Hallffe1f192013-03-22 15:13:48 -070048VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070049 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070050 const sp<IGraphicBufferProducer>& bqProducer,
51 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070052 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070053: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070054 mHwc(hwc),
55 mDisplayId(dispId),
56 mDisplayName(name),
Dan Stoza9e56aa02015-11-02 13:00:03 -080057 mSource{},
58 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
59 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070060 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070061 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080062 mProducerBuffers(),
63 mQueueBufferOutput(),
64 mSinkBufferWidth(0),
65 mSinkBufferHeight(0),
66 mCompositionType(COMPOSITION_UNKNOWN),
67 mFbFence(Fence::NO_FENCE),
68 mOutputFence(Fence::NO_FENCE),
69 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
70 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070071 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080072 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
Fabien Sanglarda34ed632017-03-14 11:43:52 -070073 mMustRecompose(false),
74 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv)
Jesse Hallffe1f192013-03-22 15:13:48 -070075{
Jesse Hall38efe862013-04-06 23:12:29 -070076 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070077 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070078
79 resetPerFrameState();
80
81 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080082 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
83 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070084 mSinkBufferWidth = sinkWidth;
85 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080086
87 // Pick the buffer format to request from the sink when not rendering to it
88 // with GLES. If the consumer needs CPU access, use the default format
89 // set by the consumer. Otherwise allow gralloc to decide the format based
90 // on usage bits.
91 int sinkUsage;
92 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
93 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
94 int sinkFormat;
95 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
96 mDefaultOutputFormat = sinkFormat;
97 } else {
98 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
99 }
100 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700101
102 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700103 mConsumer->setConsumerName(ConsumerBase::mName);
104 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
105 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700106 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -0700107 IGraphicBufferProducer::QueueBufferOutput output;
108 mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700109}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700110
111VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700112 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700113}
114
Dan Stoza71433162014-02-04 16:22:36 -0800115status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700116 if (mDisplayId < 0)
117 return NO_ERROR;
118
Dan Stoza71433162014-02-04 16:22:36 -0800119 mMustRecompose = mustRecompose;
120
Jesse Hall38efe862013-04-06 23:12:29 -0700121 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700122 "Unexpected beginFrame() in %s state", dbgStateStr());
123 mDbgState = DBG_STATE_BEGUN;
124
Jesse Hall028dc8f2013-08-20 16:35:32 -0700125 return refreshOutputBuffer();
126}
127
128status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
129 if (mDisplayId < 0)
130 return NO_ERROR;
131
132 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700133 "Unexpected prepareFrame() in %s state", dbgStateStr());
134 mDbgState = DBG_STATE_PREPARED;
135
136 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700137 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400138 // Some hardware can do RGB->YUV conversion more efficiently in hardware
139 // controlled by HWC than in hardware controlled by the video encoder.
140 // Forcing GLES-composed frames to go through an extra copy by the HWC
141 // allows the format conversion to happen there, rather than passing RGB
142 // directly to the consumer.
143 //
144 // On the other hand, when the consumer prefers RGB or can consume RGB
145 // inexpensively, this forces an unnecessary copy.
146 mCompositionType = COMPOSITION_MIXED;
147 }
148
Jesse Hall38efe862013-04-06 23:12:29 -0700149 if (mCompositionType != mDbgLastCompositionType) {
150 VDS_LOGV("prepareFrame: composition type changed to %s",
151 dbgCompositionTypeStr(mCompositionType));
152 mDbgLastCompositionType = mCompositionType;
153 }
154
Jesse Hall1e27ba22013-09-27 09:05:09 -0700155 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800156 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700157 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
158 // We must have just switched from GLES-only to MIXED or HWC
159 // composition. Stop using the format and usage requested by the GLES
160 // driver; they may be suboptimal when HWC is writing to the output
161 // buffer. For example, if the output is going to a video encoder, and
162 // HWC can write directly to YUV, some hardware can skip a
163 // memory-to-memory RGB-to-YUV conversion step.
164 //
165 // If we just switched *to* GLES-only mode, we'll change the
166 // format/usage and get a new buffer when the GLES driver calls
167 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800168 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700169 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
170 refreshOutputBuffer();
171 }
172
Jesse Hall38efe862013-04-06 23:12:29 -0700173 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700174}
175
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000176#ifndef USE_HWC2
177status_t VirtualDisplaySurface::compositionComplete() {
178 return NO_ERROR;
179}
180#endif
181
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700182status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700183 if (mDisplayId < 0)
184 return NO_ERROR;
185
186 if (mCompositionType == COMPOSITION_HWC) {
187 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
188 "Unexpected advanceFrame() in %s state on HWC frame",
189 dbgStateStr());
190 } else {
191 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
192 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
193 dbgStateStr());
194 }
195 mDbgState = DBG_STATE_HWC;
196
Jesse Hall14e8b012013-11-07 12:28:26 -0800197 if (mOutputProducerSlot < 0 ||
198 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700199 // Last chance bailout if something bad happened earlier. For example,
200 // in a GLES configuration, if the sink disappears then dequeueBuffer
201 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
202 // will soldier on. So we end up here without a buffer. There should
203 // be lots of scary messages in the log just before this.
204 VDS_LOGE("advanceFrame: no buffer, bailing out");
205 return NO_MEMORY;
206 }
207
Jesse Hall14e8b012013-11-07 12:28:26 -0800208 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
209 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700210 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
211 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
212 mFbProducerSlot, fbBuffer.get(),
213 mOutputProducerSlot, outBuffer.get());
214
Jesse Hallb716e572013-10-01 17:25:20 -0700215 // At this point we know the output buffer acquire fence,
216 // so update HWC state with it.
217 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
218
Jesse Hall14e8b012013-11-07 12:28:26 -0800219 status_t result = NO_ERROR;
220 if (fbBuffer != NULL) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000221#ifdef USE_HWC2
Chia-I Wu06d63de2017-01-04 14:58:51 +0800222 uint32_t hwcSlot = 0;
223 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800224 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800225 &hwcSlot, &hwcBuffer);
226
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 // TODO: Correctly propagate the dataspace from GL composition
Chia-I Wu06d63de2017-01-04 14:58:51 +0800228 result = mHwc.setClientTarget(mDisplayId, hwcSlot, mFbFence,
229 hwcBuffer, HAL_DATASPACE_UNKNOWN);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000230#else
231 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
232#endif
Jesse Hall14e8b012013-11-07 12:28:26 -0800233 }
234
235 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700236}
237
Jesse Hall851cfe82013-03-20 13:44:00 -0700238void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700239 if (mDisplayId < 0)
240 return;
241
242 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
243 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
244 mDbgState = DBG_STATE_IDLE;
245
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000246#ifdef USE_HWC2
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800247 sp<Fence> retireFence = mHwc.getPresentFence(mDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000248#else
249 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
250#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700251 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
252 // release the scratch buffer back to the pool
253 Mutex::Autolock lock(mMutex);
254 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
255 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000256#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800257 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
258 retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000259#else
260 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
261#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700262 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
263 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
264 }
265
266 if (mOutputProducerSlot >= 0) {
267 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
268 QueueBufferOutput qbo;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000269#ifndef USE_HWC2
270 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
271#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700272 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800273 if (mMustRecompose) {
274 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
275 QueueBufferInput(
276 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800277 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800278 Rect(mSinkBufferWidth, mSinkBufferHeight),
279 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000280#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800281 retireFence),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000282#else
283 outFence),
284#endif
Dan Stoza71433162014-02-04 16:22:36 -0800285 &qbo);
286 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700287 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800288 }
289 } else {
290 // If the surface hadn't actually been updated, then we only went
291 // through the motions of updating the display to keep our state
292 // machine happy. We cancel the buffer to avoid triggering another
293 // re-composition and causing an infinite loop.
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000294#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000296#else
297 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
298#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700299 }
300 }
301
302 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700303}
304
Dan Stoza01049c82014-11-11 10:32:31 -0800305void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700306}
307
Michael Lentine47e45402014-07-18 15:34:25 -0700308void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700309 mQueueBufferOutput.width = w;
310 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700311 mSinkBufferWidth = w;
312 mSinkBufferHeight = h;
313}
314
Dan Stoza9e56aa02015-11-02 13:00:03 -0800315const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
316 return mFbFence;
317}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318
Jesse Hall38efe862013-04-06 23:12:29 -0700319status_t VirtualDisplaySurface::requestBuffer(int pslot,
320 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700321 if (mDisplayId < 0)
322 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
323
Jesse Hall38efe862013-04-06 23:12:29 -0700324 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
325 "Unexpected requestBuffer pslot=%d in %s state",
326 pslot, dbgStateStr());
327
328 *outBuf = mProducerBuffers[pslot];
329 return NO_ERROR;
330}
331
Pablo Ceballosfa455352015-08-12 17:47:47 -0700332status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
333 int maxDequeuedBuffers) {
334 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
335}
336
337status_t VirtualDisplaySurface::setAsyncMode(bool async) {
338 return mSource[SOURCE_SINK]->setAsyncMode(async);
339}
340
Jesse Hall38efe862013-04-06 23:12:29 -0700341status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800342 PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700343 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700344
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700345 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700346 mSinkBufferWidth, mSinkBufferHeight, format, usage, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700347 if (result < 0)
348 return result;
349 int pslot = mapSource2ProducerSlot(source, *sslot);
350 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
351 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700352 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700353
Dan Stozafebd4f42014-04-09 16:14:51 -0700354 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700355 // This slot was previously dequeued from the other source; must
356 // re-request the buffer.
357 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700358 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700359 mProducerSlotSource |= sourceBit;
360 }
361
362 if (result & RELEASE_ALL_BUFFERS) {
363 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700364 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700365 mProducerBuffers[i].clear();
366 }
367 }
368 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700369 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
370 if (result < 0) {
371 mProducerBuffers[pslot].clear();
372 mSource[source]->cancelBuffer(*sslot, *fence);
373 return result;
374 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800375 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
376 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
377 mProducerBuffers[pslot]->getPixelFormat(),
378 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700379 }
380
381 return result;
382}
383
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700384status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700385 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
386 FrameEventHistoryDelta* outTimestamps) {
387 if (mDisplayId < 0) {
388 return mSource[SOURCE_SINK]->dequeueBuffer(
389 pslot, fence, w, h, format, usage, outTimestamps);
390 }
Michael Lentine47e45402014-07-18 15:34:25 -0700391
Jesse Hall38efe862013-04-06 23:12:29 -0700392 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
393 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
394 mDbgState = DBG_STATE_GLES;
395
396 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
397
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700399 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700400
Jesse Hall38efe862013-04-06 23:12:29 -0700401 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700402
403 if (mOutputProducerSlot < 0) {
404 // Last chance bailout if something bad happened earlier. For example,
405 // in a GLES configuration, if the sink disappears then dequeueBuffer
406 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
407 // will soldier on. So we end up here without a buffer. There should
408 // be lots of scary messages in the log just before this.
409 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
410 return NO_MEMORY;
411 }
412
Jesse Hall028dc8f2013-08-20 16:35:32 -0700413 // We already dequeued the output buffer. If the GLES driver wants
414 // something incompatible, we have to cancel and get a new one. This
415 // will mean that HWC will see a different output buffer between
416 // prepare and set, but since we're in GLES-only mode already it
417 // shouldn't matter.
418
Jesse Hall1e27ba22013-09-27 09:05:09 -0700419 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700420 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700421 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800422 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700423 (w != 0 && w != mSinkBufferWidth) ||
424 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700425 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
426 "want %dx%d fmt=%d use=%#x, "
427 "have %dx%d fmt=%d use=%#x",
428 w, h, format, usage,
429 mSinkBufferWidth, mSinkBufferHeight,
430 buf->getPixelFormat(), buf->getUsage());
431 mOutputFormat = format;
432 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700433 result = refreshOutputBuffer();
434 if (result < 0)
435 return result;
436 }
Jesse Hall38efe862013-04-06 23:12:29 -0700437 }
438
Jesse Hall028dc8f2013-08-20 16:35:32 -0700439 if (source == SOURCE_SINK) {
440 *pslot = mOutputProducerSlot;
441 *fence = mOutputFence;
442 } else {
443 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700444 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700445 if (result >= 0) {
446 *pslot = mapSource2ProducerSlot(source, sslot);
447 }
Jesse Hall38efe862013-04-06 23:12:29 -0700448 }
449 return result;
450}
451
Dan Stoza88a459a2014-03-12 09:34:36 -0700452status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
453 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
454 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800455}
456
Dan Stozad9822a32014-03-28 15:25:31 -0700457status_t VirtualDisplaySurface::detachNextBuffer(
458 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
459 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
460 return INVALID_OPERATION;
461}
462
Dan Stoza88a459a2014-03-12 09:34:36 -0700463status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
464 const sp<GraphicBuffer>& /* buffer */) {
465 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
466 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800467}
468
Jesse Hall38efe862013-04-06 23:12:29 -0700469status_t VirtualDisplaySurface::queueBuffer(int pslot,
470 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700471 if (mDisplayId < 0)
472 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
473
Jesse Hall38efe862013-04-06 23:12:29 -0700474 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
475 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
476 dbgStateStr());
477 mDbgState = DBG_STATE_GLES_DONE;
478
479 VDS_LOGV("queueBuffer pslot=%d", pslot);
480
481 status_t result;
482 if (mCompositionType == COMPOSITION_MIXED) {
483 // Queue the buffer back into the scratch pool
484 QueueBufferOutput scratchQBO;
485 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700486 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700487 if (result != NO_ERROR)
488 return result;
489
490 // Now acquire the buffer from the scratch pool -- should be the same
491 // slot and fence as we just queued.
492 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700493 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700494 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700495 if (result != NO_ERROR)
496 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700497 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700498 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700499 item.mSlot, sslot);
500 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
501 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700502
503 } else {
504 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
505 "Unexpected queueBuffer in state %s for compositionType %s",
506 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
507
508 // Extract the GLES release fence for HWC to acquire
509 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700510 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800511 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700512 Rect crop;
513 int scalingMode;
514 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800515 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700516 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700517
518 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700519 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700520 }
521
Brian Anderson3d4039d2016-09-23 16:31:30 -0700522 // This moves the frame timestamps and keeps a copy of all other fields.
523 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700524 return NO_ERROR;
525}
526
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700527status_t VirtualDisplaySurface::cancelBuffer(int pslot,
528 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700529 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700530 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700531
Jesse Hall38efe862013-04-06 23:12:29 -0700532 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
533 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
534 dbgStateStr());
535 VDS_LOGV("cancelBuffer pslot=%d", pslot);
536 Source source = fbSourceForCompositionType(mCompositionType);
537 return mSource[source]->cancelBuffer(
538 mapProducer2SourceSlot(source, pslot), fence);
539}
540
541int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700542 switch (what) {
543 case NATIVE_WINDOW_WIDTH:
544 *value = mSinkBufferWidth;
545 break;
546 case NATIVE_WINDOW_HEIGHT:
547 *value = mSinkBufferHeight;
548 break;
549 default:
550 return mSource[SOURCE_SINK]->query(what, value);
551 }
552 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700553}
554
Dan Stozaf0eaf252014-03-21 13:05:51 -0700555status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700556 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700557 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700558 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700559 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
560 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700561 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700562 updateQueueBufferOutput(std::move(qbo));
563 // This moves the frame timestamps and keeps a copy of all other fields.
564 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700565 }
566 return result;
567}
568
Robert Carr97b9c862016-09-08 13:54:35 -0700569status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
570 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700571}
572
Jesse Hall399184a2014-03-03 15:42:54 -0800573status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
574 return INVALID_OPERATION;
575}
576
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700577void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
578 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700579 // TODO: Should we actually allocate buffers for a virtual display?
580}
581
Dan Stoza9de72932015-04-16 17:28:43 -0700582status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
583 return INVALID_OPERATION;
584}
585
Dan Stoza812ed062015-06-02 15:45:22 -0700586status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
587 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
588 return INVALID_OPERATION;
589}
590
Dan Stozac6f30bd2015-06-08 09:32:50 -0700591String8 VirtualDisplaySurface::getConsumerName() const {
592 return String8("VirtualDisplaySurface");
593}
594
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700595status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
596 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800597 return INVALID_OPERATION;
598}
599
600status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
601 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
602 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700603}
604
Dan Stoza127fc632015-06-30 13:43:32 -0700605status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
606 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
607 return INVALID_OPERATION;
608}
609
Dan Stoza50101d02016-04-07 16:53:23 -0700610status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700611 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
612 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700613 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
614 return INVALID_OPERATION;
615}
616
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700617status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
618 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
619 return INVALID_OPERATION;
620}
621
Jesse Hall38efe862013-04-06 23:12:29 -0700622void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700623 QueueBufferOutput&& qbo) {
624 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700625 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700626}
627
628void VirtualDisplaySurface::resetPerFrameState() {
629 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530630 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700631 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700632 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530633 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700634}
635
Jesse Hall028dc8f2013-08-20 16:35:32 -0700636status_t VirtualDisplaySurface::refreshOutputBuffer() {
637 if (mOutputProducerSlot >= 0) {
638 mSource[SOURCE_SINK]->cancelBuffer(
639 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
640 mOutputFence);
641 }
642
643 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700644 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
645 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700646 if (result < 0)
647 return result;
648 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
649
Jesse Hallb716e572013-10-01 17:25:20 -0700650 // On GLES-only frames, we don't have the right output buffer acquire fence
651 // until after GLES calls queueBuffer(). So here we just set the buffer
652 // (for use in HWC prepare) but not the fence; we'll call this again with
653 // the proper fence once we have it.
654 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700655 mProducerBuffers[mOutputProducerSlot]);
656
657 return result;
658}
659
Jesse Hall38efe862013-04-06 23:12:29 -0700660// This slot mapping function is its own inverse, so two copies are unnecessary.
661// Both are kept to make the intent clear where the function is called, and for
662// the (unlikely) chance that we switch to a different mapping function.
663int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
664 if (source == SOURCE_SCRATCH) {
665 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
666 } else {
667 return sslot;
668 }
669}
670int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
671 return mapSource2ProducerSlot(source, pslot);
672}
673
674VirtualDisplaySurface::Source
675VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
676 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
677}
678
679const char* VirtualDisplaySurface::dbgStateStr() const {
680 switch (mDbgState) {
681 case DBG_STATE_IDLE: return "IDLE";
682 case DBG_STATE_PREPARED: return "PREPARED";
683 case DBG_STATE_GLES: return "GLES";
684 case DBG_STATE_GLES_DONE: return "GLES_DONE";
685 case DBG_STATE_HWC: return "HWC";
686 default: return "INVALID";
687 }
688}
689
690const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
691 switch (s) {
692 case SOURCE_SINK: return "SINK";
693 case SOURCE_SCRATCH: return "SCRATCH";
694 default: return "INVALID";
695 }
696}
697
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700698// ---------------------------------------------------------------------------
699} // namespace android
700// ---------------------------------------------------------------------------