xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/DisplayHardware/HWC2.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2015 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
18*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
19*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wconversion"
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker // #define LOG_NDEBUG 0
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
24*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "HWC2"
25*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include "HWC2.h"
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #include <android/configuration.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <common/FlagManager.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <ui/Fence.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <ui/FloatRect.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <ui/GraphicBuffer.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <ui/PictureProfileHandle.h>
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker #include "DisplayHardware/Hal.h"
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #include <algorithm>
39*38e8c45fSAndroid Build Coastguard Worker #include <cinttypes>
40*38e8c45fSAndroid Build Coastguard Worker #include <iterator>
41*38e8c45fSAndroid Build Coastguard Worker #include <set>
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::Color;
44*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::Composition;
45*38e8c45fSAndroid Build Coastguard Worker using AidlCapability = aidl::android::hardware::graphics::composer3::Capability;
46*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::DisplayCapability;
47*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::DisplayLuts;
48*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::LutProperties;
49*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::Luts;
50*38e8c45fSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::composer3::OverlayProperties;
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker namespace android {
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker using android::Fence;
55*38e8c45fSAndroid Build Coastguard Worker using android::FloatRect;
56*38e8c45fSAndroid Build Coastguard Worker using android::GraphicBuffer;
57*38e8c45fSAndroid Build Coastguard Worker using android::HdrCapabilities;
58*38e8c45fSAndroid Build Coastguard Worker using android::HdrMetadata;
59*38e8c45fSAndroid Build Coastguard Worker using android::PictureProfileHandle;
60*38e8c45fSAndroid Build Coastguard Worker using android::Rect;
61*38e8c45fSAndroid Build Coastguard Worker using android::Region;
62*38e8c45fSAndroid Build Coastguard Worker using android::sp;
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker namespace HWC2 {
65*38e8c45fSAndroid Build Coastguard Worker 
66*38e8c45fSAndroid Build Coastguard Worker using namespace android::hardware::graphics::composer::hal;
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker namespace Hwc2 = android::Hwc2;
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker namespace {
71*38e8c45fSAndroid Build Coastguard Worker 
hasMetadataKey(const std::set<Hwc2::PerFrameMetadataKey> & keys,const Hwc2::PerFrameMetadataKey & key)72*38e8c45fSAndroid Build Coastguard Worker inline bool hasMetadataKey(const std::set<Hwc2::PerFrameMetadataKey>& keys,
73*38e8c45fSAndroid Build Coastguard Worker                            const Hwc2::PerFrameMetadataKey& key) {
74*38e8c45fSAndroid Build Coastguard Worker     return keys.find(key) != keys.end();
75*38e8c45fSAndroid Build Coastguard Worker }
76*38e8c45fSAndroid Build Coastguard Worker 
77*38e8c45fSAndroid Build Coastguard Worker } // namespace anonymous
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker // Display methods
80*38e8c45fSAndroid Build Coastguard Worker Display::~Display() = default;
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker namespace impl {
83*38e8c45fSAndroid Build Coastguard Worker 
Display(android::Hwc2::Composer & composer,const std::unordered_set<AidlCapability> & capabilities,HWDisplayId id,DisplayType type)84*38e8c45fSAndroid Build Coastguard Worker Display::Display(android::Hwc2::Composer& composer,
85*38e8c45fSAndroid Build Coastguard Worker                  const std::unordered_set<AidlCapability>& capabilities, HWDisplayId id,
86*38e8c45fSAndroid Build Coastguard Worker                  DisplayType type)
87*38e8c45fSAndroid Build Coastguard Worker       : mComposer(composer), mCapabilities(capabilities), mId(id), mType(type) {
88*38e8c45fSAndroid Build Coastguard Worker     ALOGV("Created display %" PRIu64, id);
89*38e8c45fSAndroid Build Coastguard Worker     if (mType == hal::DisplayType::VIRTUAL) {
90*38e8c45fSAndroid Build Coastguard Worker         loadDisplayCapabilities();
91*38e8c45fSAndroid Build Coastguard Worker     }
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker 
~Display()94*38e8c45fSAndroid Build Coastguard Worker Display::~Display() {
95*38e8c45fSAndroid Build Coastguard Worker     // Note: The calls to onOwningDisplayDestroyed() are allowed (and expected)
96*38e8c45fSAndroid Build Coastguard Worker     // to call Display::onLayerDestroyed(). As that call removes entries from
97*38e8c45fSAndroid Build Coastguard Worker     // mLayers, we do not want to have a for loop directly over it here. Since
98*38e8c45fSAndroid Build Coastguard Worker     // the end goal is an empty mLayers anyway, we just go ahead and swap an
99*38e8c45fSAndroid Build Coastguard Worker     // initially empty local container with mLayers, and then enumerate
100*38e8c45fSAndroid Build Coastguard Worker     // the contents of the local container.
101*38e8c45fSAndroid Build Coastguard Worker     Layers destroyingLayers;
102*38e8c45fSAndroid Build Coastguard Worker     std::swap(mLayers, destroyingLayers);
103*38e8c45fSAndroid Build Coastguard Worker     for (const auto& [_, weakLayer] : destroyingLayers) {
104*38e8c45fSAndroid Build Coastguard Worker         if (std::shared_ptr layer = weakLayer.lock()) {
105*38e8c45fSAndroid Build Coastguard Worker             layer->onOwningDisplayDestroyed();
106*38e8c45fSAndroid Build Coastguard Worker         }
107*38e8c45fSAndroid Build Coastguard Worker     }
108*38e8c45fSAndroid Build Coastguard Worker 
109*38e8c45fSAndroid Build Coastguard Worker     Error error = Error::NONE;
110*38e8c45fSAndroid Build Coastguard Worker     const char* msg;
111*38e8c45fSAndroid Build Coastguard Worker     switch (mType) {
112*38e8c45fSAndroid Build Coastguard Worker         case DisplayType::PHYSICAL:
113*38e8c45fSAndroid Build Coastguard Worker             error = setVsyncEnabled(HWC2::Vsync::DISABLE);
114*38e8c45fSAndroid Build Coastguard Worker             msg = "disable VSYNC for";
115*38e8c45fSAndroid Build Coastguard Worker             break;
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker         case DisplayType::VIRTUAL:
118*38e8c45fSAndroid Build Coastguard Worker             error = static_cast<Error>(mComposer.destroyVirtualDisplay(mId));
119*38e8c45fSAndroid Build Coastguard Worker             msg = "destroy virtual";
120*38e8c45fSAndroid Build Coastguard Worker             break;
121*38e8c45fSAndroid Build Coastguard Worker 
122*38e8c45fSAndroid Build Coastguard Worker         case DisplayType::INVALID: // Used in unit tests.
123*38e8c45fSAndroid Build Coastguard Worker             break;
124*38e8c45fSAndroid Build Coastguard Worker     }
125*38e8c45fSAndroid Build Coastguard Worker 
126*38e8c45fSAndroid Build Coastguard Worker     ALOGE_IF(error != Error::NONE, "%s: Failed to %s display %" PRIu64 ": %d", __FUNCTION__, msg,
127*38e8c45fSAndroid Build Coastguard Worker              mId, static_cast<int32_t>(error));
128*38e8c45fSAndroid Build Coastguard Worker 
129*38e8c45fSAndroid Build Coastguard Worker     ALOGV("Destroyed display %" PRIu64, mId);
130*38e8c45fSAndroid Build Coastguard Worker }
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker // Required by HWC2 display
acceptChanges()133*38e8c45fSAndroid Build Coastguard Worker Error Display::acceptChanges()
134*38e8c45fSAndroid Build Coastguard Worker {
135*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.acceptDisplayChanges(mId);
136*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
137*38e8c45fSAndroid Build Coastguard Worker }
138*38e8c45fSAndroid Build Coastguard Worker 
createLayer()139*38e8c45fSAndroid Build Coastguard Worker base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> Display::createLayer() {
140*38e8c45fSAndroid Build Coastguard Worker     HWLayerId layerId = 0;
141*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.createLayer(mId, &layerId);
142*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
143*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
144*38e8c45fSAndroid Build Coastguard Worker         return base::unexpected(error);
145*38e8c45fSAndroid Build Coastguard Worker     }
146*38e8c45fSAndroid Build Coastguard Worker 
147*38e8c45fSAndroid Build Coastguard Worker     auto layer = std::make_shared<impl::Layer>(mComposer, mCapabilities, *this, layerId);
148*38e8c45fSAndroid Build Coastguard Worker     mLayers.emplace(layerId, layer);
149*38e8c45fSAndroid Build Coastguard Worker     return layer;
150*38e8c45fSAndroid Build Coastguard Worker }
151*38e8c45fSAndroid Build Coastguard Worker 
onLayerDestroyed(hal::HWLayerId layerId)152*38e8c45fSAndroid Build Coastguard Worker void Display::onLayerDestroyed(hal::HWLayerId layerId) {
153*38e8c45fSAndroid Build Coastguard Worker     mLayers.erase(layerId);
154*38e8c45fSAndroid Build Coastguard Worker }
155*38e8c45fSAndroid Build Coastguard Worker 
isVsyncPeriodSwitchSupported() const156*38e8c45fSAndroid Build Coastguard Worker bool Display::isVsyncPeriodSwitchSupported() const {
157*38e8c45fSAndroid Build Coastguard Worker     ALOGV("[%" PRIu64 "] isVsyncPeriodSwitchSupported()", mId);
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker     return mComposer.isSupported(android::Hwc2::Composer::OptionalFeature::RefreshRateSwitching);
160*38e8c45fSAndroid Build Coastguard Worker }
161*38e8c45fSAndroid Build Coastguard Worker 
hasDisplayIdleTimerCapability() const162*38e8c45fSAndroid Build Coastguard Worker bool Display::hasDisplayIdleTimerCapability() const {
163*38e8c45fSAndroid Build Coastguard Worker     bool isCapabilitySupported = false;
164*38e8c45fSAndroid Build Coastguard Worker     return mComposer.hasDisplayIdleTimerCapability(mId, &isCapabilitySupported) == Error::NONE &&
165*38e8c45fSAndroid Build Coastguard Worker             isCapabilitySupported;
166*38e8c45fSAndroid Build Coastguard Worker }
167*38e8c45fSAndroid Build Coastguard Worker 
getPhysicalDisplayOrientation(Hwc2::AidlTransform * outTransform) const168*38e8c45fSAndroid Build Coastguard Worker Error Display::getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const {
169*38e8c45fSAndroid Build Coastguard Worker     auto error = mComposer.getPhysicalDisplayOrientation(mId, outTransform);
170*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
171*38e8c45fSAndroid Build Coastguard Worker }
172*38e8c45fSAndroid Build Coastguard Worker 
getChangedCompositionTypes(std::unordered_map<HWC2::Layer *,Composition> * outTypes)173*38e8c45fSAndroid Build Coastguard Worker Error Display::getChangedCompositionTypes(std::unordered_map<HWC2::Layer*, Composition>* outTypes) {
174*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::Layer> layerIds;
175*38e8c45fSAndroid Build Coastguard Worker     std::vector<Composition> types;
176*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getChangedCompositionTypes(
177*38e8c45fSAndroid Build Coastguard Worker             mId, &layerIds, &types);
178*38e8c45fSAndroid Build Coastguard Worker     uint32_t numElements = layerIds.size();
179*38e8c45fSAndroid Build Coastguard Worker     const auto error = static_cast<Error>(intError);
180*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
181*38e8c45fSAndroid Build Coastguard Worker         return error;
182*38e8c45fSAndroid Build Coastguard Worker     }
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     outTypes->clear();
185*38e8c45fSAndroid Build Coastguard Worker     outTypes->reserve(numElements);
186*38e8c45fSAndroid Build Coastguard Worker     for (uint32_t element = 0; element < numElements; ++element) {
187*38e8c45fSAndroid Build Coastguard Worker         auto layer = getLayerById(layerIds[element]);
188*38e8c45fSAndroid Build Coastguard Worker         if (layer) {
189*38e8c45fSAndroid Build Coastguard Worker             auto type = types[element];
190*38e8c45fSAndroid Build Coastguard Worker             ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
191*38e8c45fSAndroid Build Coastguard Worker                     layer->getId(), to_string(type).c_str());
192*38e8c45fSAndroid Build Coastguard Worker             outTypes->emplace(layer.get(), type);
193*38e8c45fSAndroid Build Coastguard Worker         } else {
194*38e8c45fSAndroid Build Coastguard Worker             ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
195*38e8c45fSAndroid Build Coastguard Worker                     " on display %" PRIu64, layerIds[element], mId);
196*38e8c45fSAndroid Build Coastguard Worker         }
197*38e8c45fSAndroid Build Coastguard Worker     }
198*38e8c45fSAndroid Build Coastguard Worker 
199*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
200*38e8c45fSAndroid Build Coastguard Worker }
201*38e8c45fSAndroid Build Coastguard Worker 
getColorModes(std::vector<ColorMode> * outModes) const202*38e8c45fSAndroid Build Coastguard Worker Error Display::getColorModes(std::vector<ColorMode>* outModes) const
203*38e8c45fSAndroid Build Coastguard Worker {
204*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getColorModes(mId, outModes);
205*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
206*38e8c45fSAndroid Build Coastguard Worker }
207*38e8c45fSAndroid Build Coastguard Worker 
getSupportedPerFrameMetadata() const208*38e8c45fSAndroid Build Coastguard Worker int32_t Display::getSupportedPerFrameMetadata() const
209*38e8c45fSAndroid Build Coastguard Worker {
210*38e8c45fSAndroid Build Coastguard Worker     int32_t supportedPerFrameMetadata = 0;
211*38e8c45fSAndroid Build Coastguard Worker 
212*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::PerFrameMetadataKey> tmpKeys = mComposer.getPerFrameMetadataKeys(mId);
213*38e8c45fSAndroid Build Coastguard Worker     std::set<Hwc2::PerFrameMetadataKey> keys(tmpKeys.begin(), tmpKeys.end());
214*38e8c45fSAndroid Build Coastguard Worker 
215*38e8c45fSAndroid Build Coastguard Worker     // Check whether a specific metadata type is supported. A metadata type is considered
216*38e8c45fSAndroid Build Coastguard Worker     // supported if and only if all required fields are supported.
217*38e8c45fSAndroid Build Coastguard Worker 
218*38e8c45fSAndroid Build Coastguard Worker     // SMPTE2086
219*38e8c45fSAndroid Build Coastguard Worker     if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X) &&
220*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y) &&
221*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X) &&
222*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y) &&
223*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X) &&
224*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y) &&
225*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_X) &&
226*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_Y) &&
227*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_LUMINANCE) &&
228*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MIN_LUMINANCE)) {
229*38e8c45fSAndroid Build Coastguard Worker         supportedPerFrameMetadata |= HdrMetadata::Type::SMPTE2086;
230*38e8c45fSAndroid Build Coastguard Worker     }
231*38e8c45fSAndroid Build Coastguard Worker     // CTA861_3
232*38e8c45fSAndroid Build Coastguard Worker     if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL) &&
233*38e8c45fSAndroid Build Coastguard Worker         hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL)) {
234*38e8c45fSAndroid Build Coastguard Worker         supportedPerFrameMetadata |= HdrMetadata::Type::CTA861_3;
235*38e8c45fSAndroid Build Coastguard Worker     }
236*38e8c45fSAndroid Build Coastguard Worker 
237*38e8c45fSAndroid Build Coastguard Worker     // HDR10PLUS
238*38e8c45fSAndroid Build Coastguard Worker     if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::HDR10_PLUS_SEI)) {
239*38e8c45fSAndroid Build Coastguard Worker         supportedPerFrameMetadata |= HdrMetadata::Type::HDR10PLUS;
240*38e8c45fSAndroid Build Coastguard Worker     }
241*38e8c45fSAndroid Build Coastguard Worker 
242*38e8c45fSAndroid Build Coastguard Worker     return supportedPerFrameMetadata;
243*38e8c45fSAndroid Build Coastguard Worker }
244*38e8c45fSAndroid Build Coastguard Worker 
getRenderIntents(ColorMode colorMode,std::vector<RenderIntent> * outRenderIntents) const245*38e8c45fSAndroid Build Coastguard Worker Error Display::getRenderIntents(ColorMode colorMode,
246*38e8c45fSAndroid Build Coastguard Worker         std::vector<RenderIntent>* outRenderIntents) const
247*38e8c45fSAndroid Build Coastguard Worker {
248*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getRenderIntents(mId, colorMode, outRenderIntents);
249*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
250*38e8c45fSAndroid Build Coastguard Worker }
251*38e8c45fSAndroid Build Coastguard Worker 
getDataspaceSaturationMatrix(Dataspace dataspace,android::mat4 * outMatrix)252*38e8c45fSAndroid Build Coastguard Worker Error Display::getDataspaceSaturationMatrix(Dataspace dataspace, android::mat4* outMatrix)
253*38e8c45fSAndroid Build Coastguard Worker {
254*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getDataspaceSaturationMatrix(dataspace, outMatrix);
255*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
256*38e8c45fSAndroid Build Coastguard Worker }
257*38e8c45fSAndroid Build Coastguard Worker 
getName(std::string * outName) const258*38e8c45fSAndroid Build Coastguard Worker Error Display::getName(std::string* outName) const
259*38e8c45fSAndroid Build Coastguard Worker {
260*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getDisplayName(mId, outName);
261*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
262*38e8c45fSAndroid Build Coastguard Worker }
263*38e8c45fSAndroid Build Coastguard Worker 
getRequests(HWC2::DisplayRequest * outDisplayRequests,std::unordered_map<HWC2::Layer *,LayerRequest> * outLayerRequests)264*38e8c45fSAndroid Build Coastguard Worker Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
265*38e8c45fSAndroid Build Coastguard Worker                            std::unordered_map<HWC2::Layer*, LayerRequest>* outLayerRequests) {
266*38e8c45fSAndroid Build Coastguard Worker     uint32_t intDisplayRequests = 0;
267*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::Layer> layerIds;
268*38e8c45fSAndroid Build Coastguard Worker     std::vector<uint32_t> layerRequests;
269*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getDisplayRequests(
270*38e8c45fSAndroid Build Coastguard Worker             mId, &intDisplayRequests, &layerIds, &layerRequests);
271*38e8c45fSAndroid Build Coastguard Worker     uint32_t numElements = layerIds.size();
272*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
273*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
274*38e8c45fSAndroid Build Coastguard Worker         return error;
275*38e8c45fSAndroid Build Coastguard Worker     }
276*38e8c45fSAndroid Build Coastguard Worker 
277*38e8c45fSAndroid Build Coastguard Worker     *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
278*38e8c45fSAndroid Build Coastguard Worker     outLayerRequests->clear();
279*38e8c45fSAndroid Build Coastguard Worker     outLayerRequests->reserve(numElements);
280*38e8c45fSAndroid Build Coastguard Worker     for (uint32_t element = 0; element < numElements; ++element) {
281*38e8c45fSAndroid Build Coastguard Worker         auto layer = getLayerById(layerIds[element]);
282*38e8c45fSAndroid Build Coastguard Worker         if (layer) {
283*38e8c45fSAndroid Build Coastguard Worker             auto layerRequest =
284*38e8c45fSAndroid Build Coastguard Worker                     static_cast<LayerRequest>(layerRequests[element]);
285*38e8c45fSAndroid Build Coastguard Worker             outLayerRequests->emplace(layer.get(), layerRequest);
286*38e8c45fSAndroid Build Coastguard Worker         } else {
287*38e8c45fSAndroid Build Coastguard Worker             ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
288*38e8c45fSAndroid Build Coastguard Worker                     PRIu64, layerIds[element], mId);
289*38e8c45fSAndroid Build Coastguard Worker         }
290*38e8c45fSAndroid Build Coastguard Worker     }
291*38e8c45fSAndroid Build Coastguard Worker 
292*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
293*38e8c45fSAndroid Build Coastguard Worker }
294*38e8c45fSAndroid Build Coastguard Worker 
getConnectionType() const295*38e8c45fSAndroid Build Coastguard Worker ftl::Expected<ui::DisplayConnectionType, hal::Error> Display::getConnectionType() const {
296*38e8c45fSAndroid Build Coastguard Worker     if (!mConnectionType) {
297*38e8c45fSAndroid Build Coastguard Worker         mConnectionType = [this]() -> decltype(mConnectionType) {
298*38e8c45fSAndroid Build Coastguard Worker             if (mType != DisplayType::PHYSICAL) {
299*38e8c45fSAndroid Build Coastguard Worker                 return ftl::Unexpected(Error::BAD_DISPLAY);
300*38e8c45fSAndroid Build Coastguard Worker             }
301*38e8c45fSAndroid Build Coastguard Worker 
302*38e8c45fSAndroid Build Coastguard Worker             using ConnectionType = Hwc2::IComposerClient::DisplayConnectionType;
303*38e8c45fSAndroid Build Coastguard Worker             ConnectionType connectionType;
304*38e8c45fSAndroid Build Coastguard Worker 
305*38e8c45fSAndroid Build Coastguard Worker             if (const auto error = static_cast<Error>(
306*38e8c45fSAndroid Build Coastguard Worker                         mComposer.getDisplayConnectionType(mId, &connectionType));
307*38e8c45fSAndroid Build Coastguard Worker                 error != Error::NONE) {
308*38e8c45fSAndroid Build Coastguard Worker                 return ftl::Unexpected(error);
309*38e8c45fSAndroid Build Coastguard Worker             }
310*38e8c45fSAndroid Build Coastguard Worker 
311*38e8c45fSAndroid Build Coastguard Worker             return connectionType == ConnectionType::INTERNAL ? ui::DisplayConnectionType::Internal
312*38e8c45fSAndroid Build Coastguard Worker                                                               : ui::DisplayConnectionType::External;
313*38e8c45fSAndroid Build Coastguard Worker         }();
314*38e8c45fSAndroid Build Coastguard Worker     }
315*38e8c45fSAndroid Build Coastguard Worker 
316*38e8c45fSAndroid Build Coastguard Worker     return *mConnectionType;
317*38e8c45fSAndroid Build Coastguard Worker }
318*38e8c45fSAndroid Build Coastguard Worker 
hasCapability(DisplayCapability capability) const319*38e8c45fSAndroid Build Coastguard Worker bool Display::hasCapability(DisplayCapability capability) const {
320*38e8c45fSAndroid Build Coastguard Worker     std::scoped_lock lock(mDisplayCapabilitiesMutex);
321*38e8c45fSAndroid Build Coastguard Worker     if (mDisplayCapabilities) {
322*38e8c45fSAndroid Build Coastguard Worker         return mDisplayCapabilities->count(capability) > 0;
323*38e8c45fSAndroid Build Coastguard Worker     }
324*38e8c45fSAndroid Build Coastguard Worker 
325*38e8c45fSAndroid Build Coastguard Worker     ALOGW("Can't query capability %s."
326*38e8c45fSAndroid Build Coastguard Worker           " Display Capabilities were not queried from HWC yet",
327*38e8c45fSAndroid Build Coastguard Worker           to_string(capability).c_str());
328*38e8c45fSAndroid Build Coastguard Worker 
329*38e8c45fSAndroid Build Coastguard Worker     return false;
330*38e8c45fSAndroid Build Coastguard Worker }
331*38e8c45fSAndroid Build Coastguard Worker 
supportsDoze(bool * outSupport) const332*38e8c45fSAndroid Build Coastguard Worker Error Display::supportsDoze(bool* outSupport) const {
333*38e8c45fSAndroid Build Coastguard Worker     {
334*38e8c45fSAndroid Build Coastguard Worker         std::scoped_lock lock(mDisplayCapabilitiesMutex);
335*38e8c45fSAndroid Build Coastguard Worker         if (!mDisplayCapabilities) {
336*38e8c45fSAndroid Build Coastguard Worker             // The display has not turned on since boot, so DOZE support is unknown.
337*38e8c45fSAndroid Build Coastguard Worker             ALOGW("%s: haven't queried capabilities yet!", __func__);
338*38e8c45fSAndroid Build Coastguard Worker             return Error::NO_RESOURCES;
339*38e8c45fSAndroid Build Coastguard Worker         }
340*38e8c45fSAndroid Build Coastguard Worker     }
341*38e8c45fSAndroid Build Coastguard Worker     *outSupport = hasCapability(DisplayCapability::DOZE);
342*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
343*38e8c45fSAndroid Build Coastguard Worker }
344*38e8c45fSAndroid Build Coastguard Worker 
getHdrCapabilities(HdrCapabilities * outCapabilities) const345*38e8c45fSAndroid Build Coastguard Worker Error Display::getHdrCapabilities(HdrCapabilities* outCapabilities) const
346*38e8c45fSAndroid Build Coastguard Worker {
347*38e8c45fSAndroid Build Coastguard Worker     float maxLuminance = -1.0f;
348*38e8c45fSAndroid Build Coastguard Worker     float maxAverageLuminance = -1.0f;
349*38e8c45fSAndroid Build Coastguard Worker     float minLuminance = -1.0f;
350*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::Hdr> hdrTypes;
351*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getHdrCapabilities(mId, &hdrTypes, &maxLuminance,
352*38e8c45fSAndroid Build Coastguard Worker                                                  &maxAverageLuminance, &minLuminance);
353*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<HWC2::Error>(intError);
354*38e8c45fSAndroid Build Coastguard Worker 
355*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
356*38e8c45fSAndroid Build Coastguard Worker         return error;
357*38e8c45fSAndroid Build Coastguard Worker     }
358*38e8c45fSAndroid Build Coastguard Worker 
359*38e8c45fSAndroid Build Coastguard Worker     *outCapabilities =
360*38e8c45fSAndroid Build Coastguard Worker             HdrCapabilities(std::move(hdrTypes), maxLuminance, maxAverageLuminance, minLuminance);
361*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
362*38e8c45fSAndroid Build Coastguard Worker }
363*38e8c45fSAndroid Build Coastguard Worker 
getOverlaySupport(OverlayProperties * outProperties) const364*38e8c45fSAndroid Build Coastguard Worker Error Display::getOverlaySupport(OverlayProperties* outProperties) const {
365*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getOverlaySupport(outProperties);
366*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
367*38e8c45fSAndroid Build Coastguard Worker }
368*38e8c45fSAndroid Build Coastguard Worker 
getDisplayedContentSamplingAttributes(hal::PixelFormat * outFormat,Dataspace * outDataspace,uint8_t * outComponentMask) const369*38e8c45fSAndroid Build Coastguard Worker Error Display::getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
370*38e8c45fSAndroid Build Coastguard Worker                                                      Dataspace* outDataspace,
371*38e8c45fSAndroid Build Coastguard Worker                                                      uint8_t* outComponentMask) const {
372*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getDisplayedContentSamplingAttributes(mId, outFormat, outDataspace,
373*38e8c45fSAndroid Build Coastguard Worker                                                                     outComponentMask);
374*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
375*38e8c45fSAndroid Build Coastguard Worker }
376*38e8c45fSAndroid Build Coastguard Worker 
setDisplayContentSamplingEnabled(bool enabled,uint8_t componentMask,uint64_t maxFrames) const377*38e8c45fSAndroid Build Coastguard Worker Error Display::setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
378*38e8c45fSAndroid Build Coastguard Worker                                                 uint64_t maxFrames) const {
379*38e8c45fSAndroid Build Coastguard Worker     auto intError =
380*38e8c45fSAndroid Build Coastguard Worker             mComposer.setDisplayContentSamplingEnabled(mId, enabled, componentMask, maxFrames);
381*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
382*38e8c45fSAndroid Build Coastguard Worker }
383*38e8c45fSAndroid Build Coastguard Worker 
getDisplayedContentSample(uint64_t maxFrames,uint64_t timestamp,android::DisplayedFrameStats * outStats) const384*38e8c45fSAndroid Build Coastguard Worker Error Display::getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
385*38e8c45fSAndroid Build Coastguard Worker                                          android::DisplayedFrameStats* outStats) const {
386*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getDisplayedContentSample(mId, maxFrames, timestamp, outStats);
387*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
388*38e8c45fSAndroid Build Coastguard Worker }
389*38e8c45fSAndroid Build Coastguard Worker 
getReleaseFences(std::unordered_map<HWC2::Layer *,sp<Fence>> * outFences) const390*38e8c45fSAndroid Build Coastguard Worker Error Display::getReleaseFences(std::unordered_map<HWC2::Layer*, sp<Fence>>* outFences) const {
391*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::Layer> layerIds;
392*38e8c45fSAndroid Build Coastguard Worker     std::vector<int> fenceFds;
393*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
394*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
395*38e8c45fSAndroid Build Coastguard Worker     uint32_t numElements = layerIds.size();
396*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
397*38e8c45fSAndroid Build Coastguard Worker         return error;
398*38e8c45fSAndroid Build Coastguard Worker     }
399*38e8c45fSAndroid Build Coastguard Worker 
400*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
401*38e8c45fSAndroid Build Coastguard Worker     releaseFences.reserve(numElements);
402*38e8c45fSAndroid Build Coastguard Worker     for (uint32_t element = 0; element < numElements; ++element) {
403*38e8c45fSAndroid Build Coastguard Worker         auto layer = getLayerById(layerIds[element]);
404*38e8c45fSAndroid Build Coastguard Worker         if (layer) {
405*38e8c45fSAndroid Build Coastguard Worker             sp<Fence> fence(sp<Fence>::make(fenceFds[element]));
406*38e8c45fSAndroid Build Coastguard Worker             releaseFences.emplace(layer.get(), fence);
407*38e8c45fSAndroid Build Coastguard Worker         } else {
408*38e8c45fSAndroid Build Coastguard Worker             ALOGE("getReleaseFences: invalid layer %" PRIu64
409*38e8c45fSAndroid Build Coastguard Worker                     " found on display %" PRIu64, layerIds[element], mId);
410*38e8c45fSAndroid Build Coastguard Worker             for (; element < numElements; ++element) {
411*38e8c45fSAndroid Build Coastguard Worker                 close(fenceFds[element]);
412*38e8c45fSAndroid Build Coastguard Worker             }
413*38e8c45fSAndroid Build Coastguard Worker             return Error::BAD_LAYER;
414*38e8c45fSAndroid Build Coastguard Worker         }
415*38e8c45fSAndroid Build Coastguard Worker     }
416*38e8c45fSAndroid Build Coastguard Worker 
417*38e8c45fSAndroid Build Coastguard Worker     *outFences = std::move(releaseFences);
418*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
419*38e8c45fSAndroid Build Coastguard Worker }
420*38e8c45fSAndroid Build Coastguard Worker 
present(sp<Fence> * outPresentFence)421*38e8c45fSAndroid Build Coastguard Worker Error Display::present(sp<Fence>* outPresentFence)
422*38e8c45fSAndroid Build Coastguard Worker {
423*38e8c45fSAndroid Build Coastguard Worker     int32_t presentFenceFd = -1;
424*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
425*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
426*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
427*38e8c45fSAndroid Build Coastguard Worker         return error;
428*38e8c45fSAndroid Build Coastguard Worker     }
429*38e8c45fSAndroid Build Coastguard Worker 
430*38e8c45fSAndroid Build Coastguard Worker     *outPresentFence = sp<Fence>::make(presentFenceFd);
431*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
432*38e8c45fSAndroid Build Coastguard Worker }
433*38e8c45fSAndroid Build Coastguard Worker 
setActiveConfigWithConstraints(hal::HWConfigId configId,const VsyncPeriodChangeConstraints & constraints,VsyncPeriodChangeTimeline * outTimeline)434*38e8c45fSAndroid Build Coastguard Worker Error Display::setActiveConfigWithConstraints(hal::HWConfigId configId,
435*38e8c45fSAndroid Build Coastguard Worker                                               const VsyncPeriodChangeConstraints& constraints,
436*38e8c45fSAndroid Build Coastguard Worker                                               VsyncPeriodChangeTimeline* outTimeline) {
437*38e8c45fSAndroid Build Coastguard Worker     ALOGV("[%" PRIu64 "] setActiveConfigWithConstraints", mId);
438*38e8c45fSAndroid Build Coastguard Worker 
439*38e8c45fSAndroid Build Coastguard Worker     // FIXME (b/319505580): At least the first config set on an external display must be
440*38e8c45fSAndroid Build Coastguard Worker     // `setActiveConfig`, so skip over the block that calls `setActiveConfigWithConstraints`
441*38e8c45fSAndroid Build Coastguard Worker     // for simplicity.
442*38e8c45fSAndroid Build Coastguard Worker     const bool connected_display = FlagManager::getInstance().connected_display();
443*38e8c45fSAndroid Build Coastguard Worker 
444*38e8c45fSAndroid Build Coastguard Worker     if (isVsyncPeriodSwitchSupported() &&
445*38e8c45fSAndroid Build Coastguard Worker         (!connected_display ||
446*38e8c45fSAndroid Build Coastguard Worker          getConnectionType().value_opt() != ui::DisplayConnectionType::External)) {
447*38e8c45fSAndroid Build Coastguard Worker         Hwc2::IComposerClient::VsyncPeriodChangeConstraints hwc2Constraints;
448*38e8c45fSAndroid Build Coastguard Worker         hwc2Constraints.desiredTimeNanos = constraints.desiredTimeNanos;
449*38e8c45fSAndroid Build Coastguard Worker         hwc2Constraints.seamlessRequired = constraints.seamlessRequired;
450*38e8c45fSAndroid Build Coastguard Worker 
451*38e8c45fSAndroid Build Coastguard Worker         Hwc2::VsyncPeriodChangeTimeline vsyncPeriodChangeTimeline = {};
452*38e8c45fSAndroid Build Coastguard Worker         auto intError = mComposer.setActiveConfigWithConstraints(mId, configId, hwc2Constraints,
453*38e8c45fSAndroid Build Coastguard Worker                                                                  &vsyncPeriodChangeTimeline);
454*38e8c45fSAndroid Build Coastguard Worker         outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeTimeline.newVsyncAppliedTimeNanos;
455*38e8c45fSAndroid Build Coastguard Worker         outTimeline->refreshRequired = vsyncPeriodChangeTimeline.refreshRequired;
456*38e8c45fSAndroid Build Coastguard Worker         outTimeline->refreshTimeNanos = vsyncPeriodChangeTimeline.refreshTimeNanos;
457*38e8c45fSAndroid Build Coastguard Worker         return static_cast<Error>(intError);
458*38e8c45fSAndroid Build Coastguard Worker     }
459*38e8c45fSAndroid Build Coastguard Worker 
460*38e8c45fSAndroid Build Coastguard Worker     // Use legacy setActiveConfig instead
461*38e8c45fSAndroid Build Coastguard Worker     ALOGV("fallback to legacy setActiveConfig");
462*38e8c45fSAndroid Build Coastguard Worker     const auto now = systemTime();
463*38e8c45fSAndroid Build Coastguard Worker     if (constraints.desiredTimeNanos > now || constraints.seamlessRequired) {
464*38e8c45fSAndroid Build Coastguard Worker         ALOGE("setActiveConfigWithConstraints received constraints that can't be satisfied");
465*38e8c45fSAndroid Build Coastguard Worker     }
466*38e8c45fSAndroid Build Coastguard Worker 
467*38e8c45fSAndroid Build Coastguard Worker     auto intError_2_4 = mComposer.setActiveConfig(mId, configId);
468*38e8c45fSAndroid Build Coastguard Worker     outTimeline->newVsyncAppliedTimeNanos = std::max(now, constraints.desiredTimeNanos);
469*38e8c45fSAndroid Build Coastguard Worker     outTimeline->refreshRequired = true;
470*38e8c45fSAndroid Build Coastguard Worker     outTimeline->refreshTimeNanos = now;
471*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError_2_4);
472*38e8c45fSAndroid Build Coastguard Worker }
473*38e8c45fSAndroid Build Coastguard Worker 
setClientTarget(uint32_t slot,const sp<GraphicBuffer> & target,const sp<Fence> & acquireFence,Dataspace dataspace,float hdrSdrRatio)474*38e8c45fSAndroid Build Coastguard Worker Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
475*38e8c45fSAndroid Build Coastguard Worker                                const sp<Fence>& acquireFence, Dataspace dataspace,
476*38e8c45fSAndroid Build Coastguard Worker                                float hdrSdrRatio) {
477*38e8c45fSAndroid Build Coastguard Worker     // TODO: Properly encode client target surface damage
478*38e8c45fSAndroid Build Coastguard Worker     int32_t fenceFd = acquireFence->dup();
479*38e8c45fSAndroid Build Coastguard Worker     auto intError =
480*38e8c45fSAndroid Build Coastguard Worker             mComposer.setClientTarget(mId, slot, target, fenceFd, dataspace,
481*38e8c45fSAndroid Build Coastguard Worker                                       std::vector<Hwc2::IComposerClient::Rect>(), hdrSdrRatio);
482*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
483*38e8c45fSAndroid Build Coastguard Worker }
484*38e8c45fSAndroid Build Coastguard Worker 
setColorMode(ColorMode mode,RenderIntent renderIntent)485*38e8c45fSAndroid Build Coastguard Worker Error Display::setColorMode(ColorMode mode, RenderIntent renderIntent)
486*38e8c45fSAndroid Build Coastguard Worker {
487*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setColorMode(mId, mode, renderIntent);
488*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
489*38e8c45fSAndroid Build Coastguard Worker }
490*38e8c45fSAndroid Build Coastguard Worker 
setColorTransform(const android::mat4 & matrix)491*38e8c45fSAndroid Build Coastguard Worker Error Display::setColorTransform(const android::mat4& matrix) {
492*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setColorTransform(mId, matrix.asArray());
493*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
494*38e8c45fSAndroid Build Coastguard Worker }
495*38e8c45fSAndroid Build Coastguard Worker 
setOutputBuffer(const sp<GraphicBuffer> & buffer,const sp<Fence> & releaseFence)496*38e8c45fSAndroid Build Coastguard Worker Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
497*38e8c45fSAndroid Build Coastguard Worker         const sp<Fence>& releaseFence)
498*38e8c45fSAndroid Build Coastguard Worker {
499*38e8c45fSAndroid Build Coastguard Worker     int32_t fenceFd = releaseFence->dup();
500*38e8c45fSAndroid Build Coastguard Worker     auto handle = buffer->getNativeBuffer()->handle;
501*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
502*38e8c45fSAndroid Build Coastguard Worker     close(fenceFd);
503*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
504*38e8c45fSAndroid Build Coastguard Worker }
505*38e8c45fSAndroid Build Coastguard Worker 
setPowerMode(PowerMode mode)506*38e8c45fSAndroid Build Coastguard Worker Error Display::setPowerMode(PowerMode mode)
507*38e8c45fSAndroid Build Coastguard Worker {
508*38e8c45fSAndroid Build Coastguard Worker     auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
509*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setPowerMode(mId, intMode);
510*38e8c45fSAndroid Build Coastguard Worker 
511*38e8c45fSAndroid Build Coastguard Worker     if (mode == PowerMode::ON) {
512*38e8c45fSAndroid Build Coastguard Worker         loadDisplayCapabilities();
513*38e8c45fSAndroid Build Coastguard Worker     }
514*38e8c45fSAndroid Build Coastguard Worker 
515*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
516*38e8c45fSAndroid Build Coastguard Worker }
517*38e8c45fSAndroid Build Coastguard Worker 
setVsyncEnabled(Vsync enabled)518*38e8c45fSAndroid Build Coastguard Worker Error Display::setVsyncEnabled(Vsync enabled)
519*38e8c45fSAndroid Build Coastguard Worker {
520*38e8c45fSAndroid Build Coastguard Worker     auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
521*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
522*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
523*38e8c45fSAndroid Build Coastguard Worker }
524*38e8c45fSAndroid Build Coastguard Worker 
validate(nsecs_t expectedPresentTime,int32_t frameIntervalNs,uint32_t * outNumTypes,uint32_t * outNumRequests)525*38e8c45fSAndroid Build Coastguard Worker Error Display::validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
526*38e8c45fSAndroid Build Coastguard Worker                         uint32_t* outNumRequests) {
527*38e8c45fSAndroid Build Coastguard Worker     uint32_t numTypes = 0;
528*38e8c45fSAndroid Build Coastguard Worker     uint32_t numRequests = 0;
529*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.validateDisplay(mId, expectedPresentTime, frameIntervalNs, &numTypes,
530*38e8c45fSAndroid Build Coastguard Worker                                               &numRequests);
531*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
532*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE && !hasChangesError(error)) {
533*38e8c45fSAndroid Build Coastguard Worker         return error;
534*38e8c45fSAndroid Build Coastguard Worker     }
535*38e8c45fSAndroid Build Coastguard Worker 
536*38e8c45fSAndroid Build Coastguard Worker     *outNumTypes = numTypes;
537*38e8c45fSAndroid Build Coastguard Worker     *outNumRequests = numRequests;
538*38e8c45fSAndroid Build Coastguard Worker     return error;
539*38e8c45fSAndroid Build Coastguard Worker }
540*38e8c45fSAndroid Build Coastguard Worker 
presentOrValidate(nsecs_t expectedPresentTime,int32_t frameIntervalNs,uint32_t * outNumTypes,uint32_t * outNumRequests,sp<android::Fence> * outPresentFence,uint32_t * state)541*38e8c45fSAndroid Build Coastguard Worker Error Display::presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
542*38e8c45fSAndroid Build Coastguard Worker                                  uint32_t* outNumTypes, uint32_t* outNumRequests,
543*38e8c45fSAndroid Build Coastguard Worker                                  sp<android::Fence>* outPresentFence, uint32_t* state) {
544*38e8c45fSAndroid Build Coastguard Worker     uint32_t numTypes = 0;
545*38e8c45fSAndroid Build Coastguard Worker     uint32_t numRequests = 0;
546*38e8c45fSAndroid Build Coastguard Worker     int32_t presentFenceFd = -1;
547*38e8c45fSAndroid Build Coastguard Worker     auto intError =
548*38e8c45fSAndroid Build Coastguard Worker             mComposer.presentOrValidateDisplay(mId, expectedPresentTime, frameIntervalNs, &numTypes,
549*38e8c45fSAndroid Build Coastguard Worker                                                &numRequests, &presentFenceFd, state);
550*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
551*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE && !hasChangesError(error)) {
552*38e8c45fSAndroid Build Coastguard Worker         return error;
553*38e8c45fSAndroid Build Coastguard Worker     }
554*38e8c45fSAndroid Build Coastguard Worker 
555*38e8c45fSAndroid Build Coastguard Worker     if (*state == 1) {
556*38e8c45fSAndroid Build Coastguard Worker         *outPresentFence = sp<Fence>::make(presentFenceFd);
557*38e8c45fSAndroid Build Coastguard Worker     }
558*38e8c45fSAndroid Build Coastguard Worker 
559*38e8c45fSAndroid Build Coastguard Worker     if (*state == 0) {
560*38e8c45fSAndroid Build Coastguard Worker         *outNumTypes = numTypes;
561*38e8c45fSAndroid Build Coastguard Worker         *outNumRequests = numRequests;
562*38e8c45fSAndroid Build Coastguard Worker     }
563*38e8c45fSAndroid Build Coastguard Worker     return error;
564*38e8c45fSAndroid Build Coastguard Worker }
565*38e8c45fSAndroid Build Coastguard Worker 
setDisplayBrightness(float brightness,float brightnessNits,const Hwc2::Composer::DisplayBrightnessOptions & options)566*38e8c45fSAndroid Build Coastguard Worker ftl::Future<Error> Display::setDisplayBrightness(
567*38e8c45fSAndroid Build Coastguard Worker         float brightness, float brightnessNits,
568*38e8c45fSAndroid Build Coastguard Worker         const Hwc2::Composer::DisplayBrightnessOptions& options) {
569*38e8c45fSAndroid Build Coastguard Worker     return ftl::defer([composer = &mComposer, id = mId, brightness, brightnessNits, options] {
570*38e8c45fSAndroid Build Coastguard Worker         const auto intError =
571*38e8c45fSAndroid Build Coastguard Worker                 composer->setDisplayBrightness(id, brightness, brightnessNits, options);
572*38e8c45fSAndroid Build Coastguard Worker         return static_cast<Error>(intError);
573*38e8c45fSAndroid Build Coastguard Worker     });
574*38e8c45fSAndroid Build Coastguard Worker }
575*38e8c45fSAndroid Build Coastguard Worker 
setBootDisplayConfig(hal::HWConfigId configId)576*38e8c45fSAndroid Build Coastguard Worker Error Display::setBootDisplayConfig(hal::HWConfigId configId) {
577*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setBootDisplayConfig(mId, configId);
578*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
579*38e8c45fSAndroid Build Coastguard Worker }
580*38e8c45fSAndroid Build Coastguard Worker 
clearBootDisplayConfig()581*38e8c45fSAndroid Build Coastguard Worker Error Display::clearBootDisplayConfig() {
582*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.clearBootDisplayConfig(mId);
583*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
584*38e8c45fSAndroid Build Coastguard Worker }
585*38e8c45fSAndroid Build Coastguard Worker 
getPreferredBootDisplayConfig(hal::HWConfigId * configId) const586*38e8c45fSAndroid Build Coastguard Worker Error Display::getPreferredBootDisplayConfig(hal::HWConfigId* configId) const {
587*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getPreferredBootDisplayConfig(mId, configId);
588*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
589*38e8c45fSAndroid Build Coastguard Worker }
590*38e8c45fSAndroid Build Coastguard Worker 
setAutoLowLatencyMode(bool on)591*38e8c45fSAndroid Build Coastguard Worker Error Display::setAutoLowLatencyMode(bool on) {
592*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setAutoLowLatencyMode(mId, on);
593*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
594*38e8c45fSAndroid Build Coastguard Worker }
595*38e8c45fSAndroid Build Coastguard Worker 
getSupportedContentTypes(std::vector<ContentType> * outSupportedContentTypes) const596*38e8c45fSAndroid Build Coastguard Worker Error Display::getSupportedContentTypes(std::vector<ContentType>* outSupportedContentTypes) const {
597*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::IComposerClient::ContentType> tmpSupportedContentTypes;
598*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.getSupportedContentTypes(mId, &tmpSupportedContentTypes);
599*38e8c45fSAndroid Build Coastguard Worker     for (Hwc2::IComposerClient::ContentType contentType : tmpSupportedContentTypes) {
600*38e8c45fSAndroid Build Coastguard Worker         outSupportedContentTypes->push_back(static_cast<ContentType>(contentType));
601*38e8c45fSAndroid Build Coastguard Worker     }
602*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
603*38e8c45fSAndroid Build Coastguard Worker }
604*38e8c45fSAndroid Build Coastguard Worker 
setContentType(ContentType contentType)605*38e8c45fSAndroid Build Coastguard Worker Error Display::setContentType(ContentType contentType) {
606*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setContentType(mId, contentType);
607*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
608*38e8c45fSAndroid Build Coastguard Worker }
609*38e8c45fSAndroid Build Coastguard Worker 
getClientTargetProperty(aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness * outClientTargetProperty)610*38e8c45fSAndroid Build Coastguard Worker Error Display::getClientTargetProperty(
611*38e8c45fSAndroid Build Coastguard Worker         aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
612*38e8c45fSAndroid Build Coastguard Worker                 outClientTargetProperty) {
613*38e8c45fSAndroid Build Coastguard Worker     const auto error = mComposer.getClientTargetProperty(mId, outClientTargetProperty);
614*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
615*38e8c45fSAndroid Build Coastguard Worker }
616*38e8c45fSAndroid Build Coastguard Worker 
getRequestedLuts(LayerLuts * outLuts,LutFileDescriptorMapper & lutFileDescriptorMapper)617*38e8c45fSAndroid Build Coastguard Worker Error Display::getRequestedLuts(LayerLuts* outLuts,
618*38e8c45fSAndroid Build Coastguard Worker                                 LutFileDescriptorMapper& lutFileDescriptorMapper) {
619*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::Layer> layerIds;
620*38e8c45fSAndroid Build Coastguard Worker     std::vector<DisplayLuts::LayerLut> tmpLuts;
621*38e8c45fSAndroid Build Coastguard Worker     const auto error = static_cast<Error>(mComposer.getRequestedLuts(mId, &layerIds, &tmpLuts));
622*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
623*38e8c45fSAndroid Build Coastguard Worker         return error;
624*38e8c45fSAndroid Build Coastguard Worker     }
625*38e8c45fSAndroid Build Coastguard Worker 
626*38e8c45fSAndroid Build Coastguard Worker     uint32_t numElements = layerIds.size();
627*38e8c45fSAndroid Build Coastguard Worker     outLuts->clear();
628*38e8c45fSAndroid Build Coastguard Worker     for (uint32_t i = 0; i < numElements; ++i) {
629*38e8c45fSAndroid Build Coastguard Worker         auto layer = getLayerById(layerIds[i]);
630*38e8c45fSAndroid Build Coastguard Worker         if (layer) {
631*38e8c45fSAndroid Build Coastguard Worker             auto& layerLut = tmpLuts[i];
632*38e8c45fSAndroid Build Coastguard Worker             if (layerLut.luts.pfd.get() > 0 && layerLut.luts.offsets.has_value()) {
633*38e8c45fSAndroid Build Coastguard Worker                 const auto& offsets = layerLut.luts.offsets.value();
634*38e8c45fSAndroid Build Coastguard Worker                 std::vector<std::pair<int32_t, LutProperties>> lutOffsetsAndProperties;
635*38e8c45fSAndroid Build Coastguard Worker                 lutOffsetsAndProperties.reserve(offsets.size());
636*38e8c45fSAndroid Build Coastguard Worker                 std::transform(offsets.begin(), offsets.end(), layerLut.luts.lutProperties.begin(),
637*38e8c45fSAndroid Build Coastguard Worker                                std::back_inserter(lutOffsetsAndProperties),
638*38e8c45fSAndroid Build Coastguard Worker                                [](int32_t i, LutProperties j) { return std::make_pair(i, j); });
639*38e8c45fSAndroid Build Coastguard Worker                 outLuts->emplace_or_replace(layer.get(), lutOffsetsAndProperties);
640*38e8c45fSAndroid Build Coastguard Worker                 lutFileDescriptorMapper.emplace_or_replace(layer.get(),
641*38e8c45fSAndroid Build Coastguard Worker                                                            ndk::ScopedFileDescriptor(
642*38e8c45fSAndroid Build Coastguard Worker                                                                    layerLut.luts.pfd.release()));
643*38e8c45fSAndroid Build Coastguard Worker             }
644*38e8c45fSAndroid Build Coastguard Worker         }
645*38e8c45fSAndroid Build Coastguard Worker     }
646*38e8c45fSAndroid Build Coastguard Worker 
647*38e8c45fSAndroid Build Coastguard Worker     return Error::NONE;
648*38e8c45fSAndroid Build Coastguard Worker }
649*38e8c45fSAndroid Build Coastguard Worker 
getDisplayDecorationSupport(std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport> * support)650*38e8c45fSAndroid Build Coastguard Worker Error Display::getDisplayDecorationSupport(
651*38e8c45fSAndroid Build Coastguard Worker         std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
652*38e8c45fSAndroid Build Coastguard Worker                 support) {
653*38e8c45fSAndroid Build Coastguard Worker     const auto error = mComposer.getDisplayDecorationSupport(mId, support);
654*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
655*38e8c45fSAndroid Build Coastguard Worker }
656*38e8c45fSAndroid Build Coastguard Worker 
setIdleTimerEnabled(std::chrono::milliseconds timeout)657*38e8c45fSAndroid Build Coastguard Worker Error Display::setIdleTimerEnabled(std::chrono::milliseconds timeout) {
658*38e8c45fSAndroid Build Coastguard Worker     const auto error = mComposer.setIdleTimerEnabled(mId, timeout);
659*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
660*38e8c45fSAndroid Build Coastguard Worker }
661*38e8c45fSAndroid Build Coastguard Worker 
getMaxLayerPictureProfiles(int32_t * outMaxProfiles)662*38e8c45fSAndroid Build Coastguard Worker Error Display::getMaxLayerPictureProfiles(int32_t* outMaxProfiles) {
663*38e8c45fSAndroid Build Coastguard Worker     const auto error = mComposer.getMaxLayerPictureProfiles(mId, outMaxProfiles);
664*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
665*38e8c45fSAndroid Build Coastguard Worker }
666*38e8c45fSAndroid Build Coastguard Worker 
setPictureProfileHandle(const PictureProfileHandle & handle)667*38e8c45fSAndroid Build Coastguard Worker Error Display::setPictureProfileHandle(const PictureProfileHandle& handle) {
668*38e8c45fSAndroid Build Coastguard Worker     const auto error = mComposer.setDisplayPictureProfileId(mId, handle.getId());
669*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(error);
670*38e8c45fSAndroid Build Coastguard Worker }
671*38e8c45fSAndroid Build Coastguard Worker 
672*38e8c45fSAndroid Build Coastguard Worker // For use by Device
673*38e8c45fSAndroid Build Coastguard Worker 
setConnected(bool connected)674*38e8c45fSAndroid Build Coastguard Worker void Display::setConnected(bool connected) {
675*38e8c45fSAndroid Build Coastguard Worker     if (!mIsConnected && connected) {
676*38e8c45fSAndroid Build Coastguard Worker         mComposer.setClientTargetSlotCount(mId);
677*38e8c45fSAndroid Build Coastguard Worker     }
678*38e8c45fSAndroid Build Coastguard Worker     mIsConnected = connected;
679*38e8c45fSAndroid Build Coastguard Worker }
680*38e8c45fSAndroid Build Coastguard Worker 
681*38e8c45fSAndroid Build Coastguard Worker // Other Display methods
682*38e8c45fSAndroid Build Coastguard Worker 
getLayerById(HWLayerId id) const683*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HWC2::Layer> Display::getLayerById(HWLayerId id) const {
684*38e8c45fSAndroid Build Coastguard Worker     auto it = mLayers.find(id);
685*38e8c45fSAndroid Build Coastguard Worker     return it != mLayers.end() ? it->second.lock() : nullptr;
686*38e8c45fSAndroid Build Coastguard Worker }
687*38e8c45fSAndroid Build Coastguard Worker 
loadDisplayCapabilities()688*38e8c45fSAndroid Build Coastguard Worker void Display::loadDisplayCapabilities() {
689*38e8c45fSAndroid Build Coastguard Worker     std::call_once(mDisplayCapabilityQueryFlag, [this]() {
690*38e8c45fSAndroid Build Coastguard Worker         std::vector<DisplayCapability> tmpCapabilities;
691*38e8c45fSAndroid Build Coastguard Worker         auto error =
692*38e8c45fSAndroid Build Coastguard Worker                 static_cast<Error>(mComposer.getDisplayCapabilities(mId, &tmpCapabilities));
693*38e8c45fSAndroid Build Coastguard Worker         if (error == Error::NONE) {
694*38e8c45fSAndroid Build Coastguard Worker             std::scoped_lock lock(mDisplayCapabilitiesMutex);
695*38e8c45fSAndroid Build Coastguard Worker             mDisplayCapabilities.emplace();
696*38e8c45fSAndroid Build Coastguard Worker             for (auto capability : tmpCapabilities) {
697*38e8c45fSAndroid Build Coastguard Worker                 mDisplayCapabilities->emplace(capability);
698*38e8c45fSAndroid Build Coastguard Worker             }
699*38e8c45fSAndroid Build Coastguard Worker         } else if (error == Error::UNSUPPORTED) {
700*38e8c45fSAndroid Build Coastguard Worker             std::scoped_lock lock(mDisplayCapabilitiesMutex);
701*38e8c45fSAndroid Build Coastguard Worker             mDisplayCapabilities.emplace();
702*38e8c45fSAndroid Build Coastguard Worker             if (mCapabilities.count(AidlCapability::SKIP_CLIENT_COLOR_TRANSFORM)) {
703*38e8c45fSAndroid Build Coastguard Worker                 mDisplayCapabilities->emplace(DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
704*38e8c45fSAndroid Build Coastguard Worker             }
705*38e8c45fSAndroid Build Coastguard Worker             bool dozeSupport = false;
706*38e8c45fSAndroid Build Coastguard Worker             error = static_cast<Error>(mComposer.getDozeSupport(mId, &dozeSupport));
707*38e8c45fSAndroid Build Coastguard Worker             if (error == Error::NONE && dozeSupport) {
708*38e8c45fSAndroid Build Coastguard Worker                 mDisplayCapabilities->emplace(DisplayCapability::DOZE);
709*38e8c45fSAndroid Build Coastguard Worker             }
710*38e8c45fSAndroid Build Coastguard Worker         }
711*38e8c45fSAndroid Build Coastguard Worker     });
712*38e8c45fSAndroid Build Coastguard Worker }
713*38e8c45fSAndroid Build Coastguard Worker 
setPhysicalSizeInMm(std::optional<ui::Size> size)714*38e8c45fSAndroid Build Coastguard Worker void Display::setPhysicalSizeInMm(std::optional<ui::Size> size) {
715*38e8c45fSAndroid Build Coastguard Worker     mPhysicalSize = size;
716*38e8c45fSAndroid Build Coastguard Worker }
717*38e8c45fSAndroid Build Coastguard Worker 
718*38e8c45fSAndroid Build Coastguard Worker } // namespace impl
719*38e8c45fSAndroid Build Coastguard Worker 
720*38e8c45fSAndroid Build Coastguard Worker // Layer methods
721*38e8c45fSAndroid Build Coastguard Worker 
722*38e8c45fSAndroid Build Coastguard Worker namespace {
convertRegionToHwcRects(const Region & region)723*38e8c45fSAndroid Build Coastguard Worker std::vector<Hwc2::IComposerClient::Rect> convertRegionToHwcRects(const Region& region) {
724*38e8c45fSAndroid Build Coastguard Worker     size_t rectCount = 0;
725*38e8c45fSAndroid Build Coastguard Worker     Rect const* rectArray = region.getArray(&rectCount);
726*38e8c45fSAndroid Build Coastguard Worker 
727*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::IComposerClient::Rect> hwcRects;
728*38e8c45fSAndroid Build Coastguard Worker     hwcRects.reserve(rectCount);
729*38e8c45fSAndroid Build Coastguard Worker     for (size_t rect = 0; rect < rectCount; ++rect) {
730*38e8c45fSAndroid Build Coastguard Worker         hwcRects.push_back({rectArray[rect].left, rectArray[rect].top, rectArray[rect].right,
731*38e8c45fSAndroid Build Coastguard Worker                             rectArray[rect].bottom});
732*38e8c45fSAndroid Build Coastguard Worker     }
733*38e8c45fSAndroid Build Coastguard Worker     return hwcRects;
734*38e8c45fSAndroid Build Coastguard Worker }
735*38e8c45fSAndroid Build Coastguard Worker } // namespace
736*38e8c45fSAndroid Build Coastguard Worker 
737*38e8c45fSAndroid Build Coastguard Worker Layer::~Layer() = default;
738*38e8c45fSAndroid Build Coastguard Worker 
739*38e8c45fSAndroid Build Coastguard Worker namespace impl {
740*38e8c45fSAndroid Build Coastguard Worker 
Layer(android::Hwc2::Composer & composer,const std::unordered_set<AidlCapability> & capabilities,HWC2::Display & display,HWLayerId layerId)741*38e8c45fSAndroid Build Coastguard Worker Layer::Layer(android::Hwc2::Composer& composer,
742*38e8c45fSAndroid Build Coastguard Worker              const std::unordered_set<AidlCapability>& capabilities, HWC2::Display& display,
743*38e8c45fSAndroid Build Coastguard Worker              HWLayerId layerId)
744*38e8c45fSAndroid Build Coastguard Worker       : mComposer(composer),
745*38e8c45fSAndroid Build Coastguard Worker         mCapabilities(capabilities),
746*38e8c45fSAndroid Build Coastguard Worker         mDisplay(&display),
747*38e8c45fSAndroid Build Coastguard Worker         mId(layerId),
748*38e8c45fSAndroid Build Coastguard Worker         mColorMatrix(android::mat4()) {
749*38e8c45fSAndroid Build Coastguard Worker     ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, display.getId());
750*38e8c45fSAndroid Build Coastguard Worker }
751*38e8c45fSAndroid Build Coastguard Worker 
~Layer()752*38e8c45fSAndroid Build Coastguard Worker Layer::~Layer()
753*38e8c45fSAndroid Build Coastguard Worker {
754*38e8c45fSAndroid Build Coastguard Worker     onOwningDisplayDestroyed();
755*38e8c45fSAndroid Build Coastguard Worker }
756*38e8c45fSAndroid Build Coastguard Worker 
onOwningDisplayDestroyed()757*38e8c45fSAndroid Build Coastguard Worker void Layer::onOwningDisplayDestroyed() {
758*38e8c45fSAndroid Build Coastguard Worker     // Note: onOwningDisplayDestroyed() may be called to perform cleanup by
759*38e8c45fSAndroid Build Coastguard Worker     // either the Layer dtor or by the Display dtor and must be safe to call
760*38e8c45fSAndroid Build Coastguard Worker     // from either path. In particular, the call to Display::onLayerDestroyed()
761*38e8c45fSAndroid Build Coastguard Worker     // is expected to be safe to do,
762*38e8c45fSAndroid Build Coastguard Worker 
763*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
764*38e8c45fSAndroid Build Coastguard Worker         return;
765*38e8c45fSAndroid Build Coastguard Worker     }
766*38e8c45fSAndroid Build Coastguard Worker 
767*38e8c45fSAndroid Build Coastguard Worker     mDisplay->onLayerDestroyed(mId);
768*38e8c45fSAndroid Build Coastguard Worker 
769*38e8c45fSAndroid Build Coastguard Worker     // Note: If the HWC display was actually disconnected, these calls are will
770*38e8c45fSAndroid Build Coastguard Worker     // return an error. We always make them as there may be other reasons for
771*38e8c45fSAndroid Build Coastguard Worker     // the HWC2::Display to be destroyed.
772*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.destroyLayer(mDisplay->getId(), mId);
773*38e8c45fSAndroid Build Coastguard Worker     auto error = static_cast<Error>(intError);
774*38e8c45fSAndroid Build Coastguard Worker     ALOGE_IF(error != Error::NONE,
775*38e8c45fSAndroid Build Coastguard Worker              "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
776*38e8c45fSAndroid Build Coastguard Worker              " failed: %s (%d)",
777*38e8c45fSAndroid Build Coastguard Worker              mDisplay->getId(), mId, to_string(error).c_str(), intError);
778*38e8c45fSAndroid Build Coastguard Worker 
779*38e8c45fSAndroid Build Coastguard Worker     mDisplay = nullptr;
780*38e8c45fSAndroid Build Coastguard Worker }
781*38e8c45fSAndroid Build Coastguard Worker 
setCursorPosition(int32_t x,int32_t y)782*38e8c45fSAndroid Build Coastguard Worker Error Layer::setCursorPosition(int32_t x, int32_t y)
783*38e8c45fSAndroid Build Coastguard Worker {
784*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
785*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
786*38e8c45fSAndroid Build Coastguard Worker     }
787*38e8c45fSAndroid Build Coastguard Worker 
788*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setCursorPosition(mDisplay->getId(), mId, x, y);
789*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
790*38e8c45fSAndroid Build Coastguard Worker }
791*38e8c45fSAndroid Build Coastguard Worker 
setBuffer(uint32_t slot,const sp<GraphicBuffer> & buffer,const sp<Fence> & acquireFence)792*38e8c45fSAndroid Build Coastguard Worker Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
793*38e8c45fSAndroid Build Coastguard Worker         const sp<Fence>& acquireFence)
794*38e8c45fSAndroid Build Coastguard Worker {
795*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
796*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
797*38e8c45fSAndroid Build Coastguard Worker     }
798*38e8c45fSAndroid Build Coastguard Worker 
799*38e8c45fSAndroid Build Coastguard Worker     if (buffer == nullptr && mBufferSlot == slot) {
800*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
801*38e8c45fSAndroid Build Coastguard Worker     }
802*38e8c45fSAndroid Build Coastguard Worker     mBufferSlot = slot;
803*38e8c45fSAndroid Build Coastguard Worker 
804*38e8c45fSAndroid Build Coastguard Worker     int32_t fenceFd = acquireFence->dup();
805*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerBuffer(mDisplay->getId(), mId, slot, buffer, fenceFd);
806*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
807*38e8c45fSAndroid Build Coastguard Worker }
808*38e8c45fSAndroid Build Coastguard Worker 
setBufferSlotsToClear(const std::vector<uint32_t> & slotsToClear,uint32_t activeBufferSlot)809*38e8c45fSAndroid Build Coastguard Worker Error Layer::setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
810*38e8c45fSAndroid Build Coastguard Worker                                    uint32_t activeBufferSlot) {
811*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
812*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
813*38e8c45fSAndroid Build Coastguard Worker     }
814*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerBufferSlotsToClear(mDisplay->getId(), mId, slotsToClear,
815*38e8c45fSAndroid Build Coastguard Worker                                                          activeBufferSlot);
816*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
817*38e8c45fSAndroid Build Coastguard Worker }
818*38e8c45fSAndroid Build Coastguard Worker 
setSurfaceDamage(const Region & damage)819*38e8c45fSAndroid Build Coastguard Worker Error Layer::setSurfaceDamage(const Region& damage)
820*38e8c45fSAndroid Build Coastguard Worker {
821*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
822*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
823*38e8c45fSAndroid Build Coastguard Worker     }
824*38e8c45fSAndroid Build Coastguard Worker 
825*38e8c45fSAndroid Build Coastguard Worker     if (damage.isRect() && mDamageRegion.isRect() &&
826*38e8c45fSAndroid Build Coastguard Worker         (damage.getBounds() == mDamageRegion.getBounds())) {
827*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
828*38e8c45fSAndroid Build Coastguard Worker     }
829*38e8c45fSAndroid Build Coastguard Worker     mDamageRegion = damage;
830*38e8c45fSAndroid Build Coastguard Worker 
831*38e8c45fSAndroid Build Coastguard Worker     // We encode default full-screen damage as INVALID_RECT upstream, but as 0
832*38e8c45fSAndroid Build Coastguard Worker     // rects for HWC
833*38e8c45fSAndroid Build Coastguard Worker     Hwc2::Error intError = Hwc2::Error::NONE;
834*38e8c45fSAndroid Build Coastguard Worker     if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
835*38e8c45fSAndroid Build Coastguard Worker         intError = mComposer.setLayerSurfaceDamage(mDisplay->getId(), mId,
836*38e8c45fSAndroid Build Coastguard Worker                                                    std::vector<Hwc2::IComposerClient::Rect>());
837*38e8c45fSAndroid Build Coastguard Worker     } else {
838*38e8c45fSAndroid Build Coastguard Worker         const auto hwcRects = convertRegionToHwcRects(damage);
839*38e8c45fSAndroid Build Coastguard Worker         intError = mComposer.setLayerSurfaceDamage(mDisplay->getId(), mId, hwcRects);
840*38e8c45fSAndroid Build Coastguard Worker     }
841*38e8c45fSAndroid Build Coastguard Worker 
842*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
843*38e8c45fSAndroid Build Coastguard Worker }
844*38e8c45fSAndroid Build Coastguard Worker 
setBlendMode(BlendMode mode)845*38e8c45fSAndroid Build Coastguard Worker Error Layer::setBlendMode(BlendMode mode)
846*38e8c45fSAndroid Build Coastguard Worker {
847*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
848*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
849*38e8c45fSAndroid Build Coastguard Worker     }
850*38e8c45fSAndroid Build Coastguard Worker 
851*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerBlendMode(mDisplay->getId(), mId, mode);
852*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
853*38e8c45fSAndroid Build Coastguard Worker }
854*38e8c45fSAndroid Build Coastguard Worker 
setColor(Color color)855*38e8c45fSAndroid Build Coastguard Worker Error Layer::setColor(Color color) {
856*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
857*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
858*38e8c45fSAndroid Build Coastguard Worker     }
859*38e8c45fSAndroid Build Coastguard Worker 
860*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerColor(mDisplay->getId(), mId, color);
861*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
862*38e8c45fSAndroid Build Coastguard Worker }
863*38e8c45fSAndroid Build Coastguard Worker 
setCompositionType(Composition type)864*38e8c45fSAndroid Build Coastguard Worker Error Layer::setCompositionType(Composition type)
865*38e8c45fSAndroid Build Coastguard Worker {
866*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
867*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
868*38e8c45fSAndroid Build Coastguard Worker     }
869*38e8c45fSAndroid Build Coastguard Worker 
870*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerCompositionType(mDisplay->getId(), mId, type);
871*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
872*38e8c45fSAndroid Build Coastguard Worker }
873*38e8c45fSAndroid Build Coastguard Worker 
setDataspace(Dataspace dataspace)874*38e8c45fSAndroid Build Coastguard Worker Error Layer::setDataspace(Dataspace dataspace)
875*38e8c45fSAndroid Build Coastguard Worker {
876*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
877*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
878*38e8c45fSAndroid Build Coastguard Worker     }
879*38e8c45fSAndroid Build Coastguard Worker 
880*38e8c45fSAndroid Build Coastguard Worker     if (dataspace == mDataSpace) {
881*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
882*38e8c45fSAndroid Build Coastguard Worker     }
883*38e8c45fSAndroid Build Coastguard Worker     mDataSpace = dataspace;
884*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerDataspace(mDisplay->getId(), mId, mDataSpace);
885*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
886*38e8c45fSAndroid Build Coastguard Worker }
887*38e8c45fSAndroid Build Coastguard Worker 
setPerFrameMetadata(const int32_t supportedPerFrameMetadata,const android::HdrMetadata & metadata)888*38e8c45fSAndroid Build Coastguard Worker Error Layer::setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
889*38e8c45fSAndroid Build Coastguard Worker         const android::HdrMetadata& metadata)
890*38e8c45fSAndroid Build Coastguard Worker {
891*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
892*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
893*38e8c45fSAndroid Build Coastguard Worker     }
894*38e8c45fSAndroid Build Coastguard Worker 
895*38e8c45fSAndroid Build Coastguard Worker     if (metadata == mHdrMetadata) {
896*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
897*38e8c45fSAndroid Build Coastguard Worker     }
898*38e8c45fSAndroid Build Coastguard Worker 
899*38e8c45fSAndroid Build Coastguard Worker     mHdrMetadata = metadata;
900*38e8c45fSAndroid Build Coastguard Worker     int validTypes = mHdrMetadata.validTypes & supportedPerFrameMetadata;
901*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::PerFrameMetadata> perFrameMetadatas;
902*38e8c45fSAndroid Build Coastguard Worker     if (validTypes & HdrMetadata::SMPTE2086) {
903*38e8c45fSAndroid Build Coastguard Worker         perFrameMetadatas.insert(perFrameMetadatas.end(),
904*38e8c45fSAndroid Build Coastguard Worker                                  {{Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
905*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryRed.x},
906*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
907*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryRed.y},
908*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
909*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryGreen.x},
910*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
911*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryGreen.y},
912*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
913*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryBlue.x},
914*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
915*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.displayPrimaryBlue.y},
916*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::WHITE_POINT_X,
917*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.whitePoint.x},
918*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::WHITE_POINT_Y,
919*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.whitePoint.y},
920*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::MAX_LUMINANCE,
921*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.maxLuminance},
922*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::MIN_LUMINANCE,
923*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.smpte2086.minLuminance}});
924*38e8c45fSAndroid Build Coastguard Worker     }
925*38e8c45fSAndroid Build Coastguard Worker 
926*38e8c45fSAndroid Build Coastguard Worker     if (validTypes & HdrMetadata::CTA861_3) {
927*38e8c45fSAndroid Build Coastguard Worker         perFrameMetadatas.insert(perFrameMetadatas.end(),
928*38e8c45fSAndroid Build Coastguard Worker                                  {{Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
929*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.cta8613.maxContentLightLevel},
930*38e8c45fSAndroid Build Coastguard Worker                                   {Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
931*38e8c45fSAndroid Build Coastguard Worker                                    mHdrMetadata.cta8613.maxFrameAverageLightLevel}});
932*38e8c45fSAndroid Build Coastguard Worker     }
933*38e8c45fSAndroid Build Coastguard Worker 
934*38e8c45fSAndroid Build Coastguard Worker     const Error error = static_cast<Error>(
935*38e8c45fSAndroid Build Coastguard Worker         mComposer.setLayerPerFrameMetadata(mDisplay->getId(), mId, perFrameMetadatas));
936*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
937*38e8c45fSAndroid Build Coastguard Worker         return error;
938*38e8c45fSAndroid Build Coastguard Worker     }
939*38e8c45fSAndroid Build Coastguard Worker 
940*38e8c45fSAndroid Build Coastguard Worker     std::vector<Hwc2::PerFrameMetadataBlob> perFrameMetadataBlobs;
941*38e8c45fSAndroid Build Coastguard Worker     if (validTypes & HdrMetadata::HDR10PLUS) {
942*38e8c45fSAndroid Build Coastguard Worker         if (CC_UNLIKELY(mHdrMetadata.hdr10plus.size() == 0)) {
943*38e8c45fSAndroid Build Coastguard Worker             return Error::BAD_PARAMETER;
944*38e8c45fSAndroid Build Coastguard Worker         }
945*38e8c45fSAndroid Build Coastguard Worker 
946*38e8c45fSAndroid Build Coastguard Worker         perFrameMetadataBlobs.push_back(
947*38e8c45fSAndroid Build Coastguard Worker                 {Hwc2::PerFrameMetadataKey::HDR10_PLUS_SEI, mHdrMetadata.hdr10plus});
948*38e8c45fSAndroid Build Coastguard Worker     }
949*38e8c45fSAndroid Build Coastguard Worker 
950*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(
951*38e8c45fSAndroid Build Coastguard Worker             mComposer.setLayerPerFrameMetadataBlobs(mDisplay->getId(), mId, perFrameMetadataBlobs));
952*38e8c45fSAndroid Build Coastguard Worker }
953*38e8c45fSAndroid Build Coastguard Worker 
setDisplayFrame(const Rect & frame)954*38e8c45fSAndroid Build Coastguard Worker Error Layer::setDisplayFrame(const Rect& frame)
955*38e8c45fSAndroid Build Coastguard Worker {
956*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
957*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
958*38e8c45fSAndroid Build Coastguard Worker     }
959*38e8c45fSAndroid Build Coastguard Worker 
960*38e8c45fSAndroid Build Coastguard Worker     Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
961*38e8c45fSAndroid Build Coastguard Worker         frame.right, frame.bottom};
962*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerDisplayFrame(mDisplay->getId(), mId, hwcRect);
963*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
964*38e8c45fSAndroid Build Coastguard Worker }
965*38e8c45fSAndroid Build Coastguard Worker 
setPlaneAlpha(float alpha)966*38e8c45fSAndroid Build Coastguard Worker Error Layer::setPlaneAlpha(float alpha)
967*38e8c45fSAndroid Build Coastguard Worker {
968*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
969*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
970*38e8c45fSAndroid Build Coastguard Worker     }
971*38e8c45fSAndroid Build Coastguard Worker 
972*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerPlaneAlpha(mDisplay->getId(), mId, alpha);
973*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
974*38e8c45fSAndroid Build Coastguard Worker }
975*38e8c45fSAndroid Build Coastguard Worker 
setSidebandStream(const native_handle_t * stream)976*38e8c45fSAndroid Build Coastguard Worker Error Layer::setSidebandStream(const native_handle_t* stream)
977*38e8c45fSAndroid Build Coastguard Worker {
978*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
979*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
980*38e8c45fSAndroid Build Coastguard Worker     }
981*38e8c45fSAndroid Build Coastguard Worker 
982*38e8c45fSAndroid Build Coastguard Worker     if (mCapabilities.count(AidlCapability::SIDEBAND_STREAM) == 0) {
983*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Attempted to call setSidebandStream without checking that the "
984*38e8c45fSAndroid Build Coastguard Worker                 "device supports sideband streams");
985*38e8c45fSAndroid Build Coastguard Worker         return Error::UNSUPPORTED;
986*38e8c45fSAndroid Build Coastguard Worker     }
987*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerSidebandStream(mDisplay->getId(), mId, stream);
988*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
989*38e8c45fSAndroid Build Coastguard Worker }
990*38e8c45fSAndroid Build Coastguard Worker 
setSourceCrop(const FloatRect & crop)991*38e8c45fSAndroid Build Coastguard Worker Error Layer::setSourceCrop(const FloatRect& crop)
992*38e8c45fSAndroid Build Coastguard Worker {
993*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
994*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
995*38e8c45fSAndroid Build Coastguard Worker     }
996*38e8c45fSAndroid Build Coastguard Worker 
997*38e8c45fSAndroid Build Coastguard Worker     Hwc2::IComposerClient::FRect hwcRect{
998*38e8c45fSAndroid Build Coastguard Worker         crop.left, crop.top, crop.right, crop.bottom};
999*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerSourceCrop(mDisplay->getId(), mId, hwcRect);
1000*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1001*38e8c45fSAndroid Build Coastguard Worker }
1002*38e8c45fSAndroid Build Coastguard Worker 
setTransform(Transform transform)1003*38e8c45fSAndroid Build Coastguard Worker Error Layer::setTransform(Transform transform)
1004*38e8c45fSAndroid Build Coastguard Worker {
1005*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1006*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1007*38e8c45fSAndroid Build Coastguard Worker     }
1008*38e8c45fSAndroid Build Coastguard Worker 
1009*38e8c45fSAndroid Build Coastguard Worker     auto intTransform = static_cast<Hwc2::Transform>(transform);
1010*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerTransform(mDisplay->getId(), mId, intTransform);
1011*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1012*38e8c45fSAndroid Build Coastguard Worker }
1013*38e8c45fSAndroid Build Coastguard Worker 
setVisibleRegion(const Region & region)1014*38e8c45fSAndroid Build Coastguard Worker Error Layer::setVisibleRegion(const Region& region)
1015*38e8c45fSAndroid Build Coastguard Worker {
1016*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1017*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1018*38e8c45fSAndroid Build Coastguard Worker     }
1019*38e8c45fSAndroid Build Coastguard Worker 
1020*38e8c45fSAndroid Build Coastguard Worker     if (region.isRect() && mVisibleRegion.isRect() &&
1021*38e8c45fSAndroid Build Coastguard Worker         (region.getBounds() == mVisibleRegion.getBounds())) {
1022*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
1023*38e8c45fSAndroid Build Coastguard Worker     }
1024*38e8c45fSAndroid Build Coastguard Worker     mVisibleRegion = region;
1025*38e8c45fSAndroid Build Coastguard Worker     const auto hwcRects = convertRegionToHwcRects(region);
1026*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerVisibleRegion(mDisplay->getId(), mId, hwcRects);
1027*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1028*38e8c45fSAndroid Build Coastguard Worker }
1029*38e8c45fSAndroid Build Coastguard Worker 
setZOrder(uint32_t z)1030*38e8c45fSAndroid Build Coastguard Worker Error Layer::setZOrder(uint32_t z)
1031*38e8c45fSAndroid Build Coastguard Worker {
1032*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1033*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1034*38e8c45fSAndroid Build Coastguard Worker     }
1035*38e8c45fSAndroid Build Coastguard Worker 
1036*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerZOrder(mDisplay->getId(), mId, z);
1037*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1038*38e8c45fSAndroid Build Coastguard Worker }
1039*38e8c45fSAndroid Build Coastguard Worker 
1040*38e8c45fSAndroid Build Coastguard Worker // Composer HAL 2.3
setColorTransform(const android::mat4 & matrix)1041*38e8c45fSAndroid Build Coastguard Worker Error Layer::setColorTransform(const android::mat4& matrix) {
1042*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1043*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1044*38e8c45fSAndroid Build Coastguard Worker     }
1045*38e8c45fSAndroid Build Coastguard Worker 
1046*38e8c45fSAndroid Build Coastguard Worker     if (matrix == mColorMatrix) {
1047*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
1048*38e8c45fSAndroid Build Coastguard Worker     }
1049*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerColorTransform(mDisplay->getId(), mId, matrix.asArray());
1050*38e8c45fSAndroid Build Coastguard Worker     Error error = static_cast<Error>(intError);
1051*38e8c45fSAndroid Build Coastguard Worker     if (error != Error::NONE) {
1052*38e8c45fSAndroid Build Coastguard Worker         return error;
1053*38e8c45fSAndroid Build Coastguard Worker     }
1054*38e8c45fSAndroid Build Coastguard Worker     mColorMatrix = matrix;
1055*38e8c45fSAndroid Build Coastguard Worker     return error;
1056*38e8c45fSAndroid Build Coastguard Worker }
1057*38e8c45fSAndroid Build Coastguard Worker 
1058*38e8c45fSAndroid Build Coastguard Worker // Composer HAL 2.4
setLayerGenericMetadata(const std::string & name,bool mandatory,const std::vector<uint8_t> & value)1059*38e8c45fSAndroid Build Coastguard Worker Error Layer::setLayerGenericMetadata(const std::string& name, bool mandatory,
1060*38e8c45fSAndroid Build Coastguard Worker                                      const std::vector<uint8_t>& value) {
1061*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1062*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1063*38e8c45fSAndroid Build Coastguard Worker     }
1064*38e8c45fSAndroid Build Coastguard Worker 
1065*38e8c45fSAndroid Build Coastguard Worker     auto intError =
1066*38e8c45fSAndroid Build Coastguard Worker             mComposer.setLayerGenericMetadata(mDisplay->getId(), mId, name, mandatory, value);
1067*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1068*38e8c45fSAndroid Build Coastguard Worker }
1069*38e8c45fSAndroid Build Coastguard Worker 
1070*38e8c45fSAndroid Build Coastguard Worker // AIDL HAL
setBrightness(float brightness)1071*38e8c45fSAndroid Build Coastguard Worker Error Layer::setBrightness(float brightness) {
1072*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1073*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1074*38e8c45fSAndroid Build Coastguard Worker     }
1075*38e8c45fSAndroid Build Coastguard Worker 
1076*38e8c45fSAndroid Build Coastguard Worker     auto intError = mComposer.setLayerBrightness(mDisplay->getId(), mId, brightness);
1077*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1078*38e8c45fSAndroid Build Coastguard Worker }
1079*38e8c45fSAndroid Build Coastguard Worker 
setBlockingRegion(const Region & region)1080*38e8c45fSAndroid Build Coastguard Worker Error Layer::setBlockingRegion(const Region& region) {
1081*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1082*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1083*38e8c45fSAndroid Build Coastguard Worker     }
1084*38e8c45fSAndroid Build Coastguard Worker 
1085*38e8c45fSAndroid Build Coastguard Worker     if (region.isRect() && mBlockingRegion.isRect() &&
1086*38e8c45fSAndroid Build Coastguard Worker         (region.getBounds() == mBlockingRegion.getBounds())) {
1087*38e8c45fSAndroid Build Coastguard Worker         return Error::NONE;
1088*38e8c45fSAndroid Build Coastguard Worker     }
1089*38e8c45fSAndroid Build Coastguard Worker     mBlockingRegion = region;
1090*38e8c45fSAndroid Build Coastguard Worker     const auto hwcRects = convertRegionToHwcRects(region);
1091*38e8c45fSAndroid Build Coastguard Worker     const auto intError = mComposer.setLayerBlockingRegion(mDisplay->getId(), mId, hwcRects);
1092*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1093*38e8c45fSAndroid Build Coastguard Worker }
1094*38e8c45fSAndroid Build Coastguard Worker 
setLuts(aidl::android::hardware::graphics::composer3::Luts & luts)1095*38e8c45fSAndroid Build Coastguard Worker Error Layer::setLuts(aidl::android::hardware::graphics::composer3::Luts& luts) {
1096*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1097*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1098*38e8c45fSAndroid Build Coastguard Worker     }
1099*38e8c45fSAndroid Build Coastguard Worker     const auto intError = mComposer.setLayerLuts(mDisplay->getId(), mId, luts);
1100*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1101*38e8c45fSAndroid Build Coastguard Worker }
1102*38e8c45fSAndroid Build Coastguard Worker 
setPictureProfileHandle(const PictureProfileHandle & handle)1103*38e8c45fSAndroid Build Coastguard Worker Error Layer::setPictureProfileHandle(const PictureProfileHandle& handle) {
1104*38e8c45fSAndroid Build Coastguard Worker     if (CC_UNLIKELY(!mDisplay)) {
1105*38e8c45fSAndroid Build Coastguard Worker         return Error::BAD_DISPLAY;
1106*38e8c45fSAndroid Build Coastguard Worker     }
1107*38e8c45fSAndroid Build Coastguard Worker     const auto intError =
1108*38e8c45fSAndroid Build Coastguard Worker             mComposer.setLayerPictureProfileId(mDisplay->getId(), mId, handle.getId());
1109*38e8c45fSAndroid Build Coastguard Worker     return static_cast<Error>(intError);
1110*38e8c45fSAndroid Build Coastguard Worker }
1111*38e8c45fSAndroid Build Coastguard Worker 
1112*38e8c45fSAndroid Build Coastguard Worker } // namespace impl
1113*38e8c45fSAndroid Build Coastguard Worker } // namespace HWC2
1114*38e8c45fSAndroid Build Coastguard Worker } // namespace android
1115*38e8c45fSAndroid Build Coastguard Worker 
1116*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
1117*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop // ignored "-Wconversion"
1118