blob: 78d4a68617bf663d57b58a3ddd41da980c777d76 [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>
Ady Abrahamde549d42022-01-26 19:19:17 -080047#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri85065692022-03-18 00:58:26 +000048#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050049#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050050#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Sally Qi0cbd08b2022-08-17 12:12:28 -070051#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050052
Mathias Agopiana350ff92010-08-10 17:14:02 -070053namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070054
Peiyong Line9d809e2020-04-14 13:10:48 -070055namespace hal = hardware::graphics::composer::hal;
56
Kevin DuBois1d4249a2018-08-29 10:45:14 -070057struct DisplayedFrameStats;
Jesse Hall399184a2014-03-03 15:42:54 -080058class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080059class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080060struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070061
Lloyd Piquea822d522017-12-20 16:42:57 -080062namespace Hwc2 {
63class Composer;
64} // namespace Hwc2
65
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080066namespace compositionengine {
67class Output;
68} // namespace compositionengine
69
Lloyd Pique4603f3c2020-02-11 12:06:56 -080070struct KnownHWCGenericLayerMetadata {
71 const char* name;
72 const uint32_t id;
73};
74
Marin Shalamanov1c434292020-06-12 01:47:29 +020075// See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
76// this class.
Lloyd Pique441d5042018-10-18 16:49:51 -070077class HWComposer {
78public:
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070079 struct DeviceRequestedChanges {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050080 using ChangedTypes =
81 std::unordered_map<HWC2::Layer*,
82 aidl::android::hardware::graphics::composer3::Composition>;
Alec Mouri85065692022-03-18 00:58:26 +000083 using ClientTargetProperty =
84 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070085 using DisplayRequests = hal::DisplayRequest;
86 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
87
88 ChangedTypes changedTypes;
89 DisplayRequests displayRequests;
90 LayerRequests layerRequests;
91 ClientTargetProperty clientTargetProperty;
92 };
93
Marin Shalamanov045b7002021-01-07 16:56:24 +010094 struct HWCDisplayMode {
95 hal::HWConfigId hwcId;
96 int32_t width = -1;
97 int32_t height = -1;
98 nsecs_t vsyncPeriod = -1;
99 int32_t dpiX = -1;
100 int32_t dpiY = -1;
101 int32_t configGroup = -1;
Marin Shalamanovd3b5c5d2021-02-11 18:26:14 +0100102
103 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
104 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
105 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
106 << mode.dpiY << " group=" << mode.configGroup;
107 }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100108 };
109
Lloyd Pique441d5042018-10-18 16:49:51 -0700110 virtual ~HWComposer();
111
Yichi Chen3401b562022-01-17 15:42:35 +0800112 virtual void setCallback(HWC2::ComposerCallback&) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700113
Marin Shalamanova524a092020-07-27 21:39:55 +0200114 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700115 DisplayIdentificationData* outData) const = 0;
116
Ady Abrahamde549d42022-01-26 19:19:17 -0800117 virtual bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const = 0;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500118 virtual bool hasDisplayCapability(
119 HalDisplayId,
120 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700121
Dominik Laskowski13948602021-03-08 20:48:28 -0800122 virtual size_t getMaxVirtualDisplayCount() const = 0;
123 virtual size_t getMaxVirtualDisplayDimension() const = 0;
124
125 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
126 // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
Dominik Laskowski263eec42021-07-21 23:13:24 -0700127 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700128
Marin Shalamanova524a092020-07-27 21:39:55 +0200129 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100130
Lloyd Pique441d5042018-10-18 16:49:51 -0700131 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700132 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700133
Lloyd Pique66d68602019-02-13 14:23:31 -0800134 // Gets any required composition change requests from the HWC device.
135 //
136 // Note that frameUsesClientComposition must be set correctly based on
137 // whether the current frame appears to use client composition. If it is
138 // false some internal optimizations are allowed to present the display
139 // with fewer handshakes, but this does not work if client composition is
140 // expected.
141 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200142 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700143 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800144 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800145 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700146
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200147 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200148 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700149
150 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700151 virtual status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700152 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
153 const std::shared_ptr<FenceTime>& previousPresentFence) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700154
155 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200156 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700157
Lloyd Pique441d5042018-10-18 16:49:51 -0700158 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200159 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700160
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200161 // reset state when a display is disconnected
162 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700163
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400164 // Get the present fence/timestamp received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200165 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400166 virtual nsecs_t getPresentTimestamp(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700167
168 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200169 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700170
171 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200172 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700173 const sp<GraphicBuffer>& buffer) = 0;
174
175 // After SurfaceFlinger has retrieved the release fences for all the frames,
176 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200177 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700178
179 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200180 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700181
Sally Qibb866c12022-10-17 11:31:20 -0700182 virtual const aidl::android::hardware::graphics::composer3::OverlayProperties&
183 getOverlaySupport() const = 0;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700184
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200185 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700186
187 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200188 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700189
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700191
192 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200193 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700194 ui::Dataspace* outDataspace,
195 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200196 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700197 uint8_t componentMask,
198 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200199 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700200 DisplayedFrameStats* outStats) = 0;
201
Dan Gittik57e63c52019-01-18 16:37:54 +0000202 // Sets the brightness of a display.
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700203 virtual ftl::Future<status_t> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000204 PhysicalDisplayId, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -0800205 const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000206
Matt Buckley50c44062022-01-17 20:48:10 +0000207 // Get whether the display skipped validation on the latest present
208 virtual bool getValidateSkipped(HalDisplayId displayId) const = 0;
209
Lloyd Pique441d5042018-10-18 16:49:51 -0700210 // Events handling ---------------------------------------------------------
211
212 // Returns stable display ID (and display name on connection of new or previously disconnected
213 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100214 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200215 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
216 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700217
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200218 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
219 // TODO(b/157555476): Remove when the framework has proper support for headless mode
220 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
221
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400222 virtual bool onVsync(hal::HWDisplayId, nsecs_t timestamp) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200223 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700224
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200225 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700226
Marin Shalamanov045b7002021-01-07 16:56:24 +0100227 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700228
Marin Shalamanov045b7002021-01-07 16:56:24 +0100229 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700230
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200231 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700232
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200233 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
234 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700235
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800236 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100237 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200238 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100239 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
240 nsecs_t* outVsyncPeriod) const = 0;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100241 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100242 const hal::VsyncPeriodChangeConstraints&,
243 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200244 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100245 virtual status_t getSupportedContentTypes(
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800246 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) const = 0;
247
248 bool supportsContentType(PhysicalDisplayId displayId, hal::ContentType type) const {
249 std::vector<hal::ContentType> types;
250 return getSupportedContentTypes(displayId, &types) == NO_ERROR &&
251 std::find(types.begin(), types.end(), type) != types.end();
252 }
253
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200254 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800255
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800256 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
257 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800258
Lloyd Pique441d5042018-10-18 16:49:51 -0700259 virtual void dump(std::string& out) const = 0;
260
261 virtual Hwc2::Composer* getComposer() const = 0;
262
Marin Shalamanov8b196592021-08-09 16:24:42 +0200263 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
264 // which in practice is immediately after HWComposer construction, must occur before any
265 // call to this function.
266 // The primary display can be temporarily disconnected from the perspective
267 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
268 // if isHeadless().
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700269 //
270 // TODO(b/182939859): Remove special cases for primary display.
271 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
272 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
273 virtual bool isHeadless() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700274
Marin Shalamanova524a092020-07-27 21:39:55 +0200275 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
276 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Kriti Dang7defaf32021-11-15 11:55:43 +0100277
278 // Composer 3.0
Kriti Dang7defaf32021-11-15 11:55:43 +0100279 virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0;
280 virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0;
Kriti Dang16ca2972022-02-01 20:07:03 +0100281 virtual std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) = 0;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500282 virtual status_t getDisplayDecorationSupport(
283 PhysicalDisplayId,
284 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
285 support) = 0;
ramindani32cf0602022-03-02 02:30:29 +0000286 virtual status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) = 0;
ramindani06e518e2022-03-14 18:47:53 +0000287 virtual bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const = 0;
288 virtual Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700289};
290
Vishnu Naira3140382022-02-24 14:07:11 -0800291static inline bool operator==(const android::HWComposer::DeviceRequestedChanges& lhs,
292 const android::HWComposer::DeviceRequestedChanges& rhs) {
293 return lhs.changedTypes == rhs.changedTypes && lhs.displayRequests == rhs.displayRequests &&
294 lhs.layerRequests == rhs.layerRequests &&
295 lhs.clientTargetProperty == rhs.clientTargetProperty;
296}
297
Lloyd Pique441d5042018-10-18 16:49:51 -0700298namespace impl {
299
300class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700301public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800302 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800303 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700304
Lloyd Pique441d5042018-10-18 16:49:51 -0700305 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700306
Yichi Chen3401b562022-01-17 15:42:35 +0800307 void setCallback(HWC2::ComposerCallback&) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700308
Marin Shalamanova524a092020-07-27 21:39:55 +0200309 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700310 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700311
Ady Abrahamde549d42022-01-26 19:19:17 -0800312 bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500313 bool hasDisplayCapability(
314 HalDisplayId,
315 aidl::android::hardware::graphics::composer3::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700316
Dominik Laskowski13948602021-03-08 20:48:28 -0800317 size_t getMaxVirtualDisplayCount() const override;
318 size_t getMaxVirtualDisplayDimension() const override;
319
Dominik Laskowski263eec42021-07-21 23:13:24 -0700320 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700321
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100322 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200323 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100324
Dan Stoza9e56aa02015-11-02 13:00:03 -0800325 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700326 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700327
Lloyd Pique66d68602019-02-13 14:23:31 -0800328 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200329 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700330 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800331 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800332 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700333
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200335 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800336
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800337 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700338 status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700339 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
340 const std::shared_ptr<FenceTime>& previousPresentFence) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700341
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700342 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200343 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700344
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700345 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200346 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700347
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200348 // reset state when a display is disconnected
349 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700350
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400351 // Get the present fence/timestamp received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200352 sp<Fence> getPresentFence(HalDisplayId) const override;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400353 nsecs_t getPresentTimestamp(PhysicalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700354
Dan Stoza9e56aa02015-11-02 13:00:03 -0800355 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200356 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700357
Jesse Hall851cfe82013-03-20 13:44:00 -0700358 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200359 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700360 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700361
Dan Stoza9e56aa02015-11-02 13:00:03 -0800362 // After SurfaceFlinger has retrieved the release fences for all the frames,
363 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200364 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700365
Peiyong Lin62665892018-04-16 11:07:44 -0700366 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200367 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700368
Sally Qibb866c12022-10-17 11:31:20 -0700369 const aidl::android::hardware::graphics::composer3::OverlayProperties& getOverlaySupport()
370 const override;
Sally Qi0cbd08b2022-08-17 12:12:28 -0700371
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200372 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700373
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700374 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200375 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700376
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200377 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700378
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700379 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200380 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700381 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700382 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200383 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200384 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200385 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700386 DisplayedFrameStats* outStats) override;
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700387 ftl::Future<status_t> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000388 PhysicalDisplayId, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -0800389 const Hwc2::Composer::DisplayBrightnessOptions&) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700390
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700391 // Events handling ---------------------------------------------------------
392
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200393 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700394 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200395 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700396
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200397 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
398
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400399 bool onVsync(hal::HWDisplayId, nsecs_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200400 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700401
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200402 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700403
Marin Shalamanov045b7002021-01-07 16:56:24 +0100404 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700405
Marin Shalamanov045b7002021-01-07 16:56:24 +0100406 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700407
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200408 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700409
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200410 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600411
Matt Buckley50c44062022-01-17 20:48:10 +0000412 bool getValidateSkipped(HalDisplayId displayId) const override;
413
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800414 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100415 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200416 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100417 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
418 nsecs_t* outVsyncPeriod) const override;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100419 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100420 const hal::VsyncPeriodChangeConstraints&,
421 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200422 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
Dominik Laskowski6c7b36e2022-03-03 08:27:58 -0800423 status_t getSupportedContentTypes(PhysicalDisplayId,
424 std::vector<hal::ContentType>*) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200425 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800426
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800427 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
428
Kriti Dang7defaf32021-11-15 11:55:43 +0100429 // Composer 3.0
Kriti Dang7defaf32021-11-15 11:55:43 +0100430 status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override;
431 status_t clearBootDisplayMode(PhysicalDisplayId) override;
Kriti Dang16ca2972022-02-01 20:07:03 +0100432 std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500433 status_t getDisplayDecorationSupport(
434 PhysicalDisplayId,
435 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
436 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000437 status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) override;
ramindani06e518e2022-03-14 18:47:53 +0000438 bool hasDisplayIdleTimerCapability(PhysicalDisplayId) const override;
439 Hwc2::AidlTransform getPhysicalDisplayOrientation(PhysicalDisplayId) const override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100440
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700441 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700442 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700443
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800444 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800445
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700446 hal::HWDisplayId getPrimaryHwcDisplayId() const override {
447 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
448 return *mPrimaryHwcDisplayId;
Lloyd Pique441d5042018-10-18 16:49:51 -0700449 }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700450
451 PhysicalDisplayId getPrimaryDisplayId() const override {
452 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
453 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
454 return *id;
Lloyd Pique441d5042018-10-18 16:49:51 -0700455 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700456
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700457 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
458
Marin Shalamanova524a092020-07-27 21:39:55 +0200459 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
460 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700461
Mathias Agopiana350ff92010-08-10 17:14:02 -0700462private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800463 // For unit tests
464 friend TestableSurfaceFlinger;
465
Mathias Agopiane60b0682012-08-21 23:34:09 -0700466 struct DisplayData {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800467 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400468
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700469 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Dominik Laskowskidc2bb802022-09-28 16:02:59 -0400470 nsecs_t lastPresentTimestamp = 0;
471
Steven Thomas94e35b92017-07-26 18:48:28 -0700472 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800473
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700474 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700475 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800476
477 bool vsyncTraceToggle = false;
478
479 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700480 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700481 };
482
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100483 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
484 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
485 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
486
487 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100488 hal::Attribute attribute) const;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100489
490 void loadCapabilities();
491 void loadLayerMetadataSupport();
Sally Qibb866c12022-10-17 11:31:20 -0700492 void loadOverlayProperties();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100493
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200494 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800495
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800496 std::unique_ptr<android::Hwc2::Composer> mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800497 std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities;
Sally Qibb866c12022-10-17 11:31:20 -0700498 aidl::android::hardware::graphics::composer3::OverlayProperties mOverlayProperties;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800499 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800500 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800501
Marin Shalamanova524a092020-07-27 21:39:55 +0200502 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700503 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700504 bool mHasMultiDisplaySupport = false;
505
Dominik Laskowski13948602021-03-08 20:48:28 -0800506 const size_t mMaxVirtualDisplayDimension;
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200507 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700508};
509
Lloyd Pique441d5042018-10-18 16:49:51 -0700510} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800511} // namespace android