blob: 94b24d2e1f4f3c680dcb450ba4f121cf2f68bbe8 [file] [log] [blame]
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001/*
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
Jesse Hall38efe862013-04-06 23:12:29 -070020#include <gui/ConsumerBase.h>
21#include <gui/IGraphicBufferProducer.h>
22
Jesse Hall99c7dbb2013-03-14 14:29:29 -070023#include "DisplaySurface.h"
24
25// ---------------------------------------------------------------------------
26namespace android {
27// ---------------------------------------------------------------------------
28
29class HWComposer;
30
Jesse Hall38efe862013-04-06 23:12:29 -070031/* 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.
Jesse Hall80e0a392013-03-15 12:32:10 -070069 */
Jesse Hall38efe862013-04-06 23:12:29 -070070class VirtualDisplaySurface : public DisplaySurface,
71 private BnGraphicBufferProducer,
72 private ConsumerBase {
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073public:
Jesse Hallffe1f192013-03-22 15:13:48 -070074 VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070075 const sp<IGraphicBufferProducer>& sink,
76 const String8& name);
77
Jesse Hall38efe862013-04-06 23:12:29 -070078 //
79 // DisplaySurface interface
80 //
Jesse Hall99c7dbb2013-03-14 14:29:29 -070081 virtual sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
Jesse Hall38efe862013-04-06 23:12:29 -070082 virtual status_t prepareFrame(CompositionType compositionType);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070083 virtual status_t compositionComplete();
84 virtual status_t advanceFrame();
Jesse Hall851cfe82013-03-20 13:44:00 -070085 virtual void onFrameCommitted();
Jesse Hall99c7dbb2013-03-14 14:29:29 -070086 virtual void dump(String8& result) const;
87
88private:
Jesse Hall38efe862013-04-06 23:12:29 -070089 enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
90
Jesse Hall99c7dbb2013-03-14 14:29:29 -070091 virtual ~VirtualDisplaySurface();
92
Jesse Hall38efe862013-04-06 23:12:29 -070093 //
94 // IGraphicBufferProducer interface, used by the GLES driver.
95 //
96 virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
97 virtual status_t setBufferCount(int bufferCount);
98 virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence,
99 uint32_t w, uint32_t h, uint32_t format, uint32_t usage);
100 virtual status_t queueBuffer(int pslot,
101 const QueueBufferInput& input, QueueBufferOutput* output);
102 virtual void cancelBuffer(int pslot, const sp<Fence>& fence);
103 virtual int query(int what, int* value);
Mathias Agopian595264f2013-07-16 22:56:09 -0700104 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output);
Jesse Hall38efe862013-04-06 23:12:29 -0700105 virtual status_t disconnect(int api);
106
107 //
108 // Utility methods
109 //
110 static Source fbSourceForCompositionType(CompositionType type);
111 status_t dequeueBuffer(Source source, uint32_t format,
112 int* sslot, sp<Fence>* fence);
113 void updateQueueBufferOutput(const QueueBufferOutput& qbo);
114 void resetPerFrameState();
115
116 // Both the sink and scratch buffer pools have their own set of slots
117 // ("source slots", or "sslot"). We have to merge these into the single
118 // set of slots used by the GLES producer ("producer slots" or "pslot") and
119 // internally in the VirtualDisplaySurface. To minimize the number of times
120 // a producer slot switches which source it comes from, we map source slot
121 // numbers to producer slot numbers differently for each source.
122 static int mapSource2ProducerSlot(Source source, int sslot);
123 static int mapProducer2SourceSlot(Source source, int pslot);
124
125 //
126 // Immutable after construction
127 //
128 HWComposer& mHwc;
129 const int32_t mDisplayId;
130 const String8 mDisplayName;
131 sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
132
133 //
134 // Inter-frame state
135 //
136
137 // To avoid buffer reallocations, we track the buffer usage requested by
138 // the GLES driver in dequeueBuffer so we can use the same flags on
139 // HWC-only frames.
140 uint32_t mProducerUsage;
141
142 // Since we present a single producer interface to the GLES driver, but
143 // are internally muxing between the sink and scratch producers, we have
144 // to keep track of which source last returned each producer slot from
145 // dequeueBuffer. Each bit in mLastSlotSource corresponds to a producer
146 // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
147 // "producer slot"; see the mapSlot*() functions.
148 uint32_t mProducerSlotSource;
149 sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS];
150
151 // The QueueBufferOutput with the latest info from the sink, and with the
152 // transform hint cleared. Since we defer queueBuffer from the GLES driver
153 // to the sink, we have to return the previous version.
154 QueueBufferOutput mQueueBufferOutput;
155
156 //
157 // Intra-frame state
158 //
159
160 // Composition type and GLES buffer source for the current frame.
161 // Valid after prepareFrame(), cleared in onFrameCommitted.
162 CompositionType mCompositionType;
163
164 // Details of the current sink buffer. These become valid when a buffer is
165 // dequeued from the sink, and are used when queueing the buffer.
166 uint32_t mSinkBufferWidth, mSinkBufferHeight;
167
168 // mFbFence is the fence HWC should wait for before reading the framebuffer
169 // target buffer.
170 sp<Fence> mFbFence;
171
172 // Producer slot numbers for the buffers to use for HWC framebuffer target
173 // and output.
174 int mFbProducerSlot;
175 int mOutputProducerSlot;
176
177 // Debug only -- track the sequence of events in each frame so we can make
178 // sure they happen in the order we expect. This class implicitly models
179 // a state machine; this enum/variable makes it explicit.
180 //
181 // +-----------+-------------------+-------------+
182 // | State | Event || Next State |
183 // +-----------+-------------------+-------------+
184 // | IDLE | prepareFrame || PREPARED |
185 // | PREPARED | dequeueBuffer [1] || GLES |
186 // | PREPARED | advanceFrame [2] || HWC |
187 // | GLES | queueBuffer || GLES_DONE |
188 // | GLES_DONE | advanceFrame || HWC |
189 // | HWC | onFrameCommitted || IDLE |
190 // +-----------+-------------------++------------+
191 // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
192 // [2] COMPOSITION_HWC frames.
193 //
194 enum DbgState {
195 // no buffer dequeued, don't know anything about the next frame
196 DBG_STATE_IDLE,
197 // no buffer dequeued, but we know the buffer source for the frame
198 DBG_STATE_PREPARED,
199 // GLES driver has a buffer dequeued
200 DBG_STATE_GLES,
201 // GLES driver has queued the buffer, we haven't sent it to HWC yet
202 DBG_STATE_GLES_DONE,
203 // HWC has the buffer for this frame
204 DBG_STATE_HWC,
205 };
206 DbgState mDbgState;
207 CompositionType mDbgLastCompositionType;
208
209 const char* dbgStateStr() const;
210 static const char* dbgSourceStr(Source s);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700211};
212
213// ---------------------------------------------------------------------------
214} // namespace android
215// ---------------------------------------------------------------------------
216
217#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
218