blob: 64c1dd92954bc832a61e65268eb95d6c0637eee2 [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>
Pablo Ceballosf133d002015-10-23 12:10:13 -070022#include <gui/IProducerListener.h>
Dan Stoza11611f92015-03-12 15:12:44 -070023
Jesse Hall99c7dbb2013-03-14 14:29:29 -070024// ---------------------------------------------------------------------------
25namespace android {
26// ---------------------------------------------------------------------------
27
Jesse Hallc354eff2013-10-25 10:44:41 -070028#if defined(FORCE_HWC_COPY_FOR_VIRTUAL_DISPLAYS)
29static const bool sForceHwcCopy = true;
30#else
31static const bool sForceHwcCopy = false;
32#endif
Naseer Ahmed6a968462013-10-04 16:15:22 -040033
Jesse Hall24cd98e2014-07-13 14:37:16 -070034#define VDS_LOGE(msg, ...) ALOGE("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070035 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070036#define VDS_LOGW_IF(cond, msg, ...) ALOGW_IF(cond, "[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070037 mDisplayName.string(), ##__VA_ARGS__)
Jesse Hall24cd98e2014-07-13 14:37:16 -070038#define VDS_LOGV(msg, ...) ALOGV("[%s] " msg, \
Jesse Hall38efe862013-04-06 23:12:29 -070039 mDisplayName.string(), ##__VA_ARGS__)
40
41static const char* dbgCompositionTypeStr(DisplaySurface::CompositionType type) {
42 switch (type) {
43 case DisplaySurface::COMPOSITION_UNKNOWN: return "UNKNOWN";
44 case DisplaySurface::COMPOSITION_GLES: return "GLES";
45 case DisplaySurface::COMPOSITION_HWC: return "HWC";
46 case DisplaySurface::COMPOSITION_MIXED: return "MIXED";
47 default: return "<INVALID>";
48 }
49}
50
Jesse Hallffe1f192013-03-22 15:13:48 -070051VirtualDisplaySurface::VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070052 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070053 const sp<IGraphicBufferProducer>& bqProducer,
54 const sp<IGraphicBufferConsumer>& bqConsumer,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055 const String8& name)
Dan Stozab9b08832014-03-13 11:55:57 -070056: ConsumerBase(bqConsumer),
Jesse Hall38efe862013-04-06 23:12:29 -070057 mHwc(hwc),
58 mDisplayId(dispId),
59 mDisplayName(name),
Jesse Hall1e27ba22013-09-27 09:05:09 -070060 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070061 mProducerSlotSource(0),
62 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080063 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
64 mMustRecompose(false)
Jesse Hallffe1f192013-03-22 15:13:48 -070065{
Jesse Hall38efe862013-04-06 23:12:29 -070066 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070067 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070068
69 resetPerFrameState();
70
71 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080072 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
73 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070074 mSinkBufferWidth = sinkWidth;
75 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080076
77 // Pick the buffer format to request from the sink when not rendering to it
78 // with GLES. If the consumer needs CPU access, use the default format
79 // set by the consumer. Otherwise allow gralloc to decide the format based
80 // on usage bits.
81 int sinkUsage;
82 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
83 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
84 int sinkFormat;
85 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
86 mDefaultOutputFormat = sinkFormat;
87 } else {
88 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
89 }
90 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -070091
92 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -070093 mConsumer->setConsumerName(ConsumerBase::mName);
94 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
95 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070096 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -070097 IGraphicBufferProducer::QueueBufferOutput output;
98 mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -070099}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700100
101VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700102 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700103}
104
Dan Stoza71433162014-02-04 16:22:36 -0800105status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700106 if (mDisplayId < 0)
107 return NO_ERROR;
108
Dan Stoza71433162014-02-04 16:22:36 -0800109 mMustRecompose = mustRecompose;
110
Jesse Hall38efe862013-04-06 23:12:29 -0700111 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700112 "Unexpected beginFrame() in %s state", dbgStateStr());
113 mDbgState = DBG_STATE_BEGUN;
114
Jesse Hall028dc8f2013-08-20 16:35:32 -0700115 return refreshOutputBuffer();
116}
117
118status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
119 if (mDisplayId < 0)
120 return NO_ERROR;
121
122 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700123 "Unexpected prepareFrame() in %s state", dbgStateStr());
124 mDbgState = DBG_STATE_PREPARED;
125
126 mCompositionType = compositionType;
Naseer Ahmed6a968462013-10-04 16:15:22 -0400127 if (sForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
128 // Some hardware can do RGB->YUV conversion more efficiently in hardware
129 // controlled by HWC than in hardware controlled by the video encoder.
130 // Forcing GLES-composed frames to go through an extra copy by the HWC
131 // allows the format conversion to happen there, rather than passing RGB
132 // directly to the consumer.
133 //
134 // On the other hand, when the consumer prefers RGB or can consume RGB
135 // inexpensively, this forces an unnecessary copy.
136 mCompositionType = COMPOSITION_MIXED;
137 }
138
Jesse Hall38efe862013-04-06 23:12:29 -0700139 if (mCompositionType != mDbgLastCompositionType) {
140 VDS_LOGV("prepareFrame: composition type changed to %s",
141 dbgCompositionTypeStr(mCompositionType));
142 mDbgLastCompositionType = mCompositionType;
143 }
144
Jesse Hall1e27ba22013-09-27 09:05:09 -0700145 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800146 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700147 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
148 // We must have just switched from GLES-only to MIXED or HWC
149 // composition. Stop using the format and usage requested by the GLES
150 // driver; they may be suboptimal when HWC is writing to the output
151 // buffer. For example, if the output is going to a video encoder, and
152 // HWC can write directly to YUV, some hardware can skip a
153 // memory-to-memory RGB-to-YUV conversion step.
154 //
155 // If we just switched *to* GLES-only mode, we'll change the
156 // format/usage and get a new buffer when the GLES driver calls
157 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800158 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700159 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
160 refreshOutputBuffer();
161 }
162
Jesse Hall38efe862013-04-06 23:12:29 -0700163 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700164}
165
166status_t VirtualDisplaySurface::compositionComplete() {
167 return NO_ERROR;
168}
169
170status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700171 if (mDisplayId < 0)
172 return NO_ERROR;
173
174 if (mCompositionType == COMPOSITION_HWC) {
175 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
176 "Unexpected advanceFrame() in %s state on HWC frame",
177 dbgStateStr());
178 } else {
179 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
180 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
181 dbgStateStr());
182 }
183 mDbgState = DBG_STATE_HWC;
184
Jesse Hall14e8b012013-11-07 12:28:26 -0800185 if (mOutputProducerSlot < 0 ||
186 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700187 // Last chance bailout if something bad happened earlier. For example,
188 // in a GLES configuration, if the sink disappears then dequeueBuffer
189 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
190 // will soldier on. So we end up here without a buffer. There should
191 // be lots of scary messages in the log just before this.
192 VDS_LOGE("advanceFrame: no buffer, bailing out");
193 return NO_MEMORY;
194 }
195
Jesse Hall14e8b012013-11-07 12:28:26 -0800196 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
197 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700198 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
199 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
200 mFbProducerSlot, fbBuffer.get(),
201 mOutputProducerSlot, outBuffer.get());
202
Jesse Hallb716e572013-10-01 17:25:20 -0700203 // At this point we know the output buffer acquire fence,
204 // so update HWC state with it.
205 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
206
Jesse Hall14e8b012013-11-07 12:28:26 -0800207 status_t result = NO_ERROR;
208 if (fbBuffer != NULL) {
209 result = mHwc.fbPost(mDisplayId, mFbFence, fbBuffer);
210 }
211
212 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700213}
214
Jesse Hall851cfe82013-03-20 13:44:00 -0700215void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700216 if (mDisplayId < 0)
217 return;
218
219 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
220 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
221 mDbgState = DBG_STATE_IDLE;
222
223 sp<Fence> fbFence = mHwc.getAndResetReleaseFence(mDisplayId);
224 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
225 // release the scratch buffer back to the pool
226 Mutex::Autolock lock(mMutex);
227 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
228 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
229 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot], fbFence);
230 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
231 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
232 }
233
234 if (mOutputProducerSlot >= 0) {
235 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
236 QueueBufferOutput qbo;
237 sp<Fence> outFence = mHwc.getLastRetireFence(mDisplayId);
238 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800239 if (mMustRecompose) {
240 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
241 QueueBufferInput(
242 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800243 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800244 Rect(mSinkBufferWidth, mSinkBufferHeight),
245 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza71433162014-02-04 16:22:36 -0800246 outFence),
247 &qbo);
248 if (result == NO_ERROR) {
249 updateQueueBufferOutput(qbo);
250 }
251 } else {
252 // If the surface hadn't actually been updated, then we only went
253 // through the motions of updating the display to keep our state
254 // machine happy. We cancel the buffer to avoid triggering another
255 // re-composition and causing an infinite loop.
256 mSource[SOURCE_SINK]->cancelBuffer(sslot, outFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700257 }
258 }
259
260 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700261}
262
Dan Stoza01049c82014-11-11 10:32:31 -0800263void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700264}
265
Michael Lentine47e45402014-07-18 15:34:25 -0700266void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
267 uint32_t tmpW, tmpH, transformHint, numPendingBuffers;
268 mQueueBufferOutput.deflate(&tmpW, &tmpH, &transformHint, &numPendingBuffers);
269 mQueueBufferOutput.inflate(w, h, transformHint, numPendingBuffers);
270
271 mSinkBufferWidth = w;
272 mSinkBufferHeight = h;
273}
274
Jesse Hall38efe862013-04-06 23:12:29 -0700275status_t VirtualDisplaySurface::requestBuffer(int pslot,
276 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700277 if (mDisplayId < 0)
278 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
279
Jesse Hall38efe862013-04-06 23:12:29 -0700280 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
281 "Unexpected requestBuffer pslot=%d in %s state",
282 pslot, dbgStateStr());
283
284 *outBuf = mProducerBuffers[pslot];
285 return NO_ERROR;
286}
287
Pablo Ceballosfa455352015-08-12 17:47:47 -0700288status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
289 int maxDequeuedBuffers) {
290 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
291}
292
293status_t VirtualDisplaySurface::setAsyncMode(bool async) {
294 return mSource[SOURCE_SINK]->setAsyncMode(async);
295}
296
Jesse Hall38efe862013-04-06 23:12:29 -0700297status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800298 PixelFormat format, uint32_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700299 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700300
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700301 status_t result = mSource[source]->dequeueBuffer(sslot, fence,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700302 mSinkBufferWidth, mSinkBufferHeight, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700303 if (result < 0)
304 return result;
305 int pslot = mapSource2ProducerSlot(source, *sslot);
306 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
307 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700308 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700309
Dan Stozafebd4f42014-04-09 16:14:51 -0700310 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700311 // This slot was previously dequeued from the other source; must
312 // re-request the buffer.
313 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700314 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700315 mProducerSlotSource |= sourceBit;
316 }
317
318 if (result & RELEASE_ALL_BUFFERS) {
319 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700320 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700321 mProducerBuffers[i].clear();
322 }
323 }
324 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700325 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
326 if (result < 0) {
327 mProducerBuffers[pslot].clear();
328 mSource[source]->cancelBuffer(*sslot, *fence);
329 return result;
330 }
Jesse Hall497ba0e2013-11-04 16:43:03 -0800331 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#x",
332 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
333 mProducerBuffers[pslot]->getPixelFormat(),
334 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700335 }
336
337 return result;
338}
339
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700340status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800341 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) {
Michael Lentine47e45402014-07-18 15:34:25 -0700342 if (mDisplayId < 0)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700343 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage);
Michael Lentine47e45402014-07-18 15:34:25 -0700344
Jesse Hall38efe862013-04-06 23:12:29 -0700345 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
346 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
347 mDbgState = DBG_STATE_GLES;
348
349 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#x", w, h, format, usage);
350
Jesse Hall028dc8f2013-08-20 16:35:32 -0700351 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700352 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700353
Jesse Hall38efe862013-04-06 23:12:29 -0700354 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700355
356 if (mOutputProducerSlot < 0) {
357 // Last chance bailout if something bad happened earlier. For example,
358 // in a GLES configuration, if the sink disappears then dequeueBuffer
359 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
360 // will soldier on. So we end up here without a buffer. There should
361 // be lots of scary messages in the log just before this.
362 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
363 return NO_MEMORY;
364 }
365
Jesse Hall028dc8f2013-08-20 16:35:32 -0700366 // We already dequeued the output buffer. If the GLES driver wants
367 // something incompatible, we have to cancel and get a new one. This
368 // will mean that HWC will see a different output buffer between
369 // prepare and set, but since we're in GLES-only mode already it
370 // shouldn't matter.
371
Jesse Hall1e27ba22013-09-27 09:05:09 -0700372 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700373 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700374 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800375 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700376 (w != 0 && w != mSinkBufferWidth) ||
377 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700378 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
379 "want %dx%d fmt=%d use=%#x, "
380 "have %dx%d fmt=%d use=%#x",
381 w, h, format, usage,
382 mSinkBufferWidth, mSinkBufferHeight,
383 buf->getPixelFormat(), buf->getUsage());
384 mOutputFormat = format;
385 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700386 result = refreshOutputBuffer();
387 if (result < 0)
388 return result;
389 }
Jesse Hall38efe862013-04-06 23:12:29 -0700390 }
391
Jesse Hall028dc8f2013-08-20 16:35:32 -0700392 if (source == SOURCE_SINK) {
393 *pslot = mOutputProducerSlot;
394 *fence = mOutputFence;
395 } else {
396 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700397 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700398 if (result >= 0) {
399 *pslot = mapSource2ProducerSlot(source, sslot);
400 }
Jesse Hall38efe862013-04-06 23:12:29 -0700401 }
402 return result;
403}
404
Dan Stoza88a459a2014-03-12 09:34:36 -0700405status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
406 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
407 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800408}
409
Dan Stozad9822a32014-03-28 15:25:31 -0700410status_t VirtualDisplaySurface::detachNextBuffer(
411 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
412 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
413 return INVALID_OPERATION;
414}
415
Dan Stoza88a459a2014-03-12 09:34:36 -0700416status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
417 const sp<GraphicBuffer>& /* buffer */) {
418 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
419 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800420}
421
Jesse Hall38efe862013-04-06 23:12:29 -0700422status_t VirtualDisplaySurface::queueBuffer(int pslot,
423 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700424 if (mDisplayId < 0)
425 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
426
Jesse Hall38efe862013-04-06 23:12:29 -0700427 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
428 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
429 dbgStateStr());
430 mDbgState = DBG_STATE_GLES_DONE;
431
432 VDS_LOGV("queueBuffer pslot=%d", pslot);
433
434 status_t result;
435 if (mCompositionType == COMPOSITION_MIXED) {
436 // Queue the buffer back into the scratch pool
437 QueueBufferOutput scratchQBO;
438 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700439 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700440 if (result != NO_ERROR)
441 return result;
442
443 // Now acquire the buffer from the scratch pool -- should be the same
444 // slot and fence as we just queued.
445 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700446 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700447 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700448 if (result != NO_ERROR)
449 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700450 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700451 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700452 item.mSlot, sslot);
453 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
454 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700455
456 } else {
457 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
458 "Unexpected queueBuffer in state %s for compositionType %s",
459 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
460
461 // Extract the GLES release fence for HWC to acquire
462 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700463 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700465 Rect crop;
466 int scalingMode;
467 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800468 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700469 &scalingMode, &transform, &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
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700528void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
529 uint32_t /* height */, PixelFormat /* format */, uint32_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700530 // TODO: Should we actually allocate buffers for a virtual display?
531}
532
Dan Stoza9de72932015-04-16 17:28:43 -0700533status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
534 return INVALID_OPERATION;
535}
536
Dan Stoza812ed062015-06-02 15:45:22 -0700537status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
538 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
539 return INVALID_OPERATION;
540}
541
Dan Stozac6f30bd2015-06-08 09:32:50 -0700542String8 VirtualDisplaySurface::getConsumerName() const {
543 return String8("VirtualDisplaySurface");
544}
545
Dan Stoza7dde5992015-05-22 09:51:44 -0700546uint64_t VirtualDisplaySurface::getNextFrameNumber() const {
547 return 0;
548}
549
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700550status_t VirtualDisplaySurface::setSingleBufferMode(bool singleBufferMode) {
551 return mSource[SOURCE_SINK]->setSingleBufferMode(singleBufferMode);
552}
553
Dan Stoza127fc632015-06-30 13:43:32 -0700554status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
555 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
556 return INVALID_OPERATION;
557}
558
Jesse Hall38efe862013-04-06 23:12:29 -0700559void VirtualDisplaySurface::updateQueueBufferOutput(
560 const QueueBufferOutput& qbo) {
561 uint32_t w, h, transformHint, numPendingBuffers;
562 qbo.deflate(&w, &h, &transformHint, &numPendingBuffers);
563 mQueueBufferOutput.inflate(w, h, 0, numPendingBuffers);
564}
565
566void VirtualDisplaySurface::resetPerFrameState() {
567 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530568 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700569 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700570 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530571 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700572}
573
Jesse Hall028dc8f2013-08-20 16:35:32 -0700574status_t VirtualDisplaySurface::refreshOutputBuffer() {
575 if (mOutputProducerSlot >= 0) {
576 mSource[SOURCE_SINK]->cancelBuffer(
577 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
578 mOutputFence);
579 }
580
581 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700582 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
583 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700584 if (result < 0)
585 return result;
586 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
587
Jesse Hallb716e572013-10-01 17:25:20 -0700588 // On GLES-only frames, we don't have the right output buffer acquire fence
589 // until after GLES calls queueBuffer(). So here we just set the buffer
590 // (for use in HWC prepare) but not the fence; we'll call this again with
591 // the proper fence once we have it.
592 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700593 mProducerBuffers[mOutputProducerSlot]);
594
595 return result;
596}
597
Jesse Hall38efe862013-04-06 23:12:29 -0700598// This slot mapping function is its own inverse, so two copies are unnecessary.
599// Both are kept to make the intent clear where the function is called, and for
600// the (unlikely) chance that we switch to a different mapping function.
601int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
602 if (source == SOURCE_SCRATCH) {
603 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
604 } else {
605 return sslot;
606 }
607}
608int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
609 return mapSource2ProducerSlot(source, pslot);
610}
611
612VirtualDisplaySurface::Source
613VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
614 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
615}
616
617const char* VirtualDisplaySurface::dbgStateStr() const {
618 switch (mDbgState) {
619 case DBG_STATE_IDLE: return "IDLE";
620 case DBG_STATE_PREPARED: return "PREPARED";
621 case DBG_STATE_GLES: return "GLES";
622 case DBG_STATE_GLES_DONE: return "GLES_DONE";
623 case DBG_STATE_HWC: return "HWC";
624 default: return "INVALID";
625 }
626}
627
628const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
629 switch (s) {
630 case SOURCE_SINK: return "SINK";
631 case SOURCE_SCRATCH: return "SCRATCH";
632 default: return "INVALID";
633 }
634}
635
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700636// ---------------------------------------------------------------------------
637} // namespace android
638// ---------------------------------------------------------------------------