blob: d061a99f7db2a3eb02362d473f63fd7afa52f950 [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
Ady Abrahamc4acf512022-02-18 17:11:59 -080023#include <android-base/file.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070024#include <android/binder_ibinder_platform.h>
25#include <android/binder_manager.h>
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -050026#include <gui/TraceUtils.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070027#include <log/log.h>
28#include <utils/Trace.h>
29
30#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
31
32#include <algorithm>
33#include <cinttypes>
34
Yichi Chen3401b562022-01-17 15:42:35 +080035#include "HWC2.h"
36
Ady Abrahame7385f72021-09-05 00:54:25 -070037namespace android {
38
39using hardware::hidl_handle;
40using hardware::hidl_vec;
41using hardware::Return;
42
43using aidl::android::hardware::graphics::composer3::BnComposerCallback;
44using aidl::android::hardware::graphics::composer3::Capability;
Alec Mouri85065692022-03-18 00:58:26 +000045using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Ady Abrahame7385f72021-09-05 00:54:25 -070046using aidl::android::hardware::graphics::composer3::PowerMode;
47using aidl::android::hardware::graphics::composer3::VirtualDisplay;
48
Ady Abraham42977362021-12-07 21:04:49 -080049using aidl::android::hardware::graphics::composer3::CommandResultPayload;
Ady Abrahama6388c02021-11-11 21:11:51 -080050
Ady Abrahame7385f72021-09-05 00:54:25 -070051using AidlColorMode = aidl::android::hardware::graphics::composer3::ColorMode;
52using AidlContentType = aidl::android::hardware::graphics::composer3::ContentType;
53using AidlDisplayIdentification =
54 aidl::android::hardware::graphics::composer3::DisplayIdentification;
55using AidlDisplayContentSample = aidl::android::hardware::graphics::composer3::DisplayContentSample;
56using AidlDisplayAttribute = aidl::android::hardware::graphics::composer3::DisplayAttribute;
57using AidlDisplayCapability = aidl::android::hardware::graphics::composer3::DisplayCapability;
Ady Abrahame7385f72021-09-05 00:54:25 -070058using AidlHdrCapabilities = aidl::android::hardware::graphics::composer3::HdrCapabilities;
Sally Qi0cbd08b2022-08-17 12:12:28 -070059using AidlOverlayProperties = aidl::android::hardware::graphics::composer3::OverlayProperties;
Ady Abrahame7385f72021-09-05 00:54:25 -070060using AidlPerFrameMetadata = aidl::android::hardware::graphics::composer3::PerFrameMetadata;
61using AidlPerFrameMetadataKey = aidl::android::hardware::graphics::composer3::PerFrameMetadataKey;
62using AidlPerFrameMetadataBlob = aidl::android::hardware::graphics::composer3::PerFrameMetadataBlob;
63using AidlRenderIntent = aidl::android::hardware::graphics::composer3::RenderIntent;
64using AidlVsyncPeriodChangeConstraints =
65 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeConstraints;
66using AidlVsyncPeriodChangeTimeline =
67 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline;
Ady Abrahame7385f72021-09-05 00:54:25 -070068using AidlDisplayContentSamplingAttributes =
69 aidl::android::hardware::graphics::composer3::DisplayContentSamplingAttributes;
70using AidlFormatColorComponent = aidl::android::hardware::graphics::composer3::FormatColorComponent;
71using AidlDisplayConnectionType =
72 aidl::android::hardware::graphics::composer3::DisplayConnectionType;
Ady Abrahame7385f72021-09-05 00:54:25 -070073
74using AidlColorTransform = aidl::android::hardware::graphics::common::ColorTransform;
75using AidlDataspace = aidl::android::hardware::graphics::common::Dataspace;
76using AidlFRect = aidl::android::hardware::graphics::common::FRect;
77using AidlRect = aidl::android::hardware::graphics::common::Rect;
78using AidlTransform = aidl::android::hardware::graphics::common::Transform;
79
80namespace Hwc2 {
81
82namespace {
83
84template <typename To, typename From>
85To translate(From x) {
86 return static_cast<To>(x);
87}
88
89template <typename To, typename From>
90std::vector<To> translate(const std::vector<From>& in) {
91 std::vector<To> out;
92 out.reserve(in.size());
93 std::transform(in.begin(), in.end(), std::back_inserter(out),
94 [](From x) { return translate<To>(x); });
95 return out;
96}
97
98template <>
99AidlRect translate(IComposerClient::Rect x) {
100 return AidlRect{
101 .left = x.left,
102 .top = x.top,
103 .right = x.right,
104 .bottom = x.bottom,
105 };
106}
107
108template <>
109AidlFRect translate(IComposerClient::FRect x) {
110 return AidlFRect{
111 .left = x.left,
112 .top = x.top,
113 .right = x.right,
114 .bottom = x.bottom,
115 };
116}
117
118template <>
Ady Abrahame7385f72021-09-05 00:54:25 -0700119AidlPerFrameMetadataBlob translate(IComposerClient::PerFrameMetadataBlob x) {
120 AidlPerFrameMetadataBlob blob;
121 blob.key = translate<AidlPerFrameMetadataKey>(x.key),
Long Linga4628782022-02-18 13:44:26 -0800122 std::copy(x.blob.begin(), x.blob.end(), std::inserter(blob.blob, blob.blob.end()));
Ady Abrahame7385f72021-09-05 00:54:25 -0700123 return blob;
124}
125
126template <>
127AidlPerFrameMetadata translate(IComposerClient::PerFrameMetadata x) {
128 return AidlPerFrameMetadata{
129 .key = translate<AidlPerFrameMetadataKey>(x.key),
130 .value = x.value,
131 };
132}
133
134template <>
135DisplayedFrameStats translate(AidlDisplayContentSample x) {
136 return DisplayedFrameStats{
137 .numFrames = static_cast<uint64_t>(x.frameCount),
138 .component_0_sample = translate<uint64_t>(x.sampleComponent0),
139 .component_1_sample = translate<uint64_t>(x.sampleComponent1),
140 .component_2_sample = translate<uint64_t>(x.sampleComponent2),
141 .component_3_sample = translate<uint64_t>(x.sampleComponent3),
142 };
143}
144
145template <>
146AidlVsyncPeriodChangeConstraints translate(IComposerClient::VsyncPeriodChangeConstraints x) {
147 return AidlVsyncPeriodChangeConstraints{
148 .desiredTimeNanos = x.desiredTimeNanos,
149 .seamlessRequired = x.seamlessRequired,
150 };
151}
152
153template <>
154VsyncPeriodChangeTimeline translate(AidlVsyncPeriodChangeTimeline x) {
155 return VsyncPeriodChangeTimeline{
156 .newVsyncAppliedTimeNanos = x.newVsyncAppliedTimeNanos,
157 .refreshRequired = x.refreshRequired,
158 .refreshTimeNanos = x.refreshTimeNanos,
159 };
160}
Ady Abrahame7385f72021-09-05 00:54:25 -0700161mat4 makeMat4(std::vector<float> in) {
162 return mat4(static_cast<const float*>(in.data()));
163}
164
165} // namespace
166
167class AidlIComposerCallbackWrapper : public BnComposerCallback {
168public:
Yichi Chen3401b562022-01-17 15:42:35 +0800169 AidlIComposerCallbackWrapper(HWC2::ComposerCallback& callback) : mCallback(callback) {}
Ady Abrahame7385f72021-09-05 00:54:25 -0700170
171 ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override {
172 const auto connection = in_connected ? V2_4::IComposerCallback::Connection::CONNECTED
173 : V2_4::IComposerCallback::Connection::DISCONNECTED;
Yichi Chen3401b562022-01-17 15:42:35 +0800174 mCallback.onComposerHalHotplug(translate<Display>(in_display), connection);
Ady Abrahame7385f72021-09-05 00:54:25 -0700175 return ::ndk::ScopedAStatus::ok();
176 }
177
178 ::ndk::ScopedAStatus onRefresh(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800179 mCallback.onComposerHalRefresh(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700180 return ::ndk::ScopedAStatus::ok();
181 }
Yichi Chen3401b562022-01-17 15:42:35 +0800182
Ady Abrahame7385f72021-09-05 00:54:25 -0700183 ::ndk::ScopedAStatus onSeamlessPossible(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800184 mCallback.onComposerHalSeamlessPossible(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700185 return ::ndk::ScopedAStatus::ok();
186 }
Yichi Chen3401b562022-01-17 15:42:35 +0800187
Ady Abrahame7385f72021-09-05 00:54:25 -0700188 ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
189 int32_t in_vsyncPeriodNanos) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800190 mCallback.onComposerHalVsync(translate<Display>(in_display), in_timestamp,
191 static_cast<uint32_t>(in_vsyncPeriodNanos));
Ady Abrahame7385f72021-09-05 00:54:25 -0700192 return ::ndk::ScopedAStatus::ok();
193 }
Yichi Chen3401b562022-01-17 15:42:35 +0800194
Ady Abrahame7385f72021-09-05 00:54:25 -0700195 ::ndk::ScopedAStatus onVsyncPeriodTimingChanged(
196 int64_t in_display, const AidlVsyncPeriodChangeTimeline& in_updatedTimeline) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800197 mCallback.onComposerHalVsyncPeriodTimingChanged(translate<Display>(in_display),
198 translate<V2_4::VsyncPeriodChangeTimeline>(
199 in_updatedTimeline));
200 return ::ndk::ScopedAStatus::ok();
201 }
202
203 ::ndk::ScopedAStatus onVsyncIdle(int64_t in_display) override {
204 mCallback.onComposerHalVsyncIdle(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700205 return ::ndk::ScopedAStatus::ok();
206 }
207
208private:
Yichi Chen3401b562022-01-17 15:42:35 +0800209 HWC2::ComposerCallback& mCallback;
Ady Abrahame7385f72021-09-05 00:54:25 -0700210};
211
Ady Abraham9fc28052021-10-14 17:21:38 -0700212std::string AidlComposer::instance(const std::string& serviceName) {
213 return std::string(AidlIComposer::descriptor) + "/" + serviceName;
214}
215
216bool AidlComposer::isDeclared(const std::string& serviceName) {
217 return AServiceManager_isDeclared(instance(serviceName).c_str());
218}
Ady Abrahame7385f72021-09-05 00:54:25 -0700219
Ady Abrahama6388c02021-11-11 21:11:51 -0800220AidlComposer::AidlComposer(const std::string& serviceName) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700221 // This only waits if the service is actually declared
Ady Abraham9fc28052021-10-14 17:21:38 -0700222 mAidlComposer = AidlIComposer::fromBinder(
223 ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
Ady Abrahame7385f72021-09-05 00:54:25 -0700224 if (!mAidlComposer) {
225 LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
226 return;
227 }
228
229 if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
230 LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
231 return;
232 }
233
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400234 addReader(translate<Display>(kSingleReaderKey));
235
Brian Lindahl90553da2022-12-06 13:36:30 -0700236 // TODO(b/262041682): When using new API to clear buffer slots, don't allocate this buffer.
237 mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
238 GraphicBuffer::USAGE_HW_COMPOSER |
239 GraphicBuffer::USAGE_SW_READ_OFTEN |
240 GraphicBuffer::USAGE_SW_WRITE_OFTEN,
241 "AidlComposer");
242 if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
243 LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
244 return;
245 }
246
Ady Abrahame7385f72021-09-05 00:54:25 -0700247 ALOGI("Loaded AIDL composer3 HAL service");
248}
249
250AidlComposer::~AidlComposer() = default;
251
Ady Abraham4d211cf2021-12-14 16:19:03 -0800252bool AidlComposer::isSupported(OptionalFeature feature) const {
253 switch (feature) {
254 case OptionalFeature::RefreshRateSwitching:
Ady Abraham43065bd2021-12-10 17:22:15 -0800255 case OptionalFeature::ExpectedPresentTime:
Alec Mouricdf16792021-12-10 13:16:06 -0800256 case OptionalFeature::DisplayBrightnessCommand:
ramindani32cf0602022-03-02 02:30:29 +0000257 case OptionalFeature::KernelIdleTimer:
ramindani06e518e2022-03-14 18:47:53 +0000258 case OptionalFeature::PhysicalDisplayOrientation:
Ady Abraham4d211cf2021-12-14 16:19:03 -0800259 return true;
260 }
261}
262
Ady Abrahamde549d42022-01-26 19:19:17 -0800263std::vector<Capability> AidlComposer::getCapabilities() {
Ady Abrahame7385f72021-09-05 00:54:25 -0700264 std::vector<Capability> capabilities;
265 const auto status = mAidlComposer->getCapabilities(&capabilities);
266 if (!status.isOk()) {
267 ALOGE("getCapabilities failed %s", status.getDescription().c_str());
268 return {};
269 }
Ady Abrahamde549d42022-01-26 19:19:17 -0800270 return capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700271}
272
273std::string AidlComposer::dumpDebugInfo() {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800274 int pipefds[2];
275 int result = pipe(pipefds);
276 if (result < 0) {
277 ALOGE("dumpDebugInfo: pipe failed: %s", strerror(errno));
Ady Abrahame7385f72021-09-05 00:54:25 -0700278 return {};
279 }
Ady Abrahamc4acf512022-02-18 17:11:59 -0800280
281 std::string str;
yihsing.shen58847c52022-09-23 15:39:30 +0800282 // Use other thread to read pipe to prevent
283 // pipe is full, making HWC be blocked in writing.
284 std::thread t([&]() {
285 base::ReadFdToString(pipefds[0], &str);
286 });
Ady Abrahamc4acf512022-02-18 17:11:59 -0800287 const auto status = mAidlComposer->dump(pipefds[1], /*args*/ nullptr, /*numArgs*/ 0);
288 // Close the write-end of the pipe to make sure that when reading from the
289 // read-end we will get eof instead of blocking forever
290 close(pipefds[1]);
291
yihsing.shen58847c52022-09-23 15:39:30 +0800292 if (status != STATUS_OK) {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800293 ALOGE("dumpDebugInfo: dump failed: %d", status);
294 }
295
yihsing.shen58847c52022-09-23 15:39:30 +0800296 t.join();
Ady Abrahamc4acf512022-02-18 17:11:59 -0800297 close(pipefds[0]);
298 return str;
Ady Abrahame7385f72021-09-05 00:54:25 -0700299}
300
Yichi Chen3401b562022-01-17 15:42:35 +0800301void AidlComposer::registerCallback(HWC2::ComposerCallback& callback) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700302 if (mAidlComposerCallback) {
303 ALOGE("Callback already registered");
304 }
Yichi Chen3401b562022-01-17 15:42:35 +0800305
Ady Abraham9fc28052021-10-14 17:21:38 -0700306 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
Ady Abrahame7385f72021-09-05 00:54:25 -0700307 AIBinder_setMinSchedulerPolicy(mAidlComposerCallback->asBinder().get(), SCHED_FIFO, 2);
308
309 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
310 if (!status.isOk()) {
311 ALOGE("registerCallback failed %s", status.getDescription().c_str());
312 }
313}
314
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400315void AidlComposer::resetCommands(Display display) {
316 mMutex.lock_shared();
317 if (auto writer = getWriter(display)) {
318 writer->get().reset();
319 }
320 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700321}
322
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400323Error AidlComposer::executeCommands(Display display) {
324 mMutex.lock_shared();
325 auto error = execute(display);
326 mMutex.unlock_shared();
327 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700328}
329
330uint32_t AidlComposer::getMaxVirtualDisplayCount() {
331 int32_t count = 0;
332 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
333 if (!status.isOk()) {
334 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
335 return 0;
336 }
337 return static_cast<uint32_t>(count);
338}
339
340Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
341 Display* outDisplay) {
342 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
343 const int32_t bufferSlotCount = 1;
344 VirtualDisplay virtualDisplay;
345 const auto status =
346 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
347 static_cast<int32_t>(height),
348 static_cast<AidlPixelFormat>(*format),
349 bufferSlotCount, &virtualDisplay);
350
351 if (!status.isOk()) {
352 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
353 return static_cast<Error>(status.getServiceSpecificError());
354 }
355
356 *outDisplay = translate<Display>(virtualDisplay.display);
357 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400358 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700359 return Error::NONE;
360}
361
362Error AidlComposer::destroyVirtualDisplay(Display display) {
363 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
364 if (!status.isOk()) {
365 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
366 return static_cast<Error>(status.getServiceSpecificError());
367 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400368 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700369 return Error::NONE;
370}
371
372Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400373 Error error = Error::NONE;
374 mMutex.lock_shared();
375 if (auto writer = getWriter(display)) {
376 writer->get().acceptDisplayChanges(translate<int64_t>(display));
377 } else {
378 error = Error::BAD_DISPLAY;
379 }
380 mMutex.unlock_shared();
381 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700382}
383
384Error AidlComposer::createLayer(Display display, Layer* outLayer) {
385 int64_t layer;
386 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
387 kMaxLayerBufferCount, &layer);
388 if (!status.isOk()) {
389 ALOGE("createLayer failed %s", status.getDescription().c_str());
390 return static_cast<Error>(status.getServiceSpecificError());
391 }
392
393 *outLayer = translate<Layer>(layer);
394 return Error::NONE;
395}
396
397Error AidlComposer::destroyLayer(Display display, Layer layer) {
398 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
399 translate<int64_t>(layer));
400 if (!status.isOk()) {
401 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
402 return static_cast<Error>(status.getServiceSpecificError());
403 }
404 return Error::NONE;
405}
406
407Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
408 int32_t config;
409 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
410 if (!status.isOk()) {
411 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
412 return static_cast<Error>(status.getServiceSpecificError());
413 }
414 *outConfig = translate<Config>(config);
415 return Error::NONE;
416}
417
418Error AidlComposer::getChangedCompositionTypes(
419 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500420 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400421 std::vector<ChangedCompositionLayer> changedLayers;
422 Error error = Error::NONE;
423 {
424 mMutex.lock_shared();
425 if (auto reader = getReader(display)) {
426 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
427 } else {
428 error = Error::BAD_DISPLAY;
429 }
430 mMutex.unlock_shared();
431 }
Ady Abrahamde792782021-12-20 10:00:49 -0800432 outLayers->reserve(changedLayers.size());
433 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800434
Ady Abrahamde792782021-12-20 10:00:49 -0800435 for (const auto& layer : changedLayers) {
436 outLayers->emplace_back(translate<Layer>(layer.layer));
437 outTypes->emplace_back(layer.composition);
438 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400439 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700440}
441
442Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
443 std::vector<AidlColorMode> modes;
444 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
445 if (!status.isOk()) {
446 ALOGE("getColorModes failed %s", status.getDescription().c_str());
447 return static_cast<Error>(status.getServiceSpecificError());
448 }
449 *outModes = translate<ColorMode>(modes);
450 return Error::NONE;
451}
452
453Error AidlComposer::getDisplayAttribute(Display display, Config config,
454 IComposerClient::Attribute attribute, int32_t* outValue) {
455 const auto status =
456 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
457 translate<int32_t>(config),
458 static_cast<AidlDisplayAttribute>(attribute),
459 outValue);
460 if (!status.isOk()) {
461 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
462 return static_cast<Error>(status.getServiceSpecificError());
463 }
464 return Error::NONE;
465}
466
467Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
468 std::vector<int32_t> configs;
469 const auto status =
470 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
471 if (!status.isOk()) {
472 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
473 return static_cast<Error>(status.getServiceSpecificError());
474 }
475 *outConfigs = translate<Config>(configs);
476 return Error::NONE;
477}
478
479Error AidlComposer::getDisplayName(Display display, std::string* outName) {
480 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
481 if (!status.isOk()) {
482 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
483 return static_cast<Error>(status.getServiceSpecificError());
484 }
485 return Error::NONE;
486}
487
488Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
489 std::vector<Layer>* outLayers,
490 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400491 Error error = Error::NONE;
492 DisplayRequest displayRequests;
493 {
494 mMutex.lock_shared();
495 if (auto reader = getReader(display)) {
496 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
497 } else {
498 error = Error::BAD_DISPLAY;
499 }
500 mMutex.unlock_shared();
501 }
Ady Abrahamde792782021-12-20 10:00:49 -0800502 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
503 outLayers->reserve(displayRequests.layerRequests.size());
504 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
505
506 for (const auto& layer : displayRequests.layerRequests) {
507 outLayers->emplace_back(translate<Layer>(layer.layer));
508 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
509 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400510 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700511}
512
513Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800514 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700515 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800516 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700517 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800518 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700519 return static_cast<Error>(status.getServiceSpecificError());
520 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800521 *outSupport = std::find(capabilities.begin(), capabilities.end(),
522 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700523 return Error::NONE;
524}
525
ramindani32cf0602022-03-02 02:30:29 +0000526Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
527 std::vector<AidlDisplayCapability> capabilities;
528 const auto status =
529 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
530 if (!status.isOk()) {
531 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
532 return static_cast<Error>(status.getServiceSpecificError());
533 }
534 *outSupport = std::find(capabilities.begin(), capabilities.end(),
535 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
536 return Error::NONE;
537}
538
Ady Abrahame7385f72021-09-05 00:54:25 -0700539Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
540 float* outMaxLuminance, float* outMaxAverageLuminance,
541 float* outMinLuminance) {
542 AidlHdrCapabilities capabilities;
543 const auto status =
544 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
545 if (!status.isOk()) {
546 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
547 return static_cast<Error>(status.getServiceSpecificError());
548 }
549
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100550 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700551 *outMaxLuminance = capabilities.maxLuminance;
552 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
553 *outMinLuminance = capabilities.minLuminance;
554 return Error::NONE;
555}
556
Sally Qibb866c12022-10-17 11:31:20 -0700557Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
558 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
559 if (!status.isOk()) {
560 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
561 return static_cast<Error>(status.getServiceSpecificError());
562 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700563 return Error::NONE;
564}
565
Ady Abrahame7385f72021-09-05 00:54:25 -0700566Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
567 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400568 Error error = Error::NONE;
569 std::vector<ReleaseFences::Layer> fences;
570 {
571 mMutex.lock_shared();
572 if (auto reader = getReader(display)) {
573 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
574 } else {
575 error = Error::BAD_DISPLAY;
576 }
577 mMutex.unlock_shared();
578 }
Ady Abrahamde792782021-12-20 10:00:49 -0800579 outLayers->reserve(fences.size());
580 outReleaseFences->reserve(fences.size());
581
582 for (auto& fence : fences) {
583 outLayers->emplace_back(translate<Layer>(fence.layer));
584 // take ownership
585 const int fenceOwner = fence.fence.get();
586 *fence.fence.getR() = -1;
587 outReleaseFences->emplace_back(fenceOwner);
588 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400589 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700590}
591
592Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500593 const auto displayId = translate<int64_t>(display);
594 ATRACE_FORMAT("HwcPresentDisplay %" PRId64, displayId);
595
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400596 Error error = Error::NONE;
597 mMutex.lock_shared();
598 auto writer = getWriter(display);
599 auto reader = getReader(display);
600 if (writer && reader) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500601 writer->get().presentDisplay(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400602 error = execute(display);
603 } else {
604 error = Error::BAD_DISPLAY;
605 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700606
Ady Abrahame7385f72021-09-05 00:54:25 -0700607 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400608 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700609 return error;
610 }
611
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500612 auto fence = reader->get().takePresentFence(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400613 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800614 // take ownership
615 *outPresentFence = fence.get();
616 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700617 return Error::NONE;
618}
619
620Error AidlComposer::setActiveConfig(Display display, Config config) {
621 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
622 translate<int32_t>(config));
623 if (!status.isOk()) {
624 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
625 return static_cast<Error>(status.getServiceSpecificError());
626 }
627 return Error::NONE;
628}
629
630Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
631 int acquireFence, Dataspace dataspace,
632 const std::vector<IComposerClient::Rect>& damage) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700633 const native_handle_t* handle = nullptr;
634 if (target.get()) {
635 handle = target->getNativeBuffer()->handle;
636 }
637
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400638 Error error = Error::NONE;
639 mMutex.lock_shared();
640 if (auto writer = getWriter(display)) {
641 writer->get()
642 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
643 translate<aidl::android::hardware::graphics::common::Dataspace>(
644 dataspace),
645 translate<AidlRect>(damage));
646 } else {
647 error = Error::BAD_DISPLAY;
648 }
649 mMutex.unlock_shared();
650 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700651}
652
653Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
654 const auto status =
655 mAidlComposerClient->setColorMode(translate<int64_t>(display),
656 translate<AidlColorMode>(mode),
657 translate<AidlRenderIntent>(renderIntent));
658 if (!status.isOk()) {
659 ALOGE("setColorMode failed %s", status.getDescription().c_str());
660 return static_cast<Error>(status.getServiceSpecificError());
661 }
662 return Error::NONE;
663}
664
Ady Abrahamdc011a92021-12-21 14:06:44 -0800665Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400666 auto error = Error::NONE;
667 mMutex.lock_shared();
668 if (auto writer = getWriter(display)) {
669 writer->get().setColorTransform(translate<int64_t>(display), matrix);
670 } else {
671 error = Error::BAD_DISPLAY;
672 }
673 mMutex.unlock_shared();
674 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700675}
676
677Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
678 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400679 auto error = Error::NONE;
680 mMutex.lock_shared();
681 if (auto writer = getWriter(display)) {
682 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
683 } else {
684 error = Error::BAD_DISPLAY;
685 }
686 mMutex.unlock_shared();
687 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700688}
689
690Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
691 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
692 translate<PowerMode>(mode));
693 if (!status.isOk()) {
694 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
695 return static_cast<Error>(status.getServiceSpecificError());
696 }
697 return Error::NONE;
698}
699
700Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
701 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
702 const auto status =
703 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
704 if (!status.isOk()) {
705 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
706 return static_cast<Error>(status.getServiceSpecificError());
707 }
708 return Error::NONE;
709}
710
711Error AidlComposer::setClientTargetSlotCount(Display display) {
712 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
713 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
714 bufferSlotCount);
715 if (!status.isOk()) {
716 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
717 return static_cast<Error>(status.getServiceSpecificError());
718 }
719 return Error::NONE;
720}
721
Ady Abraham43065bd2021-12-10 17:22:15 -0800722Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
723 uint32_t* outNumTypes, uint32_t* outNumRequests) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400724 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500725 ATRACE_FORMAT("HwcValidateDisplay %" PRId64, displayId);
726
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400727 Error error = Error::NONE;
728 mMutex.lock_shared();
729 auto writer = getWriter(display);
730 auto reader = getReader(display);
731 if (writer && reader) {
732 writer->get().validateDisplay(displayId, ClockMonotonicTimestamp{expectedPresentTime});
733 error = execute(display);
734 } else {
735 error = Error::BAD_DISPLAY;
736 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700737
Ady Abrahame7385f72021-09-05 00:54:25 -0700738 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400739 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700740 return error;
741 }
742
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400743 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700744
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400745 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700746 return Error::NONE;
747}
748
Ady Abraham43065bd2021-12-10 17:22:15 -0800749Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
750 uint32_t* outNumTypes, uint32_t* outNumRequests,
751 int* outPresentFence, uint32_t* state) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400752 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500753 ATRACE_FORMAT("HwcPresentOrValidateDisplay %" PRId64, displayId);
754
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400755 Error error = Error::NONE;
756 mMutex.lock_shared();
757 auto writer = getWriter(display);
758 auto reader = getReader(display);
759 if (writer && reader) {
760 writer->get().presentOrvalidateDisplay(displayId,
761 ClockMonotonicTimestamp{expectedPresentTime});
762 error = execute(display);
763 } else {
764 error = Error::BAD_DISPLAY;
765 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700766
Ady Abrahame7385f72021-09-05 00:54:25 -0700767 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400768 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700769 return error;
770 }
771
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400772 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800773 if (!result.has_value()) {
774 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400775 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800776 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700777 }
778
Ady Abrahamde792782021-12-20 10:00:49 -0800779 *state = translate<uint32_t>(*result);
780
781 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400782 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800783 // take ownership
784 *outPresentFence = fence.get();
785 *fence.getR() = -1;
786 }
787
788 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400789 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700790 }
791
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400792 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700793 return Error::NONE;
794}
795
796Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400797 Error error = Error::NONE;
798 mMutex.lock_shared();
799 if (auto writer = getWriter(display)) {
800 writer->get().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
801 x, y);
802 } else {
803 error = Error::BAD_DISPLAY;
804 }
805 mMutex.unlock_shared();
806 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700807}
808
809Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
810 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700811 const native_handle_t* handle = nullptr;
812 if (buffer.get()) {
813 handle = buffer->getNativeBuffer()->handle;
814 }
815
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400816 Error error = Error::NONE;
817 mMutex.lock_shared();
818 if (auto writer = getWriter(display)) {
819 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
820 handle, acquireFence);
821 } else {
822 error = Error::BAD_DISPLAY;
823 }
824 mMutex.unlock_shared();
825 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700826}
827
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700828Error AidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
829 const std::vector<uint32_t>& slotsToClear,
830 uint32_t activeBufferSlot) {
831 if (slotsToClear.empty()) {
832 return Error::NONE;
833 }
834
Brian Lindahl90553da2022-12-06 13:36:30 -0700835 Error error = Error::NONE;
836 mMutex.lock_shared();
837 if (auto writer = getWriter(display)) {
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700838 // Backwards compatible way of clearing buffer slots is tricky...
839 for (uint32_t slot : slotsToClear) {
840 // Don't clear the active buffer slot because we need to restore the active buffer
841 // after clearing the requested buffer slots with a placeholder buffer.
842 if (slot != activeBufferSlot) {
843 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
844 translate<int64_t>(layer), slot,
845 mClearSlotBuffer->handle, /*fence*/ -1);
846 }
847 }
848 // Since we clear buffers by setting them to a placeholder buffer, we want to make
849 // sure that the last setLayerBuffer command is sent with the currently active
850 // buffer, not the placeholder buffer, so that there is no perceptual change when
851 // buffers are discarded.
Brian Lindahl90553da2022-12-06 13:36:30 -0700852 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700853 translate<int64_t>(layer), activeBufferSlot,
854 // The active buffer is still cached in
855 // its slot and doesn't need a fence.
856 /*buffer*/ nullptr, /*fence*/ -1);
Brian Lindahl90553da2022-12-06 13:36:30 -0700857 } else {
858 error = Error::BAD_DISPLAY;
859 }
860 mMutex.unlock_shared();
861 return error;
862}
863
Ady Abrahame7385f72021-09-05 00:54:25 -0700864Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
865 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400866 Error error = Error::NONE;
867 mMutex.lock_shared();
868 if (auto writer = getWriter(display)) {
869 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
870 translate<AidlRect>(damage));
871 } else {
872 error = Error::BAD_DISPLAY;
873 }
874 mMutex.unlock_shared();
875 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700876}
877
878Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
879 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400880 Error error = Error::NONE;
881 mMutex.lock_shared();
882 if (auto writer = getWriter(display)) {
883 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
884 translate<BlendMode>(mode));
885 } else {
886 error = Error::BAD_DISPLAY;
887 }
888 mMutex.unlock_shared();
889 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700890}
891
Ady Abraham6e60b142022-01-06 18:10:35 -0800892Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400893 Error error = Error::NONE;
894 mMutex.lock_shared();
895 if (auto writer = getWriter(display)) {
896 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
897 } else {
898 error = Error::BAD_DISPLAY;
899 }
900 mMutex.unlock_shared();
901 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700902}
903
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500904Error AidlComposer::setLayerCompositionType(
905 Display display, Layer layer,
906 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400907 Error error = Error::NONE;
908 mMutex.lock_shared();
909 if (auto writer = getWriter(display)) {
910 writer->get().setLayerCompositionType(translate<int64_t>(display),
911 translate<int64_t>(layer), type);
912 } else {
913 error = Error::BAD_DISPLAY;
914 }
915 mMutex.unlock_shared();
916 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700917}
918
919Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400920 Error error = Error::NONE;
921 mMutex.lock_shared();
922 if (auto writer = getWriter(display)) {
923 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
924 translate<AidlDataspace>(dataspace));
925 } else {
926 error = Error::BAD_DISPLAY;
927 }
928 mMutex.unlock_shared();
929 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700930}
931
932Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
933 const IComposerClient::Rect& frame) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400934 Error error = Error::NONE;
935 mMutex.lock_shared();
936 if (auto writer = getWriter(display)) {
937 writer->get().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
938 translate<AidlRect>(frame));
939 } else {
940 error = Error::BAD_DISPLAY;
941 }
942 mMutex.unlock_shared();
943 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700944}
945
946Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400947 Error error = Error::NONE;
948 mMutex.lock_shared();
949 if (auto writer = getWriter(display)) {
950 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
951 alpha);
952 } else {
953 error = Error::BAD_DISPLAY;
954 }
955 mMutex.unlock_shared();
956 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700957}
958
959Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
960 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400961 Error error = Error::NONE;
962 mMutex.lock_shared();
963 if (auto writer = getWriter(display)) {
964 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
965 stream);
966 } else {
967 error = Error::BAD_DISPLAY;
968 }
969 mMutex.unlock_shared();
970 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700971}
972
973Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
974 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400975 Error error = Error::NONE;
976 mMutex.lock_shared();
977 if (auto writer = getWriter(display)) {
978 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
979 translate<AidlFRect>(crop));
980 } else {
981 error = Error::BAD_DISPLAY;
982 }
983 mMutex.unlock_shared();
984 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700985}
986
987Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400988 Error error = Error::NONE;
989 mMutex.lock_shared();
990 if (auto writer = getWriter(display)) {
991 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
992 translate<AidlTransform>(transform));
993 } else {
994 error = Error::BAD_DISPLAY;
995 }
996 mMutex.unlock_shared();
997 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700998}
999
1000Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
1001 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001002 Error error = Error::NONE;
1003 mMutex.lock_shared();
1004 if (auto writer = getWriter(display)) {
1005 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
1006 translate<AidlRect>(visible));
1007 } else {
1008 error = Error::BAD_DISPLAY;
1009 }
1010 mMutex.unlock_shared();
1011 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001012}
1013
1014Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001015 Error error = Error::NONE;
1016 mMutex.lock_shared();
1017 if (auto writer = getWriter(display)) {
1018 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
1019 } else {
1020 error = Error::BAD_DISPLAY;
1021 }
1022 mMutex.unlock_shared();
1023 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001024}
1025
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001026Error AidlComposer::execute(Display display) {
1027 auto writer = getWriter(display);
1028 auto reader = getReader(display);
1029 if (!writer || !reader) {
1030 return Error::BAD_DISPLAY;
1031 }
1032
1033 const auto& commands = writer->get().getPendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -08001034 if (commands.empty()) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001035 writer->get().reset();
Ady Abrahame7385f72021-09-05 00:54:25 -07001036 return Error::NONE;
1037 }
1038
Ady Abrahamde792782021-12-20 10:00:49 -08001039 { // scope for results
1040 std::vector<CommandResultPayload> results;
1041 auto status = mAidlComposerClient->executeCommands(commands, &results);
1042 if (!status.isOk()) {
1043 ALOGE("executeCommands failed %s", status.getDescription().c_str());
1044 return static_cast<Error>(status.getServiceSpecificError());
1045 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001046
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001047 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -08001048 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001049 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -08001050 Error error = Error::NONE;
1051 for (const auto& cmdErr : commandErrors) {
1052 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1053 if (index < 0 || index >= commands.size()) {
1054 ALOGE("invalid command index %zu", index);
1055 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001056 }
1057
Ady Abrahama6388c02021-11-11 21:11:51 -08001058 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001059 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1060 error = translate<Error>(cmdErr.errorCode);
1061 } else {
1062 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1063 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001064 }
1065 }
1066
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001067 writer->get().reset();
Ady Abrahame7385f72021-09-05 00:54:25 -07001068
1069 return error;
1070}
1071
1072Error AidlComposer::setLayerPerFrameMetadata(
1073 Display display, Layer layer,
1074 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001075 Error error = Error::NONE;
1076 mMutex.lock_shared();
1077 if (auto writer = getWriter(display)) {
1078 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1079 translate<int64_t>(layer),
1080 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1081 } else {
1082 error = Error::BAD_DISPLAY;
1083 }
1084 mMutex.unlock_shared();
1085 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001086}
1087
1088std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1089 Display display) {
1090 std::vector<AidlPerFrameMetadataKey> keys;
1091 const auto status =
1092 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1093 if (!status.isOk()) {
1094 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1095 return {};
1096 }
1097 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1098}
1099
1100Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1101 std::vector<RenderIntent>* outRenderIntents) {
1102 std::vector<AidlRenderIntent> renderIntents;
1103 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1104 translate<AidlColorMode>(colorMode),
1105 &renderIntents);
1106 if (!status.isOk()) {
1107 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1108 return static_cast<Error>(status.getServiceSpecificError());
1109 }
1110 *outRenderIntents = translate<RenderIntent>(renderIntents);
1111 return Error::NONE;
1112}
1113
1114Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1115 std::vector<float> matrix;
1116 const auto status =
1117 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1118 &matrix);
1119 if (!status.isOk()) {
1120 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1121 return static_cast<Error>(status.getServiceSpecificError());
1122 }
1123 *outMatrix = makeMat4(matrix);
1124 return Error::NONE;
1125}
1126
1127Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1128 std::vector<uint8_t>* outData) {
1129 AidlDisplayIdentification displayIdentification;
1130 const auto status =
1131 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1132 &displayIdentification);
1133 if (!status.isOk()) {
1134 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1135 return static_cast<Error>(status.getServiceSpecificError());
1136 }
1137
1138 *outPort = static_cast<uint8_t>(displayIdentification.port);
1139 *outData = displayIdentification.data;
1140
1141 return Error::NONE;
1142}
1143
1144Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001145 Error error = Error::NONE;
1146 mMutex.lock_shared();
1147 if (auto writer = getWriter(display)) {
1148 writer->get().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1149 matrix);
1150 } else {
1151 error = Error::BAD_DISPLAY;
1152 }
1153 mMutex.unlock_shared();
1154 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001155}
1156
1157Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1158 Dataspace* outDataspace,
1159 uint8_t* outComponentMask) {
1160 if (!outFormat || !outDataspace || !outComponentMask) {
1161 return Error::BAD_PARAMETER;
1162 }
1163
1164 AidlDisplayContentSamplingAttributes attributes;
1165 const auto status =
1166 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1167 &attributes);
1168 if (!status.isOk()) {
1169 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1170 return static_cast<Error>(status.getServiceSpecificError());
1171 }
1172
1173 *outFormat = translate<PixelFormat>(attributes.format);
1174 *outDataspace = translate<Dataspace>(attributes.dataspace);
1175 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1176 return Error::NONE;
1177}
1178
1179Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1180 uint8_t componentMask, uint64_t maxFrames) {
1181 const auto status =
1182 mAidlComposerClient
1183 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1184 static_cast<AidlFormatColorComponent>(
1185 componentMask),
1186 static_cast<int64_t>(maxFrames));
1187 if (!status.isOk()) {
1188 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1189 return static_cast<Error>(status.getServiceSpecificError());
1190 }
1191 return Error::NONE;
1192}
1193
1194Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1195 uint64_t timestamp, DisplayedFrameStats* outStats) {
1196 if (!outStats) {
1197 return Error::BAD_PARAMETER;
1198 }
1199
1200 AidlDisplayContentSample sample;
1201 const auto status =
1202 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1203 static_cast<int64_t>(maxFrames),
1204 static_cast<int64_t>(timestamp),
1205 &sample);
1206 if (!status.isOk()) {
1207 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1208 return static_cast<Error>(status.getServiceSpecificError());
1209 }
1210 *outStats = translate<DisplayedFrameStats>(sample);
1211 return Error::NONE;
1212}
1213
1214Error AidlComposer::setLayerPerFrameMetadataBlobs(
1215 Display display, Layer layer,
1216 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001217 Error error = Error::NONE;
1218 mMutex.lock_shared();
1219 if (auto writer = getWriter(display)) {
1220 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1221 translate<int64_t>(layer),
1222 translate<AidlPerFrameMetadataBlob>(metadata));
1223 } else {
1224 error = Error::BAD_DISPLAY;
1225 }
1226 mMutex.unlock_shared();
1227 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001228}
1229
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001230Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001231 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001232 Error error = Error::NONE;
1233 mMutex.lock_shared();
1234 if (auto writer = getWriter(display)) {
1235 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001236
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001237 if (options.applyImmediately) {
1238 error = execute(display);
1239 mMutex.unlock_shared();
1240 return error;
1241 }
1242 } else {
1243 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001244 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001245 mMutex.unlock_shared();
1246 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001247}
1248
1249Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001250 std::vector<AidlDisplayCapability>* outCapabilities) {
1251 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1252 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001253 if (!status.isOk()) {
1254 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001255 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001256 return static_cast<Error>(status.getServiceSpecificError());
1257 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001258 return Error::NONE;
1259}
1260
1261V2_4::Error AidlComposer::getDisplayConnectionType(
1262 Display display, IComposerClient::DisplayConnectionType* outType) {
1263 AidlDisplayConnectionType type;
1264 const auto status =
1265 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1266 if (!status.isOk()) {
1267 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1268 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1269 }
1270 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1271 return V2_4::Error::NONE;
1272}
1273
1274V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1275 int32_t vsyncPeriod;
1276 const auto status =
1277 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1278 if (!status.isOk()) {
1279 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1280 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1281 }
1282 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1283 return V2_4::Error::NONE;
1284}
1285
1286V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1287 Display display, Config config,
1288 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1289 VsyncPeriodChangeTimeline* outTimeline) {
1290 AidlVsyncPeriodChangeTimeline timeline;
1291 const auto status =
1292 mAidlComposerClient
1293 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1294 translate<int32_t>(config),
1295 translate<AidlVsyncPeriodChangeConstraints>(
1296 vsyncPeriodChangeConstraints),
1297 &timeline);
1298 if (!status.isOk()) {
1299 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1300 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1301 }
1302 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1303 return V2_4::Error::NONE;
1304}
1305
1306V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1307 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1308 if (!status.isOk()) {
1309 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1310 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1311 }
1312 return V2_4::Error::NONE;
1313}
1314
1315V2_4::Error AidlComposer::getSupportedContentTypes(
1316 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1317 std::vector<AidlContentType> types;
1318 const auto status =
1319 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1320 if (!status.isOk()) {
1321 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1322 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1323 }
1324 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1325 return V2_4::Error::NONE;
1326}
1327
1328V2_4::Error AidlComposer::setContentType(Display display,
1329 IComposerClient::ContentType contentType) {
1330 const auto status =
1331 mAidlComposerClient->setContentType(translate<int64_t>(display),
1332 translate<AidlContentType>(contentType));
1333 if (!status.isOk()) {
1334 ALOGE("setContentType failed %s", status.getDescription().c_str());
1335 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1336 }
1337 return V2_4::Error::NONE;
1338}
1339
Ady Abraham3f976752021-12-20 16:17:50 -08001340V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1341 const std::vector<uint8_t>&) {
1342 // There are no users for this API. See b/209691612.
1343 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001344}
1345
1346V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001347 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
1348 // There are no users for this API. See b/209691612.
1349 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001350}
1351
Kriti Dang7defaf32021-11-15 11:55:43 +01001352Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1353 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1354 translate<int32_t>(config));
1355 if (!status.isOk()) {
1356 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1357 return static_cast<Error>(status.getServiceSpecificError());
1358 }
1359 return Error::NONE;
1360}
1361
1362Error AidlComposer::clearBootDisplayConfig(Display display) {
1363 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1364 if (!status.isOk()) {
1365 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1366 return static_cast<Error>(status.getServiceSpecificError());
1367 }
1368 return Error::NONE;
1369}
1370
1371Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1372 int32_t displayConfig;
1373 const auto status =
1374 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1375 &displayConfig);
1376 if (!status.isOk()) {
1377 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1378 return static_cast<Error>(status.getServiceSpecificError());
1379 }
1380 *config = translate<uint32_t>(displayConfig);
1381 return Error::NONE;
1382}
1383
Ady Abrahame7385f72021-09-05 00:54:25 -07001384Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001385 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001386 Error error = Error::NONE;
1387 mMutex.lock_shared();
1388 if (auto reader = getReader(display)) {
1389 *outClientTargetProperty =
1390 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1391 } else {
1392 error = Error::BAD_DISPLAY;
1393 }
1394 mMutex.unlock_shared();
1395 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001396}
1397
Alec Mouri6da0e272022-02-07 12:45:57 -08001398Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001399 Error error = Error::NONE;
1400 mMutex.lock_shared();
1401 if (auto writer = getWriter(display)) {
1402 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1403 brightness);
1404 } else {
1405 error = Error::BAD_DISPLAY;
1406 }
1407 mMutex.unlock_shared();
1408 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001409}
1410
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001411Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1412 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001413 Error error = Error::NONE;
1414 mMutex.lock_shared();
1415 if (auto writer = getWriter(display)) {
1416 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1417 translate<AidlRect>(blocking));
1418 } else {
1419 error = Error::BAD_DISPLAY;
1420 }
1421 mMutex.unlock_shared();
1422 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001423}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001424
1425Error AidlComposer::getDisplayDecorationSupport(Display display,
1426 std::optional<DisplayDecorationSupport>* support) {
1427 const auto status =
1428 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1429 if (!status.isOk()) {
1430 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1431 support->reset();
1432 return static_cast<Error>(status.getServiceSpecificError());
1433 }
1434 return Error::NONE;
1435}
ramindani32cf0602022-03-02 02:30:29 +00001436
1437Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1438 const auto status =
1439 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1440 translate<int32_t>(timeout.count()));
1441 if (!status.isOk()) {
1442 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1443 return static_cast<Error>(status.getServiceSpecificError());
1444 }
1445 return Error::NONE;
1446}
1447
ramindani06e518e2022-03-14 18:47:53 +00001448Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1449 AidlTransform* outDisplayOrientation) {
1450 const auto status =
1451 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1452 outDisplayOrientation);
1453 if (!status.isOk()) {
1454 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1455 return static_cast<Error>(status.getServiceSpecificError());
1456 }
1457 return Error::NONE;
1458}
1459
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001460ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1461 REQUIRES_SHARED(mMutex) {
1462 return mWriters.get(display);
1463}
1464
1465ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1466 REQUIRES_SHARED(mMutex) {
1467 if (mSingleReader) {
1468 display = translate<Display>(kSingleReaderKey);
1469 }
1470 return mReaders.get(display);
1471}
1472
1473void AidlComposer::removeDisplay(Display display) {
1474 mMutex.lock();
1475 bool wasErased = mWriters.erase(display);
1476 ALOGW_IF(!wasErased,
1477 "Attempting to remove writer for display %" PRId64 " which is not connected",
1478 translate<int64_t>(display));
1479 if (!mSingleReader) {
1480 removeReader(display);
1481 }
1482 mMutex.unlock();
1483}
1484
1485void AidlComposer::onHotplugDisconnect(Display display) {
1486 removeDisplay(display);
1487}
1488
1489bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
1490 const auto displayId = translate<int64_t>(display);
1491 std::vector<AidlDisplayCapability> capabilities;
1492 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1493 if (!status.isOk()) {
1494 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1495 return false;
1496 }
1497 return std::find(capabilities.begin(), capabilities.end(),
1498 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
1499}
1500
1501void AidlComposer::addReader(Display display) {
1502 const auto displayId = translate<int64_t>(display);
1503 std::optional<int64_t> displayOpt;
1504 if (displayId != kSingleReaderKey) {
1505 displayOpt.emplace(displayId);
1506 }
1507 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1508 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1509 displayId);
1510}
1511
1512void AidlComposer::removeReader(Display display) {
1513 bool wasErased = mReaders.erase(display);
1514 ALOGW_IF(!wasErased,
1515 "Attempting to remove reader for display %" PRId64 " which is not connected",
1516 translate<int64_t>(display));
1517}
1518
1519void AidlComposer::addDisplay(Display display) {
1520 const auto displayId = translate<int64_t>(display);
1521 mMutex.lock();
1522 auto [it, added] = mWriters.try_emplace(display, displayId);
1523 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1524 displayId);
1525 if (mSingleReader) {
1526 if (hasMultiThreadedPresentSupport(display)) {
1527 mSingleReader = false;
1528 removeReader(translate<Display>(kSingleReaderKey));
1529 // Note that this includes the new display.
1530 for (const auto& [existingDisplay, _] : mWriters) {
1531 addReader(existingDisplay);
1532 }
1533 }
1534 } else {
1535 addReader(display);
1536 }
1537 mMutex.unlock();
1538}
1539
1540void AidlComposer::onHotplugConnect(Display display) {
1541 addDisplay(display);
1542}
Ady Abrahame7385f72021-09-05 00:54:25 -07001543} // namespace Hwc2
1544} // namespace android