blob: a8055663f026cf8fee10a84b7df234216d750d9f [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>
Ady Abraham6e60b142022-01-06 18:10:35 -080042#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050043#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050044#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050045
Dan Stoza651bf312015-10-23 17:03:17 -070046namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080047
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070048class Fence;
49class FloatRect;
50class GraphicBuffer;
51class TestableSurfaceFlinger;
52struct DisplayedFrameStats;
53
54namespace Hwc2 {
55class Composer;
56} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070057
58namespace HWC2 {
59
Dan Stoza651bf312015-10-23 17:03:17 -070060class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070061
62namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070063
Steven Thomas94e35b92017-07-26 18:48:28 -070064// Implement this interface to receive hardware composer events.
65//
66// These callback functions will generally be called on a hwbinder thread, but
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070067// when first registering the callback the onComposerHalHotplug() function will
Steven Thomas94e35b92017-07-26 18:48:28 -070068// immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070069struct ComposerCallback {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070070 virtual void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) = 0;
71 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
72 virtual void onComposerHalVsync(hal::HWDisplayId, int64_t timestamp,
73 std::optional<hal::VsyncPeriodNanos>) = 0;
74 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
75 const hal::VsyncPeriodChangeTimeline&) = 0;
76 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080077 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070078
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070079protected:
80 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070081};
Dan Stoza651bf312015-10-23 17:03:17 -070082
Peiyong Line9d809e2020-04-14 13:10:48 -070083// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080084class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070085public:
Ana Krulec4593b692019-01-11 22:07:25 -080086 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -070087
Peiyong Line9d809e2020-04-14 13:10:48 -070088 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -080089 virtual bool isConnected() const = 0;
90 virtual void setConnected(bool connected) = 0; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -050091 virtual bool hasCapability(
92 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070093 virtual bool isVsyncPeriodSwitchSupported() const = 0;
ramindani32cf0602022-03-02 02:30:29 +000094 virtual bool hasDisplayIdleTimerCapability() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -070095 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Dan Stoza651bf312015-10-23 17:03:17 -070096
ramindani86d3d982022-03-16 20:57:42 +000097 [[nodiscard]] virtual hal::Error acceptChanges() = 0;
98 [[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
Lloyd Piquea516c002021-05-07 14:36:58 -070099 createLayer() = 0;
ramindani86d3d982022-03-16 20:57:42 +0000100 [[nodiscard]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500101 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
102 outTypes) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000103 [[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800104 // Returns a bitmask which contains HdrMetadata::Type::*.
ramindani86d3d982022-03-16 20:57:42 +0000105 [[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
106 [[nodiscard]] virtual hal::Error getRenderIntents(
Peiyong Line9d809e2020-04-14 13:10:48 -0700107 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000108 [[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
109 android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800110
ramindani86d3d982022-03-16 20:57:42 +0000111 [[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
112 [[nodiscard]] virtual hal::Error getRequests(
Peiyong Line9d809e2020-04-14 13:10:48 -0700113 hal::DisplayRequest* outDisplayRequests,
114 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000115 [[nodiscard]] virtual hal::Error getConnectionType(ui::DisplayConnectionType*) const = 0;
116 [[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
117 [[nodiscard]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800118 android::HdrCapabilities* outCapabilities) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000119 [[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700120 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800121 uint8_t* outComponentMask) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000122 [[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
123 uint8_t componentMask,
124 uint64_t maxFrames) const = 0;
125 [[nodiscard]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800126 uint64_t maxFrames, uint64_t timestamp,
127 android::DisplayedFrameStats* outStats) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000128 [[nodiscard]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800129 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000130 [[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
131 [[nodiscard]] virtual hal::Error setClientTarget(
Ana Krulec4593b692019-01-11 22:07:25 -0800132 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Peiyong Line9d809e2020-04-14 13:10:48 -0700133 const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000134 [[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
135 hal::RenderIntent renderIntent) = 0;
136 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
137 [[nodiscard]] virtual hal::Error setOutputBuffer(
Ana Krulec4593b692019-01-11 22:07:25 -0800138 const android::sp<android::GraphicBuffer>& buffer,
139 const android::sp<android::Fence>& releaseFence) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000140 [[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
141 [[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
142 [[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
143 uint32_t* outNumRequests) = 0;
144 [[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
145 uint32_t* outNumTypes,
146 uint32_t* outNumRequests,
147 android::sp<android::Fence>* outPresentFence,
148 uint32_t* state) = 0;
149 [[nodiscard]] virtual std::future<hal::Error> setDisplayBrightness(
Alec Mouricdf16792021-12-10 13:16:06 -0800150 float brightness, const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000151 [[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100152 hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700153 hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000154 [[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
155 [[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
156 [[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
Kriti Dang7defaf32021-11-15 11:55:43 +0100157 hal::HWConfigId* configId) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000158 [[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
159 [[nodiscard]] virtual hal::Error getSupportedContentTypes(
Peiyong Line9d809e2020-04-14 13:10:48 -0700160 std::vector<hal::ContentType>*) const = 0;
ramindani86d3d982022-03-16 20:57:42 +0000161 [[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
162 [[nodiscard]] virtual hal::Error getClientTargetProperty(
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700163 hal::ClientTargetProperty* outClientTargetProperty, float* outWhitePointNits) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000164 [[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500165 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
166 support) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000167 [[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
168 [[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
ramindani06e518e2022-03-14 18:47:53 +0000169 Hwc2::AidlTransform* outTransform) const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800170};
171
172namespace impl {
173
Lloyd Piquea516c002021-05-07 14:36:58 -0700174class Layer;
175
Ana Krulec4593b692019-01-11 22:07:25 -0800176class Display : public HWC2::Display {
177public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800178 Display(android::Hwc2::Composer&,
179 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
180 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800181 ~Display() override;
182
183 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700184 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700185 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700186 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500187 std::unordered_map<HWC2::Layer*,
188 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
189 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700190 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800191 // Returns a bitmask which contains HdrMetadata::Type::*.
192 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700193 hal::Error getRenderIntents(hal::ColorMode colorMode,
194 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200195 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700196
Peiyong Line9d809e2020-04-14 13:10:48 -0700197 hal::Error getName(std::string* outName) const override;
198 hal::Error getRequests(
199 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700200 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100201 hal::Error getConnectionType(ui::DisplayConnectionType*) const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700202 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700203 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
204 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
205 hal::Dataspace* outDataspace,
206 uint8_t* outComponentMask) const override;
207 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
208 uint64_t maxFrames) const override;
209 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
210 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700211 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
212 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700213 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700214 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
215 const android::sp<android::Fence>& acquireFence,
216 hal::Dataspace dataspace) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200217 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800218 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200219 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700220 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200221 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700222 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
Ady Abraham43065bd2021-12-10 17:22:15 -0800223 hal::Error validate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
224 uint32_t* outNumRequests) override;
225 hal::Error presentOrValidate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
226 uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700227 android::sp<android::Fence>* outPresentFence,
228 uint32_t* state) override;
Alec Mouricdf16792021-12-10 13:16:06 -0800229 std::future<hal::Error> setDisplayBrightness(
230 float brightness, const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100231 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
232 const hal::VsyncPeriodChangeConstraints& constraints,
233 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100234 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
235 hal::Error clearBootDisplayConfig() override;
236 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700237 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700238 hal::Error getSupportedContentTypes(
239 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700240 hal::Error setContentType(hal::ContentType) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700241 hal::Error getClientTargetProperty(hal::ClientTargetProperty* outClientTargetProperty,
242 float* outWhitePointNits) override;
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -0500243 hal::Error getDisplayDecorationSupport(
244 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
245 support) override;
ramindani32cf0602022-03-02 02:30:29 +0000246 hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700247
Dan Stoza651bf312015-10-23 17:03:17 -0700248 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700249 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800250 bool isConnected() const override { return mIsConnected; }
251 void setConnected(bool connected) override; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500252 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
253 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700254 bool isVsyncPeriodSwitchSupported() const override;
ramindani32cf0602022-03-02 02:30:29 +0000255 bool hasDisplayIdleTimerCapability() const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700256 void onLayerDestroyed(hal::HWLayerId layerId) override;
ramindani06e518e2022-03-14 18:47:53 +0000257 hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
Dan Stoza651bf312015-10-23 17:03:17 -0700258
259private:
Dan Stoza651bf312015-10-23 17:03:17 -0700260
Dan Stoza651bf312015-10-23 17:03:17 -0700261 // This may fail (and return a null pointer) if no layer with this ID exists
262 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700263 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700264
Lloyd Piquebc792092018-01-17 11:52:30 -0800265 friend android::TestableSurfaceFlinger;
266
Dan Stoza651bf312015-10-23 17:03:17 -0700267 // Member variables
268
Steven Thomas94e35b92017-07-26 18:48:28 -0700269 // These are references to data owned by HWC2::Device, which will outlive
270 // this HWC2::Display, so these references are guaranteed to be valid for
271 // the lifetime of this object.
272 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800273 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
274 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700275
Peiyong Line9d809e2020-04-14 13:10:48 -0700276 const hal::HWDisplayId mId;
277 hal::DisplayType mType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800278 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700279
Lloyd Piquea516c002021-05-07 14:36:58 -0700280 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
281 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700282
Ady Abraham27fbcc72021-09-20 14:54:57 -0700283 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700284 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500285 std::optional<
286 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
287 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Dan Stoza651bf312015-10-23 17:03:17 -0700288};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800289
Ana Krulec4593b692019-01-11 22:07:25 -0800290} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700291
Lloyd Pique35d58242018-12-18 16:33:25 -0800292class Layer {
293public:
294 virtual ~Layer();
295
Peiyong Line9d809e2020-04-14 13:10:48 -0700296 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800297
ramindani86d3d982022-03-16 20:57:42 +0000298 [[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
299 [[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
300 const android::sp<android::GraphicBuffer>& buffer,
301 const android::sp<android::Fence>& acquireFence) = 0;
302 [[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800303
ramindani86d3d982022-03-16 20:57:42 +0000304 [[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
305 [[nodiscard]] virtual hal::Error setColor(
Ady Abraham6e60b142022-01-06 18:10:35 -0800306 aidl::android::hardware::graphics::composer3::Color color) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000307 [[nodiscard]] virtual hal::Error setCompositionType(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500308 aidl::android::hardware::graphics::composer3::Composition type) = 0;
ramindani86d3d982022-03-16 20:57:42 +0000309 [[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
310 [[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
311 const android::HdrMetadata& metadata) = 0;
312 [[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
313 [[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
314 [[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
315 [[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
316 [[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
317 [[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
318 [[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800319
320 // Composer HAL 2.3
ramindani86d3d982022-03-16 20:57:42 +0000321 [[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800322
323 // Composer HAL 2.4
ramindani86d3d982022-03-16 20:57:42 +0000324 [[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
325 bool mandatory,
326 const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700327
328 // AIDL HAL
ramindani86d3d982022-03-16 20:57:42 +0000329 [[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
330 [[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800331};
332
333namespace impl {
334
Peiyong Line9d809e2020-04-14 13:10:48 -0700335// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800336
337class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700338public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700339 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800340 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
341 capabilities,
342 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800343 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700344
Lloyd Piquea516c002021-05-07 14:36:58 -0700345 void onOwningDisplayDestroyed();
346
Peiyong Line9d809e2020-04-14 13:10:48 -0700347 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700348
Peiyong Line9d809e2020-04-14 13:10:48 -0700349 hal::Error setCursorPosition(int32_t x, int32_t y) override;
350 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
351 const android::sp<android::Fence>& acquireFence) override;
352 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700353
Peiyong Line9d809e2020-04-14 13:10:48 -0700354 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800355 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500356 hal::Error setCompositionType(
357 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700358 hal::Error setDataspace(hal::Dataspace dataspace) override;
359 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
360 const android::HdrMetadata& metadata) override;
361 hal::Error setDisplayFrame(const android::Rect& frame) override;
362 hal::Error setPlaneAlpha(float alpha) override;
363 hal::Error setSidebandStream(const native_handle_t* stream) override;
364 hal::Error setSourceCrop(const android::FloatRect& crop) override;
365 hal::Error setTransform(hal::Transform transform) override;
366 hal::Error setVisibleRegion(const android::Region& region) override;
367 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700368
Peiyong Lin698147a2018-09-14 13:27:18 -0700369 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700370 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700371
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800372 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700373 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
374 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800375
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700376 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800377 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500378 hal::Error setBlockingRegion(const android::Region& region) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700379
Dan Stoza651bf312015-10-23 17:03:17 -0700380private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700381 // These are references to data owned by HWC2::Device, which will outlive
382 // this HWC2::Layer, so these references are guaranteed to be valid for
383 // the lifetime of this object.
384 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800385 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
386 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700387
Lloyd Piquea516c002021-05-07 14:36:58 -0700388 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700389 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800390
391 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
392 // multiple times.
393 android::Region mVisibleRegion = android::Region::INVALID_REGION;
394 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500395 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700396 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700397 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700398 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800399 uint32_t mBufferSlot;
Dan Stoza651bf312015-10-23 17:03:17 -0700400};
401
Lloyd Pique35d58242018-12-18 16:33:25 -0800402} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700403} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700404} // namespace android