blob: cfa4e4cb636bd0507ad5d42499fc7eed96b6f4c9 [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;
Dan Stozaf0eaf252014-03-21 13:05:51 -070030class IProducerListener;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070031
Jesse Hall38efe862013-04-06 23:12:29 -070032/* 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.
Jesse Hall80e0a392013-03-15 12:32:10 -070070 */
Jesse Hall38efe862013-04-06 23:12:29 -070071class VirtualDisplaySurface : public DisplaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070072 public BnGraphicBufferProducer,
Jesse Hall38efe862013-04-06 23:12:29 -070073 private ConsumerBase {
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074public:
Jesse Hallffe1f192013-03-22 15:13:48 -070075 VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070076 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070077 const sp<IGraphicBufferProducer>& bqProducer,
78 const sp<IGraphicBufferConsumer>& bqConsumer,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070079 const String8& name);
80
Jesse Hall38efe862013-04-06 23:12:29 -070081 //
82 // DisplaySurface interface
83 //
Dan Stoza71433162014-02-04 16:22:36 -080084 virtual status_t beginFrame(bool mustRecompose);
Jesse Hall38efe862013-04-06 23:12:29 -070085 virtual status_t prepareFrame(CompositionType compositionType);
Dan Stoza9e56aa02015-11-02 13:00:03 -080086#ifndef USE_HWC2
Jesse Hall99c7dbb2013-03-14 14:29:29 -070087 virtual status_t compositionComplete();
Dan Stoza9e56aa02015-11-02 13:00:03 -080088#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -070089 virtual status_t advanceFrame();
Jesse Hall851cfe82013-03-20 13:44:00 -070090 virtual void onFrameCommitted();
Dan Stoza01049c82014-11-11 10:32:31 -080091 virtual void dumpAsString(String8& result) const;
Michael Lentine47e45402014-07-18 15:34:25 -070092 virtual void resizeBuffers(const uint32_t w, const uint32_t h);
Dan Stoza9e56aa02015-11-02 13:00:03 -080093 virtual const sp<Fence>& getClientTargetAcquireFence() const override;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070094
95private:
Jesse Hall38efe862013-04-06 23:12:29 -070096 enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
97
Jesse Hall99c7dbb2013-03-14 14:29:29 -070098 virtual ~VirtualDisplaySurface();
99
Jesse Hall38efe862013-04-06 23:12:29 -0700100 //
101 // IGraphicBufferProducer interface, used by the GLES driver.
102 //
103 virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700104 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
105 virtual status_t setAsyncMode(bool async);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700106 virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w,
107 uint32_t h, PixelFormat format, uint32_t usage);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800108 virtual status_t detachBuffer(int slot);
Dan Stozad9822a32014-03-28 15:25:31 -0700109 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
110 sp<Fence>* outFence);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800111 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
Jesse Hall38efe862013-04-06 23:12:29 -0700112 virtual status_t queueBuffer(int pslot,
113 const QueueBufferInput& input, QueueBufferOutput* output);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700114 virtual status_t cancelBuffer(int pslot, const sp<Fence>& fence);
Jesse Hall38efe862013-04-06 23:12:29 -0700115 virtual int query(int what, int* value);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700116 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700117 int api, bool producerControlledByApp, QueueBufferOutput* output);
Jesse Hall38efe862013-04-06 23:12:29 -0700118 virtual status_t disconnect(int api);
Jesse Hall399184a2014-03-03 15:42:54 -0800119 virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700120 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800121 PixelFormat format, uint32_t usage);
Dan Stoza9de72932015-04-16 17:28:43 -0700122 virtual status_t allowAllocation(bool allow);
Dan Stoza812ed062015-06-02 15:45:22 -0700123 virtual status_t setGenerationNumber(uint32_t generationNumber);
Dan Stozac6f30bd2015-06-08 09:32:50 -0700124 virtual String8 getConsumerName() const override;
Dan Stoza7dde5992015-05-22 09:51:44 -0700125 virtual uint64_t getNextFrameNumber() const override;
Dan Stoza127fc632015-06-30 13:43:32 -0700126 virtual status_t setSingleBufferMode(bool singleBufferMode) override;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800127 virtual status_t setAutoRefresh(bool autoRefresh) override;
Dan Stoza127fc632015-06-30 13:43:32 -0700128 virtual status_t setDequeueTimeout(nsecs_t timeout) override;
Jesse Hall38efe862013-04-06 23:12:29 -0700129
130 //
131 // Utility methods
132 //
133 static Source fbSourceForCompositionType(CompositionType type);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800134 status_t dequeueBuffer(Source source, PixelFormat format, uint32_t usage,
Jesse Hall8db92552013-08-29 16:03:50 -0700135 int* sslot, sp<Fence>* fence);
Jesse Hall38efe862013-04-06 23:12:29 -0700136 void updateQueueBufferOutput(const QueueBufferOutput& qbo);
137 void resetPerFrameState();
Jesse Hall028dc8f2013-08-20 16:35:32 -0700138 status_t refreshOutputBuffer();
Jesse Hall38efe862013-04-06 23:12:29 -0700139
140 // Both the sink and scratch buffer pools have their own set of slots
141 // ("source slots", or "sslot"). We have to merge these into the single
142 // set of slots used by the GLES producer ("producer slots" or "pslot") and
143 // internally in the VirtualDisplaySurface. To minimize the number of times
144 // a producer slot switches which source it comes from, we map source slot
145 // numbers to producer slot numbers differently for each source.
146 static int mapSource2ProducerSlot(Source source, int sslot);
147 static int mapProducer2SourceSlot(Source source, int pslot);
148
149 //
150 // Immutable after construction
151 //
152 HWComposer& mHwc;
153 const int32_t mDisplayId;
154 const String8 mDisplayName;
155 sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
Jesse Hall497ba0e2013-11-04 16:43:03 -0800156 uint32_t mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700157
158 //
159 // Inter-frame state
160 //
161
Jesse Hall1e27ba22013-09-27 09:05:09 -0700162 // To avoid buffer reallocations, we track the buffer usage and format
163 // we used on the previous frame and use it again on the new frame. If
164 // the composition type changes or the GLES driver starts requesting
165 // different usage/format, we'll get a new buffer.
166 uint32_t mOutputFormat;
167 uint32_t mOutputUsage;
Jesse Hall38efe862013-04-06 23:12:29 -0700168
169 // Since we present a single producer interface to the GLES driver, but
170 // are internally muxing between the sink and scratch producers, we have
171 // to keep track of which source last returned each producer slot from
Dan Stozafebd4f42014-04-09 16:14:51 -0700172 // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
Jesse Hall38efe862013-04-06 23:12:29 -0700173 // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
174 // "producer slot"; see the mapSlot*() functions.
Dan Stozafebd4f42014-04-09 16:14:51 -0700175 uint64_t mProducerSlotSource;
Jesse Hall38efe862013-04-06 23:12:29 -0700176 sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS];
177
178 // The QueueBufferOutput with the latest info from the sink, and with the
179 // transform hint cleared. Since we defer queueBuffer from the GLES driver
180 // to the sink, we have to return the previous version.
181 QueueBufferOutput mQueueBufferOutput;
182
Michael Lentine47e45402014-07-18 15:34:25 -0700183 // Details of the current sink buffer. These become valid when a buffer is
184 // dequeued from the sink, and are used when queueing the buffer.
185 uint32_t mSinkBufferWidth, mSinkBufferHeight;
186
Jesse Hall38efe862013-04-06 23:12:29 -0700187 //
188 // Intra-frame state
189 //
190
191 // Composition type and GLES buffer source for the current frame.
192 // Valid after prepareFrame(), cleared in onFrameCommitted.
193 CompositionType mCompositionType;
194
Jesse Hall38efe862013-04-06 23:12:29 -0700195 // mFbFence is the fence HWC should wait for before reading the framebuffer
196 // target buffer.
197 sp<Fence> mFbFence;
198
Jesse Hall028dc8f2013-08-20 16:35:32 -0700199 // mOutputFence is the fence HWC should wait for before writing to the
200 // output buffer.
201 sp<Fence> mOutputFence;
202
Jesse Hall38efe862013-04-06 23:12:29 -0700203 // Producer slot numbers for the buffers to use for HWC framebuffer target
204 // and output.
205 int mFbProducerSlot;
206 int mOutputProducerSlot;
207
208 // Debug only -- track the sequence of events in each frame so we can make
209 // sure they happen in the order we expect. This class implicitly models
210 // a state machine; this enum/variable makes it explicit.
211 //
212 // +-----------+-------------------+-------------+
213 // | State | Event || Next State |
214 // +-----------+-------------------+-------------+
Jesse Hall028dc8f2013-08-20 16:35:32 -0700215 // | IDLE | beginFrame || BEGUN |
216 // | BEGUN | prepareFrame || PREPARED |
Jesse Hall38efe862013-04-06 23:12:29 -0700217 // | PREPARED | dequeueBuffer [1] || GLES |
218 // | PREPARED | advanceFrame [2] || HWC |
219 // | GLES | queueBuffer || GLES_DONE |
220 // | GLES_DONE | advanceFrame || HWC |
221 // | HWC | onFrameCommitted || IDLE |
222 // +-----------+-------------------++------------+
223 // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
224 // [2] COMPOSITION_HWC frames.
225 //
226 enum DbgState {
227 // no buffer dequeued, don't know anything about the next frame
228 DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700229 // output buffer dequeued, framebuffer source not yet known
230 DBG_STATE_BEGUN,
231 // output buffer dequeued, framebuffer source known but not provided
232 // to GLES yet.
Jesse Hall38efe862013-04-06 23:12:29 -0700233 DBG_STATE_PREPARED,
234 // GLES driver has a buffer dequeued
235 DBG_STATE_GLES,
236 // GLES driver has queued the buffer, we haven't sent it to HWC yet
237 DBG_STATE_GLES_DONE,
238 // HWC has the buffer for this frame
239 DBG_STATE_HWC,
240 };
241 DbgState mDbgState;
242 CompositionType mDbgLastCompositionType;
243
244 const char* dbgStateStr() const;
245 static const char* dbgSourceStr(Source s);
Dan Stoza71433162014-02-04 16:22:36 -0800246
247 bool mMustRecompose;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700248};
249
250// ---------------------------------------------------------------------------
251} // namespace android
252// ---------------------------------------------------------------------------
253
254#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
255