blob: 01ca38ae30b211a791ad48b4e8944b9c2f75b097 [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),
50 mProducerUsage(GRALLOC_USAGE_HW_COMPOSER),
51 mProducerSlotSource(0),
52 mDbgState(DBG_STATE_IDLE),
53 mDbgLastCompositionType(COMPOSITION_UNKNOWN)
Jesse Hallffe1f192013-03-22 15:13:48 -070054{
Jesse Hall38efe862013-04-06 23:12:29 -070055 mSource[SOURCE_SINK] = sink;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070056 mSource[SOURCE_SCRATCH] = bq;
Jesse Hall38efe862013-04-06 23:12:29 -070057
58 resetPerFrameState();
59
60 int sinkWidth, sinkHeight;
61 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
62 mSource[SOURCE_SINK]->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
63
64 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070065 mConsumer->setConsumerName(ConsumerBase::mName);
66 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
67 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
68 mConsumer->setDefaultMaxBufferCount(2);
Jesse Hallffe1f192013-03-22 15:13:48 -070069}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070070
71VirtualDisplaySurface::~VirtualDisplaySurface() {
72}
73
Jesse Hall028dc8f2013-08-20 16:35:32 -070074status_t VirtualDisplaySurface::beginFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -070075 if (mDisplayId < 0)
76 return NO_ERROR;
77
78 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -070079 "Unexpected beginFrame() in %s state", dbgStateStr());
80 mDbgState = DBG_STATE_BEGUN;
81
82 uint32_t transformHint, numPendingBuffers;
83 mQueueBufferOutput.deflate(&mSinkBufferWidth, &mSinkBufferHeight,
84 &transformHint, &numPendingBuffers);
85
86 return refreshOutputBuffer();
87}
88
89status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
90 if (mDisplayId < 0)
91 return NO_ERROR;
92
93 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -070094 "Unexpected prepareFrame() in %s state", dbgStateStr());
95 mDbgState = DBG_STATE_PREPARED;
96
97 mCompositionType = compositionType;
98
99 if (mCompositionType != mDbgLastCompositionType) {
100 VDS_LOGV("prepareFrame: composition type changed to %s",
101 dbgCompositionTypeStr(mCompositionType));
102 mDbgLastCompositionType = mCompositionType;
103 }
104
105 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700106}
107
108status_t VirtualDisplaySurface::compositionComplete() {
109 return NO_ERROR;
110}
111
112status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700113 if (mDisplayId < 0)
114 return NO_ERROR;
115
116 if (mCompositionType == COMPOSITION_HWC) {
117 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
118 "Unexpected advanceFrame() in %s state on HWC frame",
119 dbgStateStr());
120 } else {
121 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
122 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
123 dbgStateStr());
124 }
125 mDbgState = DBG_STATE_HWC;
126
Jesse Hall38efe862013-04-06 23:12:29 -0700127 if (mCompositionType == COMPOSITION_HWC) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700128 // Use the output buffer for the FB as well, though conceptually the
129 // FB is unused on this frame.
Jesse Hall38efe862013-04-06 23:12:29 -0700130 mFbProducerSlot = mOutputProducerSlot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700131 mFbFence = mOutputFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700132 }
133
134 if (mFbProducerSlot < 0 || mOutputProducerSlot < 0) {
135 // Last chance bailout if something bad happened earlier. For example,
136 // in a GLES configuration, if the sink disappears then dequeueBuffer
137 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
138 // will soldier on. So we end up here without a buffer. There should
139 // be lots of scary messages in the log just before this.
140 VDS_LOGE("advanceFrame: no buffer, bailing out");
141 return NO_MEMORY;
142 }
143
144 sp<GraphicBuffer> fbBuffer = mProducerBuffers[mFbProducerSlot];
145 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
146 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
147 mFbProducerSlot, fbBuffer.get(),
148 mOutputProducerSlot, outBuffer.get());
149
Jesse Hall028dc8f2013-08-20 16:35:32 -0700150 return mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700151}
152
Jesse Hall851cfe82013-03-20 13:44:00 -0700153void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700154 if (mDisplayId < 0)
155 return;
156
157 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
158 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
159 mDbgState = DBG_STATE_IDLE;
160
161 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
162 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
163 // release the scratch buffer back to the pool
164 Mutex::Autolock lock(mMutex);
165 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
166 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
167 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
168 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
169 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
170 }
171
172 if (mOutputProducerSlot >= 0) {
173 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
174 QueueBufferOutput qbo;
175 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
176 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
177 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
Andy McFadden3c256212013-08-16 14:55:39 -0700178 QueueBufferInput(systemTime(), false,
Jesse Hall38efe862013-04-06 23:12:29 -0700179 Rect(mSinkBufferWidth, mSinkBufferHeight),
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700180 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, outFence),
Jesse Hall38efe862013-04-06 23:12:29 -0700181 &qbo);
182 if (result == NO_ERROR) {
183 updateQueueBufferOutput(qbo);
184 }
185 }
186
187 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700188}
189
190void VirtualDisplaySurface::dump(String8& result) const {
191}
192
Jesse Hall38efe862013-04-06 23:12:29 -0700193status_t VirtualDisplaySurface::requestBuffer(int pslot,
194 sp<GraphicBuffer>* outBuf) {
195 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
196 "Unexpected requestBuffer pslot=%d in %s state",
197 pslot, dbgStateStr());
198
199 *outBuf = mProducerBuffers[pslot];
200 return NO_ERROR;
201}
202
203status_t VirtualDisplaySurface::setBufferCount(int bufferCount) {
204 return mSource[SOURCE_SINK]->setBufferCount(bufferCount);
205}
206
207status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700208 uint32_t format, int* sslot, sp<Fence>* fence, bool async) {
209 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall38efe862013-04-06 23:12:29 -0700210 mSinkBufferWidth, mSinkBufferHeight, format, mProducerUsage);
211 if (result < 0)
212 return result;
213 int pslot = mapSource2ProducerSlot(source, *sslot);
214 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
215 dbgSourceStr(source), *sslot, pslot, result);
216 uint32_t sourceBit = static_cast<uint32_t>(source) << pslot;
217
218 if ((mProducerSlotSource & (1u << pslot)) != sourceBit) {
219 // This slot was previously dequeued from the other source; must
220 // re-request the buffer.
221 result |= BUFFER_NEEDS_REALLOCATION;
222 mProducerSlotSource &= ~(1u << pslot);
223 mProducerSlotSource |= sourceBit;
224 }
225
226 if (result & RELEASE_ALL_BUFFERS) {
227 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
228 if ((mProducerSlotSource & (1u << i)) == sourceBit)
229 mProducerBuffers[i].clear();
230 }
231 }
232 if (result & BUFFER_NEEDS_REALLOCATION) {
233 mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
234 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p",
235 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get());
236 }
237
238 return result;
239}
240
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700241status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Jesse Hall38efe862013-04-06 23:12:29 -0700242 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
243 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
244 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
245 mDbgState = DBG_STATE_GLES;
246
247 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
248
Jesse Hall028dc8f2013-08-20 16:35:32 -0700249 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700250 mProducerUsage = usage | GRALLOC_USAGE_HW_COMPOSER;
251 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700252
Jesse Hall38efe862013-04-06 23:12:29 -0700253 if (source == SOURCE_SINK) {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700254 // We already dequeued the output buffer. If the GLES driver wants
255 // something incompatible, we have to cancel and get a new one. This
256 // will mean that HWC will see a different output buffer between
257 // prepare and set, but since we're in GLES-only mode already it
258 // shouldn't matter.
259
260 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
261 if ((mProducerUsage & ~buf->getUsage()) != 0 ||
262 (format != 0 && format != (uint32_t)buf->getPixelFormat()) ||
263 (w != 0 && w != mSinkBufferWidth) ||
264 (h != 0 && h != mSinkBufferHeight)) {
265 VDS_LOGV("dequeueBuffer: output buffer doesn't satisfy GLES "
266 "request, getting a new buffer");
267 result = refreshOutputBuffer();
268 if (result < 0)
269 return result;
270 }
Jesse Hall38efe862013-04-06 23:12:29 -0700271 }
272
Jesse Hall028dc8f2013-08-20 16:35:32 -0700273 if (source == SOURCE_SINK) {
274 *pslot = mOutputProducerSlot;
275 *fence = mOutputFence;
276 } else {
277 int sslot;
278 result = dequeueBuffer(source, format, &sslot, fence, async);
279 if (result >= 0) {
280 *pslot = mapSource2ProducerSlot(source, sslot);
281 }
Jesse Hall38efe862013-04-06 23:12:29 -0700282 }
283 return result;
284}
285
286status_t VirtualDisplaySurface::queueBuffer(int pslot,
287 const QueueBufferInput& input, QueueBufferOutput* output) {
288 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
289 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
290 dbgStateStr());
291 mDbgState = DBG_STATE_GLES_DONE;
292
293 VDS_LOGV("queueBuffer pslot=%d", pslot);
294
295 status_t result;
296 if (mCompositionType == COMPOSITION_MIXED) {
297 // Queue the buffer back into the scratch pool
298 QueueBufferOutput scratchQBO;
299 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700300 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700301 if (result != NO_ERROR)
302 return result;
303
304 // Now acquire the buffer from the scratch pool -- should be the same
305 // slot and fence as we just queued.
306 Mutex::Autolock lock(mMutex);
307 BufferQueue::BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700308 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700309 if (result != NO_ERROR)
310 return result;
311 VDS_LOGW_IF(item.mBuf != sslot,
312 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
313 item.mBuf, sslot);
314 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mBuf);
315 mFbFence = mSlots[item.mBuf].mFence;
316
317 } else {
318 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
319 "Unexpected queueBuffer in state %s for compositionType %s",
320 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
321
322 // Extract the GLES release fence for HWC to acquire
323 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700324 bool isAutoTimestamp;
Jesse Hall38efe862013-04-06 23:12:29 -0700325 Rect crop;
326 int scalingMode;
327 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700328 bool async;
Andy McFadden3c256212013-08-16 14:55:39 -0700329 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode,
330 &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700331
332 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700333 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700334 }
335
336 *output = mQueueBufferOutput;
337 return NO_ERROR;
338}
339
340void VirtualDisplaySurface::cancelBuffer(int pslot, const sp<Fence>& fence) {
341 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
342 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
343 dbgStateStr());
344 VDS_LOGV("cancelBuffer pslot=%d", pslot);
345 Source source = fbSourceForCompositionType(mCompositionType);
346 return mSource[source]->cancelBuffer(
347 mapProducer2SourceSlot(source, pslot), fence);
348}
349
350int VirtualDisplaySurface::query(int what, int* value) {
351 return mSource[SOURCE_SINK]->query(what, value);
352}
353
Mathias Agopian595264f2013-07-16 22:56:09 -0700354status_t VirtualDisplaySurface::connect(int api, bool producerControlledByApp,
355 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700356 QueueBufferOutput qbo;
Mathias Agopian595264f2013-07-16 22:56:09 -0700357 status_t result = mSource[SOURCE_SINK]->connect(api, producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700358 if (result == NO_ERROR) {
359 updateQueueBufferOutput(qbo);
360 *output = mQueueBufferOutput;
361 }
362 return result;
363}
364
365status_t VirtualDisplaySurface::disconnect(int api) {
366 return mSource[SOURCE_SINK]->disconnect(api);
367}
368
369void VirtualDisplaySurface::updateQueueBufferOutput(
370 const QueueBufferOutput& qbo) {
371 uint32_t w, h, transformHint, numPendingBuffers;
372 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
373 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
374}
375
376void VirtualDisplaySurface::resetPerFrameState() {
377 mCompositionType = COMPOSITION_UNKNOWN;
378 mSinkBufferWidth = 0;
379 mSinkBufferHeight = 0;
380 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700381 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700382 mFbProducerSlot = -1;
383 mOutputProducerSlot = -1;
384}
385
Jesse Hall028dc8f2013-08-20 16:35:32 -0700386status_t VirtualDisplaySurface::refreshOutputBuffer() {
387 if (mOutputProducerSlot >= 0) {
388 mSource[SOURCE_SINK]->cancelBuffer(
389 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
390 mOutputFence);
391 }
392
393 int sslot;
394 status_t result = dequeueBuffer(SOURCE_SINK, 0, &sslot, &mOutputFence, false);
395 if (result < 0)
396 return result;
397 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
398
399 result = mHwc.setOutputBuffer(mDisplayId, mOutputFence,
400 mProducerBuffers[mOutputProducerSlot]);
401
402 return result;
403}
404
Jesse Hall38efe862013-04-06 23:12:29 -0700405// This slot mapping function is its own inverse, so two copies are unnecessary.
406// Both are kept to make the intent clear where the function is called, and for
407// the (unlikely) chance that we switch to a different mapping function.
408int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
409 if (source == SOURCE_SCRATCH) {
410 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
411 } else {
412 return sslot;
413 }
414}
415int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
416 return mapSource2ProducerSlot(source, pslot);
417}
418
419VirtualDisplaySurface::Source
420VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
421 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
422}
423
424const char* VirtualDisplaySurface::dbgStateStr() const {
425 switch (mDbgState) {
426 case DBG_STATE_IDLE: return "IDLE";
427 case DBG_STATE_PREPARED: return "PREPARED";
428 case DBG_STATE_GLES: return "GLES";
429 case DBG_STATE_GLES_DONE: return "GLES_DONE";
430 case DBG_STATE_HWC: return "HWC";
431 default: return "INVALID";
432 }
433}
434
435const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
436 switch (s) {
437 case SOURCE_SINK: return "SINK";
438 case SOURCE_SCRATCH: return "SCRATCH";
439 default: return "INVALID";
440 }
441}
442
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700443// ---------------------------------------------------------------------------
444} // namespace android
445// ---------------------------------------------------------------------------