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