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