blob: 6740d8a8327370aff2ce69410316959a1ddf9bf1 [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>
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -060027#include <ui/PictureProfileHandle.h>
Yichi Chen8366f562019-03-25 19:44:06 +080028#include <ui/Region.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010029#include <ui/StaticDisplayInfo.h>
Dan Stoza651bf312015-10-23 17:03:17 -070030#include <utils/Log.h>
31#include <utils/StrongPointer.h>
32#include <utils/Timers.h>
33
34#include <functional>
35#include <string>
36#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070037#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070038#include <vector>
39
Alec Mouricdf16792021-12-10 13:16:06 -080040#include "ComposerHal.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070041#include "Hal.h"
42
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050043#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
Huihong Luo791bef92023-09-19 21:18:45 -070044#include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
Ady Abrahamde549d42022-01-26 19:19:17 -080045#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri30835f22022-03-18 00:58:26 +000046#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Ady Abraham6e60b142022-01-06 18:10:35 -080047#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050048#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050049#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Sally Qi0abc4a52024-09-26 16:13:06 -070050#include <aidl/android/hardware/graphics/composer3/Luts.h>
Sally Qi0cbd08b2022-08-17 12:12:28 -070051#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
ramindani12bfe6b2023-02-03 13:29:19 -080052#include <aidl/android/hardware/graphics/composer3/RefreshRateChangedDebugData.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050053
Dan Stoza651bf312015-10-23 17:03:17 -070054namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080055
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070056class Fence;
57class FloatRect;
58class GraphicBuffer;
59class TestableSurfaceFlinger;
60struct DisplayedFrameStats;
61
62namespace Hwc2 {
63class Composer;
64} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070065
66namespace HWC2 {
67
Dan Stoza651bf312015-10-23 17:03:17 -070068class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070069
70namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070071
Huihong Luo817ebb82023-12-13 12:54:24 -080072using aidl::android::hardware::drm::HdcpLevels;
Huihong Luo791bef92023-09-19 21:18:45 -070073using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
ramindani12bfe6b2023-02-03 13:29:19 -080074using aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
75
Steven Thomas94e35b92017-07-26 18:48:28 -070076// Implement this interface to receive hardware composer events.
77//
78// These callback functions will generally be called on a hwbinder thread, but
Huihong Luo791bef92023-09-19 21:18:45 -070079// when first registering the callback the onComposerHalHotplugEvent() function
80// will immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070081struct ComposerCallback {
Huihong Luo791bef92023-09-19 21:18:45 -070082 virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070083 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -040084 virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070085 std::optional<hal::VsyncPeriodNanos>) = 0;
86 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
87 const hal::VsyncPeriodChangeTimeline&) = 0;
88 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080089 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
ramindani12bfe6b2023-02-03 13:29:19 -080090 virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
Huihong Luo817ebb82023-12-13 12:54:24 -080091 virtual void onComposerHalHdcpLevelsChanged(hal::HWDisplayId, const HdcpLevels& levels) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070092
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070093protected:
94 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070095};
Dan Stoza651bf312015-10-23 17:03:17 -070096
Peiyong Line9d809e2020-04-14 13:10:48 -070097// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080098class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070099public:
Ana Krulec4593b692019-01-11 22:07:25 -0800100 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -0700101
Peiyong Line9d809e2020-04-14 13:10:48 -0700102 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800103 virtual bool isConnected() const = 0;
Florian Mayercffc3792022-03-01 23:50:09 +0000104 virtual void setConnected(bool connected) = 0; // For use by HWComposer only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500105 virtual bool hasCapability(
106 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -0700107 virtual bool isVsyncPeriodSwitchSupported() const = 0;
ramindani32cf0602022-03-02 02:30:29 +0000108 virtual bool hasDisplayIdleTimerCapability() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -0700109 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000110 virtual std::optional<ui::Size> getPhysicalSizeInMm() const = 0;
Dan Stoza651bf312015-10-23 17:03:17 -0700111
Sally Qi95f669a2024-08-27 11:31:42 -0700112 static const int kLutFileDescriptorMapperSize = 20;
Sally Qi0abc4a52024-09-26 16:13:06 -0700113 using LutOffsetAndProperties = std::vector<
114 std::pair<int32_t, aidl::android::hardware::graphics::composer3::LutProperties>>;
Sally Qi95f669a2024-08-27 11:31:42 -0700115 using LayerLuts =
Sally Qi0abc4a52024-09-26 16:13:06 -0700116 ftl::SmallMap<HWC2::Layer*, LutOffsetAndProperties, kLutFileDescriptorMapperSize>;
Sally Qi95f669a2024-08-27 11:31:42 -0700117 using LutFileDescriptorMapper =
118 ftl::SmallMap<HWC2::Layer*, ndk::ScopedFileDescriptor, kLutFileDescriptorMapperSize>;
119
ramindani86d3d982022-03-16 20:57:42 +0000120 [[nodiscard]] virtual hal::Error acceptChanges() = 0;
121 [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
Lloyd Piquea516c002021-05-07 14:36:58 -0700122 createLayer() = 0;
ramindani86d3d982022-03-16 20:57:42 +0000123 [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500124 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
125 outTypes) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000126 [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800127 // Returns a bitmask which contains HdrMetadata::Type::*.
ramindani86d3d982022-03-16 20:57:42 +0000128 [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
129 [[nodiscard]] virtual hal::Error getRenderIntents(
Peiyong Line9d809e2020-04-14 13:10:48 -0700130 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000131 [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
132 android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800133
ramindani86d3d982022-03-16 20:57:42 +0000134 [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
135 [[nodiscard]] virtual hal::Error getRequests(
Peiyong Line9d809e2020-04-14 13:10:48 -0700136 hal::DisplayRequest* outDisplayRequests,
137 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500138 [[nodiscard]] virtual ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType()
139 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000140 [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
141 [[nodiscard]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800142 android::HdrCapabilities* outCapabilities) const = 0;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700143 [[nodiscard]] virtual hal::Error getOverlaySupport(
144 aidl::android::hardware::graphics::composer3::OverlayProperties* outProperties)
145 const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000146 [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700147 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800148 uint8_t* outComponentMask) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000149 [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
150 uint8_t componentMask,
151 uint64_t maxFrames) const = 0;
152 [[nodiscard]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800153 uint64_t maxFrames, uint64_t timestamp,
154 android::DisplayedFrameStats* outStats) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000155 [[nodiscard]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800156 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000157 [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
158 [[nodiscard]] virtual hal::Error setClientTarget(
Ana Krulec4593b692019-01-11 22:07:25 -0800159 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Alec Mourif97df4d2023-09-06 02:10:05 +0000160 const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace,
161 float hdrSdrRatio) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000162 [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
163 hal::RenderIntent renderIntent) = 0;
164 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
165 [[nodiscard]] virtual hal::Error setOutputBuffer(
Ana Krulec4593b692019-01-11 22:07:25 -0800166 const android::sp<android::GraphicBuffer>& buffer,
167 const android::sp<android::Fence>& releaseFence) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000168 [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
169 [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
ramindani09acbb82023-11-03 09:02:38 -0700170 [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
171 uint32_t* outNumTypes, uint32_t* outNumRequests) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000172 [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700173 int32_t frameIntervalNs,
ramindani86d3d982022-03-16 20:57:42 +0000174 uint32_t* outNumTypes,
175 uint32_t* outNumRequests,
176 android::sp<android::Fence>* outPresentFence,
177 uint32_t* state) = 0;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700178 [[nodiscard]] virtual ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000179 float brightness, float brightnessNits,
180 const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000181 [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100182 hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700183 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000184 [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
185 [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
186 [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
Kriti Dang7defaf32021-11-15 11:55:43 +0100187 hal::HWConfigId* configId) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000188 [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
189 [[nodiscard]] virtual hal::Error getSupportedContentTypes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700190 std::vector<hal::ContentType>*) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000191 [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
192 [[nodiscard]] virtual hal::Error getClientTargetProperty(
Alec Mouri30835f22022-03-18 00:58:26 +0000193 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
194 outClientTargetProperty) = 0;
Sally Qi11dcd582024-08-16 18:11:27 -0700195 [[nodiscard]] virtual hal::Error getRequestedLuts(
Sally Qi95f669a2024-08-27 11:31:42 -0700196 LayerLuts* outLuts, LutFileDescriptorMapper& lutFileDescriptorMapper) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000197 [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500198 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
199 support) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000200 [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
201 [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
ramindani06e518e2022-03-14 18:47:53 +0000202 Hwc2::AidlTransform* outTransform) const = 0;
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -0600203 [[nodiscard]] virtual hal::Error getMaxLayerPictureProfiles(int32_t* maxProfiles) = 0;
204 [[nodiscard]] virtual hal::Error setPictureProfileHandle(
205 const PictureProfileHandle& handle) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800206};
207
208namespace impl {
209
Lloyd Piquea516c002021-05-07 14:36:58 -0700210class Layer;
211
Ana Krulec4593b692019-01-11 22:07:25 -0800212class Display : public HWC2::Display {
213public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800214 Display(android::Hwc2::Composer&,
215 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
216 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800217 ~Display() override;
218
219 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700220 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700221 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700222 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500223 std::unordered_map<HWC2::Layer*,
224 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
225 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700226 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800227 // Returns a bitmask which contains HdrMetadata::Type::*.
228 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700229 hal::Error getRenderIntents(hal::ColorMode colorMode,
230 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200231 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700232
Peiyong Line9d809e2020-04-14 13:10:48 -0700233 hal::Error getName(std::string* outName) const override;
234 hal::Error getRequests(
235 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700236 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500237 ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType() const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700238 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700239 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700240 hal::Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties*
241 outProperties) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700242 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
243 hal::Dataspace* outDataspace,
244 uint8_t* outComponentMask) const override;
245 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
246 uint64_t maxFrames) const override;
247 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
248 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700249 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
250 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700251 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700252 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
253 const android::sp<android::Fence>& acquireFence,
Alec Mourif97df4d2023-09-06 02:10:05 +0000254 hal::Dataspace dataspace, float hdrSdrRatio) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200255 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800256 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200257 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700258 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200259 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700260 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
ramindani09acbb82023-11-03 09:02:38 -0700261 hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
Ady Abraham43065bd2021-12-10 17:22:15 -0800262 uint32_t* outNumRequests) override;
ramindani4aac32c2023-10-30 14:13:30 -0700263 hal::Error presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
264 uint32_t* outNumTypes, uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700265 android::sp<android::Fence>* outPresentFence,
266 uint32_t* state) override;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700267 ftl::Future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000268 float brightness, float brightnessNits,
269 const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100270 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
271 const hal::VsyncPeriodChangeConstraints& constraints,
272 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100273 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
274 hal::Error clearBootDisplayConfig() override;
275 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700276 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700277 hal::Error getSupportedContentTypes(
278 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700279 hal::Error setContentType(hal::ContentType) override;
Alec Mouri30835f22022-03-18 00:58:26 +0000280 hal::Error getClientTargetProperty(
281 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
282 outClientTargetProperty) override;
Sally Qi95f669a2024-08-27 11:31:42 -0700283 hal::Error getRequestedLuts(LayerLuts* outLuts,
284 LutFileDescriptorMapper& lutFileDescriptorMapper) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500285 hal::Error getDisplayDecorationSupport(
286 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
287 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000288 hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -0600289 hal::Error getMaxLayerPictureProfiles(int32_t* maxProfiles) override;
290 hal::Error setPictureProfileHandle(const android::PictureProfileHandle& handle) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700291
Dan Stoza651bf312015-10-23 17:03:17 -0700292 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700293 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800294 bool isConnected() const override { return mIsConnected; }
Florian Mayercffc3792022-03-01 23:50:09 +0000295 void setConnected(bool connected) override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500296 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
297 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700298 bool isVsyncPeriodSwitchSupported() const override;
ramindani32cf0602022-03-02 02:30:29 +0000299 bool hasDisplayIdleTimerCapability() const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700300 void onLayerDestroyed(hal::HWLayerId layerId) override;
ramindani06e518e2022-03-14 18:47:53 +0000301 hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000302 void setPhysicalSizeInMm(std::optional<ui::Size> size);
303 std::optional<ui::Size> getPhysicalSizeInMm() const override { return mPhysicalSize; }
Dan Stoza651bf312015-10-23 17:03:17 -0700304
305private:
HyunKyounga264a352024-04-24 18:51:33 +0900306 void loadDisplayCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700307
Dan Stoza651bf312015-10-23 17:03:17 -0700308 // This may fail (and return a null pointer) if no layer with this ID exists
309 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700310 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700311
Lloyd Piquebc792092018-01-17 11:52:30 -0800312 friend android::TestableSurfaceFlinger;
313
Dan Stoza651bf312015-10-23 17:03:17 -0700314 // Member variables
315
Florian Mayercffc3792022-03-01 23:50:09 +0000316 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700317 // this HWC2::Display, so these references are guaranteed to be valid for
318 // the lifetime of this object.
319 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800320 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
321 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700322
Peiyong Line9d809e2020-04-14 13:10:48 -0700323 const hal::HWDisplayId mId;
324 hal::DisplayType mType;
Dominik Laskowski969cdcb2024-02-08 16:35:29 -0500325 // Cached on first call to getConnectionType.
326 mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800327 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700328
Lloyd Piquea516c002021-05-07 14:36:58 -0700329 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
330 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700331
Ady Abraham27fbcc72021-09-20 14:54:57 -0700332 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700333 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500334 std::optional<
335 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
336 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Lucas Berthou8d0a0c42024-08-27 14:32:31 +0000337 // Physical size in mm.
338 std::optional<ui::Size> mPhysicalSize;
Dan Stoza651bf312015-10-23 17:03:17 -0700339};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800340
Ana Krulec4593b692019-01-11 22:07:25 -0800341} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700342
Lloyd Pique35d58242018-12-18 16:33:25 -0800343class Layer {
344public:
345 virtual ~Layer();
346
Peiyong Line9d809e2020-04-14 13:10:48 -0700347 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800348
ramindani86d3d982022-03-16 20:57:42 +0000349 [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
350 [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
351 const android::sp<android::GraphicBuffer>& buffer,
352 const android::sp<android::Fence>& acquireFence) = 0;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700353 [[nodiscard]] virtual hal::Error setBufferSlotsToClear(
354 const std::vector<uint32_t>& slotsToClear, uint32_t activeBufferSlot) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000355 [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800356
ramindani86d3d982022-03-16 20:57:42 +0000357 [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
358 [[nodiscard]] virtual hal::Error setColor(
Ady Abraham6e60b142022-01-06 18:10:35 -0800359 aidl::android::hardware::graphics::composer3::Color color) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000360 [[nodiscard]] virtual hal::Error setCompositionType(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500361 aidl::android::hardware::graphics::composer3::Composition type) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000362 [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
363 [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
364 const android::HdrMetadata& metadata) = 0;
365 [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
366 [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
367 [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
368 [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
369 [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
370 [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
371 [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800372
373 // Composer HAL 2.3
ramindani86d3d982022-03-16 20:57:42 +0000374 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800375
376 // Composer HAL 2.4
ramindani86d3d982022-03-16 20:57:42 +0000377 [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
378 bool mandatory,
379 const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700380
381 // AIDL HAL
ramindani86d3d982022-03-16 20:57:42 +0000382 [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
383 [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
Sally Qi11dcd582024-08-16 18:11:27 -0700384 [[nodiscard]] virtual hal::Error setLuts(
Sally Qi0abc4a52024-09-26 16:13:06 -0700385 aidl::android::hardware::graphics::composer3::Luts& luts) = 0;
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -0600386 [[nodiscard]] virtual hal::Error setPictureProfileHandle(
387 const PictureProfileHandle& handle) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800388};
389
390namespace impl {
391
Peiyong Line9d809e2020-04-14 13:10:48 -0700392// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800393
394class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700395public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700396 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800397 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
398 capabilities,
399 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800400 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700401
Lloyd Piquea516c002021-05-07 14:36:58 -0700402 void onOwningDisplayDestroyed();
403
Peiyong Line9d809e2020-04-14 13:10:48 -0700404 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700405
Peiyong Line9d809e2020-04-14 13:10:48 -0700406 hal::Error setCursorPosition(int32_t x, int32_t y) override;
407 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
408 const android::sp<android::Fence>& acquireFence) override;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700409 hal::Error setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
410 uint32_t activeBufferSlot) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700411 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700412
Peiyong Line9d809e2020-04-14 13:10:48 -0700413 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800414 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500415 hal::Error setCompositionType(
416 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700417 hal::Error setDataspace(hal::Dataspace dataspace) override;
418 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
419 const android::HdrMetadata& metadata) override;
420 hal::Error setDisplayFrame(const android::Rect& frame) override;
421 hal::Error setPlaneAlpha(float alpha) override;
422 hal::Error setSidebandStream(const native_handle_t* stream) override;
423 hal::Error setSourceCrop(const android::FloatRect& crop) override;
424 hal::Error setTransform(hal::Transform transform) override;
425 hal::Error setVisibleRegion(const android::Region& region) override;
426 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700427
Peiyong Lin698147a2018-09-14 13:27:18 -0700428 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700429 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700430
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800431 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700432 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
433 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800434
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700435 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800436 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500437 hal::Error setBlockingRegion(const android::Region& region) override;
Sally Qi0abc4a52024-09-26 16:13:06 -0700438 hal::Error setLuts(aidl::android::hardware::graphics::composer3::Luts&) override;
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -0600439 hal::Error setPictureProfileHandle(const PictureProfileHandle& handle) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700440
Dan Stoza651bf312015-10-23 17:03:17 -0700441private:
Florian Mayercffc3792022-03-01 23:50:09 +0000442 // These are references to data owned by HWComposer, which will outlive
Steven Thomas94e35b92017-07-26 18:48:28 -0700443 // this HWC2::Layer, so these references are guaranteed to be valid for
444 // the lifetime of this object.
445 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800446 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
447 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700448
Lloyd Piquea516c002021-05-07 14:36:58 -0700449 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700450 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800451
452 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
453 // multiple times.
454 android::Region mVisibleRegion = android::Region::INVALID_REGION;
455 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500456 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700457 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700458 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700459 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800460 uint32_t mBufferSlot;
Brian Lindahl7a4cb7e2024-10-30 10:42:21 -0600461 android::PictureProfileHandle profile;
Dan Stoza651bf312015-10-23 17:03:17 -0700462};
463
Lloyd Pique35d58242018-12-18 16:33:25 -0800464} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700465} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700466} // namespace android