blob: 6250b1155ef870109adb4675c4dbdd1bf1df4113 [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>
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -050028#include <gui/TraceUtils.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070029#include <log/log.h>
30#include <utils/Trace.h>
31
32#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
33
34#include <algorithm>
35#include <cinttypes>
36
Yichi Chen3401b562022-01-17 15:42:35 +080037#include "HWC2.h"
38
Ady Abrahame7385f72021-09-05 00:54:25 -070039namespace android {
40
41using hardware::hidl_handle;
42using hardware::hidl_vec;
43using hardware::Return;
44
45using aidl::android::hardware::graphics::composer3::BnComposerCallback;
46using aidl::android::hardware::graphics::composer3::Capability;
Alec Mouri85065692022-03-18 00:58:26 +000047using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
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]);
333 return str;
Ady Abrahame7385f72021-09-05 00:54:25 -0700334}
335
Yichi Chen3401b562022-01-17 15:42:35 +0800336void AidlComposer::registerCallback(HWC2::ComposerCallback& callback) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700337 if (mAidlComposerCallback) {
338 ALOGE("Callback already registered");
339 }
Yichi Chen3401b562022-01-17 15:42:35 +0800340
Ady Abraham9fc28052021-10-14 17:21:38 -0700341 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
shuide chenf1111e12024-01-03 18:41:38 +0800342
343 ndk::SpAIBinder binder = mAidlComposerCallback->asBinder();
344 AIBinder_setMinSchedulerPolicy(binder.get(), SCHED_FIFO, 2);
Ady Abrahame7385f72021-09-05 00:54:25 -0700345
346 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
347 if (!status.isOk()) {
348 ALOGE("registerCallback failed %s", status.getDescription().c_str());
349 }
350}
351
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400352Error AidlComposer::executeCommands(Display display) {
353 mMutex.lock_shared();
354 auto error = execute(display);
355 mMutex.unlock_shared();
356 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700357}
358
359uint32_t AidlComposer::getMaxVirtualDisplayCount() {
360 int32_t count = 0;
361 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
362 if (!status.isOk()) {
363 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
364 return 0;
365 }
366 return static_cast<uint32_t>(count);
367}
368
369Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
370 Display* outDisplay) {
371 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
372 const int32_t bufferSlotCount = 1;
373 VirtualDisplay virtualDisplay;
374 const auto status =
375 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
376 static_cast<int32_t>(height),
377 static_cast<AidlPixelFormat>(*format),
378 bufferSlotCount, &virtualDisplay);
379
380 if (!status.isOk()) {
381 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
382 return static_cast<Error>(status.getServiceSpecificError());
383 }
384
385 *outDisplay = translate<Display>(virtualDisplay.display);
386 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400387 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700388 return Error::NONE;
389}
390
391Error AidlComposer::destroyVirtualDisplay(Display display) {
392 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
393 if (!status.isOk()) {
394 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
395 return static_cast<Error>(status.getServiceSpecificError());
396 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400397 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700398 return Error::NONE;
399}
400
401Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400402 Error error = Error::NONE;
403 mMutex.lock_shared();
404 if (auto writer = getWriter(display)) {
405 writer->get().acceptDisplayChanges(translate<int64_t>(display));
406 } else {
407 error = Error::BAD_DISPLAY;
408 }
409 mMutex.unlock_shared();
410 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700411}
412
413Error AidlComposer::createLayer(Display display, Layer* outLayer) {
414 int64_t layer;
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000415 Error error = Error::NONE;
416 if (!mEnableLayerCommandBatchingFlag) {
417 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
418 kMaxLayerBufferCount, &layer);
419 if (!status.isOk()) {
420 ALOGE("createLayer failed %s", status.getDescription().c_str());
421 return static_cast<Error>(status.getServiceSpecificError());
422 }
423 } else {
424 // generate a unique layerID. map in AidlComposer with <SF_layerID, HWC_layerID>
425 // Add this as a new displayCommand in execute command.
426 // return the SF generated layerID instead of calling HWC
427 layer = mLayerID++;
428 mMutex.lock_shared();
429 if (auto writer = getWriter(display)) {
430 writer->get().setLayerLifecycleBatchCommandType(translate<int64_t>(display),
431 translate<int64_t>(layer),
432 LayerLifecycleBatchCommandType::CREATE);
433 writer->get().setNewBufferSlotCount(translate<int64_t>(display),
434 translate<int64_t>(layer), kMaxLayerBufferCount);
435 } else {
436 error = Error::BAD_DISPLAY;
437 }
438 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700439 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700440 *outLayer = translate<Layer>(layer);
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000441 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700442}
443
444Error AidlComposer::destroyLayer(Display display, Layer layer) {
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000445 Error error = Error::NONE;
446 if (!mEnableLayerCommandBatchingFlag) {
447 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
448 translate<int64_t>(layer));
449 if (!status.isOk()) {
450 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
451 return static_cast<Error>(status.getServiceSpecificError());
452 }
453 } else {
454 mMutex.lock_shared();
455 if (auto writer = getWriter(display)) {
456 writer->get()
457 .setLayerLifecycleBatchCommandType(translate<int64_t>(display),
458 translate<int64_t>(layer),
459 LayerLifecycleBatchCommandType::DESTROY);
460 } else {
461 error = Error::BAD_DISPLAY;
462 }
463 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700464 }
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000465
466 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700467}
468
469Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
470 int32_t config;
471 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
472 if (!status.isOk()) {
473 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
474 return static_cast<Error>(status.getServiceSpecificError());
475 }
476 *outConfig = translate<Config>(config);
477 return Error::NONE;
478}
479
480Error AidlComposer::getChangedCompositionTypes(
481 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500482 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400483 std::vector<ChangedCompositionLayer> changedLayers;
484 Error error = Error::NONE;
485 {
486 mMutex.lock_shared();
487 if (auto reader = getReader(display)) {
488 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
489 } else {
490 error = Error::BAD_DISPLAY;
491 }
492 mMutex.unlock_shared();
493 }
Ady Abrahamde792782021-12-20 10:00:49 -0800494 outLayers->reserve(changedLayers.size());
495 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800496
Ady Abrahamde792782021-12-20 10:00:49 -0800497 for (const auto& layer : changedLayers) {
498 outLayers->emplace_back(translate<Layer>(layer.layer));
499 outTypes->emplace_back(layer.composition);
500 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400501 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700502}
503
504Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
505 std::vector<AidlColorMode> modes;
506 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
507 if (!status.isOk()) {
508 ALOGE("getColorModes failed %s", status.getDescription().c_str());
509 return static_cast<Error>(status.getServiceSpecificError());
510 }
511 *outModes = translate<ColorMode>(modes);
512 return Error::NONE;
513}
514
515Error AidlComposer::getDisplayAttribute(Display display, Config config,
516 IComposerClient::Attribute attribute, int32_t* outValue) {
517 const auto status =
518 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
519 translate<int32_t>(config),
520 static_cast<AidlDisplayAttribute>(attribute),
521 outValue);
522 if (!status.isOk()) {
523 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
524 return static_cast<Error>(status.getServiceSpecificError());
525 }
526 return Error::NONE;
527}
528
529Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
530 std::vector<int32_t> configs;
531 const auto status =
532 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
533 if (!status.isOk()) {
534 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
535 return static_cast<Error>(status.getServiceSpecificError());
536 }
537 *outConfigs = translate<Config>(configs);
538 return Error::NONE;
539}
540
ramindani263a3f12023-07-18 20:44:49 -0700541Error AidlComposer::getDisplayConfigurations(Display display, int32_t maxFrameIntervalNs,
ramindani0cd1d8d2023-06-13 13:43:23 -0700542 std::vector<DisplayConfiguration>* outConfigs) {
543 const auto status =
ramindani263a3f12023-07-18 20:44:49 -0700544 mAidlComposerClient->getDisplayConfigurations(translate<int64_t>(display),
545 maxFrameIntervalNs, outConfigs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700546 if (!status.isOk()) {
547 ALOGE("getDisplayConfigurations failed %s", status.getDescription().c_str());
548 return static_cast<Error>(status.getServiceSpecificError());
549 }
550
551 return Error::NONE;
552}
553
Ady Abrahame7385f72021-09-05 00:54:25 -0700554Error AidlComposer::getDisplayName(Display display, std::string* outName) {
555 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
556 if (!status.isOk()) {
557 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
558 return static_cast<Error>(status.getServiceSpecificError());
559 }
560 return Error::NONE;
561}
562
563Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
564 std::vector<Layer>* outLayers,
565 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400566 Error error = Error::NONE;
567 DisplayRequest displayRequests;
568 {
569 mMutex.lock_shared();
570 if (auto reader = getReader(display)) {
571 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
572 } else {
573 error = Error::BAD_DISPLAY;
574 }
575 mMutex.unlock_shared();
576 }
Ady Abrahamde792782021-12-20 10:00:49 -0800577 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
578 outLayers->reserve(displayRequests.layerRequests.size());
579 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
580
581 for (const auto& layer : displayRequests.layerRequests) {
582 outLayers->emplace_back(translate<Layer>(layer.layer));
583 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
584 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400585 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700586}
587
588Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800589 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700590 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800591 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700592 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800593 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700594 return static_cast<Error>(status.getServiceSpecificError());
595 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800596 *outSupport = std::find(capabilities.begin(), capabilities.end(),
597 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700598 return Error::NONE;
599}
600
ramindani32cf0602022-03-02 02:30:29 +0000601Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
602 std::vector<AidlDisplayCapability> capabilities;
603 const auto status =
604 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
605 if (!status.isOk()) {
606 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
607 return static_cast<Error>(status.getServiceSpecificError());
608 }
609 *outSupport = std::find(capabilities.begin(), capabilities.end(),
610 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
611 return Error::NONE;
612}
613
Ady Abrahame7385f72021-09-05 00:54:25 -0700614Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
615 float* outMaxLuminance, float* outMaxAverageLuminance,
616 float* outMinLuminance) {
617 AidlHdrCapabilities capabilities;
618 const auto status =
619 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
620 if (!status.isOk()) {
621 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
622 return static_cast<Error>(status.getServiceSpecificError());
623 }
624
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100625 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700626 *outMaxLuminance = capabilities.maxLuminance;
627 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
628 *outMinLuminance = capabilities.minLuminance;
629 return Error::NONE;
630}
631
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000632bool AidlComposer::getLayerLifecycleBatchCommand() {
633 std::vector<Capability> capabilities = getCapabilities();
634 bool hasCapability = std::find(capabilities.begin(), capabilities.end(),
635 Capability::LAYER_LIFECYCLE_BATCH_COMMAND) != capabilities.end();
636 return hasCapability;
637}
638
Sally Qibb866c12022-10-17 11:31:20 -0700639Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
640 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
641 if (!status.isOk()) {
642 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
643 return static_cast<Error>(status.getServiceSpecificError());
644 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700645 return Error::NONE;
646}
647
Ady Abrahame7385f72021-09-05 00:54:25 -0700648Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
649 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400650 Error error = Error::NONE;
651 std::vector<ReleaseFences::Layer> fences;
652 {
653 mMutex.lock_shared();
654 if (auto reader = getReader(display)) {
655 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
656 } else {
657 error = Error::BAD_DISPLAY;
658 }
659 mMutex.unlock_shared();
660 }
Ady Abrahamde792782021-12-20 10:00:49 -0800661 outLayers->reserve(fences.size());
662 outReleaseFences->reserve(fences.size());
663
664 for (auto& fence : fences) {
665 outLayers->emplace_back(translate<Layer>(fence.layer));
666 // take ownership
667 const int fenceOwner = fence.fence.get();
668 *fence.fence.getR() = -1;
669 outReleaseFences->emplace_back(fenceOwner);
670 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400671 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700672}
673
674Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500675 const auto displayId = translate<int64_t>(display);
676 ATRACE_FORMAT("HwcPresentDisplay %" PRId64, displayId);
677
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400678 Error error = Error::NONE;
679 mMutex.lock_shared();
680 auto writer = getWriter(display);
681 auto reader = getReader(display);
682 if (writer && reader) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500683 writer->get().presentDisplay(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400684 error = execute(display);
685 } else {
686 error = Error::BAD_DISPLAY;
687 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700688
Ady Abrahame7385f72021-09-05 00:54:25 -0700689 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400690 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700691 return error;
692 }
693
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500694 auto fence = reader->get().takePresentFence(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400695 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800696 // take ownership
697 *outPresentFence = fence.get();
698 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700699 return Error::NONE;
700}
701
702Error AidlComposer::setActiveConfig(Display display, Config config) {
703 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
704 translate<int32_t>(config));
705 if (!status.isOk()) {
706 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
707 return static_cast<Error>(status.getServiceSpecificError());
708 }
709 return Error::NONE;
710}
711
712Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
713 int acquireFence, Dataspace dataspace,
Alec Mourif97df4d2023-09-06 02:10:05 +0000714 const std::vector<IComposerClient::Rect>& damage,
715 float hdrSdrRatio) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700716 const native_handle_t* handle = nullptr;
717 if (target.get()) {
718 handle = target->getNativeBuffer()->handle;
719 }
720
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400721 Error error = Error::NONE;
722 mMutex.lock_shared();
723 if (auto writer = getWriter(display)) {
724 writer->get()
725 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
726 translate<aidl::android::hardware::graphics::common::Dataspace>(
727 dataspace),
Alec Mourif97df4d2023-09-06 02:10:05 +0000728 translate<AidlRect>(damage), hdrSdrRatio);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400729 } else {
730 error = Error::BAD_DISPLAY;
731 }
732 mMutex.unlock_shared();
733 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700734}
735
736Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
737 const auto status =
738 mAidlComposerClient->setColorMode(translate<int64_t>(display),
739 translate<AidlColorMode>(mode),
740 translate<AidlRenderIntent>(renderIntent));
741 if (!status.isOk()) {
742 ALOGE("setColorMode failed %s", status.getDescription().c_str());
743 return static_cast<Error>(status.getServiceSpecificError());
744 }
745 return Error::NONE;
746}
747
Ady Abrahamdc011a92021-12-21 14:06:44 -0800748Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400749 auto error = Error::NONE;
750 mMutex.lock_shared();
751 if (auto writer = getWriter(display)) {
752 writer->get().setColorTransform(translate<int64_t>(display), matrix);
753 } else {
754 error = Error::BAD_DISPLAY;
755 }
756 mMutex.unlock_shared();
757 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700758}
759
760Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
761 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400762 auto error = Error::NONE;
763 mMutex.lock_shared();
764 if (auto writer = getWriter(display)) {
765 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
766 } else {
767 error = Error::BAD_DISPLAY;
768 }
769 mMutex.unlock_shared();
770 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700771}
772
773Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
774 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
775 translate<PowerMode>(mode));
776 if (!status.isOk()) {
777 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
778 return static_cast<Error>(status.getServiceSpecificError());
779 }
780 return Error::NONE;
781}
782
783Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
784 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
785 const auto status =
786 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
787 if (!status.isOk()) {
788 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
789 return static_cast<Error>(status.getServiceSpecificError());
790 }
791 return Error::NONE;
792}
793
794Error AidlComposer::setClientTargetSlotCount(Display display) {
795 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
796 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
797 bufferSlotCount);
798 if (!status.isOk()) {
799 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
800 return static_cast<Error>(status.getServiceSpecificError());
801 }
802 return Error::NONE;
803}
804
Ady Abraham43065bd2021-12-10 17:22:15 -0800805Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani09acbb82023-11-03 09:02:38 -0700806 int32_t frameIntervalNs, uint32_t* outNumTypes,
807 uint32_t* outNumRequests) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400808 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500809 ATRACE_FORMAT("HwcValidateDisplay %" PRId64, displayId);
810
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400811 Error error = Error::NONE;
812 mMutex.lock_shared();
813 auto writer = getWriter(display);
814 auto reader = getReader(display);
815 if (writer && reader) {
ramindani09acbb82023-11-03 09:02:38 -0700816 writer->get().validateDisplay(displayId, ClockMonotonicTimestamp{expectedPresentTime},
817 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400818 error = execute(display);
819 } else {
820 error = Error::BAD_DISPLAY;
821 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700822
Ady Abrahame7385f72021-09-05 00:54:25 -0700823 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400824 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700825 return error;
826 }
827
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400828 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700829
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400830 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700831 return Error::NONE;
832}
833
Ady Abraham43065bd2021-12-10 17:22:15 -0800834Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700835 int32_t frameIntervalNs, uint32_t* outNumTypes,
836 uint32_t* outNumRequests, int* outPresentFence,
837 uint32_t* state) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400838 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500839 ATRACE_FORMAT("HwcPresentOrValidateDisplay %" PRId64, displayId);
840
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400841 Error error = Error::NONE;
842 mMutex.lock_shared();
843 auto writer = getWriter(display);
844 auto reader = getReader(display);
845 if (writer && reader) {
846 writer->get().presentOrvalidateDisplay(displayId,
ramindani4aac32c2023-10-30 14:13:30 -0700847 ClockMonotonicTimestamp{expectedPresentTime},
848 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400849 error = execute(display);
850 } else {
851 error = Error::BAD_DISPLAY;
852 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700853
Ady Abrahame7385f72021-09-05 00:54:25 -0700854 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400855 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700856 return error;
857 }
858
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400859 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800860 if (!result.has_value()) {
861 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400862 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800863 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700864 }
865
Ady Abrahamde792782021-12-20 10:00:49 -0800866 *state = translate<uint32_t>(*result);
867
868 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400869 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800870 // take ownership
871 *outPresentFence = fence.get();
872 *fence.getR() = -1;
873 }
874
875 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400876 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700877 }
878
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400879 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700880 return Error::NONE;
881}
882
883Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400884 Error error = Error::NONE;
885 mMutex.lock_shared();
886 if (auto writer = getWriter(display)) {
887 writer->get().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
888 x, y);
889 } else {
890 error = Error::BAD_DISPLAY;
891 }
892 mMutex.unlock_shared();
893 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700894}
895
896Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
897 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700898 const native_handle_t* handle = nullptr;
899 if (buffer.get()) {
900 handle = buffer->getNativeBuffer()->handle;
901 }
902
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400903 Error error = Error::NONE;
904 mMutex.lock_shared();
905 if (auto writer = getWriter(display)) {
906 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
907 handle, acquireFence);
908 } else {
909 error = Error::BAD_DISPLAY;
910 }
911 mMutex.unlock_shared();
912 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700913}
914
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700915Error AidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
916 const std::vector<uint32_t>& slotsToClear,
917 uint32_t activeBufferSlot) {
918 if (slotsToClear.empty()) {
919 return Error::NONE;
920 }
921
Brian Lindahl90553da2022-12-06 13:36:30 -0700922 Error error = Error::NONE;
923 mMutex.lock_shared();
924 if (auto writer = getWriter(display)) {
ramindani0cd1d8d2023-06-13 13:43:23 -0700925 if (mComposerInterfaceVersion > 1) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700926 writer->get().setLayerBufferSlotsToClear(translate<int64_t>(display),
927 translate<int64_t>(layer), slotsToClear);
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600928 // Backwards compatible way of clearing buffer slots is to set the layer buffer with a
929 // placeholder buffer, using the slot that needs to cleared... tricky.
930 } else if (mClearSlotBuffer != nullptr) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700931 for (uint32_t slot : slotsToClear) {
932 // Don't clear the active buffer slot because we need to restore the active buffer
933 // after clearing the requested buffer slots with a placeholder buffer.
934 if (slot != activeBufferSlot) {
935 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
936 translate<int64_t>(layer), slot,
937 mClearSlotBuffer->handle,
938 /*fence*/ -1);
939 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700940 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700941 // Since we clear buffers by setting them to a placeholder buffer, we want to make
942 // sure that the last setLayerBuffer command is sent with the currently active
943 // buffer, not the placeholder buffer, so that there is no perceptual change when
944 // buffers are discarded.
945 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
946 translate<int64_t>(layer), activeBufferSlot,
947 // The active buffer is still cached in
948 // its slot and doesn't need a fence.
949 /*buffer*/ nullptr, /*fence*/ -1);
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700950 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700951 } else {
952 error = Error::BAD_DISPLAY;
953 }
954 mMutex.unlock_shared();
955 return error;
956}
957
Ady Abrahame7385f72021-09-05 00:54:25 -0700958Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
959 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400960 Error error = Error::NONE;
961 mMutex.lock_shared();
962 if (auto writer = getWriter(display)) {
963 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
964 translate<AidlRect>(damage));
965 } else {
966 error = Error::BAD_DISPLAY;
967 }
968 mMutex.unlock_shared();
969 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700970}
971
972Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
973 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400974 Error error = Error::NONE;
975 mMutex.lock_shared();
976 if (auto writer = getWriter(display)) {
977 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
978 translate<BlendMode>(mode));
979 } else {
980 error = Error::BAD_DISPLAY;
981 }
982 mMutex.unlock_shared();
983 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700984}
985
Ady Abraham6e60b142022-01-06 18:10:35 -0800986Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400987 Error error = Error::NONE;
988 mMutex.lock_shared();
989 if (auto writer = getWriter(display)) {
990 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
991 } else {
992 error = Error::BAD_DISPLAY;
993 }
994 mMutex.unlock_shared();
995 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700996}
997
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500998Error AidlComposer::setLayerCompositionType(
999 Display display, Layer layer,
1000 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001001 Error error = Error::NONE;
1002 mMutex.lock_shared();
1003 if (auto writer = getWriter(display)) {
1004 writer->get().setLayerCompositionType(translate<int64_t>(display),
1005 translate<int64_t>(layer), type);
1006 } else {
1007 error = Error::BAD_DISPLAY;
1008 }
1009 mMutex.unlock_shared();
1010 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001011}
1012
1013Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001014 Error error = Error::NONE;
1015 mMutex.lock_shared();
1016 if (auto writer = getWriter(display)) {
1017 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
1018 translate<AidlDataspace>(dataspace));
1019 } else {
1020 error = Error::BAD_DISPLAY;
1021 }
1022 mMutex.unlock_shared();
1023 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001024}
1025
1026Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
1027 const IComposerClient::Rect& frame) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001028 Error error = Error::NONE;
1029 mMutex.lock_shared();
1030 if (auto writer = getWriter(display)) {
1031 writer->get().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
1032 translate<AidlRect>(frame));
1033 } else {
1034 error = Error::BAD_DISPLAY;
1035 }
1036 mMutex.unlock_shared();
1037 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001038}
1039
1040Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001041 Error error = Error::NONE;
1042 mMutex.lock_shared();
1043 if (auto writer = getWriter(display)) {
1044 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
1045 alpha);
1046 } else {
1047 error = Error::BAD_DISPLAY;
1048 }
1049 mMutex.unlock_shared();
1050 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001051}
1052
1053Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
1054 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001055 Error error = Error::NONE;
1056 mMutex.lock_shared();
1057 if (auto writer = getWriter(display)) {
1058 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
1059 stream);
1060 } else {
1061 error = Error::BAD_DISPLAY;
1062 }
1063 mMutex.unlock_shared();
1064 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001065}
1066
1067Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
1068 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001069 Error error = Error::NONE;
1070 mMutex.lock_shared();
1071 if (auto writer = getWriter(display)) {
1072 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
1073 translate<AidlFRect>(crop));
1074 } else {
1075 error = Error::BAD_DISPLAY;
1076 }
1077 mMutex.unlock_shared();
1078 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001079}
1080
1081Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001082 Error error = Error::NONE;
1083 mMutex.lock_shared();
1084 if (auto writer = getWriter(display)) {
1085 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
1086 translate<AidlTransform>(transform));
1087 } else {
1088 error = Error::BAD_DISPLAY;
1089 }
1090 mMutex.unlock_shared();
1091 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001092}
1093
1094Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
1095 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001096 Error error = Error::NONE;
1097 mMutex.lock_shared();
1098 if (auto writer = getWriter(display)) {
1099 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
1100 translate<AidlRect>(visible));
1101 } else {
1102 error = Error::BAD_DISPLAY;
1103 }
1104 mMutex.unlock_shared();
1105 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001106}
1107
1108Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001109 Error error = Error::NONE;
1110 mMutex.lock_shared();
1111 if (auto writer = getWriter(display)) {
1112 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
1113 } else {
1114 error = Error::BAD_DISPLAY;
1115 }
1116 mMutex.unlock_shared();
1117 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001118}
1119
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001120Error AidlComposer::execute(Display display) {
1121 auto writer = getWriter(display);
1122 auto reader = getReader(display);
1123 if (!writer || !reader) {
1124 return Error::BAD_DISPLAY;
1125 }
1126
Huihong Luoe7382c12023-04-21 20:24:32 +00001127 auto commands = writer->get().takePendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -08001128 if (commands.empty()) {
Ady Abrahame7385f72021-09-05 00:54:25 -07001129 return Error::NONE;
1130 }
1131
Ady Abrahamde792782021-12-20 10:00:49 -08001132 { // scope for results
1133 std::vector<CommandResultPayload> results;
1134 auto status = mAidlComposerClient->executeCommands(commands, &results);
1135 if (!status.isOk()) {
1136 ALOGE("executeCommands failed %s", status.getDescription().c_str());
1137 return static_cast<Error>(status.getServiceSpecificError());
1138 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001139
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001140 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -08001141 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001142 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -08001143 Error error = Error::NONE;
1144 for (const auto& cmdErr : commandErrors) {
1145 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1146 if (index < 0 || index >= commands.size()) {
1147 ALOGE("invalid command index %zu", index);
1148 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001149 }
1150
Ady Abrahama6388c02021-11-11 21:11:51 -08001151 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001152 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1153 error = translate<Error>(cmdErr.errorCode);
1154 } else {
1155 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1156 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001157 }
1158 }
1159
Ady Abrahame7385f72021-09-05 00:54:25 -07001160 return error;
1161}
1162
1163Error AidlComposer::setLayerPerFrameMetadata(
1164 Display display, Layer layer,
1165 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001166 Error error = Error::NONE;
1167 mMutex.lock_shared();
1168 if (auto writer = getWriter(display)) {
1169 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1170 translate<int64_t>(layer),
1171 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1172 } else {
1173 error = Error::BAD_DISPLAY;
1174 }
1175 mMutex.unlock_shared();
1176 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001177}
1178
1179std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1180 Display display) {
1181 std::vector<AidlPerFrameMetadataKey> keys;
1182 const auto status =
1183 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1184 if (!status.isOk()) {
1185 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1186 return {};
1187 }
1188 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1189}
1190
1191Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1192 std::vector<RenderIntent>* outRenderIntents) {
1193 std::vector<AidlRenderIntent> renderIntents;
1194 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1195 translate<AidlColorMode>(colorMode),
1196 &renderIntents);
1197 if (!status.isOk()) {
1198 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1199 return static_cast<Error>(status.getServiceSpecificError());
1200 }
1201 *outRenderIntents = translate<RenderIntent>(renderIntents);
1202 return Error::NONE;
1203}
1204
1205Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1206 std::vector<float> matrix;
1207 const auto status =
1208 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1209 &matrix);
1210 if (!status.isOk()) {
1211 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1212 return static_cast<Error>(status.getServiceSpecificError());
1213 }
1214 *outMatrix = makeMat4(matrix);
1215 return Error::NONE;
1216}
1217
1218Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1219 std::vector<uint8_t>* outData) {
1220 AidlDisplayIdentification displayIdentification;
1221 const auto status =
1222 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1223 &displayIdentification);
1224 if (!status.isOk()) {
1225 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1226 return static_cast<Error>(status.getServiceSpecificError());
1227 }
1228
1229 *outPort = static_cast<uint8_t>(displayIdentification.port);
1230 *outData = displayIdentification.data;
1231
1232 return Error::NONE;
1233}
1234
1235Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001236 Error error = Error::NONE;
1237 mMutex.lock_shared();
1238 if (auto writer = getWriter(display)) {
1239 writer->get().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1240 matrix);
1241 } else {
1242 error = Error::BAD_DISPLAY;
1243 }
1244 mMutex.unlock_shared();
1245 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001246}
1247
1248Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1249 Dataspace* outDataspace,
1250 uint8_t* outComponentMask) {
1251 if (!outFormat || !outDataspace || !outComponentMask) {
1252 return Error::BAD_PARAMETER;
1253 }
1254
1255 AidlDisplayContentSamplingAttributes attributes;
1256 const auto status =
1257 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1258 &attributes);
1259 if (!status.isOk()) {
1260 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1261 return static_cast<Error>(status.getServiceSpecificError());
1262 }
1263
1264 *outFormat = translate<PixelFormat>(attributes.format);
1265 *outDataspace = translate<Dataspace>(attributes.dataspace);
1266 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1267 return Error::NONE;
1268}
1269
1270Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1271 uint8_t componentMask, uint64_t maxFrames) {
1272 const auto status =
1273 mAidlComposerClient
1274 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1275 static_cast<AidlFormatColorComponent>(
1276 componentMask),
1277 static_cast<int64_t>(maxFrames));
1278 if (!status.isOk()) {
1279 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1280 return static_cast<Error>(status.getServiceSpecificError());
1281 }
1282 return Error::NONE;
1283}
1284
1285Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1286 uint64_t timestamp, DisplayedFrameStats* outStats) {
1287 if (!outStats) {
1288 return Error::BAD_PARAMETER;
1289 }
1290
1291 AidlDisplayContentSample sample;
1292 const auto status =
1293 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1294 static_cast<int64_t>(maxFrames),
1295 static_cast<int64_t>(timestamp),
1296 &sample);
1297 if (!status.isOk()) {
1298 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1299 return static_cast<Error>(status.getServiceSpecificError());
1300 }
1301 *outStats = translate<DisplayedFrameStats>(sample);
1302 return Error::NONE;
1303}
1304
1305Error AidlComposer::setLayerPerFrameMetadataBlobs(
1306 Display display, Layer layer,
1307 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001308 Error error = Error::NONE;
1309 mMutex.lock_shared();
1310 if (auto writer = getWriter(display)) {
1311 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1312 translate<int64_t>(layer),
1313 translate<AidlPerFrameMetadataBlob>(metadata));
1314 } else {
1315 error = Error::BAD_DISPLAY;
1316 }
1317 mMutex.unlock_shared();
1318 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001319}
1320
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001321Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001322 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001323 Error error = Error::NONE;
1324 mMutex.lock_shared();
1325 if (auto writer = getWriter(display)) {
1326 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001327
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001328 if (options.applyImmediately) {
1329 error = execute(display);
1330 mMutex.unlock_shared();
1331 return error;
1332 }
1333 } else {
1334 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001335 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001336 mMutex.unlock_shared();
1337 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001338}
1339
1340Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001341 std::vector<AidlDisplayCapability>* outCapabilities) {
1342 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1343 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001344 if (!status.isOk()) {
1345 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001346 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001347 return static_cast<Error>(status.getServiceSpecificError());
1348 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001349 return Error::NONE;
1350}
1351
1352V2_4::Error AidlComposer::getDisplayConnectionType(
1353 Display display, IComposerClient::DisplayConnectionType* outType) {
1354 AidlDisplayConnectionType type;
1355 const auto status =
1356 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1357 if (!status.isOk()) {
1358 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1359 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1360 }
1361 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1362 return V2_4::Error::NONE;
1363}
1364
1365V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1366 int32_t vsyncPeriod;
1367 const auto status =
1368 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1369 if (!status.isOk()) {
1370 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1371 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1372 }
1373 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1374 return V2_4::Error::NONE;
1375}
1376
1377V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1378 Display display, Config config,
1379 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1380 VsyncPeriodChangeTimeline* outTimeline) {
1381 AidlVsyncPeriodChangeTimeline timeline;
1382 const auto status =
1383 mAidlComposerClient
1384 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1385 translate<int32_t>(config),
1386 translate<AidlVsyncPeriodChangeConstraints>(
1387 vsyncPeriodChangeConstraints),
1388 &timeline);
1389 if (!status.isOk()) {
1390 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1391 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1392 }
1393 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1394 return V2_4::Error::NONE;
1395}
1396
1397V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1398 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1399 if (!status.isOk()) {
1400 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1401 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1402 }
1403 return V2_4::Error::NONE;
1404}
1405
1406V2_4::Error AidlComposer::getSupportedContentTypes(
1407 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1408 std::vector<AidlContentType> types;
1409 const auto status =
1410 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1411 if (!status.isOk()) {
1412 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1413 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1414 }
1415 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1416 return V2_4::Error::NONE;
1417}
1418
1419V2_4::Error AidlComposer::setContentType(Display display,
1420 IComposerClient::ContentType contentType) {
1421 const auto status =
1422 mAidlComposerClient->setContentType(translate<int64_t>(display),
1423 translate<AidlContentType>(contentType));
1424 if (!status.isOk()) {
1425 ALOGE("setContentType failed %s", status.getDescription().c_str());
1426 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1427 }
1428 return V2_4::Error::NONE;
1429}
1430
Ady Abraham3f976752021-12-20 16:17:50 -08001431V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1432 const std::vector<uint8_t>&) {
1433 // There are no users for this API. See b/209691612.
1434 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001435}
1436
1437V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001438 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
1439 // There are no users for this API. See b/209691612.
1440 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001441}
1442
Kriti Dang7defaf32021-11-15 11:55:43 +01001443Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1444 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1445 translate<int32_t>(config));
1446 if (!status.isOk()) {
1447 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1448 return static_cast<Error>(status.getServiceSpecificError());
1449 }
1450 return Error::NONE;
1451}
1452
1453Error AidlComposer::clearBootDisplayConfig(Display display) {
1454 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1455 if (!status.isOk()) {
1456 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1457 return static_cast<Error>(status.getServiceSpecificError());
1458 }
1459 return Error::NONE;
1460}
1461
1462Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1463 int32_t displayConfig;
1464 const auto status =
1465 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1466 &displayConfig);
1467 if (!status.isOk()) {
1468 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1469 return static_cast<Error>(status.getServiceSpecificError());
1470 }
1471 *config = translate<uint32_t>(displayConfig);
1472 return Error::NONE;
1473}
1474
Kriti Dang674b9372022-11-18 10:58:44 +01001475Error AidlComposer::getHdrConversionCapabilities(
1476 std::vector<AidlHdrConversionCapability>* hdrConversionCapabilities) {
1477 const auto status =
1478 mAidlComposerClient->getHdrConversionCapabilities(hdrConversionCapabilities);
1479 if (!status.isOk()) {
1480 hdrConversionCapabilities = {};
1481 ALOGE("getHdrConversionCapabilities failed %s", status.getDescription().c_str());
1482 return static_cast<Error>(status.getServiceSpecificError());
1483 }
1484 return Error::NONE;
1485}
1486
Kriti Dangd432bb52023-02-09 18:21:04 +01001487Error AidlComposer::setHdrConversionStrategy(AidlHdrConversionStrategy hdrConversionStrategy,
1488 Hdr* outPreferredHdrOutputType) {
1489 const auto status = mAidlComposerClient->setHdrConversionStrategy(hdrConversionStrategy,
1490 outPreferredHdrOutputType);
Kriti Dang674b9372022-11-18 10:58:44 +01001491 if (!status.isOk()) {
1492 ALOGE("setHdrConversionStrategy failed %s", status.getDescription().c_str());
1493 return static_cast<Error>(status.getServiceSpecificError());
1494 }
1495 return Error::NONE;
1496}
1497
ramindanib2158ee2023-02-13 20:29:59 -08001498Error AidlComposer::setRefreshRateChangedCallbackDebugEnabled(Display displayId, bool enabled) {
1499 const auto status =
1500 mAidlComposerClient->setRefreshRateChangedCallbackDebugEnabled(translate<int64_t>(
1501 displayId),
1502 enabled);
1503 if (!status.isOk()) {
1504 ALOGE("setRefreshRateChangedCallbackDebugEnabled failed %s",
1505 status.getDescription().c_str());
1506 return static_cast<Error>(status.getServiceSpecificError());
1507 }
1508 return Error::NONE;
1509}
1510
ramindani3acaaf52023-09-25 10:31:27 -07001511Error AidlComposer::notifyExpectedPresent(Display displayId, nsecs_t expectedPresentTime,
1512 int32_t frameIntervalNs) {
1513 const auto status =
1514 mAidlComposerClient->notifyExpectedPresent(translate<int64_t>(displayId),
1515 ClockMonotonicTimestamp{expectedPresentTime},
1516 frameIntervalNs);
1517
1518 if (!status.isOk()) {
1519 ALOGE("notifyExpectedPresent failed %s", status.getDescription().c_str());
1520 return static_cast<Error>(status.getServiceSpecificError());
1521 }
1522 return Error::NONE;
1523}
1524
Ady Abrahame7385f72021-09-05 00:54:25 -07001525Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001526 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001527 Error error = Error::NONE;
1528 mMutex.lock_shared();
1529 if (auto reader = getReader(display)) {
1530 *outClientTargetProperty =
1531 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1532 } else {
1533 error = Error::BAD_DISPLAY;
1534 }
1535 mMutex.unlock_shared();
1536 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001537}
1538
Alec Mouri6da0e272022-02-07 12:45:57 -08001539Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001540 Error error = Error::NONE;
1541 mMutex.lock_shared();
1542 if (auto writer = getWriter(display)) {
1543 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1544 brightness);
1545 } else {
1546 error = Error::BAD_DISPLAY;
1547 }
1548 mMutex.unlock_shared();
1549 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001550}
1551
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001552Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1553 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001554 Error error = Error::NONE;
1555 mMutex.lock_shared();
1556 if (auto writer = getWriter(display)) {
1557 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1558 translate<AidlRect>(blocking));
1559 } else {
1560 error = Error::BAD_DISPLAY;
1561 }
1562 mMutex.unlock_shared();
1563 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001564}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001565
1566Error AidlComposer::getDisplayDecorationSupport(Display display,
1567 std::optional<DisplayDecorationSupport>* support) {
1568 const auto status =
1569 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1570 if (!status.isOk()) {
1571 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1572 support->reset();
1573 return static_cast<Error>(status.getServiceSpecificError());
1574 }
1575 return Error::NONE;
1576}
ramindani32cf0602022-03-02 02:30:29 +00001577
1578Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1579 const auto status =
1580 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1581 translate<int32_t>(timeout.count()));
1582 if (!status.isOk()) {
1583 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1584 return static_cast<Error>(status.getServiceSpecificError());
1585 }
1586 return Error::NONE;
1587}
1588
ramindani06e518e2022-03-14 18:47:53 +00001589Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1590 AidlTransform* outDisplayOrientation) {
1591 const auto status =
1592 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1593 outDisplayOrientation);
1594 if (!status.isOk()) {
1595 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1596 return static_cast<Error>(status.getServiceSpecificError());
1597 }
1598 return Error::NONE;
1599}
1600
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001601ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1602 REQUIRES_SHARED(mMutex) {
1603 return mWriters.get(display);
1604}
1605
1606ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1607 REQUIRES_SHARED(mMutex) {
1608 if (mSingleReader) {
1609 display = translate<Display>(kSingleReaderKey);
1610 }
1611 return mReaders.get(display);
1612}
1613
1614void AidlComposer::removeDisplay(Display display) {
1615 mMutex.lock();
1616 bool wasErased = mWriters.erase(display);
1617 ALOGW_IF(!wasErased,
1618 "Attempting to remove writer for display %" PRId64 " which is not connected",
1619 translate<int64_t>(display));
1620 if (!mSingleReader) {
1621 removeReader(display);
1622 }
1623 mMutex.unlock();
1624}
1625
1626void AidlComposer::onHotplugDisconnect(Display display) {
1627 removeDisplay(display);
1628}
1629
1630bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
Leon Scroggins IIIdb635042023-10-20 09:58:17 -04001631 if (!FlagManager::getInstance().multithreaded_present()) return false;
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001632 const auto displayId = translate<int64_t>(display);
1633 std::vector<AidlDisplayCapability> capabilities;
1634 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1635 if (!status.isOk()) {
1636 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1637 return false;
1638 }
1639 return std::find(capabilities.begin(), capabilities.end(),
1640 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
1641}
1642
1643void AidlComposer::addReader(Display display) {
1644 const auto displayId = translate<int64_t>(display);
1645 std::optional<int64_t> displayOpt;
1646 if (displayId != kSingleReaderKey) {
1647 displayOpt.emplace(displayId);
1648 }
1649 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1650 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1651 displayId);
1652}
1653
1654void AidlComposer::removeReader(Display display) {
1655 bool wasErased = mReaders.erase(display);
1656 ALOGW_IF(!wasErased,
1657 "Attempting to remove reader for display %" PRId64 " which is not connected",
1658 translate<int64_t>(display));
1659}
1660
1661void AidlComposer::addDisplay(Display display) {
1662 const auto displayId = translate<int64_t>(display);
1663 mMutex.lock();
1664 auto [it, added] = mWriters.try_emplace(display, displayId);
1665 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1666 displayId);
1667 if (mSingleReader) {
1668 if (hasMultiThreadedPresentSupport(display)) {
1669 mSingleReader = false;
1670 removeReader(translate<Display>(kSingleReaderKey));
1671 // Note that this includes the new display.
1672 for (const auto& [existingDisplay, _] : mWriters) {
1673 addReader(existingDisplay);
1674 }
1675 }
1676 } else {
1677 addReader(display);
1678 }
1679 mMutex.unlock();
1680}
1681
1682void AidlComposer::onHotplugConnect(Display display) {
1683 addDisplay(display);
1684}
Ady Abrahame7385f72021-09-05 00:54:25 -07001685} // namespace Hwc2
1686} // namespace android