1 /* 2 * Copyright 2019 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 <cstdint> 20 #include <iterator> 21 #include <optional> 22 #include <string> 23 #include <type_traits> 24 #include <unordered_map> 25 #include <utility> 26 27 #include <compositionengine/LayerFE.h> 28 #include <renderengine/LayerSettings.h> 29 #include <ui/Fence.h> 30 #include <ui/GraphicTypes.h> 31 #include <ui/Region.h> 32 #include <ui/Transform.h> 33 #include <utils/StrongPointer.h> 34 35 #include "DisplayHardware/DisplayIdentification.h" 36 37 namespace android { 38 39 namespace HWC2 { 40 class Layer; 41 } // namespace HWC2 42 43 namespace compositionengine { 44 45 class DisplayColorProfile; 46 class LayerFE; 47 class RenderSurface; 48 class OutputLayer; 49 50 struct CompositionRefreshArgs; 51 struct LayerFECompositionState; 52 53 namespace impl { 54 struct OutputCompositionState; 55 } // namespace impl 56 57 /** 58 * Encapsulates all the state involved with composing layers for an output 59 */ 60 class Output { 61 public: 62 using ReleasedLayers = std::vector<wp<LayerFE>>; 63 using UniqueFELayerStateMap = std::unordered_map<LayerFE*, LayerFECompositionState*>; 64 65 // A helper class for enumerating the output layers using a C++11 ranged-based for loop 66 template <typename T> 67 class OutputLayersEnumerator { 68 public: 69 // TODO(lpique): Consider turning this into a C++20 view when possible. 70 template <bool IsConstIter> 71 class IteratorImpl { 72 public: 73 // Required definitions to be considered an iterator 74 using iterator_category = std::forward_iterator_tag; 75 using value_type = decltype(std::declval<T>().getOutputLayerOrderedByZByIndex(0)); 76 using difference_type = std::ptrdiff_t; 77 using pointer = std::conditional_t<IsConstIter, const value_type*, value_type*>; 78 using reference = std::conditional_t<IsConstIter, const value_type&, value_type&>; 79 80 IteratorImpl() = default; IteratorImpl(const T * output,size_t index)81 IteratorImpl(const T* output, size_t index) : mOutput(output), mIndex(index) {} 82 83 value_type operator*() const { 84 return mOutput->getOutputLayerOrderedByZByIndex(mIndex); 85 } 86 value_type operator->() const { 87 return mOutput->getOutputLayerOrderedByZByIndex(mIndex); 88 } 89 90 bool operator==(const IteratorImpl& other) const { 91 return mOutput == other.mOutput && mIndex == other.mIndex; 92 } 93 bool operator!=(const IteratorImpl& other) const { return !operator==(other); } 94 95 IteratorImpl& operator++() { 96 ++mIndex; 97 return *this; 98 } 99 IteratorImpl operator++(int) { 100 auto prev = *this; 101 ++mIndex; 102 return prev; 103 } 104 105 private: 106 const T* mOutput{nullptr}; 107 size_t mIndex{0}; 108 }; 109 110 using iterator = IteratorImpl<false>; 111 using const_iterator = IteratorImpl<true>; 112 OutputLayersEnumerator(const T & output)113 explicit OutputLayersEnumerator(const T& output) : mOutput(output) {} begin()114 auto begin() const { return iterator(&mOutput, 0); } end()115 auto end() const { return iterator(&mOutput, mOutput.getOutputLayerCount()); } cbegin()116 auto cbegin() const { return const_iterator(&mOutput, 0); } cend()117 auto cend() const { return const_iterator(&mOutput, mOutput.getOutputLayerCount()); } 118 119 private: 120 const T& mOutput; 121 }; 122 123 struct FrameFences { 124 sp<Fence> presentFence{Fence::NO_FENCE}; 125 sp<Fence> clientTargetAcquireFence{Fence::NO_FENCE}; 126 std::unordered_map<HWC2::Layer*, sp<Fence>> layerFences; 127 }; 128 129 struct ColorProfile { 130 ui::ColorMode mode{ui::ColorMode::NATIVE}; 131 ui::Dataspace dataspace{ui::Dataspace::UNKNOWN}; 132 ui::RenderIntent renderIntent{ui::RenderIntent::COLORIMETRIC}; 133 ui::Dataspace colorSpaceAgnosticDataspace{ui::Dataspace::UNKNOWN}; 134 }; 135 136 // Use internally to incrementally compute visibility/coverage 137 struct CoverageState { CoverageStateCoverageState138 explicit CoverageState(LayerFESet& latchedLayers) : latchedLayers(latchedLayers) {} 139 140 // The set of layers that had been latched for the coverage calls, to 141 // avoid duplicate requests to obtain the same front-end layer state. 142 LayerFESet& latchedLayers; 143 144 // The region of the output which is covered by layers 145 Region aboveCoveredLayers; 146 // The region of the output which is opaquely covered by layers 147 Region aboveOpaqueLayers; 148 // The region of the output which should be considered dirty 149 Region dirtyRegion; 150 }; 151 152 virtual ~Output(); 153 154 // Returns true if the output is valid. This is meant to be checked post- 155 // construction and prior to use, as not everything is set up by the 156 // constructor. 157 virtual bool isValid() const = 0; 158 159 // Returns the DisplayId the output represents, if it has one 160 virtual std::optional<DisplayId> getDisplayId() const = 0; 161 162 // Enables (or disables) composition on this output 163 virtual void setCompositionEnabled(bool) = 0; 164 165 // Sets the projection state to use 166 virtual void setProjection(const ui::Transform&, uint32_t orientation, const Rect& frame, 167 const Rect& viewport, const Rect& sourceClip, 168 const Rect& destinationClip, bool needsFiltering) = 0; 169 // Sets the bounds to use 170 virtual void setBounds(const ui::Size&) = 0; 171 172 // Sets the layer stack filtering settings for this output. See 173 // belongsInOutput for full details. 174 virtual void setLayerStackFilter(uint32_t layerStackId, bool isInternal) = 0; 175 176 // Sets the output color mode 177 virtual void setColorProfile(const ColorProfile&) = 0; 178 179 // Outputs a string with a state dump 180 virtual void dump(std::string&) const = 0; 181 182 // Gets the debug name for the output 183 virtual const std::string& getName() const = 0; 184 185 // Sets a debug name for the output 186 virtual void setName(const std::string&) = 0; 187 188 // Gets the current render color mode for the output 189 virtual DisplayColorProfile* getDisplayColorProfile() const = 0; 190 191 // Gets the current render surface for the output 192 virtual RenderSurface* getRenderSurface() const = 0; 193 194 using OutputCompositionState = compositionengine::impl::OutputCompositionState; 195 196 // Gets the raw composition state data for the output 197 // TODO(lpique): Make this protected once it is only internally called. 198 virtual const OutputCompositionState& getState() const = 0; 199 200 // Allows mutable access to the raw composition state data for the output. 201 // This is meant to be used by the various functions that are part of the 202 // composition process. 203 // TODO(lpique): Make this protected once it is only internally called. 204 virtual OutputCompositionState& editState() = 0; 205 206 // Gets the dirty region in layer stack space. 207 // If repaintEverything is true, this will be the full display bounds. 208 virtual Region getDirtyRegion(bool repaintEverything) const = 0; 209 210 // Tests whether a given layerStackId belongs in this output. 211 // A layer belongs to the output if its layerStackId matches the of the output layerStackId, 212 // unless the layer should display on the primary output only and this is not the primary output 213 214 // A layer belongs to the output if its layerStackId matches. Additionally 215 // if the layer should only show in the internal (primary) display only and 216 // this output allows that. 217 virtual bool belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const = 0; 218 219 // Determines if a layer belongs to the output. 220 virtual bool belongsInOutput(const sp<LayerFE>&) const = 0; 221 222 // Returns a pointer to the output layer corresponding to the given layer on 223 // this output, or nullptr if the layer does not have one 224 virtual OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const = 0; 225 226 // Immediately clears all layers from the output. 227 virtual void clearOutputLayers() = 0; 228 229 // For tests use only. Creates and appends an OutputLayer into the output. 230 virtual OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0; 231 232 // Gets the count of output layers managed by this output 233 virtual size_t getOutputLayerCount() const = 0; 234 235 // Gets an output layer in Z order given its index 236 virtual OutputLayer* getOutputLayerOrderedByZByIndex(size_t) const = 0; 237 238 // A helper function for enumerating all the output layers in Z order using 239 // a C++11 range-based for loop. getOutputLayersOrderedByZ()240 auto getOutputLayersOrderedByZ() const { return OutputLayersEnumerator(*this); } 241 242 // Sets the new set of layers being released this frame 243 virtual void setReleasedLayers(ReleasedLayers&&) = 0; 244 245 // Prepare the output, updating the OutputLayers used in the output 246 virtual void prepare(const CompositionRefreshArgs&, LayerFESet&) = 0; 247 248 // Presents the output, finalizing all composition details 249 virtual void present(const CompositionRefreshArgs&) = 0; 250 251 // Latches the front-end layer state for each output layer 252 virtual void updateLayerStateFromFE(const CompositionRefreshArgs&) const = 0; 253 254 protected: 255 virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0; 256 virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0; 257 258 virtual void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) = 0; 259 virtual void collectVisibleLayers(const CompositionRefreshArgs&, CoverageState&) = 0; 260 virtual void ensureOutputLayerIfVisible(sp<LayerFE>&, CoverageState&) = 0; 261 virtual void setReleasedLayers(const CompositionRefreshArgs&) = 0; 262 263 virtual void updateAndWriteCompositionState(const CompositionRefreshArgs&) = 0; 264 virtual void setColorTransform(const CompositionRefreshArgs&) = 0; 265 virtual void updateColorProfile(const CompositionRefreshArgs&) = 0; 266 virtual void beginFrame() = 0; 267 virtual void prepareFrame() = 0; 268 virtual void devOptRepaintFlash(const CompositionRefreshArgs&) = 0; 269 virtual void finishFrame(const CompositionRefreshArgs&) = 0; 270 virtual std::optional<base::unique_fd> composeSurfaces( 271 const Region&, const compositionengine::CompositionRefreshArgs& refreshArgs) = 0; 272 virtual void postFramebuffer() = 0; 273 virtual void chooseCompositionStrategy() = 0; 274 virtual bool getSkipColorTransform() const = 0; 275 virtual FrameFences presentAndGetFrameFences() = 0; 276 virtual std::vector<LayerFE::LayerSettings> generateClientCompositionRequests( 277 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) = 0; 278 virtual void appendRegionFlashRequests( 279 const Region& flashRegion, 280 std::vector<LayerFE::LayerSettings>& clientCompositionLayers) = 0; 281 virtual void setExpensiveRenderingExpected(bool enabled) = 0; 282 virtual void cacheClientCompositionRequests(uint32_t cacheSize) = 0; 283 }; 284 285 } // namespace compositionengine 286 } // namespace android 287