blob: 29e9d4097b8e27089797bc100f60d6c89e19bd6a [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 Hall38efe862013-04-06 23:12:29 -070025#define VDS_LOGE(msg, ...) ALOGE("[%s] "msg, \
26 mDisplayName.string(), ##__VA_ARGS__)
27#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] "msg, \
28 mDisplayName.string(), ##__VA_ARGS__)
29#define VDS_LOGV(msg, ...) ALOGV("[%s] "msg, \
30 mDisplayName.string(), ##__VA_ARGS__)
31
32static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
33 switch (type) {
34 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
35 case DisplaySurface::COMPOSITION_GLES: return "GLES";
36 case DisplaySurface::COMPOSITION_HWC: return "HWC";
37 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
38 default: return "<INVALID>";
39 }
40}
41
Jesse Hallffe1f192013-03-22 15:13:48 -070042VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070043 const sp<IGraphicBufferProducer>& sink,
44 const sp<BufferQueue>& bq,
45 const String8& name)
46: ConsumerBase(bq),
Jesse Hall38efe862013-04-06 23:12:29 -070047 mHwc(hwc),
48 mDisplayId(dispId),
49 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070050 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
51 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070052 mProducerSlotSource(0),
53 mDbgState(DBG_STATE_IDLE),
54 mDbgLastCompositionType(COMPOSITION_UNKNOWN)
Jesse Hallffe1f192013-03-22 15:13:48 -070055{
Jesse Hall38efe862013-04-06 23:12:29 -070056 mSource[SOURCE_SINK] = sink;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070057 mSource[SOURCE_SCRATCH] = bq;
Jesse Hall38efe862013-04-06 23:12:29 -070058
59 resetPerFrameState();
60
61 int sinkWidth, sinkHeight;
62 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
63 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
64
65 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070066 mConsumer->setConsumerName(ConsumerBase::mName);
67 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
68 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
69 mConsumer->setDefaultMaxBufferCount(2);
Jesse Hallffe1f192013-03-22 15:13:48 -070070}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071
72VirtualDisplaySurface::~VirtualDisplaySurface() {
73}
74
Jesse Hall028dc8f2013-08-20 16:35:32 -070075status_t VirtualDisplaySurface::beginFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -070076 if (mDisplayId < 0)
77 return NO_ERROR;
78
79 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -070080 "Unexpected beginFrame() in %s state", dbgStateStr());
81 mDbgState = DBG_STATE_BEGUN;
82
83 uint32_t transformHint, numPendingBuffers;
84 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
85 &transformHint, &numPendingBuffers);
86
87 return refreshOutputBuffer();
88}
89
90status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
91 if (mDisplayId < 0)
92 return NO_ERROR;
93
94 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -070095 "Unexpected prepareFrame() in %s state", dbgStateStr());
96 mDbgState = DBG_STATE_PREPARED;
97
98 mCompositionType = compositionType;
Jesse Hall38efe862013-04-06 23:12:29 -070099 if (mCompositionType != mDbgLastCompositionType) {
100 VDS_LOGV("prepareFrame: composition type changed to %s",
101 dbgCompositionTypeStr(mCompositionType));
102 mDbgLastCompositionType = mCompositionType;
103 }
104
Jesse Hall1e27ba22013-09-27 09:05:09 -0700105 if (mCompositionType != COMPOSITION_GLES &&
106 (mOutputFormat != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
107 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
108 // We must have just switched from GLES-only to MIXED or HWC
109 // composition. Stop using the format and usage requested by the GLES
110 // driver; they may be suboptimal when HWC is writing to the output
111 // buffer. For example, if the output is going to a video encoder, and
112 // HWC can write directly to YUV, some hardware can skip a
113 // memory-to-memory RGB-to-YUV conversion step.
114 //
115 // If we just switched *to* GLES-only mode, we'll change the
116 // format/usage and get a new buffer when the GLES driver calls
117 // dequeueBuffer().
118 mOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
119 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
120 refreshOutputBuffer();
121 }
122
Jesse Hall38efe862013-04-06 23:12:29 -0700123 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700124}
125
126status_t VirtualDisplaySurface::compositionComplete() {
127 return NO_ERROR;
128}
129
130status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700131 if (mDisplayId < 0)
132 return NO_ERROR;
133
134 if (mCompositionType == COMPOSITION_HWC) {
135 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
136 "Unexpected advanceFrame() in %s state on HWC frame",
137 dbgStateStr());
138 } else {
139 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
140 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
141 dbgStateStr());
142 }
143 mDbgState = DBG_STATE_HWC;
144
Jesse Hall38efe862013-04-06 23:12:29 -0700145 if (mCompositionType == COMPOSITION_HWC) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700146 // Use the output buffer for the FB as well, though conceptually the
147 // FB is unused on this frame.
Jesse Hall38efe862013-04-06 23:12:29 -0700148 mFbProducerSlot = mOutputProducerSlot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700149 mFbFence = mOutputFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700150 }
151
152 if (mFbProducerSlot < 0 || mOutputProducerSlot < 0) {
153 // Last chance bailout if something bad happened earlier. For example,
154 // in a GLES configuration, if the sink disappears then dequeueBuffer
155 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
156 // will soldier on. So we end up here without a buffer. There should
157 // be lots of scary messages in the log just before this.
158 VDS_LOGE("advanceFrame: no buffer, bailing out");
159 return NO_MEMORY;
160 }
161
162 sp<GraphicBuffer> fbBuffer = mProducerBuffers[mFbProducerSlot];
163 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
164 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
165 mFbProducerSlot, fbBuffer.get(),
166 mOutputProducerSlot, outBuffer.get());
167
Jesse Hallb716e572013-10-01 17:25:20 -0700168 // At this point we know the output buffer acquire fence,
169 // so update HWC state with it.
170 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
171
Jesse Hall028dc8f2013-08-20 16:35:32 -0700172 return mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700173}
174
Jesse Hall851cfe82013-03-20 13:44:00 -0700175void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700176 if (mDisplayId < 0)
177 return;
178
179 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
180 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
181 mDbgState = DBG_STATE_IDLE;
182
183 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
184 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
185 // release the scratch buffer back to the pool
186 Mutex::Autolock lock(mMutex);
187 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
188 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
189 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
190 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
191 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
192 }
193
194 if (mOutputProducerSlot >= 0) {
195 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
196 QueueBufferOutput qbo;
197 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
198 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
199 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
Jesse Hall8db92552013-08-29 16:03:50 -0700200 QueueBufferInput(
201 systemTime(), false /* isAutoTimestamp */,
Jesse Hall38efe862013-04-06 23:12:29 -0700202 Rect(mSinkBufferWidth, mSinkBufferHeight),
Jesse Hall8db92552013-08-29 16:03:50 -0700203 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
204 true /* async*/,
205 outFence),
Jesse Hall38efe862013-04-06 23:12:29 -0700206 &qbo);
207 if (result == NO_ERROR) {
208 updateQueueBufferOutput(qbo);
209 }
210 }
211
212 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700213}
214
215void VirtualDisplaySurface::dump(String8& result) const {
216}
217
Jesse Hall38efe862013-04-06 23:12:29 -0700218status_t VirtualDisplaySurface::requestBuffer(int pslot,
219 sp<GraphicBuffer>* outBuf) {
220 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
221 "Unexpected requestBuffer pslot=%d in %s state",
222 pslot, dbgStateStr());
223
224 *outBuf = mProducerBuffers[pslot];
225 return NO_ERROR;
226}
227
228status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
229 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
230}
231
232status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700233 uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Jesse Hall8db92552013-08-29 16:03:50 -0700234 // Don't let a slow consumer block us
235 bool async = (source == SOURCE_SINK);
236
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700237 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700238 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700239 if (result < 0)
240 return result;
241 int pslot = mapSource2ProducerSlot(source, *sslot);
242 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
243 dbgSourceStr(source), *sslot, pslot, result);
244 uint32_t sourceBit = static_cast<uint32_t>(source) << pslot;
245
246 if ((mProducerSlotSource & (1u << pslot)) != sourceBit) {
247 // This slot was previously dequeued from the other source; must
248 // re-request the buffer.
249 result |= BUFFER_NEEDS_REALLOCATION;
250 mProducerSlotSource &= ~(1u << pslot);
251 mProducerSlotSource |= sourceBit;
252 }
253
254 if (result & RELEASE_ALL_BUFFERS) {
255 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
256 if ((mProducerSlotSource & (1u << i)) == sourceBit)
257 mProducerBuffers[i].clear();
258 }
259 }
260 if (result & BUFFER_NEEDS_REALLOCATION) {
261 mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
262 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p",
263 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get());
264 }
265
266 return result;
267}
268
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700269status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Jesse Hall38efe862013-04-06 23:12:29 -0700270 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
271 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
272 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
273 mDbgState = DBG_STATE_GLES;
274
Jesse Hall8db92552013-08-29 16:03:50 -0700275 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
Jesse Hall38efe862013-04-06 23:12:29 -0700276 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
277
Jesse Hall028dc8f2013-08-20 16:35:32 -0700278 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700279 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700280
Jesse Hall38efe862013-04-06 23:12:29 -0700281 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700282
283 if (mOutputProducerSlot < 0) {
284 // Last chance bailout if something bad happened earlier. For example,
285 // in a GLES configuration, if the sink disappears then dequeueBuffer
286 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
287 // will soldier on. So we end up here without a buffer. There should
288 // be lots of scary messages in the log just before this.
289 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
290 return NO_MEMORY;
291 }
292
Jesse Hall028dc8f2013-08-20 16:35:32 -0700293 // We already dequeued the output buffer. If the GLES driver wants
294 // something incompatible, we have to cancel and get a new one. This
295 // will mean that HWC will see a different output buffer between
296 // prepare and set, but since we're in GLES-only mode already it
297 // shouldn't matter.
298
Jesse Hall1e27ba22013-09-27 09:05:09 -0700299 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700300 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700301 if ((usage & ~buf->getUsage()) != 0 ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700302 (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
303 (w != 0 && w != mSinkBufferWidth) ||
304 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700305 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
306 "want %dx%d fmt=%d use=%#x, "
307 "have %dx%d fmt=%d use=%#x",
308 w, h, format, usage,
309 mSinkBufferWidth, mSinkBufferHeight,
310 buf->getPixelFormat(), buf->getUsage());
311 mOutputFormat = format;
312 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700313 result = refreshOutputBuffer();
314 if (result < 0)
315 return result;
316 }
Jesse Hall38efe862013-04-06 23:12:29 -0700317 }
318
Jesse Hall028dc8f2013-08-20 16:35:32 -0700319 if (source == SOURCE_SINK) {
320 *pslot = mOutputProducerSlot;
321 *fence = mOutputFence;
322 } else {
323 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700324 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700325 if (result >= 0) {
326 *pslot = mapSource2ProducerSlot(source, sslot);
327 }
Jesse Hall38efe862013-04-06 23:12:29 -0700328 }
329 return result;
330}
331
332status_t VirtualDisplaySurface::queueBuffer(int pslot,
333 const QueueBufferInput& input, QueueBufferOutput* output) {
334 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
335 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
336 dbgStateStr());
337 mDbgState = DBG_STATE_GLES_DONE;
338
339 VDS_LOGV("queueBuffer pslot=%d", pslot);
340
341 status_t result;
342 if (mCompositionType == COMPOSITION_MIXED) {
343 // Queue the buffer back into the scratch pool
344 QueueBufferOutput scratchQBO;
345 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700346 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700347 if (result != NO_ERROR)
348 return result;
349
350 // Now acquire the buffer from the scratch pool -- should be the same
351 // slot and fence as we just queued.
352 Mutex::Autolock lock(mMutex);
353 BufferQueue::BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700354 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700355 if (result != NO_ERROR)
356 return result;
357 VDS_LOGW_IF(item.mBuf != sslot,
358 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
359 item.mBuf, sslot);
360 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
361 mFbFence = mSlots[item.mBuf].mFence;
362
363 } else {
364 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
365 "Unexpected queueBuffer in state %s for compositionType %s",
366 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
367
368 // Extract the GLES release fence for HWC to acquire
369 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700370 bool isAutoTimestamp;
Jesse Hall38efe862013-04-06 23:12:29 -0700371 Rect crop;
372 int scalingMode;
373 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700374 bool async;
Andy McFadden3c256212013-08-16 14:55:39 -0700375 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
376 &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700377
378 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700379 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700380 }
381
382 *output = mQueueBufferOutput;
383 return NO_ERROR;
384}
385
386void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
387 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
388 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
389 dbgStateStr());
390 VDS_LOGV("cancelBuffer pslot=%d", pslot);
391 Source source = fbSourceForCompositionType(mCompositionType);
392 return mSource[source]->cancelBuffer(
393 mapProducer2SourceSlot(source, pslot), fence);
394}
395
396int VirtualDisplaySurface::query(int what, int* value) {
397 return mSource[SOURCE_SINK]->query(what, value);
398}
399
Mathias Agopian365857d2013-09-11 19:35:45 -0700400status_t VirtualDisplaySurface::connect(const sp<IBinder>& token,
401 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700402 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700403 QueueBufferOutput qbo;
Mathias Agopian365857d2013-09-11 19:35:45 -0700404 status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700405 if (result == NO_ERROR) {
406 updateQueueBufferOutput(qbo);
407 *output = mQueueBufferOutput;
408 }
409 return result;
410}
411
412status_t VirtualDisplaySurface::disconnect(int api) {
413 return mSource[SOURCE_SINK]->disconnect(api);
414}
415
416void VirtualDisplaySurface::updateQueueBufferOutput(
417 const QueueBufferOutput& qbo) {
418 uint32_t w, h, transformHint, numPendingBuffers;
419 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
420 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
421}
422
423void VirtualDisplaySurface::resetPerFrameState() {
424 mCompositionType = COMPOSITION_UNKNOWN;
425 mSinkBufferWidth = 0;
426 mSinkBufferHeight = 0;
427 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700428 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700429 mFbProducerSlot = -1;
430 mOutputProducerSlot = -1;
431}
432
Jesse Hall028dc8f2013-08-20 16:35:32 -0700433status_t VirtualDisplaySurface::refreshOutputBuffer() {
434 if (mOutputProducerSlot >= 0) {
435 mSource[SOURCE_SINK]->cancelBuffer(
436 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
437 mOutputFence);
438 }
439
440 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700441 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
442 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700443 if (result < 0)
444 return result;
445 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
446
Jesse Hallb716e572013-10-01 17:25:20 -0700447 // On GLES-only frames, we don't have the right output buffer acquire fence
448 // until after GLES calls queueBuffer(). So here we just set the buffer
449 // (for use in HWC prepare) but not the fence; we'll call this again with
450 // the proper fence once we have it.
451 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700452 mProducerBuffers[mOutputProducerSlot]);
453
454 return result;
455}
456
Jesse Hall38efe862013-04-06 23:12:29 -0700457// This slot mapping function is its own inverse, so two copies are unnecessary.
458// Both are kept to make the intent clear where the function is called, and for
459// the (unlikely) chance that we switch to a different mapping function.
460int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
461 if (source == SOURCE_SCRATCH) {
462 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
463 } else {
464 return sslot;
465 }
466}
467int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
468 return mapSource2ProducerSlot(source, pslot);
469}
470
471VirtualDisplaySurface::Source
472VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
473 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
474}
475
476const char* VirtualDisplaySurface::dbgStateStr() const {
477 switch (mDbgState) {
478 case DBG_STATE_IDLE: return "IDLE";
479 case DBG_STATE_PREPARED: return "PREPARED";
480 case DBG_STATE_GLES: return "GLES";
481 case DBG_STATE_GLES_DONE: return "GLES_DONE";
482 case DBG_STATE_HWC: return "HWC";
483 default: return "INVALID";
484 }
485}
486
487const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
488 switch (s) {
489 case SOURCE_SINK: return "SINK";
490 case SOURCE_SCRATCH: return "SCRATCH";
491 default: return "INVALID";
492 }
493}
494
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700495// ---------------------------------------------------------------------------
496} // namespace android
497// ---------------------------------------------------------------------------