blob: f9637f07726fad26b7c1a1993a1850d2d8253104 [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>
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>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050048#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050049#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050050
Mathias Agopiana350ff92010-08-10 17:14:02 -070051namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070052
Peiyong Line9d809e2020-04-14 13:10:48 -070053namespace hal = hardware::graphics::composer::hal;
54
Kevin DuBois1d4249a2018-08-29 10:45:14 -070055struct DisplayedFrameStats;
Jesse Hall399184a2014-03-03 15:42:54 -080056class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080057class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080058struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070059
Lloyd Piquea822d522017-12-20 16:42:57 -080060namespace Hwc2 {
61class Composer;
62} // namespace Hwc2
63
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080064namespace compositionengine {
65class Output;
66} // namespace compositionengine
67
Lloyd Pique4603f3c2020-02-11 12:06:56 -080068struct KnownHWCGenericLayerMetadata {
69 const char* name;
70 const uint32_t id;
71};
72
Marin Shalamanov1c434292020-06-12 01:47:29 +020073// See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
74// this class.
Lloyd Pique441d5042018-10-18 16:49:51 -070075class HWComposer {
76public:
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070077 struct DeviceRequestedChanges {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050078 using ChangedTypes =
79 std::unordered_map<HWC2::Layer*,
80 aidl::android::hardware::graphics::composer3::Composition>;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070081 using ClientTargetProperty = hal::ClientTargetProperty;
82 using DisplayRequests = hal::DisplayRequest;
83 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
84
85 ChangedTypes changedTypes;
86 DisplayRequests displayRequests;
87 LayerRequests layerRequests;
88 ClientTargetProperty clientTargetProperty;
Alec Mourif8d093d2022-02-10 15:16:59 -080089 float clientTargetBrightness;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070090 };
91
Marin Shalamanov045b7002021-01-07 16:56:24 +010092 struct HWCDisplayMode {
93 hal::HWConfigId hwcId;
94 int32_t width = -1;
95 int32_t height = -1;
96 nsecs_t vsyncPeriod = -1;
97 int32_t dpiX = -1;
98 int32_t dpiY = -1;
99 int32_t configGroup = -1;
Marin Shalamanovd3b5c5d2021-02-11 18:26:14 +0100100
101 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
102 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
103 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
104 << mode.dpiY << " group=" << mode.configGroup;
105 }
Marin Shalamanov045b7002021-01-07 16:56:24 +0100106 };
107
Lloyd Pique441d5042018-10-18 16:49:51 -0700108 virtual ~HWComposer();
109
Yichi Chen3401b562022-01-17 15:42:35 +0800110 virtual void setCallback(HWC2::ComposerCallback&) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700111
Marin Shalamanova524a092020-07-27 21:39:55 +0200112 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700113 DisplayIdentificationData* outData) const = 0;
114
Ady Abrahamde549d42022-01-26 19:19:17 -0800115 virtual bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const = 0;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500116 virtual bool hasDisplayCapability(
117 HalDisplayId,
118 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700119
Dominik Laskowski13948602021-03-08 20:48:28 -0800120 virtual size_t getMaxVirtualDisplayCount() const = 0;
121 virtual size_t getMaxVirtualDisplayDimension() const = 0;
122
123 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
124 // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
Dominik Laskowski263eec42021-07-21 23:13:24 -0700125 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700126
Marin Shalamanova524a092020-07-27 21:39:55 +0200127 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100128
Lloyd Pique441d5042018-10-18 16:49:51 -0700129 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700130 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700131
Lloyd Pique66d68602019-02-13 14:23:31 -0800132 // Gets any required composition change requests from the HWC device.
133 //
134 // Note that frameUsesClientComposition must be set correctly based on
135 // whether the current frame appears to use client composition. If it is
136 // false some internal optimizations are allowed to present the display
137 // with fewer handshakes, but this does not work if client composition is
138 // expected.
139 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200140 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700141 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800142 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800143 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700144
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200145 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200146 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700147
148 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700149 virtual status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700150 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
151 const std::shared_ptr<FenceTime>& previousPresentFence) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700152
153 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200154 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700155
Lloyd Pique441d5042018-10-18 16:49:51 -0700156 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200157 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700158
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200159 // reset state when a display is disconnected
160 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700161
Lloyd Pique441d5042018-10-18 16:49:51 -0700162 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200163 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700164
165 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200166 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700167
168 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200169 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700170 const sp<GraphicBuffer>& buffer) = 0;
171
172 // After SurfaceFlinger has retrieved the release fences for all the frames,
173 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200174 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700175
176 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200177 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700178
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200179 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700180
181 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200182 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700183
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200184 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700185
186 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200187 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700188 ui::Dataspace* outDataspace,
189 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700191 uint8_t componentMask,
192 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200193 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700194 DisplayedFrameStats* outStats) = 0;
195
Dan Gittik57e63c52019-01-18 16:37:54 +0000196 // Sets the brightness of a display.
Alec Mouricdf16792021-12-10 13:16:06 -0800197 virtual std::future<status_t> setDisplayBrightness(
198 PhysicalDisplayId, float brightness,
199 const Hwc2::Composer::DisplayBrightnessOptions&) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000200
Lloyd Pique441d5042018-10-18 16:49:51 -0700201 // Events handling ---------------------------------------------------------
202
203 // Returns stable display ID (and display name on connection of new or previously disconnected
204 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100205 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200206 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
207 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700208
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200209 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
210 // TODO(b/157555476): Remove when the framework has proper support for headless mode
211 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
212
Marin Shalamanova524a092020-07-27 21:39:55 +0200213 virtual bool onVsync(hal::HWDisplayId, int64_t timestamp) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200214 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700215
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200216 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700217
Marin Shalamanov045b7002021-01-07 16:56:24 +0100218 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700219
Marin Shalamanov045b7002021-01-07 16:56:24 +0100220 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700221
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700223
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200224 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
225 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700226
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800227 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100228 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200229 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100230 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
231 nsecs_t* outVsyncPeriod) const = 0;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100232 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100233 const hal::VsyncPeriodChangeConstraints&,
234 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200235 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100236 virtual status_t getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200237 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) = 0;
238 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800239 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
240 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800241
Lloyd Pique441d5042018-10-18 16:49:51 -0700242 virtual void dump(std::string& out) const = 0;
243
244 virtual Hwc2::Composer* getComposer() const = 0;
245
Marin Shalamanov8b196592021-08-09 16:24:42 +0200246 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
247 // which in practice is immediately after HWComposer construction, must occur before any
248 // call to this function.
249 // The primary display can be temporarily disconnected from the perspective
250 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
251 // if isHeadless().
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700252 //
253 // TODO(b/182939859): Remove special cases for primary display.
254 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
255 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
256 virtual bool isHeadless() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700257
Marin Shalamanova524a092020-07-27 21:39:55 +0200258 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
259 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Kriti Dang7defaf32021-11-15 11:55:43 +0100260
261 // Composer 3.0
262 virtual bool getBootDisplayModeSupport() = 0;
263 virtual status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) = 0;
264 virtual status_t clearBootDisplayMode(PhysicalDisplayId) = 0;
Kriti Dang16ca2972022-02-01 20:07:03 +0100265 virtual std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) = 0;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500266 virtual status_t getDisplayDecorationSupport(
267 PhysicalDisplayId,
268 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
269 support) = 0;
ramindani32cf0602022-03-02 02:30:29 +0000270 virtual status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) = 0;
271 virtual bool hasDisplayIdleTimerCapability(PhysicalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700272};
273
274namespace impl {
275
276class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700277public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800278 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800279 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700280
Lloyd Pique441d5042018-10-18 16:49:51 -0700281 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700282
Yichi Chen3401b562022-01-17 15:42:35 +0800283 void setCallback(HWC2::ComposerCallback&) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700284
Marin Shalamanova524a092020-07-27 21:39:55 +0200285 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700286 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700287
Ady Abrahamde549d42022-01-26 19:19:17 -0800288 bool hasCapability(aidl::android::hardware::graphics::composer3::Capability) const override;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500289 bool hasDisplayCapability(
290 HalDisplayId,
291 aidl::android::hardware::graphics::composer3::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700292
Dominik Laskowski13948602021-03-08 20:48:28 -0800293 size_t getMaxVirtualDisplayCount() const override;
294 size_t getMaxVirtualDisplayDimension() const override;
295
Dominik Laskowski263eec42021-07-21 23:13:24 -0700296 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700297
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100298 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200299 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100300
Dan Stoza9e56aa02015-11-02 13:00:03 -0800301 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700302 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700303
Lloyd Pique66d68602019-02-13 14:23:31 -0800304 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200305 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700306 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800307 const std::shared_ptr<FenceTime>& previousPresentFence, nsecs_t expectedPresentTime,
Lloyd Pique66d68602019-02-13 14:23:31 -0800308 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700309
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200310 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200311 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800312
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800313 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700314 status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700315 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
316 const std::shared_ptr<FenceTime>& previousPresentFence) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700317
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700318 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200319 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700320
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700321 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200322 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700323
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200324 // reset state when a display is disconnected
325 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700326
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800327 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200328 sp<Fence> getPresentFence(HalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700329
Dan Stoza9e56aa02015-11-02 13:00:03 -0800330 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200331 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700332
Jesse Hall851cfe82013-03-20 13:44:00 -0700333 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700335 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700336
Dan Stoza9e56aa02015-11-02 13:00:03 -0800337 // After SurfaceFlinger has retrieved the release fences for all the frames,
338 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200339 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700340
Peiyong Lin62665892018-04-16 11:07:44 -0700341 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200342 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700343
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200344 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700345
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700346 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200347 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700348
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200349 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700350
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700351 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200352 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700353 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700354 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200355 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200356 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200357 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700358 DisplayedFrameStats* outStats) override;
Alec Mouricdf16792021-12-10 13:16:06 -0800359 std::future<status_t> setDisplayBrightness(
360 PhysicalDisplayId, float brightness,
361 const Hwc2::Composer::DisplayBrightnessOptions&) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700362
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700363 // Events handling ---------------------------------------------------------
364
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200365 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700366 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200367 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700368
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200369 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
370
Marin Shalamanova524a092020-07-27 21:39:55 +0200371 bool onVsync(hal::HWDisplayId, int64_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200372 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700373
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200374 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700375
Marin Shalamanov045b7002021-01-07 16:56:24 +0100376 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700377
Marin Shalamanov045b7002021-01-07 16:56:24 +0100378 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700379
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200380 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700381
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200382 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600383
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800384 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100385 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200386 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100387 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
388 nsecs_t* outVsyncPeriod) const override;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100389 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100390 const hal::VsyncPeriodChangeConstraints&,
391 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200392 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
393 status_t getSupportedContentTypes(PhysicalDisplayId, std::vector<hal::ContentType>*) override;
394 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800395
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800396 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
397
Kriti Dang7defaf32021-11-15 11:55:43 +0100398 // Composer 3.0
399 bool getBootDisplayModeSupport() override;
400 status_t setBootDisplayMode(PhysicalDisplayId, hal::HWConfigId) override;
401 status_t clearBootDisplayMode(PhysicalDisplayId) override;
Kriti Dang16ca2972022-02-01 20:07:03 +0100402 std::optional<hal::HWConfigId> getPreferredBootDisplayMode(PhysicalDisplayId) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500403 status_t getDisplayDecorationSupport(
404 PhysicalDisplayId,
405 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
406 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000407 status_t setIdleTimerEnabled(PhysicalDisplayId, std::chrono::milliseconds timeout) override;
408 bool hasDisplayIdleTimerCapability(PhysicalDisplayId) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100409
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700410 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700411 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700412
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800413 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800414
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700415 hal::HWDisplayId getPrimaryHwcDisplayId() const override {
416 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
417 return *mPrimaryHwcDisplayId;
Lloyd Pique441d5042018-10-18 16:49:51 -0700418 }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700419
420 PhysicalDisplayId getPrimaryDisplayId() const override {
421 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
422 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
423 return *id;
Lloyd Pique441d5042018-10-18 16:49:51 -0700424 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700425
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700426 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
427
Marin Shalamanova524a092020-07-27 21:39:55 +0200428 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
429 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700430
Mathias Agopiana350ff92010-08-10 17:14:02 -0700431private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800432 // For unit tests
433 friend TestableSurfaceFlinger;
434
Mathias Agopiane60b0682012-08-21 23:34:09 -0700435 struct DisplayData {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800436 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700437 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700438 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800439
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700440 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700441 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800442
443 bool vsyncTraceToggle = false;
444
445 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700446 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800447
Marin Shalamanov045b7002021-01-07 16:56:24 +0100448 nsecs_t lastHwVsync = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700449 };
450
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100451 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
452 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
453 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
454
455 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100456 hal::Attribute attribute) const;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100457
458 void loadCapabilities();
459 void loadLayerMetadataSupport();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100460
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200461 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800462
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800463 std::unique_ptr<android::Hwc2::Composer> mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800464 std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800465 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800466 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800467
Marin Shalamanova524a092020-07-27 21:39:55 +0200468 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700469 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700470 bool mHasMultiDisplaySupport = false;
471
Dominik Laskowski13948602021-03-08 20:48:28 -0800472 const size_t mMaxVirtualDisplayDimension;
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200473 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700474};
475
Lloyd Pique441d5042018-10-18 16:49:51 -0700476} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800477} // namespace android