1 /* 2 * Copyright (C) 2007 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 <memory> 20 #include <string> 21 #include <unordered_map> 22 23 #include <android-base/thread_annotations.h> 24 #include <android/native_window.h> 25 #include <binder/IBinder.h> 26 #include <compositionengine/Display.h> 27 #include <compositionengine/DisplaySurface.h> 28 #include <gui/LayerState.h> 29 #include <math/mat4.h> 30 #include <renderengine/RenderEngine.h> 31 #include <system/window.h> 32 #include <ui/DisplayId.h> 33 #include <ui/DisplayIdentification.h> 34 #include <ui/DisplayState.h> 35 #include <ui/GraphicTypes.h> 36 #include <ui/HdrCapabilities.h> 37 #include <ui/Region.h> 38 #include <ui/StaticDisplayInfo.h> 39 #include <ui/Transform.h> 40 #include <utils/Errors.h> 41 #include <utils/Mutex.h> 42 #include <utils/RefBase.h> 43 #include <utils/Timers.h> 44 45 #include "DisplayHardware/DisplayMode.h" 46 #include "DisplayHardware/Hal.h" 47 #include "FrontEnd/DisplayInfo.h" 48 #include "Scheduler/RefreshRateSelector.h" 49 #include "ThreadContext.h" 50 #include "TracedOrdinal.h" 51 #include "Utils/Dumper.h" 52 53 namespace android { 54 55 class Fence; 56 class HWComposer; 57 class HdrSdrRatioOverlay; 58 class IGraphicBufferProducer; 59 class Layer; 60 class RefreshRateOverlay; 61 class SurfaceFlinger; 62 63 struct CompositionInfo; 64 struct DisplayDeviceCreationArgs; 65 66 namespace display { 67 class DisplaySnapshot; 68 } // namespace display 69 70 class DisplayDevice : public RefBase { 71 public: 72 constexpr static float sDefaultMinLumiance = 0.0; 73 constexpr static float sDefaultMaxLumiance = 500.0; 74 enum { eReceivesInput = 0x01 }; 75 76 explicit DisplayDevice(DisplayDeviceCreationArgs& args); 77 78 // Must be destroyed on the main thread because it may call into HWComposer. 79 virtual ~DisplayDevice(); 80 getCompositionDisplay()81 std::shared_ptr<compositionengine::Display> getCompositionDisplay() const { 82 return mCompositionDisplay; 83 } 84 85 bool isVirtual() const; isPrimary()86 bool isPrimary() const { return mIsPrimary; } 87 88 // isSecure indicates whether this display can be trusted to display 89 // secure surfaces. 90 bool isSecure() const; 91 void setSecure(bool secure); 92 93 // The optimization policy influences whether this display is optimized for power or 94 // performance. 95 gui::ISurfaceComposer::OptimizationPolicy getOptimizationPolicy() const; 96 void setOptimizationPolicy(gui::ISurfaceComposer::OptimizationPolicy optimizationPolicy); 97 98 int getWidth() const; 99 int getHeight() const; getSize()100 ui::Size getSize() const { return {getWidth(), getHeight()}; } 101 102 void setLayerFilter(ui::LayerFilter); 103 void setDisplaySize(ui::Size); 104 void setProjection(ui::Rotation orientation, Rect viewport, Rect frame); 105 void stageBrightness(float brightness) REQUIRES(kMainThreadContext); 106 void persistBrightness(bool needsComposite) REQUIRES(kMainThreadContext); 107 bool isBrightnessStale() const REQUIRES(kMainThreadContext); 108 void setFlags(uint32_t flags); 109 getPhysicalOrientation()110 ui::Rotation getPhysicalOrientation() const { return mPhysicalOrientation; } getOrientation()111 ui::Rotation getOrientation() const { return mOrientation; } 112 113 std::optional<float> getStagedBrightness() const REQUIRES(kMainThreadContext); 114 ui::Transform::RotationFlags getTransformHint() const; 115 const ui::Transform& getTransform() const; 116 const Rect& getLayerStackSpaceRect() const; 117 const Rect& getOrientedDisplaySpaceRect() const; 118 ui::LayerStack getLayerStack() const; receivesInput()119 bool receivesInput() const { return mFlags & eReceivesInput; } 120 121 DisplayId getId() const; 122 getDisplayIdVariant()123 DisplayIdVariant getDisplayIdVariant() const { 124 const auto idVariant = mCompositionDisplay->getDisplayIdVariant(); 125 LOG_FATAL_IF(!idVariant); 126 return *idVariant; 127 } 128 getVirtualDisplayIdVariant()129 std::optional<VirtualDisplayIdVariant> getVirtualDisplayIdVariant() const { 130 return ftl::match( 131 getDisplayIdVariant(), 132 [](PhysicalDisplayId) { return std::optional<VirtualDisplayIdVariant>(); }, 133 [](auto id) { return std::optional<VirtualDisplayIdVariant>(id); }); 134 } 135 136 // Shorthand to upcast the ID of a display whose type is known as a precondition. getPhysicalId()137 PhysicalDisplayId getPhysicalId() const { 138 const auto physicalDisplayId = asPhysicalDisplayId(getDisplayIdVariant()); 139 LOG_FATAL_IF(!physicalDisplayId); 140 return *physicalDisplayId; 141 } 142 getVirtualId()143 VirtualDisplayId getVirtualId() const { 144 const auto virtualDisplayId = asVirtualDisplayId(getDisplayIdVariant()); 145 LOG_FATAL_IF(!virtualDisplayId); 146 return *virtualDisplayId; 147 } 148 getDisplayToken()149 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; } getSequenceId()150 int32_t getSequenceId() const { return mSequenceId; } 151 152 const Region& getUndefinedRegion() const; 153 154 int32_t getSupportedPerFrameMetadata() const; 155 156 bool hasWideColorGamut() const; 157 // Whether h/w composer has native support for specific HDR type. 158 bool hasHDR10PlusSupport() const; 159 bool hasHDR10Support() const; 160 bool hasHLGSupport() const; 161 bool hasDolbyVisionSupport() const; 162 163 void overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes); 164 165 // The returned HdrCapabilities is the combination of HDR capabilities from 166 // hardware composer and RenderEngine. When the DisplayDevice supports wide 167 // color gamut, RenderEngine is able to simulate HDR support in Display P3 168 // color space for both PQ and HLG HDR contents. The minimum and maximum 169 // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance 170 // respectively if hardware composer doesn't return meaningful values. 171 HdrCapabilities getHdrCapabilities() const; 172 173 // Return true if intent is supported by the display. 174 bool hasRenderIntent(ui::RenderIntent intent) const; 175 176 const Rect getBounds() const; bounds()177 const Rect bounds() const { return getBounds(); } 178 179 void setDisplayName(const std::string& displayName); getDisplayName()180 const std::string& getDisplayName() const { return mDisplayName; } 181 182 surfaceflinger::frontend::DisplayInfo getFrontEndInfo() const; 183 184 /* ------------------------------------------------------------------------ 185 * Display power mode management. 186 */ 187 hardware::graphics::composer::hal::PowerMode getPowerMode() const; 188 void setPowerMode(hardware::graphics::composer::hal::PowerMode); 189 bool isPoweredOn() const; 190 bool isRefreshable() const; 191 void tracePowerMode(); 192 193 // Enables layer caching on this DisplayDevice 194 void enableLayerCaching(bool enable); 195 196 ui::Dataspace getCompositionDataSpace() const; 197 refreshRateSelector()198 scheduler::RefreshRateSelector& refreshRateSelector() const { return *mRefreshRateSelector; } 199 200 // Extends the lifetime of the RefreshRateSelector, so it can outlive this DisplayDevice. holdRefreshRateSelector()201 std::shared_ptr<scheduler::RefreshRateSelector> holdRefreshRateSelector() const { 202 return mRefreshRateSelector; 203 } 204 205 // Enables an overlay to be displayed with the current refresh rate 206 // TODO(b/241285876): Move overlay to DisplayModeController. 207 void enableRefreshRateOverlay(bool enable, bool setByHwc, Fps refreshRate, Fps renderFps, 208 bool showSpinner, bool showRenderRate, bool showInMiddle) 209 REQUIRES(kMainThreadContext); 210 void updateRefreshRateOverlayRate(Fps refreshRate, Fps renderFps, bool setByHwc = false); isRefreshRateOverlayEnabled()211 bool isRefreshRateOverlayEnabled() const { return mRefreshRateOverlay != nullptr; } 212 void animateOverlay(); 213 bool onKernelTimerChanged(std::optional<DisplayModeId>, bool timerExpired); 214 void onVrrIdle(bool idle); 215 216 // Enables an overlay to be display with the hdr/sdr ratio 217 void enableHdrSdrRatioOverlay(bool enable) REQUIRES(kMainThreadContext); 218 void updateHdrSdrRatioOverlayRatio(float currentHdrSdrRatio); isHdrSdrRatioOverlayEnabled()219 bool isHdrSdrRatioOverlayEnabled() const { return mHdrSdrRatioOverlay != nullptr; } 220 getAdjustedRefreshRate()221 Fps getAdjustedRefreshRate() const { return mAdjustedRefreshRate; } 222 223 // Round the requested refresh rate to match a divisor of the pacesetter 224 // display's refresh rate. Only supported for virtual displays. 225 void adjustRefreshRate(Fps pacesetterDisplayRefreshRate); 226 227 // release HWC resources (if any) for removable displays 228 void disconnect(); 229 230 void dump(utils::Dumper&) const; 231 232 private: 233 const sp<SurfaceFlinger> mFlinger; 234 HWComposer& mHwComposer; 235 const wp<IBinder> mDisplayToken; 236 const int32_t mSequenceId; 237 238 const std::shared_ptr<compositionengine::Display> mCompositionDisplay; 239 240 std::string mDisplayName; 241 242 const ui::Rotation mPhysicalOrientation; 243 ui::Rotation mOrientation = ui::ROTATION_0; 244 bool mIsOrientationChanged = false; 245 246 TracedOrdinal<hardware::graphics::composer::hal::PowerMode> mPowerMode; 247 248 std::optional<float> mStagedBrightness; 249 std::optional<float> mBrightness; 250 251 // TODO(b/182939859): Remove special cases for primary display. 252 const bool mIsPrimary; 253 254 gui::ISurfaceComposer::OptimizationPolicy mOptimizationPolicy = 255 gui::ISurfaceComposer::OptimizationPolicy::optimizeForPerformance; 256 257 uint32_t mFlags = 0; 258 259 // Requested refresh rate in fps, supported only for virtual displays. 260 // when this value is non zero, SurfaceFlinger will try to drop frames 261 // for virtual displays to match this requested refresh rate. 262 const Fps mRequestedRefreshRate; 263 264 // Adjusted refresh rate, rounded to match a divisor of the pacesetter 265 // display's refresh rate. Only supported for virtual displays. 266 Fps mAdjustedRefreshRate = 0_Hz; 267 268 std::vector<ui::Hdr> mOverrideHdrTypes; 269 270 std::shared_ptr<scheduler::RefreshRateSelector> mRefreshRateSelector; 271 std::unique_ptr<RefreshRateOverlay> mRefreshRateOverlay; 272 std::unique_ptr<HdrSdrRatioOverlay> mHdrSdrRatioOverlay; 273 // This parameter is only used for hdr/sdr ratio overlay 274 float mHdrSdrRatio = 1.0f; 275 }; 276 277 struct DisplayDeviceState { 278 struct Physical { 279 PhysicalDisplayId id; 280 hardware::graphics::composer::hal::HWDisplayId hwcDisplayId; 281 uint8_t port; 282 DisplayModePtr activeMode; 283 284 bool operator==(const Physical& other) const { 285 return id == other.id && hwcDisplayId == other.hwcDisplayId; 286 } 287 }; 288 isVirtualDisplayDeviceState289 bool isVirtual() const { return !physical; } 290 291 int32_t sequenceId = sNextSequenceId++; 292 std::optional<Physical> physical; 293 sp<IGraphicBufferProducer> surface; 294 ui::LayerStack layerStack; 295 uint32_t flags = 0; 296 Rect layerStackSpaceRect; 297 Rect orientedDisplaySpaceRect; 298 ui::Rotation orientation = ui::ROTATION_0; 299 uint32_t width = 0; 300 uint32_t height = 0; 301 std::string displayName; 302 std::string uniqueId; 303 bool isSecure = false; 304 305 gui::ISurfaceComposer::OptimizationPolicy optimizationPolicy = 306 gui::ISurfaceComposer::OptimizationPolicy::optimizeForPerformance; 307 bool isProtected = false; 308 // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only 309 Fps requestedRefreshRate; 310 int32_t maxLayerPictureProfiles = 0; 311 bool hasPictureProcessing = false; 312 hardware::graphics::composer::hal::PowerMode initialPowerMode{ 313 hardware::graphics::composer::hal::PowerMode::OFF}; 314 315 private: 316 static std::atomic<int32_t> sNextSequenceId; 317 }; 318 319 struct DisplayDeviceCreationArgs { 320 // We use a constructor to ensure some of the values are set, without 321 // assuming a default value. 322 DisplayDeviceCreationArgs(const sp<SurfaceFlinger>&, HWComposer& hwComposer, 323 const wp<IBinder>& displayToken, 324 std::shared_ptr<compositionengine::Display>); 325 const sp<SurfaceFlinger> flinger; 326 HWComposer& hwComposer; 327 const wp<IBinder> displayToken; 328 const std::shared_ptr<compositionengine::Display> compositionDisplay; 329 std::shared_ptr<scheduler::RefreshRateSelector> refreshRateSelector; 330 331 int32_t sequenceId{0}; 332 bool isSecure{false}; 333 bool isProtected{false}; 334 sp<ANativeWindow> nativeWindow; 335 sp<compositionengine::DisplaySurface> displaySurface; 336 ui::Rotation physicalOrientation{ui::ROTATION_0}; 337 bool hasWideColorGamut{false}; 338 HdrCapabilities hdrCapabilities; 339 int32_t supportedPerFrameMetadata{0}; 340 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes; 341 hardware::graphics::composer::hal::PowerMode initialPowerMode{ 342 hardware::graphics::composer::hal::PowerMode::OFF}; 343 bool isPrimary{false}; 344 // Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only 345 Fps requestedRefreshRate; 346 }; 347 348 } // namespace android 349