1 /*
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 
20 #include <optional>
21 #include <string>
22 
23 #include <compositionengine/DisplaySurface.h>
24 #include <compositionengine/impl/HwcBufferCache.h>
25 #include <gui/ConsumerBase.h>
26 #include <gui/IGraphicBufferProducer.h>
27 #include <ui/DisplayId.h>
28 
29 #include "DisplayIdentification.h"
30 
31 // ---------------------------------------------------------------------------
32 namespace android {
33 // ---------------------------------------------------------------------------
34 
35 class HWComposer;
36 class IProducerListener;
37 
38 /* This DisplaySurface implementation supports virtual displays, where GPU
39  * and/or HWC compose into a buffer that is then passed to an arbitrary
40  * consumer (the sink) running in another process.
41  *
42  * The simplest case is when the virtual display will never use the h/w
43  * composer -- either the h/w composer doesn't support writing to buffers, or
44  * there are more virtual displays than it supports simultaneously. In this
45  * case, the GPU driver works directly with the output buffer queue, and
46  * calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do
47  * nothing.
48  *
49  * If h/w composer might be used, then each frame will fall into one of three
50  * configurations: GPU-only, HWC-only, and MIXED composition. In all of these,
51  * we must provide a FB target buffer and output buffer for the HWC set() call.
52  *
53  * In GPU-only composition, the GPU driver is given a buffer from the sink to
54  * render into. When the GPU driver queues the buffer to the
55  * VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of
56  * immediately queueing it to the sink. The buffer is used as both the FB
57  * target and output buffer for HWC, though on these frames the HWC doesn't
58  * do any work for this display and doesn't write to the output buffer. After
59  * composition is complete, the buffer is queued to the sink.
60  *
61  * In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from
62  * the sink and passes it to HWC as both the FB target buffer and output
63  * buffer. The HWC doesn't need to read from the FB target buffer, but does
64  * write to the output buffer. After composition is complete, the buffer is
65  * queued to the sink.
66  *
67  * On MIXED frames, things become more complicated, since some h/w composer
68  * implementations can't read from and write to the same buffer. This class has
69  * an internal BufferQueue that it uses as a scratch buffer pool. The GPU
70  * driver is given a scratch buffer to render into. When it finishes rendering,
71  * the buffer is queued and then immediately acquired by the
72  * VirtualDisplaySurface. The scratch buffer is then used as the FB target
73  * buffer for HWC, and a separate buffer is dequeued from the sink and used as
74  * the HWC output buffer. When HWC composition is complete, the scratch buffer
75  * is released and the output buffer is queued to the sink.
76  */
77 class VirtualDisplaySurface : public compositionengine::DisplaySurface,
78                               public BnGraphicBufferProducer,
79                               private ConsumerBase {
80 public:
81     VirtualDisplaySurface(HWComposer&, VirtualDisplayId, const sp<IGraphicBufferProducer>& sink,
82                           const sp<IGraphicBufferProducer>& bqProducer,
83                           const sp<IGraphicBufferConsumer>& bqConsumer, const std::string& name);
84 
85     //
86     // DisplaySurface interface
87     //
88     virtual status_t beginFrame(bool mustRecompose);
89     virtual status_t prepareFrame(CompositionType);
90     virtual status_t advanceFrame();
91     virtual void onFrameCommitted();
92     virtual void dumpAsString(String8& result) const;
93     virtual void resizeBuffers(const ui::Size&) override;
94     virtual const sp<Fence>& getClientTargetAcquireFence() const override;
95 
96 private:
97     enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
98 
99     virtual ~VirtualDisplaySurface();
100 
101     //
102     // IGraphicBufferProducer interface, used by the GPU driver.
103     //
104     virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
105     virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
106     virtual status_t setAsyncMode(bool async);
107     virtual status_t dequeueBuffer(int* pslot, sp<Fence>*, uint32_t w, uint32_t h, PixelFormat,
108                                    uint64_t usage, uint64_t* outBufferAge,
109                                    FrameEventHistoryDelta* outTimestamps);
110     virtual status_t detachBuffer(int slot);
111     virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence);
112     virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>&);
113     virtual status_t queueBuffer(int pslot, const QueueBufferInput&, QueueBufferOutput*);
114     virtual status_t cancelBuffer(int pslot, const sp<Fence>&);
115     virtual int query(int what, int* value);
116     virtual status_t connect(const sp<IProducerListener>&, int api, bool producerControlledByApp,
117                              QueueBufferOutput*);
118     virtual status_t disconnect(int api, DisconnectMode);
119     virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
120     virtual void allocateBuffers(uint32_t width, uint32_t height, PixelFormat, uint64_t usage);
121     virtual status_t allowAllocation(bool allow);
122     virtual status_t setGenerationNumber(uint32_t);
123     virtual String8 getConsumerName() const override;
124     virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
125     virtual status_t setAutoRefresh(bool autoRefresh) override;
126     virtual status_t setDequeueTimeout(nsecs_t timeout) override;
127     virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
128             sp<Fence>* outFence, float outTransformMatrix[16]) override;
129     virtual status_t getUniqueId(uint64_t* outId) const override;
130     virtual status_t getConsumerUsage(uint64_t* outUsage) const override;
131 
132     //
133     // Utility methods
134     //
135     static Source fbSourceForCompositionType(CompositionType);
136     status_t dequeueBuffer(Source, PixelFormat, uint64_t usage, int* sslot, sp<Fence>*);
137     void updateQueueBufferOutput(QueueBufferOutput&&);
138     void resetPerFrameState();
139     status_t refreshOutputBuffer();
140 
141     // Both the sink and scratch buffer pools have their own set of slots
142     // ("source slots", or "sslot"). We have to merge these into the single
143     // set of slots used by the graphics producer ("producer slots" or "pslot") and
144     // internally in the VirtualDisplaySurface. To minimize the number of times
145     // a producer slot switches which source it comes from, we map source slot
146     // numbers to producer slot numbers differently for each source.
147     static int mapSource2ProducerSlot(Source, int sslot);
148     static int mapProducer2SourceSlot(Source, int pslot);
149 
150     //
151     // Immutable after construction
152     //
153     HWComposer& mHwc;
154     const VirtualDisplayId mDisplayId;
155     const std::string mDisplayName;
156     sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
157     uint32_t mDefaultOutputFormat;
158 
159     //
160     // Inter-frame state
161     //
162 
163     // To avoid buffer reallocations, we track the buffer usage and format
164     // we used on the previous frame and use it again on the new frame. If
165     // the composition type changes or the GPU driver starts requesting
166     // different usage/format, we'll get a new buffer.
167     uint32_t mOutputFormat;
168     uint64_t mOutputUsage;
169 
170     // Since we present a single producer interface to the GPU driver, but
171     // are internally muxing between the sink and scratch producers, we have
172     // to keep track of which source last returned each producer slot from
173     // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
174     // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
175     // "producer slot"; see the mapSlot*() functions.
176     uint64_t mProducerSlotSource;
177     sp<GraphicBuffer> mProducerBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
178 
179     // Need to propagate reallocation to VDS consumer.
180     // Each bit corresponds to a producer slot.
181     uint64_t mProducerSlotNeedReallocation;
182 
183     // The QueueBufferOutput with the latest info from the sink, and with the
184     // transform hint cleared. Since we defer queueBuffer from the GPU driver
185     // to the sink, we have to return the previous version.
186     // Moves instead of copies are performed to avoid duplicate
187     // FrameEventHistoryDeltas.
188     QueueBufferOutput mQueueBufferOutput;
189 
190     // 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 
194     //
195     // Intra-frame state
196     //
197 
198     // Composition type and graphics buffer source for the current frame.
199     // Valid after prepareFrame(), cleared in onFrameCommitted.
200     CompositionType mCompositionType;
201 
202     // mFbFence is the fence HWC should wait for before reading the framebuffer
203     // target buffer.
204     sp<Fence> mFbFence;
205 
206     // mOutputFence is the fence HWC should wait for before writing to the
207     // output buffer.
208     sp<Fence> mOutputFence;
209 
210     // 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     // +-----------+-------------------+-------------+
222     // | IDLE      | beginFrame        || BEGUN      |
223     // | BEGUN     | prepareFrame      || PREPARED   |
224     // | PREPARED  | dequeueBuffer [1] || GPU        |
225     // | PREPARED  | advanceFrame [2]  || HWC        |
226     // | GPU       | queueBuffer       || GPU_DONE   |
227     // | GPU_DONE  | advanceFrame      || HWC        |
228     // | HWC       | onFrameCommitted  || IDLE       |
229     // +-----------+-------------------++------------+
230     // [1] COMPOSITION_GPU and COMPOSITION_MIXED frames.
231     // [2] COMPOSITION_HWC frames.
232     //
233     enum DbgState {
234         // no buffer dequeued, don't know anything about the next frame
235         DBG_STATE_IDLE,
236         // output buffer dequeued, framebuffer source not yet known
237         DBG_STATE_BEGUN,
238         // output buffer dequeued, framebuffer source known but not provided
239         // to GPU yet.
240         DBG_STATE_PREPARED,
241         // 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,
245         // 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);
253 
254     bool mMustRecompose;
255 
256     compositionengine::impl::HwcBufferCache mHwcBufferCache;
257 
258     bool mForceHwcCopy;
259 };
260 
261 // ---------------------------------------------------------------------------
262 } // namespace android
263 // ---------------------------------------------------------------------------
264 
265 #endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
266