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