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