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/FenceTime.h> 31 #include <ui/GraphicTypes.h> 32 #include <ui/LayerStack.h> 33 #include <ui/Region.h> 34 #include <ui/Transform.h> 35 #include <utils/StrongPointer.h> 36 #include <utils/Vector.h> 37 38 #include <ui/DisplayIdentification.h> 39 #include "DisplayHardware/HWComposer.h" 40 41 namespace android { 42 43 namespace HWC2 { 44 class Layer; 45 } // namespace HWC2 46 47 namespace compositionengine { 48 49 class DisplayColorProfile; 50 class LayerFE; 51 class RenderSurface; 52 class OutputLayer; 53 54 struct CompositionRefreshArgs; 55 struct LayerFECompositionState; 56 57 namespace impl { 58 struct OutputCompositionState; 59 struct GpuCompositionResult; 60 } // namespace impl 61 62 /** 63 * Encapsulates all the state involved with composing layers for an output 64 */ 65 class Output { 66 public: 67 using ReleasedLayers = std::vector<wp<LayerFE>>; 68 using UniqueFELayerStateMap = std::unordered_map<LayerFE*, LayerFECompositionState*>; 69 70 // A helper class for enumerating the output layers using a C++11 ranged-based for loop 71 template <typename T> 72 class OutputLayersEnumerator { 73 public: 74 // TODO(lpique): Consider turning this into a C++20 view when possible. 75 template <bool IsConstIter> 76 class IteratorImpl { 77 public: 78 // Required definitions to be considered an iterator 79 using iterator_category = std::forward_iterator_tag; 80 using value_type = decltype(std::declval<T>().getOutputLayerOrderedByZByIndex(0)); 81 using difference_type = std::ptrdiff_t; 82 using pointer = std::conditional_t<IsConstIter, const value_type*, value_type*>; 83 using reference = std::conditional_t<IsConstIter, const value_type&, value_type&>; 84 85 IteratorImpl() = default; IteratorImpl(const T * output,size_t index)86 IteratorImpl(const T* output, size_t index) : mOutput(output), mIndex(index) {} 87 88 value_type operator*() const { 89 return mOutput->getOutputLayerOrderedByZByIndex(mIndex); 90 } 91 value_type operator->() const { 92 return mOutput->getOutputLayerOrderedByZByIndex(mIndex); 93 } 94 95 bool operator==(const IteratorImpl& other) const { 96 return mOutput == other.mOutput && mIndex == other.mIndex; 97 } 98 bool operator!=(const IteratorImpl& other) const { return !operator==(other); } 99 100 IteratorImpl& operator++() { 101 ++mIndex; 102 return *this; 103 } 104 IteratorImpl operator++(int) { 105 auto prev = *this; 106 ++mIndex; 107 return prev; 108 } 109 110 private: 111 const T* mOutput{nullptr}; 112 size_t mIndex{0}; 113 }; 114 115 using iterator = IteratorImpl<false>; 116 using const_iterator = IteratorImpl<true>; 117 OutputLayersEnumerator(const T & output)118 explicit OutputLayersEnumerator(const T& output) : mOutput(output) {} begin()119 auto begin() const { return iterator(&mOutput, 0); } end()120 auto end() const { return iterator(&mOutput, mOutput.getOutputLayerCount()); } cbegin()121 auto cbegin() const { return const_iterator(&mOutput, 0); } cend()122 auto cend() const { return const_iterator(&mOutput, mOutput.getOutputLayerCount()); } 123 124 private: 125 const T& mOutput; 126 }; 127 128 struct FrameFences { 129 sp<Fence> presentFence{Fence::NO_FENCE}; 130 sp<Fence> clientTargetAcquireFence{Fence::NO_FENCE}; 131 std::unordered_map<HWC2::Layer*, sp<Fence>> layerFences; 132 }; 133 134 struct ColorProfile { 135 ui::ColorMode mode{ui::ColorMode::NATIVE}; 136 ui::Dataspace dataspace{ui::Dataspace::UNKNOWN}; 137 ui::RenderIntent renderIntent{ui::RenderIntent::COLORIMETRIC}; 138 ui::Dataspace colorSpaceAgnosticDataspace{ui::Dataspace::UNKNOWN}; 139 }; 140 141 // Use internally to incrementally compute visibility/coverage 142 struct CoverageState { CoverageStateCoverageState143 explicit CoverageState(LayerFESet& latchedLayers) : latchedLayers(latchedLayers) {} 144 145 // The set of layers that had been latched for the coverage calls, to 146 // avoid duplicate requests to obtain the same front-end layer state. 147 LayerFESet& latchedLayers; 148 149 // The region of the output which is covered by layers 150 Region aboveCoveredLayers; 151 // The region of the output which is opaquely covered by layers 152 Region aboveOpaqueLayers; 153 // The region of the output which should be considered dirty 154 Region dirtyRegion; 155 }; 156 157 virtual ~Output(); 158 159 // Returns true if the output is valid. This is meant to be checked post- 160 // construction and prior to use, as not everything is set up by the 161 // constructor. 162 virtual bool isValid() const = 0; 163 164 // Returns the DisplayId the output represents, if it has one 165 virtual std::optional<DisplayId> getDisplayId() const = 0; 166 167 // Enables (or disables) composition on this output 168 virtual void setCompositionEnabled(bool) = 0; 169 170 // Enables (or disables) layer caching on this output 171 virtual void setLayerCachingEnabled(bool) = 0; 172 173 // Enables (or disables) layer caching texture pool on this output 174 virtual void setLayerCachingTexturePoolEnabled(bool) = 0; 175 176 // Sets the projection state to use 177 virtual void setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect, 178 const Rect& orientedDisplaySpaceRect) = 0; 179 // Sets the brightness that will take effect next frame. 180 virtual void setNextBrightness(float brightness) = 0; 181 // Sets the bounds to use 182 virtual void setDisplaySize(const ui::Size&) = 0; 183 // Gets the transform hint used in layers that belong to this output. Used to guide 184 // composition orientation so that HW overlay can be used when display isn't in its natural 185 // orientation on some devices. Therefore usually we only use transform hint from display 186 // output. 187 virtual ui::Transform::RotationFlags getTransformHint() const = 0; 188 189 // Sets the filter for this output. See Output::includesLayer. 190 virtual void setLayerFilter(ui::LayerFilter) = 0; 191 192 // Sets the output color mode 193 virtual void setColorProfile(const ColorProfile&) = 0; 194 195 // Sets current calibrated display brightness information 196 virtual void setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) = 0; 197 198 // Outputs a string with a state dump 199 virtual void dump(std::string&) const = 0; 200 201 // Outputs planner information 202 virtual void dumpPlannerInfo(const Vector<String16>& args, std::string&) const = 0; 203 204 // Gets the debug name for the output 205 virtual const std::string& getName() const = 0; 206 207 // Sets a debug name for the output 208 virtual void setName(const std::string&) = 0; 209 210 // Gets the current render color mode for the output 211 virtual DisplayColorProfile* getDisplayColorProfile() const = 0; 212 213 // Gets the current render surface for the output 214 virtual RenderSurface* getRenderSurface() const = 0; 215 216 using OutputCompositionState = compositionengine::impl::OutputCompositionState; 217 218 // Gets the raw composition state data for the output 219 // TODO(lpique): Make this protected once it is only internally called. 220 virtual const OutputCompositionState& getState() const = 0; 221 222 // Allows mutable access to the raw composition state data for the output. 223 // This is meant to be used by the various functions that are part of the 224 // composition process. 225 // TODO(lpique): Make this protected once it is only internally called. 226 virtual OutputCompositionState& editState() = 0; 227 228 // Gets the dirty region in layer stack space. 229 virtual Region getDirtyRegion() const = 0; 230 231 // Returns whether the output includes a layer, based on their respective filters. 232 // See Output::setLayerFilter. 233 virtual bool includesLayer(ui::LayerFilter) const = 0; 234 virtual bool includesLayer(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 // Enables predicting composition strategy to run client composition earlier 269 virtual void setPredictCompositionStrategy(bool) = 0; 270 271 // Enables overriding the 170M trasnfer function as sRGB 272 virtual void setTreat170mAsSrgb(bool) = 0; 273 274 protected: 275 virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0; 276 virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0; 277 278 virtual void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) = 0; 279 virtual void collectVisibleLayers(const CompositionRefreshArgs&, CoverageState&) = 0; 280 virtual void ensureOutputLayerIfVisible(sp<LayerFE>&, CoverageState&) = 0; 281 virtual void setReleasedLayers(const CompositionRefreshArgs&) = 0; 282 283 virtual void updateCompositionState(const CompositionRefreshArgs&) = 0; 284 virtual void planComposition() = 0; 285 virtual void writeCompositionState(const CompositionRefreshArgs&) = 0; 286 virtual void setColorTransform(const CompositionRefreshArgs&) = 0; 287 virtual void updateColorProfile(const CompositionRefreshArgs&) = 0; 288 virtual void beginFrame() = 0; 289 virtual void prepareFrame() = 0; 290 291 using GpuCompositionResult = compositionengine::impl::GpuCompositionResult; 292 // Runs prepare frame in another thread while running client composition using 293 // the previous frame's composition strategy. 294 virtual GpuCompositionResult prepareFrameAsync(const CompositionRefreshArgs&) = 0; 295 virtual void devOptRepaintFlash(const CompositionRefreshArgs&) = 0; 296 virtual void finishFrame(const CompositionRefreshArgs&, GpuCompositionResult&&) = 0; 297 virtual std::optional<base::unique_fd> composeSurfaces( 298 const Region&, const compositionengine::CompositionRefreshArgs&, 299 std::shared_ptr<renderengine::ExternalTexture>, base::unique_fd&) = 0; 300 virtual void postFramebuffer() = 0; 301 virtual void renderCachedSets(const CompositionRefreshArgs&) = 0; 302 virtual bool chooseCompositionStrategy( 303 std::optional<android::HWComposer::DeviceRequestedChanges>*) = 0; 304 virtual void applyCompositionStrategy( 305 const std::optional<android::HWComposer::DeviceRequestedChanges>& changes) = 0; 306 virtual bool getSkipColorTransform() const = 0; 307 virtual FrameFences presentAndGetFrameFences() = 0; 308 virtual std::vector<LayerFE::LayerSettings> generateClientCompositionRequests( 309 bool supportsProtectedContent, ui::Dataspace outputDataspace, 310 std::vector<LayerFE*> &outLayerRef) = 0; 311 virtual void appendRegionFlashRequests( 312 const Region& flashRegion, 313 std::vector<LayerFE::LayerSettings>& clientCompositionLayers) = 0; 314 virtual void setExpensiveRenderingExpected(bool enabled) = 0; 315 virtual void setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) = 0; 316 virtual bool isPowerHintSessionEnabled() = 0; 317 virtual void cacheClientCompositionRequests(uint32_t cacheSize) = 0; 318 virtual bool canPredictCompositionStrategy(const CompositionRefreshArgs&) = 0; 319 }; 320 321 } // namespace compositionengine 322 } // namespace android 323