blob: 0d6cbb4c953a2a476618b6d73513a14bd624e890 [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
Naseer Ahmed6a968462013-10-04 16:15:22 -040025static bool sForceHwcCopy = FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS;
26
Jesse Hall38efe862013-04-06 23:12:29 -070027#define VDS_LOGE(msg, ...) ALOGE("[%s] "msg, \
28 mDisplayName.string(), ##__VA_ARGS__)
29#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] "msg, \
30 mDisplayName.string(), ##__VA_ARGS__)
31#define VDS_LOGV(msg, ...) ALOGV("[%s] "msg, \
32 mDisplayName.string(), ##__VA_ARGS__)
33
34static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
35 switch (type) {
36 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
37 case DisplaySurface::COMPOSITION_GLES: return "GLES";
38 case DisplaySurface::COMPOSITION_HWC: return "HWC";
39 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
40 default: return "<INVALID>";
41 }
42}
43
Jesse Hallffe1f192013-03-22 15:13:48 -070044VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070045 const sp<IGraphicBufferProducer>& sink,
46 const sp<BufferQueue>& bq,
47 const String8& name)
48: ConsumerBase(bq),
Jesse Hall38efe862013-04-06 23:12:29 -070049 mHwc(hwc),
50 mDisplayId(dispId),
51 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070052 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
53 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070054 mProducerSlotSource(0),
55 mDbgState(DBG_STATE_IDLE),
56 mDbgLastCompositionType(COMPOSITION_UNKNOWN)
Jesse Hallffe1f192013-03-22 15:13:48 -070057{
Jesse Hall38efe862013-04-06 23:12:29 -070058 mSource[SOURCE_SINK] = sink;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070059 mSource[SOURCE_SCRATCH] = bq;
Jesse Hall38efe862013-04-06 23:12:29 -070060
61 resetPerFrameState();
62
63 int sinkWidth, sinkHeight;
64 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
65 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
66
67 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070068 mConsumer->setConsumerName(ConsumerBase::mName);
69 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
70 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
71 mConsumer->setDefaultMaxBufferCount(2);
Jesse Hallffe1f192013-03-22 15:13:48 -070072}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073
74VirtualDisplaySurface::~VirtualDisplaySurface() {
75}
76
Jesse Hall028dc8f2013-08-20 16:35:32 -070077status_t VirtualDisplaySurface::beginFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -070078 if (mDisplayId < 0)
79 return NO_ERROR;
80
81 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -070082 "Unexpected beginFrame() in %s state", dbgStateStr());
83 mDbgState = DBG_STATE_BEGUN;
84
85 uint32_t transformHint, numPendingBuffers;
86 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
87 &transformHint, &numPendingBuffers);
88
89 return refreshOutputBuffer();
90}
91
92status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
93 if (mDisplayId < 0)
94 return NO_ERROR;
95
96 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -070097 "Unexpected prepareFrame() in %s state", dbgStateStr());
98 mDbgState = DBG_STATE_PREPARED;
99
100 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400101 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
102 // Some hardware can do RGB->YUV conversion more efficiently in hardware
103 // controlled by HWC than in hardware controlled by the video encoder.
104 // Forcing GLES-composed frames to go through an extra copy by the HWC
105 // allows the format conversion to happen there, rather than passing RGB
106 // directly to the consumer.
107 //
108 // On the other hand, when the consumer prefers RGB or can consume RGB
109 // inexpensively, this forces an unnecessary copy.
110 mCompositionType = COMPOSITION_MIXED;
111 }
112
Jesse Hall38efe862013-04-06 23:12:29 -0700113 if (mCompositionType != mDbgLastCompositionType) {
114 VDS_LOGV("prepareFrame: composition type changed to %s",
115 dbgCompositionTypeStr(mCompositionType));
116 mDbgLastCompositionType = mCompositionType;
117 }
118
Jesse Hall1e27ba22013-09-27 09:05:09 -0700119 if (mCompositionType != COMPOSITION_GLES &&
120 (mOutputFormat != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
121 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
122 // We must have just switched from GLES-only to MIXED or HWC
123 // composition. Stop using the format and usage requested by the GLES
124 // driver; they may be suboptimal when HWC is writing to the output
125 // buffer. For example, if the output is going to a video encoder, and
126 // HWC can write directly to YUV, some hardware can skip a
127 // memory-to-memory RGB-to-YUV conversion step.
128 //
129 // If we just switched *to* GLES-only mode, we'll change the
130 // format/usage and get a new buffer when the GLES driver calls
131 // dequeueBuffer().
132 mOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
133 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
134 refreshOutputBuffer();
135 }
136
Jesse Hall38efe862013-04-06 23:12:29 -0700137 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700138}
139
140status_t VirtualDisplaySurface::compositionComplete() {
141 return NO_ERROR;
142}
143
144status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700145 if (mDisplayId < 0)
146 return NO_ERROR;
147
148 if (mCompositionType == COMPOSITION_HWC) {
149 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
150 "Unexpected advanceFrame() in %s state on HWC frame",
151 dbgStateStr());
152 } else {
153 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
154 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
155 dbgStateStr());
156 }
157 mDbgState = DBG_STATE_HWC;
158
Jesse Hall38efe862013-04-06 23:12:29 -0700159 if (mCompositionType == COMPOSITION_HWC) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700160 // Use the output buffer for the FB as well, though conceptually the
161 // FB is unused on this frame.
Jesse Hall38efe862013-04-06 23:12:29 -0700162 mFbProducerSlot = mOutputProducerSlot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700163 mFbFence = mOutputFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700164 }
165
166 if (mFbProducerSlot < 0 || mOutputProducerSlot < 0) {
167 // Last chance bailout if something bad happened earlier. For example,
168 // in a GLES configuration, if the sink disappears then dequeueBuffer
169 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
170 // will soldier on. So we end up here without a buffer. There should
171 // be lots of scary messages in the log just before this.
172 VDS_LOGE("advanceFrame: no buffer, bailing out");
173 return NO_MEMORY;
174 }
175
176 sp<GraphicBuffer> fbBuffer = mProducerBuffers[mFbProducerSlot];
177 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
178 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
179 mFbProducerSlot, fbBuffer.get(),
180 mOutputProducerSlot, outBuffer.get());
181
Jesse Hallb716e572013-10-01 17:25:20 -0700182 // At this point we know the output buffer acquire fence,
183 // so update HWC state with it.
184 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
185
Jesse Hall028dc8f2013-08-20 16:35:32 -0700186 return mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700187}
188
Jesse Hall851cfe82013-03-20 13:44:00 -0700189void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700190 if (mDisplayId < 0)
191 return;
192
193 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
194 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
195 mDbgState = DBG_STATE_IDLE;
196
197 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
198 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
199 // release the scratch buffer back to the pool
200 Mutex::Autolock lock(mMutex);
201 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
202 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
203 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
204 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
205 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
206 }
207
208 if (mOutputProducerSlot >= 0) {
209 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
210 QueueBufferOutput qbo;
211 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
212 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
213 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
Jesse Hall8db92552013-08-29 16:03:50 -0700214 QueueBufferInput(
215 systemTime(), false /* isAutoTimestamp */,
Jesse Hall38efe862013-04-06 23:12:29 -0700216 Rect(mSinkBufferWidth, mSinkBufferHeight),
Jesse Hall8db92552013-08-29 16:03:50 -0700217 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
218 true /* async*/,
219 outFence),
Jesse Hall38efe862013-04-06 23:12:29 -0700220 &qbo);
221 if (result == NO_ERROR) {
222 updateQueueBufferOutput(qbo);
223 }
224 }
225
226 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700227}
228
229void VirtualDisplaySurface::dump(String8& result) const {
230}
231
Jesse Hall38efe862013-04-06 23:12:29 -0700232status_t VirtualDisplaySurface::requestBuffer(int pslot,
233 sp<GraphicBuffer>* outBuf) {
234 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
235 "Unexpected requestBuffer pslot=%d in %s state",
236 pslot, dbgStateStr());
237
238 *outBuf = mProducerBuffers[pslot];
239 return NO_ERROR;
240}
241
242status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
243 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
244}
245
246status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700247 uint32_t format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Jesse Hall8db92552013-08-29 16:03:50 -0700248 // Don't let a slow consumer block us
249 bool async = (source == SOURCE_SINK);
250
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700251 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700252 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700253 if (result < 0)
254 return result;
255 int pslot = mapSource2ProducerSlot(source, *sslot);
256 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
257 dbgSourceStr(source), *sslot, pslot, result);
258 uint32_t sourceBit = static_cast<uint32_t>(source) << pslot;
259
260 if ((mProducerSlotSource & (1u << pslot)) != sourceBit) {
261 // This slot was previously dequeued from the other source; must
262 // re-request the buffer.
263 result |= BUFFER_NEEDS_REALLOCATION;
264 mProducerSlotSource &= ~(1u << pslot);
265 mProducerSlotSource |= sourceBit;
266 }
267
268 if (result & RELEASE_ALL_BUFFERS) {
269 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
270 if ((mProducerSlotSource & (1u << i)) == sourceBit)
271 mProducerBuffers[i].clear();
272 }
273 }
274 if (result & BUFFER_NEEDS_REALLOCATION) {
275 mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
276 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p",
277 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get());
278 }
279
280 return result;
281}
282
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700283status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Jesse Hall38efe862013-04-06 23:12:29 -0700284 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
285 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
286 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
287 mDbgState = DBG_STATE_GLES;
288
Jesse Hall8db92552013-08-29 16:03:50 -0700289 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
Jesse Hall38efe862013-04-06 23:12:29 -0700290 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
291
Jesse Hall028dc8f2013-08-20 16:35:32 -0700292 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700293 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700294
Jesse Hall38efe862013-04-06 23:12:29 -0700295 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700296
297 if (mOutputProducerSlot < 0) {
298 // Last chance bailout if something bad happened earlier. For example,
299 // in a GLES configuration, if the sink disappears then dequeueBuffer
300 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
301 // will soldier on. So we end up here without a buffer. There should
302 // be lots of scary messages in the log just before this.
303 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
304 return NO_MEMORY;
305 }
306
Jesse Hall028dc8f2013-08-20 16:35:32 -0700307 // We already dequeued the output buffer. If the GLES driver wants
308 // something incompatible, we have to cancel and get a new one. This
309 // will mean that HWC will see a different output buffer between
310 // prepare and set, but since we're in GLES-only mode already it
311 // shouldn't matter.
312
Jesse Hall1e27ba22013-09-27 09:05:09 -0700313 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700314 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700315 if ((usage & ~buf->getUsage()) != 0 ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700316 (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
317 (w != 0 && w != mSinkBufferWidth) ||
318 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700319 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
320 "want %dx%d fmt=%d use=%#x, "
321 "have %dx%d fmt=%d use=%#x",
322 w, h, format, usage,
323 mSinkBufferWidth, mSinkBufferHeight,
324 buf->getPixelFormat(), buf->getUsage());
325 mOutputFormat = format;
326 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700327 result = refreshOutputBuffer();
328 if (result < 0)
329 return result;
330 }
Jesse Hall38efe862013-04-06 23:12:29 -0700331 }
332
Jesse Hall028dc8f2013-08-20 16:35:32 -0700333 if (source == SOURCE_SINK) {
334 *pslot = mOutputProducerSlot;
335 *fence = mOutputFence;
336 } else {
337 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700338 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700339 if (result >= 0) {
340 *pslot = mapSource2ProducerSlot(source, sslot);
341 }
Jesse Hall38efe862013-04-06 23:12:29 -0700342 }
343 return result;
344}
345
346status_t VirtualDisplaySurface::queueBuffer(int pslot,
347 const QueueBufferInput& input, QueueBufferOutput* output) {
348 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
349 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
350 dbgStateStr());
351 mDbgState = DBG_STATE_GLES_DONE;
352
353 VDS_LOGV("queueBuffer pslot=%d", pslot);
354
355 status_t result;
356 if (mCompositionType == COMPOSITION_MIXED) {
357 // Queue the buffer back into the scratch pool
358 QueueBufferOutput scratchQBO;
359 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700360 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700361 if (result != NO_ERROR)
362 return result;
363
364 // Now acquire the buffer from the scratch pool -- should be the same
365 // slot and fence as we just queued.
366 Mutex::Autolock lock(mMutex);
367 BufferQueue::BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700368 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700369 if (result != NO_ERROR)
370 return result;
371 VDS_LOGW_IF(item.mBuf != sslot,
372 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
373 item.mBuf, sslot);
374 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
375 mFbFence = mSlots[item.mBuf].mFence;
376
377 } else {
378 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
379 "Unexpected queueBuffer in state %s for compositionType %s",
380 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
381
382 // Extract the GLES release fence for HWC to acquire
383 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700384 bool isAutoTimestamp;
Jesse Hall38efe862013-04-06 23:12:29 -0700385 Rect crop;
386 int scalingMode;
387 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700388 bool async;
Andy McFadden3c256212013-08-16 14:55:39 -0700389 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
390 &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700391
392 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700393 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700394 }
395
396 *output = mQueueBufferOutput;
397 return NO_ERROR;
398}
399
400void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
401 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
402 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
403 dbgStateStr());
404 VDS_LOGV("cancelBuffer pslot=%d", pslot);
405 Source source = fbSourceForCompositionType(mCompositionType);
406 return mSource[source]->cancelBuffer(
407 mapProducer2SourceSlot(source, pslot), fence);
408}
409
410int VirtualDisplaySurface::query(int what, int* value) {
411 return mSource[SOURCE_SINK]->query(what, value);
412}
413
Mathias Agopian365857d2013-09-11 19:35:45 -0700414status_t VirtualDisplaySurface::connect(const sp<IBinder>& token,
415 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700416 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700417 QueueBufferOutput qbo;
Mathias Agopian365857d2013-09-11 19:35:45 -0700418 status_t result = mSource[SOURCE_SINK]->connect(token, api, producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700419 if (result == NO_ERROR) {
420 updateQueueBufferOutput(qbo);
421 *output = mQueueBufferOutput;
422 }
423 return result;
424}
425
426status_t VirtualDisplaySurface::disconnect(int api) {
427 return mSource[SOURCE_SINK]->disconnect(api);
428}
429
430void VirtualDisplaySurface::updateQueueBufferOutput(
431 const QueueBufferOutput& qbo) {
432 uint32_t w, h, transformHint, numPendingBuffers;
433 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
434 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
435}
436
437void VirtualDisplaySurface::resetPerFrameState() {
438 mCompositionType = COMPOSITION_UNKNOWN;
439 mSinkBufferWidth = 0;
440 mSinkBufferHeight = 0;
441 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700442 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700443 mFbProducerSlot = -1;
444 mOutputProducerSlot = -1;
445}
446
Jesse Hall028dc8f2013-08-20 16:35:32 -0700447status_t VirtualDisplaySurface::refreshOutputBuffer() {
448 if (mOutputProducerSlot >= 0) {
449 mSource[SOURCE_SINK]->cancelBuffer(
450 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
451 mOutputFence);
452 }
453
454 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700455 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
456 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700457 if (result < 0)
458 return result;
459 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
460
Jesse Hallb716e572013-10-01 17:25:20 -0700461 // On GLES-only frames, we don't have the right output buffer acquire fence
462 // until after GLES calls queueBuffer(). So here we just set the buffer
463 // (for use in HWC prepare) but not the fence; we'll call this again with
464 // the proper fence once we have it.
465 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700466 mProducerBuffers[mOutputProducerSlot]);
467
468 return result;
469}
470
Jesse Hall38efe862013-04-06 23:12:29 -0700471// This slot mapping function is its own inverse, so two copies are unnecessary.
472// Both are kept to make the intent clear where the function is called, and for
473// the (unlikely) chance that we switch to a different mapping function.
474int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
475 if (source == SOURCE_SCRATCH) {
476 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
477 } else {
478 return sslot;
479 }
480}
481int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
482 return mapSource2ProducerSlot(source, pslot);
483}
484
485VirtualDisplaySurface::Source
486VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
487 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
488}
489
490const char* VirtualDisplaySurface::dbgStateStr() const {
491 switch (mDbgState) {
492 case DBG_STATE_IDLE: return "IDLE";
493 case DBG_STATE_PREPARED: return "PREPARED";
494 case DBG_STATE_GLES: return "GLES";
495 case DBG_STATE_GLES_DONE: return "GLES_DONE";
496 case DBG_STATE_HWC: return "HWC";
497 default: return "INVALID";
498 }
499}
500
501const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
502 switch (s) {
503 case SOURCE_SINK: return "SINK";
504 case SOURCE_SCRATCH: return "SCRATCH";
505 default: return "INVALID";
506 }
507}
508
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700509// ---------------------------------------------------------------------------
510} // namespace android
511// ---------------------------------------------------------------------------