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 <optional> 21 #include <string> 22 #include <unordered_map> 23 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/DisplayState.h> 32 #include <ui/GraphicTypes.h> 33 #include <ui/HdrCapabilities.h> 34 #include <ui/Region.h> 35 #include <ui/StaticDisplayInfo.h> 36 #include <ui/Transform.h> 37 #include <utils/Errors.h> 38 #include <utils/Mutex.h> 39 #include <utils/RefBase.h> 40 #include <utils/Timers.h> 41 42 #include "DisplayHardware/DisplayIdentification.h" 43 #include "DisplayHardware/DisplayMode.h" 44 #include "DisplayHardware/Hal.h" 45 #include "DisplayHardware/PowerAdvisor.h" 46 47 namespace android { 48 49 class Fence; 50 class HWComposer; 51 class IGraphicBufferProducer; 52 class Layer; 53 class SurfaceFlinger; 54 55 struct CompositionInfo; 56 struct DisplayDeviceCreationArgs; 57 58 namespace compositionengine { 59 class Display; 60 class DisplaySurface; 61 } // namespace compositionengine 62 63 class DisplayDevice : public RefBase { 64 public: 65 constexpr static float sDefaultMinLumiance = 0.0; 66 constexpr static float sDefaultMaxLumiance = 500.0; 67 68 explicit DisplayDevice(DisplayDeviceCreationArgs& args); 69 70 // Must be destroyed on the main thread because it may call into HWComposer. 71 virtual ~DisplayDevice(); 72 getCompositionDisplay()73 std::shared_ptr<compositionengine::Display> getCompositionDisplay() const { 74 return mCompositionDisplay; 75 } 76 getConnectionType()77 std::optional<ui::DisplayConnectionType> getConnectionType() const { return mConnectionType; } 78 isVirtual()79 bool isVirtual() const { return !mConnectionType; } isPrimary()80 bool isPrimary() const { return mIsPrimary; } 81 82 // isSecure indicates whether this display can be trusted to display 83 // secure surfaces. 84 bool isSecure() const; 85 86 int getWidth() const; 87 int getHeight() const; getSize()88 ui::Size getSize() const { return {getWidth(), getHeight()}; } 89 90 void setLayerStack(ui::LayerStack); 91 void setDisplaySize(int width, int height); 92 void setProjection(ui::Rotation orientation, Rect viewport, Rect frame); 93 getPhysicalOrientation()94 ui::Rotation getPhysicalOrientation() const { return mPhysicalOrientation; } getOrientation()95 ui::Rotation getOrientation() const { return mOrientation; } 96 97 static ui::Transform::RotationFlags getPrimaryDisplayRotationFlags(); 98 99 ui::Transform::RotationFlags getTransformHint() const; 100 const ui::Transform& getTransform() const; 101 const Rect& getLayerStackSpaceRect() const; 102 const Rect& getOrientedDisplaySpaceRect() const; 103 bool needsFiltering() const; 104 ui::LayerStack getLayerStack() const; 105 106 DisplayId getId() const; 107 108 // Shorthand to upcast the ID of a display whose type is known as a precondition. getPhysicalId()109 PhysicalDisplayId getPhysicalId() const { 110 const auto id = PhysicalDisplayId::tryCast(getId()); 111 LOG_FATAL_IF(!id); 112 return *id; 113 } 114 getVirtualId()115 VirtualDisplayId getVirtualId() const { 116 const auto id = VirtualDisplayId::tryCast(getId()); 117 LOG_FATAL_IF(!id); 118 return *id; 119 } 120 getDisplayToken()121 const wp<IBinder>& getDisplayToken() const { return mDisplayToken; } getSequenceId()122 int32_t getSequenceId() const { return mSequenceId; } 123 124 const Region& getUndefinedRegion() const; 125 126 int32_t getSupportedPerFrameMetadata() const; 127 128 bool hasWideColorGamut() const; 129 // Whether h/w composer has native support for specific HDR type. 130 bool hasHDR10PlusSupport() const; 131 bool hasHDR10Support() const; 132 bool hasHLGSupport() const; 133 bool hasDolbyVisionSupport() const; 134 135 void overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes); 136 137 // The returned HdrCapabilities is the combination of HDR capabilities from 138 // hardware composer and RenderEngine. When the DisplayDevice supports wide 139 // color gamut, RenderEngine is able to simulate HDR support in Display P3 140 // color space for both PQ and HLG HDR contents. The minimum and maximum 141 // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance 142 // respectively if hardware composer doesn't return meaningful values. 143 HdrCapabilities getHdrCapabilities() const; 144 145 // Return true if intent is supported by the display. 146 bool hasRenderIntent(ui::RenderIntent intent) const; 147 148 const Rect& getBounds() const; bounds()149 const Rect& bounds() const { return getBounds(); } 150 151 void setDisplayName(const std::string& displayName); getDisplayName()152 const std::string& getDisplayName() const { return mDisplayName; } 153 154 void setDeviceProductInfo(std::optional<DeviceProductInfo> info); getDeviceProductInfo()155 const std::optional<DeviceProductInfo>& getDeviceProductInfo() const { 156 return mDeviceProductInfo; 157 } 158 159 /* ------------------------------------------------------------------------ 160 * Display power mode management. 161 */ 162 hardware::graphics::composer::hal::PowerMode getPowerMode() const; 163 void setPowerMode(hardware::graphics::composer::hal::PowerMode mode); 164 bool isPoweredOn() const; 165 166 // Enables layer caching on this DisplayDevice 167 void enableLayerCaching(bool enable); 168 169 ui::Dataspace getCompositionDataSpace() const; 170 171 /* ------------------------------------------------------------------------ 172 * Display mode management. 173 */ 174 const DisplayModePtr& getActiveMode() const; 175 void setActiveMode(DisplayModeId); 176 status_t initiateModeChange(DisplayModeId modeId, 177 const hal::VsyncPeriodChangeConstraints& constraints, 178 hal::VsyncPeriodChangeTimeline* outTimeline) const; 179 180 // Return the immutable list of supported display modes. The HWC may report different modes 181 // after a hotplug reconnect event, in which case the DisplayDevice object will be recreated. 182 // Hotplug reconnects are common for external displays. 183 const DisplayModes& getSupportedModes() const; 184 185 // Returns nullptr if the given mode ID is not supported. A previously 186 // supported mode may be no longer supported for some devices like TVs and 187 // set-top boxes after a hotplug reconnect. 188 DisplayModePtr getMode(DisplayModeId) const; 189 190 void onVsync(nsecs_t timestamp); 191 nsecs_t getVsyncPeriodFromHWC() const; 192 nsecs_t getRefreshTimestamp() const; 193 194 // release HWC resources (if any) for removable displays 195 void disconnect(); 196 197 /* ------------------------------------------------------------------------ 198 * Debugging 199 */ 200 uint32_t getPageFlipCount() const; 201 std::string getDebugName() const; 202 void dump(std::string& result) const; 203 204 private: 205 const sp<SurfaceFlinger> mFlinger; 206 HWComposer& mHwComposer; 207 const wp<IBinder> mDisplayToken; 208 const int32_t mSequenceId; 209 const std::optional<ui::DisplayConnectionType> mConnectionType; 210 211 const std::shared_ptr<compositionengine::Display> mCompositionDisplay; 212 213 std::string mDisplayName; 214 215 const ui::Rotation mPhysicalOrientation; 216 ui::Rotation mOrientation = ui::ROTATION_0; 217 218 static ui::Transform::RotationFlags sPrimaryDisplayRotationFlags; 219 220 hardware::graphics::composer::hal::PowerMode mPowerMode = 221 hardware::graphics::composer::hal::PowerMode::OFF; 222 DisplayModePtr mActiveMode; 223 const DisplayModes mSupportedModes; 224 225 std::atomic<nsecs_t> mLastHwVsync = 0; 226 227 // TODO(b/74619554): Remove special cases for primary display. 228 const bool mIsPrimary; 229 230 std::optional<DeviceProductInfo> mDeviceProductInfo; 231 232 std::vector<ui::Hdr> mOverrideHdrTypes; 233 }; 234 235 struct DisplayDeviceState { 236 struct Physical { 237 PhysicalDisplayId id; 238 ui::DisplayConnectionType type; 239 hardware::graphics::composer::hal::HWDisplayId hwcDisplayId; 240 std::optional<DeviceProductInfo> deviceProductInfo; 241 DisplayModes supportedModes; 242 DisplayModePtr activeMode; 243 244 bool operator==(const Physical& other) const { 245 return id == other.id && type == other.type && hwcDisplayId == other.hwcDisplayId; 246 } 247 }; 248 isVirtualDisplayDeviceState249 bool isVirtual() const { return !physical; } 250 251 int32_t sequenceId = sNextSequenceId++; 252 std::optional<Physical> physical; 253 sp<IGraphicBufferProducer> surface; 254 ui::LayerStack layerStack = ui::NO_LAYER_STACK; 255 Rect layerStackSpaceRect; 256 Rect orientedDisplaySpaceRect; 257 ui::Rotation orientation = ui::ROTATION_0; 258 uint32_t width = 0; 259 uint32_t height = 0; 260 std::string displayName; 261 bool isSecure = false; 262 263 private: 264 static std::atomic<int32_t> sNextSequenceId; 265 }; 266 267 struct DisplayDeviceCreationArgs { 268 // We use a constructor to ensure some of the values are set, without 269 // assuming a default value. 270 DisplayDeviceCreationArgs(const sp<SurfaceFlinger>&, HWComposer& hwComposer, 271 const wp<IBinder>& displayToken, 272 std::shared_ptr<compositionengine::Display>); 273 const sp<SurfaceFlinger> flinger; 274 HWComposer& hwComposer; 275 const wp<IBinder> displayToken; 276 const std::shared_ptr<compositionengine::Display> compositionDisplay; 277 278 int32_t sequenceId{0}; 279 std::optional<ui::DisplayConnectionType> connectionType; 280 bool isSecure{false}; 281 sp<ANativeWindow> nativeWindow; 282 sp<compositionengine::DisplaySurface> displaySurface; 283 ui::Rotation physicalOrientation{ui::ROTATION_0}; 284 bool hasWideColorGamut{false}; 285 HdrCapabilities hdrCapabilities; 286 int32_t supportedPerFrameMetadata{0}; 287 std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes; 288 hardware::graphics::composer::hal::PowerMode initialPowerMode{ 289 hardware::graphics::composer::hal::PowerMode::ON}; 290 bool isPrimary{false}; 291 DisplayModes supportedModes; 292 }; 293 294 // Predicates for display lookup. 295 296 struct WithLayerStack { WithLayerStackWithLayerStack297 explicit WithLayerStack(ui::LayerStack layerStack) : layerStack(layerStack) {} 298 operatorWithLayerStack299 bool operator()(const DisplayDevice& display) const { 300 return display.getLayerStack() == layerStack; 301 } 302 303 ui::LayerStack layerStack; 304 }; 305 306 } // namespace android 307