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