blob: 0eced73ff9de829642062ec9a8defb9a2cda9da4 [file] [log] [blame]
Ady Abrahame7385f72021-09-05 00:54:25 -07001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "HwcComposer"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ady Abraham9fc28052021-10-14 17:21:38 -070021#include "AidlComposerHal.h"
Ady Abrahame7385f72021-09-05 00:54:25 -070022
Brian Lindahl5b0ffe02023-06-15 14:19:43 -060023#include <SurfaceFlingerProperties.h>
Ady Abrahamc4acf512022-02-18 17:11:59 -080024#include <android-base/file.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070025#include <android/binder_ibinder_platform.h>
26#include <android/binder_manager.h>
Alec Mouri9b133ca2023-11-14 19:00:01 +000027#include <common/FlagManager.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000028#include <common/trace.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070029#include <log/log.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070030
31#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
32
33#include <algorithm>
34#include <cinttypes>
35
Yichi Chen3401b562022-01-17 15:42:35 +080036#include "HWC2.h"
37
Ady Abrahame7385f72021-09-05 00:54:25 -070038namespace android {
39
40using hardware::hidl_handle;
41using hardware::hidl_vec;
42using hardware::Return;
43
44using aidl::android::hardware::graphics::composer3::BnComposerCallback;
45using aidl::android::hardware::graphics::composer3::Capability;
Alec Mouri85065692022-03-18 00:58:26 +000046using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Sally Qi492cec32024-06-28 14:34:47 -070047using aidl::android::hardware::graphics::composer3::Lut;
Ady Abrahame7385f72021-09-05 00:54:25 -070048using aidl::android::hardware::graphics::composer3::PowerMode;
49using aidl::android::hardware::graphics::composer3::VirtualDisplay;
50
Ady Abraham42977362021-12-07 21:04:49 -080051using aidl::android::hardware::graphics::composer3::CommandResultPayload;
Ady Abrahama6388c02021-11-11 21:11:51 -080052
Ady Abrahame7385f72021-09-05 00:54:25 -070053using AidlColorMode = aidl::android::hardware::graphics::composer3::ColorMode;
54using AidlContentType = aidl::android::hardware::graphics::composer3::ContentType;
55using AidlDisplayIdentification =
56 aidl::android::hardware::graphics::composer3::DisplayIdentification;
57using AidlDisplayContentSample = aidl::android::hardware::graphics::composer3::DisplayContentSample;
58using AidlDisplayAttribute = aidl::android::hardware::graphics::composer3::DisplayAttribute;
59using AidlDisplayCapability = aidl::android::hardware::graphics::composer3::DisplayCapability;
Ady Abrahame7385f72021-09-05 00:54:25 -070060using AidlHdrCapabilities = aidl::android::hardware::graphics::composer3::HdrCapabilities;
Kriti Dang674b9372022-11-18 10:58:44 +010061using AidlHdrConversionCapability =
62 aidl::android::hardware::graphics::common::HdrConversionCapability;
63using AidlHdrConversionStrategy = aidl::android::hardware::graphics::common::HdrConversionStrategy;
Sally Qi0cbd08b2022-08-17 12:12:28 -070064using AidlOverlayProperties = aidl::android::hardware::graphics::composer3::OverlayProperties;
Ady Abrahame7385f72021-09-05 00:54:25 -070065using AidlPerFrameMetadata = aidl::android::hardware::graphics::composer3::PerFrameMetadata;
66using AidlPerFrameMetadataKey = aidl::android::hardware::graphics::composer3::PerFrameMetadataKey;
67using AidlPerFrameMetadataBlob = aidl::android::hardware::graphics::composer3::PerFrameMetadataBlob;
68using AidlRenderIntent = aidl::android::hardware::graphics::composer3::RenderIntent;
69using AidlVsyncPeriodChangeConstraints =
70 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeConstraints;
71using AidlVsyncPeriodChangeTimeline =
72 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline;
Ady Abrahame7385f72021-09-05 00:54:25 -070073using AidlDisplayContentSamplingAttributes =
74 aidl::android::hardware::graphics::composer3::DisplayContentSamplingAttributes;
75using AidlFormatColorComponent = aidl::android::hardware::graphics::composer3::FormatColorComponent;
76using AidlDisplayConnectionType =
77 aidl::android::hardware::graphics::composer3::DisplayConnectionType;
Ady Abrahame7385f72021-09-05 00:54:25 -070078
79using AidlColorTransform = aidl::android::hardware::graphics::common::ColorTransform;
80using AidlDataspace = aidl::android::hardware::graphics::common::Dataspace;
Huihong Luo791bef92023-09-19 21:18:45 -070081using AidlDisplayHotplugEvent = aidl::android::hardware::graphics::common::DisplayHotplugEvent;
Ady Abrahame7385f72021-09-05 00:54:25 -070082using AidlFRect = aidl::android::hardware::graphics::common::FRect;
83using AidlRect = aidl::android::hardware::graphics::common::Rect;
84using AidlTransform = aidl::android::hardware::graphics::common::Transform;
85
86namespace Hwc2 {
87
88namespace {
89
90template <typename To, typename From>
91To translate(From x) {
92 return static_cast<To>(x);
93}
94
95template <typename To, typename From>
96std::vector<To> translate(const std::vector<From>& in) {
97 std::vector<To> out;
98 out.reserve(in.size());
99 std::transform(in.begin(), in.end(), std::back_inserter(out),
100 [](From x) { return translate<To>(x); });
101 return out;
102}
103
104template <>
105AidlRect translate(IComposerClient::Rect x) {
106 return AidlRect{
107 .left = x.left,
108 .top = x.top,
109 .right = x.right,
110 .bottom = x.bottom,
111 };
112}
113
114template <>
115AidlFRect translate(IComposerClient::FRect x) {
116 return AidlFRect{
117 .left = x.left,
118 .top = x.top,
119 .right = x.right,
120 .bottom = x.bottom,
121 };
122}
123
124template <>
Ady Abrahame7385f72021-09-05 00:54:25 -0700125AidlPerFrameMetadataBlob translate(IComposerClient::PerFrameMetadataBlob x) {
126 AidlPerFrameMetadataBlob blob;
127 blob.key = translate<AidlPerFrameMetadataKey>(x.key),
Long Linga4628782022-02-18 13:44:26 -0800128 std::copy(x.blob.begin(), x.blob.end(), std::inserter(blob.blob, blob.blob.end()));
Ady Abrahame7385f72021-09-05 00:54:25 -0700129 return blob;
130}
131
132template <>
133AidlPerFrameMetadata translate(IComposerClient::PerFrameMetadata x) {
134 return AidlPerFrameMetadata{
135 .key = translate<AidlPerFrameMetadataKey>(x.key),
136 .value = x.value,
137 };
138}
139
140template <>
141DisplayedFrameStats translate(AidlDisplayContentSample x) {
142 return DisplayedFrameStats{
143 .numFrames = static_cast<uint64_t>(x.frameCount),
144 .component_0_sample = translate<uint64_t>(x.sampleComponent0),
145 .component_1_sample = translate<uint64_t>(x.sampleComponent1),
146 .component_2_sample = translate<uint64_t>(x.sampleComponent2),
147 .component_3_sample = translate<uint64_t>(x.sampleComponent3),
148 };
149}
150
151template <>
152AidlVsyncPeriodChangeConstraints translate(IComposerClient::VsyncPeriodChangeConstraints x) {
153 return AidlVsyncPeriodChangeConstraints{
154 .desiredTimeNanos = x.desiredTimeNanos,
155 .seamlessRequired = x.seamlessRequired,
156 };
157}
158
159template <>
160VsyncPeriodChangeTimeline translate(AidlVsyncPeriodChangeTimeline x) {
161 return VsyncPeriodChangeTimeline{
162 .newVsyncAppliedTimeNanos = x.newVsyncAppliedTimeNanos,
163 .refreshRequired = x.refreshRequired,
164 .refreshTimeNanos = x.refreshTimeNanos,
165 };
166}
Ady Abrahame7385f72021-09-05 00:54:25 -0700167mat4 makeMat4(std::vector<float> in) {
168 return mat4(static_cast<const float*>(in.data()));
169}
170
171} // namespace
172
173class AidlIComposerCallbackWrapper : public BnComposerCallback {
174public:
Yichi Chen3401b562022-01-17 15:42:35 +0800175 AidlIComposerCallbackWrapper(HWC2::ComposerCallback& callback) : mCallback(callback) {}
Ady Abrahame7385f72021-09-05 00:54:25 -0700176
177 ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override {
Huihong Luo791bef92023-09-19 21:18:45 -0700178 const auto event = in_connected ? AidlDisplayHotplugEvent::CONNECTED
179 : AidlDisplayHotplugEvent::DISCONNECTED;
180 mCallback.onComposerHalHotplugEvent(translate<Display>(in_display), event);
Ady Abrahame7385f72021-09-05 00:54:25 -0700181 return ::ndk::ScopedAStatus::ok();
182 }
183
184 ::ndk::ScopedAStatus onRefresh(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800185 mCallback.onComposerHalRefresh(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700186 return ::ndk::ScopedAStatus::ok();
187 }
Yichi Chen3401b562022-01-17 15:42:35 +0800188
Ady Abrahame7385f72021-09-05 00:54:25 -0700189 ::ndk::ScopedAStatus onSeamlessPossible(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800190 mCallback.onComposerHalSeamlessPossible(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700191 return ::ndk::ScopedAStatus::ok();
192 }
Yichi Chen3401b562022-01-17 15:42:35 +0800193
Ady Abrahame7385f72021-09-05 00:54:25 -0700194 ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
195 int32_t in_vsyncPeriodNanos) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800196 mCallback.onComposerHalVsync(translate<Display>(in_display), in_timestamp,
197 static_cast<uint32_t>(in_vsyncPeriodNanos));
Ady Abrahame7385f72021-09-05 00:54:25 -0700198 return ::ndk::ScopedAStatus::ok();
199 }
Yichi Chen3401b562022-01-17 15:42:35 +0800200
Ady Abrahame7385f72021-09-05 00:54:25 -0700201 ::ndk::ScopedAStatus onVsyncPeriodTimingChanged(
202 int64_t in_display, const AidlVsyncPeriodChangeTimeline& in_updatedTimeline) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800203 mCallback.onComposerHalVsyncPeriodTimingChanged(translate<Display>(in_display),
204 translate<V2_4::VsyncPeriodChangeTimeline>(
205 in_updatedTimeline));
206 return ::ndk::ScopedAStatus::ok();
207 }
208
209 ::ndk::ScopedAStatus onVsyncIdle(int64_t in_display) override {
210 mCallback.onComposerHalVsyncIdle(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700211 return ::ndk::ScopedAStatus::ok();
212 }
213
ramindani12bfe6b2023-02-03 13:29:19 -0800214 ::ndk::ScopedAStatus onRefreshRateChangedDebug(
215 const RefreshRateChangedDebugData& refreshRateChangedDebugData) override {
216 mCallback.onRefreshRateChangedDebug(refreshRateChangedDebugData);
217 return ::ndk::ScopedAStatus::ok();
218 }
219
Huihong Luo791bef92023-09-19 21:18:45 -0700220 ::ndk::ScopedAStatus onHotplugEvent(int64_t in_display,
221 AidlDisplayHotplugEvent event) override {
222 mCallback.onComposerHalHotplugEvent(translate<Display>(in_display), event);
223 return ::ndk::ScopedAStatus::ok();
224 }
225
Ady Abrahame7385f72021-09-05 00:54:25 -0700226private:
Yichi Chen3401b562022-01-17 15:42:35 +0800227 HWC2::ComposerCallback& mCallback;
Ady Abrahame7385f72021-09-05 00:54:25 -0700228};
229
Ady Abraham9fc28052021-10-14 17:21:38 -0700230std::string AidlComposer::instance(const std::string& serviceName) {
231 return std::string(AidlIComposer::descriptor) + "/" + serviceName;
232}
233
234bool AidlComposer::isDeclared(const std::string& serviceName) {
235 return AServiceManager_isDeclared(instance(serviceName).c_str());
236}
Ady Abrahame7385f72021-09-05 00:54:25 -0700237
Ady Abrahama6388c02021-11-11 21:11:51 -0800238AidlComposer::AidlComposer(const std::string& serviceName) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700239 // This only waits if the service is actually declared
Ady Abraham9fc28052021-10-14 17:21:38 -0700240 mAidlComposer = AidlIComposer::fromBinder(
241 ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
Ady Abrahame7385f72021-09-05 00:54:25 -0700242 if (!mAidlComposer) {
243 LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
244 return;
245 }
246
247 if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
248 LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
249 return;
250 }
251
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400252 addReader(translate<Display>(kSingleReaderKey));
253
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700254 // If unable to read interface version, then become backwards compatible.
ramindani0cd1d8d2023-06-13 13:43:23 -0700255 const auto status = mAidlComposerClient->getInterfaceVersion(&mComposerInterfaceVersion);
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700256 if (!status.isOk()) {
257 ALOGE("getInterfaceVersion for AidlComposer constructor failed %s",
258 status.getDescription().c_str());
259 }
ramindani0cd1d8d2023-06-13 13:43:23 -0700260
261 if (mComposerInterfaceVersion <= 1) {
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600262 if (sysprop::clear_slots_with_set_layer_buffer(false)) {
263 mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
264 GraphicBuffer::USAGE_HW_COMPOSER |
265 GraphicBuffer::USAGE_SW_READ_OFTEN |
266 GraphicBuffer::USAGE_SW_WRITE_OFTEN,
267 "AidlComposer");
268 if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
269 LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
270 return;
271 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700272 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700273 }
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000274 if (getLayerLifecycleBatchCommand()) {
275 mEnableLayerCommandBatchingFlag =
276 FlagManager::getInstance().enable_layer_command_batching();
277 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700278 ALOGI("Loaded AIDL composer3 HAL service");
279}
280
281AidlComposer::~AidlComposer() = default;
282
Ady Abraham4d211cf2021-12-14 16:19:03 -0800283bool AidlComposer::isSupported(OptionalFeature feature) const {
284 switch (feature) {
285 case OptionalFeature::RefreshRateSwitching:
Ady Abraham43065bd2021-12-10 17:22:15 -0800286 case OptionalFeature::ExpectedPresentTime:
Alec Mouricdf16792021-12-10 13:16:06 -0800287 case OptionalFeature::DisplayBrightnessCommand:
ramindani32cf0602022-03-02 02:30:29 +0000288 case OptionalFeature::KernelIdleTimer:
ramindani06e518e2022-03-14 18:47:53 +0000289 case OptionalFeature::PhysicalDisplayOrientation:
Ady Abraham4d211cf2021-12-14 16:19:03 -0800290 return true;
291 }
292}
293
ramindani19919ff2023-12-07 11:27:06 -0800294bool AidlComposer::isVrrSupported() const {
Sally Qid57eb0d2023-11-07 16:46:15 -0800295 return mComposerInterfaceVersion >= 3 && FlagManager::getInstance().vrr_config();
ramindani0cd1d8d2023-06-13 13:43:23 -0700296}
297
Ady Abrahamde549d42022-01-26 19:19:17 -0800298std::vector<Capability> AidlComposer::getCapabilities() {
Ady Abrahame7385f72021-09-05 00:54:25 -0700299 std::vector<Capability> capabilities;
300 const auto status = mAidlComposer->getCapabilities(&capabilities);
301 if (!status.isOk()) {
302 ALOGE("getCapabilities failed %s", status.getDescription().c_str());
303 return {};
304 }
Ady Abrahamde549d42022-01-26 19:19:17 -0800305 return capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700306}
307
308std::string AidlComposer::dumpDebugInfo() {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800309 int pipefds[2];
310 int result = pipe(pipefds);
311 if (result < 0) {
312 ALOGE("dumpDebugInfo: pipe failed: %s", strerror(errno));
Ady Abrahame7385f72021-09-05 00:54:25 -0700313 return {};
314 }
Ady Abrahamc4acf512022-02-18 17:11:59 -0800315
316 std::string str;
yihsing.shen58847c52022-09-23 15:39:30 +0800317 // Use other thread to read pipe to prevent
318 // pipe is full, making HWC be blocked in writing.
319 std::thread t([&]() {
320 base::ReadFdToString(pipefds[0], &str);
321 });
Ady Abrahamc4acf512022-02-18 17:11:59 -0800322 const auto status = mAidlComposer->dump(pipefds[1], /*args*/ nullptr, /*numArgs*/ 0);
323 // Close the write-end of the pipe to make sure that when reading from the
324 // read-end we will get eof instead of blocking forever
325 close(pipefds[1]);
326
yihsing.shen58847c52022-09-23 15:39:30 +0800327 if (status != STATUS_OK) {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800328 ALOGE("dumpDebugInfo: dump failed: %d", status);
329 }
330
yihsing.shen58847c52022-09-23 15:39:30 +0800331 t.join();
Ady Abrahamc4acf512022-02-18 17:11:59 -0800332 close(pipefds[0]);
Ady Abrahamb7b9a4f2024-01-25 15:57:24 -0800333
334 std::string hash;
335 mAidlComposer->getInterfaceHash(&hash);
336 return std::string(mAidlComposer->descriptor) +
337 " version:" + std::to_string(mComposerInterfaceVersion) + " hash:" + hash + str;
Ady Abrahame7385f72021-09-05 00:54:25 -0700338}
339
Yichi Chen3401b562022-01-17 15:42:35 +0800340void AidlComposer::registerCallback(HWC2::ComposerCallback& callback) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700341 if (mAidlComposerCallback) {
342 ALOGE("Callback already registered");
343 }
Yichi Chen3401b562022-01-17 15:42:35 +0800344
Ady Abraham9fc28052021-10-14 17:21:38 -0700345 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
shuide chenf1111e12024-01-03 18:41:38 +0800346
347 ndk::SpAIBinder binder = mAidlComposerCallback->asBinder();
348 AIBinder_setMinSchedulerPolicy(binder.get(), SCHED_FIFO, 2);
Ady Abrahame7385f72021-09-05 00:54:25 -0700349
350 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
351 if (!status.isOk()) {
352 ALOGE("registerCallback failed %s", status.getDescription().c_str());
353 }
354}
355
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400356Error AidlComposer::executeCommands(Display display) {
357 mMutex.lock_shared();
358 auto error = execute(display);
359 mMutex.unlock_shared();
360 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700361}
362
363uint32_t AidlComposer::getMaxVirtualDisplayCount() {
364 int32_t count = 0;
365 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
366 if (!status.isOk()) {
367 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
368 return 0;
369 }
370 return static_cast<uint32_t>(count);
371}
372
373Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
374 Display* outDisplay) {
375 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
376 const int32_t bufferSlotCount = 1;
377 VirtualDisplay virtualDisplay;
378 const auto status =
379 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
380 static_cast<int32_t>(height),
381 static_cast<AidlPixelFormat>(*format),
382 bufferSlotCount, &virtualDisplay);
383
384 if (!status.isOk()) {
385 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
386 return static_cast<Error>(status.getServiceSpecificError());
387 }
388
389 *outDisplay = translate<Display>(virtualDisplay.display);
390 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400391 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700392 return Error::NONE;
393}
394
395Error AidlComposer::destroyVirtualDisplay(Display display) {
396 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
397 if (!status.isOk()) {
398 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
399 return static_cast<Error>(status.getServiceSpecificError());
400 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400401 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700402 return Error::NONE;
403}
404
405Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400406 Error error = Error::NONE;
407 mMutex.lock_shared();
408 if (auto writer = getWriter(display)) {
409 writer->get().acceptDisplayChanges(translate<int64_t>(display));
410 } else {
411 error = Error::BAD_DISPLAY;
412 }
413 mMutex.unlock_shared();
414 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700415}
416
417Error AidlComposer::createLayer(Display display, Layer* outLayer) {
418 int64_t layer;
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000419 Error error = Error::NONE;
420 if (!mEnableLayerCommandBatchingFlag) {
421 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
422 kMaxLayerBufferCount, &layer);
423 if (!status.isOk()) {
424 ALOGE("createLayer failed %s", status.getDescription().c_str());
425 return static_cast<Error>(status.getServiceSpecificError());
426 }
427 } else {
428 // generate a unique layerID. map in AidlComposer with <SF_layerID, HWC_layerID>
429 // Add this as a new displayCommand in execute command.
430 // return the SF generated layerID instead of calling HWC
431 layer = mLayerID++;
432 mMutex.lock_shared();
433 if (auto writer = getWriter(display)) {
434 writer->get().setLayerLifecycleBatchCommandType(translate<int64_t>(display),
435 translate<int64_t>(layer),
436 LayerLifecycleBatchCommandType::CREATE);
437 writer->get().setNewBufferSlotCount(translate<int64_t>(display),
438 translate<int64_t>(layer), kMaxLayerBufferCount);
439 } else {
440 error = Error::BAD_DISPLAY;
441 }
442 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700443 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700444 *outLayer = translate<Layer>(layer);
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000445 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700446}
447
448Error AidlComposer::destroyLayer(Display display, Layer layer) {
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000449 Error error = Error::NONE;
450 if (!mEnableLayerCommandBatchingFlag) {
451 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
452 translate<int64_t>(layer));
453 if (!status.isOk()) {
454 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
455 return static_cast<Error>(status.getServiceSpecificError());
456 }
457 } else {
458 mMutex.lock_shared();
459 if (auto writer = getWriter(display)) {
460 writer->get()
461 .setLayerLifecycleBatchCommandType(translate<int64_t>(display),
462 translate<int64_t>(layer),
463 LayerLifecycleBatchCommandType::DESTROY);
464 } else {
465 error = Error::BAD_DISPLAY;
466 }
467 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700468 }
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000469
470 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700471}
472
473Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
474 int32_t config;
475 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
476 if (!status.isOk()) {
477 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
478 return static_cast<Error>(status.getServiceSpecificError());
479 }
480 *outConfig = translate<Config>(config);
481 return Error::NONE;
482}
483
484Error AidlComposer::getChangedCompositionTypes(
485 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500486 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400487 std::vector<ChangedCompositionLayer> changedLayers;
488 Error error = Error::NONE;
489 {
490 mMutex.lock_shared();
491 if (auto reader = getReader(display)) {
492 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
493 } else {
494 error = Error::BAD_DISPLAY;
495 }
496 mMutex.unlock_shared();
497 }
Ady Abrahamde792782021-12-20 10:00:49 -0800498 outLayers->reserve(changedLayers.size());
499 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800500
Ady Abrahamde792782021-12-20 10:00:49 -0800501 for (const auto& layer : changedLayers) {
502 outLayers->emplace_back(translate<Layer>(layer.layer));
503 outTypes->emplace_back(layer.composition);
504 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400505 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700506}
507
508Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
509 std::vector<AidlColorMode> modes;
510 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
511 if (!status.isOk()) {
512 ALOGE("getColorModes failed %s", status.getDescription().c_str());
513 return static_cast<Error>(status.getServiceSpecificError());
514 }
515 *outModes = translate<ColorMode>(modes);
516 return Error::NONE;
517}
518
519Error AidlComposer::getDisplayAttribute(Display display, Config config,
520 IComposerClient::Attribute attribute, int32_t* outValue) {
521 const auto status =
522 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
523 translate<int32_t>(config),
524 static_cast<AidlDisplayAttribute>(attribute),
525 outValue);
526 if (!status.isOk()) {
527 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
528 return static_cast<Error>(status.getServiceSpecificError());
529 }
530 return Error::NONE;
531}
532
533Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
534 std::vector<int32_t> configs;
535 const auto status =
536 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
537 if (!status.isOk()) {
538 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
539 return static_cast<Error>(status.getServiceSpecificError());
540 }
541 *outConfigs = translate<Config>(configs);
542 return Error::NONE;
543}
544
ramindani263a3f12023-07-18 20:44:49 -0700545Error AidlComposer::getDisplayConfigurations(Display display, int32_t maxFrameIntervalNs,
ramindani0cd1d8d2023-06-13 13:43:23 -0700546 std::vector<DisplayConfiguration>* outConfigs) {
547 const auto status =
ramindani263a3f12023-07-18 20:44:49 -0700548 mAidlComposerClient->getDisplayConfigurations(translate<int64_t>(display),
549 maxFrameIntervalNs, outConfigs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700550 if (!status.isOk()) {
551 ALOGE("getDisplayConfigurations failed %s", status.getDescription().c_str());
552 return static_cast<Error>(status.getServiceSpecificError());
553 }
554
555 return Error::NONE;
556}
557
Ady Abrahame7385f72021-09-05 00:54:25 -0700558Error AidlComposer::getDisplayName(Display display, std::string* outName) {
559 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
560 if (!status.isOk()) {
561 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
562 return static_cast<Error>(status.getServiceSpecificError());
563 }
564 return Error::NONE;
565}
566
567Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
568 std::vector<Layer>* outLayers,
569 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400570 Error error = Error::NONE;
571 DisplayRequest displayRequests;
572 {
573 mMutex.lock_shared();
574 if (auto reader = getReader(display)) {
575 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
576 } else {
577 error = Error::BAD_DISPLAY;
578 }
579 mMutex.unlock_shared();
580 }
Ady Abrahamde792782021-12-20 10:00:49 -0800581 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
582 outLayers->reserve(displayRequests.layerRequests.size());
583 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
584
585 for (const auto& layer : displayRequests.layerRequests) {
586 outLayers->emplace_back(translate<Layer>(layer.layer));
587 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
588 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400589 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700590}
591
592Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800593 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700594 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800595 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700596 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800597 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700598 return static_cast<Error>(status.getServiceSpecificError());
599 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800600 *outSupport = std::find(capabilities.begin(), capabilities.end(),
601 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700602 return Error::NONE;
603}
604
ramindani32cf0602022-03-02 02:30:29 +0000605Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
606 std::vector<AidlDisplayCapability> capabilities;
607 const auto status =
608 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
609 if (!status.isOk()) {
610 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
611 return static_cast<Error>(status.getServiceSpecificError());
612 }
613 *outSupport = std::find(capabilities.begin(), capabilities.end(),
614 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
615 return Error::NONE;
616}
617
Ady Abrahame7385f72021-09-05 00:54:25 -0700618Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
619 float* outMaxLuminance, float* outMaxAverageLuminance,
620 float* outMinLuminance) {
621 AidlHdrCapabilities capabilities;
622 const auto status =
623 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
624 if (!status.isOk()) {
625 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
626 return static_cast<Error>(status.getServiceSpecificError());
627 }
628
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100629 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700630 *outMaxLuminance = capabilities.maxLuminance;
631 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
632 *outMinLuminance = capabilities.minLuminance;
633 return Error::NONE;
634}
635
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000636bool AidlComposer::getLayerLifecycleBatchCommand() {
637 std::vector<Capability> capabilities = getCapabilities();
638 bool hasCapability = std::find(capabilities.begin(), capabilities.end(),
639 Capability::LAYER_LIFECYCLE_BATCH_COMMAND) != capabilities.end();
640 return hasCapability;
641}
642
Sally Qibb866c12022-10-17 11:31:20 -0700643Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
644 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
645 if (!status.isOk()) {
646 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
647 return static_cast<Error>(status.getServiceSpecificError());
648 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700649 return Error::NONE;
650}
651
Ady Abrahame7385f72021-09-05 00:54:25 -0700652Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
653 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400654 Error error = Error::NONE;
655 std::vector<ReleaseFences::Layer> fences;
656 {
657 mMutex.lock_shared();
658 if (auto reader = getReader(display)) {
659 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
660 } else {
661 error = Error::BAD_DISPLAY;
662 }
663 mMutex.unlock_shared();
664 }
Ady Abrahamde792782021-12-20 10:00:49 -0800665 outLayers->reserve(fences.size());
666 outReleaseFences->reserve(fences.size());
667
668 for (auto& fence : fences) {
669 outLayers->emplace_back(translate<Layer>(fence.layer));
670 // take ownership
671 const int fenceOwner = fence.fence.get();
672 *fence.fence.getR() = -1;
673 outReleaseFences->emplace_back(fenceOwner);
674 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400675 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700676}
677
678Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500679 const auto displayId = translate<int64_t>(display);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000680 SFTRACE_FORMAT("HwcPresentDisplay %" PRId64, displayId);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500681
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400682 Error error = Error::NONE;
683 mMutex.lock_shared();
684 auto writer = getWriter(display);
685 auto reader = getReader(display);
686 if (writer && reader) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500687 writer->get().presentDisplay(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400688 error = execute(display);
689 } else {
690 error = Error::BAD_DISPLAY;
691 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700692
Ady Abrahame7385f72021-09-05 00:54:25 -0700693 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400694 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700695 return error;
696 }
697
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500698 auto fence = reader->get().takePresentFence(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400699 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800700 // take ownership
701 *outPresentFence = fence.get();
702 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700703 return Error::NONE;
704}
705
706Error AidlComposer::setActiveConfig(Display display, Config config) {
707 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
708 translate<int32_t>(config));
709 if (!status.isOk()) {
710 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
711 return static_cast<Error>(status.getServiceSpecificError());
712 }
713 return Error::NONE;
714}
715
716Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
717 int acquireFence, Dataspace dataspace,
Alec Mourif97df4d2023-09-06 02:10:05 +0000718 const std::vector<IComposerClient::Rect>& damage,
719 float hdrSdrRatio) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700720 const native_handle_t* handle = nullptr;
721 if (target.get()) {
722 handle = target->getNativeBuffer()->handle;
723 }
724
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400725 Error error = Error::NONE;
726 mMutex.lock_shared();
727 if (auto writer = getWriter(display)) {
728 writer->get()
729 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
730 translate<aidl::android::hardware::graphics::common::Dataspace>(
731 dataspace),
Alec Mourif97df4d2023-09-06 02:10:05 +0000732 translate<AidlRect>(damage), hdrSdrRatio);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400733 } else {
734 error = Error::BAD_DISPLAY;
735 }
736 mMutex.unlock_shared();
737 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700738}
739
740Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
741 const auto status =
742 mAidlComposerClient->setColorMode(translate<int64_t>(display),
743 translate<AidlColorMode>(mode),
744 translate<AidlRenderIntent>(renderIntent));
745 if (!status.isOk()) {
746 ALOGE("setColorMode failed %s", status.getDescription().c_str());
747 return static_cast<Error>(status.getServiceSpecificError());
748 }
749 return Error::NONE;
750}
751
Ady Abrahamdc011a92021-12-21 14:06:44 -0800752Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400753 auto error = Error::NONE;
754 mMutex.lock_shared();
755 if (auto writer = getWriter(display)) {
756 writer->get().setColorTransform(translate<int64_t>(display), matrix);
757 } else {
758 error = Error::BAD_DISPLAY;
759 }
760 mMutex.unlock_shared();
761 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700762}
763
764Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
765 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400766 auto error = Error::NONE;
767 mMutex.lock_shared();
768 if (auto writer = getWriter(display)) {
769 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
770 } else {
771 error = Error::BAD_DISPLAY;
772 }
773 mMutex.unlock_shared();
774 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700775}
776
777Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
778 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
779 translate<PowerMode>(mode));
780 if (!status.isOk()) {
781 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
782 return static_cast<Error>(status.getServiceSpecificError());
783 }
784 return Error::NONE;
785}
786
787Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
788 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
789 const auto status =
790 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
791 if (!status.isOk()) {
792 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
793 return static_cast<Error>(status.getServiceSpecificError());
794 }
795 return Error::NONE;
796}
797
798Error AidlComposer::setClientTargetSlotCount(Display display) {
799 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
800 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
801 bufferSlotCount);
802 if (!status.isOk()) {
803 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
804 return static_cast<Error>(status.getServiceSpecificError());
805 }
806 return Error::NONE;
807}
808
Ady Abraham43065bd2021-12-10 17:22:15 -0800809Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani09acbb82023-11-03 09:02:38 -0700810 int32_t frameIntervalNs, uint32_t* outNumTypes,
811 uint32_t* outNumRequests) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400812 const auto displayId = translate<int64_t>(display);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000813 SFTRACE_FORMAT("HwcValidateDisplay %" PRId64, displayId);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500814
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400815 Error error = Error::NONE;
816 mMutex.lock_shared();
817 auto writer = getWriter(display);
818 auto reader = getReader(display);
819 if (writer && reader) {
ramindani09acbb82023-11-03 09:02:38 -0700820 writer->get().validateDisplay(displayId, ClockMonotonicTimestamp{expectedPresentTime},
821 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400822 error = execute(display);
823 } else {
824 error = Error::BAD_DISPLAY;
825 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700826
Ady Abrahame7385f72021-09-05 00:54:25 -0700827 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400828 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700829 return error;
830 }
831
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400832 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700833
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400834 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700835 return Error::NONE;
836}
837
Ady Abraham43065bd2021-12-10 17:22:15 -0800838Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700839 int32_t frameIntervalNs, uint32_t* outNumTypes,
840 uint32_t* outNumRequests, int* outPresentFence,
841 uint32_t* state) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400842 const auto displayId = translate<int64_t>(display);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000843 SFTRACE_FORMAT("HwcPresentOrValidateDisplay %" PRId64, displayId);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500844
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400845 Error error = Error::NONE;
846 mMutex.lock_shared();
847 auto writer = getWriter(display);
848 auto reader = getReader(display);
849 if (writer && reader) {
850 writer->get().presentOrvalidateDisplay(displayId,
ramindani4aac32c2023-10-30 14:13:30 -0700851 ClockMonotonicTimestamp{expectedPresentTime},
852 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400853 error = execute(display);
854 } else {
855 error = Error::BAD_DISPLAY;
856 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700857
Ady Abrahame7385f72021-09-05 00:54:25 -0700858 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400859 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700860 return error;
861 }
862
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400863 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800864 if (!result.has_value()) {
865 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400866 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800867 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700868 }
869
Ady Abrahamde792782021-12-20 10:00:49 -0800870 *state = translate<uint32_t>(*result);
871
872 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400873 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800874 // take ownership
875 *outPresentFence = fence.get();
876 *fence.getR() = -1;
877 }
878
879 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400880 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700881 }
882
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400883 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700884 return Error::NONE;
885}
886
887Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400888 Error error = Error::NONE;
889 mMutex.lock_shared();
890 if (auto writer = getWriter(display)) {
891 writer->get().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
892 x, y);
893 } else {
894 error = Error::BAD_DISPLAY;
895 }
896 mMutex.unlock_shared();
897 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700898}
899
900Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
901 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700902 const native_handle_t* handle = nullptr;
903 if (buffer.get()) {
904 handle = buffer->getNativeBuffer()->handle;
905 }
906
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400907 Error error = Error::NONE;
908 mMutex.lock_shared();
909 if (auto writer = getWriter(display)) {
910 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
911 handle, acquireFence);
912 } else {
913 error = Error::BAD_DISPLAY;
914 }
915 mMutex.unlock_shared();
916 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700917}
918
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700919Error AidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
920 const std::vector<uint32_t>& slotsToClear,
921 uint32_t activeBufferSlot) {
922 if (slotsToClear.empty()) {
923 return Error::NONE;
924 }
925
Brian Lindahl90553da2022-12-06 13:36:30 -0700926 Error error = Error::NONE;
927 mMutex.lock_shared();
928 if (auto writer = getWriter(display)) {
ramindani0cd1d8d2023-06-13 13:43:23 -0700929 if (mComposerInterfaceVersion > 1) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700930 writer->get().setLayerBufferSlotsToClear(translate<int64_t>(display),
931 translate<int64_t>(layer), slotsToClear);
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600932 // Backwards compatible way of clearing buffer slots is to set the layer buffer with a
933 // placeholder buffer, using the slot that needs to cleared... tricky.
934 } else if (mClearSlotBuffer != nullptr) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700935 for (uint32_t slot : slotsToClear) {
936 // Don't clear the active buffer slot because we need to restore the active buffer
937 // after clearing the requested buffer slots with a placeholder buffer.
938 if (slot != activeBufferSlot) {
939 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
940 translate<int64_t>(layer), slot,
941 mClearSlotBuffer->handle,
942 /*fence*/ -1);
943 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700944 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700945 // Since we clear buffers by setting them to a placeholder buffer, we want to make
946 // sure that the last setLayerBuffer command is sent with the currently active
947 // buffer, not the placeholder buffer, so that there is no perceptual change when
948 // buffers are discarded.
949 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
950 translate<int64_t>(layer), activeBufferSlot,
951 // The active buffer is still cached in
952 // its slot and doesn't need a fence.
953 /*buffer*/ nullptr, /*fence*/ -1);
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700954 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700955 } else {
956 error = Error::BAD_DISPLAY;
957 }
958 mMutex.unlock_shared();
959 return error;
960}
961
Ady Abrahame7385f72021-09-05 00:54:25 -0700962Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
963 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400964 Error error = Error::NONE;
965 mMutex.lock_shared();
966 if (auto writer = getWriter(display)) {
967 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
968 translate<AidlRect>(damage));
969 } else {
970 error = Error::BAD_DISPLAY;
971 }
972 mMutex.unlock_shared();
973 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700974}
975
976Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
977 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400978 Error error = Error::NONE;
979 mMutex.lock_shared();
980 if (auto writer = getWriter(display)) {
981 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
982 translate<BlendMode>(mode));
983 } else {
984 error = Error::BAD_DISPLAY;
985 }
986 mMutex.unlock_shared();
987 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700988}
989
Ady Abraham6e60b142022-01-06 18:10:35 -0800990Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400991 Error error = Error::NONE;
992 mMutex.lock_shared();
993 if (auto writer = getWriter(display)) {
994 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
995 } else {
996 error = Error::BAD_DISPLAY;
997 }
998 mMutex.unlock_shared();
999 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001000}
1001
Leon Scroggins III2e1aa182021-12-01 17:33:12 -05001002Error AidlComposer::setLayerCompositionType(
1003 Display display, Layer layer,
1004 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001005 Error error = Error::NONE;
1006 mMutex.lock_shared();
1007 if (auto writer = getWriter(display)) {
1008 writer->get().setLayerCompositionType(translate<int64_t>(display),
1009 translate<int64_t>(layer), type);
1010 } else {
1011 error = Error::BAD_DISPLAY;
1012 }
1013 mMutex.unlock_shared();
1014 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001015}
1016
1017Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001018 Error error = Error::NONE;
1019 mMutex.lock_shared();
1020 if (auto writer = getWriter(display)) {
1021 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
1022 translate<AidlDataspace>(dataspace));
1023 } else {
1024 error = Error::BAD_DISPLAY;
1025 }
1026 mMutex.unlock_shared();
1027 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001028}
1029
1030Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
1031 const IComposerClient::Rect& frame) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001032 Error error = Error::NONE;
1033 mMutex.lock_shared();
1034 if (auto writer = getWriter(display)) {
1035 writer->get().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
1036 translate<AidlRect>(frame));
1037 } else {
1038 error = Error::BAD_DISPLAY;
1039 }
1040 mMutex.unlock_shared();
1041 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001042}
1043
1044Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001045 Error error = Error::NONE;
1046 mMutex.lock_shared();
1047 if (auto writer = getWriter(display)) {
1048 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
1049 alpha);
1050 } else {
1051 error = Error::BAD_DISPLAY;
1052 }
1053 mMutex.unlock_shared();
1054 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001055}
1056
1057Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
1058 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001059 Error error = Error::NONE;
1060 mMutex.lock_shared();
1061 if (auto writer = getWriter(display)) {
1062 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
1063 stream);
1064 } else {
1065 error = Error::BAD_DISPLAY;
1066 }
1067 mMutex.unlock_shared();
1068 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001069}
1070
1071Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
1072 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001073 Error error = Error::NONE;
1074 mMutex.lock_shared();
1075 if (auto writer = getWriter(display)) {
1076 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
1077 translate<AidlFRect>(crop));
1078 } else {
1079 error = Error::BAD_DISPLAY;
1080 }
1081 mMutex.unlock_shared();
1082 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001083}
1084
1085Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001086 Error error = Error::NONE;
1087 mMutex.lock_shared();
1088 if (auto writer = getWriter(display)) {
1089 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
1090 translate<AidlTransform>(transform));
1091 } else {
1092 error = Error::BAD_DISPLAY;
1093 }
1094 mMutex.unlock_shared();
1095 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001096}
1097
1098Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
1099 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001100 Error error = Error::NONE;
1101 mMutex.lock_shared();
1102 if (auto writer = getWriter(display)) {
1103 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
1104 translate<AidlRect>(visible));
1105 } else {
1106 error = Error::BAD_DISPLAY;
1107 }
1108 mMutex.unlock_shared();
1109 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001110}
1111
1112Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001113 Error error = Error::NONE;
1114 mMutex.lock_shared();
1115 if (auto writer = getWriter(display)) {
1116 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
1117 } else {
1118 error = Error::BAD_DISPLAY;
1119 }
1120 mMutex.unlock_shared();
1121 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001122}
1123
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001124Error AidlComposer::execute(Display display) {
1125 auto writer = getWriter(display);
1126 auto reader = getReader(display);
1127 if (!writer || !reader) {
1128 return Error::BAD_DISPLAY;
1129 }
1130
Huihong Luoe7382c12023-04-21 20:24:32 +00001131 auto commands = writer->get().takePendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -08001132 if (commands.empty()) {
Ady Abrahame7385f72021-09-05 00:54:25 -07001133 return Error::NONE;
1134 }
1135
Ady Abrahamde792782021-12-20 10:00:49 -08001136 { // scope for results
1137 std::vector<CommandResultPayload> results;
1138 auto status = mAidlComposerClient->executeCommands(commands, &results);
1139 if (!status.isOk()) {
1140 ALOGE("executeCommands failed %s", status.getDescription().c_str());
1141 return static_cast<Error>(status.getServiceSpecificError());
1142 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001143
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001144 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -08001145 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001146 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -08001147 Error error = Error::NONE;
1148 for (const auto& cmdErr : commandErrors) {
1149 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1150 if (index < 0 || index >= commands.size()) {
1151 ALOGE("invalid command index %zu", index);
1152 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001153 }
1154
Ady Abrahama6388c02021-11-11 21:11:51 -08001155 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001156 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1157 error = translate<Error>(cmdErr.errorCode);
1158 } else {
1159 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1160 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001161 }
1162 }
1163
Ady Abrahame7385f72021-09-05 00:54:25 -07001164 return error;
1165}
1166
1167Error AidlComposer::setLayerPerFrameMetadata(
1168 Display display, Layer layer,
1169 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001170 Error error = Error::NONE;
1171 mMutex.lock_shared();
1172 if (auto writer = getWriter(display)) {
1173 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1174 translate<int64_t>(layer),
1175 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1176 } else {
1177 error = Error::BAD_DISPLAY;
1178 }
1179 mMutex.unlock_shared();
1180 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001181}
1182
1183std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1184 Display display) {
1185 std::vector<AidlPerFrameMetadataKey> keys;
1186 const auto status =
1187 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1188 if (!status.isOk()) {
1189 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1190 return {};
1191 }
1192 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1193}
1194
1195Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1196 std::vector<RenderIntent>* outRenderIntents) {
1197 std::vector<AidlRenderIntent> renderIntents;
1198 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1199 translate<AidlColorMode>(colorMode),
1200 &renderIntents);
1201 if (!status.isOk()) {
1202 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1203 return static_cast<Error>(status.getServiceSpecificError());
1204 }
1205 *outRenderIntents = translate<RenderIntent>(renderIntents);
1206 return Error::NONE;
1207}
1208
1209Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1210 std::vector<float> matrix;
1211 const auto status =
1212 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1213 &matrix);
1214 if (!status.isOk()) {
1215 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1216 return static_cast<Error>(status.getServiceSpecificError());
1217 }
1218 *outMatrix = makeMat4(matrix);
1219 return Error::NONE;
1220}
1221
1222Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1223 std::vector<uint8_t>* outData) {
1224 AidlDisplayIdentification displayIdentification;
1225 const auto status =
1226 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1227 &displayIdentification);
1228 if (!status.isOk()) {
1229 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1230 return static_cast<Error>(status.getServiceSpecificError());
1231 }
1232
1233 *outPort = static_cast<uint8_t>(displayIdentification.port);
1234 *outData = displayIdentification.data;
1235
1236 return Error::NONE;
1237}
1238
1239Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001240 Error error = Error::NONE;
1241 mMutex.lock_shared();
1242 if (auto writer = getWriter(display)) {
1243 writer->get().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1244 matrix);
1245 } else {
1246 error = Error::BAD_DISPLAY;
1247 }
1248 mMutex.unlock_shared();
1249 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001250}
1251
1252Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1253 Dataspace* outDataspace,
1254 uint8_t* outComponentMask) {
1255 if (!outFormat || !outDataspace || !outComponentMask) {
1256 return Error::BAD_PARAMETER;
1257 }
1258
1259 AidlDisplayContentSamplingAttributes attributes;
1260 const auto status =
1261 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1262 &attributes);
1263 if (!status.isOk()) {
1264 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1265 return static_cast<Error>(status.getServiceSpecificError());
1266 }
1267
1268 *outFormat = translate<PixelFormat>(attributes.format);
1269 *outDataspace = translate<Dataspace>(attributes.dataspace);
1270 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1271 return Error::NONE;
1272}
1273
1274Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1275 uint8_t componentMask, uint64_t maxFrames) {
1276 const auto status =
1277 mAidlComposerClient
1278 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1279 static_cast<AidlFormatColorComponent>(
1280 componentMask),
1281 static_cast<int64_t>(maxFrames));
1282 if (!status.isOk()) {
1283 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1284 return static_cast<Error>(status.getServiceSpecificError());
1285 }
1286 return Error::NONE;
1287}
1288
1289Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1290 uint64_t timestamp, DisplayedFrameStats* outStats) {
1291 if (!outStats) {
1292 return Error::BAD_PARAMETER;
1293 }
1294
1295 AidlDisplayContentSample sample;
1296 const auto status =
1297 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1298 static_cast<int64_t>(maxFrames),
1299 static_cast<int64_t>(timestamp),
1300 &sample);
1301 if (!status.isOk()) {
1302 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1303 return static_cast<Error>(status.getServiceSpecificError());
1304 }
1305 *outStats = translate<DisplayedFrameStats>(sample);
1306 return Error::NONE;
1307}
1308
1309Error AidlComposer::setLayerPerFrameMetadataBlobs(
1310 Display display, Layer layer,
1311 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001312 Error error = Error::NONE;
1313 mMutex.lock_shared();
1314 if (auto writer = getWriter(display)) {
1315 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1316 translate<int64_t>(layer),
1317 translate<AidlPerFrameMetadataBlob>(metadata));
1318 } else {
1319 error = Error::BAD_DISPLAY;
1320 }
1321 mMutex.unlock_shared();
1322 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001323}
1324
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001325Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001326 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001327 Error error = Error::NONE;
1328 mMutex.lock_shared();
1329 if (auto writer = getWriter(display)) {
1330 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001331
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001332 if (options.applyImmediately) {
1333 error = execute(display);
1334 mMutex.unlock_shared();
1335 return error;
1336 }
1337 } else {
1338 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001339 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001340 mMutex.unlock_shared();
1341 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001342}
1343
1344Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001345 std::vector<AidlDisplayCapability>* outCapabilities) {
1346 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1347 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001348 if (!status.isOk()) {
1349 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001350 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001351 return static_cast<Error>(status.getServiceSpecificError());
1352 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001353 return Error::NONE;
1354}
1355
1356V2_4::Error AidlComposer::getDisplayConnectionType(
1357 Display display, IComposerClient::DisplayConnectionType* outType) {
1358 AidlDisplayConnectionType type;
1359 const auto status =
1360 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1361 if (!status.isOk()) {
1362 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1363 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1364 }
1365 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1366 return V2_4::Error::NONE;
1367}
1368
1369V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1370 int32_t vsyncPeriod;
1371 const auto status =
1372 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1373 if (!status.isOk()) {
1374 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1375 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1376 }
1377 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1378 return V2_4::Error::NONE;
1379}
1380
1381V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1382 Display display, Config config,
1383 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1384 VsyncPeriodChangeTimeline* outTimeline) {
1385 AidlVsyncPeriodChangeTimeline timeline;
1386 const auto status =
1387 mAidlComposerClient
1388 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1389 translate<int32_t>(config),
1390 translate<AidlVsyncPeriodChangeConstraints>(
1391 vsyncPeriodChangeConstraints),
1392 &timeline);
1393 if (!status.isOk()) {
1394 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1395 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1396 }
1397 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1398 return V2_4::Error::NONE;
1399}
1400
1401V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1402 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1403 if (!status.isOk()) {
1404 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1405 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1406 }
1407 return V2_4::Error::NONE;
1408}
1409
1410V2_4::Error AidlComposer::getSupportedContentTypes(
1411 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1412 std::vector<AidlContentType> types;
1413 const auto status =
1414 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1415 if (!status.isOk()) {
1416 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1417 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1418 }
1419 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1420 return V2_4::Error::NONE;
1421}
1422
1423V2_4::Error AidlComposer::setContentType(Display display,
1424 IComposerClient::ContentType contentType) {
1425 const auto status =
1426 mAidlComposerClient->setContentType(translate<int64_t>(display),
1427 translate<AidlContentType>(contentType));
1428 if (!status.isOk()) {
1429 ALOGE("setContentType failed %s", status.getDescription().c_str());
1430 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1431 }
1432 return V2_4::Error::NONE;
1433}
1434
Ady Abraham3f976752021-12-20 16:17:50 -08001435V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1436 const std::vector<uint8_t>&) {
1437 // There are no users for this API. See b/209691612.
1438 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001439}
1440
1441V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001442 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
1443 // There are no users for this API. See b/209691612.
1444 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001445}
1446
Kriti Dang7defaf32021-11-15 11:55:43 +01001447Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1448 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1449 translate<int32_t>(config));
1450 if (!status.isOk()) {
1451 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1452 return static_cast<Error>(status.getServiceSpecificError());
1453 }
1454 return Error::NONE;
1455}
1456
1457Error AidlComposer::clearBootDisplayConfig(Display display) {
1458 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1459 if (!status.isOk()) {
1460 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1461 return static_cast<Error>(status.getServiceSpecificError());
1462 }
1463 return Error::NONE;
1464}
1465
1466Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1467 int32_t displayConfig;
1468 const auto status =
1469 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1470 &displayConfig);
1471 if (!status.isOk()) {
1472 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1473 return static_cast<Error>(status.getServiceSpecificError());
1474 }
1475 *config = translate<uint32_t>(displayConfig);
1476 return Error::NONE;
1477}
1478
Kriti Dang674b9372022-11-18 10:58:44 +01001479Error AidlComposer::getHdrConversionCapabilities(
1480 std::vector<AidlHdrConversionCapability>* hdrConversionCapabilities) {
1481 const auto status =
1482 mAidlComposerClient->getHdrConversionCapabilities(hdrConversionCapabilities);
1483 if (!status.isOk()) {
1484 hdrConversionCapabilities = {};
1485 ALOGE("getHdrConversionCapabilities failed %s", status.getDescription().c_str());
1486 return static_cast<Error>(status.getServiceSpecificError());
1487 }
1488 return Error::NONE;
1489}
1490
Kriti Dangd432bb52023-02-09 18:21:04 +01001491Error AidlComposer::setHdrConversionStrategy(AidlHdrConversionStrategy hdrConversionStrategy,
1492 Hdr* outPreferredHdrOutputType) {
1493 const auto status = mAidlComposerClient->setHdrConversionStrategy(hdrConversionStrategy,
1494 outPreferredHdrOutputType);
Kriti Dang674b9372022-11-18 10:58:44 +01001495 if (!status.isOk()) {
1496 ALOGE("setHdrConversionStrategy failed %s", status.getDescription().c_str());
1497 return static_cast<Error>(status.getServiceSpecificError());
1498 }
1499 return Error::NONE;
1500}
1501
ramindanib2158ee2023-02-13 20:29:59 -08001502Error AidlComposer::setRefreshRateChangedCallbackDebugEnabled(Display displayId, bool enabled) {
1503 const auto status =
1504 mAidlComposerClient->setRefreshRateChangedCallbackDebugEnabled(translate<int64_t>(
1505 displayId),
1506 enabled);
1507 if (!status.isOk()) {
1508 ALOGE("setRefreshRateChangedCallbackDebugEnabled failed %s",
1509 status.getDescription().c_str());
1510 return static_cast<Error>(status.getServiceSpecificError());
1511 }
1512 return Error::NONE;
1513}
1514
ramindani3acaaf52023-09-25 10:31:27 -07001515Error AidlComposer::notifyExpectedPresent(Display displayId, nsecs_t expectedPresentTime,
1516 int32_t frameIntervalNs) {
1517 const auto status =
1518 mAidlComposerClient->notifyExpectedPresent(translate<int64_t>(displayId),
1519 ClockMonotonicTimestamp{expectedPresentTime},
1520 frameIntervalNs);
1521
1522 if (!status.isOk()) {
1523 ALOGE("notifyExpectedPresent failed %s", status.getDescription().c_str());
1524 return static_cast<Error>(status.getServiceSpecificError());
1525 }
1526 return Error::NONE;
1527}
1528
Ady Abrahame7385f72021-09-05 00:54:25 -07001529Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001530 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001531 Error error = Error::NONE;
1532 mMutex.lock_shared();
1533 if (auto reader = getReader(display)) {
1534 *outClientTargetProperty =
1535 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1536 } else {
1537 error = Error::BAD_DISPLAY;
1538 }
1539 mMutex.unlock_shared();
1540 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001541}
1542
Sally Qi492cec32024-06-28 14:34:47 -07001543Error AidlComposer::getDisplayLuts(Display display, std::vector<Lut>* outLuts) {
1544 Error error = Error::NONE;
1545 mMutex.lock_shared();
1546 if (auto reader = getReader(display)) {
1547 *outLuts = reader->get().takeDisplayLuts(translate<int64_t>(display));
1548 } else {
1549 error = Error::BAD_DISPLAY;
1550 }
1551 mMutex.unlock_shared();
1552 return error;
1553}
1554
Alec Mouri6da0e272022-02-07 12:45:57 -08001555Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001556 Error error = Error::NONE;
1557 mMutex.lock_shared();
1558 if (auto writer = getWriter(display)) {
1559 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1560 brightness);
1561 } else {
1562 error = Error::BAD_DISPLAY;
1563 }
1564 mMutex.unlock_shared();
1565 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001566}
1567
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001568Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1569 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001570 Error error = Error::NONE;
1571 mMutex.lock_shared();
1572 if (auto writer = getWriter(display)) {
1573 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1574 translate<AidlRect>(blocking));
1575 } else {
1576 error = Error::BAD_DISPLAY;
1577 }
1578 mMutex.unlock_shared();
1579 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001580}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001581
1582Error AidlComposer::getDisplayDecorationSupport(Display display,
1583 std::optional<DisplayDecorationSupport>* support) {
1584 const auto status =
1585 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1586 if (!status.isOk()) {
1587 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1588 support->reset();
1589 return static_cast<Error>(status.getServiceSpecificError());
1590 }
1591 return Error::NONE;
1592}
ramindani32cf0602022-03-02 02:30:29 +00001593
1594Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1595 const auto status =
1596 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1597 translate<int32_t>(timeout.count()));
1598 if (!status.isOk()) {
1599 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1600 return static_cast<Error>(status.getServiceSpecificError());
1601 }
1602 return Error::NONE;
1603}
1604
ramindani06e518e2022-03-14 18:47:53 +00001605Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1606 AidlTransform* outDisplayOrientation) {
1607 const auto status =
1608 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1609 outDisplayOrientation);
1610 if (!status.isOk()) {
1611 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1612 return static_cast<Error>(status.getServiceSpecificError());
1613 }
1614 return Error::NONE;
1615}
1616
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001617ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1618 REQUIRES_SHARED(mMutex) {
1619 return mWriters.get(display);
1620}
1621
1622ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1623 REQUIRES_SHARED(mMutex) {
1624 if (mSingleReader) {
1625 display = translate<Display>(kSingleReaderKey);
1626 }
1627 return mReaders.get(display);
1628}
1629
1630void AidlComposer::removeDisplay(Display display) {
1631 mMutex.lock();
1632 bool wasErased = mWriters.erase(display);
1633 ALOGW_IF(!wasErased,
1634 "Attempting to remove writer for display %" PRId64 " which is not connected",
1635 translate<int64_t>(display));
1636 if (!mSingleReader) {
1637 removeReader(display);
1638 }
1639 mMutex.unlock();
1640}
1641
1642void AidlComposer::onHotplugDisconnect(Display display) {
1643 removeDisplay(display);
1644}
1645
1646bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
Leon Scroggins IIIdb635042023-10-20 09:58:17 -04001647 if (!FlagManager::getInstance().multithreaded_present()) return false;
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001648 const auto displayId = translate<int64_t>(display);
1649 std::vector<AidlDisplayCapability> capabilities;
1650 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1651 if (!status.isOk()) {
1652 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1653 return false;
1654 }
1655 return std::find(capabilities.begin(), capabilities.end(),
1656 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
1657}
1658
1659void AidlComposer::addReader(Display display) {
1660 const auto displayId = translate<int64_t>(display);
1661 std::optional<int64_t> displayOpt;
1662 if (displayId != kSingleReaderKey) {
1663 displayOpt.emplace(displayId);
1664 }
1665 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1666 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1667 displayId);
1668}
1669
1670void AidlComposer::removeReader(Display display) {
1671 bool wasErased = mReaders.erase(display);
1672 ALOGW_IF(!wasErased,
1673 "Attempting to remove reader for display %" PRId64 " which is not connected",
1674 translate<int64_t>(display));
1675}
1676
1677void AidlComposer::addDisplay(Display display) {
1678 const auto displayId = translate<int64_t>(display);
1679 mMutex.lock();
1680 auto [it, added] = mWriters.try_emplace(display, displayId);
1681 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1682 displayId);
1683 if (mSingleReader) {
1684 if (hasMultiThreadedPresentSupport(display)) {
1685 mSingleReader = false;
1686 removeReader(translate<Display>(kSingleReaderKey));
1687 // Note that this includes the new display.
1688 for (const auto& [existingDisplay, _] : mWriters) {
1689 addReader(existingDisplay);
1690 }
1691 }
1692 } else {
1693 addReader(display);
1694 }
1695 mMutex.unlock();
1696}
1697
1698void AidlComposer::onHotplugConnect(Display display) {
1699 addDisplay(display);
1700}
Ady Abrahame7385f72021-09-05 00:54:25 -07001701} // namespace Hwc2
1702} // namespace android