blob: 82175402154364874d3b39c917bf0a4835657500 [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>
Dan Stoza11611f92015-03-12 15:12:44 -070025
Jesse Hall99c7dbb2013-03-14 14:29:29 -070026// ---------------------------------------------------------------------------
27namespace android {
28// ---------------------------------------------------------------------------
29
Jesse Hall24cd98e2014-07-13 14:37:16 -070030#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070031 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070032#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070033 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070034#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070035 mDisplayName.string(), ##__VA_ARGS__)
36
37static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
38 switch (type) {
39 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
40 case DisplaySurface::COMPOSITION_GLES: return "GLES";
41 case DisplaySurface::COMPOSITION_HWC: return "HWC";
42 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
43 default: return "<INVALID>";
44 }
45}
46
Jesse Hallffe1f192013-03-22 15:13:48 -070047VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070048 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070049 const sp<IGraphicBufferProducer>& bqProducer,
50 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070051 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070052: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070053 mHwc(hwc),
54 mDisplayId(dispId),
55 mDisplayName(name),
Dan Stoza9e56aa02015-11-02 13:00:03 -080056 mSource{},
57 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
58 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070059 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070060 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080061 mProducerBuffers(),
62 mQueueBufferOutput(),
63 mSinkBufferWidth(0),
64 mSinkBufferHeight(0),
65 mCompositionType(COMPOSITION_UNKNOWN),
66 mFbFence(Fence::NO_FENCE),
67 mOutputFence(Fence::NO_FENCE),
68 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
69 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070070 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080071 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
Fabien Sanglarda34ed632017-03-14 11:43:52 -070072 mMustRecompose(false),
73 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv)
Jesse Hallffe1f192013-03-22 15:13:48 -070074{
Jesse Hall38efe862013-04-06 23:12:29 -070075 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070076 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070077
78 resetPerFrameState();
79
80 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080081 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
82 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070083 mSinkBufferWidth = sinkWidth;
84 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080085
86 // Pick the buffer format to request from the sink when not rendering to it
87 // with GLES. If the consumer needs CPU access, use the default format
88 // set by the consumer. Otherwise allow gralloc to decide the format based
89 // on usage bits.
90 int sinkUsage;
91 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
92 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
93 int sinkFormat;
94 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
95 mDefaultOutputFormat = sinkFormat;
96 } else {
97 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
98 }
99 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700100
101 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700102 mConsumer->setConsumerName(ConsumerBase::mName);
103 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
104 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700105 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -0700106 IGraphicBufferProducer::QueueBufferOutput output;
107 mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700108}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700109
110VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700111 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700112}
113
Dan Stoza71433162014-02-04 16:22:36 -0800114status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700115 if (mDisplayId < 0)
116 return NO_ERROR;
117
Dan Stoza71433162014-02-04 16:22:36 -0800118 mMustRecompose = mustRecompose;
119
Jesse Hall38efe862013-04-06 23:12:29 -0700120 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700121 "Unexpected beginFrame() in %s state", dbgStateStr());
122 mDbgState = DBG_STATE_BEGUN;
123
Jesse Hall028dc8f2013-08-20 16:35:32 -0700124 return refreshOutputBuffer();
125}
126
127status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
128 if (mDisplayId < 0)
129 return NO_ERROR;
130
131 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700132 "Unexpected prepareFrame() in %s state", dbgStateStr());
133 mDbgState = DBG_STATE_PREPARED;
134
135 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700136 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400137 // Some hardware can do RGB->YUV conversion more efficiently in hardware
138 // controlled by HWC than in hardware controlled by the video encoder.
139 // Forcing GLES-composed frames to go through an extra copy by the HWC
140 // allows the format conversion to happen there, rather than passing RGB
141 // directly to the consumer.
142 //
143 // On the other hand, when the consumer prefers RGB or can consume RGB
144 // inexpensively, this forces an unnecessary copy.
145 mCompositionType = COMPOSITION_MIXED;
146 }
147
Jesse Hall38efe862013-04-06 23:12:29 -0700148 if (mCompositionType != mDbgLastCompositionType) {
149 VDS_LOGV("prepareFrame: composition type changed to %s",
150 dbgCompositionTypeStr(mCompositionType));
151 mDbgLastCompositionType = mCompositionType;
152 }
153
Jesse Hall1e27ba22013-09-27 09:05:09 -0700154 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800155 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700156 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
157 // We must have just switched from GLES-only to MIXED or HWC
158 // composition. Stop using the format and usage requested by the GLES
159 // driver; they may be suboptimal when HWC is writing to the output
160 // buffer. For example, if the output is going to a video encoder, and
161 // HWC can write directly to YUV, some hardware can skip a
162 // memory-to-memory RGB-to-YUV conversion step.
163 //
164 // If we just switched *to* GLES-only mode, we'll change the
165 // format/usage and get a new buffer when the GLES driver calls
166 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800167 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700168 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
169 refreshOutputBuffer();
170 }
171
Jesse Hall38efe862013-04-06 23:12:29 -0700172 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700173}
174
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000175#ifndef USE_HWC2
176status_t VirtualDisplaySurface::compositionComplete() {
177 return NO_ERROR;
178}
179#endif
180
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700181status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700182 if (mDisplayId < 0)
183 return NO_ERROR;
184
185 if (mCompositionType == COMPOSITION_HWC) {
186 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
187 "Unexpected advanceFrame() in %s state on HWC frame",
188 dbgStateStr());
189 } else {
190 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
191 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
192 dbgStateStr());
193 }
194 mDbgState = DBG_STATE_HWC;
195
Jesse Hall14e8b012013-11-07 12:28:26 -0800196 if (mOutputProducerSlot < 0 ||
197 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700198 // Last chance bailout if something bad happened earlier. For example,
199 // in a GLES configuration, if the sink disappears then dequeueBuffer
200 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
201 // will soldier on. So we end up here without a buffer. There should
202 // be lots of scary messages in the log just before this.
203 VDS_LOGE("advanceFrame: no buffer, bailing out");
204 return NO_MEMORY;
205 }
206
Jesse Hall14e8b012013-11-07 12:28:26 -0800207 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
208 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700209 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
210 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
211 mFbProducerSlot, fbBuffer.get(),
212 mOutputProducerSlot, outBuffer.get());
213
Jesse Hallb716e572013-10-01 17:25:20 -0700214 // At this point we know the output buffer acquire fence,
215 // so update HWC state with it.
216 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
217
Jesse Hall14e8b012013-11-07 12:28:26 -0800218 status_t result = NO_ERROR;
219 if (fbBuffer != NULL) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000220#ifdef USE_HWC2
Chia-I Wu06d63de2017-01-04 14:58:51 +0800221 uint32_t hwcSlot = 0;
222 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800223 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800224 &hwcSlot, &hwcBuffer);
225
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 // TODO: Correctly propagate the dataspace from GL composition
Chia-I Wu06d63de2017-01-04 14:58:51 +0800227 result = mHwc.setClientTarget(mDisplayId, hwcSlot, mFbFence,
228 hwcBuffer, HAL_DATASPACE_UNKNOWN);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000229#else
230 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
231#endif
Jesse Hall14e8b012013-11-07 12:28:26 -0800232 }
233
234 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700235}
236
Jesse Hall851cfe82013-03-20 13:44:00 -0700237void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700238 if (mDisplayId < 0)
239 return;
240
241 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
242 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
243 mDbgState = DBG_STATE_IDLE;
244
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000245#ifdef USE_HWC2
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800246 sp<Fence> retireFence = mHwc.getPresentFence(mDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000247#else
248 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
249#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700250 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
251 // release the scratch buffer back to the pool
252 Mutex::Autolock lock(mMutex);
253 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
254 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000255#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
257 retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000258#else
259 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
260#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700261 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
262 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
263 }
264
265 if (mOutputProducerSlot >= 0) {
266 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
267 QueueBufferOutput qbo;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000268#ifndef USE_HWC2
269 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
270#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700271 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800272 if (mMustRecompose) {
273 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
274 QueueBufferInput(
275 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800276 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800277 Rect(mSinkBufferWidth, mSinkBufferHeight),
278 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000279#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800280 retireFence),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000281#else
282 outFence),
283#endif
Dan Stoza71433162014-02-04 16:22:36 -0800284 &qbo);
285 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700286 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800287 }
288 } else {
289 // If the surface hadn't actually been updated, then we only went
290 // through the motions of updating the display to keep our state
291 // machine happy. We cancel the buffer to avoid triggering another
292 // re-composition and causing an infinite loop.
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000293#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800294 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000295#else
296 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
297#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700298 }
299 }
300
301 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700302}
303
Dan Stoza01049c82014-11-11 10:32:31 -0800304void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700305}
306
Michael Lentine47e45402014-07-18 15:34:25 -0700307void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700308 mQueueBufferOutput.width = w;
309 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700310 mSinkBufferWidth = w;
311 mSinkBufferHeight = h;
312}
313
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
315 return mFbFence;
316}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317
Jesse Hall38efe862013-04-06 23:12:29 -0700318status_t VirtualDisplaySurface::requestBuffer(int pslot,
319 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700320 if (mDisplayId < 0)
321 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
322
Jesse Hall38efe862013-04-06 23:12:29 -0700323 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
324 "Unexpected requestBuffer pslot=%d in %s state",
325 pslot, dbgStateStr());
326
327 *outBuf = mProducerBuffers[pslot];
328 return NO_ERROR;
329}
330
Pablo Ceballosfa455352015-08-12 17:47:47 -0700331status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
332 int maxDequeuedBuffers) {
333 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
334}
335
336status_t VirtualDisplaySurface::setAsyncMode(bool async) {
337 return mSource[SOURCE_SINK]->setAsyncMode(async);
338}
339
Jesse Hall38efe862013-04-06 23:12:29 -0700340status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800341 PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700342 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700343
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700344 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700345 mSinkBufferWidth, mSinkBufferHeight, format, usage, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700346 if (result < 0)
347 return result;
348 int pslot = mapSource2ProducerSlot(source, *sslot);
349 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
350 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700351 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700352
Dan Stozafebd4f42014-04-09 16:14:51 -0700353 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700354 // This slot was previously dequeued from the other source; must
355 // re-request the buffer.
356 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700357 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700358 mProducerSlotSource |= sourceBit;
359 }
360
361 if (result & RELEASE_ALL_BUFFERS) {
362 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700363 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700364 mProducerBuffers[i].clear();
365 }
366 }
367 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700368 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
369 if (result < 0) {
370 mProducerBuffers[pslot].clear();
371 mSource[source]->cancelBuffer(*sslot, *fence);
372 return result;
373 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800374 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
375 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
376 mProducerBuffers[pslot]->getPixelFormat(),
377 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700378 }
379
380 return result;
381}
382
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700383status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700384 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
385 FrameEventHistoryDelta* outTimestamps) {
386 if (mDisplayId < 0) {
387 return mSource[SOURCE_SINK]->dequeueBuffer(
388 pslot, fence, w, h, format, usage, outTimestamps);
389 }
Michael Lentine47e45402014-07-18 15:34:25 -0700390
Jesse Hall38efe862013-04-06 23:12:29 -0700391 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
392 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
393 mDbgState = DBG_STATE_GLES;
394
395 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
396
Jesse Hall028dc8f2013-08-20 16:35:32 -0700397 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700398 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700399
Jesse Hall38efe862013-04-06 23:12:29 -0700400 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700401
402 if (mOutputProducerSlot < 0) {
403 // Last chance bailout if something bad happened earlier. For example,
404 // in a GLES configuration, if the sink disappears then dequeueBuffer
405 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
406 // will soldier on. So we end up here without a buffer. There should
407 // be lots of scary messages in the log just before this.
408 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
409 return NO_MEMORY;
410 }
411
Jesse Hall028dc8f2013-08-20 16:35:32 -0700412 // We already dequeued the output buffer. If the GLES driver wants
413 // something incompatible, we have to cancel and get a new one. This
414 // will mean that HWC will see a different output buffer between
415 // prepare and set, but since we're in GLES-only mode already it
416 // shouldn't matter.
417
Jesse Hall1e27ba22013-09-27 09:05:09 -0700418 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700419 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700420 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800421 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700422 (w != 0 && w != mSinkBufferWidth) ||
423 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700424 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
425 "want %dx%d fmt=%d use=%#x, "
426 "have %dx%d fmt=%d use=%#x",
427 w, h, format, usage,
428 mSinkBufferWidth, mSinkBufferHeight,
429 buf->getPixelFormat(), buf->getUsage());
430 mOutputFormat = format;
431 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700432 result = refreshOutputBuffer();
433 if (result < 0)
434 return result;
435 }
Jesse Hall38efe862013-04-06 23:12:29 -0700436 }
437
Jesse Hall028dc8f2013-08-20 16:35:32 -0700438 if (source == SOURCE_SINK) {
439 *pslot = mOutputProducerSlot;
440 *fence = mOutputFence;
441 } else {
442 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700443 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700444 if (result >= 0) {
445 *pslot = mapSource2ProducerSlot(source, sslot);
446 }
Jesse Hall38efe862013-04-06 23:12:29 -0700447 }
448 return result;
449}
450
Dan Stoza88a459a2014-03-12 09:34:36 -0700451status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
452 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
453 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800454}
455
Dan Stozad9822a32014-03-28 15:25:31 -0700456status_t VirtualDisplaySurface::detachNextBuffer(
457 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
458 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
459 return INVALID_OPERATION;
460}
461
Dan Stoza88a459a2014-03-12 09:34:36 -0700462status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
463 const sp<GraphicBuffer>& /* buffer */) {
464 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
465 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800466}
467
Jesse Hall38efe862013-04-06 23:12:29 -0700468status_t VirtualDisplaySurface::queueBuffer(int pslot,
469 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700470 if (mDisplayId < 0)
471 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
472
Jesse Hall38efe862013-04-06 23:12:29 -0700473 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
474 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
475 dbgStateStr());
476 mDbgState = DBG_STATE_GLES_DONE;
477
478 VDS_LOGV("queueBuffer pslot=%d", pslot);
479
480 status_t result;
481 if (mCompositionType == COMPOSITION_MIXED) {
482 // Queue the buffer back into the scratch pool
483 QueueBufferOutput scratchQBO;
484 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700485 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700486 if (result != NO_ERROR)
487 return result;
488
489 // Now acquire the buffer from the scratch pool -- should be the same
490 // slot and fence as we just queued.
491 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700492 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700493 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700494 if (result != NO_ERROR)
495 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700496 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700497 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700498 item.mSlot, sslot);
499 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
500 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700501
502 } else {
503 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
504 "Unexpected queueBuffer in state %s for compositionType %s",
505 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
506
507 // Extract the GLES release fence for HWC to acquire
508 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700509 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800510 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700511 Rect crop;
512 int scalingMode;
513 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800514 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700515 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700516
517 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700518 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700519 }
520
Brian Anderson3d4039d2016-09-23 16:31:30 -0700521 // This moves the frame timestamps and keeps a copy of all other fields.
522 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700523 return NO_ERROR;
524}
525
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700526status_t VirtualDisplaySurface::cancelBuffer(int pslot,
527 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700528 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700529 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700530
Jesse Hall38efe862013-04-06 23:12:29 -0700531 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
532 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
533 dbgStateStr());
534 VDS_LOGV("cancelBuffer pslot=%d", pslot);
535 Source source = fbSourceForCompositionType(mCompositionType);
536 return mSource[source]->cancelBuffer(
537 mapProducer2SourceSlot(source, pslot), fence);
538}
539
540int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700541 switch (what) {
542 case NATIVE_WINDOW_WIDTH:
543 *value = mSinkBufferWidth;
544 break;
545 case NATIVE_WINDOW_HEIGHT:
546 *value = mSinkBufferHeight;
547 break;
548 default:
549 return mSource[SOURCE_SINK]->query(what, value);
550 }
551 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700552}
553
Dan Stozaf0eaf252014-03-21 13:05:51 -0700554status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700555 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700556 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700557 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700558 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
559 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700560 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700561 updateQueueBufferOutput(std::move(qbo));
562 // This moves the frame timestamps and keeps a copy of all other fields.
563 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700564 }
565 return result;
566}
567
Robert Carr97b9c862016-09-08 13:54:35 -0700568status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
569 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700570}
571
Jesse Hall399184a2014-03-03 15:42:54 -0800572status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
573 return INVALID_OPERATION;
574}
575
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700576void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
577 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700578 // TODO: Should we actually allocate buffers for a virtual display?
579}
580
Dan Stoza9de72932015-04-16 17:28:43 -0700581status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
582 return INVALID_OPERATION;
583}
584
Dan Stoza812ed062015-06-02 15:45:22 -0700585status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
586 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
587 return INVALID_OPERATION;
588}
589
Dan Stozac6f30bd2015-06-08 09:32:50 -0700590String8 VirtualDisplaySurface::getConsumerName() const {
591 return String8("VirtualDisplaySurface");
592}
593
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700594status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
595 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800596 return INVALID_OPERATION;
597}
598
599status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
600 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
601 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700602}
603
Dan Stoza127fc632015-06-30 13:43:32 -0700604status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
605 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
606 return INVALID_OPERATION;
607}
608
Dan Stoza50101d02016-04-07 16:53:23 -0700609status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700610 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
611 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700612 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
613 return INVALID_OPERATION;
614}
615
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700616status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
617 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
618 return INVALID_OPERATION;
619}
620
Jesse Hall38efe862013-04-06 23:12:29 -0700621void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700622 QueueBufferOutput&& qbo) {
623 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700624 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700625}
626
627void VirtualDisplaySurface::resetPerFrameState() {
628 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530629 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700630 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700631 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530632 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700633}
634
Jesse Hall028dc8f2013-08-20 16:35:32 -0700635status_t VirtualDisplaySurface::refreshOutputBuffer() {
636 if (mOutputProducerSlot >= 0) {
637 mSource[SOURCE_SINK]->cancelBuffer(
638 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
639 mOutputFence);
640 }
641
642 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700643 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
644 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700645 if (result < 0)
646 return result;
647 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
648
Jesse Hallb716e572013-10-01 17:25:20 -0700649 // On GLES-only frames, we don't have the right output buffer acquire fence
650 // until after GLES calls queueBuffer(). So here we just set the buffer
651 // (for use in HWC prepare) but not the fence; we'll call this again with
652 // the proper fence once we have it.
653 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700654 mProducerBuffers[mOutputProducerSlot]);
655
656 return result;
657}
658
Jesse Hall38efe862013-04-06 23:12:29 -0700659// This slot mapping function is its own inverse, so two copies are unnecessary.
660// Both are kept to make the intent clear where the function is called, and for
661// the (unlikely) chance that we switch to a different mapping function.
662int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
663 if (source == SOURCE_SCRATCH) {
664 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
665 } else {
666 return sslot;
667 }
668}
669int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
670 return mapSource2ProducerSlot(source, pslot);
671}
672
673VirtualDisplaySurface::Source
674VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
675 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
676}
677
678const char* VirtualDisplaySurface::dbgStateStr() const {
679 switch (mDbgState) {
680 case DBG_STATE_IDLE: return "IDLE";
681 case DBG_STATE_PREPARED: return "PREPARED";
682 case DBG_STATE_GLES: return "GLES";
683 case DBG_STATE_GLES_DONE: return "GLES_DONE";
684 case DBG_STATE_HWC: return "HWC";
685 default: return "INVALID";
686 }
687}
688
689const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
690 switch (s) {
691 case SOURCE_SINK: return "SINK";
692 case SOURCE_SCRATCH: return "SCRATCH";
693 default: return "INVALID";
694 }
695}
696
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700697// ---------------------------------------------------------------------------
698} // namespace android
699// ---------------------------------------------------------------------------