blob: a37b8dd44ea521483e6418bed0527ff56c7c64b5 [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
Ady Abrahamde549d42022-01-26 19:19:17 -080040#include <aidl/android/hardware/graphics/composer3/Capability.h>
Ady Abraham6e60b142022-01-06 18:10:35 -080041#include <aidl/android/hardware/graphics/composer3/Color.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050042#include <aidl/android/hardware/graphics/composer3/Composition.h>
Leon Scroggins III5967aec2021-12-29 11:14:22 -050043#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050044
Dan Stoza651bf312015-10-23 17:03:17 -070045namespace android {
Lloyd Piquebc792092018-01-17 11:52:30 -080046
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070047class Fence;
48class FloatRect;
49class GraphicBuffer;
50class TestableSurfaceFlinger;
51struct DisplayedFrameStats;
52
53namespace Hwc2 {
54class Composer;
55} // namespace Hwc2
Dan Stoza651bf312015-10-23 17:03:17 -070056
57namespace HWC2 {
58
Dan Stoza651bf312015-10-23 17:03:17 -070059class Layer;
Peiyong Line9d809e2020-04-14 13:10:48 -070060
61namespace hal = android::hardware::graphics::composer::hal;
Dan Stoza651bf312015-10-23 17:03:17 -070062
Steven Thomas94e35b92017-07-26 18:48:28 -070063// Implement this interface to receive hardware composer events.
64//
65// These callback functions will generally be called on a hwbinder thread, but
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070066// when first registering the callback the onComposerHalHotplug() function will
Steven Thomas94e35b92017-07-26 18:48:28 -070067// immediately be called on the thread calling registerCallback().
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070068struct ComposerCallback {
Dominik Laskowski0deb06e2021-04-16 23:18:31 -070069 virtual void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) = 0;
70 virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
71 virtual void onComposerHalVsync(hal::HWDisplayId, int64_t timestamp,
72 std::optional<hal::VsyncPeriodNanos>) = 0;
73 virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
74 const hal::VsyncPeriodChangeTimeline&) = 0;
75 virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
Yichi Chen3401b562022-01-17 15:42:35 +080076 virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070077
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070078protected:
79 ~ComposerCallback() = default;
Steven Thomas94e35b92017-07-26 18:48:28 -070080};
Dan Stoza651bf312015-10-23 17:03:17 -070081
Peiyong Line9d809e2020-04-14 13:10:48 -070082// Convenience C++ class to access per display functions directly.
Ana Krulec4593b692019-01-11 22:07:25 -080083class Display {
Dan Stoza651bf312015-10-23 17:03:17 -070084public:
Ana Krulec4593b692019-01-11 22:07:25 -080085 virtual ~Display();
Dan Stoza651bf312015-10-23 17:03:17 -070086
Peiyong Line9d809e2020-04-14 13:10:48 -070087 virtual hal::HWDisplayId getId() const = 0;
Ana Krulec4593b692019-01-11 22:07:25 -080088 virtual bool isConnected() const = 0;
89 virtual void setConnected(bool connected) = 0; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -050090 virtual bool hasCapability(
91 aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
Ady Abraham7159f572019-10-11 11:10:18 -070092 virtual bool isVsyncPeriodSwitchSupported() const = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -070093 virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
Dan Stoza651bf312015-10-23 17:03:17 -070094
Peiyong Line9d809e2020-04-14 13:10:48 -070095 [[clang::warn_unused_result]] virtual hal::Error acceptChanges() = 0;
Lloyd Piquea516c002021-05-07 14:36:58 -070096 [[clang::warn_unused_result]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
97 createLayer() = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -070098 [[clang::warn_unused_result]] virtual hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050099 std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
100 outTypes) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700101 [[clang::warn_unused_result]] virtual hal::Error getColorModes(
102 std::vector<hal::ColorMode>* outModes) const = 0;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800103 // Returns a bitmask which contains HdrMetadata::Type::*.
Ana Krulec4593b692019-01-11 22:07:25 -0800104 [[clang::warn_unused_result]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700105 [[clang::warn_unused_result]] virtual hal::Error getRenderIntents(
106 hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
107 [[clang::warn_unused_result]] virtual hal::Error getDataspaceSaturationMatrix(
108 hal::Dataspace dataspace, android::mat4* outMatrix) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800109
Peiyong Line9d809e2020-04-14 13:10:48 -0700110 [[clang::warn_unused_result]] virtual hal::Error getName(std::string* outName) const = 0;
111 [[clang::warn_unused_result]] virtual hal::Error getRequests(
112 hal::DisplayRequest* outDisplayRequests,
113 std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
114 [[clang::warn_unused_result]] virtual hal::Error getConnectionType(
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100115 ui::DisplayConnectionType*) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700116 [[clang::warn_unused_result]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
117 [[clang::warn_unused_result]] virtual hal::Error getHdrCapabilities(
Ana Krulec4593b692019-01-11 22:07:25 -0800118 android::HdrCapabilities* outCapabilities) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700119 [[clang::warn_unused_result]] virtual hal::Error getDisplayedContentSamplingAttributes(
120 hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
Ana Krulec4593b692019-01-11 22:07:25 -0800121 uint8_t* outComponentMask) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700122 [[clang::warn_unused_result]] virtual hal::Error setDisplayContentSamplingEnabled(
Ana Krulec4593b692019-01-11 22:07:25 -0800123 bool enabled, uint8_t componentMask, uint64_t maxFrames) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700124 [[clang::warn_unused_result]] virtual hal::Error getDisplayedContentSample(
Ana Krulec4593b692019-01-11 22:07:25 -0800125 uint64_t maxFrames, uint64_t timestamp,
126 android::DisplayedFrameStats* outStats) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700127 [[clang::warn_unused_result]] virtual hal::Error getReleaseFences(
Ana Krulec4593b692019-01-11 22:07:25 -0800128 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700129 [[clang::warn_unused_result]] virtual hal::Error present(
Ana Krulec4593b692019-01-11 22:07:25 -0800130 android::sp<android::Fence>* outPresentFence) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700131 [[clang::warn_unused_result]] 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;
134 [[clang::warn_unused_result]] virtual hal::Error setColorMode(
135 hal::ColorMode mode, hal::RenderIntent renderIntent) = 0;
136 [[clang::warn_unused_result]] virtual hal::Error setColorTransform(
Ady Abrahamdc011a92021-12-21 14:06:44 -0800137 const android::mat4& matrix) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700138 [[clang::warn_unused_result]] 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;
Peiyong Line9d809e2020-04-14 13:10:48 -0700141 [[clang::warn_unused_result]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
142 [[clang::warn_unused_result]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
Ady Abraham43065bd2021-12-10 17:22:15 -0800143 [[clang::warn_unused_result]] virtual hal::Error validate(nsecs_t expectedPresentTime,
144 uint32_t* outNumTypes,
Peiyong Line9d809e2020-04-14 13:10:48 -0700145 uint32_t* outNumRequests) = 0;
146 [[clang::warn_unused_result]] virtual hal::Error presentOrValidate(
Ady Abraham43065bd2021-12-10 17:22:15 -0800147 nsecs_t expectedPresentTime, uint32_t* outNumTypes, uint32_t* outNumRequests,
Ana Krulec4593b692019-01-11 22:07:25 -0800148 android::sp<android::Fence>* outPresentFence, uint32_t* state) = 0;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700149 [[clang::warn_unused_result]] virtual std::future<hal::Error> setDisplayBrightness(
Alec Mouricdf16792021-12-10 13:16:06 -0800150 float brightness, const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700151 [[clang::warn_unused_result]] 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;
Kriti Dang7defaf32021-11-15 11:55:43 +0100154 [[clang::warn_unused_result]] virtual hal::Error setBootDisplayConfig(
155 hal::HWConfigId configId) = 0;
156 [[clang::warn_unused_result]] virtual hal::Error clearBootDisplayConfig() = 0;
157 [[clang::warn_unused_result]] virtual hal::Error getPreferredBootDisplayConfig(
158 hal::HWConfigId* configId) const = 0;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700159 [[clang::warn_unused_result]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700160 [[clang::warn_unused_result]] virtual hal::Error getSupportedContentTypes(
161 std::vector<hal::ContentType>*) const = 0;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700162 [[clang::warn_unused_result]] virtual hal::Error setContentType(hal::ContentType) = 0;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700163 [[clang::warn_unused_result]] virtual hal::Error getClientTargetProperty(
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700164 hal::ClientTargetProperty* outClientTargetProperty, float* outWhitePointNits) = 0;
Ana Krulec4593b692019-01-11 22:07:25 -0800165};
166
167namespace impl {
168
Lloyd Piquea516c002021-05-07 14:36:58 -0700169class Layer;
170
Ana Krulec4593b692019-01-11 22:07:25 -0800171class Display : public HWC2::Display {
172public:
Ady Abrahamde549d42022-01-26 19:19:17 -0800173 Display(android::Hwc2::Composer&,
174 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
175 hal::HWDisplayId, hal::DisplayType);
Ana Krulec4593b692019-01-11 22:07:25 -0800176 ~Display() override;
177
178 // Required by HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700179 hal::Error acceptChanges() override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700180 base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700181 hal::Error getChangedCompositionTypes(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500182 std::unordered_map<HWC2::Layer*,
183 aidl::android::hardware::graphics::composer3::Composition>* outTypes)
184 override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700185 hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
Ana Krulec4593b692019-01-11 22:07:25 -0800186 // Returns a bitmask which contains HdrMetadata::Type::*.
187 int32_t getSupportedPerFrameMetadata() const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700188 hal::Error getRenderIntents(hal::ColorMode colorMode,
189 std::vector<hal::RenderIntent>* outRenderIntents) const override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190 hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700191
Peiyong Line9d809e2020-04-14 13:10:48 -0700192 hal::Error getName(std::string* outName) const override;
193 hal::Error getRequests(
194 hal::DisplayRequest* outDisplayRequests,
Lloyd Piquea516c002021-05-07 14:36:58 -0700195 std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100196 hal::Error getConnectionType(ui::DisplayConnectionType*) const override;
Ady Abraham27fbcc72021-09-20 14:54:57 -0700197 hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
Peiyong Line9d809e2020-04-14 13:10:48 -0700198 hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
199 hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
200 hal::Dataspace* outDataspace,
201 uint8_t* outComponentMask) const override;
202 hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
203 uint64_t maxFrames) const override;
204 hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
205 android::DisplayedFrameStats* outStats) const override;
Lloyd Piquea516c002021-05-07 14:36:58 -0700206 hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
207 outFences) const override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700208 hal::Error present(android::sp<android::Fence>* outPresentFence) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700209 hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
210 const android::sp<android::Fence>& acquireFence,
211 hal::Dataspace dataspace) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200212 hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
Ady Abrahamdc011a92021-12-21 14:06:44 -0800213 hal::Error setColorTransform(const android::mat4& matrix) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200214 hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
Peiyong Line9d809e2020-04-14 13:10:48 -0700215 const android::sp<android::Fence>& releaseFence) override;
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200216 hal::Error setPowerMode(hal::PowerMode) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700217 hal::Error setVsyncEnabled(hal::Vsync enabled) override;
Ady Abraham43065bd2021-12-10 17:22:15 -0800218 hal::Error validate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
219 uint32_t* outNumRequests) override;
220 hal::Error presentOrValidate(nsecs_t expectedPresentTime, uint32_t* outNumTypes,
221 uint32_t* outNumRequests,
Peiyong Line9d809e2020-04-14 13:10:48 -0700222 android::sp<android::Fence>* outPresentFence,
223 uint32_t* state) override;
Alec Mouricdf16792021-12-10 13:16:06 -0800224 std::future<hal::Error> setDisplayBrightness(
225 float brightness, const Hwc2::Composer::DisplayBrightnessOptions& options) override;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100226 hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
227 const hal::VsyncPeriodChangeConstraints& constraints,
228 hal::VsyncPeriodChangeTimeline* outTimeline) override;
Kriti Dang7defaf32021-11-15 11:55:43 +0100229 hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
230 hal::Error clearBootDisplayConfig() override;
231 hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700232 hal::Error setAutoLowLatencyMode(bool on) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700233 hal::Error getSupportedContentTypes(
234 std::vector<hal::ContentType>* outSupportedContentTypes) const override;
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700235 hal::Error setContentType(hal::ContentType) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700236 hal::Error getClientTargetProperty(hal::ClientTargetProperty* outClientTargetProperty,
237 float* outWhitePointNits) override;
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700238
Dan Stoza651bf312015-10-23 17:03:17 -0700239 // Other Display methods
Peiyong Line9d809e2020-04-14 13:10:48 -0700240 hal::HWDisplayId getId() const override { return mId; }
Ana Krulec4593b692019-01-11 22:07:25 -0800241 bool isConnected() const override { return mIsConnected; }
242 void setConnected(bool connected) override; // For use by Device only
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500243 bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
244 const override EXCLUDES(mDisplayCapabilitiesMutex);
Lloyd Piquea516c002021-05-07 14:36:58 -0700245 bool isVsyncPeriodSwitchSupported() const override;
246 void onLayerDestroyed(hal::HWLayerId layerId) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700247
248private:
Dan Stoza651bf312015-10-23 17:03:17 -0700249
Dan Stoza651bf312015-10-23 17:03:17 -0700250 // This may fail (and return a null pointer) if no layer with this ID exists
251 // on this display
Lloyd Piquea516c002021-05-07 14:36:58 -0700252 std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700253
Lloyd Piquebc792092018-01-17 11:52:30 -0800254 friend android::TestableSurfaceFlinger;
255
Dan Stoza651bf312015-10-23 17:03:17 -0700256 // Member variables
257
Steven Thomas94e35b92017-07-26 18:48:28 -0700258 // These are references to data owned by HWC2::Device, which will outlive
259 // this HWC2::Display, so these references are guaranteed to be valid for
260 // the lifetime of this object.
261 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800262 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
263 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700264
Peiyong Line9d809e2020-04-14 13:10:48 -0700265 const hal::HWDisplayId mId;
266 hal::DisplayType mType;
Dominik Laskowski55c85402020-01-21 16:25:47 -0800267 bool mIsConnected = false;
Ady Abraham7159f572019-10-11 11:10:18 -0700268
Lloyd Piquea516c002021-05-07 14:36:58 -0700269 using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
270 Layers mLayers;
Ady Abraham7159f572019-10-11 11:10:18 -0700271
Ady Abraham27fbcc72021-09-20 14:54:57 -0700272 mutable std::mutex mDisplayCapabilitiesMutex;
Peiyong Lin1336e6e2019-05-28 09:23:50 -0700273 std::once_flag mDisplayCapabilityQueryFlag;
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500274 std::optional<
275 std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
276 mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
Dan Stoza651bf312015-10-23 17:03:17 -0700277};
Dominik Laskowski55c85402020-01-21 16:25:47 -0800278
Ana Krulec4593b692019-01-11 22:07:25 -0800279} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700280
Lloyd Pique35d58242018-12-18 16:33:25 -0800281class Layer {
282public:
283 virtual ~Layer();
284
Peiyong Line9d809e2020-04-14 13:10:48 -0700285 virtual hal::HWLayerId getId() const = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800286
Peiyong Line9d809e2020-04-14 13:10:48 -0700287 [[clang::warn_unused_result]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
288 [[clang::warn_unused_result]] virtual hal::Error setBuffer(
Lloyd Pique35d58242018-12-18 16:33:25 -0800289 uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
290 const android::sp<android::Fence>& acquireFence) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700291 [[clang::warn_unused_result]] virtual hal::Error setSurfaceDamage(
292 const android::Region& damage) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800293
Peiyong Line9d809e2020-04-14 13:10:48 -0700294 [[clang::warn_unused_result]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
Ady Abraham6e60b142022-01-06 18:10:35 -0800295 [[clang::warn_unused_result]] virtual hal::Error setColor(
296 aidl::android::hardware::graphics::composer3::Color color) = 0;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500297 [[clang::warn_unused_result]] virtual hal::Error setCompositionType(
298 aidl::android::hardware::graphics::composer3::Composition type) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700299 [[clang::warn_unused_result]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
300 [[clang::warn_unused_result]] virtual hal::Error setPerFrameMetadata(
Lloyd Pique35d58242018-12-18 16:33:25 -0800301 const int32_t supportedPerFrameMetadata, const android::HdrMetadata& metadata) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700302 [[clang::warn_unused_result]] virtual hal::Error setDisplayFrame(
303 const android::Rect& frame) = 0;
304 [[clang::warn_unused_result]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
305 [[clang::warn_unused_result]] virtual hal::Error setSidebandStream(
Lloyd Pique35d58242018-12-18 16:33:25 -0800306 const native_handle_t* stream) = 0;
Peiyong Line9d809e2020-04-14 13:10:48 -0700307 [[clang::warn_unused_result]] virtual hal::Error setSourceCrop(
308 const android::FloatRect& crop) = 0;
309 [[clang::warn_unused_result]] virtual hal::Error setTransform(hal::Transform transform) = 0;
310 [[clang::warn_unused_result]] virtual hal::Error setVisibleRegion(
311 const android::Region& region) = 0;
312 [[clang::warn_unused_result]] virtual hal::Error setZOrder(uint32_t z) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800313
314 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700315 [[clang::warn_unused_result]] virtual hal::Error setColorTransform(
316 const android::mat4& matrix) = 0;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800317
318 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700319 [[clang::warn_unused_result]] virtual hal::Error setLayerGenericMetadata(
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800320 const std::string& name, bool mandatory, const std::vector<uint8_t>& value) = 0;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700321
322 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800323 [[clang::warn_unused_result]] virtual hal::Error setBrightness(float brightness) = 0;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500324 [[clang::warn_unused_result]] virtual hal::Error setBlockingRegion(
325 const android::Region& region) = 0;
Lloyd Pique35d58242018-12-18 16:33:25 -0800326};
327
328namespace impl {
329
Peiyong Line9d809e2020-04-14 13:10:48 -0700330// Convenience C++ class to access per layer functions directly.
Lloyd Pique35d58242018-12-18 16:33:25 -0800331
332class Layer : public HWC2::Layer {
Dan Stoza651bf312015-10-23 17:03:17 -0700333public:
Peiyong Line9d809e2020-04-14 13:10:48 -0700334 Layer(android::Hwc2::Composer& composer,
Ady Abrahamde549d42022-01-26 19:19:17 -0800335 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
336 capabilities,
337 HWC2::Display& display, hal::HWLayerId layerId);
Lloyd Pique35d58242018-12-18 16:33:25 -0800338 ~Layer() override;
Dan Stoza651bf312015-10-23 17:03:17 -0700339
Lloyd Piquea516c002021-05-07 14:36:58 -0700340 void onOwningDisplayDestroyed();
341
Peiyong Line9d809e2020-04-14 13:10:48 -0700342 hal::HWLayerId getId() const override { return mId; }
Dan Stoza651bf312015-10-23 17:03:17 -0700343
Peiyong Line9d809e2020-04-14 13:10:48 -0700344 hal::Error setCursorPosition(int32_t x, int32_t y) override;
345 hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
346 const android::sp<android::Fence>& acquireFence) override;
347 hal::Error setSurfaceDamage(const android::Region& damage) override;
Steven Thomas94e35b92017-07-26 18:48:28 -0700348
Peiyong Line9d809e2020-04-14 13:10:48 -0700349 hal::Error setBlendMode(hal::BlendMode mode) override;
Ady Abraham6e60b142022-01-06 18:10:35 -0800350 hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500351 hal::Error setCompositionType(
352 aidl::android::hardware::graphics::composer3::Composition type) override;
Peiyong Line9d809e2020-04-14 13:10:48 -0700353 hal::Error setDataspace(hal::Dataspace dataspace) override;
354 hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
355 const android::HdrMetadata& metadata) override;
356 hal::Error setDisplayFrame(const android::Rect& frame) override;
357 hal::Error setPlaneAlpha(float alpha) override;
358 hal::Error setSidebandStream(const native_handle_t* stream) override;
359 hal::Error setSourceCrop(const android::FloatRect& crop) override;
360 hal::Error setTransform(hal::Transform transform) override;
361 hal::Error setVisibleRegion(const android::Region& region) override;
362 hal::Error setZOrder(uint32_t z) override;
Dan Stoza651bf312015-10-23 17:03:17 -0700363
Peiyong Lin698147a2018-09-14 13:27:18 -0700364 // Composer HAL 2.3
Peiyong Line9d809e2020-04-14 13:10:48 -0700365 hal::Error setColorTransform(const android::mat4& matrix) override;
Peiyong Lin698147a2018-09-14 13:27:18 -0700366
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800367 // Composer HAL 2.4
Peiyong Line9d809e2020-04-14 13:10:48 -0700368 hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
369 const std::vector<uint8_t>& value) override;
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800370
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700371 // AIDL HAL
Alec Mouri6da0e272022-02-07 12:45:57 -0800372 hal::Error setBrightness(float brightness) override;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500373 hal::Error setBlockingRegion(const android::Region& region) override;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700374
Dan Stoza651bf312015-10-23 17:03:17 -0700375private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700376 // These are references to data owned by HWC2::Device, which will outlive
377 // this HWC2::Layer, so these references are guaranteed to be valid for
378 // the lifetime of this object.
379 android::Hwc2::Composer& mComposer;
Ady Abrahamde549d42022-01-26 19:19:17 -0800380 const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
381 mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700382
Lloyd Piquea516c002021-05-07 14:36:58 -0700383 HWC2::Display* mDisplay;
Peiyong Line9d809e2020-04-14 13:10:48 -0700384 hal::HWLayerId mId;
Yichi Chen8366f562019-03-25 19:44:06 +0800385
386 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC
387 // multiple times.
388 android::Region mVisibleRegion = android::Region::INVALID_REGION;
389 android::Region mDamageRegion = android::Region::INVALID_REGION;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -0500390 android::Region mBlockingRegion = android::Region::INVALID_REGION;
Peiyong Line9d809e2020-04-14 13:10:48 -0700391 hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700392 android::HdrMetadata mHdrMetadata;
Peiyong Lin698147a2018-09-14 13:27:18 -0700393 android::mat4 mColorMatrix;
Yichi Chen8366f562019-03-25 19:44:06 +0800394 uint32_t mBufferSlot;
Dan Stoza651bf312015-10-23 17:03:17 -0700395};
396
Lloyd Pique35d58242018-12-18 16:33:25 -0800397} // namespace impl
Dan Stoza651bf312015-10-23 17:03:17 -0700398} // namespace HWC2
Peiyong Line9d809e2020-04-14 13:10:48 -0700399} // namespace android