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