VirtualDisplaySurface.h revision d9822a3843017444364899afc3c23fb5be6b9cb9
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// ---------------------------------------------------------------------------
26namespace android {
27// ---------------------------------------------------------------------------
28
29class HWComposer;
30class 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 */
71class VirtualDisplaySurface : public DisplaySurface,
72                              public BnGraphicBufferProducer,
73                              private ConsumerBase {
74public:
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    virtual status_t compositionComplete();
87    virtual status_t advanceFrame();
88    virtual void onFrameCommitted();
89    virtual void dump(String8& result) const;
90
91private:
92    enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
93
94    virtual ~VirtualDisplaySurface();
95
96    //
97    // IGraphicBufferProducer interface, used by the GLES driver.
98    //
99    virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
100    virtual status_t setBufferCount(int bufferCount);
101    virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
102            uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
103    virtual status_t detachBuffer(int slot);
104    virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
105            sp<Fence>* outFence);
106    virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
107    virtual status_t queueBuffer(int pslot,
108            const QueueBufferInput& input, QueueBufferOutput* output);
109    virtual void cancelBuffer(int pslot, const sp<Fence>& fence);
110    virtual int query(int what, int* value);
111    virtual status_t connect(const sp<IProducerListener>& listener,
112            int api, bool producerControlledByApp, QueueBufferOutput* output);
113    virtual status_t disconnect(int api);
114    virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
115
116    //
117    // Utility methods
118    //
119    static Source fbSourceForCompositionType(CompositionType type);
120    status_t dequeueBuffer(Source source, uint32_t format, uint32_t usage,
121            int* sslot, sp<Fence>* fence);
122    void updateQueueBufferOutput(const QueueBufferOutput& qbo);
123    void resetPerFrameState();
124    status_t refreshOutputBuffer();
125
126    // Both the sink and scratch buffer pools have their own set of slots
127    // ("source slots", or "sslot"). We have to merge these into the single
128    // set of slots used by the GLES producer ("producer slots" or "pslot") and
129    // internally in the VirtualDisplaySurface. To minimize the number of times
130    // a producer slot switches which source it comes from, we map source slot
131    // numbers to producer slot numbers differently for each source.
132    static int mapSource2ProducerSlot(Source source, int sslot);
133    static int mapProducer2SourceSlot(Source source, int pslot);
134
135    //
136    // Immutable after construction
137    //
138    HWComposer& mHwc;
139    const int32_t mDisplayId;
140    const String8 mDisplayName;
141    sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
142    uint32_t mDefaultOutputFormat;
143
144    //
145    // Inter-frame state
146    //
147
148    // To avoid buffer reallocations, we track the buffer usage and format
149    // we used on the previous frame and use it again on the new frame. If
150    // the composition type changes or the GLES driver starts requesting
151    // different usage/format, we'll get a new buffer.
152    uint32_t mOutputFormat;
153    uint32_t mOutputUsage;
154
155    // Since we present a single producer interface to the GLES driver, but
156    // are internally muxing between the sink and scratch producers, we have
157    // to keep track of which source last returned each producer slot from
158    // dequeueBuffer. Each bit in mLastSlotSource corresponds to a producer
159    // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
160    // "producer slot"; see the mapSlot*() functions.
161    uint32_t mProducerSlotSource;
162    sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS];
163
164    // The QueueBufferOutput with the latest info from the sink, and with the
165    // transform hint cleared. Since we defer queueBuffer from the GLES driver
166    // to the sink, we have to return the previous version.
167    QueueBufferOutput mQueueBufferOutput;
168
169    //
170    // Intra-frame state
171    //
172
173    // Composition type and GLES buffer source for the current frame.
174    // Valid after prepareFrame(), cleared in onFrameCommitted.
175    CompositionType mCompositionType;
176
177    // Details of the current sink buffer. These become valid when a buffer is
178    // dequeued from the sink, and are used when queueing the buffer.
179    uint32_t mSinkBufferWidth, mSinkBufferHeight;
180
181    // mFbFence is the fence HWC should wait for before reading the framebuffer
182    // target buffer.
183    sp<Fence> mFbFence;
184
185    // mOutputFence is the fence HWC should wait for before writing to the
186    // output buffer.
187    sp<Fence> mOutputFence;
188
189    // Producer slot numbers for the buffers to use for HWC framebuffer target
190    // and output.
191    int mFbProducerSlot;
192    int mOutputProducerSlot;
193
194    // Debug only -- track the sequence of events in each frame so we can make
195    // sure they happen in the order we expect. This class implicitly models
196    // a state machine; this enum/variable makes it explicit.
197    //
198    // +-----------+-------------------+-------------+
199    // | State     | Event             || Next State |
200    // +-----------+-------------------+-------------+
201    // | IDLE      | beginFrame        || BEGUN      |
202    // | BEGUN     | prepareFrame      || PREPARED   |
203    // | PREPARED  | dequeueBuffer [1] || GLES       |
204    // | PREPARED  | advanceFrame [2]  || HWC        |
205    // | GLES      | queueBuffer       || GLES_DONE  |
206    // | GLES_DONE | advanceFrame      || HWC        |
207    // | HWC       | onFrameCommitted  || IDLE       |
208    // +-----------+-------------------++------------+
209    // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
210    // [2] COMPOSITION_HWC frames.
211    //
212    enum DbgState {
213        // no buffer dequeued, don't know anything about the next frame
214        DBG_STATE_IDLE,
215        // output buffer dequeued, framebuffer source not yet known
216        DBG_STATE_BEGUN,
217        // output buffer dequeued, framebuffer source known but not provided
218        // to GLES yet.
219        DBG_STATE_PREPARED,
220        // GLES driver has a buffer dequeued
221        DBG_STATE_GLES,
222        // GLES driver has queued the buffer, we haven't sent it to HWC yet
223        DBG_STATE_GLES_DONE,
224        // HWC has the buffer for this frame
225        DBG_STATE_HWC,
226    };
227    DbgState mDbgState;
228    CompositionType mDbgLastCompositionType;
229
230    const char* dbgStateStr() const;
231    static const char* dbgSourceStr(Source s);
232
233    bool mMustRecompose;
234};
235
236// ---------------------------------------------------------------------------
237} // namespace android
238// ---------------------------------------------------------------------------
239
240#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
241
242