blob: c415560321b833bc8692b35eb609daaef51458dd [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
21// ---------------------------------------------------------------------------
22namespace android {
23// ---------------------------------------------------------------------------
24
Jesse Hallc354eff2013-10-25 10:44:41 -070025#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
26static const bool sForceHwcCopy = true;
27#else
28static const bool sForceHwcCopy = false;
29#endif
Naseer Ahmed6a968462013-10-04 16:15:22 -040030
Jesse Hall38efe862013-04-06 23:12:29 -070031#define VDS_LOGE(msg, ...) ALOGE("[%s] "msg, \
32 mDisplayName.string(), ##__VA_ARGS__)
33#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] "msg, \
34 mDisplayName.string(), ##__VA_ARGS__)
35#define VDS_LOGV(msg, ...) ALOGV("[%s] "msg, \
36 mDisplayName.string(), ##__VA_ARGS__)
37
38static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
39 switch (type) {
40 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
41 case DisplaySurface::COMPOSITION_GLES: return "GLES";
42 case DisplaySurface::COMPOSITION_HWC: return "HWC";
43 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
44 default: return "<INVALID>";
45 }
46}
47
Jesse Hallffe1f192013-03-22 15:13:48 -070048VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070049 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070050 const sp<IGraphicBufferProducer>& bqProducer,
51 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070052 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070053: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070054 mHwc(hwc),
55 mDisplayId(dispId),
56 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070057 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070058 mProducerSlotSource(0),
59 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080060 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
61 mMustRecompose(false)
Jesse Hallffe1f192013-03-22 15:13:48 -070062{
Jesse Hall38efe862013-04-06 23:12:29 -070063 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070064 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070065
66 resetPerFrameState();
67
68 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080069 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
70 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
71
72 // Pick the buffer format to request from the sink when not rendering to it
73 // with GLES. If the consumer needs CPU access, use the default format
74 // set by the consumer. Otherwise allow gralloc to decide the format based
75 // on usage bits.
76 int sinkUsage;
77 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
78 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
79 int sinkFormat;
80 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
81 mDefaultOutputFormat = sinkFormat;
82 } else {
83 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
84 }
85 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -070086
87 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070088 mConsumer->setConsumerName(ConsumerBase::mName);
89 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
90 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
91 mConsumer->setDefaultMaxBufferCount(2);
Jesse Hallffe1f192013-03-22 15:13:48 -070092}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070093
94VirtualDisplaySurface::~VirtualDisplaySurface() {
95}
96
Dan Stoza71433162014-02-04 16:22:36 -080097status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -070098 if (mDisplayId < 0)
99 return NO_ERROR;
100
Dan Stoza71433162014-02-04 16:22:36 -0800101 mMustRecompose = mustRecompose;
102
Jesse Hall38efe862013-04-06 23:12:29 -0700103 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700104 "Unexpected beginFrame() in %s state", dbgStateStr());
105 mDbgState = DBG_STATE_BEGUN;
106
107 uint32_t transformHint, numPendingBuffers;
108 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
109 &transformHint, &numPendingBuffers);
110
111 return refreshOutputBuffer();
112}
113
114status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
115 if (mDisplayId < 0)
116 return NO_ERROR;
117
118 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700119 "Unexpected prepareFrame() in %s state", dbgStateStr());
120 mDbgState = DBG_STATE_PREPARED;
121
122 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400123 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
124 // Some hardware can do RGB->YUV conversion more efficiently in hardware
125 // controlled by HWC than in hardware controlled by the video encoder.
126 // Forcing GLES-composed frames to go through an extra copy by the HWC
127 // allows the format conversion to happen there, rather than passing RGB
128 // directly to the consumer.
129 //
130 // On the other hand, when the consumer prefers RGB or can consume RGB
131 // inexpensively, this forces an unnecessary copy.
132 mCompositionType = COMPOSITION_MIXED;
133 }
134
Jesse Hall38efe862013-04-06 23:12:29 -0700135 if (mCompositionType != mDbgLastCompositionType) {
136 VDS_LOGV("prepareFrame: composition type changed to %s",
137 dbgCompositionTypeStr(mCompositionType));
138 mDbgLastCompositionType = mCompositionType;
139 }
140
Jesse Hall1e27ba22013-09-27 09:05:09 -0700141 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800142 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700143 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
144 // We must have just switched from GLES-only to MIXED or HWC
145 // composition. Stop using the format and usage requested by the GLES
146 // driver; they may be suboptimal when HWC is writing to the output
147 // buffer. For example, if the output is going to a video encoder, and
148 // HWC can write directly to YUV, some hardware can skip a
149 // memory-to-memory RGB-to-YUV conversion step.
150 //
151 // If we just switched *to* GLES-only mode, we'll change the
152 // format/usage and get a new buffer when the GLES driver calls
153 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800154 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700155 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
156 refreshOutputBuffer();
157 }
158
Jesse Hall38efe862013-04-06 23:12:29 -0700159 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700160}
161
162status_t VirtualDisplaySurface::compositionComplete() {
163 return NO_ERROR;
164}
165
166status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700167 if (mDisplayId < 0)
168 return NO_ERROR;
169
170 if (mCompositionType == COMPOSITION_HWC) {
171 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
172 "Unexpected advanceFrame() in %s state on HWC frame",
173 dbgStateStr());
174 } else {
175 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
176 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
177 dbgStateStr());
178 }
179 mDbgState = DBG_STATE_HWC;
180
Jesse Hall14e8b012013-11-07 12:28:26 -0800181 if (mOutputProducerSlot < 0 ||
182 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700183 // Last chance bailout if something bad happened earlier. For example,
184 // in a GLES configuration, if the sink disappears then dequeueBuffer
185 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
186 // will soldier on. So we end up here without a buffer. There should
187 // be lots of scary messages in the log just before this.
188 VDS_LOGE("advanceFrame: no buffer, bailing out");
189 return NO_MEMORY;
190 }
191
Jesse Hall14e8b012013-11-07 12:28:26 -0800192 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
193 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700194 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
195 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
196 mFbProducerSlot, fbBuffer.get(),
197 mOutputProducerSlot, outBuffer.get());
198
Jesse Hallb716e572013-10-01 17:25:20 -0700199 // At this point we know the output buffer acquire fence,
200 // so update HWC state with it.
201 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
202
Jesse Hall14e8b012013-11-07 12:28:26 -0800203 status_t result = NO_ERROR;
204 if (fbBuffer != NULL) {
205 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
206 }
207
208 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700209}
210
Jesse Hall851cfe82013-03-20 13:44:00 -0700211void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700212 if (mDisplayId < 0)
213 return;
214
215 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
216 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
217 mDbgState = DBG_STATE_IDLE;
218
219 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
220 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
221 // release the scratch buffer back to the pool
222 Mutex::Autolock lock(mMutex);
223 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
224 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
225 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
226 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
227 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
228 }
229
230 if (mOutputProducerSlot >= 0) {
231 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
232 QueueBufferOutput qbo;
233 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
234 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800235 if (mMustRecompose) {
236 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
237 QueueBufferInput(
238 systemTime(), false /* isAutoTimestamp */,
239 Rect(mSinkBufferWidth, mSinkBufferHeight),
240 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
241 true /* async*/,
242 outFence),
243 &qbo);
244 if (result == NO_ERROR) {
245 updateQueueBufferOutput(qbo);
246 }
247 } else {
248 // If the surface hadn't actually been updated, then we only went
249 // through the motions of updating the display to keep our state
250 // machine happy. We cancel the buffer to avoid triggering another
251 // re-composition and causing an infinite loop.
252 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700253 }
254 }
255
256 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700257}
258
Dan Stozafebd4f42014-04-09 16:14:51 -0700259void VirtualDisplaySurface::dump(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700260}
261
Jesse Hall38efe862013-04-06 23:12:29 -0700262status_t VirtualDisplaySurface::requestBuffer(int pslot,
263 sp<GraphicBuffer>* outBuf) {
264 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
265 "Unexpected requestBuffer pslot=%d in %s state",
266 pslot, dbgStateStr());
267
268 *outBuf = mProducerBuffers[pslot];
269 return NO_ERROR;
270}
271
272status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
273 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
274}
275
276status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700277 uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Jesse Hall8db92552013-08-29 16:03:50 -0700278 // Don't let a slow consumer block us
279 bool async = (source == SOURCE_SINK);
280
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700281 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700282 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700283 if (result < 0)
284 return result;
285 int pslot = mapSource2ProducerSlot(source, *sslot);
286 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
287 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700288 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700289
Dan Stozafebd4f42014-04-09 16:14:51 -0700290 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700291 // This slot was previously dequeued from the other source; must
292 // re-request the buffer.
293 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700294 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700295 mProducerSlotSource |= sourceBit;
296 }
297
298 if (result & RELEASE_ALL_BUFFERS) {
299 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700300 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700301 mProducerBuffers[i].clear();
302 }
303 }
304 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700305 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
306 if (result < 0) {
307 mProducerBuffers[pslot].clear();
308 mSource[source]->cancelBuffer(*sslot, *fence);
309 return result;
310 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800311 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
312 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
313 mProducerBuffers[pslot]->getPixelFormat(),
314 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700315 }
316
317 return result;
318}
319
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700320status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Jesse Hall38efe862013-04-06 23:12:29 -0700321 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
322 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
323 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
324 mDbgState = DBG_STATE_GLES;
325
Jesse Hall8db92552013-08-29 16:03:50 -0700326 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
Jesse Hall38efe862013-04-06 23:12:29 -0700327 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
328
Jesse Hall028dc8f2013-08-20 16:35:32 -0700329 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700330 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700331
Jesse Hall38efe862013-04-06 23:12:29 -0700332 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700333
334 if (mOutputProducerSlot < 0) {
335 // Last chance bailout if something bad happened earlier. For example,
336 // in a GLES configuration, if the sink disappears then dequeueBuffer
337 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
338 // will soldier on. So we end up here without a buffer. There should
339 // be lots of scary messages in the log just before this.
340 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
341 return NO_MEMORY;
342 }
343
Jesse Hall028dc8f2013-08-20 16:35:32 -0700344 // We already dequeued the output buffer. If the GLES driver wants
345 // something incompatible, we have to cancel and get a new one. This
346 // will mean that HWC will see a different output buffer between
347 // prepare and set, but since we're in GLES-only mode already it
348 // shouldn't matter.
349
Jesse Hall1e27ba22013-09-27 09:05:09 -0700350 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700351 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700352 if ((usage & ~buf->getUsage()) != 0 ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700353 (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
354 (w != 0 && w != mSinkBufferWidth) ||
355 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700356 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
357 "want %dx%d fmt=%d use=%#x, "
358 "have %dx%d fmt=%d use=%#x",
359 w, h, format, usage,
360 mSinkBufferWidth, mSinkBufferHeight,
361 buf->getPixelFormat(), buf->getUsage());
362 mOutputFormat = format;
363 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700364 result = refreshOutputBuffer();
365 if (result < 0)
366 return result;
367 }
Jesse Hall38efe862013-04-06 23:12:29 -0700368 }
369
Jesse Hall028dc8f2013-08-20 16:35:32 -0700370 if (source == SOURCE_SINK) {
371 *pslot = mOutputProducerSlot;
372 *fence = mOutputFence;
373 } else {
374 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700375 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700376 if (result >= 0) {
377 *pslot = mapSource2ProducerSlot(source, sslot);
378 }
Jesse Hall38efe862013-04-06 23:12:29 -0700379 }
380 return result;
381}
382
Dan Stoza88a459a2014-03-12 09:34:36 -0700383status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
384 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
385 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386}
387
Dan Stozad9822a32014-03-28 15:25:31 -0700388status_t VirtualDisplaySurface::detachNextBuffer(
389 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
390 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
391 return INVALID_OPERATION;
392}
393
Dan Stoza88a459a2014-03-12 09:34:36 -0700394status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
395 const sp<GraphicBuffer>& /* buffer */) {
396 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
397 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800398}
399
Jesse Hall38efe862013-04-06 23:12:29 -0700400status_t VirtualDisplaySurface::queueBuffer(int pslot,
401 const QueueBufferInput& input, QueueBufferOutput* output) {
402 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
403 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
404 dbgStateStr());
405 mDbgState = DBG_STATE_GLES_DONE;
406
407 VDS_LOGV("queueBuffer pslot=%d", pslot);
408
409 status_t result;
410 if (mCompositionType == COMPOSITION_MIXED) {
411 // Queue the buffer back into the scratch pool
412 QueueBufferOutput scratchQBO;
413 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700414 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700415 if (result != NO_ERROR)
416 return result;
417
418 // Now acquire the buffer from the scratch pool -- should be the same
419 // slot and fence as we just queued.
420 Mutex::Autolock lock(mMutex);
421 BufferQueue::BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700422 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700423 if (result != NO_ERROR)
424 return result;
425 VDS_LOGW_IF(item.mBuf != sslot,
426 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
427 item.mBuf, sslot);
428 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
429 mFbFence = mSlots[item.mBuf].mFence;
430
431 } else {
432 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
433 "Unexpected queueBuffer in state %s for compositionType %s",
434 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
435
436 // Extract the GLES release fence for HWC to acquire
437 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700438 bool isAutoTimestamp;
Jesse Hall38efe862013-04-06 23:12:29 -0700439 Rect crop;
440 int scalingMode;
441 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700442 bool async;
Andy McFadden3c256212013-08-16 14:55:39 -0700443 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
444 &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700445
446 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700447 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700448 }
449
450 *output = mQueueBufferOutput;
451 return NO_ERROR;
452}
453
454void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
455 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
456 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
457 dbgStateStr());
458 VDS_LOGV("cancelBuffer pslot=%d", pslot);
459 Source source = fbSourceForCompositionType(mCompositionType);
460 return mSource[source]->cancelBuffer(
461 mapProducer2SourceSlot(source, pslot), fence);
462}
463
464int VirtualDisplaySurface::query(int what, int* value) {
465 return mSource[SOURCE_SINK]->query(what, value);
466}
467
Dan Stozaf0eaf252014-03-21 13:05:51 -0700468status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700469 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700470 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700471 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700472 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
473 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700474 if (result == NO_ERROR) {
475 updateQueueBufferOutput(qbo);
476 *output = mQueueBufferOutput;
477 }
478 return result;
479}
480
481status_t VirtualDisplaySurface::disconnect(int api) {
482 return mSource[SOURCE_SINK]->disconnect(api);
483}
484
Jesse Hall399184a2014-03-03 15:42:54 -0800485status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
486 return INVALID_OPERATION;
487}
488
Jesse Hall38efe862013-04-06 23:12:29 -0700489void VirtualDisplaySurface::updateQueueBufferOutput(
490 const QueueBufferOutput& qbo) {
491 uint32_t w, h, transformHint, numPendingBuffers;
492 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
493 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
494}
495
496void VirtualDisplaySurface::resetPerFrameState() {
497 mCompositionType = COMPOSITION_UNKNOWN;
498 mSinkBufferWidth = 0;
499 mSinkBufferHeight = 0;
mayank parsharb988f852014-01-10 10:27:45 +0530500 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700501 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700502 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530503 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700504}
505
Jesse Hall028dc8f2013-08-20 16:35:32 -0700506status_t VirtualDisplaySurface::refreshOutputBuffer() {
507 if (mOutputProducerSlot >= 0) {
508 mSource[SOURCE_SINK]->cancelBuffer(
509 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
510 mOutputFence);
511 }
512
513 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700514 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
515 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700516 if (result < 0)
517 return result;
518 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
519
Jesse Hallb716e572013-10-01 17:25:20 -0700520 // On GLES-only frames, we don't have the right output buffer acquire fence
521 // until after GLES calls queueBuffer(). So here we just set the buffer
522 // (for use in HWC prepare) but not the fence; we'll call this again with
523 // the proper fence once we have it.
524 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700525 mProducerBuffers[mOutputProducerSlot]);
526
527 return result;
528}
529
Jesse Hall38efe862013-04-06 23:12:29 -0700530// This slot mapping function is its own inverse, so two copies are unnecessary.
531// Both are kept to make the intent clear where the function is called, and for
532// the (unlikely) chance that we switch to a different mapping function.
533int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
534 if (source == SOURCE_SCRATCH) {
535 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
536 } else {
537 return sslot;
538 }
539}
540int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
541 return mapSource2ProducerSlot(source, pslot);
542}
543
544VirtualDisplaySurface::Source
545VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
546 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
547}
548
549const char* VirtualDisplaySurface::dbgStateStr() const {
550 switch (mDbgState) {
551 case DBG_STATE_IDLE: return "IDLE";
552 case DBG_STATE_PREPARED: return "PREPARED";
553 case DBG_STATE_GLES: return "GLES";
554 case DBG_STATE_GLES_DONE: return "GLES_DONE";
555 case DBG_STATE_HWC: return "HWC";
556 default: return "INVALID";
557 }
558}
559
560const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
561 switch (s) {
562 case SOURCE_SINK: return "SINK";
563 case SOURCE_SCRATCH: return "SCRATCH";
564 default: return "INVALID";
565 }
566}
567
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700568// ---------------------------------------------------------------------------
569} // namespace android
570// ---------------------------------------------------------------------------