blob: 61f92f4d74f007890a2fb0b4b62935e8f0d8a1a8 [file] [log] [blame]
Dan Stoza651bf312015-10-23 17:03:17 -07001/*
2 * Copyright 2015 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
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070017#pragma once
Dan Stoza651bf312015-10-23 17:03:17 -070018
Lloyd Piquea516c002021-05-07 14:36:58 -070019#include <android-base/expected.h>
Ady Abraham27fbcc72021-09-20 14:54:57 -070020#include <android-base/thread_annotations.h>
Dominik Laskowski969cdcb2024-02-08 16:35:29 -050021#include <ftl/expected.h>
Dominik Laskowskib17c6212022-05-09 09:36:19 -070022#include <ftl/future.h>
Sally Qi95f669a2024-08-27 11:31:42 -070023#include <ftl/small_map.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070024#include <gui/HdrMetadata.h>
Mathias Agopian1d77b712017-02-17 15:46:13 -080025#include <math/mat4.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070026#include <ui/HdrCapabilities.h>
Yichi Chen8366f562019-03-25 19:44:06 +080027#include <ui/Region.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010028#include <ui/StaticDisplayInfo.h>
Dan Stoza651bf312015-10-23 17:03:17 -070029#include <utils/Log.h>
30#include <utils/StrongPointer.h>
31#include <utils/Timers.h>
32
33#include <functional>
34#include <string>
35#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070036#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070037#include <vector>
38
Alec Mouricdf16792021-12-10 13:16:06 -080039#include "ComposerHal.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070040#include "Hal.h"
41
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050042#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
Huihong Luo791bef92023-09-19 21:18:45 -070043#include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
Ady Abrahamde549d42022-01-26 19:19:17 -080044#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri30835f22022-03-18 00:58:26 +000045#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Ady Abraham6e60b142022-01-06 18:10:35 -080046#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050047#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050048#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Sally Qi492cec32024-06-28 14:34:47 -070049#include <aidl/android/hardware/graphics/composer3/Lut.h>
Sally Qi0cbd08b2022-08-17 12:12:28 -070050#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
ramindani12bfe6b2023-02-03 13:29:19 -080051#include <aidl/android/hardware/graphics/composer3/RefreshRateChangedDebugData.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050052
Dan Stoza651bf312015-10-23 17:03:17 -070053namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080054
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070055class Fence;
56class FloatRect;
57class GraphicBuffer;
58class TestableSurfaceFlinger;
59struct DisplayedFrameStats;
60
61namespace Hwc2 {
62class Composer;
63} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070064
65namespace HWC2 {
66
Dan Stoza651bf312015-10-23 17:03:17 -070067class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070068
69namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070070
Huihong Luo817ebb82023-12-13 12:54:24 -080071using aidl::android::hardware::drm::HdcpLevels;
Huihong Luo791bef92023-09-19 21:18:45 -070072using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
ramindani12bfe6b2023-02-03 13:29:19 -080073using aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
74
Steven Thomas94e35b92017-07-26 18:48:28 -070075// Implement this interface to receive hardware composer events.
76//
77// These callback functions will generally be called on a hwbinder thread, but
Huihong Luo791bef92023-09-19 21:18:45 -070078// when first registering the callback the onComposerHalHotplugEvent() function
79// will immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070080struct ComposerCallback {
Huihong Luo791bef92023-09-19 21:18:45 -070081 virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070082 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -040083 virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070084 std::optional<hal::VsyncPeriodNanos>) = 0;
85 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
86 const hal::VsyncPeriodChangeTimeline&) = 0;
87 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080088 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
ramindani12bfe6b2023-02-03 13:29:19 -080089 virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
Huihong Luo817ebb82023-12-13 12:54:24 -080090 virtual void onComposerHalHdcpLevelsChanged(hal::HWDisplayId, const HdcpLevels& levels) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070091
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070092protected:
93 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070094};
Dan Stoza651bf312015-10-23 17:03:17 -070095
Peiyong Line9d809e2020-04-14 13:10:48 -070096// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080097class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070098public:
Ana Krulec4593b692019-01-11 22:07:25 -080099 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -0700100
Peiyong Line9d809e2020-04-14 13:10:48 -0700101 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800102 virtual bool isConnected() const = 0;
Florian Mayercffc3792022-03-01 23:50:09 +0000103 virtual void setConnected(bool connected) = 0; // For use by HWComposer only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500104 virtual bool hasCapability(
105 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -0700106 virtual bool isVsyncPeriodSwitchSupported() const = 0;
ramindani32cf0602022-03-02 02:30:29 +0000107 virtual bool hasDisplayIdleTimerCapability() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -0700108 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000109 virtual std::optional<ui::Size> getPhysicalSizeInMm() const = 0;
Dan Stoza651bf312015-10-23 17:03:17 -0700110
Sally Qi95f669a2024-08-27 11:31:42 -0700111 static const int kLutFileDescriptorMapperSize = 20;
112 using LayerLuts =
113 ftl::SmallMap<HWC2::Layer*, aidl::android::hardware::graphics::composer3::LutProperties,
114 kLutFileDescriptorMapperSize>;
115 using LutFileDescriptorMapper =
116 ftl::SmallMap<HWC2::Layer*, ndk::ScopedFileDescriptor, kLutFileDescriptorMapperSize>;
117
ramindani86d3d982022-03-16 20:57:42 +0000118 [[nodiscard]] virtual hal::Error acceptChanges() = 0;
119 [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
Lloyd Piquea516c002021-05-07 14:36:58 -0700120 createLayer() = 0;
ramindani86d3d982022-03-16 20:57:42 +0000121 [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500122 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
123 outTypes) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000124 [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800125 // Returns a bitmask which contains HdrMetadata::Type::*.
ramindani86d3d982022-03-16 20:57:42 +0000126 [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
127 [[nodiscard]] virtual hal::Error getRenderIntents(
Peiyong Line9d809e2020-04-14 13:10:48 -0700128 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000129 [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
130 android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800131
ramindani86d3d982022-03-16 20:57:42 +0000132 [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
133 [[nodiscard]] virtual hal::Error getRequests(
Peiyong Line9d809e2020-04-14 13:10:48 -0700134 hal::DisplayRequest* outDisplayRequests,
135 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500136 [[nodiscard]] virtual ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType()
137 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000138 [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
139 [[nodiscard]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800140 android::HdrCapabilities* outCapabilities) const = 0;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700141 [[nodiscard]] virtual hal::Error getOverlaySupport(
142 aidl::android::hardware::graphics::composer3::OverlayProperties* outProperties)
143 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000144 [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700145 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800146 uint8_t* outComponentMask) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000147 [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
148 uint8_t componentMask,
149 uint64_t maxFrames) const = 0;
150 [[nodiscard]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800151 uint64_t maxFrames, uint64_t timestamp,
152 android::DisplayedFrameStats* outStats) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000153 [[nodiscard]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800154 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000155 [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
156 [[nodiscard]] virtual hal::Error setClientTarget(
Ana Krulec4593b692019-01-11 22:07:25 -0800157 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Alec Mourif97df4d2023-09-06 02:10:05 +0000158 const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace,
159 float hdrSdrRatio) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000160 [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
161 hal::RenderIntent renderIntent) = 0;
162 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
163 [[nodiscard]] virtual hal::Error setOutputBuffer(
Ana Krulec4593b692019-01-11 22:07:25 -0800164 const android::sp<android::GraphicBuffer>& buffer,
165 const android::sp<android::Fence>& releaseFence) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000166 [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
167 [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
ramindani09acbb82023-11-03 09:02:38 -0700168 [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
169 uint32_t* outNumTypes, uint32_t* outNumRequests) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000170 [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700171 int32_t frameIntervalNs,
ramindani86d3d982022-03-16 20:57:42 +0000172 uint32_t* outNumTypes,
173 uint32_t* outNumRequests,
174 android::sp<android::Fence>* outPresentFence,
175 uint32_t* state) = 0;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700176 [[nodiscard]] virtual ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000177 float brightness, float brightnessNits,
178 const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000179 [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100180 hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700181 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000182 [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
183 [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
184 [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
Kriti Dang7defaf32021-11-15 11:55:43 +0100185 hal::HWConfigId* configId) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000186 [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
187 [[nodiscard]] virtual hal::Error getSupportedContentTypes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700188 std::vector<hal::ContentType>*) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000189 [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
190 [[nodiscard]] virtual hal::Error getClientTargetProperty(
Alec Mouri30835f22022-03-18 00:58:26 +0000191 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
192 outClientTargetProperty) = 0;
Sally Qi11dcd582024-08-16 18:11:27 -0700193 [[nodiscard]] virtual hal::Error getRequestedLuts(
Sally Qi95f669a2024-08-27 11:31:42 -0700194 LayerLuts* outLuts, LutFileDescriptorMapper& lutFileDescriptorMapper) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000195 [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500196 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
197 support) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000198 [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
199 [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
ramindani06e518e2022-03-14 18:47:53 +0000200 Hwc2::AidlTransform* outTransform) const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800201};
202
203namespace impl {
204
Lloyd Piquea516c002021-05-07 14:36:58 -0700205class Layer;
206
Ana Krulec4593b692019-01-11 22:07:25 -0800207class Display : public HWC2::Display {
208public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800209 Display(android::Hwc2::Composer&,
210 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
211 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800212 ~Display() override;
213
214 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700215 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700216 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700217 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500218 std::unordered_map<HWC2::Layer*,
219 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
220 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700221 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800222 // Returns a bitmask which contains HdrMetadata::Type::*.
223 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700224 hal::Error getRenderIntents(hal::ColorMode colorMode,
225 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200226 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700227
Peiyong Line9d809e2020-04-14 13:10:48 -0700228 hal::Error getName(std::string* outName) const override;
229 hal::Error getRequests(
230 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700231 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500232 ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType() const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700233 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700234 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700235 hal::Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties*
236 outProperties) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700237 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
238 hal::Dataspace* outDataspace,
239 uint8_t* outComponentMask) const override;
240 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
241 uint64_t maxFrames) const override;
242 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
243 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700244 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
245 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700246 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700247 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
248 const android::sp<android::Fence>& acquireFence,
Alec Mourif97df4d2023-09-06 02:10:05 +0000249 hal::Dataspace dataspace, float hdrSdrRatio) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200250 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800251 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200252 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700253 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200254 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700255 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
ramindani09acbb82023-11-03 09:02:38 -0700256 hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
Ady Abraham43065bd2021-12-10 17:22:15 -0800257 uint32_t* outNumRequests) override;
ramindani4aac32c2023-10-30 14:13:30 -0700258 hal::Error presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
259 uint32_t* outNumTypes, uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700260 android::sp<android::Fence>* outPresentFence,
261 uint32_t* state) override;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700262 ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000263 float brightness, float brightnessNits,
264 const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100265 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
266 const hal::VsyncPeriodChangeConstraints& constraints,
267 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100268 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
269 hal::Error clearBootDisplayConfig() override;
270 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700271 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700272 hal::Error getSupportedContentTypes(
273 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700274 hal::Error setContentType(hal::ContentType) override;
Alec Mouri30835f22022-03-18 00:58:26 +0000275 hal::Error getClientTargetProperty(
276 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
277 outClientTargetProperty) override;
Sally Qi95f669a2024-08-27 11:31:42 -0700278 hal::Error getRequestedLuts(LayerLuts* outLuts,
279 LutFileDescriptorMapper& lutFileDescriptorMapper) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500280 hal::Error getDisplayDecorationSupport(
281 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
282 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000283 hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700284
Dan Stoza651bf312015-10-23 17:03:17 -0700285 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700286 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800287 bool isConnected() const override { return mIsConnected; }
Florian Mayercffc3792022-03-01 23:50:09 +0000288 void setConnected(bool connected) override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500289 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
290 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700291 bool isVsyncPeriodSwitchSupported() const override;
ramindani32cf0602022-03-02 02:30:29 +0000292 bool hasDisplayIdleTimerCapability() const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700293 void onLayerDestroyed(hal::HWLayerId layerId) override;
ramindani06e518e2022-03-14 18:47:53 +0000294 hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000295 void setPhysicalSizeInMm(std::optional<ui::Size> size);
296 std::optional<ui::Size> getPhysicalSizeInMm() const override { return mPhysicalSize; }
Dan Stoza651bf312015-10-23 17:03:17 -0700297
298private:
HyunKyounga264a352024-04-24 18:51:33 +0900299 void loadDisplayCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700300
Dan Stoza651bf312015-10-23 17:03:17 -0700301 // This may fail (and return a null pointer) if no layer with this ID exists
302 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700303 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700304
Lloyd Piquebc792092018-01-17 11:52:30 -0800305 friend android::TestableSurfaceFlinger;
306
Dan Stoza651bf312015-10-23 17:03:17 -0700307 // Member variables
308
Florian Mayercffc3792022-03-01 23:50:09 +0000309 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700310 // this HWC2::Display, so these references are guaranteed to be valid for
311 // the lifetime of this object.
312 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800313 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
314 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700315
Peiyong Line9d809e2020-04-14 13:10:48 -0700316 const hal::HWDisplayId mId;
317 hal::DisplayType mType;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500318 // Cached on first call to getConnectionType.
319 mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800320 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700321
Lloyd Piquea516c002021-05-07 14:36:58 -0700322 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
323 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700324
Ady Abraham27fbcc72021-09-20 14:54:57 -0700325 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700326 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500327 std::optional<
328 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
329 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000330 // Physical size in mm.
331 std::optional<ui::Size> mPhysicalSize;
Dan Stoza651bf312015-10-23 17:03:17 -0700332};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800333
Ana Krulec4593b692019-01-11 22:07:25 -0800334} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700335
Lloyd Pique35d58242018-12-18 16:33:25 -0800336class Layer {
337public:
338 virtual ~Layer();
339
Peiyong Line9d809e2020-04-14 13:10:48 -0700340 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800341
ramindani86d3d982022-03-16 20:57:42 +0000342 [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
343 [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
344 const android::sp<android::GraphicBuffer>& buffer,
345 const android::sp<android::Fence>& acquireFence) = 0;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700346 [[nodiscard]] virtual hal::Error setBufferSlotsToClear(
347 const std::vector<uint32_t>& slotsToClear, uint32_t activeBufferSlot) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000348 [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800349
ramindani86d3d982022-03-16 20:57:42 +0000350 [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
351 [[nodiscard]] virtual hal::Error setColor(
Ady Abraham6e60b142022-01-06 18:10:35 -0800352 aidl::android::hardware::graphics::composer3::Color color) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000353 [[nodiscard]] virtual hal::Error setCompositionType(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500354 aidl::android::hardware::graphics::composer3::Composition type) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000355 [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
356 [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
357 const android::HdrMetadata& metadata) = 0;
358 [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
359 [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
360 [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
361 [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
362 [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
363 [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
364 [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800365
366 // Composer HAL 2.3
ramindani86d3d982022-03-16 20:57:42 +0000367 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800368
369 // Composer HAL 2.4
ramindani86d3d982022-03-16 20:57:42 +0000370 [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
371 bool mandatory,
372 const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700373
374 // AIDL HAL
ramindani86d3d982022-03-16 20:57:42 +0000375 [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
376 [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
Sally Qi11dcd582024-08-16 18:11:27 -0700377 [[nodiscard]] virtual hal::Error setLuts(
378 std::vector<aidl::android::hardware::graphics::composer3::Lut>& luts) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800379};
380
381namespace impl {
382
Peiyong Line9d809e2020-04-14 13:10:48 -0700383// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800384
385class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700386public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700387 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800388 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
389 capabilities,
390 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800391 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700392
Lloyd Piquea516c002021-05-07 14:36:58 -0700393 void onOwningDisplayDestroyed();
394
Peiyong Line9d809e2020-04-14 13:10:48 -0700395 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700396
Peiyong Line9d809e2020-04-14 13:10:48 -0700397 hal::Error setCursorPosition(int32_t x, int32_t y) override;
398 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
399 const android::sp<android::Fence>& acquireFence) override;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700400 hal::Error setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
401 uint32_t activeBufferSlot) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700402 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700403
Peiyong Line9d809e2020-04-14 13:10:48 -0700404 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800405 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500406 hal::Error setCompositionType(
407 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700408 hal::Error setDataspace(hal::Dataspace dataspace) override;
409 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
410 const android::HdrMetadata& metadata) override;
411 hal::Error setDisplayFrame(const android::Rect& frame) override;
412 hal::Error setPlaneAlpha(float alpha) override;
413 hal::Error setSidebandStream(const native_handle_t* stream) override;
414 hal::Error setSourceCrop(const android::FloatRect& crop) override;
415 hal::Error setTransform(hal::Transform transform) override;
416 hal::Error setVisibleRegion(const android::Region& region) override;
417 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700418
Peiyong Lin698147a2018-09-14 13:27:18 -0700419 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700420 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700421
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800422 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700423 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
424 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800425
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700426 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800427 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500428 hal::Error setBlockingRegion(const android::Region& region) override;
Sally Qi11dcd582024-08-16 18:11:27 -0700429 hal::Error setLuts(
430 std::vector<aidl::android::hardware::graphics::composer3::Lut>& luts) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700431
Dan Stoza651bf312015-10-23 17:03:17 -0700432private:
Florian Mayercffc3792022-03-01 23:50:09 +0000433 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700434 // this HWC2::Layer, so these references are guaranteed to be valid for
435 // the lifetime of this object.
436 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800437 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
438 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700439
Lloyd Piquea516c002021-05-07 14:36:58 -0700440 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700441 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800442
443 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
444 // multiple times.
445 android::Region mVisibleRegion = android::Region::INVALID_REGION;
446 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500447 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700448 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700449 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700450 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800451 uint32_t mBufferSlot;
Dan Stoza651bf312015-10-23 17:03:17 -0700452};
453
Lloyd Pique35d58242018-12-18 16:33:25 -0800454} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700455} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700456} // namespace android