blob: 9e57602e6029cf3bc81c477792cf9679c12efbcf [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>
Dominik Laskowski5690bde2020-04-23 19:04:22 -070020#include <future>
Dan Stoza9e56aa02015-11-02 13:00:03 -080021#include <memory>
Dominik Laskowski1af47932018-11-12 10:20:46 -080022#include <mutex>
Steven Thomas6e8f7062017-11-22 14:15:29 -080023#include <optional>
Dominik Laskowski075d3172018-05-24 15:50:06 -070024#include <unordered_map>
25#include <unordered_set>
Dan Stoza9e56aa02015-11-02 13:00:03 -080026#include <vector>
27
Dominik Laskowski1af47932018-11-12 10:20:46 -080028#include <android-base/thread_annotations.h>
Ady Abrahamec7aa8a2021-06-28 12:37:09 -070029#include <ui/FenceTime.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080030
31// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010034#pragma clang diagnostic ignored "-Wextra"
Dominik Laskowski1af47932018-11-12 10:20:46 -080035#include <ui/GraphicTypes.h>
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010036#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
Ady Abraham8a82ba62020-01-17 12:43:17 -080037
Dominik Laskowski1af47932018-11-12 10:20:46 -080038#include <utils/StrongPointer.h>
39#include <utils/Timers.h>
40
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070041#include "DisplayIdentification.h"
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 III2e1aa182021-12-01 17:33:12 -050046#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050047#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050048
Mathias Agopiana350ff92010-08-10 17:14:02 -070049namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070050
Peiyong Line9d809e2020-04-14 13:10:48 -070051namespace hal = hardware::graphics::composer::hal;
52
Kevin DuBois1d4249a2018-08-29 10:45:14 -070053struct DisplayedFrameStats;
Jesse Hall399184a2014-03-03 15:42:54 -080054class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080055class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080056struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070057
Lloyd Piquea822d522017-12-20 16:42:57 -080058namespace Hwc2 {
59class Composer;
60} // namespace Hwc2
61
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080062namespace compositionengine {
63class Output;
64} // namespace compositionengine
65
Lloyd Pique4603f3c2020-02-11 12:06:56 -080066struct KnownHWCGenericLayerMetadata {
67 const char* name;
68 const uint32_t id;
69};
70
Marin Shalamanov1c434292020-06-12 01:47:29 +020071// See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
72// this class.
Lloyd Pique441d5042018-10-18 16:49:51 -070073class HWComposer {
74public:
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070075 struct DeviceRequestedChanges {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050076 using ChangedTypes =
77 std::unordered_map<HWC2::Layer*,
78 aidl::android::hardware::graphics::composer3::Composition>;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070079 using ClientTargetProperty = hal::ClientTargetProperty;
80 using DisplayRequests = hal::DisplayRequest;
81 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
82
83 ChangedTypes changedTypes;
84 DisplayRequests displayRequests;
85 LayerRequests layerRequests;
86 ClientTargetProperty clientTargetProperty;
Alec Mouricdf6cbc2021-11-01 17:21:15 -070087 float clientTargetWhitePointNits;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070088 };
89
Marin Shalamanov045b7002021-01-07 16:56:24 +010090 struct HWCDisplayMode {
91 hal::HWConfigId hwcId;
92 int32_t width = -1;
93 int32_t height = -1;
94 nsecs_t vsyncPeriod = -1;
95 int32_t dpiX = -1;
96 int32_t dpiY = -1;
97 int32_t configGroup = -1;
Marin Shalamanovd3b5c5d2021-02-11 18:26:14 +010098
99 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
100 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
101 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
102 << mode.dpiY << " group=" << mode.configGroup;
103 }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100104 };
105
Lloyd Pique441d5042018-10-18 16:49:51 -0700106 virtual ~HWComposer();
107
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700108 virtual void setCallback(HWC2::ComposerCallback*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700109
Marin Shalamanova524a092020-07-27 21:39:55 +0200110 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700111 DisplayIdentificationData* outData) const = 0;
112
Marin Shalamanova524a092020-07-27 21:39:55 +0200113 virtual bool hasCapability(hal::Capability) const = 0;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500114 virtual bool hasDisplayCapability(
115 HalDisplayId,
116 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700117
Dominik Laskowski13948602021-03-08 20:48:28 -0800118 virtual size_t getMaxVirtualDisplayCount() const = 0;
119 virtual size_t getMaxVirtualDisplayDimension() const = 0;
120
121 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
122 // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
Dominik Laskowski263eec42021-07-21 23:13:24 -0700123 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700124
Marin Shalamanova524a092020-07-27 21:39:55 +0200125 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100126
Lloyd Pique441d5042018-10-18 16:49:51 -0700127 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700128 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700129
Lloyd Pique66d68602019-02-13 14:23:31 -0800130 // Gets any required composition change requests from the HWC device.
131 //
132 // Note that frameUsesClientComposition must be set correctly based on
133 // whether the current frame appears to use client composition. If it is
134 // false some internal optimizations are allowed to present the display
135 // with fewer handshakes, but this does not work if client composition is
136 // expected.
137 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200138 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700139 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800140 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800141 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700142
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200143 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200144 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700145
146 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700147 virtual status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700148 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
149 const std::shared_ptr<FenceTime>& previousPresentFence) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700150
151 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200152 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700153
Lloyd Pique441d5042018-10-18 16:49:51 -0700154 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200155 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700156
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200157 // reset state when a display is disconnected
158 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700159
Lloyd Pique441d5042018-10-18 16:49:51 -0700160 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200161 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700162
163 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200164 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700165
166 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200167 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700168 const sp<GraphicBuffer>& buffer) = 0;
169
170 // After SurfaceFlinger has retrieved the release fences for all the frames,
171 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200172 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700173
174 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200175 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700176
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200177 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700178
179 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200180 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700181
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200182 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700183
184 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200185 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700186 ui::Dataspace* outDataspace,
187 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200188 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700189 uint8_t componentMask,
190 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200191 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700192 DisplayedFrameStats* outStats) = 0;
193
Dan Gittik57e63c52019-01-18 16:37:54 +0000194 // Sets the brightness of a display.
Alec Mouricdf16792021-12-10 13:16:06 -0800195 virtual std::future<status_t> setDisplayBrightness(
196 PhysicalDisplayId, float brightness,
197 const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000198
Lloyd Pique441d5042018-10-18 16:49:51 -0700199 // Events handling ---------------------------------------------------------
200
201 // Returns stable display ID (and display name on connection of new or previously disconnected
202 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100203 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200204 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
205 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700206
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200207 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
208 // TODO(b/157555476): Remove when the framework has proper support for headless mode
209 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
210
Marin Shalamanova524a092020-07-27 21:39:55 +0200211 virtual bool onVsync(hal::HWDisplayId, int64_t timestamp) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200212 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700213
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200214 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700215
Marin Shalamanov045b7002021-01-07 16:56:24 +0100216 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700217
Marin Shalamanov045b7002021-01-07 16:56:24 +0100218 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700219
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200220 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700221
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
223 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700224
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800225 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100226 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200227 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100228 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
229 nsecs_t* outVsyncPeriod) const = 0;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100230 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100231 const hal::VsyncPeriodChangeConstraints&,
232 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200233 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100234 virtual status_t getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200235 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) = 0;
236 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800237 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
238 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800239
Lloyd Pique441d5042018-10-18 16:49:51 -0700240 virtual void dump(std::string& out) const = 0;
241
242 virtual Hwc2::Composer* getComposer() const = 0;
243
Marin Shalamanov8b196592021-08-09 16:24:42 +0200244 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
245 // which in practice is immediately after HWComposer construction, must occur before any
246 // call to this function.
247 // The primary display can be temporarily disconnected from the perspective
248 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
249 // if isHeadless().
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700250 //
251 // TODO(b/182939859): Remove special cases for primary display.
252 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
253 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
254 virtual bool isHeadless() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700255
Marin Shalamanova524a092020-07-27 21:39:55 +0200256 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
257 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Kriti Dang7defaf32021-11-15 11:55:43 +0100258
259 // Composer 3.0
260 virtual bool getBootDisplayModeSupport() = 0;
261 virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0;
262 virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0;
263 virtual hal::HWConfigId getPreferredBootDisplayMode(PhysicalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700264};
265
266namespace impl {
267
268class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700269public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800270 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800271 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700272
Lloyd Pique441d5042018-10-18 16:49:51 -0700273 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700274
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700275 void setCallback(HWC2::ComposerCallback*) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700276
Marin Shalamanova524a092020-07-27 21:39:55 +0200277 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700278 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700279
Marin Shalamanova524a092020-07-27 21:39:55 +0200280 bool hasCapability(hal::Capability) const override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500281 bool hasDisplayCapability(
282 HalDisplayId,
283 aidl::android::hardware::graphics::composer3::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700284
Dominik Laskowski13948602021-03-08 20:48:28 -0800285 size_t getMaxVirtualDisplayCount() const override;
286 size_t getMaxVirtualDisplayDimension() const override;
287
Dominik Laskowski263eec42021-07-21 23:13:24 -0700288 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700289
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100290 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200291 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100292
Dan Stoza9e56aa02015-11-02 13:00:03 -0800293 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700294 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700295
Lloyd Pique66d68602019-02-13 14:23:31 -0800296 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200297 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700298 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800299 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800300 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700301
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200302 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200303 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800305 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700306 status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700307 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
308 const std::shared_ptr<FenceTime>& previousPresentFence) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700309
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700310 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200311 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700312
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700313 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200314 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700315
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200316 // reset state when a display is disconnected
317 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700318
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800319 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200320 sp<Fence> getPresentFence(HalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700321
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200323 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700324
Jesse Hall851cfe82013-03-20 13:44:00 -0700325 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200326 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700327 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700328
Dan Stoza9e56aa02015-11-02 13:00:03 -0800329 // After SurfaceFlinger has retrieved the release fences for all the frames,
330 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200331 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700332
Peiyong Lin62665892018-04-16 11:07:44 -0700333 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700335
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200336 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700337
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700338 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200339 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700340
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200341 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700342
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700343 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200344 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700345 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700346 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200347 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200348 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200349 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700350 DisplayedFrameStats* outStats) override;
Alec Mouricdf16792021-12-10 13:16:06 -0800351 std::future<status_t> setDisplayBrightness(
352 PhysicalDisplayId, float brightness,
353 const Hwc2::Composer::DisplayBrightnessOptions&) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700354
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700355 // Events handling ---------------------------------------------------------
356
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200357 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700358 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200359 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700360
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200361 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
362
Marin Shalamanova524a092020-07-27 21:39:55 +0200363 bool onVsync(hal::HWDisplayId, int64_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200364 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700365
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200366 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700367
Marin Shalamanov045b7002021-01-07 16:56:24 +0100368 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700369
Marin Shalamanov045b7002021-01-07 16:56:24 +0100370 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700371
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200372 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700373
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200374 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600375
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800376 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100377 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200378 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100379 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
380 nsecs_t* outVsyncPeriod) const override;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100381 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100382 const hal::VsyncPeriodChangeConstraints&,
383 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200384 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
385 status_t getSupportedContentTypes(PhysicalDisplayId, std::vector<hal::ContentType>*) override;
386 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800387
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800388 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
389
Kriti Dang7defaf32021-11-15 11:55:43 +0100390 // Composer 3.0
391 bool getBootDisplayModeSupport() override;
392 status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override;
393 status_t clearBootDisplayMode(PhysicalDisplayId) override;
394 hal::HWConfigId getPreferredBootDisplayMode(PhysicalDisplayId) override;
395
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700396 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700397 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700398
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800399 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800400
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700401 hal::HWDisplayId getPrimaryHwcDisplayId() const override {
402 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
403 return *mPrimaryHwcDisplayId;
Lloyd Pique441d5042018-10-18 16:49:51 -0700404 }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700405
406 PhysicalDisplayId getPrimaryDisplayId() const override {
407 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
408 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
409 return *id;
Lloyd Pique441d5042018-10-18 16:49:51 -0700410 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700411
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700412 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
413
Marin Shalamanova524a092020-07-27 21:39:55 +0200414 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
415 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700416
Mathias Agopiana350ff92010-08-10 17:14:02 -0700417private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800418 // For unit tests
419 friend TestableSurfaceFlinger;
420
Mathias Agopiane60b0682012-08-21 23:34:09 -0700421 struct DisplayData {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800422 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700423 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700424 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800425
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700426 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700427 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800428
429 bool vsyncTraceToggle = false;
430
431 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700432 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800433
Marin Shalamanov045b7002021-01-07 16:56:24 +0100434 nsecs_t lastHwVsync = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700435 };
436
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100437 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
438 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
439 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
440
441 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100442 hal::Attribute attribute) const;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100443
444 void loadCapabilities();
445 void loadLayerMetadataSupport();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100446
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200447 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800448
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800449 std::unique_ptr<android::Hwc2::Composer> mComposer;
Peiyong Line9d809e2020-04-14 13:10:48 -0700450 std::unordered_set<hal::Capability> mCapabilities;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800451 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800452 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800453
Marin Shalamanova524a092020-07-27 21:39:55 +0200454 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700455 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700456 bool mHasMultiDisplaySupport = false;
457
Dominik Laskowski13948602021-03-08 20:48:28 -0800458 const size_t mMaxVirtualDisplayDimension;
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200459 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700460};
461
Lloyd Pique441d5042018-10-18 16:49:51 -0700462} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800463} // namespace android