blob: 544f445a064ecefab149df0f405743424f61f039 [file] [log] [blame]
Ady Abrahame7385f72021-09-05 00:54:25 -07001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "HwcComposer"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ady Abraham9fc28052021-10-14 17:21:38 -070021#include "AidlComposerHal.h"
Ady Abrahame7385f72021-09-05 00:54:25 -070022
Brian Lindahl5b0ffe02023-06-15 14:19:43 -060023#include <SurfaceFlingerProperties.h>
Ady Abrahamc4acf512022-02-18 17:11:59 -080024#include <android-base/file.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070025#include <android/binder_ibinder_platform.h>
26#include <android/binder_manager.h>
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -050027#include <gui/TraceUtils.h>
Ady Abrahame7385f72021-09-05 00:54:25 -070028#include <log/log.h>
29#include <utils/Trace.h>
30
31#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
32
33#include <algorithm>
34#include <cinttypes>
35
Yichi Chen3401b562022-01-17 15:42:35 +080036#include "HWC2.h"
37
Ady Abrahame7385f72021-09-05 00:54:25 -070038namespace android {
39
40using hardware::hidl_handle;
41using hardware::hidl_vec;
42using hardware::Return;
43
44using aidl::android::hardware::graphics::composer3::BnComposerCallback;
45using aidl::android::hardware::graphics::composer3::Capability;
Alec Mouri85065692022-03-18 00:58:26 +000046using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
Ady Abrahame7385f72021-09-05 00:54:25 -070047using aidl::android::hardware::graphics::composer3::PowerMode;
48using aidl::android::hardware::graphics::composer3::VirtualDisplay;
49
Ady Abraham42977362021-12-07 21:04:49 -080050using aidl::android::hardware::graphics::composer3::CommandResultPayload;
Ady Abrahama6388c02021-11-11 21:11:51 -080051
Ady Abrahame7385f72021-09-05 00:54:25 -070052using AidlColorMode = aidl::android::hardware::graphics::composer3::ColorMode;
53using AidlContentType = aidl::android::hardware::graphics::composer3::ContentType;
54using AidlDisplayIdentification =
55 aidl::android::hardware::graphics::composer3::DisplayIdentification;
56using AidlDisplayContentSample = aidl::android::hardware::graphics::composer3::DisplayContentSample;
57using AidlDisplayAttribute = aidl::android::hardware::graphics::composer3::DisplayAttribute;
58using AidlDisplayCapability = aidl::android::hardware::graphics::composer3::DisplayCapability;
Ady Abrahame7385f72021-09-05 00:54:25 -070059using AidlHdrCapabilities = aidl::android::hardware::graphics::composer3::HdrCapabilities;
Kriti Dang674b9372022-11-18 10:58:44 +010060using AidlHdrConversionCapability =
61 aidl::android::hardware::graphics::common::HdrConversionCapability;
62using AidlHdrConversionStrategy = aidl::android::hardware::graphics::common::HdrConversionStrategy;
Sally Qi0cbd08b2022-08-17 12:12:28 -070063using AidlOverlayProperties = aidl::android::hardware::graphics::composer3::OverlayProperties;
Ady Abrahame7385f72021-09-05 00:54:25 -070064using AidlPerFrameMetadata = aidl::android::hardware::graphics::composer3::PerFrameMetadata;
65using AidlPerFrameMetadataKey = aidl::android::hardware::graphics::composer3::PerFrameMetadataKey;
66using AidlPerFrameMetadataBlob = aidl::android::hardware::graphics::composer3::PerFrameMetadataBlob;
67using AidlRenderIntent = aidl::android::hardware::graphics::composer3::RenderIntent;
68using AidlVsyncPeriodChangeConstraints =
69 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeConstraints;
70using AidlVsyncPeriodChangeTimeline =
71 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline;
Ady Abrahame7385f72021-09-05 00:54:25 -070072using AidlDisplayContentSamplingAttributes =
73 aidl::android::hardware::graphics::composer3::DisplayContentSamplingAttributes;
74using AidlFormatColorComponent = aidl::android::hardware::graphics::composer3::FormatColorComponent;
75using AidlDisplayConnectionType =
76 aidl::android::hardware::graphics::composer3::DisplayConnectionType;
Ady Abrahame7385f72021-09-05 00:54:25 -070077
78using AidlColorTransform = aidl::android::hardware::graphics::common::ColorTransform;
79using AidlDataspace = aidl::android::hardware::graphics::common::Dataspace;
80using AidlFRect = aidl::android::hardware::graphics::common::FRect;
81using AidlRect = aidl::android::hardware::graphics::common::Rect;
82using AidlTransform = aidl::android::hardware::graphics::common::Transform;
83
84namespace Hwc2 {
85
86namespace {
87
88template <typename To, typename From>
89To translate(From x) {
90 return static_cast<To>(x);
91}
92
93template <typename To, typename From>
94std::vector<To> translate(const std::vector<From>& in) {
95 std::vector<To> out;
96 out.reserve(in.size());
97 std::transform(in.begin(), in.end(), std::back_inserter(out),
98 [](From x) { return translate<To>(x); });
99 return out;
100}
101
102template <>
103AidlRect translate(IComposerClient::Rect x) {
104 return AidlRect{
105 .left = x.left,
106 .top = x.top,
107 .right = x.right,
108 .bottom = x.bottom,
109 };
110}
111
112template <>
113AidlFRect translate(IComposerClient::FRect x) {
114 return AidlFRect{
115 .left = x.left,
116 .top = x.top,
117 .right = x.right,
118 .bottom = x.bottom,
119 };
120}
121
122template <>
Ady Abrahame7385f72021-09-05 00:54:25 -0700123AidlPerFrameMetadataBlob translate(IComposerClient::PerFrameMetadataBlob x) {
124 AidlPerFrameMetadataBlob blob;
125 blob.key = translate<AidlPerFrameMetadataKey>(x.key),
Long Linga4628782022-02-18 13:44:26 -0800126 std::copy(x.blob.begin(), x.blob.end(), std::inserter(blob.blob, blob.blob.end()));
Ady Abrahame7385f72021-09-05 00:54:25 -0700127 return blob;
128}
129
130template <>
131AidlPerFrameMetadata translate(IComposerClient::PerFrameMetadata x) {
132 return AidlPerFrameMetadata{
133 .key = translate<AidlPerFrameMetadataKey>(x.key),
134 .value = x.value,
135 };
136}
137
138template <>
139DisplayedFrameStats translate(AidlDisplayContentSample x) {
140 return DisplayedFrameStats{
141 .numFrames = static_cast<uint64_t>(x.frameCount),
142 .component_0_sample = translate<uint64_t>(x.sampleComponent0),
143 .component_1_sample = translate<uint64_t>(x.sampleComponent1),
144 .component_2_sample = translate<uint64_t>(x.sampleComponent2),
145 .component_3_sample = translate<uint64_t>(x.sampleComponent3),
146 };
147}
148
149template <>
150AidlVsyncPeriodChangeConstraints translate(IComposerClient::VsyncPeriodChangeConstraints x) {
151 return AidlVsyncPeriodChangeConstraints{
152 .desiredTimeNanos = x.desiredTimeNanos,
153 .seamlessRequired = x.seamlessRequired,
154 };
155}
156
157template <>
158VsyncPeriodChangeTimeline translate(AidlVsyncPeriodChangeTimeline x) {
159 return VsyncPeriodChangeTimeline{
160 .newVsyncAppliedTimeNanos = x.newVsyncAppliedTimeNanos,
161 .refreshRequired = x.refreshRequired,
162 .refreshTimeNanos = x.refreshTimeNanos,
163 };
164}
Ady Abrahame7385f72021-09-05 00:54:25 -0700165mat4 makeMat4(std::vector<float> in) {
166 return mat4(static_cast<const float*>(in.data()));
167}
168
169} // namespace
170
171class AidlIComposerCallbackWrapper : public BnComposerCallback {
172public:
Yichi Chen3401b562022-01-17 15:42:35 +0800173 AidlIComposerCallbackWrapper(HWC2::ComposerCallback& callback) : mCallback(callback) {}
Ady Abrahame7385f72021-09-05 00:54:25 -0700174
175 ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override {
176 const auto connection = in_connected ? V2_4::IComposerCallback::Connection::CONNECTED
177 : V2_4::IComposerCallback::Connection::DISCONNECTED;
Yichi Chen3401b562022-01-17 15:42:35 +0800178 mCallback.onComposerHalHotplug(translate<Display>(in_display), connection);
Ady Abrahame7385f72021-09-05 00:54:25 -0700179 return ::ndk::ScopedAStatus::ok();
180 }
181
182 ::ndk::ScopedAStatus onRefresh(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800183 mCallback.onComposerHalRefresh(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 onSeamlessPossible(int64_t in_display) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800188 mCallback.onComposerHalSeamlessPossible(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700189 return ::ndk::ScopedAStatus::ok();
190 }
Yichi Chen3401b562022-01-17 15:42:35 +0800191
Ady Abrahame7385f72021-09-05 00:54:25 -0700192 ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
193 int32_t in_vsyncPeriodNanos) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800194 mCallback.onComposerHalVsync(translate<Display>(in_display), in_timestamp,
195 static_cast<uint32_t>(in_vsyncPeriodNanos));
Ady Abrahame7385f72021-09-05 00:54:25 -0700196 return ::ndk::ScopedAStatus::ok();
197 }
Yichi Chen3401b562022-01-17 15:42:35 +0800198
Ady Abrahame7385f72021-09-05 00:54:25 -0700199 ::ndk::ScopedAStatus onVsyncPeriodTimingChanged(
200 int64_t in_display, const AidlVsyncPeriodChangeTimeline& in_updatedTimeline) override {
Yichi Chen3401b562022-01-17 15:42:35 +0800201 mCallback.onComposerHalVsyncPeriodTimingChanged(translate<Display>(in_display),
202 translate<V2_4::VsyncPeriodChangeTimeline>(
203 in_updatedTimeline));
204 return ::ndk::ScopedAStatus::ok();
205 }
206
207 ::ndk::ScopedAStatus onVsyncIdle(int64_t in_display) override {
208 mCallback.onComposerHalVsyncIdle(translate<Display>(in_display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700209 return ::ndk::ScopedAStatus::ok();
210 }
211
ramindani12bfe6b2023-02-03 13:29:19 -0800212 ::ndk::ScopedAStatus onRefreshRateChangedDebug(
213 const RefreshRateChangedDebugData& refreshRateChangedDebugData) override {
214 mCallback.onRefreshRateChangedDebug(refreshRateChangedDebugData);
215 return ::ndk::ScopedAStatus::ok();
216 }
217
Ady Abrahame7385f72021-09-05 00:54:25 -0700218private:
Yichi Chen3401b562022-01-17 15:42:35 +0800219 HWC2::ComposerCallback& mCallback;
Ady Abrahame7385f72021-09-05 00:54:25 -0700220};
221
Ady Abraham9fc28052021-10-14 17:21:38 -0700222std::string AidlComposer::instance(const std::string& serviceName) {
223 return std::string(AidlIComposer::descriptor) + "/" + serviceName;
224}
225
226bool AidlComposer::isDeclared(const std::string& serviceName) {
227 return AServiceManager_isDeclared(instance(serviceName).c_str());
228}
Ady Abrahame7385f72021-09-05 00:54:25 -0700229
Ady Abrahama6388c02021-11-11 21:11:51 -0800230AidlComposer::AidlComposer(const std::string& serviceName) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700231 // This only waits if the service is actually declared
Ady Abraham9fc28052021-10-14 17:21:38 -0700232 mAidlComposer = AidlIComposer::fromBinder(
233 ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
Ady Abrahame7385f72021-09-05 00:54:25 -0700234 if (!mAidlComposer) {
235 LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
236 return;
237 }
238
239 if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
240 LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
241 return;
242 }
243
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400244 addReader(translate<Display>(kSingleReaderKey));
245
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700246 // If unable to read interface version, then become backwards compatible.
ramindani0cd1d8d2023-06-13 13:43:23 -0700247 const auto status = mAidlComposerClient->getInterfaceVersion(&mComposerInterfaceVersion);
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700248 if (!status.isOk()) {
249 ALOGE("getInterfaceVersion for AidlComposer constructor failed %s",
250 status.getDescription().c_str());
251 }
ramindani0cd1d8d2023-06-13 13:43:23 -0700252
253 if (mComposerInterfaceVersion <= 1) {
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600254 if (sysprop::clear_slots_with_set_layer_buffer(false)) {
255 mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
256 GraphicBuffer::USAGE_HW_COMPOSER |
257 GraphicBuffer::USAGE_SW_READ_OFTEN |
258 GraphicBuffer::USAGE_SW_WRITE_OFTEN,
259 "AidlComposer");
260 if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
261 LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
262 return;
263 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700264 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700265 }
266
Ady Abrahame7385f72021-09-05 00:54:25 -0700267 ALOGI("Loaded AIDL composer3 HAL service");
268}
269
270AidlComposer::~AidlComposer() = default;
271
Ady Abraham4d211cf2021-12-14 16:19:03 -0800272bool AidlComposer::isSupported(OptionalFeature feature) const {
273 switch (feature) {
274 case OptionalFeature::RefreshRateSwitching:
Ady Abraham43065bd2021-12-10 17:22:15 -0800275 case OptionalFeature::ExpectedPresentTime:
Alec Mouricdf16792021-12-10 13:16:06 -0800276 case OptionalFeature::DisplayBrightnessCommand:
ramindani32cf0602022-03-02 02:30:29 +0000277 case OptionalFeature::KernelIdleTimer:
ramindani06e518e2022-03-14 18:47:53 +0000278 case OptionalFeature::PhysicalDisplayOrientation:
Ady Abraham4d211cf2021-12-14 16:19:03 -0800279 return true;
280 }
281}
282
ramindani0cd1d8d2023-06-13 13:43:23 -0700283bool AidlComposer::getDisplayConfigurationsSupported() const {
284 return mComposerInterfaceVersion >= 3;
285}
286
Ady Abrahamde549d42022-01-26 19:19:17 -0800287std::vector<Capability> AidlComposer::getCapabilities() {
Ady Abrahame7385f72021-09-05 00:54:25 -0700288 std::vector<Capability> capabilities;
289 const auto status = mAidlComposer->getCapabilities(&capabilities);
290 if (!status.isOk()) {
291 ALOGE("getCapabilities failed %s", status.getDescription().c_str());
292 return {};
293 }
Ady Abrahamde549d42022-01-26 19:19:17 -0800294 return capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700295}
296
297std::string AidlComposer::dumpDebugInfo() {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800298 int pipefds[2];
299 int result = pipe(pipefds);
300 if (result < 0) {
301 ALOGE("dumpDebugInfo: pipe failed: %s", strerror(errno));
Ady Abrahame7385f72021-09-05 00:54:25 -0700302 return {};
303 }
Ady Abrahamc4acf512022-02-18 17:11:59 -0800304
305 std::string str;
yihsing.shen58847c52022-09-23 15:39:30 +0800306 // Use other thread to read pipe to prevent
307 // pipe is full, making HWC be blocked in writing.
308 std::thread t([&]() {
309 base::ReadFdToString(pipefds[0], &str);
310 });
Ady Abrahamc4acf512022-02-18 17:11:59 -0800311 const auto status = mAidlComposer->dump(pipefds[1], /*args*/ nullptr, /*numArgs*/ 0);
312 // Close the write-end of the pipe to make sure that when reading from the
313 // read-end we will get eof instead of blocking forever
314 close(pipefds[1]);
315
yihsing.shen58847c52022-09-23 15:39:30 +0800316 if (status != STATUS_OK) {
Ady Abrahamc4acf512022-02-18 17:11:59 -0800317 ALOGE("dumpDebugInfo: dump failed: %d", status);
318 }
319
yihsing.shen58847c52022-09-23 15:39:30 +0800320 t.join();
Ady Abrahamc4acf512022-02-18 17:11:59 -0800321 close(pipefds[0]);
322 return str;
Ady Abrahame7385f72021-09-05 00:54:25 -0700323}
324
Yichi Chen3401b562022-01-17 15:42:35 +0800325void AidlComposer::registerCallback(HWC2::ComposerCallback& callback) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700326 if (mAidlComposerCallback) {
327 ALOGE("Callback already registered");
328 }
Yichi Chen3401b562022-01-17 15:42:35 +0800329
Ady Abraham9fc28052021-10-14 17:21:38 -0700330 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
Ady Abrahame7385f72021-09-05 00:54:25 -0700331 AIBinder_setMinSchedulerPolicy(mAidlComposerCallback->asBinder().get(), SCHED_FIFO, 2);
332
333 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
334 if (!status.isOk()) {
335 ALOGE("registerCallback failed %s", status.getDescription().c_str());
336 }
337}
338
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400339Error AidlComposer::executeCommands(Display display) {
340 mMutex.lock_shared();
341 auto error = execute(display);
342 mMutex.unlock_shared();
343 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700344}
345
346uint32_t AidlComposer::getMaxVirtualDisplayCount() {
347 int32_t count = 0;
348 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
349 if (!status.isOk()) {
350 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
351 return 0;
352 }
353 return static_cast<uint32_t>(count);
354}
355
356Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
357 Display* outDisplay) {
358 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
359 const int32_t bufferSlotCount = 1;
360 VirtualDisplay virtualDisplay;
361 const auto status =
362 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
363 static_cast<int32_t>(height),
364 static_cast<AidlPixelFormat>(*format),
365 bufferSlotCount, &virtualDisplay);
366
367 if (!status.isOk()) {
368 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
369 return static_cast<Error>(status.getServiceSpecificError());
370 }
371
372 *outDisplay = translate<Display>(virtualDisplay.display);
373 *format = static_cast<PixelFormat>(virtualDisplay.format);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400374 addDisplay(translate<Display>(virtualDisplay.display));
Ady Abrahame7385f72021-09-05 00:54:25 -0700375 return Error::NONE;
376}
377
378Error AidlComposer::destroyVirtualDisplay(Display display) {
379 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
380 if (!status.isOk()) {
381 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
382 return static_cast<Error>(status.getServiceSpecificError());
383 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400384 removeDisplay(display);
Ady Abrahame7385f72021-09-05 00:54:25 -0700385 return Error::NONE;
386}
387
388Error AidlComposer::acceptDisplayChanges(Display display) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400389 Error error = Error::NONE;
390 mMutex.lock_shared();
391 if (auto writer = getWriter(display)) {
392 writer->get().acceptDisplayChanges(translate<int64_t>(display));
393 } else {
394 error = Error::BAD_DISPLAY;
395 }
396 mMutex.unlock_shared();
397 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700398}
399
400Error AidlComposer::createLayer(Display display, Layer* outLayer) {
401 int64_t layer;
402 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
403 kMaxLayerBufferCount, &layer);
404 if (!status.isOk()) {
405 ALOGE("createLayer failed %s", status.getDescription().c_str());
406 return static_cast<Error>(status.getServiceSpecificError());
407 }
408
409 *outLayer = translate<Layer>(layer);
410 return Error::NONE;
411}
412
413Error AidlComposer::destroyLayer(Display display, Layer layer) {
414 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
415 translate<int64_t>(layer));
416 if (!status.isOk()) {
417 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
418 return static_cast<Error>(status.getServiceSpecificError());
419 }
420 return Error::NONE;
421}
422
423Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
424 int32_t config;
425 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
426 if (!status.isOk()) {
427 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
428 return static_cast<Error>(status.getServiceSpecificError());
429 }
430 *outConfig = translate<Config>(config);
431 return Error::NONE;
432}
433
434Error AidlComposer::getChangedCompositionTypes(
435 Display display, std::vector<Layer>* outLayers,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500436 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400437 std::vector<ChangedCompositionLayer> changedLayers;
438 Error error = Error::NONE;
439 {
440 mMutex.lock_shared();
441 if (auto reader = getReader(display)) {
442 changedLayers = reader->get().takeChangedCompositionTypes(translate<int64_t>(display));
443 } else {
444 error = Error::BAD_DISPLAY;
445 }
446 mMutex.unlock_shared();
447 }
Ady Abrahamde792782021-12-20 10:00:49 -0800448 outLayers->reserve(changedLayers.size());
449 outTypes->reserve(changedLayers.size());
Ady Abrahama6388c02021-11-11 21:11:51 -0800450
Ady Abrahamde792782021-12-20 10:00:49 -0800451 for (const auto& layer : changedLayers) {
452 outLayers->emplace_back(translate<Layer>(layer.layer));
453 outTypes->emplace_back(layer.composition);
454 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400455 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700456}
457
458Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
459 std::vector<AidlColorMode> modes;
460 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
461 if (!status.isOk()) {
462 ALOGE("getColorModes failed %s", status.getDescription().c_str());
463 return static_cast<Error>(status.getServiceSpecificError());
464 }
465 *outModes = translate<ColorMode>(modes);
466 return Error::NONE;
467}
468
469Error AidlComposer::getDisplayAttribute(Display display, Config config,
470 IComposerClient::Attribute attribute, int32_t* outValue) {
471 const auto status =
472 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
473 translate<int32_t>(config),
474 static_cast<AidlDisplayAttribute>(attribute),
475 outValue);
476 if (!status.isOk()) {
477 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
478 return static_cast<Error>(status.getServiceSpecificError());
479 }
480 return Error::NONE;
481}
482
483Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
484 std::vector<int32_t> configs;
485 const auto status =
486 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
487 if (!status.isOk()) {
488 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
489 return static_cast<Error>(status.getServiceSpecificError());
490 }
491 *outConfigs = translate<Config>(configs);
492 return Error::NONE;
493}
494
ramindani263a3f12023-07-18 20:44:49 -0700495Error AidlComposer::getDisplayConfigurations(Display display, int32_t maxFrameIntervalNs,
ramindani0cd1d8d2023-06-13 13:43:23 -0700496 std::vector<DisplayConfiguration>* outConfigs) {
497 const auto status =
ramindani263a3f12023-07-18 20:44:49 -0700498 mAidlComposerClient->getDisplayConfigurations(translate<int64_t>(display),
499 maxFrameIntervalNs, outConfigs);
ramindani0cd1d8d2023-06-13 13:43:23 -0700500 if (!status.isOk()) {
501 ALOGE("getDisplayConfigurations failed %s", status.getDescription().c_str());
502 return static_cast<Error>(status.getServiceSpecificError());
503 }
504
505 return Error::NONE;
506}
507
Ady Abrahame7385f72021-09-05 00:54:25 -0700508Error AidlComposer::getDisplayName(Display display, std::string* outName) {
509 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
510 if (!status.isOk()) {
511 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
512 return static_cast<Error>(status.getServiceSpecificError());
513 }
514 return Error::NONE;
515}
516
517Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
518 std::vector<Layer>* outLayers,
519 std::vector<uint32_t>* outLayerRequestMasks) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400520 Error error = Error::NONE;
521 DisplayRequest displayRequests;
522 {
523 mMutex.lock_shared();
524 if (auto reader = getReader(display)) {
525 displayRequests = reader->get().takeDisplayRequests(translate<int64_t>(display));
526 } else {
527 error = Error::BAD_DISPLAY;
528 }
529 mMutex.unlock_shared();
530 }
Ady Abrahamde792782021-12-20 10:00:49 -0800531 *outDisplayRequestMask = translate<uint32_t>(displayRequests.mask);
532 outLayers->reserve(displayRequests.layerRequests.size());
533 outLayerRequestMasks->reserve(displayRequests.layerRequests.size());
534
535 for (const auto& layer : displayRequests.layerRequests) {
536 outLayers->emplace_back(translate<Layer>(layer.layer));
537 outLayerRequestMasks->emplace_back(translate<uint32_t>(layer.mask));
538 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400539 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700540}
541
542Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800543 std::vector<AidlDisplayCapability> capabilities;
Ady Abrahame7385f72021-09-05 00:54:25 -0700544 const auto status =
Ady Abraham33b92b92021-12-08 18:30:27 -0800545 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -0700546 if (!status.isOk()) {
Ady Abraham33b92b92021-12-08 18:30:27 -0800547 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Ady Abrahame7385f72021-09-05 00:54:25 -0700548 return static_cast<Error>(status.getServiceSpecificError());
549 }
Ady Abraham33b92b92021-12-08 18:30:27 -0800550 *outSupport = std::find(capabilities.begin(), capabilities.end(),
551 AidlDisplayCapability::DOZE) != capabilities.end();
Ady Abrahame7385f72021-09-05 00:54:25 -0700552 return Error::NONE;
553}
554
ramindani32cf0602022-03-02 02:30:29 +0000555Error AidlComposer::hasDisplayIdleTimerCapability(Display display, bool* outSupport) {
556 std::vector<AidlDisplayCapability> capabilities;
557 const auto status =
558 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
559 if (!status.isOk()) {
560 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
561 return static_cast<Error>(status.getServiceSpecificError());
562 }
563 *outSupport = std::find(capabilities.begin(), capabilities.end(),
564 AidlDisplayCapability::DISPLAY_IDLE_TIMER) != capabilities.end();
565 return Error::NONE;
566}
567
Ady Abrahame7385f72021-09-05 00:54:25 -0700568Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
569 float* outMaxLuminance, float* outMaxAverageLuminance,
570 float* outMinLuminance) {
571 AidlHdrCapabilities capabilities;
572 const auto status =
573 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
574 if (!status.isOk()) {
575 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
576 return static_cast<Error>(status.getServiceSpecificError());
577 }
578
Marc Kassisbdf7e4b2022-11-04 17:26:48 +0100579 *outTypes = capabilities.types;
Ady Abrahame7385f72021-09-05 00:54:25 -0700580 *outMaxLuminance = capabilities.maxLuminance;
581 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
582 *outMinLuminance = capabilities.minLuminance;
583 return Error::NONE;
584}
585
Sally Qibb866c12022-10-17 11:31:20 -0700586Error AidlComposer::getOverlaySupport(AidlOverlayProperties* outProperties) {
587 const auto status = mAidlComposerClient->getOverlaySupport(outProperties);
588 if (!status.isOk()) {
589 ALOGE("getOverlaySupport failed %s", status.getDescription().c_str());
590 return static_cast<Error>(status.getServiceSpecificError());
591 }
Sally Qi0cbd08b2022-08-17 12:12:28 -0700592 return Error::NONE;
593}
594
Ady Abrahame7385f72021-09-05 00:54:25 -0700595Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
596 std::vector<int>* outReleaseFences) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400597 Error error = Error::NONE;
598 std::vector<ReleaseFences::Layer> fences;
599 {
600 mMutex.lock_shared();
601 if (auto reader = getReader(display)) {
602 fences = reader->get().takeReleaseFences(translate<int64_t>(display));
603 } else {
604 error = Error::BAD_DISPLAY;
605 }
606 mMutex.unlock_shared();
607 }
Ady Abrahamde792782021-12-20 10:00:49 -0800608 outLayers->reserve(fences.size());
609 outReleaseFences->reserve(fences.size());
610
611 for (auto& fence : fences) {
612 outLayers->emplace_back(translate<Layer>(fence.layer));
613 // take ownership
614 const int fenceOwner = fence.fence.get();
615 *fence.fence.getR() = -1;
616 outReleaseFences->emplace_back(fenceOwner);
617 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400618 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700619}
620
621Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500622 const auto displayId = translate<int64_t>(display);
623 ATRACE_FORMAT("HwcPresentDisplay %" PRId64, displayId);
624
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400625 Error error = Error::NONE;
626 mMutex.lock_shared();
627 auto writer = getWriter(display);
628 auto reader = getReader(display);
629 if (writer && reader) {
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500630 writer->get().presentDisplay(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400631 error = execute(display);
632 } else {
633 error = Error::BAD_DISPLAY;
634 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700635
Ady Abrahame7385f72021-09-05 00:54:25 -0700636 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400637 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700638 return error;
639 }
640
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500641 auto fence = reader->get().takePresentFence(displayId);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400642 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800643 // take ownership
644 *outPresentFence = fence.get();
645 *fence.getR() = -1;
Ady Abrahame7385f72021-09-05 00:54:25 -0700646 return Error::NONE;
647}
648
649Error AidlComposer::setActiveConfig(Display display, Config config) {
650 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
651 translate<int32_t>(config));
652 if (!status.isOk()) {
653 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
654 return static_cast<Error>(status.getServiceSpecificError());
655 }
656 return Error::NONE;
657}
658
659Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
660 int acquireFence, Dataspace dataspace,
661 const std::vector<IComposerClient::Rect>& damage) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700662 const native_handle_t* handle = nullptr;
663 if (target.get()) {
664 handle = target->getNativeBuffer()->handle;
665 }
666
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400667 Error error = Error::NONE;
668 mMutex.lock_shared();
669 if (auto writer = getWriter(display)) {
670 writer->get()
671 .setClientTarget(translate<int64_t>(display), slot, handle, acquireFence,
672 translate<aidl::android::hardware::graphics::common::Dataspace>(
673 dataspace),
674 translate<AidlRect>(damage));
675 } else {
676 error = Error::BAD_DISPLAY;
677 }
678 mMutex.unlock_shared();
679 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700680}
681
682Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
683 const auto status =
684 mAidlComposerClient->setColorMode(translate<int64_t>(display),
685 translate<AidlColorMode>(mode),
686 translate<AidlRenderIntent>(renderIntent));
687 if (!status.isOk()) {
688 ALOGE("setColorMode failed %s", status.getDescription().c_str());
689 return static_cast<Error>(status.getServiceSpecificError());
690 }
691 return Error::NONE;
692}
693
Ady Abrahamdc011a92021-12-21 14:06:44 -0800694Error AidlComposer::setColorTransform(Display display, const float* matrix) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400695 auto error = Error::NONE;
696 mMutex.lock_shared();
697 if (auto writer = getWriter(display)) {
698 writer->get().setColorTransform(translate<int64_t>(display), matrix);
699 } else {
700 error = Error::BAD_DISPLAY;
701 }
702 mMutex.unlock_shared();
703 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700704}
705
706Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
707 int releaseFence) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400708 auto error = Error::NONE;
709 mMutex.lock_shared();
710 if (auto writer = getWriter(display)) {
711 writer->get().setOutputBuffer(translate<int64_t>(display), 0, buffer, dup(releaseFence));
712 } else {
713 error = Error::BAD_DISPLAY;
714 }
715 mMutex.unlock_shared();
716 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700717}
718
719Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
720 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
721 translate<PowerMode>(mode));
722 if (!status.isOk()) {
723 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
724 return static_cast<Error>(status.getServiceSpecificError());
725 }
726 return Error::NONE;
727}
728
729Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
730 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
731 const auto status =
732 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
733 if (!status.isOk()) {
734 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
735 return static_cast<Error>(status.getServiceSpecificError());
736 }
737 return Error::NONE;
738}
739
740Error AidlComposer::setClientTargetSlotCount(Display display) {
741 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
742 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
743 bufferSlotCount);
744 if (!status.isOk()) {
745 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
746 return static_cast<Error>(status.getServiceSpecificError());
747 }
748 return Error::NONE;
749}
750
Ady Abraham43065bd2021-12-10 17:22:15 -0800751Error AidlComposer::validateDisplay(Display display, nsecs_t expectedPresentTime,
752 uint32_t* outNumTypes, uint32_t* outNumRequests) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400753 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500754 ATRACE_FORMAT("HwcValidateDisplay %" PRId64, displayId);
755
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400756 Error error = Error::NONE;
757 mMutex.lock_shared();
758 auto writer = getWriter(display);
759 auto reader = getReader(display);
760 if (writer && reader) {
761 writer->get().validateDisplay(displayId, 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 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700773
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400774 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700775 return Error::NONE;
776}
777
Ady Abraham43065bd2021-12-10 17:22:15 -0800778Error AidlComposer::presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime,
ramindani4aac32c2023-10-30 14:13:30 -0700779 int32_t frameIntervalNs, uint32_t* outNumTypes,
780 uint32_t* outNumRequests, int* outPresentFence,
781 uint32_t* state) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400782 const auto displayId = translate<int64_t>(display);
Leon Scroggins IIIe85e16e2022-12-12 12:51:18 -0500783 ATRACE_FORMAT("HwcPresentOrValidateDisplay %" PRId64, displayId);
784
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400785 Error error = Error::NONE;
786 mMutex.lock_shared();
787 auto writer = getWriter(display);
788 auto reader = getReader(display);
789 if (writer && reader) {
790 writer->get().presentOrvalidateDisplay(displayId,
ramindani4aac32c2023-10-30 14:13:30 -0700791 ClockMonotonicTimestamp{expectedPresentTime},
792 frameIntervalNs);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400793 error = execute(display);
794 } else {
795 error = Error::BAD_DISPLAY;
796 }
Ady Abrahame7385f72021-09-05 00:54:25 -0700797
Ady Abrahame7385f72021-09-05 00:54:25 -0700798 if (error != Error::NONE) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400799 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700800 return error;
801 }
802
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400803 const auto result = reader->get().takePresentOrValidateStage(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800804 if (!result.has_value()) {
805 *state = translate<uint32_t>(-1);
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400806 mMutex.unlock_shared();
Ady Abrahamde792782021-12-20 10:00:49 -0800807 return Error::NO_RESOURCES;
Ady Abrahame7385f72021-09-05 00:54:25 -0700808 }
809
Ady Abrahamde792782021-12-20 10:00:49 -0800810 *state = translate<uint32_t>(*result);
811
812 if (*result == PresentOrValidate::Result::Presented) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400813 auto fence = reader->get().takePresentFence(displayId);
Ady Abrahamde792782021-12-20 10:00:49 -0800814 // take ownership
815 *outPresentFence = fence.get();
816 *fence.getR() = -1;
817 }
818
819 if (*result == PresentOrValidate::Result::Validated) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400820 reader->get().hasChanges(displayId, outNumTypes, outNumRequests);
Ady Abrahame7385f72021-09-05 00:54:25 -0700821 }
822
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400823 mMutex.unlock_shared();
Ady Abrahame7385f72021-09-05 00:54:25 -0700824 return Error::NONE;
825}
826
827Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
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().setLayerCursorPosition(translate<int64_t>(display), translate<int64_t>(layer),
832 x, y);
833 } else {
834 error = Error::BAD_DISPLAY;
835 }
836 mMutex.unlock_shared();
837 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700838}
839
840Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
841 const sp<GraphicBuffer>& buffer, int acquireFence) {
Ady Abrahame7385f72021-09-05 00:54:25 -0700842 const native_handle_t* handle = nullptr;
843 if (buffer.get()) {
844 handle = buffer->getNativeBuffer()->handle;
845 }
846
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400847 Error error = Error::NONE;
848 mMutex.lock_shared();
849 if (auto writer = getWriter(display)) {
850 writer->get().setLayerBuffer(translate<int64_t>(display), translate<int64_t>(layer), slot,
851 handle, acquireFence);
852 } else {
853 error = Error::BAD_DISPLAY;
854 }
855 mMutex.unlock_shared();
856 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700857}
858
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700859Error AidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
860 const std::vector<uint32_t>& slotsToClear,
861 uint32_t activeBufferSlot) {
862 if (slotsToClear.empty()) {
863 return Error::NONE;
864 }
865
Brian Lindahl90553da2022-12-06 13:36:30 -0700866 Error error = Error::NONE;
867 mMutex.lock_shared();
868 if (auto writer = getWriter(display)) {
ramindani0cd1d8d2023-06-13 13:43:23 -0700869 if (mComposerInterfaceVersion > 1) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700870 writer->get().setLayerBufferSlotsToClear(translate<int64_t>(display),
871 translate<int64_t>(layer), slotsToClear);
Brian Lindahl5b0ffe02023-06-15 14:19:43 -0600872 // Backwards compatible way of clearing buffer slots is to set the layer buffer with a
873 // placeholder buffer, using the slot that needs to cleared... tricky.
874 } else if (mClearSlotBuffer != nullptr) {
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700875 for (uint32_t slot : slotsToClear) {
876 // Don't clear the active buffer slot because we need to restore the active buffer
877 // after clearing the requested buffer slots with a placeholder buffer.
878 if (slot != activeBufferSlot) {
879 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
880 translate<int64_t>(layer), slot,
881 mClearSlotBuffer->handle,
882 /*fence*/ -1);
883 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700884 }
Brian Lindahldbf7e3a2022-12-16 11:43:39 -0700885 // Since we clear buffers by setting them to a placeholder buffer, we want to make
886 // sure that the last setLayerBuffer command is sent with the currently active
887 // buffer, not the placeholder buffer, so that there is no perceptual change when
888 // buffers are discarded.
889 writer->get().setLayerBufferWithNewCommand(translate<int64_t>(display),
890 translate<int64_t>(layer), activeBufferSlot,
891 // The active buffer is still cached in
892 // its slot and doesn't need a fence.
893 /*buffer*/ nullptr, /*fence*/ -1);
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700894 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700895 } else {
896 error = Error::BAD_DISPLAY;
897 }
898 mMutex.unlock_shared();
899 return error;
900}
901
Ady Abrahame7385f72021-09-05 00:54:25 -0700902Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
903 const std::vector<IComposerClient::Rect>& damage) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400904 Error error = Error::NONE;
905 mMutex.lock_shared();
906 if (auto writer = getWriter(display)) {
907 writer->get().setLayerSurfaceDamage(translate<int64_t>(display), translate<int64_t>(layer),
908 translate<AidlRect>(damage));
909 } else {
910 error = Error::BAD_DISPLAY;
911 }
912 mMutex.unlock_shared();
913 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700914}
915
916Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
917 IComposerClient::BlendMode mode) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400918 Error error = Error::NONE;
919 mMutex.lock_shared();
920 if (auto writer = getWriter(display)) {
921 writer->get().setLayerBlendMode(translate<int64_t>(display), translate<int64_t>(layer),
922 translate<BlendMode>(mode));
923 } else {
924 error = Error::BAD_DISPLAY;
925 }
926 mMutex.unlock_shared();
927 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700928}
929
Ady Abraham6e60b142022-01-06 18:10:35 -0800930Error AidlComposer::setLayerColor(Display display, Layer layer, const Color& color) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400931 Error error = Error::NONE;
932 mMutex.lock_shared();
933 if (auto writer = getWriter(display)) {
934 writer->get().setLayerColor(translate<int64_t>(display), translate<int64_t>(layer), color);
935 } else {
936 error = Error::BAD_DISPLAY;
937 }
938 mMutex.unlock_shared();
939 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700940}
941
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500942Error AidlComposer::setLayerCompositionType(
943 Display display, Layer layer,
944 aidl::android::hardware::graphics::composer3::Composition type) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400945 Error error = Error::NONE;
946 mMutex.lock_shared();
947 if (auto writer = getWriter(display)) {
948 writer->get().setLayerCompositionType(translate<int64_t>(display),
949 translate<int64_t>(layer), type);
950 } else {
951 error = Error::BAD_DISPLAY;
952 }
953 mMutex.unlock_shared();
954 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700955}
956
957Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400958 Error error = Error::NONE;
959 mMutex.lock_shared();
960 if (auto writer = getWriter(display)) {
961 writer->get().setLayerDataspace(translate<int64_t>(display), translate<int64_t>(layer),
962 translate<AidlDataspace>(dataspace));
963 } else {
964 error = Error::BAD_DISPLAY;
965 }
966 mMutex.unlock_shared();
967 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700968}
969
970Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
971 const IComposerClient::Rect& frame) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400972 Error error = Error::NONE;
973 mMutex.lock_shared();
974 if (auto writer = getWriter(display)) {
975 writer->get().setLayerDisplayFrame(translate<int64_t>(display), translate<int64_t>(layer),
976 translate<AidlRect>(frame));
977 } else {
978 error = Error::BAD_DISPLAY;
979 }
980 mMutex.unlock_shared();
981 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700982}
983
984Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400985 Error error = Error::NONE;
986 mMutex.lock_shared();
987 if (auto writer = getWriter(display)) {
988 writer->get().setLayerPlaneAlpha(translate<int64_t>(display), translate<int64_t>(layer),
989 alpha);
990 } else {
991 error = Error::BAD_DISPLAY;
992 }
993 mMutex.unlock_shared();
994 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -0700995}
996
997Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
998 const native_handle_t* stream) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -0400999 Error error = Error::NONE;
1000 mMutex.lock_shared();
1001 if (auto writer = getWriter(display)) {
1002 writer->get().setLayerSidebandStream(translate<int64_t>(display), translate<int64_t>(layer),
1003 stream);
1004 } else {
1005 error = Error::BAD_DISPLAY;
1006 }
1007 mMutex.unlock_shared();
1008 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001009}
1010
1011Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
1012 const IComposerClient::FRect& crop) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001013 Error error = Error::NONE;
1014 mMutex.lock_shared();
1015 if (auto writer = getWriter(display)) {
1016 writer->get().setLayerSourceCrop(translate<int64_t>(display), translate<int64_t>(layer),
1017 translate<AidlFRect>(crop));
1018 } else {
1019 error = Error::BAD_DISPLAY;
1020 }
1021 mMutex.unlock_shared();
1022 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001023}
1024
1025Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001026 Error error = Error::NONE;
1027 mMutex.lock_shared();
1028 if (auto writer = getWriter(display)) {
1029 writer->get().setLayerTransform(translate<int64_t>(display), translate<int64_t>(layer),
1030 translate<AidlTransform>(transform));
1031 } else {
1032 error = Error::BAD_DISPLAY;
1033 }
1034 mMutex.unlock_shared();
1035 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001036}
1037
1038Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
1039 const std::vector<IComposerClient::Rect>& visible) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001040 Error error = Error::NONE;
1041 mMutex.lock_shared();
1042 if (auto writer = getWriter(display)) {
1043 writer->get().setLayerVisibleRegion(translate<int64_t>(display), translate<int64_t>(layer),
1044 translate<AidlRect>(visible));
1045 } else {
1046 error = Error::BAD_DISPLAY;
1047 }
1048 mMutex.unlock_shared();
1049 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001050}
1051
1052Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001053 Error error = Error::NONE;
1054 mMutex.lock_shared();
1055 if (auto writer = getWriter(display)) {
1056 writer->get().setLayerZOrder(translate<int64_t>(display), translate<int64_t>(layer), z);
1057 } else {
1058 error = Error::BAD_DISPLAY;
1059 }
1060 mMutex.unlock_shared();
1061 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001062}
1063
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001064Error AidlComposer::execute(Display display) {
1065 auto writer = getWriter(display);
1066 auto reader = getReader(display);
1067 if (!writer || !reader) {
1068 return Error::BAD_DISPLAY;
1069 }
1070
Huihong Luoe7382c12023-04-21 20:24:32 +00001071 auto commands = writer->get().takePendingCommands();
Ady Abrahama6388c02021-11-11 21:11:51 -08001072 if (commands.empty()) {
Ady Abrahame7385f72021-09-05 00:54:25 -07001073 return Error::NONE;
1074 }
1075
Ady Abrahamde792782021-12-20 10:00:49 -08001076 { // scope for results
1077 std::vector<CommandResultPayload> results;
1078 auto status = mAidlComposerClient->executeCommands(commands, &results);
1079 if (!status.isOk()) {
1080 ALOGE("executeCommands failed %s", status.getDescription().c_str());
1081 return static_cast<Error>(status.getServiceSpecificError());
1082 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001083
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001084 reader->get().parse(std::move(results));
Ady Abrahamde792782021-12-20 10:00:49 -08001085 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001086 const auto commandErrors = reader->get().takeErrors();
Ady Abrahama6388c02021-11-11 21:11:51 -08001087 Error error = Error::NONE;
1088 for (const auto& cmdErr : commandErrors) {
1089 const auto index = static_cast<size_t>(cmdErr.commandIndex);
1090 if (index < 0 || index >= commands.size()) {
1091 ALOGE("invalid command index %zu", index);
1092 return Error::BAD_PARAMETER;
Ady Abrahame7385f72021-09-05 00:54:25 -07001093 }
1094
Ady Abrahama6388c02021-11-11 21:11:51 -08001095 const auto& command = commands[index];
Ady Abraham42977362021-12-07 21:04:49 -08001096 if (command.validateDisplay || command.presentDisplay || command.presentOrValidateDisplay) {
1097 error = translate<Error>(cmdErr.errorCode);
1098 } else {
1099 ALOGW("command '%s' generated error %" PRId32, command.toString().c_str(),
1100 cmdErr.errorCode);
Ady Abrahame7385f72021-09-05 00:54:25 -07001101 }
1102 }
1103
Ady Abrahame7385f72021-09-05 00:54:25 -07001104 return error;
1105}
1106
1107Error AidlComposer::setLayerPerFrameMetadata(
1108 Display display, Layer layer,
1109 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001110 Error error = Error::NONE;
1111 mMutex.lock_shared();
1112 if (auto writer = getWriter(display)) {
1113 writer->get().setLayerPerFrameMetadata(translate<int64_t>(display),
1114 translate<int64_t>(layer),
1115 translate<AidlPerFrameMetadata>(perFrameMetadatas));
1116 } else {
1117 error = Error::BAD_DISPLAY;
1118 }
1119 mMutex.unlock_shared();
1120 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001121}
1122
1123std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
1124 Display display) {
1125 std::vector<AidlPerFrameMetadataKey> keys;
1126 const auto status =
1127 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
1128 if (!status.isOk()) {
1129 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
1130 return {};
1131 }
1132 return translate<IComposerClient::PerFrameMetadataKey>(keys);
1133}
1134
1135Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1136 std::vector<RenderIntent>* outRenderIntents) {
1137 std::vector<AidlRenderIntent> renderIntents;
1138 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
1139 translate<AidlColorMode>(colorMode),
1140 &renderIntents);
1141 if (!status.isOk()) {
1142 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
1143 return static_cast<Error>(status.getServiceSpecificError());
1144 }
1145 *outRenderIntents = translate<RenderIntent>(renderIntents);
1146 return Error::NONE;
1147}
1148
1149Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1150 std::vector<float> matrix;
1151 const auto status =
1152 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
1153 &matrix);
1154 if (!status.isOk()) {
1155 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
1156 return static_cast<Error>(status.getServiceSpecificError());
1157 }
1158 *outMatrix = makeMat4(matrix);
1159 return Error::NONE;
1160}
1161
1162Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1163 std::vector<uint8_t>* outData) {
1164 AidlDisplayIdentification displayIdentification;
1165 const auto status =
1166 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
1167 &displayIdentification);
1168 if (!status.isOk()) {
1169 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
1170 return static_cast<Error>(status.getServiceSpecificError());
1171 }
1172
1173 *outPort = static_cast<uint8_t>(displayIdentification.port);
1174 *outData = displayIdentification.data;
1175
1176 return Error::NONE;
1177}
1178
1179Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
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().setLayerColorTransform(translate<int64_t>(display), translate<int64_t>(layer),
1184 matrix);
1185 } else {
1186 error = Error::BAD_DISPLAY;
1187 }
1188 mMutex.unlock_shared();
1189 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001190}
1191
1192Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1193 Dataspace* outDataspace,
1194 uint8_t* outComponentMask) {
1195 if (!outFormat || !outDataspace || !outComponentMask) {
1196 return Error::BAD_PARAMETER;
1197 }
1198
1199 AidlDisplayContentSamplingAttributes attributes;
1200 const auto status =
1201 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
1202 &attributes);
1203 if (!status.isOk()) {
1204 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
1205 return static_cast<Error>(status.getServiceSpecificError());
1206 }
1207
1208 *outFormat = translate<PixelFormat>(attributes.format);
1209 *outDataspace = translate<Dataspace>(attributes.dataspace);
1210 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
1211 return Error::NONE;
1212}
1213
1214Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1215 uint8_t componentMask, uint64_t maxFrames) {
1216 const auto status =
1217 mAidlComposerClient
1218 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
1219 static_cast<AidlFormatColorComponent>(
1220 componentMask),
1221 static_cast<int64_t>(maxFrames));
1222 if (!status.isOk()) {
1223 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
1224 return static_cast<Error>(status.getServiceSpecificError());
1225 }
1226 return Error::NONE;
1227}
1228
1229Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1230 uint64_t timestamp, DisplayedFrameStats* outStats) {
1231 if (!outStats) {
1232 return Error::BAD_PARAMETER;
1233 }
1234
1235 AidlDisplayContentSample sample;
1236 const auto status =
1237 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
1238 static_cast<int64_t>(maxFrames),
1239 static_cast<int64_t>(timestamp),
1240 &sample);
1241 if (!status.isOk()) {
1242 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
1243 return static_cast<Error>(status.getServiceSpecificError());
1244 }
1245 *outStats = translate<DisplayedFrameStats>(sample);
1246 return Error::NONE;
1247}
1248
1249Error AidlComposer::setLayerPerFrameMetadataBlobs(
1250 Display display, Layer layer,
1251 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001252 Error error = Error::NONE;
1253 mMutex.lock_shared();
1254 if (auto writer = getWriter(display)) {
1255 writer->get().setLayerPerFrameMetadataBlobs(translate<int64_t>(display),
1256 translate<int64_t>(layer),
1257 translate<AidlPerFrameMetadataBlob>(metadata));
1258 } else {
1259 error = Error::BAD_DISPLAY;
1260 }
1261 mMutex.unlock_shared();
1262 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001263}
1264
Alec Mouri4d8a05d2022-03-23 18:14:26 +00001265Error AidlComposer::setDisplayBrightness(Display display, float brightness, float brightnessNits,
Alec Mouricdf16792021-12-10 13:16:06 -08001266 const DisplayBrightnessOptions& options) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001267 Error error = Error::NONE;
1268 mMutex.lock_shared();
1269 if (auto writer = getWriter(display)) {
1270 writer->get().setDisplayBrightness(translate<int64_t>(display), brightness, brightnessNits);
Alec Mouricdf16792021-12-10 13:16:06 -08001271
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001272 if (options.applyImmediately) {
1273 error = execute(display);
1274 mMutex.unlock_shared();
1275 return error;
1276 }
1277 } else {
1278 error = Error::BAD_DISPLAY;
Alec Mouricdf16792021-12-10 13:16:06 -08001279 }
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001280 mMutex.unlock_shared();
1281 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001282}
1283
1284Error AidlComposer::getDisplayCapabilities(Display display,
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001285 std::vector<AidlDisplayCapability>* outCapabilities) {
1286 const auto status = mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display),
1287 outCapabilities);
Ady Abrahame7385f72021-09-05 00:54:25 -07001288 if (!status.isOk()) {
1289 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
Leon Scroggins III5967aec2021-12-29 11:14:22 -05001290 outCapabilities->clear();
Ady Abrahame7385f72021-09-05 00:54:25 -07001291 return static_cast<Error>(status.getServiceSpecificError());
1292 }
Ady Abrahame7385f72021-09-05 00:54:25 -07001293 return Error::NONE;
1294}
1295
1296V2_4::Error AidlComposer::getDisplayConnectionType(
1297 Display display, IComposerClient::DisplayConnectionType* outType) {
1298 AidlDisplayConnectionType type;
1299 const auto status =
1300 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
1301 if (!status.isOk()) {
1302 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
1303 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1304 }
1305 *outType = translate<IComposerClient::DisplayConnectionType>(type);
1306 return V2_4::Error::NONE;
1307}
1308
1309V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1310 int32_t vsyncPeriod;
1311 const auto status =
1312 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
1313 if (!status.isOk()) {
1314 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
1315 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1316 }
1317 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
1318 return V2_4::Error::NONE;
1319}
1320
1321V2_4::Error AidlComposer::setActiveConfigWithConstraints(
1322 Display display, Config config,
1323 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1324 VsyncPeriodChangeTimeline* outTimeline) {
1325 AidlVsyncPeriodChangeTimeline timeline;
1326 const auto status =
1327 mAidlComposerClient
1328 ->setActiveConfigWithConstraints(translate<int64_t>(display),
1329 translate<int32_t>(config),
1330 translate<AidlVsyncPeriodChangeConstraints>(
1331 vsyncPeriodChangeConstraints),
1332 &timeline);
1333 if (!status.isOk()) {
1334 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
1335 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1336 }
1337 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
1338 return V2_4::Error::NONE;
1339}
1340
1341V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1342 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1343 if (!status.isOk()) {
1344 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1345 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1346 }
1347 return V2_4::Error::NONE;
1348}
1349
1350V2_4::Error AidlComposer::getSupportedContentTypes(
1351 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1352 std::vector<AidlContentType> types;
1353 const auto status =
1354 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1355 if (!status.isOk()) {
1356 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1357 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1358 }
1359 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1360 return V2_4::Error::NONE;
1361}
1362
1363V2_4::Error AidlComposer::setContentType(Display display,
1364 IComposerClient::ContentType contentType) {
1365 const auto status =
1366 mAidlComposerClient->setContentType(translate<int64_t>(display),
1367 translate<AidlContentType>(contentType));
1368 if (!status.isOk()) {
1369 ALOGE("setContentType failed %s", status.getDescription().c_str());
1370 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1371 }
1372 return V2_4::Error::NONE;
1373}
1374
Ady Abraham3f976752021-12-20 16:17:50 -08001375V2_4::Error AidlComposer::setLayerGenericMetadata(Display, Layer, const std::string&, bool,
1376 const std::vector<uint8_t>&) {
1377 // There are no users for this API. See b/209691612.
1378 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001379}
1380
1381V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
Ady Abraham3f976752021-12-20 16:17:50 -08001382 std::vector<IComposerClient::LayerGenericMetadataKey>*) {
1383 // There are no users for this API. See b/209691612.
1384 return V2_4::Error::UNSUPPORTED;
Ady Abrahame7385f72021-09-05 00:54:25 -07001385}
1386
Kriti Dang7defaf32021-11-15 11:55:43 +01001387Error AidlComposer::setBootDisplayConfig(Display display, Config config) {
1388 const auto status = mAidlComposerClient->setBootDisplayConfig(translate<int64_t>(display),
1389 translate<int32_t>(config));
1390 if (!status.isOk()) {
1391 ALOGE("setBootDisplayConfig failed %s", status.getDescription().c_str());
1392 return static_cast<Error>(status.getServiceSpecificError());
1393 }
1394 return Error::NONE;
1395}
1396
1397Error AidlComposer::clearBootDisplayConfig(Display display) {
1398 const auto status = mAidlComposerClient->clearBootDisplayConfig(translate<int64_t>(display));
1399 if (!status.isOk()) {
1400 ALOGE("clearBootDisplayConfig failed %s", status.getDescription().c_str());
1401 return static_cast<Error>(status.getServiceSpecificError());
1402 }
1403 return Error::NONE;
1404}
1405
1406Error AidlComposer::getPreferredBootDisplayConfig(Display display, Config* config) {
1407 int32_t displayConfig;
1408 const auto status =
1409 mAidlComposerClient->getPreferredBootDisplayConfig(translate<int64_t>(display),
1410 &displayConfig);
1411 if (!status.isOk()) {
1412 ALOGE("getPreferredBootDisplayConfig failed %s", status.getDescription().c_str());
1413 return static_cast<Error>(status.getServiceSpecificError());
1414 }
1415 *config = translate<uint32_t>(displayConfig);
1416 return Error::NONE;
1417}
1418
Kriti Dang674b9372022-11-18 10:58:44 +01001419Error AidlComposer::getHdrConversionCapabilities(
1420 std::vector<AidlHdrConversionCapability>* hdrConversionCapabilities) {
1421 const auto status =
1422 mAidlComposerClient->getHdrConversionCapabilities(hdrConversionCapabilities);
1423 if (!status.isOk()) {
1424 hdrConversionCapabilities = {};
1425 ALOGE("getHdrConversionCapabilities failed %s", status.getDescription().c_str());
1426 return static_cast<Error>(status.getServiceSpecificError());
1427 }
1428 return Error::NONE;
1429}
1430
Kriti Dangd432bb52023-02-09 18:21:04 +01001431Error AidlComposer::setHdrConversionStrategy(AidlHdrConversionStrategy hdrConversionStrategy,
1432 Hdr* outPreferredHdrOutputType) {
1433 const auto status = mAidlComposerClient->setHdrConversionStrategy(hdrConversionStrategy,
1434 outPreferredHdrOutputType);
Kriti Dang674b9372022-11-18 10:58:44 +01001435 if (!status.isOk()) {
1436 ALOGE("setHdrConversionStrategy failed %s", status.getDescription().c_str());
1437 return static_cast<Error>(status.getServiceSpecificError());
1438 }
1439 return Error::NONE;
1440}
1441
ramindanib2158ee2023-02-13 20:29:59 -08001442Error AidlComposer::setRefreshRateChangedCallbackDebugEnabled(Display displayId, bool enabled) {
1443 const auto status =
1444 mAidlComposerClient->setRefreshRateChangedCallbackDebugEnabled(translate<int64_t>(
1445 displayId),
1446 enabled);
1447 if (!status.isOk()) {
1448 ALOGE("setRefreshRateChangedCallbackDebugEnabled failed %s",
1449 status.getDescription().c_str());
1450 return static_cast<Error>(status.getServiceSpecificError());
1451 }
1452 return Error::NONE;
1453}
1454
ramindani3acaaf52023-09-25 10:31:27 -07001455Error AidlComposer::notifyExpectedPresent(Display displayId, nsecs_t expectedPresentTime,
1456 int32_t frameIntervalNs) {
1457 const auto status =
1458 mAidlComposerClient->notifyExpectedPresent(translate<int64_t>(displayId),
1459 ClockMonotonicTimestamp{expectedPresentTime},
1460 frameIntervalNs);
1461
1462 if (!status.isOk()) {
1463 ALOGE("notifyExpectedPresent failed %s", status.getDescription().c_str());
1464 return static_cast<Error>(status.getServiceSpecificError());
1465 }
1466 return Error::NONE;
1467}
1468
Ady Abrahame7385f72021-09-05 00:54:25 -07001469Error AidlComposer::getClientTargetProperty(
Alec Mouri85065692022-03-18 00:58:26 +00001470 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001471 Error error = Error::NONE;
1472 mMutex.lock_shared();
1473 if (auto reader = getReader(display)) {
1474 *outClientTargetProperty =
1475 reader->get().takeClientTargetProperty(translate<int64_t>(display));
1476 } else {
1477 error = Error::BAD_DISPLAY;
1478 }
1479 mMutex.unlock_shared();
1480 return error;
Ady Abrahame7385f72021-09-05 00:54:25 -07001481}
1482
Alec Mouri6da0e272022-02-07 12:45:57 -08001483Error AidlComposer::setLayerBrightness(Display display, Layer layer, float brightness) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001484 Error error = Error::NONE;
1485 mMutex.lock_shared();
1486 if (auto writer = getWriter(display)) {
1487 writer->get().setLayerBrightness(translate<int64_t>(display), translate<int64_t>(layer),
1488 brightness);
1489 } else {
1490 error = Error::BAD_DISPLAY;
1491 }
1492 mMutex.unlock_shared();
1493 return error;
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001494}
1495
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001496Error AidlComposer::setLayerBlockingRegion(Display display, Layer layer,
1497 const std::vector<IComposerClient::Rect>& blocking) {
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001498 Error error = Error::NONE;
1499 mMutex.lock_shared();
1500 if (auto writer = getWriter(display)) {
1501 writer->get().setLayerBlockingRegion(translate<int64_t>(display), translate<int64_t>(layer),
1502 translate<AidlRect>(blocking));
1503 } else {
1504 error = Error::BAD_DISPLAY;
1505 }
1506 mMutex.unlock_shared();
1507 return error;
Leon Scroggins IIId77d3162022-01-05 10:42:28 -05001508}
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05001509
1510Error AidlComposer::getDisplayDecorationSupport(Display display,
1511 std::optional<DisplayDecorationSupport>* support) {
1512 const auto status =
1513 mAidlComposerClient->getDisplayDecorationSupport(translate<int64_t>(display), support);
1514 if (!status.isOk()) {
1515 ALOGE("getDisplayDecorationSupport failed %s", status.getDescription().c_str());
1516 support->reset();
1517 return static_cast<Error>(status.getServiceSpecificError());
1518 }
1519 return Error::NONE;
1520}
ramindani32cf0602022-03-02 02:30:29 +00001521
1522Error AidlComposer::setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) {
1523 const auto status =
1524 mAidlComposerClient->setIdleTimerEnabled(translate<int64_t>(displayId),
1525 translate<int32_t>(timeout.count()));
1526 if (!status.isOk()) {
1527 ALOGE("setIdleTimerEnabled failed %s", status.getDescription().c_str());
1528 return static_cast<Error>(status.getServiceSpecificError());
1529 }
1530 return Error::NONE;
1531}
1532
ramindani06e518e2022-03-14 18:47:53 +00001533Error AidlComposer::getPhysicalDisplayOrientation(Display displayId,
1534 AidlTransform* outDisplayOrientation) {
1535 const auto status =
1536 mAidlComposerClient->getDisplayPhysicalOrientation(translate<int64_t>(displayId),
1537 outDisplayOrientation);
1538 if (!status.isOk()) {
1539 ALOGE("getPhysicalDisplayOrientation failed %s", status.getDescription().c_str());
1540 return static_cast<Error>(status.getServiceSpecificError());
1541 }
1542 return Error::NONE;
1543}
1544
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001545ftl::Optional<std::reference_wrapper<ComposerClientWriter>> AidlComposer::getWriter(Display display)
1546 REQUIRES_SHARED(mMutex) {
1547 return mWriters.get(display);
1548}
1549
1550ftl::Optional<std::reference_wrapper<ComposerClientReader>> AidlComposer::getReader(Display display)
1551 REQUIRES_SHARED(mMutex) {
1552 if (mSingleReader) {
1553 display = translate<Display>(kSingleReaderKey);
1554 }
1555 return mReaders.get(display);
1556}
1557
1558void AidlComposer::removeDisplay(Display display) {
1559 mMutex.lock();
1560 bool wasErased = mWriters.erase(display);
1561 ALOGW_IF(!wasErased,
1562 "Attempting to remove writer for display %" PRId64 " which is not connected",
1563 translate<int64_t>(display));
1564 if (!mSingleReader) {
1565 removeReader(display);
1566 }
1567 mMutex.unlock();
1568}
1569
1570void AidlComposer::onHotplugDisconnect(Display display) {
1571 removeDisplay(display);
1572}
1573
1574bool AidlComposer::hasMultiThreadedPresentSupport(Display display) {
Leon Scroggins IIIc1cf4582023-03-23 18:37:44 -04001575#if 0
1576 // TODO (b/259132483): Reenable
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001577 const auto displayId = translate<int64_t>(display);
1578 std::vector<AidlDisplayCapability> capabilities;
1579 const auto status = mAidlComposerClient->getDisplayCapabilities(displayId, &capabilities);
1580 if (!status.isOk()) {
1581 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
1582 return false;
1583 }
1584 return std::find(capabilities.begin(), capabilities.end(),
1585 AidlDisplayCapability::MULTI_THREADED_PRESENT) != capabilities.end();
Leon Scroggins IIIc1cf4582023-03-23 18:37:44 -04001586#else
1587 (void) display;
1588 return false;
1589#endif
Leon Scroggins IIIe24d78f2022-09-20 16:38:19 -04001590}
1591
1592void AidlComposer::addReader(Display display) {
1593 const auto displayId = translate<int64_t>(display);
1594 std::optional<int64_t> displayOpt;
1595 if (displayId != kSingleReaderKey) {
1596 displayOpt.emplace(displayId);
1597 }
1598 auto [it, added] = mReaders.try_emplace(display, std::move(displayOpt));
1599 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1600 displayId);
1601}
1602
1603void AidlComposer::removeReader(Display display) {
1604 bool wasErased = mReaders.erase(display);
1605 ALOGW_IF(!wasErased,
1606 "Attempting to remove reader for display %" PRId64 " which is not connected",
1607 translate<int64_t>(display));
1608}
1609
1610void AidlComposer::addDisplay(Display display) {
1611 const auto displayId = translate<int64_t>(display);
1612 mMutex.lock();
1613 auto [it, added] = mWriters.try_emplace(display, displayId);
1614 ALOGW_IF(!added, "Attempting to add writer for display %" PRId64 " which is already connected",
1615 displayId);
1616 if (mSingleReader) {
1617 if (hasMultiThreadedPresentSupport(display)) {
1618 mSingleReader = false;
1619 removeReader(translate<Display>(kSingleReaderKey));
1620 // Note that this includes the new display.
1621 for (const auto& [existingDisplay, _] : mWriters) {
1622 addReader(existingDisplay);
1623 }
1624 }
1625 } else {
1626 addReader(display);
1627 }
1628 mMutex.unlock();
1629}
1630
1631void AidlComposer::onHotplugConnect(Display display) {
1632 addDisplay(display);
1633}
Ady Abrahame7385f72021-09-05 00:54:25 -07001634} // namespace Hwc2
1635} // namespace android