blob: 0880a44c0ee21b385cf53b2595480b46deda328a [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);
Jesse Hallffe1f192013-03-22 15:13:48 -070095}
Jesse Hall99c7dbb2013-03-14 14:29:29 -070096
97VirtualDisplaySurface::~VirtualDisplaySurface() {
98}
99
Dan Stoza71433162014-02-04 16:22:36 -0800100status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700101 if (mDisplayId < 0)
102 return NO_ERROR;
103
Dan Stoza71433162014-02-04 16:22:36 -0800104 mMustRecompose = mustRecompose;
105
Jesse Hall38efe862013-04-06 23:12:29 -0700106 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700107 "Unexpected beginFrame() in %s state", dbgStateStr());
108 mDbgState = DBG_STATE_BEGUN;
109
Jesse Hall028dc8f2013-08-20 16:35:32 -0700110 return refreshOutputBuffer();
111}
112
113status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
114 if (mDisplayId < 0)
115 return NO_ERROR;
116
117 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700118 "Unexpected prepareFrame() in %s state", dbgStateStr());
119 mDbgState = DBG_STATE_PREPARED;
120
121 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400122 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
123 // Some hardware can do RGB->YUV conversion more efficiently in hardware
124 // controlled by HWC than in hardware controlled by the video encoder.
125 // Forcing GLES-composed frames to go through an extra copy by the HWC
126 // allows the format conversion to happen there, rather than passing RGB
127 // directly to the consumer.
128 //
129 // On the other hand, when the consumer prefers RGB or can consume RGB
130 // inexpensively, this forces an unnecessary copy.
131 mCompositionType = COMPOSITION_MIXED;
132 }
133
Jesse Hall38efe862013-04-06 23:12:29 -0700134 if (mCompositionType != mDbgLastCompositionType) {
135 VDS_LOGV("prepareFrame: composition type changed to %s",
136 dbgCompositionTypeStr(mCompositionType));
137 mDbgLastCompositionType = mCompositionType;
138 }
139
Jesse Hall1e27ba22013-09-27 09:05:09 -0700140 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800141 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700142 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
143 // We must have just switched from GLES-only to MIXED or HWC
144 // composition. Stop using the format and usage requested by the GLES
145 // driver; they may be suboptimal when HWC is writing to the output
146 // buffer. For example, if the output is going to a video encoder, and
147 // HWC can write directly to YUV, some hardware can skip a
148 // memory-to-memory RGB-to-YUV conversion step.
149 //
150 // If we just switched *to* GLES-only mode, we'll change the
151 // format/usage and get a new buffer when the GLES driver calls
152 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800153 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700154 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
155 refreshOutputBuffer();
156 }
157
Jesse Hall38efe862013-04-06 23:12:29 -0700158 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700159}
160
161status_t VirtualDisplaySurface::compositionComplete() {
162 return NO_ERROR;
163}
164
165status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700166 if (mDisplayId < 0)
167 return NO_ERROR;
168
169 if (mCompositionType == COMPOSITION_HWC) {
170 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
171 "Unexpected advanceFrame() in %s state on HWC frame",
172 dbgStateStr());
173 } else {
174 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
175 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
176 dbgStateStr());
177 }
178 mDbgState = DBG_STATE_HWC;
179
Jesse Hall14e8b012013-11-07 12:28:26 -0800180 if (mOutputProducerSlot < 0 ||
181 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700182 // Last chance bailout if something bad happened earlier. For example,
183 // in a GLES configuration, if the sink disappears then dequeueBuffer
184 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
185 // will soldier on. So we end up here without a buffer. There should
186 // be lots of scary messages in the log just before this.
187 VDS_LOGE("advanceFrame: no buffer, bailing out");
188 return NO_MEMORY;
189 }
190
Jesse Hall14e8b012013-11-07 12:28:26 -0800191 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
192 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700193 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
194 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
195 mFbProducerSlot, fbBuffer.get(),
196 mOutputProducerSlot, outBuffer.get());
197
Jesse Hallb716e572013-10-01 17:25:20 -0700198 // At this point we know the output buffer acquire fence,
199 // so update HWC state with it.
200 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
201
Jesse Hall14e8b012013-11-07 12:28:26 -0800202 status_t result = NO_ERROR;
203 if (fbBuffer != NULL) {
204 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
205 }
206
207 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700208}
209
Jesse Hall851cfe82013-03-20 13:44:00 -0700210void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700211 if (mDisplayId < 0)
212 return;
213
214 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
215 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
216 mDbgState = DBG_STATE_IDLE;
217
218 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
219 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
220 // release the scratch buffer back to the pool
221 Mutex::Autolock lock(mMutex);
222 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
223 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
224 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
225 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
226 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
227 }
228
229 if (mOutputProducerSlot >= 0) {
230 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
231 QueueBufferOutput qbo;
232 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
233 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800234 if (mMustRecompose) {
235 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
236 QueueBufferInput(
237 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800238 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800239 Rect(mSinkBufferWidth, mSinkBufferHeight),
240 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
241 true /* async*/,
242 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 // Don't let a slow consumer block us
297 bool async = (source == SOURCE_SINK);
298
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700299 status_t result = mSource[source]->dequeueBuffer(sslot, fence, async,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700300 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700301 if (result < 0)
302 return result;
303 int pslot = mapSource2ProducerSlot(source, *sslot);
304 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
305 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700306 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700307
Dan Stozafebd4f42014-04-09 16:14:51 -0700308 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700309 // This slot was previously dequeued from the other source; must
310 // re-request the buffer.
311 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700312 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700313 mProducerSlotSource |= sourceBit;
314 }
315
316 if (result & RELEASE_ALL_BUFFERS) {
317 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700318 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700319 mProducerBuffers[i].clear();
320 }
321 }
322 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700323 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
324 if (result < 0) {
325 mProducerBuffers[pslot].clear();
326 mSource[source]->cancelBuffer(*sslot, *fence);
327 return result;
328 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800329 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
330 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
331 mProducerBuffers[pslot]->getPixelFormat(),
332 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700333 }
334
335 return result;
336}
337
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700338status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800339 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) {
Michael Lentine47e45402014-07-18 15:34:25 -0700340 if (mDisplayId < 0)
341 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, async, w, h, format, usage);
342
Jesse Hall38efe862013-04-06 23:12:29 -0700343 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
344 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
345 mDbgState = DBG_STATE_GLES;
346
Jesse Hall8db92552013-08-29 16:03:50 -0700347 VDS_LOGW_IF(!async, "EGL called dequeueBuffer with !async despite eglSwapInterval(0)");
Jesse Hall38efe862013-04-06 23:12:29 -0700348 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
349
Jesse Hall028dc8f2013-08-20 16:35:32 -0700350 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700351 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700352
Jesse Hall38efe862013-04-06 23:12:29 -0700353 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700354
355 if (mOutputProducerSlot < 0) {
356 // Last chance bailout if something bad happened earlier. For example,
357 // in a GLES configuration, if the sink disappears then dequeueBuffer
358 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
359 // will soldier on. So we end up here without a buffer. There should
360 // be lots of scary messages in the log just before this.
361 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
362 return NO_MEMORY;
363 }
364
Jesse Hall028dc8f2013-08-20 16:35:32 -0700365 // We already dequeued the output buffer. If the GLES driver wants
366 // something incompatible, we have to cancel and get a new one. This
367 // will mean that HWC will see a different output buffer between
368 // prepare and set, but since we're in GLES-only mode already it
369 // shouldn't matter.
370
Jesse Hall1e27ba22013-09-27 09:05:09 -0700371 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700372 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700373 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800374 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700375 (w != 0 && w != mSinkBufferWidth) ||
376 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700377 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
378 "want %dx%d fmt=%d use=%#x, "
379 "have %dx%d fmt=%d use=%#x",
380 w, h, format, usage,
381 mSinkBufferWidth, mSinkBufferHeight,
382 buf->getPixelFormat(), buf->getUsage());
383 mOutputFormat = format;
384 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700385 result = refreshOutputBuffer();
386 if (result < 0)
387 return result;
388 }
Jesse Hall38efe862013-04-06 23:12:29 -0700389 }
390
Jesse Hall028dc8f2013-08-20 16:35:32 -0700391 if (source == SOURCE_SINK) {
392 *pslot = mOutputProducerSlot;
393 *fence = mOutputFence;
394 } else {
395 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700396 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700397 if (result >= 0) {
398 *pslot = mapSource2ProducerSlot(source, sslot);
399 }
Jesse Hall38efe862013-04-06 23:12:29 -0700400 }
401 return result;
402}
403
Dan Stoza88a459a2014-03-12 09:34:36 -0700404status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
405 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
406 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800407}
408
Dan Stozad9822a32014-03-28 15:25:31 -0700409status_t VirtualDisplaySurface::detachNextBuffer(
410 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
411 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
412 return INVALID_OPERATION;
413}
414
Dan Stoza88a459a2014-03-12 09:34:36 -0700415status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
416 const sp<GraphicBuffer>& /* buffer */) {
417 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
418 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800419}
420
Jesse Hall38efe862013-04-06 23:12:29 -0700421status_t VirtualDisplaySurface::queueBuffer(int pslot,
422 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700423 if (mDisplayId < 0)
424 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
425
Jesse Hall38efe862013-04-06 23:12:29 -0700426 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
427 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
428 dbgStateStr());
429 mDbgState = DBG_STATE_GLES_DONE;
430
431 VDS_LOGV("queueBuffer pslot=%d", pslot);
432
433 status_t result;
434 if (mCompositionType == COMPOSITION_MIXED) {
435 // Queue the buffer back into the scratch pool
436 QueueBufferOutput scratchQBO;
437 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700438 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700439 if (result != NO_ERROR)
440 return result;
441
442 // Now acquire the buffer from the scratch pool -- should be the same
443 // slot and fence as we just queued.
444 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700445 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700446 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700447 if (result != NO_ERROR)
448 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700449 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700450 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700451 item.mSlot, sslot);
452 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
453 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700454
455 } else {
456 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
457 "Unexpected queueBuffer in state %s for compositionType %s",
458 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
459
460 // Extract the GLES release fence for HWC to acquire
461 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700462 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700464 Rect crop;
465 int scalingMode;
466 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700467 bool async;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800468 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
469 &scalingMode, &transform, &async, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700470
471 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700472 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700473 }
474
475 *output = mQueueBufferOutput;
476 return NO_ERROR;
477}
478
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700479status_t VirtualDisplaySurface::cancelBuffer(int pslot,
480 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700481 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700482 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700483
Jesse Hall38efe862013-04-06 23:12:29 -0700484 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
485 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
486 dbgStateStr());
487 VDS_LOGV("cancelBuffer pslot=%d", pslot);
488 Source source = fbSourceForCompositionType(mCompositionType);
489 return mSource[source]->cancelBuffer(
490 mapProducer2SourceSlot(source, pslot), fence);
491}
492
493int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700494 switch (what) {
495 case NATIVE_WINDOW_WIDTH:
496 *value = mSinkBufferWidth;
497 break;
498 case NATIVE_WINDOW_HEIGHT:
499 *value = mSinkBufferHeight;
500 break;
501 default:
502 return mSource[SOURCE_SINK]->query(what, value);
503 }
504 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700505}
506
Dan Stozaf0eaf252014-03-21 13:05:51 -0700507status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700508 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700509 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700510 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700511 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
512 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700513 if (result == NO_ERROR) {
514 updateQueueBufferOutput(qbo);
515 *output = mQueueBufferOutput;
516 }
517 return result;
518}
519
520status_t VirtualDisplaySurface::disconnect(int api) {
521 return mSource[SOURCE_SINK]->disconnect(api);
522}
523
Jesse Hall399184a2014-03-03 15:42:54 -0800524status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
525 return INVALID_OPERATION;
526}
527
Dan Stoza29a3e902014-06-20 13:13:57 -0700528void VirtualDisplaySurface::allocateBuffers(bool /* async */,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800529 uint32_t /* width */, uint32_t /* height */, PixelFormat /* format */,
Dan Stoza29a3e902014-06-20 13:13:57 -0700530 uint32_t /* usage */) {
531 // TODO: Should we actually allocate buffers for a virtual display?
532}
533
Dan Stoza9de72932015-04-16 17:28:43 -0700534status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
535 return INVALID_OPERATION;
536}
537
Dan Stoza812ed062015-06-02 15:45:22 -0700538status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
539 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
540 return INVALID_OPERATION;
541}
542
Dan Stozac6f30bd2015-06-08 09:32:50 -0700543String8 VirtualDisplaySurface::getConsumerName() const {
544 return String8("VirtualDisplaySurface");
545}
546
Jesse Hall38efe862013-04-06 23:12:29 -0700547void VirtualDisplaySurface::updateQueueBufferOutput(
548 const QueueBufferOutput& qbo) {
549 uint32_t w, h, transformHint, numPendingBuffers;
550 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
551 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
552}
553
554void VirtualDisplaySurface::resetPerFrameState() {
555 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530556 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700557 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700558 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530559 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700560}
561
Jesse Hall028dc8f2013-08-20 16:35:32 -0700562status_t VirtualDisplaySurface::refreshOutputBuffer() {
563 if (mOutputProducerSlot >= 0) {
564 mSource[SOURCE_SINK]->cancelBuffer(
565 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
566 mOutputFence);
567 }
568
569 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700570 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
571 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700572 if (result < 0)
573 return result;
574 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
575
Jesse Hallb716e572013-10-01 17:25:20 -0700576 // On GLES-only frames, we don't have the right output buffer acquire fence
577 // until after GLES calls queueBuffer(). So here we just set the buffer
578 // (for use in HWC prepare) but not the fence; we'll call this again with
579 // the proper fence once we have it.
580 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700581 mProducerBuffers[mOutputProducerSlot]);
582
583 return result;
584}
585
Jesse Hall38efe862013-04-06 23:12:29 -0700586// This slot mapping function is its own inverse, so two copies are unnecessary.
587// Both are kept to make the intent clear where the function is called, and for
588// the (unlikely) chance that we switch to a different mapping function.
589int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
590 if (source == SOURCE_SCRATCH) {
591 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
592 } else {
593 return sslot;
594 }
595}
596int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
597 return mapSource2ProducerSlot(source, pslot);
598}
599
600VirtualDisplaySurface::Source
601VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
602 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
603}
604
605const char* VirtualDisplaySurface::dbgStateStr() const {
606 switch (mDbgState) {
607 case DBG_STATE_IDLE: return "IDLE";
608 case DBG_STATE_PREPARED: return "PREPARED";
609 case DBG_STATE_GLES: return "GLES";
610 case DBG_STATE_GLES_DONE: return "GLES_DONE";
611 case DBG_STATE_HWC: return "HWC";
612 default: return "INVALID";
613 }
614}
615
616const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
617 switch (s) {
618 case SOURCE_SINK: return "SINK";
619 case SOURCE_SCRATCH: return "SCRATCH";
620 default: return "INVALID";
621 }
622}
623
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700624// ---------------------------------------------------------------------------
625} // namespace android
626// ---------------------------------------------------------------------------