blob: 1afed360f6e2d3aed3b34b6ca5158cf78ead9e98 [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>
Pablo Ceballosf133d002015-10-23 12:10:13 -070022#include <gui/IProducerListener.h>
Dan Stoza11611f92015-03-12 15:12:44 -070023
Jesse Hall99c7dbb2013-03-14 14:29:29 -070024// ---------------------------------------------------------------------------
25namespace android {
26// ---------------------------------------------------------------------------
27
Jesse Hallc354eff2013-10-25 10:44:41 -070028#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
29static const bool sForceHwcCopy = true;
30#else
31static const bool sForceHwcCopy = false;
32#endif
Naseer Ahmed6a968462013-10-04 16:15:22 -040033
Jesse Hall24cd98e2014-07-13 14:37:16 -070034#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070035 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070036#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070037 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070038#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070039 mDisplayName.string(), ##__VA_ARGS__)
40
41static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
42 switch (type) {
43 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
44 case DisplaySurface::COMPOSITION_GLES: return "GLES";
45 case DisplaySurface::COMPOSITION_HWC: return "HWC";
46 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
47 default: return "<INVALID>";
48 }
49}
50
Jesse Hallffe1f192013-03-22 15:13:48 -070051VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070052 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070053 const sp<IGraphicBufferProducer>& bqProducer,
54 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070056: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070057 mHwc(hwc),
58 mDisplayId(dispId),
59 mDisplayName(name),
Dan Stoza9e56aa02015-11-02 13:00:03 -080060 mSource{},
61 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
62 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070063 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070064 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mProducerBuffers(),
66 mQueueBufferOutput(),
67 mSinkBufferWidth(0),
68 mSinkBufferHeight(0),
69 mCompositionType(COMPOSITION_UNKNOWN),
70 mFbFence(Fence::NO_FENCE),
71 mOutputFence(Fence::NO_FENCE),
72 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
73 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070074 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080075 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
76 mMustRecompose(false)
Jesse Hallffe1f192013-03-22 15:13:48 -070077{
Jesse Hall38efe862013-04-06 23:12:29 -070078 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070079 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070080
81 resetPerFrameState();
82
83 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080084 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
85 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070086 mSinkBufferWidth = sinkWidth;
87 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080088
89 // Pick the buffer format to request from the sink when not rendering to it
90 // with GLES. If the consumer needs CPU access, use the default format
91 // set by the consumer. Otherwise allow gralloc to decide the format based
92 // on usage bits.
93 int sinkUsage;
94 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
95 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
96 int sinkFormat;
97 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
98 mDefaultOutputFormat = sinkFormat;
99 } else {
100 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
101 }
102 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700103
104 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700105 mConsumer->setConsumerName(ConsumerBase::mName);
106 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
107 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700108 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -0700109 IGraphicBufferProducer::QueueBufferOutput output;
110 mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700111}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700112
113VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700114 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700115}
116
Dan Stoza71433162014-02-04 16:22:36 -0800117status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700118 if (mDisplayId < 0)
119 return NO_ERROR;
120
Dan Stoza71433162014-02-04 16:22:36 -0800121 mMustRecompose = mustRecompose;
122
Jesse Hall38efe862013-04-06 23:12:29 -0700123 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700124 "Unexpected beginFrame() in %s state", dbgStateStr());
125 mDbgState = DBG_STATE_BEGUN;
126
Jesse Hall028dc8f2013-08-20 16:35:32 -0700127 return refreshOutputBuffer();
128}
129
130status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
131 if (mDisplayId < 0)
132 return NO_ERROR;
133
134 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700135 "Unexpected prepareFrame() in %s state", dbgStateStr());
136 mDbgState = DBG_STATE_PREPARED;
137
138 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400139 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
140 // Some hardware can do RGB->YUV conversion more efficiently in hardware
141 // controlled by HWC than in hardware controlled by the video encoder.
142 // Forcing GLES-composed frames to go through an extra copy by the HWC
143 // allows the format conversion to happen there, rather than passing RGB
144 // directly to the consumer.
145 //
146 // On the other hand, when the consumer prefers RGB or can consume RGB
147 // inexpensively, this forces an unnecessary copy.
148 mCompositionType = COMPOSITION_MIXED;
149 }
150
Jesse Hall38efe862013-04-06 23:12:29 -0700151 if (mCompositionType != mDbgLastCompositionType) {
152 VDS_LOGV("prepareFrame: composition type changed to %s",
153 dbgCompositionTypeStr(mCompositionType));
154 mDbgLastCompositionType = mCompositionType;
155 }
156
Jesse Hall1e27ba22013-09-27 09:05:09 -0700157 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800158 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700159 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
160 // We must have just switched from GLES-only to MIXED or HWC
161 // composition. Stop using the format and usage requested by the GLES
162 // driver; they may be suboptimal when HWC is writing to the output
163 // buffer. For example, if the output is going to a video encoder, and
164 // HWC can write directly to YUV, some hardware can skip a
165 // memory-to-memory RGB-to-YUV conversion step.
166 //
167 // If we just switched *to* GLES-only mode, we'll change the
168 // format/usage and get a new buffer when the GLES driver calls
169 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800170 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700171 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
172 refreshOutputBuffer();
173 }
174
Jesse Hall38efe862013-04-06 23:12:29 -0700175 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700176}
177
Dan Stoza9e56aa02015-11-02 13:00:03 -0800178#ifndef USE_HWC2
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700179status_t VirtualDisplaySurface::compositionComplete() {
180 return NO_ERROR;
181}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800182#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700183
184status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700185 if (mDisplayId < 0)
186 return NO_ERROR;
187
188 if (mCompositionType == COMPOSITION_HWC) {
189 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
190 "Unexpected advanceFrame() in %s state on HWC frame",
191 dbgStateStr());
192 } else {
193 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
194 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
195 dbgStateStr());
196 }
197 mDbgState = DBG_STATE_HWC;
198
Jesse Hall14e8b012013-11-07 12:28:26 -0800199 if (mOutputProducerSlot < 0 ||
200 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700201 // Last chance bailout if something bad happened earlier. For example,
202 // in a GLES configuration, if the sink disappears then dequeueBuffer
203 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
204 // will soldier on. So we end up here without a buffer. There should
205 // be lots of scary messages in the log just before this.
206 VDS_LOGE("advanceFrame: no buffer, bailing out");
207 return NO_MEMORY;
208 }
209
Jesse Hall14e8b012013-11-07 12:28:26 -0800210 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
211 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700212 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
213 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
214 mFbProducerSlot, fbBuffer.get(),
215 mOutputProducerSlot, outBuffer.get());
216
Jesse Hallb716e572013-10-01 17:25:20 -0700217 // At this point we know the output buffer acquire fence,
218 // so update HWC state with it.
219 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
220
Jesse Hall14e8b012013-11-07 12:28:26 -0800221 status_t result = NO_ERROR;
222 if (fbBuffer != NULL) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223#ifdef USE_HWC2
224 // TODO: Correctly propagate the dataspace from GL composition
225 result = mHwc.setClientTarget(mDisplayId, mFbFence, fbBuffer,
226 HAL_DATASPACE_UNKNOWN);
227#else
Jesse Hall14e8b012013-11-07 12:28:26 -0800228 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229#endif
Jesse Hall14e8b012013-11-07 12:28:26 -0800230 }
231
232 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700233}
234
Jesse Hall851cfe82013-03-20 13:44:00 -0700235void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700236 if (mDisplayId < 0)
237 return;
238
239 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
240 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
241 mDbgState = DBG_STATE_IDLE;
242
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243#ifdef USE_HWC2
244 sp<Fence> retireFence = mHwc.getRetireFence(mDisplayId);
245#else
Jesse Hall38efe862013-04-06 23:12:29 -0700246 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700248 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
249 // release the scratch buffer back to the pool
250 Mutex::Autolock lock(mMutex);
251 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
252 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253#ifdef USE_HWC2
254 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
255 retireFence);
256#else
Jesse Hall38efe862013-04-06 23:12:29 -0700257 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700259 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
260 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
261 }
262
263 if (mOutputProducerSlot >= 0) {
264 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
265 QueueBufferOutput qbo;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800266#ifndef USE_HWC2
Jesse Hall38efe862013-04-06 23:12:29 -0700267 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800268#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700269 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800270 if (mMustRecompose) {
271 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
272 QueueBufferInput(
273 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800274 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800275 Rect(mSinkBufferWidth, mSinkBufferHeight),
276 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277#ifdef USE_HWC2
278 retireFence),
279#else
Dan Stoza71433162014-02-04 16:22:36 -0800280 outFence),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800281#endif
Dan Stoza71433162014-02-04 16:22:36 -0800282 &qbo);
283 if (result == NO_ERROR) {
284 updateQueueBufferOutput(qbo);
285 }
286 } else {
287 // If the surface hadn't actually been updated, then we only went
288 // through the motions of updating the display to keep our state
289 // machine happy. We cancel the buffer to avoid triggering another
290 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800291#ifdef USE_HWC2
292 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
293#else
Dan Stoza71433162014-02-04 16:22:36 -0800294 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700296 }
297 }
298
299 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700300}
301
Dan Stoza01049c82014-11-11 10:32:31 -0800302void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700303}
304
Michael Lentine47e45402014-07-18 15:34:25 -0700305void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
306 uint32_t tmpW, tmpH, transformHint, numPendingBuffers;
307 mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers);
308 mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers);
309
310 mSinkBufferWidth = w;
311 mSinkBufferHeight = h;
312}
313
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314#ifdef USE_HWC2
315const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
316 return mFbFence;
317}
318#endif
319
Jesse Hall38efe862013-04-06 23:12:29 -0700320status_t VirtualDisplaySurface::requestBuffer(int pslot,
321 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700322 if (mDisplayId < 0)
323 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
324
Jesse Hall38efe862013-04-06 23:12:29 -0700325 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
326 "Unexpected requestBuffer pslot=%d in %s state",
327 pslot, dbgStateStr());
328
329 *outBuf = mProducerBuffers[pslot];
330 return NO_ERROR;
331}
332
Pablo Ceballosfa455352015-08-12 17:47:47 -0700333status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
334 int maxDequeuedBuffers) {
335 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
336}
337
338status_t VirtualDisplaySurface::setAsyncMode(bool async) {
339 return mSource[SOURCE_SINK]->setAsyncMode(async);
340}
341
Jesse Hall38efe862013-04-06 23:12:29 -0700342status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800343 PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700344 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700345
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700346 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700347 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700348 if (result < 0)
349 return result;
350 int pslot = mapSource2ProducerSlot(source, *sslot);
351 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
352 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700353 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700354
Dan Stozafebd4f42014-04-09 16:14:51 -0700355 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700356 // This slot was previously dequeued from the other source; must
357 // re-request the buffer.
358 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700359 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700360 mProducerSlotSource |= sourceBit;
361 }
362
363 if (result & RELEASE_ALL_BUFFERS) {
364 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700365 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700366 mProducerBuffers[i].clear();
367 }
368 }
369 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700370 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
371 if (result < 0) {
372 mProducerBuffers[pslot].clear();
373 mSource[source]->cancelBuffer(*sslot, *fence);
374 return result;
375 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800376 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
377 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
378 mProducerBuffers[pslot]->getPixelFormat(),
379 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700380 }
381
382 return result;
383}
384
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700385status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800386 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) {
Michael Lentine47e45402014-07-18 15:34:25 -0700387 if (mDisplayId < 0)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700388 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage);
Michael Lentine47e45402014-07-18 15:34:25 -0700389
Jesse Hall38efe862013-04-06 23:12:29 -0700390 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
391 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
392 mDbgState = DBG_STATE_GLES;
393
394 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
395
Jesse Hall028dc8f2013-08-20 16:35:32 -0700396 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700397 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398
Jesse Hall38efe862013-04-06 23:12:29 -0700399 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700400
401 if (mOutputProducerSlot < 0) {
402 // Last chance bailout if something bad happened earlier. For example,
403 // in a GLES configuration, if the sink disappears then dequeueBuffer
404 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
405 // will soldier on. So we end up here without a buffer. There should
406 // be lots of scary messages in the log just before this.
407 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
408 return NO_MEMORY;
409 }
410
Jesse Hall028dc8f2013-08-20 16:35:32 -0700411 // We already dequeued the output buffer. If the GLES driver wants
412 // something incompatible, we have to cancel and get a new one. This
413 // will mean that HWC will see a different output buffer between
414 // prepare and set, but since we're in GLES-only mode already it
415 // shouldn't matter.
416
Jesse Hall1e27ba22013-09-27 09:05:09 -0700417 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700418 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700419 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800420 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700421 (w != 0 && w != mSinkBufferWidth) ||
422 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700423 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
424 "want %dx%d fmt=%d use=%#x, "
425 "have %dx%d fmt=%d use=%#x",
426 w, h, format, usage,
427 mSinkBufferWidth, mSinkBufferHeight,
428 buf->getPixelFormat(), buf->getUsage());
429 mOutputFormat = format;
430 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700431 result = refreshOutputBuffer();
432 if (result < 0)
433 return result;
434 }
Jesse Hall38efe862013-04-06 23:12:29 -0700435 }
436
Jesse Hall028dc8f2013-08-20 16:35:32 -0700437 if (source == SOURCE_SINK) {
438 *pslot = mOutputProducerSlot;
439 *fence = mOutputFence;
440 } else {
441 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700442 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700443 if (result >= 0) {
444 *pslot = mapSource2ProducerSlot(source, sslot);
445 }
Jesse Hall38efe862013-04-06 23:12:29 -0700446 }
447 return result;
448}
449
Dan Stoza88a459a2014-03-12 09:34:36 -0700450status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
451 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
452 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800453}
454
Dan Stozad9822a32014-03-28 15:25:31 -0700455status_t VirtualDisplaySurface::detachNextBuffer(
456 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
457 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
458 return INVALID_OPERATION;
459}
460
Dan Stoza88a459a2014-03-12 09:34:36 -0700461status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
462 const sp<GraphicBuffer>& /* buffer */) {
463 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
464 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800465}
466
Jesse Hall38efe862013-04-06 23:12:29 -0700467status_t VirtualDisplaySurface::queueBuffer(int pslot,
468 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700469 if (mDisplayId < 0)
470 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
471
Jesse Hall38efe862013-04-06 23:12:29 -0700472 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
473 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
474 dbgStateStr());
475 mDbgState = DBG_STATE_GLES_DONE;
476
477 VDS_LOGV("queueBuffer pslot=%d", pslot);
478
479 status_t result;
480 if (mCompositionType == COMPOSITION_MIXED) {
481 // Queue the buffer back into the scratch pool
482 QueueBufferOutput scratchQBO;
483 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700484 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700485 if (result != NO_ERROR)
486 return result;
487
488 // Now acquire the buffer from the scratch pool -- should be the same
489 // slot and fence as we just queued.
490 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700491 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700492 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700493 if (result != NO_ERROR)
494 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700495 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700496 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700497 item.mSlot, sslot);
498 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
499 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700500
501 } else {
502 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
503 "Unexpected queueBuffer in state %s for compositionType %s",
504 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
505
506 // Extract the GLES release fence for HWC to acquire
507 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700508 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800509 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700510 Rect crop;
511 int scalingMode;
512 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800513 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700514 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700515
516 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700517 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700518 }
519
520 *output = mQueueBufferOutput;
521 return NO_ERROR;
522}
523
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700524status_t VirtualDisplaySurface::cancelBuffer(int pslot,
525 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700526 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700527 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700528
Jesse Hall38efe862013-04-06 23:12:29 -0700529 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
530 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
531 dbgStateStr());
532 VDS_LOGV("cancelBuffer pslot=%d", pslot);
533 Source source = fbSourceForCompositionType(mCompositionType);
534 return mSource[source]->cancelBuffer(
535 mapProducer2SourceSlot(source, pslot), fence);
536}
537
538int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700539 switch (what) {
540 case NATIVE_WINDOW_WIDTH:
541 *value = mSinkBufferWidth;
542 break;
543 case NATIVE_WINDOW_HEIGHT:
544 *value = mSinkBufferHeight;
545 break;
546 default:
547 return mSource[SOURCE_SINK]->query(what, value);
548 }
549 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700550}
551
Dan Stozaf0eaf252014-03-21 13:05:51 -0700552status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700553 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700554 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700555 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700556 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
557 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700558 if (result == NO_ERROR) {
559 updateQueueBufferOutput(qbo);
560 *output = mQueueBufferOutput;
561 }
562 return result;
563}
564
565status_t VirtualDisplaySurface::disconnect(int api) {
566 return mSource[SOURCE_SINK]->disconnect(api);
567}
568
Jesse Hall399184a2014-03-03 15:42:54 -0800569status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
570 return INVALID_OPERATION;
571}
572
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700573void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
574 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700575 // TODO: Should we actually allocate buffers for a virtual display?
576}
577
Dan Stoza9de72932015-04-16 17:28:43 -0700578status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
579 return INVALID_OPERATION;
580}
581
Dan Stoza812ed062015-06-02 15:45:22 -0700582status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
583 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
584 return INVALID_OPERATION;
585}
586
Dan Stozac6f30bd2015-06-08 09:32:50 -0700587String8 VirtualDisplaySurface::getConsumerName() const {
588 return String8("VirtualDisplaySurface");
589}
590
Dan Stoza7dde5992015-05-22 09:51:44 -0700591uint64_t VirtualDisplaySurface::getNextFrameNumber() const {
592 return 0;
593}
594
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800595status_t VirtualDisplaySurface::setSingleBufferMode(bool /*singleBufferMode*/) {
596 ALOGE("setSingleBufferMode not supported on VirtualDisplaySurface");
597 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
Jesse Hall38efe862013-04-06 23:12:29 -0700610void VirtualDisplaySurface::updateQueueBufferOutput(
611 const QueueBufferOutput& qbo) {
612 uint32_t w, h, transformHint, numPendingBuffers;
613 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
614 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
615}
616
617void VirtualDisplaySurface::resetPerFrameState() {
618 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530619 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700620 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700621 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530622 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700623}
624
Jesse Hall028dc8f2013-08-20 16:35:32 -0700625status_t VirtualDisplaySurface::refreshOutputBuffer() {
626 if (mOutputProducerSlot >= 0) {
627 mSource[SOURCE_SINK]->cancelBuffer(
628 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
629 mOutputFence);
630 }
631
632 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700633 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
634 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700635 if (result < 0)
636 return result;
637 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
638
Jesse Hallb716e572013-10-01 17:25:20 -0700639 // On GLES-only frames, we don't have the right output buffer acquire fence
640 // until after GLES calls queueBuffer(). So here we just set the buffer
641 // (for use in HWC prepare) but not the fence; we'll call this again with
642 // the proper fence once we have it.
643 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700644 mProducerBuffers[mOutputProducerSlot]);
645
646 return result;
647}
648
Jesse Hall38efe862013-04-06 23:12:29 -0700649// This slot mapping function is its own inverse, so two copies are unnecessary.
650// Both are kept to make the intent clear where the function is called, and for
651// the (unlikely) chance that we switch to a different mapping function.
652int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
653 if (source == SOURCE_SCRATCH) {
654 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
655 } else {
656 return sslot;
657 }
658}
659int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
660 return mapSource2ProducerSlot(source, pslot);
661}
662
663VirtualDisplaySurface::Source
664VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
665 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
666}
667
668const char* VirtualDisplaySurface::dbgStateStr() const {
669 switch (mDbgState) {
670 case DBG_STATE_IDLE: return "IDLE";
671 case DBG_STATE_PREPARED: return "PREPARED";
672 case DBG_STATE_GLES: return "GLES";
673 case DBG_STATE_GLES_DONE: return "GLES_DONE";
674 case DBG_STATE_HWC: return "HWC";
675 default: return "INVALID";
676 }
677}
678
679const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
680 switch (s) {
681 case SOURCE_SINK: return "SINK";
682 case SOURCE_SCRATCH: return "SCRATCH";
683 default: return "INVALID";
684 }
685}
686
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700687// ---------------------------------------------------------------------------
688} // namespace android
689// ---------------------------------------------------------------------------