blob: d8af5bf4497ccd50001cc473185eed52df087189 [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
17#ifndef ANDROID_SF_HWCOMPOSER_H
18#define ANDROID_SF_HWCOMPOSER_H
19
Dominik Laskowski1af47932018-11-12 10:20:46 -080020#include <cstdint>
Dominik Laskowski5690bde2020-04-23 19:04:22 -070021#include <future>
Dan Stoza9e56aa02015-11-02 13:00:03 -080022#include <memory>
Dominik Laskowski1af47932018-11-12 10:20:46 -080023#include <mutex>
Steven Thomas6e8f7062017-11-22 14:15:29 -080024#include <optional>
Dominik Laskowski075d3172018-05-24 15:50:06 -070025#include <unordered_map>
26#include <unordered_set>
Dan Stoza9e56aa02015-11-02 13:00:03 -080027#include <vector>
28
Dominik Laskowski1af47932018-11-12 10:20:46 -080029#include <android-base/thread_annotations.h>
30#include <ui/Fence.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"
Dominik Laskowski1af47932018-11-12 10:20:46 -080035#include <ui/GraphicTypes.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080036#pragma clang diagnostic pop
37
Dominik Laskowski1af47932018-11-12 10:20:46 -080038#include <utils/StrongPointer.h>
39#include <utils/Timers.h>
40
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020041#include "DisplayIdGenerator.h"
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070042#include "DisplayIdentification.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
Lloyd Pique441d5042018-10-18 16:49:51 -070084 virtual ~HWComposer();
85
Lloyd Pique4603f3c2020-02-11 12:06:56 -080086 virtual void setConfiguration(HWC2::ComposerCallback* callback, int32_t sequenceId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -070087
Marin Shalamanova524a092020-07-27 21:39:55 +020088 virtual bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -070089 DisplayIdentificationData* outData) const = 0;
90
Marin Shalamanova524a092020-07-27 21:39:55 +020091 virtual bool hasCapability(hal::Capability) const = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020092 virtual bool hasDisplayCapability(HalDisplayId, hal::DisplayCapability) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -070093
94 // Attempts to allocate a virtual display and returns its ID if created on the HWC device.
95 virtual std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
Marin Shalamanova524a092020-07-27 21:39:55 +020096 ui::PixelFormat*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -070097
Marin Shalamanova524a092020-07-27 21:39:55 +020098 virtual void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) = 0;
Marin Shalamanovbdd59152020-02-14 15:30:06 +010099
Lloyd Pique441d5042018-10-18 16:49:51 -0700100 // Attempts to create a new layer on this display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200101 virtual HWC2::Layer* createLayer(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700102 // Destroy a previously created layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200103 virtual void destroyLayer(HalDisplayId, HWC2::Layer*) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700104
Lloyd Pique66d68602019-02-13 14:23:31 -0800105 // Gets any required composition change requests from the HWC device.
106 //
107 // Note that frameUsesClientComposition must be set correctly based on
108 // whether the current frame appears to use client composition. If it is
109 // false some internal optimizations are allowed to present the display
110 // with fewer handshakes, but this does not work if client composition is
111 // expected.
112 virtual status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200113 HalDisplayId, bool frameUsesClientComposition,
Lloyd Pique66d68602019-02-13 14:23:31 -0800114 std::optional<DeviceRequestedChanges>* outChanges) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700115
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200116 virtual status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200117 const sp<GraphicBuffer>& target, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700118
119 // Present layers to the display and read releaseFences.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200120 virtual status_t presentAndGetReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700121
122 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200123 virtual status_t setPowerMode(PhysicalDisplayId, hal::PowerMode) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700124
Lloyd Pique441d5042018-10-18 16:49:51 -0700125 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200126 virtual status_t setColorTransform(HalDisplayId, const mat4& transform) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700127
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200128 // reset state when a display is disconnected
129 virtual void disconnectDisplay(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700130
Lloyd Pique441d5042018-10-18 16:49:51 -0700131 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200132 virtual sp<Fence> getPresentFence(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700133
134 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200135 virtual sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700136
137 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200138 virtual status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700139 const sp<GraphicBuffer>& buffer) = 0;
140
141 // After SurfaceFlinger has retrieved the release fences for all the frames,
142 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200143 virtual void clearReleaseFences(HalDisplayId) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700144
145 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200146 virtual status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700147
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200148 virtual int32_t getSupportedPerFrameMetadata(HalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700149
150 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200151 virtual std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700152
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200153 virtual mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700154
155 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200156 virtual status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Lloyd Pique441d5042018-10-18 16:49:51 -0700157 ui::Dataspace* outDataspace,
158 uint8_t* outComponentMask) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200159 virtual status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled,
Lloyd Pique441d5042018-10-18 16:49:51 -0700160 uint8_t componentMask,
161 uint64_t maxFrames) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200162 virtual status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700163 DisplayedFrameStats* outStats) = 0;
164
Dan Gittik57e63c52019-01-18 16:37:54 +0000165 // Sets the brightness of a display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200166 virtual std::future<status_t> setDisplayBrightness(PhysicalDisplayId, float brightness) = 0;
Dan Gittik57e63c52019-01-18 16:37:54 +0000167
Lloyd Pique441d5042018-10-18 16:49:51 -0700168 // Events handling ---------------------------------------------------------
169
170 // Returns stable display ID (and display name on connection of new or previously disconnected
171 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100172 // This function is called from SurfaceFlinger.
Marin Shalamanova524a092020-07-27 21:39:55 +0200173 virtual std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId,
174 hal::Connection) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700175
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200176 // If true we'll update the DeviceProductInfo on subsequent hotplug connected events.
177 // TODO(b/157555476): Remove when the framework has proper support for headless mode
178 virtual bool updatesDeviceProductInfoOnHotplugReconnect() const = 0;
179
Marin Shalamanova524a092020-07-27 21:39:55 +0200180 virtual bool onVsync(hal::HWDisplayId, int64_t timestamp) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200181 virtual void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700182
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200183 virtual nsecs_t getRefreshTimestamp(PhysicalDisplayId) const = 0;
184 virtual bool isConnected(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700185
186 // Non-const because it can update configMap inside of mDisplayData
187 virtual std::vector<std::shared_ptr<const HWC2::Display::Config>> getConfigs(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200188 PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700189
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 virtual std::shared_ptr<const HWC2::Display::Config> getActiveConfig(
191 PhysicalDisplayId) const = 0;
192 virtual int getActiveConfigIndex(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700193
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200194 virtual std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700195
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200196 virtual status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode mode,
197 ui::RenderIntent) = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700198
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800199 // Composer 2.4
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200200 virtual DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const = 0;
201 virtual bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const = 0;
202 virtual nsecs_t getDisplayVsyncPeriod(PhysicalDisplayId) const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800203 virtual status_t setActiveConfigWithConstraints(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200204 PhysicalDisplayId, size_t configId, const hal::VsyncPeriodChangeConstraints&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700205 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200206 virtual status_t setAutoLowLatencyMode(PhysicalDisplayId, bool on) = 0;
Galia Peycheva5492cb52019-10-30 14:13:16 +0100207 virtual status_t getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200208 PhysicalDisplayId, std::vector<hal::ContentType>* outSupportedContentTypes) = 0;
209 virtual status_t setContentType(PhysicalDisplayId, hal::ContentType) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800210 virtual const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata()
211 const = 0;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800212
Lloyd Pique441d5042018-10-18 16:49:51 -0700213 // for debugging ----------------------------------------------------------
214 virtual void dump(std::string& out) const = 0;
215
216 virtual Hwc2::Composer* getComposer() const = 0;
217
218 // TODO(b/74619554): Remove special cases for internal/external display.
Peiyong Line9d809e2020-04-14 13:10:48 -0700219 virtual std::optional<hal::HWDisplayId> getInternalHwcDisplayId() const = 0;
220 virtual std::optional<hal::HWDisplayId> getExternalHwcDisplayId() const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700221
Marin Shalamanova524a092020-07-27 21:39:55 +0200222 virtual std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const = 0;
223 virtual std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const = 0;
Lloyd Pique441d5042018-10-18 16:49:51 -0700224};
225
226namespace impl {
227
228class HWComposer final : public android::HWComposer {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229public:
Dominik Laskowski1af47932018-11-12 10:20:46 -0800230 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800231 explicit HWComposer(const std::string& composerServiceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700232
Lloyd Pique441d5042018-10-18 16:49:51 -0700233 ~HWComposer() override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700234
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800235 void setConfiguration(HWC2::ComposerCallback* callback, int32_t sequenceId) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700236
Marin Shalamanova524a092020-07-27 21:39:55 +0200237 bool getDisplayIdentificationData(hal::HWDisplayId, uint8_t* outPort,
Lloyd Pique441d5042018-10-18 16:49:51 -0700238 DisplayIdentificationData* outData) const override;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700239
Marin Shalamanova524a092020-07-27 21:39:55 +0200240 bool hasCapability(hal::Capability) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200241 bool hasDisplayCapability(HalDisplayId, hal::DisplayCapability) const override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700242
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243 // Attempts to allocate a virtual display and returns its ID if created on the HWC device.
244 std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
Marin Shalamanova524a092020-07-27 21:39:55 +0200245 ui::PixelFormat*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700246
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100247 // Called from SurfaceFlinger, when the state for a new physical display needs to be recreated.
Marin Shalamanova524a092020-07-27 21:39:55 +0200248 void allocatePhysicalDisplay(hal::HWDisplayId, PhysicalDisplayId) override;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100249
Dan Stoza9e56aa02015-11-02 13:00:03 -0800250 // Attempts to create a new layer on this display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200251 HWC2::Layer* createLayer(HalDisplayId) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700252 // Destroy a previously created layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200253 void destroyLayer(HalDisplayId, HWC2::Layer*) override;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700254
Lloyd Pique66d68602019-02-13 14:23:31 -0800255 status_t getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200256 HalDisplayId, bool frameUsesClientComposition,
Lloyd Pique66d68602019-02-13 14:23:31 -0800257 std::optional<DeviceRequestedChanges>* outChanges) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200259 status_t setClientTarget(HalDisplayId, uint32_t slot, const sp<Fence>& acquireFence,
Marin Shalamanova524a092020-07-27 21:39:55 +0200260 const sp<GraphicBuffer>& target, ui::Dataspace) override;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800262 // Present layers to the display and read releaseFences.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200263 status_t presentAndGetReleaseFences(HalDisplayId) override;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700264
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700265 // set power mode
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200266 status_t setPowerMode(PhysicalDisplayId, hal::PowerMode mode) override;
Colin Cross10fbdb62012-07-12 17:56:34 -0700267
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700268 // Sets a color transform to be applied to the result of composition
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200269 status_t setColorTransform(HalDisplayId, const mat4& transform) override;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700270
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200271 // reset state when a display is disconnected
272 void disconnectDisplay(HalDisplayId) override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700273
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800274 // get the present fence received from the last call to present.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200275 sp<Fence> getPresentFence(HalDisplayId) const override;
Mathias Agopianda27af92012-09-13 18:17:13 -0700276
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277 // Get last release fence for the given layer
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200278 sp<Fence> getLayerReleaseFence(HalDisplayId, HWC2::Layer*) const override;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700279
Jesse Hall851cfe82013-03-20 13:44:00 -0700280 // Set the output buffer and acquire fence for a virtual display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200281 status_t setOutputBuffer(HalVirtualDisplayId, const sp<Fence>& acquireFence,
Lloyd Pique441d5042018-10-18 16:49:51 -0700282 const sp<GraphicBuffer>& buffer) override;
Jesse Hall851cfe82013-03-20 13:44:00 -0700283
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 // After SurfaceFlinger has retrieved the release fences for all the frames,
285 // it can call this to clear the shared pointers in the release fence map
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200286 void clearReleaseFences(HalDisplayId) override;
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700287
Peiyong Lin62665892018-04-16 11:07:44 -0700288 // Fetches the HDR capabilities of the given display
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200289 status_t getHdrCapabilities(HalDisplayId, HdrCapabilities* outCapabilities) override;
Dan Stozac4f471e2016-03-24 09:31:08 -0700290
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200291 int32_t getSupportedPerFrameMetadata(HalDisplayId) const override;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700292
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700293 // Returns the available RenderIntent of the given display.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200294 std::vector<ui::RenderIntent> getRenderIntents(HalDisplayId, ui::ColorMode) const override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700295
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200296 mat4 getDataspaceSaturationMatrix(HalDisplayId, ui::Dataspace) override;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700297
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700298 // Returns the attributes of the color sampling engine.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200299 status_t getDisplayedContentSamplingAttributes(HalDisplayId, ui::PixelFormat* outFormat,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700300 ui::Dataspace* outDataspace,
Lloyd Pique441d5042018-10-18 16:49:51 -0700301 uint8_t* outComponentMask) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200302 status_t setDisplayContentSamplingEnabled(HalDisplayId, bool enabled, uint8_t componentMask,
Marin Shalamanova524a092020-07-27 21:39:55 +0200303 uint64_t maxFrames) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200304 status_t getDisplayedContentSample(HalDisplayId, uint64_t maxFrames, uint64_t timestamp,
Lloyd Pique441d5042018-10-18 16:49:51 -0700305 DisplayedFrameStats* outStats) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200306 std::future<status_t> setDisplayBrightness(PhysicalDisplayId, float brightness) override;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700307
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700308 // Events handling ---------------------------------------------------------
309
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200310 // Returns PhysicalDisplayId (and display name on connection of new or previously disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -0700311 // display), or std::nullopt if hotplug event was ignored.
Marin Shalamanova524a092020-07-27 21:39:55 +0200312 std::optional<DisplayIdentificationInfo> onHotplug(hal::HWDisplayId, hal::Connection) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700313
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200314 bool updatesDeviceProductInfoOnHotplugReconnect() const override;
315
Marin Shalamanova524a092020-07-27 21:39:55 +0200316 bool onVsync(hal::HWDisplayId, int64_t timestamp) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200317 void setVsyncEnabled(PhysicalDisplayId, hal::Vsync enabled) override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700318
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200319 nsecs_t getRefreshTimestamp(PhysicalDisplayId) const override;
320 bool isConnected(PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700321
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 // Non-const because it can update configMap inside of mDisplayData
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200323 std::vector<std::shared_ptr<const HWC2::Display::Config>> getConfigs(
324 PhysicalDisplayId) const override;
Dan Stoza7f7da322014-05-02 15:26:25 -0700325
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200326 std::shared_ptr<const HWC2::Display::Config> getActiveConfig(PhysicalDisplayId) const override;
327 int getActiveConfigIndex(PhysicalDisplayId) const override;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700328
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200329 std::vector<ui::ColorMode> getColorModes(PhysicalDisplayId) const override;
Michael Wright28f24d02016-07-12 13:30:53 -0700330
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200331 status_t setActiveColorMode(PhysicalDisplayId, ui::ColorMode, ui::RenderIntent) override;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600332
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800333 // Composer 2.4
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 DisplayConnectionType getDisplayConnectionType(PhysicalDisplayId) const override;
335 bool isVsyncPeriodSwitchSupported(PhysicalDisplayId) const override;
336 nsecs_t getDisplayVsyncPeriod(PhysicalDisplayId) const override;
337 status_t setActiveConfigWithConstraints(PhysicalDisplayId, size_t configId,
Marin Shalamanova524a092020-07-27 21:39:55 +0200338 const hal::VsyncPeriodChangeConstraints&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700339 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200340 status_t setAutoLowLatencyMode(PhysicalDisplayId, bool) override;
341 status_t getSupportedContentTypes(PhysicalDisplayId, std::vector<hal::ContentType>*) override;
342 status_t setContentType(PhysicalDisplayId, hal::ContentType) override;
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800343
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800344 const std::unordered_map<std::string, bool>& getSupportedLayerGenericMetadata() const override;
345
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700346 // for debugging ----------------------------------------------------------
Lloyd Pique441d5042018-10-18 16:49:51 -0700347 void dump(std::string& out) const override;
Mathias Agopian83727852010-09-23 18:13:21 -0700348
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800349 Hwc2::Composer* getComposer() const override { return mComposer.get(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800350
Dominik Laskowski075d3172018-05-24 15:50:06 -0700351 // TODO(b/74619554): Remove special cases for internal/external display.
Peiyong Line9d809e2020-04-14 13:10:48 -0700352 std::optional<hal::HWDisplayId> getInternalHwcDisplayId() const override {
Lloyd Pique441d5042018-10-18 16:49:51 -0700353 return mInternalHwcDisplayId;
354 }
Peiyong Line9d809e2020-04-14 13:10:48 -0700355 std::optional<hal::HWDisplayId> getExternalHwcDisplayId() const override {
Lloyd Pique441d5042018-10-18 16:49:51 -0700356 return mExternalHwcDisplayId;
357 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700358
Marin Shalamanova524a092020-07-27 21:39:55 +0200359 std::optional<PhysicalDisplayId> toPhysicalDisplayId(hal::HWDisplayId) const override;
360 std::optional<hal::HWDisplayId> fromPhysicalDisplayId(PhysicalDisplayId) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700361
Mathias Agopiana350ff92010-08-10 17:14:02 -0700362private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800363 // For unit tests
364 friend TestableSurfaceFlinger;
365
Marin Shalamanova524a092020-07-27 21:39:55 +0200366 std::optional<DisplayIdentificationInfo> onHotplugConnect(hal::HWDisplayId);
367 std::optional<DisplayIdentificationInfo> onHotplugDisconnect(hal::HWDisplayId);
368 bool shouldIgnoreHotplugConnect(hal::HWDisplayId, bool hasDisplayIdentificationData) const;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100369
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800370 void loadCapabilities();
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800371 void loadLayerMetadataSupport();
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800372 uint32_t getMaxVirtualDisplayCount() const;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700373
Mathias Agopiane60b0682012-08-21 23:34:09 -0700374 struct DisplayData {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700375 bool isVirtual = false;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800376 std::unique_ptr<HWC2::Display> hwcDisplay;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700377 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700378 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700379 buffer_handle_t outbufHandle = nullptr;
380 sp<Fence> outbufAcquireFence = Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800381 mutable std::unordered_map<int32_t,
382 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800383
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700384 bool validateWasSkipped;
Peiyong Line9d809e2020-04-14 13:10:48 -0700385 hal::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800386
387 bool vsyncTraceToggle = false;
388
389 std::mutex vsyncEnabledLock;
Peiyong Line9d809e2020-04-14 13:10:48 -0700390 hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800391
392 mutable std::mutex lastHwVsyncLock;
393 nsecs_t lastHwVsync GUARDED_BY(lastHwVsyncLock) = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700394 };
395
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200396 std::unordered_map<HalDisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800397
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800398 std::unique_ptr<android::Hwc2::Composer> mComposer;
Peiyong Line9d809e2020-04-14 13:10:48 -0700399 std::unordered_set<hal::Capability> mCapabilities;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800400 std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800401 bool mRegisteredCallback = false;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800402
Marin Shalamanova524a092020-07-27 21:39:55 +0200403 std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
Peiyong Line9d809e2020-04-14 13:10:48 -0700404 std::optional<hal::HWDisplayId> mInternalHwcDisplayId;
405 std::optional<hal::HWDisplayId> mExternalHwcDisplayId;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700406 bool mHasMultiDisplaySupport = false;
407
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200408 RandomDisplayIdGenerator<HalVirtualDisplayId> mVirtualIdGenerator{getMaxVirtualDisplayCount()};
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200409
410 const bool mUpdateDeviceProductInfoOnHotplugReconnect;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700411};
412
Lloyd Pique441d5042018-10-18 16:49:51 -0700413} // namespace impl
Dominik Laskowski1af47932018-11-12 10:20:46 -0800414} // namespace android
Mathias Agopiana350ff92010-08-10 17:14:02 -0700415
416#endif // ANDROID_SF_HWCOMPOSER_H