blob: 5c8aceae92d2b723cfc7f2acac8bef183fc754ad [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
Chia-I Wu06d63de2017-01-04 14:58:51 +080020#include "DisplaySurface.h"
Chia-I Wuaaff73f2017-02-13 12:28:24 -080021#include "HWComposerBufferCache.h"
Chia-I Wu06d63de2017-01-04 14:58:51 +080022
Jesse Hall38efe862013-04-06 23:12:29 -070023#include <gui/ConsumerBase.h>
24#include <gui/IGraphicBufferProducer.h>
25
Jesse Hall99c7dbb2013-03-14 14:29:29 -070026// ---------------------------------------------------------------------------
27namespace android {
28// ---------------------------------------------------------------------------
29
30class HWComposer;
Dan Stozaf0eaf252014-03-21 13:05:51 -070031class IProducerListener;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070032
Jesse Hall38efe862013-04-06 23:12:29 -070033/* This DisplaySurface implementation supports virtual displays, where GLES
34 * and/or HWC compose into a buffer that is then passed to an arbitrary
35 * consumer (the sink) running in another process.
36 *
37 * The simplest case is when the virtual display will never use the h/w
38 * composer -- either the h/w composer doesn't support writing to buffers, or
39 * there are more virtual displays than it supports simultaneously. In this
40 * case, the GLES driver works directly with the output buffer queue, and
41 * calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do
42 * nothing.
43 *
44 * If h/w composer might be used, then each frame will fall into one of three
45 * configurations: GLES-only, HWC-only, and MIXED composition. In all of these,
46 * we must provide a FB target buffer and output buffer for the HWC set() call.
47 *
48 * In GLES-only composition, the GLES driver is given a buffer from the sink to
49 * render into. When the GLES driver queues the buffer to the
50 * VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of
51 * immediately queueing it to the sink. The buffer is used as both the FB
52 * target and output buffer for HWC, though on these frames the HWC doesn't
53 * do any work for this display and doesn't write to the output buffer. After
54 * composition is complete, the buffer is queued to the sink.
55 *
56 * In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from
57 * the sink and passes it to HWC as both the FB target buffer and output
58 * buffer. The HWC doesn't need to read from the FB target buffer, but does
59 * write to the output buffer. After composition is complete, the buffer is
60 * queued to the sink.
61 *
62 * On MIXED frames, things become more complicated, since some h/w composer
63 * implementations can't read from and write to the same buffer. This class has
64 * an internal BufferQueue that it uses as a scratch buffer pool. The GLES
65 * driver is given a scratch buffer to render into. When it finishes rendering,
66 * the buffer is queued and then immediately acquired by the
67 * VirtualDisplaySurface. The scratch buffer is then used as the FB target
68 * buffer for HWC, and a separate buffer is dequeued from the sink and used as
69 * the HWC output buffer. When HWC composition is complete, the scratch buffer
70 * is released and the output buffer is queued to the sink.
Jesse Hall80e0a392013-03-15 12:32:10 -070071 */
Jesse Hall38efe862013-04-06 23:12:29 -070072class VirtualDisplaySurface : public DisplaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070073 public BnGraphicBufferProducer,
Jesse Hall38efe862013-04-06 23:12:29 -070074 private ConsumerBase {
Jesse Hall99c7dbb2013-03-14 14:29:29 -070075public:
Jesse Hallffe1f192013-03-22 15:13:48 -070076 VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070077 const sp<IGraphicBufferProducer>& sink,
Dan Stozab9b08832014-03-13 11:55:57 -070078 const sp<IGraphicBufferProducer>& bqProducer,
79 const sp<IGraphicBufferConsumer>& bqConsumer,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070080 const String8& name);
81
Jesse Hall38efe862013-04-06 23:12:29 -070082 //
83 // DisplaySurface interface
84 //
Dan Stoza71433162014-02-04 16:22:36 -080085 virtual status_t beginFrame(bool mustRecompose);
Jesse Hall38efe862013-04-06 23:12:29 -070086 virtual status_t prepareFrame(CompositionType compositionType);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070087 virtual status_t advanceFrame();
Jesse Hall851cfe82013-03-20 13:44:00 -070088 virtual void onFrameCommitted();
Dan Stoza01049c82014-11-11 10:32:31 -080089 virtual void dumpAsString(String8& result) const;
Michael Lentine47e45402014-07-18 15:34:25 -070090 virtual void resizeBuffers(const uint32_t w, const uint32_t h);
Dan Stoza9e56aa02015-11-02 13:00:03 -080091 virtual const sp<Fence>& getClientTargetAcquireFence() const override;
Jesse Hall99c7dbb2013-03-14 14:29:29 -070092
93private:
Jesse Hall38efe862013-04-06 23:12:29 -070094 enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
95
Jesse Hall99c7dbb2013-03-14 14:29:29 -070096 virtual ~VirtualDisplaySurface();
97
Jesse Hall38efe862013-04-06 23:12:29 -070098 //
99 // IGraphicBufferProducer interface, used by the GLES driver.
100 //
101 virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
Pablo Ceballosfa455352015-08-12 17:47:47 -0700102 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
103 virtual status_t setAsyncMode(bool async);
Ian Elliotta2eb34c2017-07-18 11:05:49 -0600104 virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, uint32_t h,
105 PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
106 FrameEventHistoryDelta* outTimestamps);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800107 virtual status_t detachBuffer(int slot);
Dan Stozad9822a32014-03-28 15:25:31 -0700108 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
109 sp<Fence>* outFence);
Dan Stoza9f3053d2014-03-06 15:14:33 -0800110 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
Jesse Hall38efe862013-04-06 23:12:29 -0700111 virtual status_t queueBuffer(int pslot,
112 const QueueBufferInput& input, QueueBufferOutput* output);
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700113 virtual status_t cancelBuffer(int pslot, const sp<Fence>& fence);
Jesse Hall38efe862013-04-06 23:12:29 -0700114 virtual int query(int what, int* value);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700115 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700116 int api, bool producerControlledByApp, QueueBufferOutput* output);
Robert Carr97b9c862016-09-08 13:54:35 -0700117 virtual status_t disconnect(int api, DisconnectMode mode);
Jesse Hall399184a2014-03-03 15:42:54 -0800118 virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700119 virtual void allocateBuffers(uint32_t width, uint32_t height,
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700120 PixelFormat format, uint64_t usage);
Dan Stoza9de72932015-04-16 17:28:43 -0700121 virtual status_t allowAllocation(bool allow);
Dan Stoza812ed062015-06-02 15:45:22 -0700122 virtual status_t setGenerationNumber(uint32_t generationNumber);
Dan Stozac6f30bd2015-06-08 09:32:50 -0700123 virtual String8 getConsumerName() const override;
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700124 virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800125 virtual status_t setAutoRefresh(bool autoRefresh) override;
Dan Stoza127fc632015-06-30 13:43:32 -0700126 virtual status_t setDequeueTimeout(nsecs_t timeout) override;
Dan Stoza50101d02016-04-07 16:53:23 -0700127 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700128 sp<Fence>* outFence, float outTransformMatrix[16]) override;
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700129 virtual status_t getUniqueId(uint64_t* outId) const override;
Chia-I Wue2786ea2017-08-07 10:36:08 -0700130 virtual status_t getConsumerUsage(uint64_t* outUsage) const override;
Jesse Hall38efe862013-04-06 23:12:29 -0700131
132 //
133 // Utility methods
134 //
135 static Source fbSourceForCompositionType(CompositionType type);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700136 status_t dequeueBuffer(Source source, PixelFormat format, uint64_t usage,
Jesse Hall8db92552013-08-29 16:03:50 -0700137 int* sslot, sp<Fence>* fence);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700138 void updateQueueBufferOutput(QueueBufferOutput&& qbo);
Jesse Hall38efe862013-04-06 23:12:29 -0700139 void resetPerFrameState();
Jesse Hall028dc8f2013-08-20 16:35:32 -0700140 status_t refreshOutputBuffer();
Jesse Hall38efe862013-04-06 23:12:29 -0700141
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_*
Jesse Hall497ba0e2013-11-04 16:43:03 -0800158 uint32_t mDefaultOutputFormat;
Jesse Hall38efe862013-04-06 23:12:29 -0700159
160 //
161 // Inter-frame state
162 //
163
Jesse Hall1e27ba22013-09-27 09:05:09 -0700164 // 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;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700169 uint64_t mOutputUsage;
Jesse Hall38efe862013-04-06 23:12:29 -0700170
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
Dan Stozafebd4f42014-04-09 16:14:51 -0700174 // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
Jesse Hall38efe862013-04-06 23:12:29 -0700175 // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
176 // "producer slot"; see the mapSlot*() functions.
Dan Stozafebd4f42014-04-09 16:14:51 -0700177 uint64_t mProducerSlotSource;
Mathias Agopiana9347642017-02-13 16:42:28 -0800178 sp<GraphicBuffer> mProducerBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
Jesse Hall38efe862013-04-06 23:12:29 -0700179
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.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700183 // Moves instead of copies are performed to avoid duplicate
184 // FrameEventHistoryDeltas.
Jesse Hall38efe862013-04-06 23:12:29 -0700185 QueueBufferOutput mQueueBufferOutput;
186
Michael Lentine47e45402014-07-18 15:34:25 -0700187 // Details of the current sink buffer. These become valid when a buffer is
188 // dequeued from the sink, and are used when queueing the buffer.
189 uint32_t mSinkBufferWidth, mSinkBufferHeight;
190
Jesse Hall38efe862013-04-06 23:12:29 -0700191 //
192 // Intra-frame state
193 //
194
195 // Composition type and GLES buffer source for the current frame.
196 // Valid after prepareFrame(), cleared in onFrameCommitted.
197 CompositionType mCompositionType;
198
Jesse Hall38efe862013-04-06 23:12:29 -0700199 // mFbFence is the fence HWC should wait for before reading the framebuffer
200 // target buffer.
201 sp<Fence> mFbFence;
202
Jesse Hall028dc8f2013-08-20 16:35:32 -0700203 // mOutputFence is the fence HWC should wait for before writing to the
204 // output buffer.
205 sp<Fence> mOutputFence;
206
Jesse Hall38efe862013-04-06 23:12:29 -0700207 // Producer slot numbers for the buffers to use for HWC framebuffer target
208 // and output.
209 int mFbProducerSlot;
210 int mOutputProducerSlot;
211
212 // Debug only -- track the sequence of events in each frame so we can make
213 // sure they happen in the order we expect. This class implicitly models
214 // a state machine; this enum/variable makes it explicit.
215 //
216 // +-----------+-------------------+-------------+
217 // | State | Event || Next State |
218 // +-----------+-------------------+-------------+
Jesse Hall028dc8f2013-08-20 16:35:32 -0700219 // | IDLE | beginFrame || BEGUN |
220 // | BEGUN | prepareFrame || PREPARED |
Jesse Hall38efe862013-04-06 23:12:29 -0700221 // | PREPARED | dequeueBuffer [1] || GLES |
222 // | PREPARED | advanceFrame [2] || HWC |
223 // | GLES | queueBuffer || GLES_DONE |
224 // | GLES_DONE | advanceFrame || HWC |
225 // | HWC | onFrameCommitted || IDLE |
226 // +-----------+-------------------++------------+
227 // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
228 // [2] COMPOSITION_HWC frames.
229 //
230 enum DbgState {
231 // no buffer dequeued, don't know anything about the next frame
232 DBG_STATE_IDLE,
Jesse Hall028dc8f2013-08-20 16:35:32 -0700233 // output buffer dequeued, framebuffer source not yet known
234 DBG_STATE_BEGUN,
235 // output buffer dequeued, framebuffer source known but not provided
236 // to GLES yet.
Jesse Hall38efe862013-04-06 23:12:29 -0700237 DBG_STATE_PREPARED,
238 // GLES driver has a buffer dequeued
239 DBG_STATE_GLES,
240 // GLES driver has queued the buffer, we haven't sent it to HWC yet
241 DBG_STATE_GLES_DONE,
242 // HWC has the buffer for this frame
243 DBG_STATE_HWC,
244 };
245 DbgState mDbgState;
246 CompositionType mDbgLastCompositionType;
247
248 const char* dbgStateStr() const;
249 static const char* dbgSourceStr(Source s);
Dan Stoza71433162014-02-04 16:22:36 -0800250
251 bool mMustRecompose;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800252
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800253 HWComposerBufferCache mHwcBufferCache;
Fabien Sanglarda34ed632017-03-14 11:43:52 -0700254
255 bool mForceHwcCopy;
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700256};
257
258// ---------------------------------------------------------------------------
259} // namespace android
260// ---------------------------------------------------------------------------
261
262#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H