blob: 48971bc2d032e557a8b4a136d5418f41c4d3be28 [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
Dan Stoza11611f92015-03-12 15:12:44 -070021#include <gui/BufferItem.h>
22
Jesse Hall99c7dbb2013-03-14 14:29:29 -070023// ---------------------------------------------------------------------------
24namespace android {
25// ---------------------------------------------------------------------------
26
Jesse Hallc354eff2013-10-25 10:44:41 -070027#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
28static const bool sForceHwcCopy = true;
29#else
30static const bool sForceHwcCopy = false;
31#endif
Naseer Ahmed6a968462013-10-04 16:15:22 -040032
Jesse Hall24cd98e2014-07-13 14:37:16 -070033#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070034 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070035#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070036 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070037#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070038 mDisplayName.string(), ##__VA_ARGS__)
39
40static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
41 switch (type) {
42 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
43 case DisplaySurface::COMPOSITION_GLES: return "GLES";
44 case DisplaySurface::COMPOSITION_HWC: return "HWC";
45 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
46 default: return "<INVALID>";
47 }
48}
49
Jesse Hallffe1f192013-03-22 15:13:48 -070050VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070051 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070052 const sp<IGraphicBufferProducer>& bqProducer,
53 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070054 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070055: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070056 mHwc(hwc),
57 mDisplayId(dispId),
58 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070059 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070060 mProducerSlotSource(0),
61 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080062 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
63 mMustRecompose(false)
Jesse Hallffe1f192013-03-22 15:13:48 -070064{
Jesse Hall38efe862013-04-06 23:12:29 -070065 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070066 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070067
68 resetPerFrameState();
69
70 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080071 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
72 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070073 mSinkBufferWidth = sinkWidth;
74 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080075
76 // Pick the buffer format to request from the sink when not rendering to it
77 // with GLES. If the consumer needs CPU access, use the default format
78 // set by the consumer. Otherwise allow gralloc to decide the format based
79 // on usage bits.
80 int sinkUsage;
81 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
82 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
83 int sinkFormat;
84 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
85 mDefaultOutputFormat = sinkFormat;
86 } else {
87 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
88 }
89 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -070090
91 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070092 mConsumer->setConsumerName(ConsumerBase::mName);
93 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
94 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070095 sink->setAsyncMode(true);
Jesse Hallffe1f192013-03-22 15:13:48 -070096}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070097
98VirtualDisplaySurface::~VirtualDisplaySurface() {
99}
100
Dan Stoza71433162014-02-04 16:22:36 -0800101status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700102 if (mDisplayId < 0)
103 return NO_ERROR;
104
Dan Stoza71433162014-02-04 16:22:36 -0800105 mMustRecompose = mustRecompose;
106
Jesse Hall38efe862013-04-06 23:12:29 -0700107 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700108 "Unexpected beginFrame() in %s state", dbgStateStr());
109 mDbgState = DBG_STATE_BEGUN;
110
Jesse Hall028dc8f2013-08-20 16:35:32 -0700111 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 */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800239 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800240 Rect(mSinkBufferWidth, mSinkBufferHeight),
241 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza71433162014-02-04 16:22:36 -0800242 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 Stoza01049c82014-11-11 10:32:31 -0800259void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700260}
261
Michael Lentine47e45402014-07-18 15:34:25 -0700262void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
263 uint32_t tmpW, tmpH, transformHint, numPendingBuffers;
264 mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers);
265 mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers);
266
267 mSinkBufferWidth = w;
268 mSinkBufferHeight = h;
269}
270
Jesse Hall38efe862013-04-06 23:12:29 -0700271status_t VirtualDisplaySurface::requestBuffer(int pslot,
272 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700273 if (mDisplayId < 0)
274 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
275
Jesse Hall38efe862013-04-06 23:12:29 -0700276 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
277 "Unexpected requestBuffer pslot=%d in %s state",
278 pslot, dbgStateStr());
279
280 *outBuf = mProducerBuffers[pslot];
281 return NO_ERROR;
282}
283
Pablo Ceballosfa455352015-08-12 17:47:47 -0700284status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
285 int maxDequeuedBuffers) {
286 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
287}
288
289status_t VirtualDisplaySurface::setAsyncMode(bool async) {
290 return mSource[SOURCE_SINK]->setAsyncMode(async);
291}
292
Jesse Hall38efe862013-04-06 23:12:29 -0700293status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800294 PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700295 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700296
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700297 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700298 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700299 if (result < 0)
300 return result;
301 int pslot = mapSource2ProducerSlot(source, *sslot);
302 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
303 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700304 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700305
Dan Stozafebd4f42014-04-09 16:14:51 -0700306 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700307 // This slot was previously dequeued from the other source; must
308 // re-request the buffer.
309 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700310 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700311 mProducerSlotSource |= sourceBit;
312 }
313
314 if (result & RELEASE_ALL_BUFFERS) {
315 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700316 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700317 mProducerBuffers[i].clear();
318 }
319 }
320 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700321 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
322 if (result < 0) {
323 mProducerBuffers[pslot].clear();
324 mSource[source]->cancelBuffer(*sslot, *fence);
325 return result;
326 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800327 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
328 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
329 mProducerBuffers[pslot]->getPixelFormat(),
330 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700331 }
332
333 return result;
334}
335
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700336status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800337 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) {
Michael Lentine47e45402014-07-18 15:34:25 -0700338 if (mDisplayId < 0)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700339 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage);
Michael Lentine47e45402014-07-18 15:34:25 -0700340
Jesse Hall38efe862013-04-06 23:12:29 -0700341 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
342 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
343 mDbgState = DBG_STATE_GLES;
344
345 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
346
Jesse Hall028dc8f2013-08-20 16:35:32 -0700347 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700348 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700349
Jesse Hall38efe862013-04-06 23:12:29 -0700350 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700351
352 if (mOutputProducerSlot < 0) {
353 // Last chance bailout if something bad happened earlier. For example,
354 // in a GLES configuration, if the sink disappears then dequeueBuffer
355 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
356 // will soldier on. So we end up here without a buffer. There should
357 // be lots of scary messages in the log just before this.
358 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
359 return NO_MEMORY;
360 }
361
Jesse Hall028dc8f2013-08-20 16:35:32 -0700362 // We already dequeued the output buffer. If the GLES driver wants
363 // something incompatible, we have to cancel and get a new one. This
364 // will mean that HWC will see a different output buffer between
365 // prepare and set, but since we're in GLES-only mode already it
366 // shouldn't matter.
367
Jesse Hall1e27ba22013-09-27 09:05:09 -0700368 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700369 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700370 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800371 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700372 (w != 0 && w != mSinkBufferWidth) ||
373 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700374 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
375 "want %dx%d fmt=%d use=%#x, "
376 "have %dx%d fmt=%d use=%#x",
377 w, h, format, usage,
378 mSinkBufferWidth, mSinkBufferHeight,
379 buf->getPixelFormat(), buf->getUsage());
380 mOutputFormat = format;
381 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700382 result = refreshOutputBuffer();
383 if (result < 0)
384 return result;
385 }
Jesse Hall38efe862013-04-06 23:12:29 -0700386 }
387
Jesse Hall028dc8f2013-08-20 16:35:32 -0700388 if (source == SOURCE_SINK) {
389 *pslot = mOutputProducerSlot;
390 *fence = mOutputFence;
391 } else {
392 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700393 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700394 if (result >= 0) {
395 *pslot = mapSource2ProducerSlot(source, sslot);
396 }
Jesse Hall38efe862013-04-06 23:12:29 -0700397 }
398 return result;
399}
400
Dan Stoza88a459a2014-03-12 09:34:36 -0700401status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
402 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
403 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800404}
405
Dan Stozad9822a32014-03-28 15:25:31 -0700406status_t VirtualDisplaySurface::detachNextBuffer(
407 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
408 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
409 return INVALID_OPERATION;
410}
411
Dan Stoza88a459a2014-03-12 09:34:36 -0700412status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
413 const sp<GraphicBuffer>& /* buffer */) {
414 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
415 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800416}
417
Jesse Hall38efe862013-04-06 23:12:29 -0700418status_t VirtualDisplaySurface::queueBuffer(int pslot,
419 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700420 if (mDisplayId < 0)
421 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
422
Jesse Hall38efe862013-04-06 23:12:29 -0700423 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
424 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
425 dbgStateStr());
426 mDbgState = DBG_STATE_GLES_DONE;
427
428 VDS_LOGV("queueBuffer pslot=%d", pslot);
429
430 status_t result;
431 if (mCompositionType == COMPOSITION_MIXED) {
432 // Queue the buffer back into the scratch pool
433 QueueBufferOutput scratchQBO;
434 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700435 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700436 if (result != NO_ERROR)
437 return result;
438
439 // Now acquire the buffer from the scratch pool -- should be the same
440 // slot and fence as we just queued.
441 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700442 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700443 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700444 if (result != NO_ERROR)
445 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700446 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700447 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700448 item.mSlot, sslot);
449 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
450 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700451
452 } else {
453 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
454 "Unexpected queueBuffer in state %s for compositionType %s",
455 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
456
457 // Extract the GLES release fence for HWC to acquire
458 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700459 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700461 Rect crop;
462 int scalingMode;
463 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700465 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700466
467 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700468 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700469 }
470
471 *output = mQueueBufferOutput;
472 return NO_ERROR;
473}
474
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700475status_t VirtualDisplaySurface::cancelBuffer(int pslot,
476 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700477 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700478 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700479
Jesse Hall38efe862013-04-06 23:12:29 -0700480 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
481 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
482 dbgStateStr());
483 VDS_LOGV("cancelBuffer pslot=%d", pslot);
484 Source source = fbSourceForCompositionType(mCompositionType);
485 return mSource[source]->cancelBuffer(
486 mapProducer2SourceSlot(source, pslot), fence);
487}
488
489int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700490 switch (what) {
491 case NATIVE_WINDOW_WIDTH:
492 *value = mSinkBufferWidth;
493 break;
494 case NATIVE_WINDOW_HEIGHT:
495 *value = mSinkBufferHeight;
496 break;
497 default:
498 return mSource[SOURCE_SINK]->query(what, value);
499 }
500 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700501}
502
Dan Stozaf0eaf252014-03-21 13:05:51 -0700503status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700504 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700505 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700506 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700507 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
508 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700509 if (result == NO_ERROR) {
510 updateQueueBufferOutput(qbo);
511 *output = mQueueBufferOutput;
512 }
513 return result;
514}
515
516status_t VirtualDisplaySurface::disconnect(int api) {
517 return mSource[SOURCE_SINK]->disconnect(api);
518}
519
Jesse Hall399184a2014-03-03 15:42:54 -0800520status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
521 return INVALID_OPERATION;
522}
523
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700524void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
525 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700526 // TODO: Should we actually allocate buffers for a virtual display?
527}
528
Dan Stoza9de72932015-04-16 17:28:43 -0700529status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
530 return INVALID_OPERATION;
531}
532
Dan Stoza812ed062015-06-02 15:45:22 -0700533status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
534 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
535 return INVALID_OPERATION;
536}
537
Dan Stozac6f30bd2015-06-08 09:32:50 -0700538String8 VirtualDisplaySurface::getConsumerName() const {
539 return String8("VirtualDisplaySurface");
540}
541
Jesse Hall38efe862013-04-06 23:12:29 -0700542void VirtualDisplaySurface::updateQueueBufferOutput(
543 const QueueBufferOutput& qbo) {
544 uint32_t w, h, transformHint, numPendingBuffers;
545 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
546 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
547}
548
549void VirtualDisplaySurface::resetPerFrameState() {
550 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530551 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700552 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700553 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530554 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700555}
556
Jesse Hall028dc8f2013-08-20 16:35:32 -0700557status_t VirtualDisplaySurface::refreshOutputBuffer() {
558 if (mOutputProducerSlot >= 0) {
559 mSource[SOURCE_SINK]->cancelBuffer(
560 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
561 mOutputFence);
562 }
563
564 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700565 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
566 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700567 if (result < 0)
568 return result;
569 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
570
Jesse Hallb716e572013-10-01 17:25:20 -0700571 // On GLES-only frames, we don't have the right output buffer acquire fence
572 // until after GLES calls queueBuffer(). So here we just set the buffer
573 // (for use in HWC prepare) but not the fence; we'll call this again with
574 // the proper fence once we have it.
575 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700576 mProducerBuffers[mOutputProducerSlot]);
577
578 return result;
579}
580
Jesse Hall38efe862013-04-06 23:12:29 -0700581// This slot mapping function is its own inverse, so two copies are unnecessary.
582// Both are kept to make the intent clear where the function is called, and for
583// the (unlikely) chance that we switch to a different mapping function.
584int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
585 if (source == SOURCE_SCRATCH) {
586 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
587 } else {
588 return sslot;
589 }
590}
591int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
592 return mapSource2ProducerSlot(source, pslot);
593}
594
595VirtualDisplaySurface::Source
596VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
597 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
598}
599
600const char* VirtualDisplaySurface::dbgStateStr() const {
601 switch (mDbgState) {
602 case DBG_STATE_IDLE: return "IDLE";
603 case DBG_STATE_PREPARED: return "PREPARED";
604 case DBG_STATE_GLES: return "GLES";
605 case DBG_STATE_GLES_DONE: return "GLES_DONE";
606 case DBG_STATE_HWC: return "HWC";
607 default: return "INVALID";
608 }
609}
610
611const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
612 switch (s) {
613 case SOURCE_SINK: return "SINK";
614 case SOURCE_SCRATCH: return "SCRATCH";
615 default: return "INVALID";
616 }
617}
618
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700619// ---------------------------------------------------------------------------
620} // namespace android
621// ---------------------------------------------------------------------------