blob: c5a4f99a4b4ec3828cfa238d602c10528047c3c7 [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"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070020
Dan Stoza11611f92015-03-12 15:12:44 -070021#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080022#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070023#include <gui/IProducerListener.h>
Dan Stoza11611f92015-03-12 15:12:44 -070024
Jesse Hall99c7dbb2013-03-14 14:29:29 -070025// ---------------------------------------------------------------------------
26namespace android {
27// ---------------------------------------------------------------------------
28
Jesse Hallc354eff2013-10-25 10:44:41 -070029#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
30static const bool sForceHwcCopy = true;
31#else
32static const bool sForceHwcCopy = false;
33#endif
Naseer Ahmed6a968462013-10-04 16:15:22 -040034
Jesse Hall24cd98e2014-07-13 14:37:16 -070035#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070036 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070037#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070038 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070039#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070040 mDisplayName.string(), ##__VA_ARGS__)
41
42static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
43 switch (type) {
44 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
45 case DisplaySurface::COMPOSITION_GLES: return "GLES";
46 case DisplaySurface::COMPOSITION_HWC: return "HWC";
47 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
48 default: return "<INVALID>";
49 }
50}
51
Jesse Hallffe1f192013-03-22 15:13:48 -070052VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070053 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070054 const sp<IGraphicBufferProducer>& bqProducer,
55 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070056 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070057: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070058 mHwc(hwc),
59 mDisplayId(dispId),
60 mDisplayName(name),
Dan Stoza9e56aa02015-11-02 13:00:03 -080061 mSource{},
62 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
63 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070064 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070065 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080066 mProducerBuffers(),
67 mQueueBufferOutput(),
68 mSinkBufferWidth(0),
69 mSinkBufferHeight(0),
70 mCompositionType(COMPOSITION_UNKNOWN),
71 mFbFence(Fence::NO_FENCE),
72 mOutputFence(Fence::NO_FENCE),
73 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
74 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070075 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080076 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
77 mMustRecompose(false)
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;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400140 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
141 // 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;
227 mHwcBufferCache->getHwcBuffer(mFbProducerSlot, fbBuffer,
228 &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,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800345 PixelFormat format, uint32_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
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700348 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700349 mSinkBufferWidth, mSinkBufferHeight, format, usage, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700350 if (result < 0)
351 return result;
352 int pslot = mapSource2ProducerSlot(source, *sslot);
353 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
354 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700355 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700356
Dan Stozafebd4f42014-04-09 16:14:51 -0700357 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700358 // This slot was previously dequeued from the other source; must
359 // re-request the buffer.
360 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700361 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700362 mProducerSlotSource |= sourceBit;
363 }
364
365 if (result & RELEASE_ALL_BUFFERS) {
366 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700367 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700368 mProducerBuffers[i].clear();
369 }
370 }
371 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700372 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
373 if (result < 0) {
374 mProducerBuffers[pslot].clear();
375 mSource[source]->cancelBuffer(*sslot, *fence);
376 return result;
377 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800378 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
379 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
380 mProducerBuffers[pslot]->getPixelFormat(),
381 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700382 }
383
384 return result;
385}
386
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700387status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700388 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
389 FrameEventHistoryDelta* outTimestamps) {
390 if (mDisplayId < 0) {
391 return mSource[SOURCE_SINK]->dequeueBuffer(
392 pslot, fence, w, h, format, usage, outTimestamps);
393 }
Michael Lentine47e45402014-07-18 15:34:25 -0700394
Jesse Hall38efe862013-04-06 23:12:29 -0700395 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
396 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
397 mDbgState = DBG_STATE_GLES;
398
399 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
400
Jesse Hall028dc8f2013-08-20 16:35:32 -0700401 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700402 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700403
Jesse Hall38efe862013-04-06 23:12:29 -0700404 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700405
406 if (mOutputProducerSlot < 0) {
407 // Last chance bailout if something bad happened earlier. For example,
408 // in a GLES configuration, if the sink disappears then dequeueBuffer
409 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
410 // will soldier on. So we end up here without a buffer. There should
411 // be lots of scary messages in the log just before this.
412 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
413 return NO_MEMORY;
414 }
415
Jesse Hall028dc8f2013-08-20 16:35:32 -0700416 // We already dequeued the output buffer. If the GLES driver wants
417 // something incompatible, we have to cancel and get a new one. This
418 // will mean that HWC will see a different output buffer between
419 // prepare and set, but since we're in GLES-only mode already it
420 // shouldn't matter.
421
Jesse Hall1e27ba22013-09-27 09:05:09 -0700422 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700423 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700424 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800425 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700426 (w != 0 && w != mSinkBufferWidth) ||
427 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700428 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
429 "want %dx%d fmt=%d use=%#x, "
430 "have %dx%d fmt=%d use=%#x",
431 w, h, format, usage,
432 mSinkBufferWidth, mSinkBufferHeight,
433 buf->getPixelFormat(), buf->getUsage());
434 mOutputFormat = format;
435 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700436 result = refreshOutputBuffer();
437 if (result < 0)
438 return result;
439 }
Jesse Hall38efe862013-04-06 23:12:29 -0700440 }
441
Jesse Hall028dc8f2013-08-20 16:35:32 -0700442 if (source == SOURCE_SINK) {
443 *pslot = mOutputProducerSlot;
444 *fence = mOutputFence;
445 } else {
446 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700447 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700448 if (result >= 0) {
449 *pslot = mapSource2ProducerSlot(source, sslot);
450 }
Jesse Hall38efe862013-04-06 23:12:29 -0700451 }
452 return result;
453}
454
Dan Stoza88a459a2014-03-12 09:34:36 -0700455status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
456 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
457 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800458}
459
Dan Stozad9822a32014-03-28 15:25:31 -0700460status_t VirtualDisplaySurface::detachNextBuffer(
461 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
462 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
463 return INVALID_OPERATION;
464}
465
Dan Stoza88a459a2014-03-12 09:34:36 -0700466status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
467 const sp<GraphicBuffer>& /* buffer */) {
468 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
469 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800470}
471
Jesse Hall38efe862013-04-06 23:12:29 -0700472status_t VirtualDisplaySurface::queueBuffer(int pslot,
473 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700474 if (mDisplayId < 0)
475 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
476
Jesse Hall38efe862013-04-06 23:12:29 -0700477 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
478 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
479 dbgStateStr());
480 mDbgState = DBG_STATE_GLES_DONE;
481
482 VDS_LOGV("queueBuffer pslot=%d", pslot);
483
484 status_t result;
485 if (mCompositionType == COMPOSITION_MIXED) {
486 // Queue the buffer back into the scratch pool
487 QueueBufferOutput scratchQBO;
488 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700489 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700490 if (result != NO_ERROR)
491 return result;
492
493 // Now acquire the buffer from the scratch pool -- should be the same
494 // slot and fence as we just queued.
495 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700496 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700497 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700498 if (result != NO_ERROR)
499 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700500 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700501 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700502 item.mSlot, sslot);
503 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
504 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700505
506 } else {
507 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
508 "Unexpected queueBuffer in state %s for compositionType %s",
509 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
510
511 // Extract the GLES release fence for HWC to acquire
512 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700513 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800514 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700515 Rect crop;
516 int scalingMode;
517 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800518 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700519 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700520
521 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700522 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700523 }
524
Brian Anderson3d4039d2016-09-23 16:31:30 -0700525 // This moves the frame timestamps and keeps a copy of all other fields.
526 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700527 return NO_ERROR;
528}
529
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700530status_t VirtualDisplaySurface::cancelBuffer(int pslot,
531 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700532 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700533 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700534
Jesse Hall38efe862013-04-06 23:12:29 -0700535 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
536 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
537 dbgStateStr());
538 VDS_LOGV("cancelBuffer pslot=%d", pslot);
539 Source source = fbSourceForCompositionType(mCompositionType);
540 return mSource[source]->cancelBuffer(
541 mapProducer2SourceSlot(source, pslot), fence);
542}
543
544int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700545 switch (what) {
546 case NATIVE_WINDOW_WIDTH:
547 *value = mSinkBufferWidth;
548 break;
549 case NATIVE_WINDOW_HEIGHT:
550 *value = mSinkBufferHeight;
551 break;
552 default:
553 return mSource[SOURCE_SINK]->query(what, value);
554 }
555 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700556}
557
Dan Stozaf0eaf252014-03-21 13:05:51 -0700558status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700559 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700560 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700561 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700562 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
563 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700564 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700565 updateQueueBufferOutput(std::move(qbo));
566 // This moves the frame timestamps and keeps a copy of all other fields.
567 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700568 }
569 return result;
570}
571
Robert Carr97b9c862016-09-08 13:54:35 -0700572status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
573 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700574}
575
Jesse Hall399184a2014-03-03 15:42:54 -0800576status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
577 return INVALID_OPERATION;
578}
579
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700580void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
581 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700582 // TODO: Should we actually allocate buffers for a virtual display?
583}
584
Dan Stoza9de72932015-04-16 17:28:43 -0700585status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
586 return INVALID_OPERATION;
587}
588
Dan Stoza812ed062015-06-02 15:45:22 -0700589status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
590 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
591 return INVALID_OPERATION;
592}
593
Dan Stozac6f30bd2015-06-08 09:32:50 -0700594String8 VirtualDisplaySurface::getConsumerName() const {
595 return String8("VirtualDisplaySurface");
596}
597
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700598status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
599 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800600 return INVALID_OPERATION;
601}
602
603status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
604 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
605 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700606}
607
Dan Stoza127fc632015-06-30 13:43:32 -0700608status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
609 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
610 return INVALID_OPERATION;
611}
612
Dan Stoza50101d02016-04-07 16:53:23 -0700613status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700614 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
615 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700616 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
617 return INVALID_OPERATION;
618}
619
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700620status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
621 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
622 return INVALID_OPERATION;
623}
624
Jesse Hall38efe862013-04-06 23:12:29 -0700625void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700626 QueueBufferOutput&& qbo) {
627 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700628 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700629}
630
631void VirtualDisplaySurface::resetPerFrameState() {
632 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530633 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700634 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700635 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530636 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700637}
638
Jesse Hall028dc8f2013-08-20 16:35:32 -0700639status_t VirtualDisplaySurface::refreshOutputBuffer() {
640 if (mOutputProducerSlot >= 0) {
641 mSource[SOURCE_SINK]->cancelBuffer(
642 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
643 mOutputFence);
644 }
645
646 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700647 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
648 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700649 if (result < 0)
650 return result;
651 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
652
Jesse Hallb716e572013-10-01 17:25:20 -0700653 // On GLES-only frames, we don't have the right output buffer acquire fence
654 // until after GLES calls queueBuffer(). So here we just set the buffer
655 // (for use in HWC prepare) but not the fence; we'll call this again with
656 // the proper fence once we have it.
657 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700658 mProducerBuffers[mOutputProducerSlot]);
659
660 return result;
661}
662
Jesse Hall38efe862013-04-06 23:12:29 -0700663// This slot mapping function is its own inverse, so two copies are unnecessary.
664// Both are kept to make the intent clear where the function is called, and for
665// the (unlikely) chance that we switch to a different mapping function.
666int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
667 if (source == SOURCE_SCRATCH) {
668 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
669 } else {
670 return sslot;
671 }
672}
673int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
674 return mapSource2ProducerSlot(source, pslot);
675}
676
677VirtualDisplaySurface::Source
678VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
679 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
680}
681
682const char* VirtualDisplaySurface::dbgStateStr() const {
683 switch (mDbgState) {
684 case DBG_STATE_IDLE: return "IDLE";
685 case DBG_STATE_PREPARED: return "PREPARED";
686 case DBG_STATE_GLES: return "GLES";
687 case DBG_STATE_GLES_DONE: return "GLES_DONE";
688 case DBG_STATE_HWC: return "HWC";
689 default: return "INVALID";
690 }
691}
692
693const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
694 switch (s) {
695 case SOURCE_SINK: return "SINK";
696 case SOURCE_SCRATCH: return "SCRATCH";
697 default: return "INVALID";
698 }
699}
700
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700701// ---------------------------------------------------------------------------
702} // namespace android
703// ---------------------------------------------------------------------------