blob: 800e36db4859073cc5e80ae1550518a1b82d599b [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>
26#include <log/log.h>
27#include <utils/Trace.h>
28
29#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
30
31#include <algorithm>
32#include <cinttypes>
33
Yichi Chen3401b562022-01-17 15:42:35 +080034#include "HWC2.h"
35
Ady Abrahame7385f72021-09-05 00:54:25 -070036namespace android {
37
38using hardware::hidl_handle;
39using hardware::hidl_vec;
40using hardware::Return;
41
42using aidl::android::hardware::graphics::composer3::BnComposerCallback;
43using aidl::android::hardware::graphics::composer3::Capability;
Alec Mouri85065692022-03-18 00:58:26 +000044using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Ady Abrahame7385f72021-09-05 00:54:25 -070045using aidl::android::hardware::graphics::composer3::PowerMode;
46using aidl::android::hardware::graphics::composer3::VirtualDisplay;
47
Ady Abraham42977362021-12-07 21:04:49 -080048using aidl::android::hardware::graphics::composer3::CommandResultPayload;
Ady Abrahama6388c02021-11-11 21:11:51 -080049
Ady Abrahame7385f72021-09-05 00:54:25 -070050using AidlColorMode = aidl::android::hardware::graphics::composer3::ColorMode;
51using AidlContentType = aidl::android::hardware::graphics::composer3::ContentType;
52using AidlDisplayIdentification =
53 aidl::android::hardware::graphics::composer3::DisplayIdentification;
54using AidlDisplayContentSample = aidl::android::hardware::graphics::composer3::DisplayContentSample;
55using AidlDisplayAttribute = aidl::android::hardware::graphics::composer3::DisplayAttribute;
56using AidlDisplayCapability = aidl::android::hardware::graphics::composer3::DisplayCapability;
Ady Abrahame7385f72021-09-05 00:54:25 -070057using AidlHdrCapabilities = aidl::android::hardware::graphics::composer3::HdrCapabilities;
Sally Qi0cbd08b2022-08-17 12:12:28 -070058using AidlOverlayProperties = aidl::android::hardware::graphics::composer3::OverlayProperties;
Ady Abrahame7385f72021-09-05 00:54:25 -070059using AidlPerFrameMetadata = aidl::android::hardware::graphics::composer3::PerFrameMetadata;
60using AidlPerFrameMetadataKey = aidl::android::hardware::graphics::composer3::PerFrameMetadataKey;
61using AidlPerFrameMetadataBlob = aidl::android::hardware::graphics::composer3::PerFrameMetadataBlob;
62using AidlRenderIntent = aidl::android::hardware::graphics::composer3::RenderIntent;
63using AidlVsyncPeriodChangeConstraints =
64 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeConstraints;
65using AidlVsyncPeriodChangeTimeline =
66 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline;
Ady Abrahame7385f72021-09-05 00:54:25 -070067using AidlDisplayContentSamplingAttributes =
68 aidl::android::hardware::graphics::composer3::DisplayContentSamplingAttributes;
69using AidlFormatColorComponent = aidl::android::hardware::graphics::composer3::FormatColorComponent;
70using AidlDisplayConnectionType =
71 aidl::android::hardware::graphics::composer3::DisplayConnectionType;
Ady Abrahame7385f72021-09-05 00:54:25 -070072
73using AidlColorTransform = aidl::android::hardware::graphics::common::ColorTransform;
74using AidlDataspace = aidl::android::hardware::graphics::common::Dataspace;
75using AidlFRect = aidl::android::hardware::graphics::common::FRect;
76using AidlRect = aidl::android::hardware::graphics::common::Rect;
77using AidlTransform = aidl::android::hardware::graphics::common::Transform;
78
79namespace Hwc2 {
80
81namespace {
82
83template <typename To, typename From>
84To translate(From x) {
85 return static_cast<To>(x);
86}
87
88template <typename To, typename From>
89std::vector<To> translate(const std::vector<From>& in) {
90 std::vector<To> out;
91 out.reserve(in.size());
92 std::transform(in.begin(), in.end(), std::back_inserter(out),
93 [](From x) { return translate<To>(x); });
94 return out;
95}
96
97template <>
98AidlRect translate(IComposerClient::Rect x) {
99 return AidlRect{
100 .left = x.left,
101 .top = x.top,
102 .right = x.right,
103 .bottom = x.bottom,
104 };
105}
106
107template <>
108AidlFRect translate(IComposerClient::FRect x) {
109 return AidlFRect{
110 .left = x.left,
111 .top = x.top,
112 .right = x.right,
113 .bottom = x.bottom,
114 };
115}
116
117template <>
Ady Abrahame7385f72021-09-05 00:54:25 -0700118AidlPerFrameMetadataBlob translate(IComposerClient::PerFrameMetadataBlob x) {
119 AidlPerFrameMetadataBlob blob;
120 blob.key = translate<AidlPerFrameMetadataKey>(x.key),
Long Linga4628782022-02-18 13:44:26 -0800121 std::copy(x.blob.begin(), x.blob.end(), std::inserter(blob.blob, blob.blob.end()));
Ady Abrahame7385f72021-09-05 00:54:25 -0700122 return blob;
123}
124
125template <>
126AidlPerFrameMetadata translate(IComposerClient::PerFrameMetadata x) {
127 return AidlPerFrameMetadata{
128 .key = translate<AidlPerFrameMetadataKey>(x.key),
129 .value = x.value,
130 };
131}
132
133template <>
134DisplayedFrameStats translate(AidlDisplayContentSample x) {
135 return DisplayedFrameStats{
136 .numFrames = static_cast<uint64_t>(x.frameCount),
137 .component_0_sample = translate<uint64_t>(x.sampleComponent0),
138 .component_1_sample = translate<uint64_t>(x.sampleComponent1),
139 .component_2_sample = translate<uint64_t>(x.sampleComponent2),
140 .component_3_sample = translate<uint64_t>(x.sampleComponent3),
141 };
142}
143
144template <>
145AidlVsyncPeriodChangeConstraints translate(IComposerClient::VsyncPeriodChangeConstraints x) {
146 return AidlVsyncPeriodChangeConstraints{
147 .desiredTimeNanos = x.desiredTimeNanos,
148 .seamlessRequired = x.seamlessRequired,
149 };
150}
151
152template <>
153VsyncPeriodChangeTimeline translate(AidlVsyncPeriodChangeTimeline x) {
154 return VsyncPeriodChangeTimeline{
155 .newVsyncAppliedTimeNanos = x.newVsyncAppliedTimeNanos,
156 .refreshRequired = x.refreshRequired,
157 .refreshTimeNanos = x.refreshTimeNanos,
158 };
159}
Ady Abrahame7385f72021-09-05 00:54:25 -0700160mat4 makeMat4(std::vector<float> in) {
161 return mat4(static_cast<const float*>(in.data()));
162}
163
164} // namespace
165
166class AidlIComposerCallbackWrapper : public BnComposerCallback {
167public:
Yichi Chen3401b562022-01-17 15:42:35 +0800168 AidlIComposerCallbackWrapper(HWC2::ComposerCallback& callback) : mCallback(callback) {}
Ady Abrahame7385f72021-09-05 00:54:25 -0700169
170 ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override {
171 const auto connection = in_connected ? V2_4::IComposerCallback::Connection::CONNECTED
172 : V2_4::IComposerCallback::Connection::DISCONNECTED;
Yichi Chen3401b562022-01-17 15:42:35 +0800173 mCallback.onComposerHalHotplug(translate<Display>(in_display), connection);
Ady Abrahame7385f72021-09-05 00:54:25 -0700174 return ::ndk::ScopedAStatus::ok();
175 }
176
177 ::ndk::ScopedAStatus onRefresh(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800178 mCallback.onComposerHalRefresh(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700179 return ::ndk::ScopedAStatus::ok();
180 }
Yichi Chen3401b562022-01-17 15:42:35 +0800181
Ady Abrahame7385f72021-09-05 00:54:25 -0700182 ::ndk::ScopedAStatus onSeamlessPossible(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800183 mCallback.onComposerHalSeamlessPossible(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700184 return ::ndk::ScopedAStatus::ok();
185 }
Yichi Chen3401b562022-01-17 15:42:35 +0800186
Ady Abrahame7385f72021-09-05 00:54:25 -0700187 ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
188 int32_t in_vsyncPeriodNanos) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800189 mCallback.onComposerHalVsync(translate<Display>(in_display), in_timestamp,
190 static_cast<uint32_t>(in_vsyncPeriodNanos));
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 onVsyncPeriodTimingChanged(
195 int64_t in_display, const AidlVsyncPeriodChangeTimeline& in_updatedTimeline) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800196 mCallback.onComposerHalVsyncPeriodTimingChanged(translate<Display>(in_display),
197 translate<V2_4::VsyncPeriodChangeTimeline>(
198 in_updatedTimeline));
199 return ::ndk::ScopedAStatus::ok();
200 }
201
202 ::ndk::ScopedAStatus onVsyncIdle(int64_t in_display) override {
203 mCallback.onComposerHalVsyncIdle(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700204 return ::ndk::ScopedAStatus::ok();
205 }
206
207private:
Yichi Chen3401b562022-01-17 15:42:35 +0800208 HWC2::ComposerCallback& mCallback;
Ady Abrahame7385f72021-09-05 00:54:25 -0700209};
210
Ady Abraham9fc28052021-10-14 17:21:38 -0700211std::string AidlComposer::instance(const std::string& serviceName) {
212 return std::string(AidlIComposer::descriptor) + "/" + serviceName;
213}
214
215bool AidlComposer::isDeclared(const std::string& serviceName) {
216 return AServiceManager_isDeclared(instance(serviceName).c_str());
217}
Ady Abrahame7385f72021-09-05 00:54:25 -0700218
Ady Abrahama6388c02021-11-11 21:11:51 -0800219AidlComposer::AidlComposer(const std::string& serviceName) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700220 // This only waits if the service is actually declared
Ady Abraham9fc28052021-10-14 17:21:38 -0700221 mAidlComposer = AidlIComposer::fromBinder(
222 ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
Ady Abrahame7385f72021-09-05 00:54:25 -0700223 if (!mAidlComposer) {
224 LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
225 return;
226 }
227
228 if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
229 LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
230 return;
231 }
232
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400233 addReader(translate<Display>(kSingleReaderKey));
234
Ady Abrahame7385f72021-09-05 00:54:25 -0700235 ALOGI("Loaded AIDL composer3 HAL service");
236}
237
238AidlComposer::~AidlComposer() = default;
239
Ady Abraham4d211cf2021-12-14 16:19:03 -0800240bool AidlComposer::isSupported(OptionalFeature feature) const {
241 switch (feature) {
242 case OptionalFeature::RefreshRateSwitching:
Ady Abraham43065bd2021-12-10 17:22:15 -0800243 case OptionalFeature::ExpectedPresentTime:
Alec Mouricdf16792021-12-10 13:16:06 -0800244 case OptionalFeature::DisplayBrightnessCommand:
ramindani32cf0602022-03-02 02:30:29 +0000245 case OptionalFeature::KernelIdleTimer:
ramindani06e518e2022-03-14 18:47:53 +0000246 case OptionalFeature::PhysicalDisplayOrientation:
Ady Abraham4d211cf2021-12-14 16:19:03 -0800247 return true;
248 }
249}
250
Ady Abrahamde549d42022-01-26 19:19:17 -0800251std::vector<Capability> AidlComposer::getCapabilities() {
Ady Abrahame7385f72021-09-05 00:54:25 -0700252 std::vector<Capability> capabilities;
253 const auto status = mAidlComposer->getCapabilities(&capabilities);
254 if (!status.isOk()) {
255 ALOGE("getCapabilities failed %s", status.getDescription().c_str());
256 return {};
257 }
Ady Abrahamde549d42022-01-26 19:19:17 -0800258 return capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700259}
260
261std::string AidlComposer::dumpDebugInfo() {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800262 int pipefds[2];
263 int result = pipe(pipefds);
264 if (result < 0) {
265 ALOGE("dumpDebugInfo: pipe failed: %s", strerror(errno));
Ady Abrahame7385f72021-09-05 00:54:25 -0700266 return {};
267 }
Ady Abrahamc4acf512022-02-18 17:11:59 -0800268
269 std::string str;
yihsing.shen58847c52022-09-23 15:39:30 +0800270 // Use other thread to read pipe to prevent
271 // pipe is full, making HWC be blocked in writing.
272 std::thread t([&]() {
273 base::ReadFdToString(pipefds[0], &str);
274 });
Ady Abrahamc4acf512022-02-18 17:11:59 -0800275 const auto status = mAidlComposer->dump(pipefds[1], /*args*/ nullptr, /*numArgs*/ 0);
276 // Close the write-end of the pipe to make sure that when reading from the
277 // read-end we will get eof instead of blocking forever
278 close(pipefds[1]);
279
yihsing.shen58847c52022-09-23 15:39:30 +0800280 if (status != STATUS_OK) {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800281 ALOGE("dumpDebugInfo: dump failed: %d", status);
282 }
283
yihsing.shen58847c52022-09-23 15:39:30 +0800284 t.join();
Ady Abrahamc4acf512022-02-18 17:11:59 -0800285 close(pipefds[0]);
286 return str;
Ady Abrahame7385f72021-09-05 00:54:25 -0700287}
288
Yichi Chen3401b562022-01-17 15:42:35 +0800289void AidlComposer::registerCallback(HWC2::ComposerCallback& callback) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700290 if (mAidlComposerCallback) {
291 ALOGE("Callback already registered");
292 }
Yichi Chen3401b562022-01-17 15:42:35 +0800293
Ady Abraham9fc28052021-10-14 17:21:38 -0700294 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
Ady Abrahame7385f72021-09-05 00:54:25 -0700295 AIBinder_setMinSchedulerPolicy(mAidlComposerCallback->asBinder().get(), SCHED_FIFO, 2);
296
297 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
298 if (!status.isOk()) {
299 ALOGE("registerCallback failed %s", status.getDescription().c_str());
300 }
301}
302
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400303void AidlComposer::resetCommands(Display display) {
304 mMutex.lock_shared();
305 if (auto writer = getWriter(display)) {
306 writer->get().reset();
307 }
308 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700309}
310
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400311Error AidlComposer::executeCommands(Display display) {
312 mMutex.lock_shared();
313 auto error = execute(display);
314 mMutex.unlock_shared();
315 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700316}
317
318uint32_t AidlComposer::getMaxVirtualDisplayCount() {
319 int32_t count = 0;
320 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
321 if (!status.isOk()) {
322 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
323 return 0;
324 }
325 return static_cast<uint32_t>(count);
326}
327
328Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
329 Display* outDisplay) {
330 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
331 const int32_t bufferSlotCount = 1;
332 VirtualDisplay virtualDisplay;
333 const auto status =
334 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
335 static_cast<int32_t>(height),
336 static_cast<AidlPixelFormat>(*format),
337 bufferSlotCount, &virtualDisplay);
338
339 if (!status.isOk()) {
340 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
341 return static_cast<Error>(status.getServiceSpecificError());
342 }
343
344 *outDisplay = translate<Display>(virtualDisplay.display);
345 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400346 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700347 return Error::NONE;
348}
349
350Error AidlComposer::destroyVirtualDisplay(Display display) {
351 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
352 if (!status.isOk()) {
353 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
354 return static_cast<Error>(status.getServiceSpecificError());
355 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400356 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700357 return Error::NONE;
358}
359
360Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400361 Error error = Error::NONE;
362 mMutex.lock_shared();
363 if (auto writer = getWriter(display)) {
364 writer->get().acceptDisplayChanges(translate<int64_t>(display));
365 } else {
366 error = Error::BAD_DISPLAY;
367 }
368 mMutex.unlock_shared();
369 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700370}
371
372Error AidlComposer::createLayer(Display display, Layer* outLayer) {
373 int64_t layer;
374 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
375 kMaxLayerBufferCount, &layer);
376 if (!status.isOk()) {
377 ALOGE("createLayer failed %s", status.getDescription().c_str());
378 return static_cast<Error>(status.getServiceSpecificError());
379 }
380
381 *outLayer = translate<Layer>(layer);
382 return Error::NONE;
383}
384
385Error AidlComposer::destroyLayer(Display display, Layer layer) {
386 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
387 translate<int64_t>(layer));
388 if (!status.isOk()) {
389 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
390 return static_cast<Error>(status.getServiceSpecificError());
391 }
392 return Error::NONE;
393}
394
395Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
396 int32_t config;
397 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
398 if (!status.isOk()) {
399 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
400 return static_cast<Error>(status.getServiceSpecificError());
401 }
402 *outConfig = translate<Config>(config);
403 return Error::NONE;
404}
405
406Error AidlComposer::getChangedCompositionTypes(
407 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500408 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400409 std::vector<ChangedCompositionLayer> changedLayers;
410 Error error = Error::NONE;
411 {
412 mMutex.lock_shared();
413 if (auto reader = getReader(display)) {
414 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
415 } else {
416 error = Error::BAD_DISPLAY;
417 }
418 mMutex.unlock_shared();
419 }
Ady Abrahamde792782021-12-20 10:00:49 -0800420 outLayers->reserve(changedLayers.size());
421 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800422
Ady Abrahamde792782021-12-20 10:00:49 -0800423 for (const auto& layer : changedLayers) {
424 outLayers->emplace_back(translate<Layer>(layer.layer));
425 outTypes->emplace_back(layer.composition);
426 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400427 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700428}
429
430Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
431 std::vector<AidlColorMode> modes;
432 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
433 if (!status.isOk()) {
434 ALOGE("getColorModes failed %s", status.getDescription().c_str());
435 return static_cast<Error>(status.getServiceSpecificError());
436 }
437 *outModes = translate<ColorMode>(modes);
438 return Error::NONE;
439}
440
441Error AidlComposer::getDisplayAttribute(Display display, Config config,
442 IComposerClient::Attribute attribute, int32_t* outValue) {
443 const auto status =
444 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
445 translate<int32_t>(config),
446 static_cast<AidlDisplayAttribute>(attribute),
447 outValue);
448 if (!status.isOk()) {
449 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
450 return static_cast<Error>(status.getServiceSpecificError());
451 }
452 return Error::NONE;
453}
454
455Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
456 std::vector<int32_t> configs;
457 const auto status =
458 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
459 if (!status.isOk()) {
460 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
461 return static_cast<Error>(status.getServiceSpecificError());
462 }
463 *outConfigs = translate<Config>(configs);
464 return Error::NONE;
465}
466
467Error AidlComposer::getDisplayName(Display display, std::string* outName) {
468 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
469 if (!status.isOk()) {
470 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
471 return static_cast<Error>(status.getServiceSpecificError());
472 }
473 return Error::NONE;
474}
475
476Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
477 std::vector<Layer>* outLayers,
478 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400479 Error error = Error::NONE;
480 DisplayRequest displayRequests;
481 {
482 mMutex.lock_shared();
483 if (auto reader = getReader(display)) {
484 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
485 } else {
486 error = Error::BAD_DISPLAY;
487 }
488 mMutex.unlock_shared();
489 }
Ady Abrahamde792782021-12-20 10:00:49 -0800490 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
491 outLayers->reserve(displayRequests.layerRequests.size());
492 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
493
494 for (const auto& layer : displayRequests.layerRequests) {
495 outLayers->emplace_back(translate<Layer>(layer.layer));
496 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
497 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400498 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700499}
500
501Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800502 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700503 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800504 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700505 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800506 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700507 return static_cast<Error>(status.getServiceSpecificError());
508 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800509 *outSupport = std::find(capabilities.begin(), capabilities.end(),
510 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700511 return Error::NONE;
512}
513
ramindani32cf0602022-03-02 02:30:29 +0000514Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
515 std::vector<AidlDisplayCapability> capabilities;
516 const auto status =
517 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
518 if (!status.isOk()) {
519 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
520 return static_cast<Error>(status.getServiceSpecificError());
521 }
522 *outSupport = std::find(capabilities.begin(), capabilities.end(),
523 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
524 return Error::NONE;
525}
526
Ady Abrahame7385f72021-09-05 00:54:25 -0700527Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
528 float* outMaxLuminance, float* outMaxAverageLuminance,
529 float* outMinLuminance) {
530 AidlHdrCapabilities capabilities;
531 const auto status =
532 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
533 if (!status.isOk()) {
534 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
535 return static_cast<Error>(status.getServiceSpecificError());
536 }
537
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100538 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700539 *outMaxLuminance = capabilities.maxLuminance;
540 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
541 *outMinLuminance = capabilities.minLuminance;
542 return Error::NONE;
543}
544
Sally Qibb866c12022-10-17 11:31:20 -0700545Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
546 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
547 if (!status.isOk()) {
548 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
549 return static_cast<Error>(status.getServiceSpecificError());
550 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700551 return Error::NONE;
552}
553
Ady Abrahame7385f72021-09-05 00:54:25 -0700554Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
555 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400556 Error error = Error::NONE;
557 std::vector<ReleaseFences::Layer> fences;
558 {
559 mMutex.lock_shared();
560 if (auto reader = getReader(display)) {
561 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
562 } else {
563 error = Error::BAD_DISPLAY;
564 }
565 mMutex.unlock_shared();
566 }
Ady Abrahamde792782021-12-20 10:00:49 -0800567 outLayers->reserve(fences.size());
568 outReleaseFences->reserve(fences.size());
569
570 for (auto& fence : fences) {
571 outLayers->emplace_back(translate<Layer>(fence.layer));
572 // take ownership
573 const int fenceOwner = fence.fence.get();
574 *fence.fence.getR() = -1;
575 outReleaseFences->emplace_back(fenceOwner);
576 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400577 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700578}
579
580Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
581 ATRACE_NAME("HwcPresentDisplay");
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400582 Error error = Error::NONE;
583 mMutex.lock_shared();
584 auto writer = getWriter(display);
585 auto reader = getReader(display);
586 if (writer && reader) {
587 writer->get().presentDisplay(translate<int64_t>(display));
588 error = execute(display);
589 } else {
590 error = Error::BAD_DISPLAY;
591 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700592
Ady Abrahame7385f72021-09-05 00:54:25 -0700593 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400594 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700595 return error;
596 }
597
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400598 auto fence = reader->get().takePresentFence(translate<int64_t>(display));
599 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800600 // take ownership
601 *outPresentFence = fence.get();
602 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700603 return Error::NONE;
604}
605
606Error AidlComposer::setActiveConfig(Display display, Config config) {
607 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
608 translate<int32_t>(config));
609 if (!status.isOk()) {
610 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
611 return static_cast<Error>(status.getServiceSpecificError());
612 }
613 return Error::NONE;
614}
615
616Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
617 int acquireFence, Dataspace dataspace,
618 const std::vector<IComposerClient::Rect>& damage) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700619 const native_handle_t* handle = nullptr;
620 if (target.get()) {
621 handle = target->getNativeBuffer()->handle;
622 }
623
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400624 Error error = Error::NONE;
625 mMutex.lock_shared();
626 if (auto writer = getWriter(display)) {
627 writer->get()
628 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
629 translate<aidl::android::hardware::graphics::common::Dataspace>(
630 dataspace),
631 translate<AidlRect>(damage));
632 } else {
633 error = Error::BAD_DISPLAY;
634 }
635 mMutex.unlock_shared();
636 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700637}
638
639Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
640 const auto status =
641 mAidlComposerClient->setColorMode(translate<int64_t>(display),
642 translate<AidlColorMode>(mode),
643 translate<AidlRenderIntent>(renderIntent));
644 if (!status.isOk()) {
645 ALOGE("setColorMode failed %s", status.getDescription().c_str());
646 return static_cast<Error>(status.getServiceSpecificError());
647 }
648 return Error::NONE;
649}
650
Ady Abrahamdc011a92021-12-21 14:06:44 -0800651Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400652 auto error = Error::NONE;
653 mMutex.lock_shared();
654 if (auto writer = getWriter(display)) {
655 writer->get().setColorTransform(translate<int64_t>(display), matrix);
656 } else {
657 error = Error::BAD_DISPLAY;
658 }
659 mMutex.unlock_shared();
660 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700661}
662
663Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
664 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400665 auto error = Error::NONE;
666 mMutex.lock_shared();
667 if (auto writer = getWriter(display)) {
668 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
669 } else {
670 error = Error::BAD_DISPLAY;
671 }
672 mMutex.unlock_shared();
673 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700674}
675
676Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
677 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
678 translate<PowerMode>(mode));
679 if (!status.isOk()) {
680 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
681 return static_cast<Error>(status.getServiceSpecificError());
682 }
683 return Error::NONE;
684}
685
686Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
687 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
688 const auto status =
689 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
690 if (!status.isOk()) {
691 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
692 return static_cast<Error>(status.getServiceSpecificError());
693 }
694 return Error::NONE;
695}
696
697Error AidlComposer::setClientTargetSlotCount(Display display) {
698 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
699 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
700 bufferSlotCount);
701 if (!status.isOk()) {
702 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
703 return static_cast<Error>(status.getServiceSpecificError());
704 }
705 return Error::NONE;
706}
707
Ady Abraham43065bd2021-12-10 17:22:15 -0800708Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
709 uint32_t* outNumTypes, uint32_t* outNumRequests) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700710 ATRACE_NAME("HwcValidateDisplay");
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400711 const auto displayId = translate<int64_t>(display);
712 Error error = Error::NONE;
713 mMutex.lock_shared();
714 auto writer = getWriter(display);
715 auto reader = getReader(display);
716 if (writer && reader) {
717 writer->get().validateDisplay(displayId, ClockMonotonicTimestamp{expectedPresentTime});
718 error = execute(display);
719 } else {
720 error = Error::BAD_DISPLAY;
721 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700722
Ady Abrahame7385f72021-09-05 00:54:25 -0700723 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400724 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700725 return error;
726 }
727
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400728 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700729
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400730 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700731 return Error::NONE;
732}
733
Ady Abraham43065bd2021-12-10 17:22:15 -0800734Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
735 uint32_t* outNumTypes, uint32_t* outNumRequests,
736 int* outPresentFence, uint32_t* state) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700737 ATRACE_NAME("HwcPresentOrValidateDisplay");
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400738 const auto displayId = translate<int64_t>(display);
739 Error error = Error::NONE;
740 mMutex.lock_shared();
741 auto writer = getWriter(display);
742 auto reader = getReader(display);
743 if (writer && reader) {
744 writer->get().presentOrvalidateDisplay(displayId,
745 ClockMonotonicTimestamp{expectedPresentTime});
746 error = execute(display);
747 } else {
748 error = Error::BAD_DISPLAY;
749 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700750
Ady Abrahame7385f72021-09-05 00:54:25 -0700751 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400752 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700753 return error;
754 }
755
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400756 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800757 if (!result.has_value()) {
758 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400759 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800760 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700761 }
762
Ady Abrahamde792782021-12-20 10:00:49 -0800763 *state = translate<uint32_t>(*result);
764
765 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400766 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800767 // take ownership
768 *outPresentFence = fence.get();
769 *fence.getR() = -1;
770 }
771
772 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400773 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700774 }
775
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400776 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700777 return Error::NONE;
778}
779
780Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400781 Error error = Error::NONE;
782 mMutex.lock_shared();
783 if (auto writer = getWriter(display)) {
784 writer->get().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
785 x, y);
786 } else {
787 error = Error::BAD_DISPLAY;
788 }
789 mMutex.unlock_shared();
790 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700791}
792
793Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
794 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700795 const native_handle_t* handle = nullptr;
796 if (buffer.get()) {
797 handle = buffer->getNativeBuffer()->handle;
798 }
799
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400800 Error error = Error::NONE;
801 mMutex.lock_shared();
802 if (auto writer = getWriter(display)) {
803 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
804 handle, acquireFence);
805 } else {
806 error = Error::BAD_DISPLAY;
807 }
808 mMutex.unlock_shared();
809 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700810}
811
812Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
813 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400814 Error error = Error::NONE;
815 mMutex.lock_shared();
816 if (auto writer = getWriter(display)) {
817 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
818 translate<AidlRect>(damage));
819 } else {
820 error = Error::BAD_DISPLAY;
821 }
822 mMutex.unlock_shared();
823 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700824}
825
826Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
827 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400828 Error error = Error::NONE;
829 mMutex.lock_shared();
830 if (auto writer = getWriter(display)) {
831 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
832 translate<BlendMode>(mode));
833 } else {
834 error = Error::BAD_DISPLAY;
835 }
836 mMutex.unlock_shared();
837 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700838}
839
Ady Abraham6e60b142022-01-06 18:10:35 -0800840Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400841 Error error = Error::NONE;
842 mMutex.lock_shared();
843 if (auto writer = getWriter(display)) {
844 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
845 } else {
846 error = Error::BAD_DISPLAY;
847 }
848 mMutex.unlock_shared();
849 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700850}
851
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500852Error AidlComposer::setLayerCompositionType(
853 Display display, Layer layer,
854 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400855 Error error = Error::NONE;
856 mMutex.lock_shared();
857 if (auto writer = getWriter(display)) {
858 writer->get().setLayerCompositionType(translate<int64_t>(display),
859 translate<int64_t>(layer), type);
860 } else {
861 error = Error::BAD_DISPLAY;
862 }
863 mMutex.unlock_shared();
864 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700865}
866
867Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400868 Error error = Error::NONE;
869 mMutex.lock_shared();
870 if (auto writer = getWriter(display)) {
871 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
872 translate<AidlDataspace>(dataspace));
873 } else {
874 error = Error::BAD_DISPLAY;
875 }
876 mMutex.unlock_shared();
877 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700878}
879
880Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
881 const IComposerClient::Rect& frame) {
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().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
886 translate<AidlRect>(frame));
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::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400895 Error error = Error::NONE;
896 mMutex.lock_shared();
897 if (auto writer = getWriter(display)) {
898 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
899 alpha);
900 } else {
901 error = Error::BAD_DISPLAY;
902 }
903 mMutex.unlock_shared();
904 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700905}
906
907Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
908 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400909 Error error = Error::NONE;
910 mMutex.lock_shared();
911 if (auto writer = getWriter(display)) {
912 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
913 stream);
914 } else {
915 error = Error::BAD_DISPLAY;
916 }
917 mMutex.unlock_shared();
918 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700919}
920
921Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
922 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400923 Error error = Error::NONE;
924 mMutex.lock_shared();
925 if (auto writer = getWriter(display)) {
926 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
927 translate<AidlFRect>(crop));
928 } else {
929 error = Error::BAD_DISPLAY;
930 }
931 mMutex.unlock_shared();
932 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700933}
934
935Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400936 Error error = Error::NONE;
937 mMutex.lock_shared();
938 if (auto writer = getWriter(display)) {
939 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
940 translate<AidlTransform>(transform));
941 } else {
942 error = Error::BAD_DISPLAY;
943 }
944 mMutex.unlock_shared();
945 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700946}
947
948Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
949 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400950 Error error = Error::NONE;
951 mMutex.lock_shared();
952 if (auto writer = getWriter(display)) {
953 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
954 translate<AidlRect>(visible));
955 } else {
956 error = Error::BAD_DISPLAY;
957 }
958 mMutex.unlock_shared();
959 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700960}
961
962Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400963 Error error = Error::NONE;
964 mMutex.lock_shared();
965 if (auto writer = getWriter(display)) {
966 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
967 } else {
968 error = Error::BAD_DISPLAY;
969 }
970 mMutex.unlock_shared();
971 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700972}
973
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400974Error AidlComposer::execute(Display display) {
975 auto writer = getWriter(display);
976 auto reader = getReader(display);
977 if (!writer || !reader) {
978 return Error::BAD_DISPLAY;
979 }
980
981 const auto& commands = writer->get().getPendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -0800982 if (commands.empty()) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400983 writer->get().reset();
Ady Abrahame7385f72021-09-05 00:54:25 -0700984 return Error::NONE;
985 }
986
Ady Abrahamde792782021-12-20 10:00:49 -0800987 { // scope for results
988 std::vector<CommandResultPayload> results;
989 auto status = mAidlComposerClient->executeCommands(commands, &results);
990 if (!status.isOk()) {
991 ALOGE("executeCommands failed %s", status.getDescription().c_str());
992 return static_cast<Error>(status.getServiceSpecificError());
993 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700994
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400995 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -0800996 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400997 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -0800998 Error error = Error::NONE;
999 for (const auto& cmdErr : commandErrors) {
1000 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1001 if (index < 0 || index >= commands.size()) {
1002 ALOGE("invalid command index %zu", index);
1003 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001004 }
1005
Ady Abrahama6388c02021-11-11 21:11:51 -08001006 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001007 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1008 error = translate<Error>(cmdErr.errorCode);
1009 } else {
1010 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1011 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001012 }
1013 }
1014
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001015 writer->get().reset();
Ady Abrahame7385f72021-09-05 00:54:25 -07001016
1017 return error;
1018}
1019
1020Error AidlComposer::setLayerPerFrameMetadata(
1021 Display display, Layer layer,
1022 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001023 Error error = Error::NONE;
1024 mMutex.lock_shared();
1025 if (auto writer = getWriter(display)) {
1026 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1027 translate<int64_t>(layer),
1028 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1029 } else {
1030 error = Error::BAD_DISPLAY;
1031 }
1032 mMutex.unlock_shared();
1033 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001034}
1035
1036std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1037 Display display) {
1038 std::vector<AidlPerFrameMetadataKey> keys;
1039 const auto status =
1040 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1041 if (!status.isOk()) {
1042 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1043 return {};
1044 }
1045 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1046}
1047
1048Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1049 std::vector<RenderIntent>* outRenderIntents) {
1050 std::vector<AidlRenderIntent> renderIntents;
1051 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1052 translate<AidlColorMode>(colorMode),
1053 &renderIntents);
1054 if (!status.isOk()) {
1055 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1056 return static_cast<Error>(status.getServiceSpecificError());
1057 }
1058 *outRenderIntents = translate<RenderIntent>(renderIntents);
1059 return Error::NONE;
1060}
1061
1062Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1063 std::vector<float> matrix;
1064 const auto status =
1065 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1066 &matrix);
1067 if (!status.isOk()) {
1068 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1069 return static_cast<Error>(status.getServiceSpecificError());
1070 }
1071 *outMatrix = makeMat4(matrix);
1072 return Error::NONE;
1073}
1074
1075Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1076 std::vector<uint8_t>* outData) {
1077 AidlDisplayIdentification displayIdentification;
1078 const auto status =
1079 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1080 &displayIdentification);
1081 if (!status.isOk()) {
1082 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1083 return static_cast<Error>(status.getServiceSpecificError());
1084 }
1085
1086 *outPort = static_cast<uint8_t>(displayIdentification.port);
1087 *outData = displayIdentification.data;
1088
1089 return Error::NONE;
1090}
1091
1092Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001093 Error error = Error::NONE;
1094 mMutex.lock_shared();
1095 if (auto writer = getWriter(display)) {
1096 writer->get().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1097 matrix);
1098 } else {
1099 error = Error::BAD_DISPLAY;
1100 }
1101 mMutex.unlock_shared();
1102 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001103}
1104
1105Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1106 Dataspace* outDataspace,
1107 uint8_t* outComponentMask) {
1108 if (!outFormat || !outDataspace || !outComponentMask) {
1109 return Error::BAD_PARAMETER;
1110 }
1111
1112 AidlDisplayContentSamplingAttributes attributes;
1113 const auto status =
1114 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1115 &attributes);
1116 if (!status.isOk()) {
1117 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1118 return static_cast<Error>(status.getServiceSpecificError());
1119 }
1120
1121 *outFormat = translate<PixelFormat>(attributes.format);
1122 *outDataspace = translate<Dataspace>(attributes.dataspace);
1123 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1124 return Error::NONE;
1125}
1126
1127Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1128 uint8_t componentMask, uint64_t maxFrames) {
1129 const auto status =
1130 mAidlComposerClient
1131 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1132 static_cast<AidlFormatColorComponent>(
1133 componentMask),
1134 static_cast<int64_t>(maxFrames));
1135 if (!status.isOk()) {
1136 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1137 return static_cast<Error>(status.getServiceSpecificError());
1138 }
1139 return Error::NONE;
1140}
1141
1142Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1143 uint64_t timestamp, DisplayedFrameStats* outStats) {
1144 if (!outStats) {
1145 return Error::BAD_PARAMETER;
1146 }
1147
1148 AidlDisplayContentSample sample;
1149 const auto status =
1150 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1151 static_cast<int64_t>(maxFrames),
1152 static_cast<int64_t>(timestamp),
1153 &sample);
1154 if (!status.isOk()) {
1155 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1156 return static_cast<Error>(status.getServiceSpecificError());
1157 }
1158 *outStats = translate<DisplayedFrameStats>(sample);
1159 return Error::NONE;
1160}
1161
1162Error AidlComposer::setLayerPerFrameMetadataBlobs(
1163 Display display, Layer layer,
1164 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001165 Error error = Error::NONE;
1166 mMutex.lock_shared();
1167 if (auto writer = getWriter(display)) {
1168 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1169 translate<int64_t>(layer),
1170 translate<AidlPerFrameMetadataBlob>(metadata));
1171 } else {
1172 error = Error::BAD_DISPLAY;
1173 }
1174 mMutex.unlock_shared();
1175 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001176}
1177
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001178Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001179 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001180 Error error = Error::NONE;
1181 mMutex.lock_shared();
1182 if (auto writer = getWriter(display)) {
1183 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001184
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001185 if (options.applyImmediately) {
1186 error = execute(display);
1187 mMutex.unlock_shared();
1188 return error;
1189 }
1190 } else {
1191 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001192 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001193 mMutex.unlock_shared();
1194 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001195}
1196
1197Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001198 std::vector<AidlDisplayCapability>* outCapabilities) {
1199 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1200 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001201 if (!status.isOk()) {
1202 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001203 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001204 return static_cast<Error>(status.getServiceSpecificError());
1205 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001206 return Error::NONE;
1207}
1208
1209V2_4::Error AidlComposer::getDisplayConnectionType(
1210 Display display, IComposerClient::DisplayConnectionType* outType) {
1211 AidlDisplayConnectionType type;
1212 const auto status =
1213 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1214 if (!status.isOk()) {
1215 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1216 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1217 }
1218 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1219 return V2_4::Error::NONE;
1220}
1221
1222V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1223 int32_t vsyncPeriod;
1224 const auto status =
1225 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1226 if (!status.isOk()) {
1227 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1228 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1229 }
1230 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1231 return V2_4::Error::NONE;
1232}
1233
1234V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1235 Display display, Config config,
1236 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1237 VsyncPeriodChangeTimeline* outTimeline) {
1238 AidlVsyncPeriodChangeTimeline timeline;
1239 const auto status =
1240 mAidlComposerClient
1241 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1242 translate<int32_t>(config),
1243 translate<AidlVsyncPeriodChangeConstraints>(
1244 vsyncPeriodChangeConstraints),
1245 &timeline);
1246 if (!status.isOk()) {
1247 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1248 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1249 }
1250 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1251 return V2_4::Error::NONE;
1252}
1253
1254V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1255 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1256 if (!status.isOk()) {
1257 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1258 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1259 }
1260 return V2_4::Error::NONE;
1261}
1262
1263V2_4::Error AidlComposer::getSupportedContentTypes(
1264 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1265 std::vector<AidlContentType> types;
1266 const auto status =
1267 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1268 if (!status.isOk()) {
1269 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1270 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1271 }
1272 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1273 return V2_4::Error::NONE;
1274}
1275
1276V2_4::Error AidlComposer::setContentType(Display display,
1277 IComposerClient::ContentType contentType) {
1278 const auto status =
1279 mAidlComposerClient->setContentType(translate<int64_t>(display),
1280 translate<AidlContentType>(contentType));
1281 if (!status.isOk()) {
1282 ALOGE("setContentType failed %s", status.getDescription().c_str());
1283 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1284 }
1285 return V2_4::Error::NONE;
1286}
1287
Ady Abraham3f976752021-12-20 16:17:50 -08001288V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1289 const std::vector<uint8_t>&) {
1290 // There are no users for this API. See b/209691612.
1291 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001292}
1293
1294V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001295 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
1296 // There are no users for this API. See b/209691612.
1297 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001298}
1299
Kriti Dang7defaf32021-11-15 11:55:43 +01001300Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1301 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1302 translate<int32_t>(config));
1303 if (!status.isOk()) {
1304 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1305 return static_cast<Error>(status.getServiceSpecificError());
1306 }
1307 return Error::NONE;
1308}
1309
1310Error AidlComposer::clearBootDisplayConfig(Display display) {
1311 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1312 if (!status.isOk()) {
1313 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1314 return static_cast<Error>(status.getServiceSpecificError());
1315 }
1316 return Error::NONE;
1317}
1318
1319Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1320 int32_t displayConfig;
1321 const auto status =
1322 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1323 &displayConfig);
1324 if (!status.isOk()) {
1325 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1326 return static_cast<Error>(status.getServiceSpecificError());
1327 }
1328 *config = translate<uint32_t>(displayConfig);
1329 return Error::NONE;
1330}
1331
Ady Abrahame7385f72021-09-05 00:54:25 -07001332Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001333 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001334 Error error = Error::NONE;
1335 mMutex.lock_shared();
1336 if (auto reader = getReader(display)) {
1337 *outClientTargetProperty =
1338 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1339 } else {
1340 error = Error::BAD_DISPLAY;
1341 }
1342 mMutex.unlock_shared();
1343 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001344}
1345
Alec Mouri6da0e272022-02-07 12:45:57 -08001346Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001347 Error error = Error::NONE;
1348 mMutex.lock_shared();
1349 if (auto writer = getWriter(display)) {
1350 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1351 brightness);
1352 } else {
1353 error = Error::BAD_DISPLAY;
1354 }
1355 mMutex.unlock_shared();
1356 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001357}
1358
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001359Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1360 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001361 Error error = Error::NONE;
1362 mMutex.lock_shared();
1363 if (auto writer = getWriter(display)) {
1364 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1365 translate<AidlRect>(blocking));
1366 } else {
1367 error = Error::BAD_DISPLAY;
1368 }
1369 mMutex.unlock_shared();
1370 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001371}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001372
1373Error AidlComposer::getDisplayDecorationSupport(Display display,
1374 std::optional<DisplayDecorationSupport>* support) {
1375 const auto status =
1376 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1377 if (!status.isOk()) {
1378 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1379 support->reset();
1380 return static_cast<Error>(status.getServiceSpecificError());
1381 }
1382 return Error::NONE;
1383}
ramindani32cf0602022-03-02 02:30:29 +00001384
1385Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1386 const auto status =
1387 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1388 translate<int32_t>(timeout.count()));
1389 if (!status.isOk()) {
1390 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1391 return static_cast<Error>(status.getServiceSpecificError());
1392 }
1393 return Error::NONE;
1394}
1395
ramindani06e518e2022-03-14 18:47:53 +00001396Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1397 AidlTransform* outDisplayOrientation) {
1398 const auto status =
1399 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1400 outDisplayOrientation);
1401 if (!status.isOk()) {
1402 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1403 return static_cast<Error>(status.getServiceSpecificError());
1404 }
1405 return Error::NONE;
1406}
1407
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001408ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1409 REQUIRES_SHARED(mMutex) {
1410 return mWriters.get(display);
1411}
1412
1413ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1414 REQUIRES_SHARED(mMutex) {
1415 if (mSingleReader) {
1416 display = translate<Display>(kSingleReaderKey);
1417 }
1418 return mReaders.get(display);
1419}
1420
1421void AidlComposer::removeDisplay(Display display) {
1422 mMutex.lock();
1423 bool wasErased = mWriters.erase(display);
1424 ALOGW_IF(!wasErased,
1425 "Attempting to remove writer for display %" PRId64 " which is not connected",
1426 translate<int64_t>(display));
1427 if (!mSingleReader) {
1428 removeReader(display);
1429 }
1430 mMutex.unlock();
1431}
1432
1433void AidlComposer::onHotplugDisconnect(Display display) {
1434 removeDisplay(display);
1435}
1436
1437bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
1438 const auto displayId = translate<int64_t>(display);
1439 std::vector<AidlDisplayCapability> capabilities;
1440 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1441 if (!status.isOk()) {
1442 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1443 return false;
1444 }
1445 return std::find(capabilities.begin(), capabilities.end(),
1446 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
1447}
1448
1449void AidlComposer::addReader(Display display) {
1450 const auto displayId = translate<int64_t>(display);
1451 std::optional<int64_t> displayOpt;
1452 if (displayId != kSingleReaderKey) {
1453 displayOpt.emplace(displayId);
1454 }
1455 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1456 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1457 displayId);
1458}
1459
1460void AidlComposer::removeReader(Display display) {
1461 bool wasErased = mReaders.erase(display);
1462 ALOGW_IF(!wasErased,
1463 "Attempting to remove reader for display %" PRId64 " which is not connected",
1464 translate<int64_t>(display));
1465}
1466
1467void AidlComposer::addDisplay(Display display) {
1468 const auto displayId = translate<int64_t>(display);
1469 mMutex.lock();
1470 auto [it, added] = mWriters.try_emplace(display, displayId);
1471 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1472 displayId);
1473 if (mSingleReader) {
1474 if (hasMultiThreadedPresentSupport(display)) {
1475 mSingleReader = false;
1476 removeReader(translate<Display>(kSingleReaderKey));
1477 // Note that this includes the new display.
1478 for (const auto& [existingDisplay, _] : mWriters) {
1479 addReader(existingDisplay);
1480 }
1481 }
1482 } else {
1483 addReader(display);
1484 }
1485 mMutex.unlock();
1486}
1487
1488void AidlComposer::onHotplugConnect(Display display) {
1489 addDisplay(display);
1490}
Ady Abrahame7385f72021-09-05 00:54:25 -07001491} // namespace Hwc2
1492} // namespace android