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