blob: f7bfbce65bcc42d269adb0225a0b956f939ee923 [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
ramindani0cd1d8d2023-06-13 13:43:23 -0700294bool AidlComposer::getDisplayConfigurationsSupported() 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);
Ady Abrahame7385f72021-09-05 00:54:25 -0700342 AIBinder_setMinSchedulerPolicy(mAidlComposerCallback->asBinder().get(), SCHED_FIFO, 2);
343
344 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
345 if (!status.isOk()) {
346 ALOGE("registerCallback failed %s", status.getDescription().c_str());
347 }
348}
349
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400350Error AidlComposer::executeCommands(Display display) {
351 mMutex.lock_shared();
352 auto error = execute(display);
353 mMutex.unlock_shared();
354 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700355}
356
357uint32_t AidlComposer::getMaxVirtualDisplayCount() {
358 int32_t count = 0;
359 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
360 if (!status.isOk()) {
361 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
362 return 0;
363 }
364 return static_cast<uint32_t>(count);
365}
366
367Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
368 Display* outDisplay) {
369 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
370 const int32_t bufferSlotCount = 1;
371 VirtualDisplay virtualDisplay;
372 const auto status =
373 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
374 static_cast<int32_t>(height),
375 static_cast<AidlPixelFormat>(*format),
376 bufferSlotCount, &virtualDisplay);
377
378 if (!status.isOk()) {
379 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
380 return static_cast<Error>(status.getServiceSpecificError());
381 }
382
383 *outDisplay = translate<Display>(virtualDisplay.display);
384 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400385 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700386 return Error::NONE;
387}
388
389Error AidlComposer::destroyVirtualDisplay(Display display) {
390 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
391 if (!status.isOk()) {
392 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
393 return static_cast<Error>(status.getServiceSpecificError());
394 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400395 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700396 return Error::NONE;
397}
398
399Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400400 Error error = Error::NONE;
401 mMutex.lock_shared();
402 if (auto writer = getWriter(display)) {
403 writer->get().acceptDisplayChanges(translate<int64_t>(display));
404 } else {
405 error = Error::BAD_DISPLAY;
406 }
407 mMutex.unlock_shared();
408 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700409}
410
411Error AidlComposer::createLayer(Display display, Layer* outLayer) {
412 int64_t layer;
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000413 Error error = Error::NONE;
414 if (!mEnableLayerCommandBatchingFlag) {
415 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
416 kMaxLayerBufferCount, &layer);
417 if (!status.isOk()) {
418 ALOGE("createLayer failed %s", status.getDescription().c_str());
419 return static_cast<Error>(status.getServiceSpecificError());
420 }
421 } else {
422 // generate a unique layerID. map in AidlComposer with <SF_layerID, HWC_layerID>
423 // Add this as a new displayCommand in execute command.
424 // return the SF generated layerID instead of calling HWC
425 layer = mLayerID++;
426 mMutex.lock_shared();
427 if (auto writer = getWriter(display)) {
428 writer->get().setLayerLifecycleBatchCommandType(translate<int64_t>(display),
429 translate<int64_t>(layer),
430 LayerLifecycleBatchCommandType::CREATE);
431 writer->get().setNewBufferSlotCount(translate<int64_t>(display),
432 translate<int64_t>(layer), kMaxLayerBufferCount);
433 } else {
434 error = Error::BAD_DISPLAY;
435 }
436 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700437 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700438 *outLayer = translate<Layer>(layer);
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000439 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700440}
441
442Error AidlComposer::destroyLayer(Display display, Layer layer) {
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000443 Error error = Error::NONE;
444 if (!mEnableLayerCommandBatchingFlag) {
445 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
446 translate<int64_t>(layer));
447 if (!status.isOk()) {
448 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
449 return static_cast<Error>(status.getServiceSpecificError());
450 }
451 } else {
452 mMutex.lock_shared();
453 if (auto writer = getWriter(display)) {
454 writer->get()
455 .setLayerLifecycleBatchCommandType(translate<int64_t>(display),
456 translate<int64_t>(layer),
457 LayerLifecycleBatchCommandType::DESTROY);
458 } else {
459 error = Error::BAD_DISPLAY;
460 }
461 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700462 }
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000463
464 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700465}
466
467Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
468 int32_t config;
469 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
470 if (!status.isOk()) {
471 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
472 return static_cast<Error>(status.getServiceSpecificError());
473 }
474 *outConfig = translate<Config>(config);
475 return Error::NONE;
476}
477
478Error AidlComposer::getChangedCompositionTypes(
479 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500480 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400481 std::vector<ChangedCompositionLayer> changedLayers;
482 Error error = Error::NONE;
483 {
484 mMutex.lock_shared();
485 if (auto reader = getReader(display)) {
486 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
487 } else {
488 error = Error::BAD_DISPLAY;
489 }
490 mMutex.unlock_shared();
491 }
Ady Abrahamde792782021-12-20 10:00:49 -0800492 outLayers->reserve(changedLayers.size());
493 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800494
Ady Abrahamde792782021-12-20 10:00:49 -0800495 for (const auto& layer : changedLayers) {
496 outLayers->emplace_back(translate<Layer>(layer.layer));
497 outTypes->emplace_back(layer.composition);
498 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400499 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700500}
501
502Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
503 std::vector<AidlColorMode> modes;
504 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
505 if (!status.isOk()) {
506 ALOGE("getColorModes failed %s", status.getDescription().c_str());
507 return static_cast<Error>(status.getServiceSpecificError());
508 }
509 *outModes = translate<ColorMode>(modes);
510 return Error::NONE;
511}
512
513Error AidlComposer::getDisplayAttribute(Display display, Config config,
514 IComposerClient::Attribute attribute, int32_t* outValue) {
515 const auto status =
516 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
517 translate<int32_t>(config),
518 static_cast<AidlDisplayAttribute>(attribute),
519 outValue);
520 if (!status.isOk()) {
521 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
522 return static_cast<Error>(status.getServiceSpecificError());
523 }
524 return Error::NONE;
525}
526
527Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
528 std::vector<int32_t> configs;
529 const auto status =
530 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
531 if (!status.isOk()) {
532 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
533 return static_cast<Error>(status.getServiceSpecificError());
534 }
535 *outConfigs = translate<Config>(configs);
536 return Error::NONE;
537}
538
ramindani263a3f12023-07-18 20:44:49 -0700539Error AidlComposer::getDisplayConfigurations(Display display, int32_t maxFrameIntervalNs,
ramindani0cd1d8d2023-06-13 13:43:23 -0700540 std::vector<DisplayConfiguration>* outConfigs) {
541 const auto status =
ramindani263a3f12023-07-18 20:44:49 -0700542 mAidlComposerClient->getDisplayConfigurations(translate<int64_t>(display),
543 maxFrameIntervalNs, outConfigs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700544 if (!status.isOk()) {
545 ALOGE("getDisplayConfigurations failed %s", status.getDescription().c_str());
546 return static_cast<Error>(status.getServiceSpecificError());
547 }
548
549 return Error::NONE;
550}
551
Ady Abrahame7385f72021-09-05 00:54:25 -0700552Error AidlComposer::getDisplayName(Display display, std::string* outName) {
553 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
554 if (!status.isOk()) {
555 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
556 return static_cast<Error>(status.getServiceSpecificError());
557 }
558 return Error::NONE;
559}
560
561Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
562 std::vector<Layer>* outLayers,
563 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400564 Error error = Error::NONE;
565 DisplayRequest displayRequests;
566 {
567 mMutex.lock_shared();
568 if (auto reader = getReader(display)) {
569 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
570 } else {
571 error = Error::BAD_DISPLAY;
572 }
573 mMutex.unlock_shared();
574 }
Ady Abrahamde792782021-12-20 10:00:49 -0800575 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
576 outLayers->reserve(displayRequests.layerRequests.size());
577 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
578
579 for (const auto& layer : displayRequests.layerRequests) {
580 outLayers->emplace_back(translate<Layer>(layer.layer));
581 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
582 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400583 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700584}
585
586Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800587 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700588 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800589 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700590 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800591 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700592 return static_cast<Error>(status.getServiceSpecificError());
593 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800594 *outSupport = std::find(capabilities.begin(), capabilities.end(),
595 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700596 return Error::NONE;
597}
598
ramindani32cf0602022-03-02 02:30:29 +0000599Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
600 std::vector<AidlDisplayCapability> capabilities;
601 const auto status =
602 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
603 if (!status.isOk()) {
604 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
605 return static_cast<Error>(status.getServiceSpecificError());
606 }
607 *outSupport = std::find(capabilities.begin(), capabilities.end(),
608 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
609 return Error::NONE;
610}
611
Ady Abrahame7385f72021-09-05 00:54:25 -0700612Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
613 float* outMaxLuminance, float* outMaxAverageLuminance,
614 float* outMinLuminance) {
615 AidlHdrCapabilities capabilities;
616 const auto status =
617 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
618 if (!status.isOk()) {
619 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
620 return static_cast<Error>(status.getServiceSpecificError());
621 }
622
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100623 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700624 *outMaxLuminance = capabilities.maxLuminance;
625 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
626 *outMinLuminance = capabilities.minLuminance;
627 return Error::NONE;
628}
629
Manali Bhutiyani96f866c2023-11-09 18:09:44 +0000630bool AidlComposer::getLayerLifecycleBatchCommand() {
631 std::vector<Capability> capabilities = getCapabilities();
632 bool hasCapability = std::find(capabilities.begin(), capabilities.end(),
633 Capability::LAYER_LIFECYCLE_BATCH_COMMAND) != capabilities.end();
634 return hasCapability;
635}
636
Sally Qibb866c12022-10-17 11:31:20 -0700637Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
638 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
639 if (!status.isOk()) {
640 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
641 return static_cast<Error>(status.getServiceSpecificError());
642 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700643 return Error::NONE;
644}
645
Ady Abrahame7385f72021-09-05 00:54:25 -0700646Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
647 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400648 Error error = Error::NONE;
649 std::vector<ReleaseFences::Layer> fences;
650 {
651 mMutex.lock_shared();
652 if (auto reader = getReader(display)) {
653 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
654 } else {
655 error = Error::BAD_DISPLAY;
656 }
657 mMutex.unlock_shared();
658 }
Ady Abrahamde792782021-12-20 10:00:49 -0800659 outLayers->reserve(fences.size());
660 outReleaseFences->reserve(fences.size());
661
662 for (auto& fence : fences) {
663 outLayers->emplace_back(translate<Layer>(fence.layer));
664 // take ownership
665 const int fenceOwner = fence.fence.get();
666 *fence.fence.getR() = -1;
667 outReleaseFences->emplace_back(fenceOwner);
668 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400669 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700670}
671
672Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500673 const auto displayId = translate<int64_t>(display);
674 ATRACE_FORMAT("HwcPresentDisplay %" PRId64, displayId);
675
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400676 Error error = Error::NONE;
677 mMutex.lock_shared();
678 auto writer = getWriter(display);
679 auto reader = getReader(display);
680 if (writer && reader) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500681 writer->get().presentDisplay(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400682 error = execute(display);
683 } else {
684 error = Error::BAD_DISPLAY;
685 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700686
Ady Abrahame7385f72021-09-05 00:54:25 -0700687 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400688 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700689 return error;
690 }
691
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500692 auto fence = reader->get().takePresentFence(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400693 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800694 // take ownership
695 *outPresentFence = fence.get();
696 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700697 return Error::NONE;
698}
699
700Error AidlComposer::setActiveConfig(Display display, Config config) {
701 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
702 translate<int32_t>(config));
703 if (!status.isOk()) {
704 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
705 return static_cast<Error>(status.getServiceSpecificError());
706 }
707 return Error::NONE;
708}
709
710Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
711 int acquireFence, Dataspace dataspace,
Alec Mourif97df4d2023-09-06 02:10:05 +0000712 const std::vector<IComposerClient::Rect>& damage,
713 float hdrSdrRatio) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700714 const native_handle_t* handle = nullptr;
715 if (target.get()) {
716 handle = target->getNativeBuffer()->handle;
717 }
718
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400719 Error error = Error::NONE;
720 mMutex.lock_shared();
721 if (auto writer = getWriter(display)) {
722 writer->get()
723 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
724 translate<aidl::android::hardware::graphics::common::Dataspace>(
725 dataspace),
Alec Mourif97df4d2023-09-06 02:10:05 +0000726 translate<AidlRect>(damage), hdrSdrRatio);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400727 } else {
728 error = Error::BAD_DISPLAY;
729 }
730 mMutex.unlock_shared();
731 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700732}
733
734Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
735 const auto status =
736 mAidlComposerClient->setColorMode(translate<int64_t>(display),
737 translate<AidlColorMode>(mode),
738 translate<AidlRenderIntent>(renderIntent));
739 if (!status.isOk()) {
740 ALOGE("setColorMode failed %s", status.getDescription().c_str());
741 return static_cast<Error>(status.getServiceSpecificError());
742 }
743 return Error::NONE;
744}
745
Ady Abrahamdc011a92021-12-21 14:06:44 -0800746Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400747 auto error = Error::NONE;
748 mMutex.lock_shared();
749 if (auto writer = getWriter(display)) {
750 writer->get().setColorTransform(translate<int64_t>(display), matrix);
751 } else {
752 error = Error::BAD_DISPLAY;
753 }
754 mMutex.unlock_shared();
755 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700756}
757
758Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
759 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400760 auto error = Error::NONE;
761 mMutex.lock_shared();
762 if (auto writer = getWriter(display)) {
763 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
764 } else {
765 error = Error::BAD_DISPLAY;
766 }
767 mMutex.unlock_shared();
768 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700769}
770
771Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
772 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
773 translate<PowerMode>(mode));
774 if (!status.isOk()) {
775 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
776 return static_cast<Error>(status.getServiceSpecificError());
777 }
778 return Error::NONE;
779}
780
781Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
782 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
783 const auto status =
784 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
785 if (!status.isOk()) {
786 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
787 return static_cast<Error>(status.getServiceSpecificError());
788 }
789 return Error::NONE;
790}
791
792Error AidlComposer::setClientTargetSlotCount(Display display) {
793 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
794 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
795 bufferSlotCount);
796 if (!status.isOk()) {
797 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
798 return static_cast<Error>(status.getServiceSpecificError());
799 }
800 return Error::NONE;
801}
802
Ady Abraham43065bd2021-12-10 17:22:15 -0800803Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani09acbb82023-11-03 09:02:38 -0700804 int32_t frameIntervalNs, uint32_t* outNumTypes,
805 uint32_t* outNumRequests) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400806 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500807 ATRACE_FORMAT("HwcValidateDisplay %" PRId64, displayId);
808
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400809 Error error = Error::NONE;
810 mMutex.lock_shared();
811 auto writer = getWriter(display);
812 auto reader = getReader(display);
813 if (writer && reader) {
ramindani09acbb82023-11-03 09:02:38 -0700814 writer->get().validateDisplay(displayId, ClockMonotonicTimestamp{expectedPresentTime},
815 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400816 error = execute(display);
817 } else {
818 error = Error::BAD_DISPLAY;
819 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700820
Ady Abrahame7385f72021-09-05 00:54:25 -0700821 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400822 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700823 return error;
824 }
825
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400826 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700827
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400828 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700829 return Error::NONE;
830}
831
Ady Abraham43065bd2021-12-10 17:22:15 -0800832Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700833 int32_t frameIntervalNs, uint32_t* outNumTypes,
834 uint32_t* outNumRequests, int* outPresentFence,
835 uint32_t* state) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400836 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500837 ATRACE_FORMAT("HwcPresentOrValidateDisplay %" PRId64, displayId);
838
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400839 Error error = Error::NONE;
840 mMutex.lock_shared();
841 auto writer = getWriter(display);
842 auto reader = getReader(display);
843 if (writer && reader) {
844 writer->get().presentOrvalidateDisplay(displayId,
ramindani4aac32c2023-10-30 14:13:30 -0700845 ClockMonotonicTimestamp{expectedPresentTime},
846 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400847 error = execute(display);
848 } else {
849 error = Error::BAD_DISPLAY;
850 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700851
Ady Abrahame7385f72021-09-05 00:54:25 -0700852 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400853 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700854 return error;
855 }
856
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400857 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800858 if (!result.has_value()) {
859 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400860 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800861 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700862 }
863
Ady Abrahamde792782021-12-20 10:00:49 -0800864 *state = translate<uint32_t>(*result);
865
866 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400867 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800868 // take ownership
869 *outPresentFence = fence.get();
870 *fence.getR() = -1;
871 }
872
873 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400874 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700875 }
876
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400877 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700878 return Error::NONE;
879}
880
881Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400882 Error error = Error::NONE;
883 mMutex.lock_shared();
884 if (auto writer = getWriter(display)) {
885 writer->get().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
886 x, y);
887 } else {
888 error = Error::BAD_DISPLAY;
889 }
890 mMutex.unlock_shared();
891 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700892}
893
894Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
895 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700896 const native_handle_t* handle = nullptr;
897 if (buffer.get()) {
898 handle = buffer->getNativeBuffer()->handle;
899 }
900
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400901 Error error = Error::NONE;
902 mMutex.lock_shared();
903 if (auto writer = getWriter(display)) {
904 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
905 handle, acquireFence);
906 } else {
907 error = Error::BAD_DISPLAY;
908 }
909 mMutex.unlock_shared();
910 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700911}
912
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700913Error AidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
914 const std::vector<uint32_t>& slotsToClear,
915 uint32_t activeBufferSlot) {
916 if (slotsToClear.empty()) {
917 return Error::NONE;
918 }
919
Brian Lindahl90553da2022-12-06 13:36:30 -0700920 Error error = Error::NONE;
921 mMutex.lock_shared();
922 if (auto writer = getWriter(display)) {
ramindani0cd1d8d2023-06-13 13:43:23 -0700923 if (mComposerInterfaceVersion > 1) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700924 writer->get().setLayerBufferSlotsToClear(translate<int64_t>(display),
925 translate<int64_t>(layer), slotsToClear);
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600926 // Backwards compatible way of clearing buffer slots is to set the layer buffer with a
927 // placeholder buffer, using the slot that needs to cleared... tricky.
928 } else if (mClearSlotBuffer != nullptr) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700929 for (uint32_t slot : slotsToClear) {
930 // Don't clear the active buffer slot because we need to restore the active buffer
931 // after clearing the requested buffer slots with a placeholder buffer.
932 if (slot != activeBufferSlot) {
933 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
934 translate<int64_t>(layer), slot,
935 mClearSlotBuffer->handle,
936 /*fence*/ -1);
937 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700938 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700939 // Since we clear buffers by setting them to a placeholder buffer, we want to make
940 // sure that the last setLayerBuffer command is sent with the currently active
941 // buffer, not the placeholder buffer, so that there is no perceptual change when
942 // buffers are discarded.
943 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
944 translate<int64_t>(layer), activeBufferSlot,
945 // The active buffer is still cached in
946 // its slot and doesn't need a fence.
947 /*buffer*/ nullptr, /*fence*/ -1);
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700948 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700949 } else {
950 error = Error::BAD_DISPLAY;
951 }
952 mMutex.unlock_shared();
953 return error;
954}
955
Ady Abrahame7385f72021-09-05 00:54:25 -0700956Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
957 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400958 Error error = Error::NONE;
959 mMutex.lock_shared();
960 if (auto writer = getWriter(display)) {
961 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
962 translate<AidlRect>(damage));
963 } else {
964 error = Error::BAD_DISPLAY;
965 }
966 mMutex.unlock_shared();
967 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700968}
969
970Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
971 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400972 Error error = Error::NONE;
973 mMutex.lock_shared();
974 if (auto writer = getWriter(display)) {
975 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
976 translate<BlendMode>(mode));
977 } else {
978 error = Error::BAD_DISPLAY;
979 }
980 mMutex.unlock_shared();
981 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700982}
983
Ady Abraham6e60b142022-01-06 18:10:35 -0800984Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400985 Error error = Error::NONE;
986 mMutex.lock_shared();
987 if (auto writer = getWriter(display)) {
988 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
989 } else {
990 error = Error::BAD_DISPLAY;
991 }
992 mMutex.unlock_shared();
993 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700994}
995
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500996Error AidlComposer::setLayerCompositionType(
997 Display display, Layer layer,
998 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400999 Error error = Error::NONE;
1000 mMutex.lock_shared();
1001 if (auto writer = getWriter(display)) {
1002 writer->get().setLayerCompositionType(translate<int64_t>(display),
1003 translate<int64_t>(layer), type);
1004 } else {
1005 error = Error::BAD_DISPLAY;
1006 }
1007 mMutex.unlock_shared();
1008 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001009}
1010
1011Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001012 Error error = Error::NONE;
1013 mMutex.lock_shared();
1014 if (auto writer = getWriter(display)) {
1015 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
1016 translate<AidlDataspace>(dataspace));
1017 } else {
1018 error = Error::BAD_DISPLAY;
1019 }
1020 mMutex.unlock_shared();
1021 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001022}
1023
1024Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
1025 const IComposerClient::Rect& frame) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001026 Error error = Error::NONE;
1027 mMutex.lock_shared();
1028 if (auto writer = getWriter(display)) {
1029 writer->get().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
1030 translate<AidlRect>(frame));
1031 } else {
1032 error = Error::BAD_DISPLAY;
1033 }
1034 mMutex.unlock_shared();
1035 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001036}
1037
1038Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001039 Error error = Error::NONE;
1040 mMutex.lock_shared();
1041 if (auto writer = getWriter(display)) {
1042 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
1043 alpha);
1044 } else {
1045 error = Error::BAD_DISPLAY;
1046 }
1047 mMutex.unlock_shared();
1048 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001049}
1050
1051Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
1052 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001053 Error error = Error::NONE;
1054 mMutex.lock_shared();
1055 if (auto writer = getWriter(display)) {
1056 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
1057 stream);
1058 } else {
1059 error = Error::BAD_DISPLAY;
1060 }
1061 mMutex.unlock_shared();
1062 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001063}
1064
1065Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
1066 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001067 Error error = Error::NONE;
1068 mMutex.lock_shared();
1069 if (auto writer = getWriter(display)) {
1070 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
1071 translate<AidlFRect>(crop));
1072 } else {
1073 error = Error::BAD_DISPLAY;
1074 }
1075 mMutex.unlock_shared();
1076 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001077}
1078
1079Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001080 Error error = Error::NONE;
1081 mMutex.lock_shared();
1082 if (auto writer = getWriter(display)) {
1083 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
1084 translate<AidlTransform>(transform));
1085 } else {
1086 error = Error::BAD_DISPLAY;
1087 }
1088 mMutex.unlock_shared();
1089 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001090}
1091
1092Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
1093 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001094 Error error = Error::NONE;
1095 mMutex.lock_shared();
1096 if (auto writer = getWriter(display)) {
1097 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
1098 translate<AidlRect>(visible));
1099 } else {
1100 error = Error::BAD_DISPLAY;
1101 }
1102 mMutex.unlock_shared();
1103 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001104}
1105
1106Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001107 Error error = Error::NONE;
1108 mMutex.lock_shared();
1109 if (auto writer = getWriter(display)) {
1110 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
1111 } else {
1112 error = Error::BAD_DISPLAY;
1113 }
1114 mMutex.unlock_shared();
1115 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001116}
1117
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001118Error AidlComposer::execute(Display display) {
1119 auto writer = getWriter(display);
1120 auto reader = getReader(display);
1121 if (!writer || !reader) {
1122 return Error::BAD_DISPLAY;
1123 }
1124
Huihong Luoe7382c12023-04-21 20:24:32 +00001125 auto commands = writer->get().takePendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -08001126 if (commands.empty()) {
Ady Abrahame7385f72021-09-05 00:54:25 -07001127 return Error::NONE;
1128 }
1129
Ady Abrahamde792782021-12-20 10:00:49 -08001130 { // scope for results
1131 std::vector<CommandResultPayload> results;
1132 auto status = mAidlComposerClient->executeCommands(commands, &results);
1133 if (!status.isOk()) {
1134 ALOGE("executeCommands failed %s", status.getDescription().c_str());
1135 return static_cast<Error>(status.getServiceSpecificError());
1136 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001137
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001138 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -08001139 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001140 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -08001141 Error error = Error::NONE;
1142 for (const auto& cmdErr : commandErrors) {
1143 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1144 if (index < 0 || index >= commands.size()) {
1145 ALOGE("invalid command index %zu", index);
1146 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001147 }
1148
Ady Abrahama6388c02021-11-11 21:11:51 -08001149 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001150 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1151 error = translate<Error>(cmdErr.errorCode);
1152 } else {
1153 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1154 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001155 }
1156 }
1157
Ady Abrahame7385f72021-09-05 00:54:25 -07001158 return error;
1159}
1160
1161Error AidlComposer::setLayerPerFrameMetadata(
1162 Display display, Layer layer,
1163 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001164 Error error = Error::NONE;
1165 mMutex.lock_shared();
1166 if (auto writer = getWriter(display)) {
1167 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1168 translate<int64_t>(layer),
1169 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1170 } else {
1171 error = Error::BAD_DISPLAY;
1172 }
1173 mMutex.unlock_shared();
1174 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001175}
1176
1177std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1178 Display display) {
1179 std::vector<AidlPerFrameMetadataKey> keys;
1180 const auto status =
1181 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1182 if (!status.isOk()) {
1183 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1184 return {};
1185 }
1186 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1187}
1188
1189Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1190 std::vector<RenderIntent>* outRenderIntents) {
1191 std::vector<AidlRenderIntent> renderIntents;
1192 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1193 translate<AidlColorMode>(colorMode),
1194 &renderIntents);
1195 if (!status.isOk()) {
1196 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1197 return static_cast<Error>(status.getServiceSpecificError());
1198 }
1199 *outRenderIntents = translate<RenderIntent>(renderIntents);
1200 return Error::NONE;
1201}
1202
1203Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1204 std::vector<float> matrix;
1205 const auto status =
1206 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1207 &matrix);
1208 if (!status.isOk()) {
1209 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1210 return static_cast<Error>(status.getServiceSpecificError());
1211 }
1212 *outMatrix = makeMat4(matrix);
1213 return Error::NONE;
1214}
1215
1216Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1217 std::vector<uint8_t>* outData) {
1218 AidlDisplayIdentification displayIdentification;
1219 const auto status =
1220 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1221 &displayIdentification);
1222 if (!status.isOk()) {
1223 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1224 return static_cast<Error>(status.getServiceSpecificError());
1225 }
1226
1227 *outPort = static_cast<uint8_t>(displayIdentification.port);
1228 *outData = displayIdentification.data;
1229
1230 return Error::NONE;
1231}
1232
1233Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001234 Error error = Error::NONE;
1235 mMutex.lock_shared();
1236 if (auto writer = getWriter(display)) {
1237 writer->get().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1238 matrix);
1239 } else {
1240 error = Error::BAD_DISPLAY;
1241 }
1242 mMutex.unlock_shared();
1243 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001244}
1245
1246Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1247 Dataspace* outDataspace,
1248 uint8_t* outComponentMask) {
1249 if (!outFormat || !outDataspace || !outComponentMask) {
1250 return Error::BAD_PARAMETER;
1251 }
1252
1253 AidlDisplayContentSamplingAttributes attributes;
1254 const auto status =
1255 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1256 &attributes);
1257 if (!status.isOk()) {
1258 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1259 return static_cast<Error>(status.getServiceSpecificError());
1260 }
1261
1262 *outFormat = translate<PixelFormat>(attributes.format);
1263 *outDataspace = translate<Dataspace>(attributes.dataspace);
1264 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1265 return Error::NONE;
1266}
1267
1268Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1269 uint8_t componentMask, uint64_t maxFrames) {
1270 const auto status =
1271 mAidlComposerClient
1272 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1273 static_cast<AidlFormatColorComponent>(
1274 componentMask),
1275 static_cast<int64_t>(maxFrames));
1276 if (!status.isOk()) {
1277 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1278 return static_cast<Error>(status.getServiceSpecificError());
1279 }
1280 return Error::NONE;
1281}
1282
1283Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1284 uint64_t timestamp, DisplayedFrameStats* outStats) {
1285 if (!outStats) {
1286 return Error::BAD_PARAMETER;
1287 }
1288
1289 AidlDisplayContentSample sample;
1290 const auto status =
1291 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1292 static_cast<int64_t>(maxFrames),
1293 static_cast<int64_t>(timestamp),
1294 &sample);
1295 if (!status.isOk()) {
1296 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1297 return static_cast<Error>(status.getServiceSpecificError());
1298 }
1299 *outStats = translate<DisplayedFrameStats>(sample);
1300 return Error::NONE;
1301}
1302
1303Error AidlComposer::setLayerPerFrameMetadataBlobs(
1304 Display display, Layer layer,
1305 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001306 Error error = Error::NONE;
1307 mMutex.lock_shared();
1308 if (auto writer = getWriter(display)) {
1309 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1310 translate<int64_t>(layer),
1311 translate<AidlPerFrameMetadataBlob>(metadata));
1312 } else {
1313 error = Error::BAD_DISPLAY;
1314 }
1315 mMutex.unlock_shared();
1316 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001317}
1318
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001319Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001320 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001321 Error error = Error::NONE;
1322 mMutex.lock_shared();
1323 if (auto writer = getWriter(display)) {
1324 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001325
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001326 if (options.applyImmediately) {
1327 error = execute(display);
1328 mMutex.unlock_shared();
1329 return error;
1330 }
1331 } else {
1332 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001333 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001334 mMutex.unlock_shared();
1335 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001336}
1337
1338Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001339 std::vector<AidlDisplayCapability>* outCapabilities) {
1340 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1341 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001342 if (!status.isOk()) {
1343 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001344 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001345 return static_cast<Error>(status.getServiceSpecificError());
1346 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001347 return Error::NONE;
1348}
1349
1350V2_4::Error AidlComposer::getDisplayConnectionType(
1351 Display display, IComposerClient::DisplayConnectionType* outType) {
1352 AidlDisplayConnectionType type;
1353 const auto status =
1354 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1355 if (!status.isOk()) {
1356 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1357 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1358 }
1359 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1360 return V2_4::Error::NONE;
1361}
1362
1363V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1364 int32_t vsyncPeriod;
1365 const auto status =
1366 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1367 if (!status.isOk()) {
1368 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1369 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1370 }
1371 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1372 return V2_4::Error::NONE;
1373}
1374
1375V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1376 Display display, Config config,
1377 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1378 VsyncPeriodChangeTimeline* outTimeline) {
1379 AidlVsyncPeriodChangeTimeline timeline;
1380 const auto status =
1381 mAidlComposerClient
1382 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1383 translate<int32_t>(config),
1384 translate<AidlVsyncPeriodChangeConstraints>(
1385 vsyncPeriodChangeConstraints),
1386 &timeline);
1387 if (!status.isOk()) {
1388 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1389 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1390 }
1391 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1392 return V2_4::Error::NONE;
1393}
1394
1395V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1396 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1397 if (!status.isOk()) {
1398 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1399 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1400 }
1401 return V2_4::Error::NONE;
1402}
1403
1404V2_4::Error AidlComposer::getSupportedContentTypes(
1405 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1406 std::vector<AidlContentType> types;
1407 const auto status =
1408 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1409 if (!status.isOk()) {
1410 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1411 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1412 }
1413 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1414 return V2_4::Error::NONE;
1415}
1416
1417V2_4::Error AidlComposer::setContentType(Display display,
1418 IComposerClient::ContentType contentType) {
1419 const auto status =
1420 mAidlComposerClient->setContentType(translate<int64_t>(display),
1421 translate<AidlContentType>(contentType));
1422 if (!status.isOk()) {
1423 ALOGE("setContentType failed %s", status.getDescription().c_str());
1424 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1425 }
1426 return V2_4::Error::NONE;
1427}
1428
Ady Abraham3f976752021-12-20 16:17:50 -08001429V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1430 const std::vector<uint8_t>&) {
1431 // There are no users for this API. See b/209691612.
1432 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001433}
1434
1435V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001436 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
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
Kriti Dang7defaf32021-11-15 11:55:43 +01001441Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1442 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1443 translate<int32_t>(config));
1444 if (!status.isOk()) {
1445 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1446 return static_cast<Error>(status.getServiceSpecificError());
1447 }
1448 return Error::NONE;
1449}
1450
1451Error AidlComposer::clearBootDisplayConfig(Display display) {
1452 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1453 if (!status.isOk()) {
1454 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1455 return static_cast<Error>(status.getServiceSpecificError());
1456 }
1457 return Error::NONE;
1458}
1459
1460Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1461 int32_t displayConfig;
1462 const auto status =
1463 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1464 &displayConfig);
1465 if (!status.isOk()) {
1466 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1467 return static_cast<Error>(status.getServiceSpecificError());
1468 }
1469 *config = translate<uint32_t>(displayConfig);
1470 return Error::NONE;
1471}
1472
Kriti Dang674b9372022-11-18 10:58:44 +01001473Error AidlComposer::getHdrConversionCapabilities(
1474 std::vector<AidlHdrConversionCapability>* hdrConversionCapabilities) {
1475 const auto status =
1476 mAidlComposerClient->getHdrConversionCapabilities(hdrConversionCapabilities);
1477 if (!status.isOk()) {
1478 hdrConversionCapabilities = {};
1479 ALOGE("getHdrConversionCapabilities failed %s", status.getDescription().c_str());
1480 return static_cast<Error>(status.getServiceSpecificError());
1481 }
1482 return Error::NONE;
1483}
1484
Kriti Dangd432bb52023-02-09 18:21:04 +01001485Error AidlComposer::setHdrConversionStrategy(AidlHdrConversionStrategy hdrConversionStrategy,
1486 Hdr* outPreferredHdrOutputType) {
1487 const auto status = mAidlComposerClient->setHdrConversionStrategy(hdrConversionStrategy,
1488 outPreferredHdrOutputType);
Kriti Dang674b9372022-11-18 10:58:44 +01001489 if (!status.isOk()) {
1490 ALOGE("setHdrConversionStrategy failed %s", status.getDescription().c_str());
1491 return static_cast<Error>(status.getServiceSpecificError());
1492 }
1493 return Error::NONE;
1494}
1495
ramindanib2158ee2023-02-13 20:29:59 -08001496Error AidlComposer::setRefreshRateChangedCallbackDebugEnabled(Display displayId, bool enabled) {
1497 const auto status =
1498 mAidlComposerClient->setRefreshRateChangedCallbackDebugEnabled(translate<int64_t>(
1499 displayId),
1500 enabled);
1501 if (!status.isOk()) {
1502 ALOGE("setRefreshRateChangedCallbackDebugEnabled failed %s",
1503 status.getDescription().c_str());
1504 return static_cast<Error>(status.getServiceSpecificError());
1505 }
1506 return Error::NONE;
1507}
1508
ramindani3acaaf52023-09-25 10:31:27 -07001509Error AidlComposer::notifyExpectedPresent(Display displayId, nsecs_t expectedPresentTime,
1510 int32_t frameIntervalNs) {
1511 const auto status =
1512 mAidlComposerClient->notifyExpectedPresent(translate<int64_t>(displayId),
1513 ClockMonotonicTimestamp{expectedPresentTime},
1514 frameIntervalNs);
1515
1516 if (!status.isOk()) {
1517 ALOGE("notifyExpectedPresent failed %s", status.getDescription().c_str());
1518 return static_cast<Error>(status.getServiceSpecificError());
1519 }
1520 return Error::NONE;
1521}
1522
Ady Abrahame7385f72021-09-05 00:54:25 -07001523Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001524 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001525 Error error = Error::NONE;
1526 mMutex.lock_shared();
1527 if (auto reader = getReader(display)) {
1528 *outClientTargetProperty =
1529 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1530 } else {
1531 error = Error::BAD_DISPLAY;
1532 }
1533 mMutex.unlock_shared();
1534 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001535}
1536
Alec Mouri6da0e272022-02-07 12:45:57 -08001537Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001538 Error error = Error::NONE;
1539 mMutex.lock_shared();
1540 if (auto writer = getWriter(display)) {
1541 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1542 brightness);
1543 } else {
1544 error = Error::BAD_DISPLAY;
1545 }
1546 mMutex.unlock_shared();
1547 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001548}
1549
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001550Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1551 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001552 Error error = Error::NONE;
1553 mMutex.lock_shared();
1554 if (auto writer = getWriter(display)) {
1555 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1556 translate<AidlRect>(blocking));
1557 } else {
1558 error = Error::BAD_DISPLAY;
1559 }
1560 mMutex.unlock_shared();
1561 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001562}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001563
1564Error AidlComposer::getDisplayDecorationSupport(Display display,
1565 std::optional<DisplayDecorationSupport>* support) {
1566 const auto status =
1567 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1568 if (!status.isOk()) {
1569 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1570 support->reset();
1571 return static_cast<Error>(status.getServiceSpecificError());
1572 }
1573 return Error::NONE;
1574}
ramindani32cf0602022-03-02 02:30:29 +00001575
1576Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1577 const auto status =
1578 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1579 translate<int32_t>(timeout.count()));
1580 if (!status.isOk()) {
1581 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1582 return static_cast<Error>(status.getServiceSpecificError());
1583 }
1584 return Error::NONE;
1585}
1586
ramindani06e518e2022-03-14 18:47:53 +00001587Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1588 AidlTransform* outDisplayOrientation) {
1589 const auto status =
1590 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1591 outDisplayOrientation);
1592 if (!status.isOk()) {
1593 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1594 return static_cast<Error>(status.getServiceSpecificError());
1595 }
1596 return Error::NONE;
1597}
1598
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001599ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1600 REQUIRES_SHARED(mMutex) {
1601 return mWriters.get(display);
1602}
1603
1604ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1605 REQUIRES_SHARED(mMutex) {
1606 if (mSingleReader) {
1607 display = translate<Display>(kSingleReaderKey);
1608 }
1609 return mReaders.get(display);
1610}
1611
1612void AidlComposer::removeDisplay(Display display) {
1613 mMutex.lock();
1614 bool wasErased = mWriters.erase(display);
1615 ALOGW_IF(!wasErased,
1616 "Attempting to remove writer for display %" PRId64 " which is not connected",
1617 translate<int64_t>(display));
1618 if (!mSingleReader) {
1619 removeReader(display);
1620 }
1621 mMutex.unlock();
1622}
1623
1624void AidlComposer::onHotplugDisconnect(Display display) {
1625 removeDisplay(display);
1626}
1627
1628bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
Leon Scroggins IIIdb635042023-10-20 09:58:17 -04001629 if (!FlagManager::getInstance().multithreaded_present()) return false;
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001630 const auto displayId = translate<int64_t>(display);
1631 std::vector<AidlDisplayCapability> capabilities;
1632 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1633 if (!status.isOk()) {
1634 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1635 return false;
1636 }
1637 return std::find(capabilities.begin(), capabilities.end(),
1638 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
1639}
1640
1641void AidlComposer::addReader(Display display) {
1642 const auto displayId = translate<int64_t>(display);
1643 std::optional<int64_t> displayOpt;
1644 if (displayId != kSingleReaderKey) {
1645 displayOpt.emplace(displayId);
1646 }
1647 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1648 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1649 displayId);
1650}
1651
1652void AidlComposer::removeReader(Display display) {
1653 bool wasErased = mReaders.erase(display);
1654 ALOGW_IF(!wasErased,
1655 "Attempting to remove reader for display %" PRId64 " which is not connected",
1656 translate<int64_t>(display));
1657}
1658
1659void AidlComposer::addDisplay(Display display) {
1660 const auto displayId = translate<int64_t>(display);
1661 mMutex.lock();
1662 auto [it, added] = mWriters.try_emplace(display, displayId);
1663 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1664 displayId);
1665 if (mSingleReader) {
1666 if (hasMultiThreadedPresentSupport(display)) {
1667 mSingleReader = false;
1668 removeReader(translate<Display>(kSingleReaderKey));
1669 // Note that this includes the new display.
1670 for (const auto& [existingDisplay, _] : mWriters) {
1671 addReader(existingDisplay);
1672 }
1673 }
1674 } else {
1675 addReader(display);
1676 }
1677 mMutex.unlock();
1678}
1679
1680void AidlComposer::onHotplugConnect(Display display) {
1681 addDisplay(display);
1682}
Ady Abrahame7385f72021-09-05 00:54:25 -07001683} // namespace Hwc2
1684} // namespace android