blob: cca20bd47fefd38e3f4c8ccabf766611709399fb [file] [log] [blame]
Dan Stoza651bf312015-10-23 17:03:17 -07001/*
2 * Copyright 2015 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 Laskowski8b01cc02020-07-14 19:02:41 -070017#pragma once
Dan Stoza651bf312015-10-23 17:03:17 -070018
Lloyd Piquea516c002021-05-07 14:36:58 -070019#include <android-base/expected.h>
Ady Abraham27fbcc72021-09-20 14:54:57 -070020#include <android-base/thread_annotations.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070021#include <gui/HdrMetadata.h>
Mathias Agopian1d77b712017-02-17 15:46:13 -080022#include <math/mat4.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070023#include <ui/HdrCapabilities.h>
Yichi Chen8366f562019-03-25 19:44:06 +080024#include <ui/Region.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010025#include <ui/StaticDisplayInfo.h>
Dan Stoza651bf312015-10-23 17:03:17 -070026#include <utils/Log.h>
27#include <utils/StrongPointer.h>
28#include <utils/Timers.h>
29
30#include <functional>
Dominik Laskowski5690bde2020-04-23 19:04:22 -070031#include <future>
Dan Stoza651bf312015-10-23 17:03:17 -070032#include <string>
33#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070034#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070035#include <vector>
36
Alec Mouricdf16792021-12-10 13:16:06 -080037#include "ComposerHal.h"
Peiyong Line9d809e2020-04-14 13:10:48 -070038#include "Hal.h"
39
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050040#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
Ady Abrahamde549d42022-01-26 19:19:17 -080041#include <aidl/android/hardware/graphics/composer3/Capability.h>
Alec Mouri85065692022-03-18 00:58:26 +000042#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
Ady Abraham6e60b142022-01-06 18:10:35 -080043#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050044#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050045#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050046
Dan Stoza651bf312015-10-23 17:03:17 -070047namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080048
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070049class Fence;
50class FloatRect;
51class GraphicBuffer;
52class TestableSurfaceFlinger;
53struct DisplayedFrameStats;
54
55namespace Hwc2 {
56class Composer;
57} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070058
59namespace HWC2 {
60
Dan Stoza651bf312015-10-23 17:03:17 -070061class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070062
63namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070064
Steven Thomas94e35b92017-07-26 18:48:28 -070065// Implement this interface to receive hardware composer events.
66//
67// These callback functions will generally be called on a hwbinder thread, but
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070068// when first registering the callback the onComposerHalHotplug() function will
Steven Thomas94e35b92017-07-26 18:48:28 -070069// immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070070struct ComposerCallback {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070071 virtual void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) = 0;
72 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
73 virtual void onComposerHalVsync(hal::HWDisplayId, int64_t timestamp,
74 std::optional<hal::VsyncPeriodNanos>) = 0;
75 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
76 const hal::VsyncPeriodChangeTimeline&) = 0;
77 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080078 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070079
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070080protected:
81 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070082};
Dan Stoza651bf312015-10-23 17:03:17 -070083
Peiyong Line9d809e2020-04-14 13:10:48 -070084// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080085class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070086public:
Ana Krulec4593b692019-01-11 22:07:25 -080087 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -070088
Peiyong Line9d809e2020-04-14 13:10:48 -070089 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -080090 virtual bool isConnected() const = 0;
91 virtual void setConnected(bool connected) = 0; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -050092 virtual bool hasCapability(
93 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070094 virtual bool isVsyncPeriodSwitchSupported() const = 0;
ramindani32cf0602022-03-02 02:30:29 +000095 virtual bool hasDisplayIdleTimerCapability() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -070096 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Dan Stoza651bf312015-10-23 17:03:17 -070097
ramindani86d3d982022-03-16 20:57:42 +000098 [[nodiscard]] virtual hal::Error acceptChanges() = 0;
99 [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
Lloyd Piquea516c002021-05-07 14:36:58 -0700100 createLayer() = 0;
ramindani86d3d982022-03-16 20:57:42 +0000101 [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500102 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
103 outTypes) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000104 [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800105 // Returns a bitmask which contains HdrMetadata::Type::*.
ramindani86d3d982022-03-16 20:57:42 +0000106 [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
107 [[nodiscard]] virtual hal::Error getRenderIntents(
Peiyong Line9d809e2020-04-14 13:10:48 -0700108 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000109 [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
110 android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800111
ramindani86d3d982022-03-16 20:57:42 +0000112 [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
113 [[nodiscard]] virtual hal::Error getRequests(
Peiyong Line9d809e2020-04-14 13:10:48 -0700114 hal::DisplayRequest* outDisplayRequests,
115 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000116 [[nodiscard]] virtual hal::Error getConnectionType(ui::DisplayConnectionType*) const = 0;
117 [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
118 [[nodiscard]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800119 android::HdrCapabilities* outCapabilities) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000120 [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700121 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800122 uint8_t* outComponentMask) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000123 [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
124 uint8_t componentMask,
125 uint64_t maxFrames) const = 0;
126 [[nodiscard]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800127 uint64_t maxFrames, uint64_t timestamp,
128 android::DisplayedFrameStats* outStats) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000129 [[nodiscard]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800130 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000131 [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
132 [[nodiscard]] virtual hal::Error setClientTarget(
Ana Krulec4593b692019-01-11 22:07:25 -0800133 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Peiyong Line9d809e2020-04-14 13:10:48 -0700134 const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000135 [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
136 hal::RenderIntent renderIntent) = 0;
137 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
138 [[nodiscard]] virtual hal::Error setOutputBuffer(
Ana Krulec4593b692019-01-11 22:07:25 -0800139 const android::sp<android::GraphicBuffer>& buffer,
140 const android::sp<android::Fence>& releaseFence) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000141 [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
142 [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
143 [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
144 uint32_t* outNumRequests) = 0;
145 [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
146 uint32_t* outNumTypes,
147 uint32_t* outNumRequests,
148 android::sp<android::Fence>* outPresentFence,
149 uint32_t* state) = 0;
150 [[nodiscard]] virtual std::future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000151 float brightness, float brightnessNits,
152 const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000153 [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100154 hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700155 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000156 [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
157 [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
158 [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
Kriti Dang7defaf32021-11-15 11:55:43 +0100159 hal::HWConfigId* configId) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000160 [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
161 [[nodiscard]] virtual hal::Error getSupportedContentTypes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700162 std::vector<hal::ContentType>*) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000163 [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
164 [[nodiscard]] virtual hal::Error getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +0000165 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
166 outClientTargetProperty) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000167 [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500168 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
169 support) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000170 [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
171 [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
ramindani06e518e2022-03-14 18:47:53 +0000172 Hwc2::AidlTransform* outTransform) const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800173};
174
175namespace impl {
176
Lloyd Piquea516c002021-05-07 14:36:58 -0700177class Layer;
178
Ana Krulec4593b692019-01-11 22:07:25 -0800179class Display : public HWC2::Display {
180public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800181 Display(android::Hwc2::Composer&,
182 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
183 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800184 ~Display() override;
185
186 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700187 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700188 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700189 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500190 std::unordered_map<HWC2::Layer*,
191 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
192 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700193 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800194 // Returns a bitmask which contains HdrMetadata::Type::*.
195 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700196 hal::Error getRenderIntents(hal::ColorMode colorMode,
197 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200198 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700199
Peiyong Line9d809e2020-04-14 13:10:48 -0700200 hal::Error getName(std::string* outName) const override;
201 hal::Error getRequests(
202 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700203 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100204 hal::Error getConnectionType(ui::DisplayConnectionType*) const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700205 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700206 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
207 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
208 hal::Dataspace* outDataspace,
209 uint8_t* outComponentMask) const override;
210 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
211 uint64_t maxFrames) const override;
212 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
213 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700214 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
215 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700216 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700217 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
218 const android::sp<android::Fence>& acquireFence,
219 hal::Dataspace dataspace) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200220 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800221 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700223 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200224 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700225 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
Ady Abraham43065bd2021-12-10 17:22:15 -0800226 hal::Error validate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
227 uint32_t* outNumRequests) override;
228 hal::Error presentOrValidate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
229 uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700230 android::sp<android::Fence>* outPresentFence,
231 uint32_t* state) override;
Alec Mouricdf16792021-12-10 13:16:06 -0800232 std::future<hal::Error> setDisplayBrightness(
Alec Mouri4d8a05d2022-03-23 18:14:26 +0000233 float brightness, float brightnessNits,
234 const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100235 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
236 const hal::VsyncPeriodChangeConstraints& constraints,
237 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100238 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
239 hal::Error clearBootDisplayConfig() override;
240 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700241 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700242 hal::Error getSupportedContentTypes(
243 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700244 hal::Error setContentType(hal::ContentType) override;
Alec Mouri85065692022-03-18 00:58:26 +0000245 hal::Error getClientTargetProperty(
246 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
247 outClientTargetProperty) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500248 hal::Error getDisplayDecorationSupport(
249 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
250 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000251 hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700252
Dan Stoza651bf312015-10-23 17:03:17 -0700253 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700254 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800255 bool isConnected() const override { return mIsConnected; }
256 void setConnected(bool connected) override; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500257 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
258 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700259 bool isVsyncPeriodSwitchSupported() const override;
ramindani32cf0602022-03-02 02:30:29 +0000260 bool hasDisplayIdleTimerCapability() const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700261 void onLayerDestroyed(hal::HWLayerId layerId) override;
ramindani06e518e2022-03-14 18:47:53 +0000262 hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
Dan Stoza651bf312015-10-23 17:03:17 -0700263
264private:
Dan Stoza651bf312015-10-23 17:03:17 -0700265
Dan Stoza651bf312015-10-23 17:03:17 -0700266 // This may fail (and return a null pointer) if no layer with this ID exists
267 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700268 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700269
Lloyd Piquebc792092018-01-17 11:52:30 -0800270 friend android::TestableSurfaceFlinger;
271
Dan Stoza651bf312015-10-23 17:03:17 -0700272 // Member variables
273
Steven Thomas94e35b92017-07-26 18:48:28 -0700274 // These are references to data owned by HWC2::Device, which will outlive
275 // this HWC2::Display, so these references are guaranteed to be valid for
276 // the lifetime of this object.
277 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800278 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
279 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700280
Peiyong Line9d809e2020-04-14 13:10:48 -0700281 const hal::HWDisplayId mId;
282 hal::DisplayType mType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800283 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700284
Lloyd Piquea516c002021-05-07 14:36:58 -0700285 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
286 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700287
Ady Abraham27fbcc72021-09-20 14:54:57 -0700288 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700289 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500290 std::optional<
291 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
292 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Dan Stoza651bf312015-10-23 17:03:17 -0700293};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800294
Ana Krulec4593b692019-01-11 22:07:25 -0800295} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700296
Lloyd Pique35d58242018-12-18 16:33:25 -0800297class Layer {
298public:
299 virtual ~Layer();
300
Peiyong Line9d809e2020-04-14 13:10:48 -0700301 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800302
ramindani86d3d982022-03-16 20:57:42 +0000303 [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
304 [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
305 const android::sp<android::GraphicBuffer>& buffer,
306 const android::sp<android::Fence>& acquireFence) = 0;
307 [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800308
ramindani86d3d982022-03-16 20:57:42 +0000309 [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
310 [[nodiscard]] virtual hal::Error setColor(
Ady Abraham6e60b142022-01-06 18:10:35 -0800311 aidl::android::hardware::graphics::composer3::Color color) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000312 [[nodiscard]] virtual hal::Error setCompositionType(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500313 aidl::android::hardware::graphics::composer3::Composition type) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000314 [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
315 [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
316 const android::HdrMetadata& metadata) = 0;
317 [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
318 [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
319 [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
320 [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
321 [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
322 [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
323 [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800324
325 // Composer HAL 2.3
ramindani86d3d982022-03-16 20:57:42 +0000326 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800327
328 // Composer HAL 2.4
ramindani86d3d982022-03-16 20:57:42 +0000329 [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
330 bool mandatory,
331 const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700332
333 // AIDL HAL
ramindani86d3d982022-03-16 20:57:42 +0000334 [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
335 [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800336};
337
338namespace impl {
339
Peiyong Line9d809e2020-04-14 13:10:48 -0700340// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800341
342class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700343public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700344 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800345 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
346 capabilities,
347 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800348 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700349
Lloyd Piquea516c002021-05-07 14:36:58 -0700350 void onOwningDisplayDestroyed();
351
Peiyong Line9d809e2020-04-14 13:10:48 -0700352 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700353
Peiyong Line9d809e2020-04-14 13:10:48 -0700354 hal::Error setCursorPosition(int32_t x, int32_t y) override;
355 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
356 const android::sp<android::Fence>& acquireFence) override;
357 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700358
Peiyong Line9d809e2020-04-14 13:10:48 -0700359 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800360 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500361 hal::Error setCompositionType(
362 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700363 hal::Error setDataspace(hal::Dataspace dataspace) override;
364 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
365 const android::HdrMetadata& metadata) override;
366 hal::Error setDisplayFrame(const android::Rect& frame) override;
367 hal::Error setPlaneAlpha(float alpha) override;
368 hal::Error setSidebandStream(const native_handle_t* stream) override;
369 hal::Error setSourceCrop(const android::FloatRect& crop) override;
370 hal::Error setTransform(hal::Transform transform) override;
371 hal::Error setVisibleRegion(const android::Region& region) override;
372 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700373
Peiyong Lin698147a2018-09-14 13:27:18 -0700374 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700375 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700376
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800377 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700378 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
379 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800380
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700381 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800382 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500383 hal::Error setBlockingRegion(const android::Region& region) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700384
Dan Stoza651bf312015-10-23 17:03:17 -0700385private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700386 // These are references to data owned by HWC2::Device, which will outlive
387 // this HWC2::Layer, so these references are guaranteed to be valid for
388 // the lifetime of this object.
389 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800390 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
391 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700392
Lloyd Piquea516c002021-05-07 14:36:58 -0700393 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700394 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800395
396 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
397 // multiple times.
398 android::Region mVisibleRegion = android::Region::INVALID_REGION;
399 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500400 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700401 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700402 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700403 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800404 uint32_t mBufferSlot;
Dan Stoza651bf312015-10-23 17:03:17 -0700405};
406
Lloyd Pique35d58242018-12-18 16:33:25 -0800407} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700408} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700409} // namespace android