blob: dba6f7cf9a00332ad69dfd844b2986182ed9d5db [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"
Mathias Agopiancb496ac2017-05-22 14:21:00 -070019
20#include <inttypes.h>
21
Jesse Hall38efe862013-04-06 23:12:29 -070022#include "HWComposer.h"
Fabien Sanglarda34ed632017-03-14 11:43:52 -070023#include "SurfaceFlinger.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070024
Dan Stoza11611f92015-03-12 15:12:44 -070025#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080026#include <gui/BufferQueue.h>
Pablo Ceballosf133d002015-10-23 12:10:13 -070027#include <gui/IProducerListener.h>
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070028#include <system/window.h>
Dan Stoza11611f92015-03-12 15:12:44 -070029
Jesse Hall99c7dbb2013-03-14 14:29:29 -070030// ---------------------------------------------------------------------------
31namespace android {
32// ---------------------------------------------------------------------------
33
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),
Dan Stoza9e56aa02015-11-02 13:00:03 -080060 mSource{},
61 mDefaultOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
62 mOutputFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Jesse Hall1e27ba22013-09-27 09:05:09 -070063 mOutputUsage(GRALLOC_USAGE_HW_COMPOSER),
Jesse Hall38efe862013-04-06 23:12:29 -070064 mProducerSlotSource(0),
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mProducerBuffers(),
66 mQueueBufferOutput(),
67 mSinkBufferWidth(0),
68 mSinkBufferHeight(0),
69 mCompositionType(COMPOSITION_UNKNOWN),
70 mFbFence(Fence::NO_FENCE),
71 mOutputFence(Fence::NO_FENCE),
72 mFbProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
73 mOutputProducerSlot(BufferQueue::INVALID_BUFFER_SLOT),
Jesse Hall38efe862013-04-06 23:12:29 -070074 mDbgState(DBG_STATE_IDLE),
Dan Stoza71433162014-02-04 16:22:36 -080075 mDbgLastCompositionType(COMPOSITION_UNKNOWN),
Fabien Sanglarda34ed632017-03-14 11:43:52 -070076 mMustRecompose(false),
77 mForceHwcCopy(SurfaceFlinger::useHwcForRgbToYuv)
Jesse Hallffe1f192013-03-22 15:13:48 -070078{
Jesse Hall38efe862013-04-06 23:12:29 -070079 mSource[SOURCE_SINK] = sink;
Dan Stozab9b08832014-03-13 11:55:57 -070080 mSource[SOURCE_SCRATCH] = bqProducer;
Jesse Hall38efe862013-04-06 23:12:29 -070081
82 resetPerFrameState();
83
84 int sinkWidth, sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080085 sink->query(NATIVE_WINDOW_WIDTH, &sinkWidth);
86 sink->query(NATIVE_WINDOW_HEIGHT, &sinkHeight);
Michael Lentine47e45402014-07-18 15:34:25 -070087 mSinkBufferWidth = sinkWidth;
88 mSinkBufferHeight = sinkHeight;
Jesse Hall497ba0e2013-11-04 16:43:03 -080089
90 // Pick the buffer format to request from the sink when not rendering to it
91 // with GLES. If the consumer needs CPU access, use the default format
92 // set by the consumer. Otherwise allow gralloc to decide the format based
93 // on usage bits.
94 int sinkUsage;
95 sink->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
96 if (sinkUsage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
97 int sinkFormat;
98 sink->query(NATIVE_WINDOW_FORMAT, &sinkFormat);
99 mDefaultOutputFormat = sinkFormat;
100 } else {
101 mDefaultOutputFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
102 }
103 mOutputFormat = mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700104
105 ConsumerBase::mName = String8::format("VDS: %s", mDisplayName.string());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700106 mConsumer->setConsumerName(ConsumerBase::mName);
107 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
108 mConsumer->setDefaultBufferSize(sinkWidth, sinkHeight);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700109 sink->setAsyncMode(true);
Pablo Ceballosf133d002015-10-23 12:10:13 -0700110 IGraphicBufferProducer::QueueBufferOutput output;
111 mSource[SOURCE_SCRATCH]->connect(NULL, NATIVE_WINDOW_API_EGL, false, &output);
Jesse Hallffe1f192013-03-22 15:13:48 -0700112}
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700113
114VirtualDisplaySurface::~VirtualDisplaySurface() {
Pablo Ceballosf133d002015-10-23 12:10:13 -0700115 mSource[SOURCE_SCRATCH]->disconnect(NATIVE_WINDOW_API_EGL);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700116}
117
Dan Stoza71433162014-02-04 16:22:36 -0800118status_t VirtualDisplaySurface::beginFrame(bool mustRecompose) {
Jesse Hall38efe862013-04-06 23:12:29 -0700119 if (mDisplayId < 0)
120 return NO_ERROR;
121
Dan Stoza71433162014-02-04 16:22:36 -0800122 mMustRecompose = mustRecompose;
123
Jesse Hall38efe862013-04-06 23:12:29 -0700124 VDS_LOGW_IF(mDbgState != DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700125 "Unexpected beginFrame() in %s state", dbgStateStr());
126 mDbgState = DBG_STATE_BEGUN;
127
Jesse Hall028dc8f2013-08-20 16:35:32 -0700128 return refreshOutputBuffer();
129}
130
131status_t VirtualDisplaySurface::prepareFrame(CompositionType compositionType) {
132 if (mDisplayId < 0)
133 return NO_ERROR;
134
135 VDS_LOGW_IF(mDbgState != DBG_STATE_BEGUN,
Jesse Hall38efe862013-04-06 23:12:29 -0700136 "Unexpected prepareFrame() in %s state", dbgStateStr());
137 mDbgState = DBG_STATE_PREPARED;
138
139 mCompositionType = compositionType;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700140 if (mForceHwcCopy && mCompositionType == COMPOSITION_GLES) {
Naseer Ahmed6a968462013-10-04 16:15:22 -0400141 // Some hardware can do RGB->YUV conversion more efficiently in hardware
142 // controlled by HWC than in hardware controlled by the video encoder.
143 // Forcing GLES-composed frames to go through an extra copy by the HWC
144 // allows the format conversion to happen there, rather than passing RGB
145 // directly to the consumer.
146 //
147 // On the other hand, when the consumer prefers RGB or can consume RGB
148 // inexpensively, this forces an unnecessary copy.
149 mCompositionType = COMPOSITION_MIXED;
150 }
151
Jesse Hall38efe862013-04-06 23:12:29 -0700152 if (mCompositionType != mDbgLastCompositionType) {
153 VDS_LOGV("prepareFrame: composition type changed to %s",
154 dbgCompositionTypeStr(mCompositionType));
155 mDbgLastCompositionType = mCompositionType;
156 }
157
Jesse Hall1e27ba22013-09-27 09:05:09 -0700158 if (mCompositionType != COMPOSITION_GLES &&
Jesse Hall497ba0e2013-11-04 16:43:03 -0800159 (mOutputFormat != mDefaultOutputFormat ||
Jesse Hall1e27ba22013-09-27 09:05:09 -0700160 mOutputUsage != GRALLOC_USAGE_HW_COMPOSER)) {
161 // We must have just switched from GLES-only to MIXED or HWC
162 // composition. Stop using the format and usage requested by the GLES
163 // driver; they may be suboptimal when HWC is writing to the output
164 // buffer. For example, if the output is going to a video encoder, and
165 // HWC can write directly to YUV, some hardware can skip a
166 // memory-to-memory RGB-to-YUV conversion step.
167 //
168 // If we just switched *to* GLES-only mode, we'll change the
169 // format/usage and get a new buffer when the GLES driver calls
170 // dequeueBuffer().
Jesse Hall497ba0e2013-11-04 16:43:03 -0800171 mOutputFormat = mDefaultOutputFormat;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700172 mOutputUsage = GRALLOC_USAGE_HW_COMPOSER;
173 refreshOutputBuffer();
174 }
175
Jesse Hall38efe862013-04-06 23:12:29 -0700176 return NO_ERROR;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700177}
178
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700179status_t VirtualDisplaySurface::advanceFrame() {
Jesse Hall38efe862013-04-06 23:12:29 -0700180 if (mDisplayId < 0)
181 return NO_ERROR;
182
183 if (mCompositionType == COMPOSITION_HWC) {
184 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
185 "Unexpected advanceFrame() in %s state on HWC frame",
186 dbgStateStr());
187 } else {
188 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES_DONE,
189 "Unexpected advanceFrame() in %s state on GLES/MIXED frame",
190 dbgStateStr());
191 }
192 mDbgState = DBG_STATE_HWC;
193
Jesse Hall14e8b012013-11-07 12:28:26 -0800194 if (mOutputProducerSlot < 0 ||
195 (mCompositionType != COMPOSITION_HWC && mFbProducerSlot < 0)) {
Jesse Hall38efe862013-04-06 23:12:29 -0700196 // Last chance bailout if something bad happened earlier. For example,
197 // in a GLES configuration, if the sink disappears then dequeueBuffer
198 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
199 // will soldier on. So we end up here without a buffer. There should
200 // be lots of scary messages in the log just before this.
201 VDS_LOGE("advanceFrame: no buffer, bailing out");
202 return NO_MEMORY;
203 }
204
Jesse Hall14e8b012013-11-07 12:28:26 -0800205 sp<GraphicBuffer> fbBuffer = mFbProducerSlot >= 0 ?
206 mProducerBuffers[mFbProducerSlot] : sp<GraphicBuffer>(NULL);
Jesse Hall38efe862013-04-06 23:12:29 -0700207 sp<GraphicBuffer> outBuffer = mProducerBuffers[mOutputProducerSlot];
208 VDS_LOGV("advanceFrame: fb=%d(%p) out=%d(%p)",
209 mFbProducerSlot, fbBuffer.get(),
210 mOutputProducerSlot, outBuffer.get());
211
Jesse Hallb716e572013-10-01 17:25:20 -0700212 // At this point we know the output buffer acquire fence,
213 // so update HWC state with it.
214 mHwc.setOutputBuffer(mDisplayId, mOutputFence, outBuffer);
215
Jesse Hall14e8b012013-11-07 12:28:26 -0800216 status_t result = NO_ERROR;
217 if (fbBuffer != NULL) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800218 uint32_t hwcSlot = 0;
219 sp<GraphicBuffer> hwcBuffer;
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800220 mHwcBufferCache.getHwcBuffer(mFbProducerSlot, fbBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800221 &hwcSlot, &hwcBuffer);
222
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223 // TODO: Correctly propagate the dataspace from GL composition
Chia-I Wu06d63de2017-01-04 14:58:51 +0800224 result = mHwc.setClientTarget(mDisplayId, hwcSlot, mFbFence,
225 hwcBuffer, HAL_DATASPACE_UNKNOWN);
Jesse Hall14e8b012013-11-07 12:28:26 -0800226 }
227
228 return result;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700229}
230
Jesse Hall851cfe82013-03-20 13:44:00 -0700231void VirtualDisplaySurface::onFrameCommitted() {
Jesse Hall38efe862013-04-06 23:12:29 -0700232 if (mDisplayId < 0)
233 return;
234
235 VDS_LOGW_IF(mDbgState != DBG_STATE_HWC,
236 "Unexpected onFrameCommitted() in %s state", dbgStateStr());
237 mDbgState = DBG_STATE_IDLE;
238
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800239 sp<Fence> retireFence = mHwc.getPresentFence(mDisplayId);
Jesse Hall38efe862013-04-06 23:12:29 -0700240 if (mCompositionType == COMPOSITION_MIXED && mFbProducerSlot >= 0) {
241 // release the scratch buffer back to the pool
242 Mutex::Autolock lock(mMutex);
243 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, mFbProducerSlot);
244 VDS_LOGV("onFrameCommitted: release scratch sslot=%d", sslot);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245 addReleaseFenceLocked(sslot, mProducerBuffers[mFbProducerSlot],
246 retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700247 releaseBufferLocked(sslot, mProducerBuffers[mFbProducerSlot],
248 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
249 }
250
251 if (mOutputProducerSlot >= 0) {
252 int sslot = mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot);
253 QueueBufferOutput qbo;
Jesse Hall38efe862013-04-06 23:12:29 -0700254 VDS_LOGV("onFrameCommitted: queue sink sslot=%d", sslot);
Dan Stoza71433162014-02-04 16:22:36 -0800255 if (mMustRecompose) {
256 status_t result = mSource[SOURCE_SINK]->queueBuffer(sslot,
257 QueueBufferInput(
258 systemTime(), false /* isAutoTimestamp */,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800259 HAL_DATASPACE_UNKNOWN,
Dan Stoza71433162014-02-04 16:22:36 -0800260 Rect(mSinkBufferWidth, mSinkBufferHeight),
261 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0 /* transform */,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800262 retireFence),
Dan Stoza71433162014-02-04 16:22:36 -0800263 &qbo);
264 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700265 updateQueueBufferOutput(std::move(qbo));
Dan Stoza71433162014-02-04 16:22:36 -0800266 }
267 } else {
268 // If the surface hadn't actually been updated, then we only went
269 // through the motions of updating the display to keep our state
270 // machine happy. We cancel the buffer to avoid triggering another
271 // re-composition and causing an infinite loop.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 mSource[SOURCE_SINK]->cancelBuffer(sslot, retireFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700273 }
274 }
275
276 resetPerFrameState();
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700277}
278
Dan Stoza01049c82014-11-11 10:32:31 -0800279void VirtualDisplaySurface::dumpAsString(String8& /* result */) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700280}
281
Michael Lentine47e45402014-07-18 15:34:25 -0700282void VirtualDisplaySurface::resizeBuffers(const uint32_t w, const uint32_t h) {
Brian Andersonbaaad322016-07-22 15:55:13 -0700283 mQueueBufferOutput.width = w;
284 mQueueBufferOutput.height = h;
Michael Lentine47e45402014-07-18 15:34:25 -0700285 mSinkBufferWidth = w;
286 mSinkBufferHeight = h;
287}
288
Dan Stoza9e56aa02015-11-02 13:00:03 -0800289const sp<Fence>& VirtualDisplaySurface::getClientTargetAcquireFence() const {
290 return mFbFence;
291}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800292
Jesse Hall38efe862013-04-06 23:12:29 -0700293status_t VirtualDisplaySurface::requestBuffer(int pslot,
294 sp<GraphicBuffer>* outBuf) {
Michael Lentine47e45402014-07-18 15:34:25 -0700295 if (mDisplayId < 0)
296 return mSource[SOURCE_SINK]->requestBuffer(pslot, outBuf);
297
Jesse Hall38efe862013-04-06 23:12:29 -0700298 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
299 "Unexpected requestBuffer pslot=%d in %s state",
300 pslot, dbgStateStr());
301
302 *outBuf = mProducerBuffers[pslot];
303 return NO_ERROR;
304}
305
Pablo Ceballosfa455352015-08-12 17:47:47 -0700306status_t VirtualDisplaySurface::setMaxDequeuedBufferCount(
307 int maxDequeuedBuffers) {
308 return mSource[SOURCE_SINK]->setMaxDequeuedBufferCount(maxDequeuedBuffers);
309}
310
311status_t VirtualDisplaySurface::setAsyncMode(bool async) {
312 return mSource[SOURCE_SINK]->setAsyncMode(async);
313}
314
Jesse Hall38efe862013-04-06 23:12:29 -0700315status_t VirtualDisplaySurface::dequeueBuffer(Source source,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700316 PixelFormat format, uint64_t usage, int* sslot, sp<Fence>* fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700317 LOG_FATAL_IF(mDisplayId < 0, "mDisplayId=%d but should not be < 0.", mDisplayId);
Jesse Hall8db92552013-08-29 16:03:50 -0700318
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600319 status_t result =
320 mSource[source]->dequeueBuffer(sslot, fence, mSinkBufferWidth, mSinkBufferHeight,
321 format, usage, nullptr, nullptr);
Jesse Hall38efe862013-04-06 23:12:29 -0700322 if (result < 0)
323 return result;
324 int pslot = mapSource2ProducerSlot(source, *sslot);
325 VDS_LOGV("dequeueBuffer(%s): sslot=%d pslot=%d result=%d",
326 dbgSourceStr(source), *sslot, pslot, result);
Dan Stozafebd4f42014-04-09 16:14:51 -0700327 uint64_t sourceBit = static_cast<uint64_t>(source) << pslot;
Jesse Hall38efe862013-04-06 23:12:29 -0700328
Dan Stozafebd4f42014-04-09 16:14:51 -0700329 if ((mProducerSlotSource & (1ULL << pslot)) != sourceBit) {
Jesse Hall38efe862013-04-06 23:12:29 -0700330 // This slot was previously dequeued from the other source; must
331 // re-request the buffer.
332 result |= BUFFER_NEEDS_REALLOCATION;
Dan Stozafebd4f42014-04-09 16:14:51 -0700333 mProducerSlotSource &= ~(1ULL << pslot);
Jesse Hall38efe862013-04-06 23:12:29 -0700334 mProducerSlotSource |= sourceBit;
335 }
336
337 if (result & RELEASE_ALL_BUFFERS) {
338 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700339 if ((mProducerSlotSource & (1ULL << i)) == sourceBit)
Jesse Hall38efe862013-04-06 23:12:29 -0700340 mProducerBuffers[i].clear();
341 }
342 }
343 if (result & BUFFER_NEEDS_REALLOCATION) {
Jesse Hall0b63cd12014-04-29 16:14:14 -0700344 result = mSource[source]->requestBuffer(*sslot, &mProducerBuffers[pslot]);
345 if (result < 0) {
346 mProducerBuffers[pslot].clear();
347 mSource[source]->cancelBuffer(*sslot, *fence);
348 return result;
349 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700350 VDS_LOGV("dequeueBuffer(%s): buffers[%d]=%p fmt=%d usage=%#" PRIx64,
Jesse Hall497ba0e2013-11-04 16:43:03 -0800351 dbgSourceStr(source), pslot, mProducerBuffers[pslot].get(),
352 mProducerBuffers[pslot]->getPixelFormat(),
353 mProducerBuffers[pslot]->getUsage());
Jesse Hall38efe862013-04-06 23:12:29 -0700354 }
355
356 return result;
357}
358
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600359status_t VirtualDisplaySurface::dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
360 PixelFormat format, uint64_t usage,
361 uint64_t* outBufferAge,
362 FrameEventHistoryDelta* outTimestamps) {
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700363 if (mDisplayId < 0) {
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600364 return mSource[SOURCE_SINK]->dequeueBuffer(pslot, fence, w, h, format, usage, outBufferAge,
365 outTimestamps);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700366 }
Michael Lentine47e45402014-07-18 15:34:25 -0700367
Jesse Hall38efe862013-04-06 23:12:29 -0700368 VDS_LOGW_IF(mDbgState != DBG_STATE_PREPARED,
369 "Unexpected dequeueBuffer() in %s state", dbgStateStr());
370 mDbgState = DBG_STATE_GLES;
371
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700372 VDS_LOGV("dequeueBuffer %dx%d fmt=%d usage=%#" PRIx64, w, h, format, usage);
Jesse Hall38efe862013-04-06 23:12:29 -0700373
Jesse Hall028dc8f2013-08-20 16:35:32 -0700374 status_t result = NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700375 Source source = fbSourceForCompositionType(mCompositionType);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700376
Jesse Hall38efe862013-04-06 23:12:29 -0700377 if (source == SOURCE_SINK) {
Mathias Agopian6da15f42013-09-25 20:40:07 -0700378
379 if (mOutputProducerSlot < 0) {
380 // Last chance bailout if something bad happened earlier. For example,
381 // in a GLES configuration, if the sink disappears then dequeueBuffer
382 // will fail, the GLES driver won't queue a buffer, but SurfaceFlinger
383 // will soldier on. So we end up here without a buffer. There should
384 // be lots of scary messages in the log just before this.
385 VDS_LOGE("dequeueBuffer: no buffer, bailing out");
386 return NO_MEMORY;
387 }
388
Jesse Hall028dc8f2013-08-20 16:35:32 -0700389 // We already dequeued the output buffer. If the GLES driver wants
390 // something incompatible, we have to cancel and get a new one. This
391 // will mean that HWC will see a different output buffer between
392 // prepare and set, but since we're in GLES-only mode already it
393 // shouldn't matter.
394
Jesse Hall1e27ba22013-09-27 09:05:09 -0700395 usage |= GRALLOC_USAGE_HW_COMPOSER;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700396 const sp<GraphicBuffer>& buf = mProducerBuffers[mOutputProducerSlot];
Jesse Hall1e27ba22013-09-27 09:05:09 -0700397 if ((usage & ~buf->getUsage()) != 0 ||
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800398 (format != 0 && format != buf->getPixelFormat()) ||
Jesse Hall028dc8f2013-08-20 16:35:32 -0700399 (w != 0 && w != mSinkBufferWidth) ||
400 (h != 0 && h != mSinkBufferHeight)) {
Jesse Hall1e27ba22013-09-27 09:05:09 -0700401 VDS_LOGV("dequeueBuffer: dequeueing new output buffer: "
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700402 "want %dx%d fmt=%d use=%#" PRIx64 ", "
403 "have %dx%d fmt=%d use=%#" PRIx64,
Jesse Hall1e27ba22013-09-27 09:05:09 -0700404 w, h, format, usage,
405 mSinkBufferWidth, mSinkBufferHeight,
406 buf->getPixelFormat(), buf->getUsage());
407 mOutputFormat = format;
408 mOutputUsage = usage;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700409 result = refreshOutputBuffer();
410 if (result < 0)
411 return result;
412 }
Jesse Hall38efe862013-04-06 23:12:29 -0700413 }
414
Jesse Hall028dc8f2013-08-20 16:35:32 -0700415 if (source == SOURCE_SINK) {
416 *pslot = mOutputProducerSlot;
417 *fence = mOutputFence;
418 } else {
419 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700420 result = dequeueBuffer(source, format, usage, &sslot, fence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700421 if (result >= 0) {
422 *pslot = mapSource2ProducerSlot(source, sslot);
423 }
Jesse Hall38efe862013-04-06 23:12:29 -0700424 }
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600425 if (outBufferAge) {
426 *outBufferAge = 0;
427 }
Jesse Hall38efe862013-04-06 23:12:29 -0700428 return result;
429}
430
Dan Stoza88a459a2014-03-12 09:34:36 -0700431status_t VirtualDisplaySurface::detachBuffer(int /* slot */) {
432 VDS_LOGE("detachBuffer is not available for VirtualDisplaySurface");
433 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800434}
435
Dan Stozad9822a32014-03-28 15:25:31 -0700436status_t VirtualDisplaySurface::detachNextBuffer(
437 sp<GraphicBuffer>* /* outBuffer */, sp<Fence>* /* outFence */) {
438 VDS_LOGE("detachNextBuffer is not available for VirtualDisplaySurface");
439 return INVALID_OPERATION;
440}
441
Dan Stoza88a459a2014-03-12 09:34:36 -0700442status_t VirtualDisplaySurface::attachBuffer(int* /* outSlot */,
443 const sp<GraphicBuffer>& /* buffer */) {
444 VDS_LOGE("attachBuffer is not available for VirtualDisplaySurface");
445 return INVALID_OPERATION;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800446}
447
Jesse Hall38efe862013-04-06 23:12:29 -0700448status_t VirtualDisplaySurface::queueBuffer(int pslot,
449 const QueueBufferInput& input, QueueBufferOutput* output) {
Michael Lentine47e45402014-07-18 15:34:25 -0700450 if (mDisplayId < 0)
451 return mSource[SOURCE_SINK]->queueBuffer(pslot, input, output);
452
Jesse Hall38efe862013-04-06 23:12:29 -0700453 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
454 "Unexpected queueBuffer(pslot=%d) in %s state", pslot,
455 dbgStateStr());
456 mDbgState = DBG_STATE_GLES_DONE;
457
458 VDS_LOGV("queueBuffer pslot=%d", pslot);
459
460 status_t result;
461 if (mCompositionType == COMPOSITION_MIXED) {
462 // Queue the buffer back into the scratch pool
463 QueueBufferOutput scratchQBO;
464 int sslot = mapProducer2SourceSlot(SOURCE_SCRATCH, pslot);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700465 result = mSource[SOURCE_SCRATCH]->queueBuffer(sslot, input, &scratchQBO);
Jesse Hall38efe862013-04-06 23:12:29 -0700466 if (result != NO_ERROR)
467 return result;
468
469 // Now acquire the buffer from the scratch pool -- should be the same
470 // slot and fence as we just queued.
471 Mutex::Autolock lock(mMutex);
Dan Stoza11611f92015-03-12 15:12:44 -0700472 BufferItem item;
Jesse Hallbce76112013-07-16 13:46:20 -0700473 result = acquireBufferLocked(&item, 0);
Jesse Hall38efe862013-04-06 23:12:29 -0700474 if (result != NO_ERROR)
475 return result;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700476 VDS_LOGW_IF(item.mSlot != sslot,
Jesse Hall38efe862013-04-06 23:12:29 -0700477 "queueBuffer: acquired sslot %d from SCRATCH after queueing sslot %d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700478 item.mSlot, sslot);
479 mFbProducerSlot = mapSource2ProducerSlot(SOURCE_SCRATCH, item.mSlot);
480 mFbFence = mSlots[item.mSlot].mFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700481
482 } else {
483 LOG_FATAL_IF(mCompositionType != COMPOSITION_GLES,
484 "Unexpected queueBuffer in state %s for compositionType %s",
485 dbgStateStr(), dbgCompositionTypeStr(mCompositionType));
486
487 // Extract the GLES release fence for HWC to acquire
488 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700489 bool isAutoTimestamp;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800490 android_dataspace dataSpace;
Jesse Hall38efe862013-04-06 23:12:29 -0700491 Rect crop;
492 int scalingMode;
493 uint32_t transform;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800494 input.deflate(&timestamp, &isAutoTimestamp, &dataSpace, &crop,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700495 &scalingMode, &transform, &mFbFence);
Jesse Hall38efe862013-04-06 23:12:29 -0700496
497 mFbProducerSlot = pslot;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700498 mOutputFence = mFbFence;
Jesse Hall38efe862013-04-06 23:12:29 -0700499 }
500
Brian Anderson3d4039d2016-09-23 16:31:30 -0700501 // This moves the frame timestamps and keeps a copy of all other fields.
502 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700503 return NO_ERROR;
504}
505
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700506status_t VirtualDisplaySurface::cancelBuffer(int pslot,
507 const sp<Fence>& fence) {
Michael Lentine47e45402014-07-18 15:34:25 -0700508 if (mDisplayId < 0)
Michael Lentinefd9d1832014-07-30 15:39:17 -0700509 return mSource[SOURCE_SINK]->cancelBuffer(mapProducer2SourceSlot(SOURCE_SINK, pslot), fence);
Michael Lentine47e45402014-07-18 15:34:25 -0700510
Jesse Hall38efe862013-04-06 23:12:29 -0700511 VDS_LOGW_IF(mDbgState != DBG_STATE_GLES,
512 "Unexpected cancelBuffer(pslot=%d) in %s state", pslot,
513 dbgStateStr());
514 VDS_LOGV("cancelBuffer pslot=%d", pslot);
515 Source source = fbSourceForCompositionType(mCompositionType);
516 return mSource[source]->cancelBuffer(
517 mapProducer2SourceSlot(source, pslot), fence);
518}
519
520int VirtualDisplaySurface::query(int what, int* value) {
Michael Lentine47e45402014-07-18 15:34:25 -0700521 switch (what) {
522 case NATIVE_WINDOW_WIDTH:
523 *value = mSinkBufferWidth;
524 break;
525 case NATIVE_WINDOW_HEIGHT:
526 *value = mSinkBufferHeight;
527 break;
528 default:
529 return mSource[SOURCE_SINK]->query(what, value);
530 }
531 return NO_ERROR;
Jesse Hall38efe862013-04-06 23:12:29 -0700532}
533
Dan Stozaf0eaf252014-03-21 13:05:51 -0700534status_t VirtualDisplaySurface::connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700535 int api, bool producerControlledByApp,
Mathias Agopian595264f2013-07-16 22:56:09 -0700536 QueueBufferOutput* output) {
Jesse Hall38efe862013-04-06 23:12:29 -0700537 QueueBufferOutput qbo;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700538 status_t result = mSource[SOURCE_SINK]->connect(listener, api,
539 producerControlledByApp, &qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700540 if (result == NO_ERROR) {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700541 updateQueueBufferOutput(std::move(qbo));
542 // This moves the frame timestamps and keeps a copy of all other fields.
543 *output = std::move(mQueueBufferOutput);
Jesse Hall38efe862013-04-06 23:12:29 -0700544 }
545 return result;
546}
547
Robert Carr97b9c862016-09-08 13:54:35 -0700548status_t VirtualDisplaySurface::disconnect(int api, DisconnectMode mode) {
549 return mSource[SOURCE_SINK]->disconnect(api, mode);
Jesse Hall38efe862013-04-06 23:12:29 -0700550}
551
Jesse Hall399184a2014-03-03 15:42:54 -0800552status_t VirtualDisplaySurface::setSidebandStream(const sp<NativeHandle>& /*stream*/) {
553 return INVALID_OPERATION;
554}
555
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700556void VirtualDisplaySurface::allocateBuffers(uint32_t /* width */,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700557 uint32_t /* height */, PixelFormat /* format */, uint64_t /* usage */) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700558 // TODO: Should we actually allocate buffers for a virtual display?
559}
560
Dan Stoza9de72932015-04-16 17:28:43 -0700561status_t VirtualDisplaySurface::allowAllocation(bool /* allow */) {
562 return INVALID_OPERATION;
563}
564
Dan Stoza812ed062015-06-02 15:45:22 -0700565status_t VirtualDisplaySurface::setGenerationNumber(uint32_t /* generation */) {
566 ALOGE("setGenerationNumber not supported on VirtualDisplaySurface");
567 return INVALID_OPERATION;
568}
569
Dan Stozac6f30bd2015-06-08 09:32:50 -0700570String8 VirtualDisplaySurface::getConsumerName() const {
571 return String8("VirtualDisplaySurface");
572}
573
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700574status_t VirtualDisplaySurface::setSharedBufferMode(bool /*sharedBufferMode*/) {
575 ALOGE("setSharedBufferMode not supported on VirtualDisplaySurface");
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800576 return INVALID_OPERATION;
577}
578
579status_t VirtualDisplaySurface::setAutoRefresh(bool /*autoRefresh*/) {
580 ALOGE("setAutoRefresh not supported on VirtualDisplaySurface");
581 return INVALID_OPERATION;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700582}
583
Dan Stoza127fc632015-06-30 13:43:32 -0700584status_t VirtualDisplaySurface::setDequeueTimeout(nsecs_t /* timeout */) {
585 ALOGE("setDequeueTimeout not supported on VirtualDisplaySurface");
586 return INVALID_OPERATION;
587}
588
Dan Stoza50101d02016-04-07 16:53:23 -0700589status_t VirtualDisplaySurface::getLastQueuedBuffer(
John Reck1a61da52016-04-28 13:18:15 -0700590 sp<GraphicBuffer>* /*outBuffer*/, sp<Fence>* /*outFence*/,
591 float[16] /* outTransformMatrix*/) {
Dan Stoza50101d02016-04-07 16:53:23 -0700592 ALOGE("getLastQueuedBuffer not supported on VirtualDisplaySurface");
593 return INVALID_OPERATION;
594}
595
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700596status_t VirtualDisplaySurface::getUniqueId(uint64_t* /*outId*/) const {
597 ALOGE("getUniqueId not supported on VirtualDisplaySurface");
598 return INVALID_OPERATION;
599}
600
Chia-I Wue2786ea2017-08-07 10:36:08 -0700601status_t VirtualDisplaySurface::getConsumerUsage(uint64_t* outUsage) const {
602 return mSource[SOURCE_SINK]->getConsumerUsage(outUsage);
603}
604
Jesse Hall38efe862013-04-06 23:12:29 -0700605void VirtualDisplaySurface::updateQueueBufferOutput(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700606 QueueBufferOutput&& qbo) {
607 mQueueBufferOutput = std::move(qbo);
Brian Andersonbaaad322016-07-22 15:55:13 -0700608 mQueueBufferOutput.transformHint = 0;
Jesse Hall38efe862013-04-06 23:12:29 -0700609}
610
611void VirtualDisplaySurface::resetPerFrameState() {
612 mCompositionType = COMPOSITION_UNKNOWN;
mayank parsharb988f852014-01-10 10:27:45 +0530613 mFbFence = Fence::NO_FENCE;
Jesse Hall028dc8f2013-08-20 16:35:32 -0700614 mOutputFence = Fence::NO_FENCE;
Jesse Hall38efe862013-04-06 23:12:29 -0700615 mOutputProducerSlot = -1;
mayank parsharfdfde882014-01-23 15:08:31 +0530616 mFbProducerSlot = -1;
Jesse Hall38efe862013-04-06 23:12:29 -0700617}
618
Jesse Hall028dc8f2013-08-20 16:35:32 -0700619status_t VirtualDisplaySurface::refreshOutputBuffer() {
620 if (mOutputProducerSlot >= 0) {
621 mSource[SOURCE_SINK]->cancelBuffer(
622 mapProducer2SourceSlot(SOURCE_SINK, mOutputProducerSlot),
623 mOutputFence);
624 }
625
626 int sslot;
Jesse Hall1e27ba22013-09-27 09:05:09 -0700627 status_t result = dequeueBuffer(SOURCE_SINK, mOutputFormat, mOutputUsage,
628 &sslot, &mOutputFence);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700629 if (result < 0)
630 return result;
631 mOutputProducerSlot = mapSource2ProducerSlot(SOURCE_SINK, sslot);
632
Jesse Hallb716e572013-10-01 17:25:20 -0700633 // On GLES-only frames, we don't have the right output buffer acquire fence
634 // until after GLES calls queueBuffer(). So here we just set the buffer
635 // (for use in HWC prepare) but not the fence; we'll call this again with
636 // the proper fence once we have it.
637 result = mHwc.setOutputBuffer(mDisplayId, Fence::NO_FENCE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700638 mProducerBuffers[mOutputProducerSlot]);
639
640 return result;
641}
642
Jesse Hall38efe862013-04-06 23:12:29 -0700643// This slot mapping function is its own inverse, so two copies are unnecessary.
644// Both are kept to make the intent clear where the function is called, and for
645// the (unlikely) chance that we switch to a different mapping function.
646int VirtualDisplaySurface::mapSource2ProducerSlot(Source source, int sslot) {
647 if (source == SOURCE_SCRATCH) {
648 return BufferQueue::NUM_BUFFER_SLOTS - sslot - 1;
649 } else {
650 return sslot;
651 }
652}
653int VirtualDisplaySurface::mapProducer2SourceSlot(Source source, int pslot) {
654 return mapSource2ProducerSlot(source, pslot);
655}
656
657VirtualDisplaySurface::Source
658VirtualDisplaySurface::fbSourceForCompositionType(CompositionType type) {
659 return type == COMPOSITION_MIXED ? SOURCE_SCRATCH : SOURCE_SINK;
660}
661
662const char* VirtualDisplaySurface::dbgStateStr() const {
663 switch (mDbgState) {
664 case DBG_STATE_IDLE: return "IDLE";
665 case DBG_STATE_PREPARED: return "PREPARED";
666 case DBG_STATE_GLES: return "GLES";
667 case DBG_STATE_GLES_DONE: return "GLES_DONE";
668 case DBG_STATE_HWC: return "HWC";
669 default: return "INVALID";
670 }
671}
672
673const char* VirtualDisplaySurface::dbgSourceStr(Source s) {
674 switch (s) {
675 case SOURCE_SINK: return "SINK";
676 case SOURCE_SCRATCH: return "SCRATCH";
677 default: return "INVALID";
678 }
679}
680
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700681// ---------------------------------------------------------------------------
682} // namespace android
683// ---------------------------------------------------------------------------