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