blob: 5b948318aeff07b593ed574b3860ba3fd435d426 [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>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070023#include <gui/HdrMetadata.h>
Mathias Agopian1d77b712017-02-17 15:46:13 -080024#include <math/mat4.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070025#include <ui/HdrCapabilities.h>
Yichi Chen8366f562019-03-25 19:44:06 +080026#include <ui/Region.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010027#include <ui/StaticDisplayInfo.h>
Dan Stoza651bf312015-10-23 17:03:17 -070028#include <utils/Log.h>
29#include <utils/StrongPointer.h>
30#include <utils/Timers.h>
31
32#include <functional>
33#include <string>
34#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070035#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070036#include <vector>
37
Alec Mouricdf16792021-12-10 13:16:06 -080038#include "ComposerHal.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070039#include "Hal.h"
40
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050041#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
Huihong Luo791bef92023-09-19 21:18:45 -070042#include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
Ady Abrahamde549d42022-01-26 19:19:17 -080043#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri30835f22022-03-18 00:58:26 +000044#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Ady Abraham6e60b142022-01-06 18:10:35 -080045#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050046#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050047#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Sally Qi0cbd08b2022-08-17 12:12:28 -070048#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
ramindani12bfe6b2023-02-03 13:29:19 -080049#include <aidl/android/hardware/graphics/composer3/RefreshRateChangedDebugData.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050050
Dan Stoza651bf312015-10-23 17:03:17 -070051namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080052
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070053class Fence;
54class FloatRect;
55class GraphicBuffer;
56class TestableSurfaceFlinger;
57struct DisplayedFrameStats;
58
59namespace Hwc2 {
60class Composer;
61} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070062
63namespace HWC2 {
64
Dan Stoza651bf312015-10-23 17:03:17 -070065class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070066
67namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070068
Huihong Luo791bef92023-09-19 21:18:45 -070069using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
ramindani12bfe6b2023-02-03 13:29:19 -080070using aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
71
Steven Thomas94e35b92017-07-26 18:48:28 -070072// Implement this interface to receive hardware composer events.
73//
74// These callback functions will generally be called on a hwbinder thread, but
Huihong Luo791bef92023-09-19 21:18:45 -070075// when first registering the callback the onComposerHalHotplugEvent() function
76// will immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070077struct ComposerCallback {
Huihong Luo791bef92023-09-19 21:18:45 -070078 virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070079 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -040080 virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070081 std::optional<hal::VsyncPeriodNanos>) = 0;
82 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
83 const hal::VsyncPeriodChangeTimeline&) = 0;
84 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080085 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
ramindani12bfe6b2023-02-03 13:29:19 -080086 virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070087
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070088protected:
89 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070090};
Dan Stoza651bf312015-10-23 17:03:17 -070091
Peiyong Line9d809e2020-04-14 13:10:48 -070092// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080093class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070094public:
Ana Krulec4593b692019-01-11 22:07:25 -080095 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -070096
Peiyong Line9d809e2020-04-14 13:10:48 -070097 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -080098 virtual bool isConnected() const = 0;
Florian Mayercffc3792022-03-01 23:50:09 +000099 virtual void setConnected(bool connected) = 0; // For use by HWComposer only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500100 virtual bool hasCapability(
101 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -0700102 virtual bool isVsyncPeriodSwitchSupported() const = 0;
ramindani32cf0602022-03-02 02:30:29 +0000103 virtual bool hasDisplayIdleTimerCapability() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -0700104 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Dan Stoza651bf312015-10-23 17:03:17 -0700105
ramindani86d3d982022-03-16 20:57:42 +0000106 [[nodiscard]] virtual hal::Error acceptChanges() = 0;
107 [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
Lloyd Piquea516c002021-05-07 14:36:58 -0700108 createLayer() = 0;
ramindani86d3d982022-03-16 20:57:42 +0000109 [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500110 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
111 outTypes) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000112 [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800113 // Returns a bitmask which contains HdrMetadata::Type::*.
ramindani86d3d982022-03-16 20:57:42 +0000114 [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
115 [[nodiscard]] virtual hal::Error getRenderIntents(
Peiyong Line9d809e2020-04-14 13:10:48 -0700116 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000117 [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
118 android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800119
ramindani86d3d982022-03-16 20:57:42 +0000120 [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
121 [[nodiscard]] virtual hal::Error getRequests(
Peiyong Line9d809e2020-04-14 13:10:48 -0700122 hal::DisplayRequest* outDisplayRequests,
123 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500124 [[nodiscard]] virtual ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType()
125 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000126 [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
127 [[nodiscard]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800128 android::HdrCapabilities* outCapabilities) const = 0;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700129 [[nodiscard]] virtual hal::Error getOverlaySupport(
130 aidl::android::hardware::graphics::composer3::OverlayProperties* outProperties)
131 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000132 [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700133 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800134 uint8_t* outComponentMask) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000135 [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
136 uint8_t componentMask,
137 uint64_t maxFrames) const = 0;
138 [[nodiscard]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800139 uint64_t maxFrames, uint64_t timestamp,
140 android::DisplayedFrameStats* outStats) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000141 [[nodiscard]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800142 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000143 [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
144 [[nodiscard]] virtual hal::Error setClientTarget(
Ana Krulec4593b692019-01-11 22:07:25 -0800145 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Alec Mourif97df4d2023-09-06 02:10:05 +0000146 const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace,
147 float hdrSdrRatio) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000148 [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
149 hal::RenderIntent renderIntent) = 0;
150 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
151 [[nodiscard]] virtual hal::Error setOutputBuffer(
Ana Krulec4593b692019-01-11 22:07:25 -0800152 const android::sp<android::GraphicBuffer>& buffer,
153 const android::sp<android::Fence>& releaseFence) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000154 [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
155 [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
ramindani09acbb82023-11-03 09:02:38 -0700156 [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
157 uint32_t* outNumTypes, uint32_t* outNumRequests) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000158 [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700159 int32_t frameIntervalNs,
ramindani86d3d982022-03-16 20:57:42 +0000160 uint32_t* outNumTypes,
161 uint32_t* outNumRequests,
162 android::sp<android::Fence>* outPresentFence,
163 uint32_t* state) = 0;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700164 [[nodiscard]] virtual ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000165 float brightness, float brightnessNits,
166 const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000167 [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100168 hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700169 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000170 [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
171 [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
172 [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
Kriti Dang7defaf32021-11-15 11:55:43 +0100173 hal::HWConfigId* configId) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000174 [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
175 [[nodiscard]] virtual hal::Error getSupportedContentTypes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700176 std::vector<hal::ContentType>*) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000177 [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
178 [[nodiscard]] virtual hal::Error getClientTargetProperty(
Alec Mouri30835f22022-03-18 00:58:26 +0000179 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
180 outClientTargetProperty) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000181 [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500182 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
183 support) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000184 [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
185 [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
ramindani06e518e2022-03-14 18:47:53 +0000186 Hwc2::AidlTransform* outTransform) const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800187};
188
189namespace impl {
190
Lloyd Piquea516c002021-05-07 14:36:58 -0700191class Layer;
192
Ana Krulec4593b692019-01-11 22:07:25 -0800193class Display : public HWC2::Display {
194public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800195 Display(android::Hwc2::Composer&,
196 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
197 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800198 ~Display() override;
199
200 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700201 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700202 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700203 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500204 std::unordered_map<HWC2::Layer*,
205 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
206 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700207 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800208 // Returns a bitmask which contains HdrMetadata::Type::*.
209 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700210 hal::Error getRenderIntents(hal::ColorMode colorMode,
211 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200212 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700213
Peiyong Line9d809e2020-04-14 13:10:48 -0700214 hal::Error getName(std::string* outName) const override;
215 hal::Error getRequests(
216 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700217 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500218 ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType() const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700219 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700220 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700221 hal::Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties*
222 outProperties) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700223 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
224 hal::Dataspace* outDataspace,
225 uint8_t* outComponentMask) const override;
226 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
227 uint64_t maxFrames) const override;
228 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
229 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700230 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
231 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700232 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700233 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
234 const android::sp<android::Fence>& acquireFence,
Alec Mourif97df4d2023-09-06 02:10:05 +0000235 hal::Dataspace dataspace, float hdrSdrRatio) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200236 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800237 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200238 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700239 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200240 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700241 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
ramindani09acbb82023-11-03 09:02:38 -0700242 hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
Ady Abraham43065bd2021-12-10 17:22:15 -0800243 uint32_t* outNumRequests) override;
ramindani4aac32c2023-10-30 14:13:30 -0700244 hal::Error presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
245 uint32_t* outNumTypes, uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700246 android::sp<android::Fence>* outPresentFence,
247 uint32_t* state) override;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700248 ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000249 float brightness, float brightnessNits,
250 const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100251 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
252 const hal::VsyncPeriodChangeConstraints& constraints,
253 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100254 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
255 hal::Error clearBootDisplayConfig() override;
256 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700257 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700258 hal::Error getSupportedContentTypes(
259 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700260 hal::Error setContentType(hal::ContentType) override;
Alec Mouri30835f22022-03-18 00:58:26 +0000261 hal::Error getClientTargetProperty(
262 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
263 outClientTargetProperty) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500264 hal::Error getDisplayDecorationSupport(
265 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
266 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000267 hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700268
Dan Stoza651bf312015-10-23 17:03:17 -0700269 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700270 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800271 bool isConnected() const override { return mIsConnected; }
Florian Mayercffc3792022-03-01 23:50:09 +0000272 void setConnected(bool connected) override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500273 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
274 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700275 bool isVsyncPeriodSwitchSupported() const override;
ramindani32cf0602022-03-02 02:30:29 +0000276 bool hasDisplayIdleTimerCapability() const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700277 void onLayerDestroyed(hal::HWLayerId layerId) override;
ramindani06e518e2022-03-14 18:47:53 +0000278 hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
Dan Stoza651bf312015-10-23 17:03:17 -0700279
280private:
HyunKyounga264a352024-04-24 18:51:33 +0900281 void loadDisplayCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700282
Dan Stoza651bf312015-10-23 17:03:17 -0700283 // This may fail (and return a null pointer) if no layer with this ID exists
284 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700285 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700286
Lloyd Piquebc792092018-01-17 11:52:30 -0800287 friend android::TestableSurfaceFlinger;
288
Dan Stoza651bf312015-10-23 17:03:17 -0700289 // Member variables
290
Florian Mayercffc3792022-03-01 23:50:09 +0000291 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 // this HWC2::Display, so these references are guaranteed to be valid for
293 // the lifetime of this object.
294 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800295 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
296 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700297
Peiyong Line9d809e2020-04-14 13:10:48 -0700298 const hal::HWDisplayId mId;
299 hal::DisplayType mType;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500300 // Cached on first call to getConnectionType.
301 mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800302 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700303
Lloyd Piquea516c002021-05-07 14:36:58 -0700304 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
305 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700306
Ady Abraham27fbcc72021-09-20 14:54:57 -0700307 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700308 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500309 std::optional<
310 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
311 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Dan Stoza651bf312015-10-23 17:03:17 -0700312};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800313
Ana Krulec4593b692019-01-11 22:07:25 -0800314} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700315
Lloyd Pique35d58242018-12-18 16:33:25 -0800316class Layer {
317public:
318 virtual ~Layer();
319
Peiyong Line9d809e2020-04-14 13:10:48 -0700320 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800321
ramindani86d3d982022-03-16 20:57:42 +0000322 [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
323 [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
324 const android::sp<android::GraphicBuffer>& buffer,
325 const android::sp<android::Fence>& acquireFence) = 0;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700326 [[nodiscard]] virtual hal::Error setBufferSlotsToClear(
327 const std::vector<uint32_t>& slotsToClear, uint32_t activeBufferSlot) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000328 [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800329
ramindani86d3d982022-03-16 20:57:42 +0000330 [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
331 [[nodiscard]] virtual hal::Error setColor(
Ady Abraham6e60b142022-01-06 18:10:35 -0800332 aidl::android::hardware::graphics::composer3::Color color) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000333 [[nodiscard]] virtual hal::Error setCompositionType(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500334 aidl::android::hardware::graphics::composer3::Composition type) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000335 [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
336 [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
337 const android::HdrMetadata& metadata) = 0;
338 [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
339 [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
340 [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
341 [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
342 [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
343 [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
344 [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800345
346 // Composer HAL 2.3
ramindani86d3d982022-03-16 20:57:42 +0000347 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800348
349 // Composer HAL 2.4
ramindani86d3d982022-03-16 20:57:42 +0000350 [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
351 bool mandatory,
352 const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700353
354 // AIDL HAL
ramindani86d3d982022-03-16 20:57:42 +0000355 [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
356 [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800357};
358
359namespace impl {
360
Peiyong Line9d809e2020-04-14 13:10:48 -0700361// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800362
363class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700364public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700365 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800366 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
367 capabilities,
368 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800369 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700370
Lloyd Piquea516c002021-05-07 14:36:58 -0700371 void onOwningDisplayDestroyed();
372
Peiyong Line9d809e2020-04-14 13:10:48 -0700373 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700374
Peiyong Line9d809e2020-04-14 13:10:48 -0700375 hal::Error setCursorPosition(int32_t x, int32_t y) override;
376 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
377 const android::sp<android::Fence>& acquireFence) override;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700378 hal::Error setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
379 uint32_t activeBufferSlot) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700380 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700381
Peiyong Line9d809e2020-04-14 13:10:48 -0700382 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800383 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500384 hal::Error setCompositionType(
385 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700386 hal::Error setDataspace(hal::Dataspace dataspace) override;
387 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
388 const android::HdrMetadata& metadata) override;
389 hal::Error setDisplayFrame(const android::Rect& frame) override;
390 hal::Error setPlaneAlpha(float alpha) override;
391 hal::Error setSidebandStream(const native_handle_t* stream) override;
392 hal::Error setSourceCrop(const android::FloatRect& crop) override;
393 hal::Error setTransform(hal::Transform transform) override;
394 hal::Error setVisibleRegion(const android::Region& region) override;
395 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700396
Peiyong Lin698147a2018-09-14 13:27:18 -0700397 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700398 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700399
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800400 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700401 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
402 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800403
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700404 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800405 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500406 hal::Error setBlockingRegion(const android::Region& region) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700407
Dan Stoza651bf312015-10-23 17:03:17 -0700408private:
Florian Mayercffc3792022-03-01 23:50:09 +0000409 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700410 // this HWC2::Layer, so these references are guaranteed to be valid for
411 // the lifetime of this object.
412 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800413 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
414 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700415
Lloyd Piquea516c002021-05-07 14:36:58 -0700416 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700417 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800418
419 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
420 // multiple times.
421 android::Region mVisibleRegion = android::Region::INVALID_REGION;
422 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500423 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700424 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700425 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700426 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800427 uint32_t mBufferSlot;
Dan Stoza651bf312015-10-23 17:03:17 -0700428};
429
Lloyd Pique35d58242018-12-18 16:33:25 -0800430} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700431} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700432} // namespace android