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