1 /* 2 * Copyright (C) 2010 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 <memory> 21 #include <mutex> 22 #include <optional> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <vector> 26 27 #include <android-base/thread_annotations.h> 28 #include <ftl/future.h> 29 #include <ui/DisplayIdentification.h> 30 #include <ui/FenceTime.h> 31 32 // TODO(b/129481165): remove the #pragma below and fix conversion issues 33 #pragma clang diagnostic push 34 #pragma clang diagnostic ignored "-Wconversion" 35 #pragma clang diagnostic ignored "-Wextra" 36 #include <ui/GraphicTypes.h> 37 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" 38 39 #include <utils/StrongPointer.h> 40 #include <utils/Timers.h> 41 42 #include "DisplayMode.h" 43 #include "HWC2.h" 44 #include "Hal.h" 45 46 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h> 47 #include <aidl/android/hardware/graphics/composer3/Capability.h> 48 #include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h> 49 #include <aidl/android/hardware/graphics/composer3/Composition.h> 50 #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h> 51 52 namespace android { 53 54 namespace hal = hardware::graphics::composer::hal; 55 56 struct DisplayedFrameStats; 57 class GraphicBuffer; 58 class TestableSurfaceFlinger; 59 struct CompositionInfo; 60 61 namespace Hwc2 { 62 class Composer; 63 } // namespace Hwc2 64 65 namespace compositionengine { 66 class Output; 67 } // namespace compositionengine 68 69 struct KnownHWCGenericLayerMetadata { 70 const char* name; 71 const uint32_t id; 72 }; 73 74 // See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing 75 // this class. 76 class HWComposer { 77 public: 78 struct DeviceRequestedChanges { 79 using ChangedTypes = 80 std::unordered_map<HWC2::Layer*, 81 aidl::android::hardware::graphics::composer3::Composition>; 82 using ClientTargetProperty = 83 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness; 84 using DisplayRequests = hal::DisplayRequest; 85 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>; 86 87 ChangedTypes changedTypes; 88 DisplayRequests displayRequests; 89 LayerRequests layerRequests; 90 ClientTargetProperty clientTargetProperty; 91 }; 92 93 struct HWCDisplayMode { 94 hal::HWConfigId hwcId; 95 int32_t width = -1; 96 int32_t height = -1; 97 nsecs_t vsyncPeriod = -1; 98 int32_t dpiX = -1; 99 int32_t dpiY = -1; 100 int32_t configGroup = -1; 101 102 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) { 103 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height 104 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x" 105 << mode.dpiY << " group=" << mode.configGroup; 106 } 107 }; 108 109 virtual ~HWComposer(); 110 111 virtual void setCallback(HWC2::ComposerCallback&) = 0; 112 113 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort, 114 DisplayIdentificationData* outData) const = 0; 115 116 virtual bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const = 0; 117 virtual bool hasDisplayCapability( 118 HalDisplayId, 119 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0; 120 121 virtual size_t getMaxVirtualDisplayCount() const = 0; 122 virtual size_t getMaxVirtualDisplayDimension() const = 0; 123 124 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays 125 // supported by the HWC can be queried in advance, but allocation may fail for other reasons. 126 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0; 127 128 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0; 129 130 // Attempts to create a new layer on this display 131 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0; 132 133 // Gets any required composition change requests from the HWC device. 134 // 135 // Note that frameUsesClientComposition must be set correctly based on 136 // whether the current frame appears to use client composition. If it is 137 // false some internal optimizations are allowed to present the display 138 // with fewer handshakes, but this does not work if client composition is 139 // expected. 140 virtual status_t getDeviceCompositionChanges( 141 HalDisplayId, bool frameUsesClientComposition, 142 std::chrono::steady_clock::time_point earliestPresentTime, 143 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime, 144 std::optional<DeviceRequestedChanges>* outChanges) = 0; 145 146 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence, 147 const sp<GraphicBuffer>& target, ui::Dataspace) = 0; 148 149 // Present layers to the display and read releaseFences. 150 virtual status_t presentAndGetReleaseFences( 151 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime, 152 const std::shared_ptr<FenceTime>& previousPresentFence) = 0; 153 154 // set power mode 155 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0; 156 157 // Sets a color transform to be applied to the result of composition 158 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0; 159 160 // reset state when a display is disconnected 161 virtual void disconnectDisplay(HalDisplayId) = 0; 162 163 // get the present fence received from the last call to present. 164 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0; 165 166 // Get last release fence for the given layer 167 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0; 168 169 // Set the output buffer and acquire fence for a virtual display. 170 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence, 171 const sp<GraphicBuffer>& buffer) = 0; 172 173 // After SurfaceFlinger has retrieved the release fences for all the frames, 174 // it can call this to clear the shared pointers in the release fence map 175 virtual void clearReleaseFences(HalDisplayId) = 0; 176 177 // Fetches the HDR capabilities of the given display 178 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0; 179 180 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0; 181 182 // Returns the available RenderIntent of the given display. 183 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0; 184 185 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0; 186 187 // Returns the attributes of the color sampling engine. 188 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat, 189 ui::Dataspace* outDataspace, 190 uint8_t* outComponentMask) = 0; 191 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, 192 uint8_t componentMask, 193 uint64_t maxFrames) = 0; 194 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp, 195 DisplayedFrameStats* outStats) = 0; 196 197 // Sets the brightness of a display. 198 virtual ftl::Future<status_t> setDisplayBrightness( 199 PhysicalDisplayId, float brightness, float brightnessNits, 200 const Hwc2::Composer::DisplayBrightnessOptions&) = 0; 201 202 // Get whether the display skipped validation on the latest present 203 virtual bool getValidateSkipped(HalDisplayId displayId) const = 0; 204 205 // Events handling --------------------------------------------------------- 206 207 // Returns stable display ID (and display name on connection of new or previously disconnected 208 // display), or std::nullopt if hotplug event was ignored. 209 // This function is called from SurfaceFlinger. 210 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, 211 hal::Connection) = 0; 212 213 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events. 214 // TODO(b/157555476): Remove when the framework has proper support for headless mode 215 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0; 216 217 virtual bool onVsync(hal::HWDisplayId, int64_t timestamp) = 0; 218 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0; 219 220 virtual bool isConnected(PhysicalDisplayId) const = 0; 221 222 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0; 223 224 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0; 225 226 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0; 227 228 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode, 229 ui::RenderIntent) = 0; 230 231 // Composer 2.4 232 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0; 233 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0; 234 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId, 235 nsecs_t* outVsyncPeriod) const = 0; 236 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId, 237 const hal::VsyncPeriodChangeConstraints&, 238 hal::VsyncPeriodChangeTimeline* outTimeline) = 0; 239 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0; 240 virtual status_t getSupportedContentTypes( 241 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) const = 0; 242 supportsContentType(PhysicalDisplayId displayId,hal::ContentType type)243 bool supportsContentType(PhysicalDisplayId displayId, hal::ContentType type) const { 244 std::vector<hal::ContentType> types; 245 return getSupportedContentTypes(displayId, &types) == NO_ERROR && 246 std::find(types.begin(), types.end(), type) != types.end(); 247 } 248 249 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0; 250 251 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() 252 const = 0; 253 254 virtual void dump(std::string& out) const = 0; 255 256 virtual Hwc2::Composer* getComposer() const = 0; 257 258 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug, 259 // which in practice is immediately after HWComposer construction, must occur before any 260 // call to this function. 261 // The primary display can be temporarily disconnected from the perspective 262 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId() 263 // if isHeadless(). 264 // 265 // TODO(b/182939859): Remove special cases for primary display. 266 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0; 267 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0; 268 virtual bool isHeadless() const = 0; 269 270 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0; 271 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0; 272 273 // Composer 3.0 274 virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0; 275 virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0; 276 virtual std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) = 0; 277 virtual status_t getDisplayDecorationSupport( 278 PhysicalDisplayId, 279 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>* 280 support) = 0; 281 virtual status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) = 0; 282 virtual bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const = 0; 283 virtual Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const = 0; 284 }; 285 286 static inline bool operator==(const android::HWComposer::DeviceRequestedChanges& lhs, 287 const android::HWComposer::DeviceRequestedChanges& rhs) { 288 return lhs.changedTypes == rhs.changedTypes && lhs.displayRequests == rhs.displayRequests && 289 lhs.layerRequests == rhs.layerRequests && 290 lhs.clientTargetProperty == rhs.clientTargetProperty; 291 } 292 293 namespace impl { 294 295 class HWComposer final : public android::HWComposer { 296 public: 297 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer); 298 explicit HWComposer(const std::string& composerServiceName); 299 300 ~HWComposer() override; 301 302 void setCallback(HWC2::ComposerCallback&) override; 303 304 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort, 305 DisplayIdentificationData* outData) const override; 306 307 bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const override; 308 bool hasDisplayCapability( 309 HalDisplayId, 310 aidl::android::hardware::graphics::composer3::DisplayCapability) const override; 311 312 size_t getMaxVirtualDisplayCount() const override; 313 size_t getMaxVirtualDisplayDimension() const override; 314 315 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override; 316 317 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated. 318 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override; 319 320 // Attempts to create a new layer on this display 321 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override; 322 323 status_t getDeviceCompositionChanges( 324 HalDisplayId, bool frameUsesClientComposition, 325 std::chrono::steady_clock::time_point earliestPresentTime, 326 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime, 327 std::optional<DeviceRequestedChanges>* outChanges) override; 328 329 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence, 330 const sp<GraphicBuffer>& target, ui::Dataspace) override; 331 332 // Present layers to the display and read releaseFences. 333 status_t presentAndGetReleaseFences( 334 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime, 335 const std::shared_ptr<FenceTime>& previousPresentFence) override; 336 337 // set power mode 338 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override; 339 340 // Sets a color transform to be applied to the result of composition 341 status_t setColorTransform(HalDisplayId, const mat4& transform) override; 342 343 // reset state when a display is disconnected 344 void disconnectDisplay(HalDisplayId) override; 345 346 // get the present fence received from the last call to present. 347 sp<Fence> getPresentFence(HalDisplayId) const override; 348 349 // Get last release fence for the given layer 350 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override; 351 352 // Set the output buffer and acquire fence for a virtual display. 353 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence, 354 const sp<GraphicBuffer>& buffer) override; 355 356 // After SurfaceFlinger has retrieved the release fences for all the frames, 357 // it can call this to clear the shared pointers in the release fence map 358 void clearReleaseFences(HalDisplayId) override; 359 360 // Fetches the HDR capabilities of the given display 361 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override; 362 363 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override; 364 365 // Returns the available RenderIntent of the given display. 366 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override; 367 368 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override; 369 370 // Returns the attributes of the color sampling engine. 371 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat, 372 ui::Dataspace* outDataspace, 373 uint8_t* outComponentMask) override; 374 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask, 375 uint64_t maxFrames) override; 376 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp, 377 DisplayedFrameStats* outStats) override; 378 ftl::Future<status_t> setDisplayBrightness( 379 PhysicalDisplayId, float brightness, float brightnessNits, 380 const Hwc2::Composer::DisplayBrightnessOptions&) override; 381 382 // Events handling --------------------------------------------------------- 383 384 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected 385 // display), or std::nullopt if hotplug event was ignored. 386 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override; 387 388 bool updatesDeviceProductInfoOnHotplugReconnect() const override; 389 390 bool onVsync(hal::HWDisplayId, int64_t timestamp) override; 391 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override; 392 393 bool isConnected(PhysicalDisplayId) const override; 394 395 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override; 396 397 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override; 398 399 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override; 400 401 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override; 402 403 bool getValidateSkipped(HalDisplayId displayId) const override; 404 405 // Composer 2.4 406 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override; 407 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override; 408 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId, 409 nsecs_t* outVsyncPeriod) const override; 410 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId, 411 const hal::VsyncPeriodChangeConstraints&, 412 hal::VsyncPeriodChangeTimeline* outTimeline) override; 413 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override; 414 status_t getSupportedContentTypes(PhysicalDisplayId, 415 std::vector<hal::ContentType>*) const override; 416 status_t setContentType(PhysicalDisplayId, hal::ContentType) override; 417 418 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override; 419 420 // Composer 3.0 421 status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override; 422 status_t clearBootDisplayMode(PhysicalDisplayId) override; 423 std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) override; 424 status_t getDisplayDecorationSupport( 425 PhysicalDisplayId, 426 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>* 427 support) override; 428 status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) override; 429 bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const override; 430 Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const override; 431 432 // for debugging ---------------------------------------------------------- 433 void dump(std::string& out) const override; 434 getComposer()435 Hwc2::Composer* getComposer() const override { return mComposer.get(); } 436 getPrimaryHwcDisplayId()437 hal::HWDisplayId getPrimaryHwcDisplayId() const override { 438 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display"); 439 return *mPrimaryHwcDisplayId; 440 } 441 getPrimaryDisplayId()442 PhysicalDisplayId getPrimaryDisplayId() const override { 443 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId()); 444 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display"); 445 return *id; 446 } 447 isHeadless()448 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; } 449 450 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override; 451 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override; 452 453 private: 454 // For unit tests 455 friend TestableSurfaceFlinger; 456 457 struct DisplayData { 458 std::unique_ptr<HWC2::Display> hwcDisplay; 459 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires 460 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences; 461 462 bool validateWasSkipped; 463 hal::Error presentError; 464 465 bool vsyncTraceToggle = false; 466 467 std::mutex vsyncEnabledLock; 468 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE; 469 470 nsecs_t lastHwVsync = 0; 471 }; 472 473 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId); 474 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId); 475 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const; 476 477 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId, 478 hal::Attribute attribute) const; 479 480 void loadCapabilities(); 481 void loadLayerMetadataSupport(); 482 483 std::unordered_map<HalDisplayId, DisplayData> mDisplayData; 484 485 std::unique_ptr<android::Hwc2::Composer> mComposer; 486 std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities; 487 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata; 488 bool mRegisteredCallback = false; 489 490 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap; 491 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId; 492 bool mHasMultiDisplaySupport = false; 493 494 const size_t mMaxVirtualDisplayDimension; 495 const bool mUpdateDeviceProductInfoOnHotplugReconnect; 496 }; 497 498 } // namespace impl 499 } // namespace android 500