blob: acebfb2362f3f7b782c8ec01add6af79d3b0abaf [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowskif8db0f02021-04-19 11:05:25 -070017#pragma once
Mathias Agopiana350ff92010-08-10 17:14:02 -070018
Dominik Laskowski1af47932018-11-12 10:20:46 -080019#include <cstdint>
Dan Stoza9e56aa02015-11-02 13:00:03 -080020#include <memory>
Dominik Laskowski1af47932018-11-12 10:20:46 -080021#include <mutex>
Steven Thomas6e8f7062017-11-22 14:15:29 -080022#include <optional>
Dominik Laskowski075d3172018-05-24 15:50:06 -070023#include <unordered_map>
24#include <unordered_set>
Dan Stoza9e56aa02015-11-02 13:00:03 -080025#include <vector>
26
Dominik Laskowski1af47932018-11-12 10:20:46 -080027#include <android-base/thread_annotations.h>
Dominik Laskowskib17c6212022-05-09 09:36:19 -070028#include <ftl/future.h>
Alec Mouriff793872022-01-13 17:45:06 -080029#include <ui/DisplayIdentification.h>
Ady Abrahamec7aa8a2021-06-28 12:37:09 -070030#include <ui/FenceTime.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080031
32// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010035#pragma clang diagnostic ignored "-Wextra"
Dominik Laskowski1af47932018-11-12 10:20:46 -080036#include <ui/GraphicTypes.h>
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010037#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
Ady Abraham8a82ba62020-01-17 12:43:17 -080038
Dominik Laskowski1af47932018-11-12 10:20:46 -080039#include <utils/StrongPointer.h>
40#include <utils/Timers.h>
41
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010042#include "DisplayMode.h"
Dominik Laskowski1af47932018-11-12 10:20:46 -080043#include "HWC2.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070044#include "Hal.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080045
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050046#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
Kriti Dang674b9372022-11-18 10:58:44 +010047#include <aidl/android/hardware/graphics/common/HdrConversionCapability.h>
48#include <aidl/android/hardware/graphics/common/HdrConversionStrategy.h>
Ady Abrahamde549d42022-01-26 19:19:17 -080049#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri85065692022-03-18 00:58:26 +000050#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050051#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050052#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Sally Qi0cbd08b2022-08-17 12:12:28 -070053#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050054
Mathias Agopiana350ff92010-08-10 17:14:02 -070055namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070056
Peiyong Line9d809e2020-04-14 13:10:48 -070057namespace hal = hardware::graphics::composer::hal;
58
Kevin DuBois1d4249a2018-08-29 10:45:14 -070059struct DisplayedFrameStats;
Jesse Hall399184a2014-03-03 15:42:54 -080060class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080061class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080062struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070063
Lloyd Piquea822d522017-12-20 16:42:57 -080064namespace Hwc2 {
65class Composer;
66} // namespace Hwc2
67
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068namespace compositionengine {
69class Output;
70} // namespace compositionengine
71
Lloyd Pique4603f3c2020-02-11 12:06:56 -080072struct KnownHWCGenericLayerMetadata {
73 const char* name;
74 const uint32_t id;
75};
76
Marin Shalamanov1c434292020-06-12 01:47:29 +020077// See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
78// this class.
Lloyd Pique441d5042018-10-18 16:49:51 -070079class HWComposer {
80public:
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070081 struct DeviceRequestedChanges {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050082 using ChangedTypes =
83 std::unordered_map<HWC2::Layer*,
84 aidl::android::hardware::graphics::composer3::Composition>;
Alec Mouri85065692022-03-18 00:58:26 +000085 using ClientTargetProperty =
86 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070087 using DisplayRequests = hal::DisplayRequest;
88 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
89
90 ChangedTypes changedTypes;
91 DisplayRequests displayRequests;
92 LayerRequests layerRequests;
93 ClientTargetProperty clientTargetProperty;
94 };
95
Marin Shalamanov045b7002021-01-07 16:56:24 +010096 struct HWCDisplayMode {
97 hal::HWConfigId hwcId;
98 int32_t width = -1;
99 int32_t height = -1;
100 nsecs_t vsyncPeriod = -1;
101 int32_t dpiX = -1;
102 int32_t dpiY = -1;
103 int32_t configGroup = -1;
Marin Shalamanovd3b5c5d2021-02-11 18:26:14 +0100104
105 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
106 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
107 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
108 << mode.dpiY << " group=" << mode.configGroup;
109 }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100110 };
111
Lloyd Pique441d5042018-10-18 16:49:51 -0700112 virtual ~HWComposer();
113
Yichi Chen3401b562022-01-17 15:42:35 +0800114 virtual void setCallback(HWC2::ComposerCallback&) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700115
Marin Shalamanova524a092020-07-27 21:39:55 +0200116 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700117 DisplayIdentificationData* outData) const = 0;
118
Ady Abrahamde549d42022-01-26 19:19:17 -0800119 virtual bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const = 0;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500120 virtual bool hasDisplayCapability(
121 HalDisplayId,
122 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700123
Dominik Laskowski13948602021-03-08 20:48:28 -0800124 virtual size_t getMaxVirtualDisplayCount() const = 0;
125 virtual size_t getMaxVirtualDisplayDimension() const = 0;
126
127 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
128 // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
Dominik Laskowski263eec42021-07-21 23:13:24 -0700129 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700130
Marin Shalamanova524a092020-07-27 21:39:55 +0200131 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100132
Lloyd Pique441d5042018-10-18 16:49:51 -0700133 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700134 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700135
Lloyd Pique66d68602019-02-13 14:23:31 -0800136 // Gets any required composition change requests from the HWC device.
137 //
138 // Note that frameUsesClientComposition must be set correctly based on
139 // whether the current frame appears to use client composition. If it is
140 // false some internal optimizations are allowed to present the display
141 // with fewer handshakes, but this does not work if client composition is
142 // expected.
143 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200144 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700145 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800146 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800147 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700148
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200149 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200150 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700151
152 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700153 virtual status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700154 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
155 const std::shared_ptr<FenceTime>& previousPresentFence) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700156
157 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200158 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700159
Lloyd Pique441d5042018-10-18 16:49:51 -0700160 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200161 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700162
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200163 // reset state when a display is disconnected
164 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700165
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400166 // Get the present fence/timestamp received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200167 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400168 virtual nsecs_t getPresentTimestamp(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700169
170 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200171 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700172
173 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200174 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700175 const sp<GraphicBuffer>& buffer) = 0;
176
177 // After SurfaceFlinger has retrieved the release fences for all the frames,
178 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200179 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700180
181 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200182 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700183
Sally Qibb866c12022-10-17 11:31:20 -0700184 virtual const aidl::android::hardware::graphics::composer3::OverlayProperties&
185 getOverlaySupport() const = 0;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700186
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200187 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700188
189 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700191
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200192 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700193
194 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200195 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700196 ui::Dataspace* outDataspace,
197 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200198 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700199 uint8_t componentMask,
200 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200201 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700202 DisplayedFrameStats* outStats) = 0;
203
Dan Gittik57e63c52019-01-18 16:37:54 +0000204 // Sets the brightness of a display.
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700205 virtual ftl::Future<status_t> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000206 PhysicalDisplayId, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -0800207 const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000208
Matt Buckley50c44062022-01-17 20:48:10 +0000209 // Get whether the display skipped validation on the latest present
210 virtual bool getValidateSkipped(HalDisplayId displayId) const = 0;
211
Lloyd Pique441d5042018-10-18 16:49:51 -0700212 // Events handling ---------------------------------------------------------
213
214 // Returns stable display ID (and display name on connection of new or previously disconnected
215 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100216 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200217 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
218 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700219
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200220 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
221 // TODO(b/157555476): Remove when the framework has proper support for headless mode
222 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
223
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500224 // Called when a vsync happens. If the vsync is valid, returns the
225 // corresponding PhysicalDisplayId. Otherwise returns nullopt.
226 virtual std::optional<PhysicalDisplayId> onVsync(hal::HWDisplayId, nsecs_t timestamp) = 0;
227
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200228 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700229
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200230 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700231
Marin Shalamanov045b7002021-01-07 16:56:24 +0100232 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700233
Marin Shalamanov045b7002021-01-07 16:56:24 +0100234 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700235
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200236 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700237
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200238 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
239 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700240
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800241 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100242 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200243 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100244 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
245 nsecs_t* outVsyncPeriod) const = 0;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100246 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100247 const hal::VsyncPeriodChangeConstraints&,
248 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200249 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100250 virtual status_t getSupportedContentTypes(
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800251 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) const = 0;
252
253 bool supportsContentType(PhysicalDisplayId displayId, hal::ContentType type) const {
254 std::vector<hal::ContentType> types;
255 return getSupportedContentTypes(displayId, &types) == NO_ERROR &&
256 std::find(types.begin(), types.end(), type) != types.end();
257 }
258
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200259 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800260
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800261 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
262 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800263
Lloyd Pique441d5042018-10-18 16:49:51 -0700264 virtual void dump(std::string& out) const = 0;
265
266 virtual Hwc2::Composer* getComposer() const = 0;
267
Marin Shalamanov8b196592021-08-09 16:24:42 +0200268 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
269 // which in practice is immediately after HWComposer construction, must occur before any
270 // call to this function.
271 // The primary display can be temporarily disconnected from the perspective
272 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
273 // if isHeadless().
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700274 //
275 // TODO(b/182939859): Remove special cases for primary display.
276 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
277 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
278 virtual bool isHeadless() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700279
Marin Shalamanova524a092020-07-27 21:39:55 +0200280 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
281 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Kriti Dang7defaf32021-11-15 11:55:43 +0100282
283 // Composer 3.0
Kriti Dang7defaf32021-11-15 11:55:43 +0100284 virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0;
285 virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0;
Kriti Dang16ca2972022-02-01 20:07:03 +0100286 virtual std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) = 0;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500287 virtual status_t getDisplayDecorationSupport(
288 PhysicalDisplayId,
289 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
290 support) = 0;
ramindani32cf0602022-03-02 02:30:29 +0000291 virtual status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) = 0;
ramindani06e518e2022-03-14 18:47:53 +0000292 virtual bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const = 0;
293 virtual Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const = 0;
Kriti Dang674b9372022-11-18 10:58:44 +0100294 virtual std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
295 getHdrConversionCapabilities() const = 0;
296 virtual status_t setHdrConversionStrategy(
297 aidl::android::hardware::graphics::common::HdrConversionStrategy) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700298};
299
Vishnu Naira3140382022-02-24 14:07:11 -0800300static inline bool operator==(const android::HWComposer::DeviceRequestedChanges& lhs,
301 const android::HWComposer::DeviceRequestedChanges& rhs) {
302 return lhs.changedTypes == rhs.changedTypes && lhs.displayRequests == rhs.displayRequests &&
303 lhs.layerRequests == rhs.layerRequests &&
304 lhs.clientTargetProperty == rhs.clientTargetProperty;
305}
306
Lloyd Pique441d5042018-10-18 16:49:51 -0700307namespace impl {
308
309class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700310public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800311 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800312 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700313
Lloyd Pique441d5042018-10-18 16:49:51 -0700314 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700315
Yichi Chen3401b562022-01-17 15:42:35 +0800316 void setCallback(HWC2::ComposerCallback&) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700317
Marin Shalamanova524a092020-07-27 21:39:55 +0200318 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700319 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700320
Ady Abrahamde549d42022-01-26 19:19:17 -0800321 bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500322 bool hasDisplayCapability(
323 HalDisplayId,
324 aidl::android::hardware::graphics::composer3::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700325
Dominik Laskowski13948602021-03-08 20:48:28 -0800326 size_t getMaxVirtualDisplayCount() const override;
327 size_t getMaxVirtualDisplayDimension() const override;
328
Dominik Laskowski263eec42021-07-21 23:13:24 -0700329 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700330
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100331 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200332 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100333
Dan Stoza9e56aa02015-11-02 13:00:03 -0800334 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700335 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700336
Lloyd Pique66d68602019-02-13 14:23:31 -0800337 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200338 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700339 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800340 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800341 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700342
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200343 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200344 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800345
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800346 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700347 status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700348 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
349 const std::shared_ptr<FenceTime>& previousPresentFence) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700350
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700351 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200352 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700353
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700354 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200355 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700356
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200357 // reset state when a display is disconnected
358 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700359
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400360 // Get the present fence/timestamp received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200361 sp<Fence> getPresentFence(HalDisplayId) const override;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400362 nsecs_t getPresentTimestamp(PhysicalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700363
Dan Stoza9e56aa02015-11-02 13:00:03 -0800364 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200365 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700366
Jesse Hall851cfe82013-03-20 13:44:00 -0700367 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200368 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700369 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700370
Dan Stoza9e56aa02015-11-02 13:00:03 -0800371 // After SurfaceFlinger has retrieved the release fences for all the frames,
372 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200373 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700374
Peiyong Lin62665892018-04-16 11:07:44 -0700375 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200376 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700377
Sally Qibb866c12022-10-17 11:31:20 -0700378 const aidl::android::hardware::graphics::composer3::OverlayProperties& getOverlaySupport()
379 const override;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700380
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200381 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700382
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700383 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200384 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700385
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200386 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700387
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700388 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200389 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700390 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700391 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200392 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200393 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200394 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700395 DisplayedFrameStats* outStats) override;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700396 ftl::Future<status_t> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000397 PhysicalDisplayId, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -0800398 const Hwc2::Composer::DisplayBrightnessOptions&) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700399
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700400 // Events handling ---------------------------------------------------------
401
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200402 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700403 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200404 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700405
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200406 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
407
Leon Scroggins III959a7ff2023-02-07 11:24:25 -0500408 std::optional<PhysicalDisplayId> onVsync(hal::HWDisplayId, nsecs_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200409 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700410
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200411 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700412
Marin Shalamanov045b7002021-01-07 16:56:24 +0100413 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700414
Marin Shalamanov045b7002021-01-07 16:56:24 +0100415 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700416
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200417 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700418
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200419 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600420
Matt Buckley50c44062022-01-17 20:48:10 +0000421 bool getValidateSkipped(HalDisplayId displayId) const override;
422
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800423 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100424 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200425 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100426 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
427 nsecs_t* outVsyncPeriod) const override;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100428 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100429 const hal::VsyncPeriodChangeConstraints&,
430 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200431 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800432 status_t getSupportedContentTypes(PhysicalDisplayId,
433 std::vector<hal::ContentType>*) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200434 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800435
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800436 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
437
Kriti Dang7defaf32021-11-15 11:55:43 +0100438 // Composer 3.0
Kriti Dang7defaf32021-11-15 11:55:43 +0100439 status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override;
440 status_t clearBootDisplayMode(PhysicalDisplayId) override;
Kriti Dang16ca2972022-02-01 20:07:03 +0100441 std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500442 status_t getDisplayDecorationSupport(
443 PhysicalDisplayId,
444 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
445 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000446 status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) override;
ramindani06e518e2022-03-14 18:47:53 +0000447 bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const override;
448 Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const override;
Kriti Dang674b9372022-11-18 10:58:44 +0100449 std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
450 getHdrConversionCapabilities() const override;
451 status_t setHdrConversionStrategy(
452 aidl::android::hardware::graphics::common::HdrConversionStrategy) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100453
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700454 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700455 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700456
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800457 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800458
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700459 hal::HWDisplayId getPrimaryHwcDisplayId() const override {
460 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
461 return *mPrimaryHwcDisplayId;
Lloyd Pique441d5042018-10-18 16:49:51 -0700462 }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700463
464 PhysicalDisplayId getPrimaryDisplayId() const override {
465 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
466 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
467 return *id;
Lloyd Pique441d5042018-10-18 16:49:51 -0700468 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700469
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700470 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
471
Marin Shalamanova524a092020-07-27 21:39:55 +0200472 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
473 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700474
Mathias Agopiana350ff92010-08-10 17:14:02 -0700475private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800476 // For unit tests
477 friend TestableSurfaceFlinger;
478
Mathias Agopiane60b0682012-08-21 23:34:09 -0700479 struct DisplayData {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800480 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400481
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700482 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400483 nsecs_t lastPresentTimestamp = 0;
484
Steven Thomas94e35b92017-07-26 18:48:28 -0700485 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800486
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700487 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700488 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800489
490 bool vsyncTraceToggle = false;
491
492 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700493 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700494 };
495
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100496 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
497 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
498 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
499
500 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100501 hal::Attribute attribute) const;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100502
503 void loadCapabilities();
504 void loadLayerMetadataSupport();
Sally Qibb866c12022-10-17 11:31:20 -0700505 void loadOverlayProperties();
Kriti Dang674b9372022-11-18 10:58:44 +0100506 void loadHdrConversionCapabilities();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100507
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200508 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800509
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800510 std::unique_ptr<android::Hwc2::Composer> mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800511 std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities;
Sally Qibb866c12022-10-17 11:31:20 -0700512 aidl::android::hardware::graphics::composer3::OverlayProperties mOverlayProperties;
Kriti Dang674b9372022-11-18 10:58:44 +0100513 std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
514 mHdrConversionCapabilities = {};
515
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800516 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800517 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800518
Marin Shalamanova524a092020-07-27 21:39:55 +0200519 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700520 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700521 bool mHasMultiDisplaySupport = false;
522
Dominik Laskowski13948602021-03-08 20:48:28 -0800523 const size_t mMaxVirtualDisplayDimension;
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200524 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700525};
526
Lloyd Pique441d5042018-10-18 16:49:51 -0700527} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800528} // namespace android