blob: 91be4398a409055d6494a5a66b556f9db7536082 [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,
50 const sp<BufferQueue>& bq,
51 const String8& name)
52: ConsumerBase(bq),
Jesse Hall38efe862013-04-06 23:12:29 -070053 mHwc(hwc),
54 mDisplayId(dispId),
55 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070056 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070057 mProducerSlotSource(0),
58 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080059 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
60 mMustRecompose(false)
Jesse Hallffe1f192013-03-22 15:13:48 -070061{
Jesse Hall38efe862013-04-06 23:12:29 -070062 mSource[SOURCE_SINK] = sink;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070063 mSource[SOURCE_SCRATCH] = bq;
Jesse Hall38efe862013-04-06 23:12:29 -070064
65 resetPerFrameState();
66
67 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080068 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
69 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
70
71 // Pick the buffer format to request from the sink when not rendering to it
72 // with GLES. If the consumer needs CPU access, use the default format
73 // set by the consumer. Otherwise allow gralloc to decide the format based
74 // on usage bits.
75 int sinkUsage;
76 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
77 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
78 int sinkFormat;
79 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
80 mDefaultOutputFormat = sinkFormat;
81 } else {
82 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
83 }
84 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -070085
86 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070087 mConsumer->setConsumerName(ConsumerBase::mName);
88 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
89 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
90 mConsumer->setDefaultMaxBufferCount(2);
Jesse Hallffe1f192013-03-22 15:13:48 -070091}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070092
93VirtualDisplaySurface::~VirtualDisplaySurface() {
94}
95
Dan Stoza71433162014-02-04 16:22:36 -080096status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -070097 if (mDisplayId < 0)
98 return NO_ERROR;
99
Dan Stoza71433162014-02-04 16:22:36 -0800100 mMustRecompose = mustRecompose;
101
Jesse Hall38efe862013-04-06 23:12:29 -0700102 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700103 "Unexpected beginFrame() in %s state", dbgStateStr());
104 mDbgState = DBG_STATE_BEGUN;
105
106 uint32_t transformHint, numPendingBuffers;
107 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
108 &transformHint, &numPendingBuffers);
109
110 return refreshOutputBuffer();
111}
112
113status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
114 if (mDisplayId < 0)
115 return NO_ERROR;
116
117 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700118 "Unexpected prepareFrame() in %s state", dbgStateStr());
119 mDbgState = DBG_STATE_PREPARED;
120
121 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400122 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
123 // Some hardware can do RGB->YUV conversion more efficiently in hardware
124 // controlled by HWC than in hardware controlled by the video encoder.
125 // Forcing GLES-composed frames to go through an extra copy by the HWC
126 // allows the format conversion to happen there, rather than passing RGB
127 // directly to the consumer.
128 //
129 // On the other hand, when the consumer prefers RGB or can consume RGB
130 // inexpensively, this forces an unnecessary copy.
131 mCompositionType = COMPOSITION_MIXED;
132 }
133
Jesse Hall38efe862013-04-06 23:12:29 -0700134 if (mCompositionType != mDbgLastCompositionType) {
135 VDS_LOGV("prepareFrame: composition type changed to %s",
136 dbgCompositionTypeStr(mCompositionType));
137 mDbgLastCompositionType = mCompositionType;
138 }
139
Jesse Hall1e27ba22013-09-27 09:05:09 -0700140 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800141 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700142 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
143 // We must have just switched from GLES-only to MIXED or HWC
144 // composition. Stop using the format and usage requested by the GLES
145 // driver; they may be suboptimal when HWC is writing to the output
146 // buffer. For example, if the output is going to a video encoder, and
147 // HWC can write directly to YUV, some hardware can skip a
148 // memory-to-memory RGB-to-YUV conversion step.
149 //
150 // If we just switched *to* GLES-only mode, we'll change the
151 // format/usage and get a new buffer when the GLES driver calls
152 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800153 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700154 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
155 refreshOutputBuffer();
156 }
157
Jesse Hall38efe862013-04-06 23:12:29 -0700158 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700159}
160
161status_t VirtualDisplaySurface::compositionComplete() {
162 return NO_ERROR;
163}
164
165status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700166 if (mDisplayId < 0)
167 return NO_ERROR;
168
169 if (mCompositionType == COMPOSITION_HWC) {
170 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
171 "Unexpected advanceFrame() in %s state on HWC frame",
172 dbgStateStr());
173 } else {
174 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
175 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
176 dbgStateStr());
177 }
178 mDbgState = DBG_STATE_HWC;
179
Jesse Hall14e8b012013-11-07 12:28:26 -0800180 if (mOutputProducerSlot < 0 ||
181 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700182 // Last chance bailout if something bad happened earlier. For example,
183 // in a GLES configuration, if the sink disappears then dequeueBuffer
184 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
185 // will soldier on. So we end up here without a buffer. There should
186 // be lots of scary messages in the log just before this.
187 VDS_LOGE("advanceFrame: no buffer, bailing out");
188 return NO_MEMORY;
189 }
190
Jesse Hall14e8b012013-11-07 12:28:26 -0800191 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
192 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700193 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
194 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
195 mFbProducerSlot, fbBuffer.get(),
196 mOutputProducerSlot, outBuffer.get());
197
Jesse Hallb716e572013-10-01 17:25:20 -0700198 // At this point we know the output buffer acquire fence,
199 // so update HWC state with it.
200 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
201
Jesse Hall14e8b012013-11-07 12:28:26 -0800202 status_t result = NO_ERROR;
203 if (fbBuffer != NULL) {
204 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
205 }
206
207 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700208}
209
Jesse Hall851cfe82013-03-20 13:44:00 -0700210void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700211 if (mDisplayId < 0)
212 return;
213
214 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
215 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
216 mDbgState = DBG_STATE_IDLE;
217
218 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
219 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
220 // release the scratch buffer back to the pool
221 Mutex::Autolock lock(mMutex);
222 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
223 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
224 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
225 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
226 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
227 }
228
229 if (mOutputProducerSlot >= 0) {
230 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
231 QueueBufferOutput qbo;
232 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
233 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800234 if (mMustRecompose) {
235 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
236 QueueBufferInput(
237 systemTime(), false /* isAutoTimestamp */,
238 Rect(mSinkBufferWidth, mSinkBufferHeight),
239 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
240 true /* async*/,
241 outFence),
242 &qbo);
243 if (result == NO_ERROR) {
244 updateQueueBufferOutput(qbo);
245 }
246 } else {
247 // If the surface hadn't actually been updated, then we only went
248 // through the motions of updating the display to keep our state
249 // machine happy. We cancel the buffer to avoid triggering another
250 // re-composition and causing an infinite loop.
251 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700252 }
253 }
254
255 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700256}
257
258void VirtualDisplaySurface::dump(String8& result) const {
259}
260
Jesse Hall38efe862013-04-06 23:12:29 -0700261status_t VirtualDisplaySurface::requestBuffer(int pslot,
262 sp<GraphicBuffer>* outBuf) {
263 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
264 "Unexpected requestBuffer pslot=%d in %s state",
265 pslot, dbgStateStr());
266
267 *outBuf = mProducerBuffers[pslot];
268 return NO_ERROR;
269}
270
271status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
272 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
273}
274
275status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700276 uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Jesse Hall8db92552013-08-29 16:03:50 -0700277 // Don't let a slow consumer block us
278 bool async = (source == SOURCE_SINK);
279
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700280 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700281 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700282 if (result < 0)
283 return result;
284 int pslot = mapSource2ProducerSlot(source, *sslot);
285 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
286 dbgSourceStr(source), *sslot, pslot, result);
287 uint32_t sourceBit = static_cast<uint32_t>(source) << pslot;
288
289 if ((mProducerSlotSource & (1u << pslot)) != sourceBit) {
290 // This slot was previously dequeued from the other source; must
291 // re-request the buffer.
292 result |= BUFFER_NEEDS_REALLOCATION;
293 mProducerSlotSource &= ~(1u << pslot);
294 mProducerSlotSource |= sourceBit;
295 }
296
297 if (result & RELEASE_ALL_BUFFERS) {
298 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
299 if ((mProducerSlotSource & (1u << i)) == sourceBit)
300 mProducerBuffers[i].clear();
301 }
302 }
303 if (result & BUFFER_NEEDS_REALLOCATION) {
304 mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
Jesse Hall497ba0e2013-11-04 16:43:03 -0800305 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
306 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
307 mProducerBuffers[pslot]->getPixelFormat(),
308 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700309 }
310
311 return result;
312}
313
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700314status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Jesse Hall38efe862013-04-06 23:12:29 -0700315 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
316 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
317 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
318 mDbgState = DBG_STATE_GLES;
319
Jesse Hall8db92552013-08-29 16:03:50 -0700320 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
Jesse Hall38efe862013-04-06 23:12:29 -0700321 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
322
Jesse Hall028dc8f2013-08-20 16:35:32 -0700323 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700324 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700325
Jesse Hall38efe862013-04-06 23:12:29 -0700326 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700327
328 if (mOutputProducerSlot < 0) {
329 // Last chance bailout if something bad happened earlier. For example,
330 // in a GLES configuration, if the sink disappears then dequeueBuffer
331 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
332 // will soldier on. So we end up here without a buffer. There should
333 // be lots of scary messages in the log just before this.
334 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
335 return NO_MEMORY;
336 }
337
Jesse Hall028dc8f2013-08-20 16:35:32 -0700338 // We already dequeued the output buffer. If the GLES driver wants
339 // something incompatible, we have to cancel and get a new one. This
340 // will mean that HWC will see a different output buffer between
341 // prepare and set, but since we're in GLES-only mode already it
342 // shouldn't matter.
343
Jesse Hall1e27ba22013-09-27 09:05:09 -0700344 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700345 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700346 if ((usage & ~buf->getUsage()) != 0 ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700347 (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
348 (w != 0 && w != mSinkBufferWidth) ||
349 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700350 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
351 "want %dx%d fmt=%d use=%#x, "
352 "have %dx%d fmt=%d use=%#x",
353 w, h, format, usage,
354 mSinkBufferWidth, mSinkBufferHeight,
355 buf->getPixelFormat(), buf->getUsage());
356 mOutputFormat = format;
357 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700358 result = refreshOutputBuffer();
359 if (result < 0)
360 return result;
361 }
Jesse Hall38efe862013-04-06 23:12:29 -0700362 }
363
Jesse Hall028dc8f2013-08-20 16:35:32 -0700364 if (source == SOURCE_SINK) {
365 *pslot = mOutputProducerSlot;
366 *fence = mOutputFence;
367 } else {
368 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700369 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700370 if (result >= 0) {
371 *pslot = mapSource2ProducerSlot(source, sslot);
372 }
Jesse Hall38efe862013-04-06 23:12:29 -0700373 }
374 return result;
375}
376
Dan Stoza88a459a2014-03-12 09:34:36 -0700377status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
378 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
379 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800380}
381
Dan Stoza88a459a2014-03-12 09:34:36 -0700382status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
383 const sp<GraphicBuffer>& /* buffer */) {
384 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
385 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800386}
387
Jesse Hall38efe862013-04-06 23:12:29 -0700388status_t VirtualDisplaySurface::queueBuffer(int pslot,
389 const QueueBufferInput& input, QueueBufferOutput* output) {
390 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
391 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
392 dbgStateStr());
393 mDbgState = DBG_STATE_GLES_DONE;
394
395 VDS_LOGV("queueBuffer pslot=%d", pslot);
396
397 status_t result;
398 if (mCompositionType == COMPOSITION_MIXED) {
399 // Queue the buffer back into the scratch pool
400 QueueBufferOutput scratchQBO;
401 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700402 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700403 if (result != NO_ERROR)
404 return result;
405
406 // Now acquire the buffer from the scratch pool -- should be the same
407 // slot and fence as we just queued.
408 Mutex::Autolock lock(mMutex);
409 BufferQueue::BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700410 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700411 if (result != NO_ERROR)
412 return result;
413 VDS_LOGW_IF(item.mBuf != sslot,
414 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
415 item.mBuf, sslot);
416 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
417 mFbFence = mSlots[item.mBuf].mFence;
418
419 } else {
420 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
421 "Unexpected queueBuffer in state %s for compositionType %s",
422 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
423
424 // Extract the GLES release fence for HWC to acquire
425 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700426 bool isAutoTimestamp;
Jesse Hall38efe862013-04-06 23:12:29 -0700427 Rect crop;
428 int scalingMode;
429 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700430 bool async;
Andy McFadden3c256212013-08-16 14:55:39 -0700431 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
432 &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700433
434 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700435 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700436 }
437
438 *output = mQueueBufferOutput;
439 return NO_ERROR;
440}
441
442void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
443 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
444 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
445 dbgStateStr());
446 VDS_LOGV("cancelBuffer pslot=%d", pslot);
447 Source source = fbSourceForCompositionType(mCompositionType);
448 return mSource[source]->cancelBuffer(
449 mapProducer2SourceSlot(source, pslot), fence);
450}
451
452int VirtualDisplaySurface::query(int what, int* value) {
453 return mSource[SOURCE_SINK]->query(what, value);
454}
455
Mathias Agopian365857d2013-09-11 19:35:45 -0700456status_t VirtualDisplaySurface::connect(const sp<IBinder>& token,
457 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700458 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700459 QueueBufferOutput qbo;
Mathias Agopian365857d2013-09-11 19:35:45 -0700460 status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700461 if (result == NO_ERROR) {
462 updateQueueBufferOutput(qbo);
463 *output = mQueueBufferOutput;
464 }
465 return result;
466}
467
468status_t VirtualDisplaySurface::disconnect(int api) {
469 return mSource[SOURCE_SINK]->disconnect(api);
470}
471
472void VirtualDisplaySurface::updateQueueBufferOutput(
473 const QueueBufferOutput& qbo) {
474 uint32_t w, h, transformHint, numPendingBuffers;
475 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
476 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
477}
478
479void VirtualDisplaySurface::resetPerFrameState() {
480 mCompositionType = COMPOSITION_UNKNOWN;
481 mSinkBufferWidth = 0;
482 mSinkBufferHeight = 0;
mayank parsharb988f852014-01-10 10:27:45 +0530483 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700484 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700485 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530486 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700487}
488
Jesse Hall028dc8f2013-08-20 16:35:32 -0700489status_t VirtualDisplaySurface::refreshOutputBuffer() {
490 if (mOutputProducerSlot >= 0) {
491 mSource[SOURCE_SINK]->cancelBuffer(
492 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
493 mOutputFence);
494 }
495
496 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700497 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
498 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700499 if (result < 0)
500 return result;
501 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
502
Jesse Hallb716e572013-10-01 17:25:20 -0700503 // On GLES-only frames, we don't have the right output buffer acquire fence
504 // until after GLES calls queueBuffer(). So here we just set the buffer
505 // (for use in HWC prepare) but not the fence; we'll call this again with
506 // the proper fence once we have it.
507 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700508 mProducerBuffers[mOutputProducerSlot]);
509
510 return result;
511}
512
Jesse Hall38efe862013-04-06 23:12:29 -0700513// This slot mapping function is its own inverse, so two copies are unnecessary.
514// Both are kept to make the intent clear where the function is called, and for
515// the (unlikely) chance that we switch to a different mapping function.
516int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
517 if (source == SOURCE_SCRATCH) {
518 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
519 } else {
520 return sslot;
521 }
522}
523int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
524 return mapSource2ProducerSlot(source, pslot);
525}
526
527VirtualDisplaySurface::Source
528VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
529 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
530}
531
532const char* VirtualDisplaySurface::dbgStateStr() const {
533 switch (mDbgState) {
534 case DBG_STATE_IDLE: return "IDLE";
535 case DBG_STATE_PREPARED: return "PREPARED";
536 case DBG_STATE_GLES: return "GLES";
537 case DBG_STATE_GLES_DONE: return "GLES_DONE";
538 case DBG_STATE_HWC: return "HWC";
539 default: return "INVALID";
540 }
541}
542
543const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
544 switch (s) {
545 case SOURCE_SINK: return "SINK";
546 case SOURCE_SCRATCH: return "SCRATCH";
547 default: return "INVALID";
548 }
549}
550
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700551// ---------------------------------------------------------------------------
552} // namespace android
553// ---------------------------------------------------------------------------