blob: 3cbad8f8ae7bb4293e8046f6a1c38bbc9fca9bfb [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
17#ifndef ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
18#define ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
19
Dominik Laskowski075d3172018-05-24 15:50:06 -070020#include <optional>
Dominik Laskowskibf170d92018-04-19 15:08:05 -070021#include <string>
22
Lloyd Pique542307f2018-10-19 13:24:08 -070023#include <compositionengine/DisplaySurface.h>
Lloyd Pique76ed7032018-12-04 17:24:28 -080024#include <compositionengine/impl/HwcBufferCache.h>
Jesse Hall38efe862013-04-06 23:12:29 -070025#include <gui/ConsumerBase.h>
26#include <gui/IGraphicBufferProducer.h>
27
Lloyd Pique542307f2018-10-19 13:24:08 -070028#include "DisplayIdentification.h"
Lloyd Pique542307f2018-10-19 13:24:08 -070029
Jesse Hall99c7dbb2013-03-14 14:29:29 -070030// ---------------------------------------------------------------------------
31namespace android {
32// ---------------------------------------------------------------------------
33
34class HWComposer;
Dan Stozaf0eaf252014-03-21 13:05:51 -070035class IProducerListener;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070036
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080037/* This DisplaySurface implementation supports virtual displays, where GPU
Jesse Hall38efe862013-04-06 23:12:29 -070038 * and/or HWC compose into a buffer that is then passed to an arbitrary
39 * consumer (the sink) running in another process.
40 *
41 * The simplest case is when the virtual display will never use the h/w
42 * composer -- either the h/w composer doesn't support writing to buffers, or
43 * there are more virtual displays than it supports simultaneously. In this
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080044 * case, the GPU driver works directly with the output buffer queue, and
Jesse Hall38efe862013-04-06 23:12:29 -070045 * calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do
46 * nothing.
47 *
48 * If h/w composer might be used, then each frame will fall into one of three
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080049 * configurations: GPU-only, HWC-only, and MIXED composition. In all of these,
Jesse Hall38efe862013-04-06 23:12:29 -070050 * we must provide a FB target buffer and output buffer for the HWC set() call.
51 *
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080052 * In GPU-only composition, the GPU driver is given a buffer from the sink to
53 * render into. When the GPU driver queues the buffer to the
Jesse Hall38efe862013-04-06 23:12:29 -070054 * VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of
55 * immediately queueing it to the sink. The buffer is used as both the FB
56 * target and output buffer for HWC, though on these frames the HWC doesn't
57 * do any work for this display and doesn't write to the output buffer. After
58 * composition is complete, the buffer is queued to the sink.
59 *
60 * In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from
61 * the sink and passes it to HWC as both the FB target buffer and output
62 * buffer. The HWC doesn't need to read from the FB target buffer, but does
63 * write to the output buffer. After composition is complete, the buffer is
64 * queued to the sink.
65 *
66 * On MIXED frames, things become more complicated, since some h/w composer
67 * implementations can't read from and write to the same buffer. This class has
Peiyong Linf3ffc4e2019-12-13 00:46:24 -080068 * an internal BufferQueue that it uses as a scratch buffer pool. The GPU
Jesse Hall38efe862013-04-06 23:12:29 -070069 * driver is given a scratch buffer to render into. When it finishes rendering,
70 * the buffer is queued and then immediately acquired by the
71 * VirtualDisplaySurface. The scratch buffer is then used as the FB target
72 * buffer for HWC, and a separate buffer is dequeued from the sink and used as
73 * the HWC output buffer. When HWC composition is complete, the scratch buffer
74 * is released and the output buffer is queued to the sink.
Jesse Hall80e0a392013-03-15 12:32:10 -070075 */
Lloyd Pique542307f2018-10-19 13:24:08 -070076class VirtualDisplaySurface : public compositionengine::DisplaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070077 public BnGraphicBufferProducer,
Jesse Hall38efe862013-04-06 23:12:29 -070078 private ConsumerBase {
Jesse Hall99c7dbb2013-03-14 14:29:29 -070079public:
Dominik Laskowski075d3172018-05-24 15:50:06 -070080 VirtualDisplaySurface(HWComposer& hwc, const std::optional<DisplayId>& displayId,
81 const sp<IGraphicBufferProducer>& sink,
82 const sp<IGraphicBufferProducer>& bqProducer,
83 const sp<IGraphicBufferConsumer>& bqConsumer, const std::string& name);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070084
Jesse Hall38efe862013-04-06 23:12:29 -070085 //
86 // DisplaySurface interface
87 //
Dan Stoza71433162014-02-04 16:22:36 -080088 virtual status_t beginFrame(bool mustRecompose);
Jesse Hall38efe862013-04-06 23:12:29 -070089 virtual status_t prepareFrame(CompositionType compositionType);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070090 virtual status_t advanceFrame();
Jesse Hall851cfe82013-03-20 13:44:00 -070091 virtual void onFrameCommitted();
Dan Stoza01049c82014-11-11 10:32:31 -080092 virtual void dumpAsString(String8& result) const;
Michael Lentine47e45402014-07-18 15:34:25 -070093 virtual void resizeBuffers(const uint32_t w, const uint32_t h);
Dan Stoza9e56aa02015-11-02 13:00:03 -080094 virtual const sp<Fence>& getClientTargetAcquireFence() const override;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070095
96private:
Jesse Hall38efe862013-04-06 23:12:29 -070097 enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
98
Jesse Hall99c7dbb2013-03-14 14:29:29 -070099 virtual ~VirtualDisplaySurface();
100
Jesse Hall38efe862013-04-06 23:12:29 -0700101 //
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800102 // IGraphicBufferProducer interface, used by the GPU driver.
Jesse Hall38efe862013-04-06 23:12:29 -0700103 //
104 virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700105 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
106 virtual status_t setAsyncMode(bool async);
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600107 virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
108 PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
109 FrameEventHistoryDelta* outTimestamps);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800110 virtual status_t detachBuffer(int slot);
Dan Stozad9822a32014-03-28 15:25:31 -0700111 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
112 sp<Fence>* outFence);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800113 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
Jesse Hall38efe862013-04-06 23:12:29 -0700114 virtual status_t queueBuffer(int pslot,
115 const QueueBufferInput& input, QueueBufferOutput* output);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700116 virtual status_t cancelBuffer(int pslot, const sp<Fence>& fence);
Jesse Hall38efe862013-04-06 23:12:29 -0700117 virtual int query(int what, int* value);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700118 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700119 int api, bool producerControlledByApp, QueueBufferOutput* output);
Robert Carr97b9c862016-09-08 13:54:35 -0700120 virtual status_t disconnect(int api, DisconnectMode mode);
Jesse Hall399184a2014-03-03 15:42:54 -0800121 virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700122 virtual void allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700123 PixelFormat format, uint64_t usage);
Dan Stoza9de72932015-04-16 17:28:43 -0700124 virtual status_t allowAllocation(bool allow);
Dan Stoza812ed062015-06-02 15:45:22 -0700125 virtual status_t setGenerationNumber(uint32_t generationNumber);
Dan Stozac6f30bd2015-06-08 09:32:50 -0700126 virtual String8 getConsumerName() const override;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700127 virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800128 virtual status_t setAutoRefresh(bool autoRefresh) override;
Dan Stoza127fc632015-06-30 13:43:32 -0700129 virtual status_t setDequeueTimeout(nsecs_t timeout) override;
Dan Stoza50101d02016-04-07 16:53:23 -0700130 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700131 sp<Fence>* outFence, float outTransformMatrix[16]) override;
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700132 virtual status_t getUniqueId(uint64_t* outId) const override;
Chia-I Wue2786ea2017-08-07 10:36:08 -0700133 virtual status_t getConsumerUsage(uint64_t* outUsage) const override;
Jesse Hall38efe862013-04-06 23:12:29 -0700134
135 //
136 // Utility methods
137 //
138 static Source fbSourceForCompositionType(CompositionType type);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700139 status_t dequeueBuffer(Source source, PixelFormat format, uint64_t usage,
Jesse Hall8db92552013-08-29 16:03:50 -0700140 int* sslot, sp<Fence>* fence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700141 void updateQueueBufferOutput(QueueBufferOutput&& qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700142 void resetPerFrameState();
Jesse Hall028dc8f2013-08-20 16:35:32 -0700143 status_t refreshOutputBuffer();
Jesse Hall38efe862013-04-06 23:12:29 -0700144
145 // Both the sink and scratch buffer pools have their own set of slots
146 // ("source slots", or "sslot"). We have to merge these into the single
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800147 // set of slots used by the graphics producer ("producer slots" or "pslot") and
Jesse Hall38efe862013-04-06 23:12:29 -0700148 // internally in the VirtualDisplaySurface. To minimize the number of times
149 // a producer slot switches which source it comes from, we map source slot
150 // numbers to producer slot numbers differently for each source.
151 static int mapSource2ProducerSlot(Source source, int sslot);
152 static int mapProducer2SourceSlot(Source source, int pslot);
153
154 //
155 // Immutable after construction
156 //
157 HWComposer& mHwc;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700158 const std::optional<DisplayId> mDisplayId;
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700159 const std::string mDisplayName;
Jesse Hall38efe862013-04-06 23:12:29 -0700160 sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
Jesse Hall497ba0e2013-11-04 16:43:03 -0800161 uint32_t mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700162
163 //
164 // Inter-frame state
165 //
166
Jesse Hall1e27ba22013-09-27 09:05:09 -0700167 // To avoid buffer reallocations, we track the buffer usage and format
168 // we used on the previous frame and use it again on the new frame. If
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800169 // the composition type changes or the GPU driver starts requesting
Jesse Hall1e27ba22013-09-27 09:05:09 -0700170 // different usage/format, we'll get a new buffer.
171 uint32_t mOutputFormat;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700172 uint64_t mOutputUsage;
Jesse Hall38efe862013-04-06 23:12:29 -0700173
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800174 // Since we present a single producer interface to the GPU driver, but
Jesse Hall38efe862013-04-06 23:12:29 -0700175 // are internally muxing between the sink and scratch producers, we have
176 // to keep track of which source last returned each producer slot from
Dan Stozafebd4f42014-04-09 16:14:51 -0700177 // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
Jesse Hall38efe862013-04-06 23:12:29 -0700178 // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
179 // "producer slot"; see the mapSlot*() functions.
Dan Stozafebd4f42014-04-09 16:14:51 -0700180 uint64_t mProducerSlotSource;
Mathias Agopiana9347642017-02-13 16:42:28 -0800181 sp<GraphicBuffer> mProducerBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
Jesse Hall38efe862013-04-06 23:12:29 -0700182
183 // The QueueBufferOutput with the latest info from the sink, and with the
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800184 // transform hint cleared. Since we defer queueBuffer from the GPU driver
Jesse Hall38efe862013-04-06 23:12:29 -0700185 // to the sink, we have to return the previous version.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700186 // Moves instead of copies are performed to avoid duplicate
187 // FrameEventHistoryDeltas.
Jesse Hall38efe862013-04-06 23:12:29 -0700188 QueueBufferOutput mQueueBufferOutput;
189
Michael Lentine47e45402014-07-18 15:34:25 -0700190 // Details of the current sink buffer. These become valid when a buffer is
191 // dequeued from the sink, and are used when queueing the buffer.
192 uint32_t mSinkBufferWidth, mSinkBufferHeight;
193
Jesse Hall38efe862013-04-06 23:12:29 -0700194 //
195 // Intra-frame state
196 //
197
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800198 // Composition type and graphics buffer source for the current frame.
Jesse Hall38efe862013-04-06 23:12:29 -0700199 // Valid after prepareFrame(), cleared in onFrameCommitted.
200 CompositionType mCompositionType;
201
Jesse Hall38efe862013-04-06 23:12:29 -0700202 // mFbFence is the fence HWC should wait for before reading the framebuffer
203 // target buffer.
204 sp<Fence> mFbFence;
205
Jesse Hall028dc8f2013-08-20 16:35:32 -0700206 // mOutputFence is the fence HWC should wait for before writing to the
207 // output buffer.
208 sp<Fence> mOutputFence;
209
Jesse Hall38efe862013-04-06 23:12:29 -0700210 // Producer slot numbers for the buffers to use for HWC framebuffer target
211 // and output.
212 int mFbProducerSlot;
213 int mOutputProducerSlot;
214
215 // Debug only -- track the sequence of events in each frame so we can make
216 // sure they happen in the order we expect. This class implicitly models
217 // a state machine; this enum/variable makes it explicit.
218 //
219 // +-----------+-------------------+-------------+
220 // | State | Event || Next State |
221 // +-----------+-------------------+-------------+
Jesse Hall028dc8f2013-08-20 16:35:32 -0700222 // | IDLE | beginFrame || BEGUN |
223 // | BEGUN | prepareFrame || PREPARED |
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800224 // | PREPARED | dequeueBuffer [1] || GPU |
Jesse Hall38efe862013-04-06 23:12:29 -0700225 // | PREPARED | advanceFrame [2] || HWC |
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800226 // | GPU | queueBuffer || GPU_DONE |
227 // | GPU_DONE | advanceFrame || HWC |
Jesse Hall38efe862013-04-06 23:12:29 -0700228 // | HWC | onFrameCommitted || IDLE |
229 // +-----------+-------------------++------------+
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800230 // [1] COMPOSITION_GPU and COMPOSITION_MIXED frames.
Jesse Hall38efe862013-04-06 23:12:29 -0700231 // [2] COMPOSITION_HWC frames.
232 //
233 enum DbgState {
234 // no buffer dequeued, don't know anything about the next frame
235 DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700236 // output buffer dequeued, framebuffer source not yet known
237 DBG_STATE_BEGUN,
238 // output buffer dequeued, framebuffer source known but not provided
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800239 // to GPU yet.
Jesse Hall38efe862013-04-06 23:12:29 -0700240 DBG_STATE_PREPARED,
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800241 // GPU driver has a buffer dequeued
242 DBG_STATE_GPU,
243 // GPU driver has queued the buffer, we haven't sent it to HWC yet
244 DBG_STATE_GPU_DONE,
Jesse Hall38efe862013-04-06 23:12:29 -0700245 // HWC has the buffer for this frame
246 DBG_STATE_HWC,
247 };
248 DbgState mDbgState;
249 CompositionType mDbgLastCompositionType;
250
251 const char* dbgStateStr() const;
252 static const char* dbgSourceStr(Source s);
Dan Stoza71433162014-02-04 16:22:36 -0800253
254 bool mMustRecompose;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800255
Lloyd Pique76ed7032018-12-04 17:24:28 -0800256 compositionengine::impl::HwcBufferCache mHwcBufferCache;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700257
258 bool mForceHwcCopy;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700259};
260
261// ---------------------------------------------------------------------------
262} // namespace android
263// ---------------------------------------------------------------------------
264
265#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H