blob: 66edd651953c7564deb3e0a6d5939a96eeec3989 [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
Mathias Agopiana350ff92010-08-10 17:14:02 -070046namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070047
Peiyong Line9d809e2020-04-14 13:10:48 -070048namespace hal = hardware::graphics::composer::hal;
49
Kevin DuBois1d4249a2018-08-29 10:45:14 -070050struct DisplayedFrameStats;
Jesse Hall399184a2014-03-03 15:42:54 -080051class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080052class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080053struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070054
Lloyd Piquea822d522017-12-20 16:42:57 -080055namespace Hwc2 {
56class Composer;
57} // namespace Hwc2
58
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059namespace compositionengine {
60class Output;
61} // namespace compositionengine
62
Lloyd Pique4603f3c2020-02-11 12:06:56 -080063struct KnownHWCGenericLayerMetadata {
64 const char* name;
65 const uint32_t id;
66};
67
Marin Shalamanov1c434292020-06-12 01:47:29 +020068// See the comment for SurfaceFlinger::getHwComposer for the thread safety rules for accessing
69// this class.
Lloyd Pique441d5042018-10-18 16:49:51 -070070class HWComposer {
71public:
Peiyong Lindfc3f7c2020-05-07 20:15:50 -070072 struct DeviceRequestedChanges {
73 using ChangedTypes = std::unordered_map<HWC2::Layer*, hal::Composition>;
74 using ClientTargetProperty = hal::ClientTargetProperty;
75 using DisplayRequests = hal::DisplayRequest;
76 using LayerRequests = std::unordered_map<HWC2::Layer*, hal::LayerRequest>;
77
78 ChangedTypes changedTypes;
79 DisplayRequests displayRequests;
80 LayerRequests layerRequests;
81 ClientTargetProperty clientTargetProperty;
82 };
83
Marin Shalamanov045b7002021-01-07 16:56:24 +010084 struct HWCDisplayMode {
85 hal::HWConfigId hwcId;
86 int32_t width = -1;
87 int32_t height = -1;
88 nsecs_t vsyncPeriod = -1;
89 int32_t dpiX = -1;
90 int32_t dpiY = -1;
91 int32_t configGroup = -1;
Marin Shalamanovd3b5c5d2021-02-11 18:26:14 +010092
93 friend std::ostream& operator<<(std::ostream& os, const HWCDisplayMode& mode) {
94 return os << "id=" << mode.hwcId << " res=" << mode.width << "x" << mode.height
95 << " vsyncPeriod=" << mode.vsyncPeriod << " dpi=" << mode.dpiX << "x"
96 << mode.dpiY << " group=" << mode.configGroup;
97 }
Marin Shalamanov045b7002021-01-07 16:56:24 +010098 };
99
Lloyd Pique441d5042018-10-18 16:49:51 -0700100 virtual ~HWComposer();
101
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700102 virtual void setCallback(HWC2::ComposerCallback*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700103
Marin Shalamanova524a092020-07-27 21:39:55 +0200104 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700105 DisplayIdentificationData* outData) const = 0;
106
Marin Shalamanova524a092020-07-27 21:39:55 +0200107 virtual bool hasCapability(hal::Capability) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200108 virtual bool hasDisplayCapability(HalDisplayId, hal::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700109
Dominik Laskowski13948602021-03-08 20:48:28 -0800110 virtual size_t getMaxVirtualDisplayCount() const = 0;
111 virtual size_t getMaxVirtualDisplayDimension() const = 0;
112
113 // Attempts to allocate a virtual display on the HWC. The maximum number of virtual displays
114 // supported by the HWC can be queried in advance, but allocation may fail for other reasons.
Dominik Laskowski263eec42021-07-21 23:13:24 -0700115 virtual bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700116
Marin Shalamanova524a092020-07-27 21:39:55 +0200117 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100118
Lloyd Pique441d5042018-10-18 16:49:51 -0700119 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700120 virtual std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700121
Lloyd Pique66d68602019-02-13 14:23:31 -0800122 // Gets any required composition change requests from the HWC device.
123 //
124 // Note that frameUsesClientComposition must be set correctly based on
125 // whether the current frame appears to use client composition. If it is
126 // false some internal optimizations are allowed to present the display
127 // with fewer handshakes, but this does not work if client composition is
128 // expected.
129 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200130 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700131 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700132 const std::shared_ptr<FenceTime>& previousPresentFence,
Lloyd Pique66d68602019-02-13 14:23:31 -0800133 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700134
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200135 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200136 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700137
138 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700139 virtual status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700140 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
141 const std::shared_ptr<FenceTime>& previousPresentFence) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700142
143 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200144 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700145
Lloyd Pique441d5042018-10-18 16:49:51 -0700146 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200147 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700148
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200149 // reset state when a display is disconnected
150 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700151
Lloyd Pique441d5042018-10-18 16:49:51 -0700152 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200153 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700154
155 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200156 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700157
158 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200159 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700160 const sp<GraphicBuffer>& buffer) = 0;
161
162 // After SurfaceFlinger has retrieved the release fences for all the frames,
163 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200164 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700165
166 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200167 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700168
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200169 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700170
171 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200172 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700173
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200174 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700175
176 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200177 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700178 ui::Dataspace* outDataspace,
179 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200180 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700181 uint8_t componentMask,
182 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200183 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700184 DisplayedFrameStats* outStats) = 0;
185
Dan Gittik57e63c52019-01-18 16:37:54 +0000186 // Sets the brightness of a display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200187 virtual std::future<status_t> setDisplayBrightness(PhysicalDisplayId, float brightness) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000188
Lloyd Pique441d5042018-10-18 16:49:51 -0700189 // Events handling ---------------------------------------------------------
190
191 // Returns stable display ID (and display name on connection of new or previously disconnected
192 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100193 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200194 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
195 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700196
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200197 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
198 // TODO(b/157555476): Remove when the framework has proper support for headless mode
199 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
200
Marin Shalamanova524a092020-07-27 21:39:55 +0200201 virtual bool onVsync(hal::HWDisplayId, int64_t timestamp) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200202 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700203
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200204 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700205
Marin Shalamanov045b7002021-01-07 16:56:24 +0100206 virtual std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700207
Marin Shalamanov045b7002021-01-07 16:56:24 +0100208 virtual std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700209
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200210 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700211
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200212 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
213 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700214
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800215 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100216 virtual ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200217 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100218 virtual status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
219 nsecs_t* outVsyncPeriod) const = 0;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100220 virtual status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100221 const hal::VsyncPeriodChangeConstraints&,
222 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200223 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100224 virtual status_t getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200225 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) = 0;
226 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800227 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
228 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800229
Lloyd Pique441d5042018-10-18 16:49:51 -0700230 virtual void dump(std::string& out) const = 0;
231
232 virtual Hwc2::Composer* getComposer() const = 0;
233
Marin Shalamanov8b196592021-08-09 16:24:42 +0200234 // Returns the first display connected at boot. Its connection via HWComposer::onHotplug,
235 // which in practice is immediately after HWComposer construction, must occur before any
236 // call to this function.
237 // The primary display can be temporarily disconnected from the perspective
238 // of this class. Callers must not call getPrimaryHwcDisplayId() or getPrimaryDisplayId()
239 // if isHeadless().
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700240 //
241 // TODO(b/182939859): Remove special cases for primary display.
242 virtual hal::HWDisplayId getPrimaryHwcDisplayId() const = 0;
243 virtual PhysicalDisplayId getPrimaryDisplayId() const = 0;
244 virtual bool isHeadless() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700245
Marin Shalamanova524a092020-07-27 21:39:55 +0200246 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
247 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700248};
249
250namespace impl {
251
252class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800254 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800255 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700256
Lloyd Pique441d5042018-10-18 16:49:51 -0700257 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258
Dominik Laskowskiebc8d3a2021-04-16 23:18:31 -0700259 void setCallback(HWC2::ComposerCallback*) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700260
Marin Shalamanova524a092020-07-27 21:39:55 +0200261 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700262 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700263
Marin Shalamanova524a092020-07-27 21:39:55 +0200264 bool hasCapability(hal::Capability) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200265 bool hasDisplayCapability(HalDisplayId, hal::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700266
Dominik Laskowski13948602021-03-08 20:48:28 -0800267 size_t getMaxVirtualDisplayCount() const override;
268 size_t getMaxVirtualDisplayDimension() const override;
269
Dominik Laskowski263eec42021-07-21 23:13:24 -0700270 bool allocateVirtualDisplay(HalVirtualDisplayId, ui::Size, ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700271
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100272 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200273 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100274
Dan Stoza9e56aa02015-11-02 13:00:03 -0800275 // Attempts to create a new layer on this display
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700276 std::shared_ptr<HWC2::Layer> createLayer(HalDisplayId) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700277
Lloyd Pique66d68602019-02-13 14:23:31 -0800278 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200279 HalDisplayId, bool frameUsesClientComposition,
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700280 std::chrono::steady_clock::time_point earliestPresentTime,
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700281 const std::shared_ptr<FenceTime>& previousPresentFence,
Lloyd Pique66d68602019-02-13 14:23:31 -0800282 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700283
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200284 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200285 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800286
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800287 // Present layers to the display and read releaseFences.
Ady Abrahamb42cdc12021-05-11 14:31:26 -0700288 status_t presentAndGetReleaseFences(
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700289 HalDisplayId, std::chrono::steady_clock::time_point earliestPresentTime,
290 const std::shared_ptr<FenceTime>& previousPresentFence) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700291
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700292 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200293 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700294
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700295 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200296 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700297
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200298 // reset state when a display is disconnected
299 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700300
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800301 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200302 sp<Fence> getPresentFence(HalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700303
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200305 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700306
Jesse Hall851cfe82013-03-20 13:44:00 -0700307 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200308 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700309 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700310
Dan Stoza9e56aa02015-11-02 13:00:03 -0800311 // After SurfaceFlinger has retrieved the release fences for all the frames,
312 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200313 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700314
Peiyong Lin62665892018-04-16 11:07:44 -0700315 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200316 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700317
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200318 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700319
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700320 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200321 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700322
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200323 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700324
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700325 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200326 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700327 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700328 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200329 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200330 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200331 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700332 DisplayedFrameStats* outStats) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200333 std::future<status_t> setDisplayBrightness(PhysicalDisplayId, float brightness) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700334
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700335 // Events handling ---------------------------------------------------------
336
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200337 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200339 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700340
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200341 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
342
Marin Shalamanova524a092020-07-27 21:39:55 +0200343 bool onVsync(hal::HWDisplayId, int64_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200344 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700345
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200346 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700347
Marin Shalamanov045b7002021-01-07 16:56:24 +0100348 std::vector<HWCDisplayMode> getModes(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700349
Marin Shalamanov045b7002021-01-07 16:56:24 +0100350 std::optional<hal::HWConfigId> getActiveMode(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700351
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200352 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700353
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200354 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600355
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800356 // Composer 2.4
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100357 ui::DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200358 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
Marin Shalamanov045b7002021-01-07 16:56:24 +0100359 status_t getDisplayVsyncPeriod(PhysicalDisplayId displayId,
360 nsecs_t* outVsyncPeriod) const override;
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100361 status_t setActiveModeWithConstraints(PhysicalDisplayId, hal::HWConfigId,
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100362 const hal::VsyncPeriodChangeConstraints&,
363 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200364 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
365 status_t getSupportedContentTypes(PhysicalDisplayId, std::vector<hal::ContentType>*) override;
366 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800367
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800368 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
369
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700370 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700371 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700372
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800373 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800374
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700375 hal::HWDisplayId getPrimaryHwcDisplayId() const override {
376 LOG_ALWAYS_FATAL_IF(!mPrimaryHwcDisplayId, "Missing HWC primary display");
377 return *mPrimaryHwcDisplayId;
Lloyd Pique441d5042018-10-18 16:49:51 -0700378 }
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700379
380 PhysicalDisplayId getPrimaryDisplayId() const override {
381 const auto id = toPhysicalDisplayId(getPrimaryHwcDisplayId());
382 LOG_ALWAYS_FATAL_IF(!id, "Missing primary display");
383 return *id;
Lloyd Pique441d5042018-10-18 16:49:51 -0700384 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700385
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700386 virtual bool isHeadless() const override { return !mPrimaryHwcDisplayId; }
387
Marin Shalamanova524a092020-07-27 21:39:55 +0200388 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
389 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700390
Mathias Agopiana350ff92010-08-10 17:14:02 -0700391private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800392 // For unit tests
393 friend TestableSurfaceFlinger;
394
Mathias Agopiane60b0682012-08-21 23:34:09 -0700395 struct DisplayData {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800396 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700397 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700398 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800399
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700400 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700401 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800402
403 bool vsyncTraceToggle = false;
404
405 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700406 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800407
Marin Shalamanov045b7002021-01-07 16:56:24 +0100408 nsecs_t lastHwVsync = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700409 };
410
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100411 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
412 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
413 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
414
415 int32_t getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
Marin Shalamanov045b7002021-01-07 16:56:24 +0100416 hal::Attribute attribute) const;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100417
418 void loadCapabilities();
419 void loadLayerMetadataSupport();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100420
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200421 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800422
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800423 std::unique_ptr<android::Hwc2::Composer> mComposer;
Peiyong Line9d809e2020-04-14 13:10:48 -0700424 std::unordered_set<hal::Capability> mCapabilities;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800425 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800426 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800427
Marin Shalamanova524a092020-07-27 21:39:55 +0200428 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Dominik Laskowskif8db0f02021-04-19 11:05:25 -0700429 std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700430 bool mHasMultiDisplaySupport = false;
431
Dominik Laskowski13948602021-03-08 20:48:28 -0800432 const size_t mMaxVirtualDisplayDimension;
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200433 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700434};
435
Lloyd Pique441d5042018-10-18 16:49:51 -0700436} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800437} // namespace android