blob: ba57be5d65b9e0b331ae8d1823e08cc581237c62 [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
23#include <android/binder_ibinder_platform.h>
24#include <android/binder_manager.h>
25#include <log/log.h>
26#include <utils/Trace.h>
27
28#include <aidl/android/hardware/graphics/composer3/BnComposerCallback.h>
29
30#include <algorithm>
31#include <cinttypes>
32
33namespace android {
34
35using hardware::hidl_handle;
36using hardware::hidl_vec;
37using hardware::Return;
38
39using aidl::android::hardware::graphics::composer3::BnComposerCallback;
40using aidl::android::hardware::graphics::composer3::Capability;
41using aidl::android::hardware::graphics::composer3::PowerMode;
42using aidl::android::hardware::graphics::composer3::VirtualDisplay;
43
44using AidlColorMode = aidl::android::hardware::graphics::composer3::ColorMode;
45using AidlContentType = aidl::android::hardware::graphics::composer3::ContentType;
46using AidlDisplayIdentification =
47 aidl::android::hardware::graphics::composer3::DisplayIdentification;
48using AidlDisplayContentSample = aidl::android::hardware::graphics::composer3::DisplayContentSample;
49using AidlDisplayAttribute = aidl::android::hardware::graphics::composer3::DisplayAttribute;
50using AidlDisplayCapability = aidl::android::hardware::graphics::composer3::DisplayCapability;
51using AidlExecuteCommandsStatus =
52 aidl::android::hardware::graphics::composer3::ExecuteCommandsStatus;
53using AidlHdrCapabilities = aidl::android::hardware::graphics::composer3::HdrCapabilities;
54using AidlPerFrameMetadata = aidl::android::hardware::graphics::composer3::PerFrameMetadata;
55using AidlPerFrameMetadataKey = aidl::android::hardware::graphics::composer3::PerFrameMetadataKey;
56using AidlPerFrameMetadataBlob = aidl::android::hardware::graphics::composer3::PerFrameMetadataBlob;
57using AidlRenderIntent = aidl::android::hardware::graphics::composer3::RenderIntent;
58using AidlVsyncPeriodChangeConstraints =
59 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeConstraints;
60using AidlVsyncPeriodChangeTimeline =
61 aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline;
62using AidlLayerGenericMetadataKey =
63 aidl::android::hardware::graphics::composer3::LayerGenericMetadataKey;
64using AidlDisplayContentSamplingAttributes =
65 aidl::android::hardware::graphics::composer3::DisplayContentSamplingAttributes;
66using AidlFormatColorComponent = aidl::android::hardware::graphics::composer3::FormatColorComponent;
67using AidlDisplayConnectionType =
68 aidl::android::hardware::graphics::composer3::DisplayConnectionType;
69using AidlIComposerClient = aidl::android::hardware::graphics::composer3::IComposerClient;
70
71using AidlColorTransform = aidl::android::hardware::graphics::common::ColorTransform;
72using AidlDataspace = aidl::android::hardware::graphics::common::Dataspace;
73using AidlFRect = aidl::android::hardware::graphics::common::FRect;
74using AidlRect = aidl::android::hardware::graphics::common::Rect;
75using AidlTransform = aidl::android::hardware::graphics::common::Transform;
76
77namespace Hwc2 {
78
79namespace {
80
81template <typename To, typename From>
82To translate(From x) {
83 return static_cast<To>(x);
84}
85
86template <typename To, typename From>
87std::vector<To> translate(const std::vector<From>& in) {
88 std::vector<To> out;
89 out.reserve(in.size());
90 std::transform(in.begin(), in.end(), std::back_inserter(out),
91 [](From x) { return translate<To>(x); });
92 return out;
93}
94
95template <>
96AidlRect translate(IComposerClient::Rect x) {
97 return AidlRect{
98 .left = x.left,
99 .top = x.top,
100 .right = x.right,
101 .bottom = x.bottom,
102 };
103}
104
105template <>
106AidlFRect translate(IComposerClient::FRect x) {
107 return AidlFRect{
108 .left = x.left,
109 .top = x.top,
110 .right = x.right,
111 .bottom = x.bottom,
112 };
113}
114
115template <>
116Color translate(IComposerClient::Color x) {
117 return Color{
118 .r = static_cast<int8_t>(x.r),
119 .g = static_cast<int8_t>(x.g),
120 .b = static_cast<int8_t>(x.b),
121 .a = static_cast<int8_t>(x.a),
122 };
123}
124
125template <>
126AidlPerFrameMetadataBlob translate(IComposerClient::PerFrameMetadataBlob x) {
127 AidlPerFrameMetadataBlob blob;
128 blob.key = translate<AidlPerFrameMetadataKey>(x.key),
129 std::copy(blob.blob.begin(), blob.blob.end(), x.blob.begin());
130 return blob;
131}
132
133template <>
134AidlPerFrameMetadata translate(IComposerClient::PerFrameMetadata x) {
135 return AidlPerFrameMetadata{
136 .key = translate<AidlPerFrameMetadataKey>(x.key),
137 .value = x.value,
138 };
139}
140
141template <>
142DisplayedFrameStats translate(AidlDisplayContentSample x) {
143 return DisplayedFrameStats{
144 .numFrames = static_cast<uint64_t>(x.frameCount),
145 .component_0_sample = translate<uint64_t>(x.sampleComponent0),
146 .component_1_sample = translate<uint64_t>(x.sampleComponent1),
147 .component_2_sample = translate<uint64_t>(x.sampleComponent2),
148 .component_3_sample = translate<uint64_t>(x.sampleComponent3),
149 };
150}
151
152template <>
153AidlVsyncPeriodChangeConstraints translate(IComposerClient::VsyncPeriodChangeConstraints x) {
154 return AidlVsyncPeriodChangeConstraints{
155 .desiredTimeNanos = x.desiredTimeNanos,
156 .seamlessRequired = x.seamlessRequired,
157 };
158}
159
160template <>
161VsyncPeriodChangeTimeline translate(AidlVsyncPeriodChangeTimeline x) {
162 return VsyncPeriodChangeTimeline{
163 .newVsyncAppliedTimeNanos = x.newVsyncAppliedTimeNanos,
164 .refreshRequired = x.refreshRequired,
165 .refreshTimeNanos = x.refreshTimeNanos,
166 };
167}
168
169template <>
170IComposerClient::LayerGenericMetadataKey translate(AidlLayerGenericMetadataKey x) {
171 return IComposerClient::LayerGenericMetadataKey{
172 .name = x.name,
173 .mandatory = x.mandatory,
174 };
175}
176
177mat4 makeMat4(std::vector<float> in) {
178 return mat4(static_cast<const float*>(in.data()));
179}
180
181} // namespace
182
183class AidlIComposerCallbackWrapper : public BnComposerCallback {
184public:
185 AidlIComposerCallbackWrapper(sp<V2_4::IComposerCallback> callback)
186 : mCallback(std::move(callback)) {}
187
188 ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override {
189 const auto connection = in_connected ? V2_4::IComposerCallback::Connection::CONNECTED
190 : V2_4::IComposerCallback::Connection::DISCONNECTED;
191 mCallback->onHotplug(translate<Display>(in_display), connection);
192 return ::ndk::ScopedAStatus::ok();
193 }
194
195 ::ndk::ScopedAStatus onRefresh(int64_t in_display) override {
196 mCallback->onRefresh(translate<Display>(in_display));
197 return ::ndk::ScopedAStatus::ok();
198 }
199 ::ndk::ScopedAStatus onSeamlessPossible(int64_t in_display) override {
200 mCallback->onSeamlessPossible(translate<Display>(in_display));
201 return ::ndk::ScopedAStatus::ok();
202 }
203 ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
204 int32_t in_vsyncPeriodNanos) override {
205 mCallback->onVsync_2_4(translate<Display>(in_display), in_timestamp,
206 static_cast<uint32_t>(in_vsyncPeriodNanos));
207 return ::ndk::ScopedAStatus::ok();
208 }
209 ::ndk::ScopedAStatus onVsyncPeriodTimingChanged(
210 int64_t in_display, const AidlVsyncPeriodChangeTimeline& in_updatedTimeline) override {
211 mCallback->onVsyncPeriodTimingChanged(translate<Display>(in_display),
212 translate<V2_4::VsyncPeriodChangeTimeline>(
213 in_updatedTimeline));
214 return ::ndk::ScopedAStatus::ok();
215 }
216
217private:
218 sp<V2_4::IComposerCallback> mCallback;
219};
220
Ady Abraham9fc28052021-10-14 17:21:38 -0700221std::string AidlComposer::instance(const std::string& serviceName) {
222 return std::string(AidlIComposer::descriptor) + "/" + serviceName;
223}
224
225bool AidlComposer::isDeclared(const std::string& serviceName) {
226 return AServiceManager_isDeclared(instance(serviceName).c_str());
227}
Ady Abrahame7385f72021-09-05 00:54:25 -0700228
229AidlComposer::AidlComposer(const std::string& serviceName) : mWriter(kWriterInitialSize) {
230 // This only waits if the service is actually declared
Ady Abraham9fc28052021-10-14 17:21:38 -0700231 mAidlComposer = AidlIComposer::fromBinder(
232 ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
Ady Abrahame7385f72021-09-05 00:54:25 -0700233 if (!mAidlComposer) {
234 LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
235 return;
236 }
237
238 if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
239 LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
240 return;
241 }
242
243 ALOGI("Loaded AIDL composer3 HAL service");
244}
245
246AidlComposer::~AidlComposer() = default;
247
248std::vector<IComposer::Capability> AidlComposer::getCapabilities() {
249 std::vector<Capability> capabilities;
250 const auto status = mAidlComposer->getCapabilities(&capabilities);
251 if (!status.isOk()) {
252 ALOGE("getCapabilities failed %s", status.getDescription().c_str());
253 return {};
254 }
255 return translate<IComposer::Capability>(capabilities);
256}
257
258std::string AidlComposer::dumpDebugInfo() {
259 std::string info;
260 const auto status = mAidlComposer->dumpDebugInfo(&info);
261 if (!status.isOk()) {
262 ALOGE("dumpDebugInfo failed %s", status.getDescription().c_str());
263 return {};
264 }
265 return info;
266}
267
268void AidlComposer::registerCallback(const sp<IComposerCallback>& callback) {
269 if (mAidlComposerCallback) {
270 ALOGE("Callback already registered");
271 }
Ady Abraham9fc28052021-10-14 17:21:38 -0700272 mAidlComposerCallback = ndk::SharedRefBase::make<AidlIComposerCallbackWrapper>(callback);
Ady Abrahame7385f72021-09-05 00:54:25 -0700273 AIBinder_setMinSchedulerPolicy(mAidlComposerCallback->asBinder().get(), SCHED_FIFO, 2);
274
275 const auto status = mAidlComposerClient->registerCallback(mAidlComposerCallback);
276 if (!status.isOk()) {
277 ALOGE("registerCallback failed %s", status.getDescription().c_str());
278 }
279}
280
281void AidlComposer::resetCommands() {
282 mWriter.reset();
283}
284
285Error AidlComposer::executeCommands() {
286 return execute();
287}
288
289uint32_t AidlComposer::getMaxVirtualDisplayCount() {
290 int32_t count = 0;
291 const auto status = mAidlComposerClient->getMaxVirtualDisplayCount(&count);
292 if (!status.isOk()) {
293 ALOGE("getMaxVirtualDisplayCount failed %s", status.getDescription().c_str());
294 return 0;
295 }
296 return static_cast<uint32_t>(count);
297}
298
299Error AidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
300 Display* outDisplay) {
301 using AidlPixelFormat = aidl::android::hardware::graphics::common::PixelFormat;
302 const int32_t bufferSlotCount = 1;
303 VirtualDisplay virtualDisplay;
304 const auto status =
305 mAidlComposerClient->createVirtualDisplay(static_cast<int32_t>(width),
306 static_cast<int32_t>(height),
307 static_cast<AidlPixelFormat>(*format),
308 bufferSlotCount, &virtualDisplay);
309
310 if (!status.isOk()) {
311 ALOGE("createVirtualDisplay failed %s", status.getDescription().c_str());
312 return static_cast<Error>(status.getServiceSpecificError());
313 }
314
315 *outDisplay = translate<Display>(virtualDisplay.display);
316 *format = static_cast<PixelFormat>(virtualDisplay.format);
317 return Error::NONE;
318}
319
320Error AidlComposer::destroyVirtualDisplay(Display display) {
321 const auto status = mAidlComposerClient->destroyVirtualDisplay(translate<int64_t>(display));
322 if (!status.isOk()) {
323 ALOGE("destroyVirtualDisplay failed %s", status.getDescription().c_str());
324 return static_cast<Error>(status.getServiceSpecificError());
325 }
326 return Error::NONE;
327}
328
329Error AidlComposer::acceptDisplayChanges(Display display) {
330 mWriter.selectDisplay(translate<int64_t>(display));
331 mWriter.acceptDisplayChanges();
332 return Error::NONE;
333}
334
335Error AidlComposer::createLayer(Display display, Layer* outLayer) {
336 int64_t layer;
337 const auto status = mAidlComposerClient->createLayer(translate<int64_t>(display),
338 kMaxLayerBufferCount, &layer);
339 if (!status.isOk()) {
340 ALOGE("createLayer failed %s", status.getDescription().c_str());
341 return static_cast<Error>(status.getServiceSpecificError());
342 }
343
344 *outLayer = translate<Layer>(layer);
345 return Error::NONE;
346}
347
348Error AidlComposer::destroyLayer(Display display, Layer layer) {
349 const auto status = mAidlComposerClient->destroyLayer(translate<int64_t>(display),
350 translate<int64_t>(layer));
351 if (!status.isOk()) {
352 ALOGE("destroyLayer failed %s", status.getDescription().c_str());
353 return static_cast<Error>(status.getServiceSpecificError());
354 }
355 return Error::NONE;
356}
357
358Error AidlComposer::getActiveConfig(Display display, Config* outConfig) {
359 int32_t config;
360 const auto status = mAidlComposerClient->getActiveConfig(translate<int64_t>(display), &config);
361 if (!status.isOk()) {
362 ALOGE("getActiveConfig failed %s", status.getDescription().c_str());
363 return static_cast<Error>(status.getServiceSpecificError());
364 }
365 *outConfig = translate<Config>(config);
366 return Error::NONE;
367}
368
369Error AidlComposer::getChangedCompositionTypes(
370 Display display, std::vector<Layer>* outLayers,
371 std::vector<IComposerClient::Composition>* outTypes) {
372 mReader.takeChangedCompositionTypes(display, outLayers, outTypes);
373 return Error::NONE;
374}
375
376Error AidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
377 std::vector<AidlColorMode> modes;
378 const auto status = mAidlComposerClient->getColorModes(translate<int64_t>(display), &modes);
379 if (!status.isOk()) {
380 ALOGE("getColorModes failed %s", status.getDescription().c_str());
381 return static_cast<Error>(status.getServiceSpecificError());
382 }
383 *outModes = translate<ColorMode>(modes);
384 return Error::NONE;
385}
386
387Error AidlComposer::getDisplayAttribute(Display display, Config config,
388 IComposerClient::Attribute attribute, int32_t* outValue) {
389 const auto status =
390 mAidlComposerClient->getDisplayAttribute(translate<int64_t>(display),
391 translate<int32_t>(config),
392 static_cast<AidlDisplayAttribute>(attribute),
393 outValue);
394 if (!status.isOk()) {
395 ALOGE("getDisplayAttribute failed %s", status.getDescription().c_str());
396 return static_cast<Error>(status.getServiceSpecificError());
397 }
398 return Error::NONE;
399}
400
401Error AidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
402 std::vector<int32_t> configs;
403 const auto status =
404 mAidlComposerClient->getDisplayConfigs(translate<int64_t>(display), &configs);
405 if (!status.isOk()) {
406 ALOGE("getDisplayConfigs failed %s", status.getDescription().c_str());
407 return static_cast<Error>(status.getServiceSpecificError());
408 }
409 *outConfigs = translate<Config>(configs);
410 return Error::NONE;
411}
412
413Error AidlComposer::getDisplayName(Display display, std::string* outName) {
414 const auto status = mAidlComposerClient->getDisplayName(translate<int64_t>(display), outName);
415 if (!status.isOk()) {
416 ALOGE("getDisplayName failed %s", status.getDescription().c_str());
417 return static_cast<Error>(status.getServiceSpecificError());
418 }
419 return Error::NONE;
420}
421
422Error AidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
423 std::vector<Layer>* outLayers,
424 std::vector<uint32_t>* outLayerRequestMasks) {
425 mReader.takeDisplayRequests(display, outDisplayRequestMask, outLayers, outLayerRequestMasks);
426 return Error::NONE;
427}
428
429Error AidlComposer::getDozeSupport(Display display, bool* outSupport) {
430 const auto status =
431 mAidlComposerClient->getDozeSupport(translate<int64_t>(display), outSupport);
432 if (!status.isOk()) {
433 ALOGE("getDozeSupport failed %s", status.getDescription().c_str());
434 return static_cast<Error>(status.getServiceSpecificError());
435 }
436 return Error::NONE;
437}
438
439Error AidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
440 float* outMaxLuminance, float* outMaxAverageLuminance,
441 float* outMinLuminance) {
442 AidlHdrCapabilities capabilities;
443 const auto status =
444 mAidlComposerClient->getHdrCapabilities(translate<int64_t>(display), &capabilities);
445 if (!status.isOk()) {
446 ALOGE("getHdrCapabilities failed %s", status.getDescription().c_str());
447 return static_cast<Error>(status.getServiceSpecificError());
448 }
449
450 *outTypes = translate<Hdr>(capabilities.types);
451 *outMaxLuminance = capabilities.maxLuminance;
452 *outMaxAverageLuminance = capabilities.maxAverageLuminance;
453 *outMinLuminance = capabilities.minLuminance;
454 return Error::NONE;
455}
456
457Error AidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
458 std::vector<int>* outReleaseFences) {
459 mReader.takeReleaseFences(display, outLayers, outReleaseFences);
460 return Error::NONE;
461}
462
463Error AidlComposer::presentDisplay(Display display, int* outPresentFence) {
464 ATRACE_NAME("HwcPresentDisplay");
465 mWriter.selectDisplay(translate<int64_t>(display));
466 mWriter.presentDisplay();
467
468 Error error = execute();
469 if (error != Error::NONE) {
470 return error;
471 }
472
473 mReader.takePresentFence(display, outPresentFence);
474
475 return Error::NONE;
476}
477
478Error AidlComposer::setActiveConfig(Display display, Config config) {
479 const auto status = mAidlComposerClient->setActiveConfig(translate<int64_t>(display),
480 translate<int32_t>(config));
481 if (!status.isOk()) {
482 ALOGE("setActiveConfig failed %s", status.getDescription().c_str());
483 return static_cast<Error>(status.getServiceSpecificError());
484 }
485 return Error::NONE;
486}
487
488Error AidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
489 int acquireFence, Dataspace dataspace,
490 const std::vector<IComposerClient::Rect>& damage) {
491 mWriter.selectDisplay(translate<int64_t>(display));
492
493 const native_handle_t* handle = nullptr;
494 if (target.get()) {
495 handle = target->getNativeBuffer()->handle;
496 }
497
498 mWriter.setClientTarget(slot, handle, acquireFence,
499 translate<aidl::android::hardware::graphics::common::Dataspace>(
500 dataspace),
501 translate<AidlRect>(damage));
502 return Error::NONE;
503}
504
505Error AidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
506 const auto status =
507 mAidlComposerClient->setColorMode(translate<int64_t>(display),
508 translate<AidlColorMode>(mode),
509 translate<AidlRenderIntent>(renderIntent));
510 if (!status.isOk()) {
511 ALOGE("setColorMode failed %s", status.getDescription().c_str());
512 return static_cast<Error>(status.getServiceSpecificError());
513 }
514 return Error::NONE;
515}
516
517Error AidlComposer::setColorTransform(Display display, const float* matrix, ColorTransform hint) {
518 mWriter.selectDisplay(translate<int64_t>(display));
519 mWriter.setColorTransform(matrix, translate<AidlColorTransform>(hint));
520 return Error::NONE;
521}
522
523Error AidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
524 int releaseFence) {
525 mWriter.selectDisplay(translate<int64_t>(display));
526 mWriter.setOutputBuffer(0, buffer, dup(releaseFence));
527 return Error::NONE;
528}
529
530Error AidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
531 const auto status = mAidlComposerClient->setPowerMode(translate<int64_t>(display),
532 translate<PowerMode>(mode));
533 if (!status.isOk()) {
534 ALOGE("setPowerMode failed %s", status.getDescription().c_str());
535 return static_cast<Error>(status.getServiceSpecificError());
536 }
537 return Error::NONE;
538}
539
540Error AidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
541 const bool enableVsync = enabled == IComposerClient::Vsync::ENABLE;
542 const auto status =
543 mAidlComposerClient->setVsyncEnabled(translate<int64_t>(display), enableVsync);
544 if (!status.isOk()) {
545 ALOGE("setVsyncEnabled failed %s", status.getDescription().c_str());
546 return static_cast<Error>(status.getServiceSpecificError());
547 }
548 return Error::NONE;
549}
550
551Error AidlComposer::setClientTargetSlotCount(Display display) {
552 const int32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
553 const auto status = mAidlComposerClient->setClientTargetSlotCount(translate<int64_t>(display),
554 bufferSlotCount);
555 if (!status.isOk()) {
556 ALOGE("setClientTargetSlotCount failed %s", status.getDescription().c_str());
557 return static_cast<Error>(status.getServiceSpecificError());
558 }
559 return Error::NONE;
560}
561
562Error AidlComposer::validateDisplay(Display display, uint32_t* outNumTypes,
563 uint32_t* outNumRequests) {
564 ATRACE_NAME("HwcValidateDisplay");
565 mWriter.selectDisplay(translate<int64_t>(display));
566 mWriter.validateDisplay();
567
568 Error error = execute();
569 if (error != Error::NONE) {
570 return error;
571 }
572
573 mReader.hasChanges(display, outNumTypes, outNumRequests);
574
575 return Error::NONE;
576}
577
578Error AidlComposer::presentOrValidateDisplay(Display display, uint32_t* outNumTypes,
579 uint32_t* outNumRequests, int* outPresentFence,
580 uint32_t* state) {
581 ATRACE_NAME("HwcPresentOrValidateDisplay");
582 mWriter.selectDisplay(translate<int64_t>(display));
583 mWriter.presentOrvalidateDisplay();
584
585 Error error = execute();
586 if (error != Error::NONE) {
587 return error;
588 }
589
590 mReader.takePresentOrValidateStage(display, state);
591
592 if (*state == 1) { // Present succeeded
593 mReader.takePresentFence(display, outPresentFence);
594 }
595
596 if (*state == 0) { // Validate succeeded.
597 mReader.hasChanges(display, outNumTypes, outNumRequests);
598 }
599
600 return Error::NONE;
601}
602
603Error AidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
604 mWriter.selectDisplay(translate<int64_t>(display));
605 mWriter.selectLayer(translate<int64_t>(layer));
606 mWriter.setLayerCursorPosition(x, y);
607 return Error::NONE;
608}
609
610Error AidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
611 const sp<GraphicBuffer>& buffer, int acquireFence) {
612 mWriter.selectDisplay(translate<int64_t>(display));
613 mWriter.selectLayer(translate<int64_t>(layer));
614
615 const native_handle_t* handle = nullptr;
616 if (buffer.get()) {
617 handle = buffer->getNativeBuffer()->handle;
618 }
619
620 mWriter.setLayerBuffer(slot, handle, acquireFence);
621 return Error::NONE;
622}
623
624Error AidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
625 const std::vector<IComposerClient::Rect>& damage) {
626 mWriter.selectDisplay(translate<int64_t>(display));
627 mWriter.selectLayer(translate<int64_t>(layer));
628 mWriter.setLayerSurfaceDamage(translate<AidlRect>(damage));
629 return Error::NONE;
630}
631
632Error AidlComposer::setLayerBlendMode(Display display, Layer layer,
633 IComposerClient::BlendMode mode) {
634 mWriter.selectDisplay(translate<int64_t>(display));
635 mWriter.selectLayer(translate<int64_t>(layer));
636 mWriter.setLayerBlendMode(translate<BlendMode>(mode));
637 return Error::NONE;
638}
639
640Error AidlComposer::setLayerColor(Display display, Layer layer,
641 const IComposerClient::Color& color) {
642 mWriter.selectDisplay(translate<int64_t>(display));
643 mWriter.selectLayer(translate<int64_t>(layer));
644 mWriter.setLayerColor(translate<Color>(color));
645 return Error::NONE;
646}
647
648Error AidlComposer::setLayerCompositionType(Display display, Layer layer,
649 IComposerClient::Composition type) {
650 mWriter.selectDisplay(translate<int64_t>(display));
651 mWriter.selectLayer(translate<int64_t>(layer));
652 mWriter.setLayerCompositionType(translate<Composition>(type));
653 return Error::NONE;
654}
655
656Error AidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
657 mWriter.selectDisplay(translate<int64_t>(display));
658 mWriter.selectLayer(translate<int64_t>(layer));
659 mWriter.setLayerDataspace(translate<AidlDataspace>(dataspace));
660 return Error::NONE;
661}
662
663Error AidlComposer::setLayerDisplayFrame(Display display, Layer layer,
664 const IComposerClient::Rect& frame) {
665 mWriter.selectDisplay(translate<int64_t>(display));
666 mWriter.selectLayer(translate<int64_t>(layer));
667 mWriter.setLayerDisplayFrame(translate<AidlRect>(frame));
668 return Error::NONE;
669}
670
671Error AidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
672 mWriter.selectDisplay(translate<int64_t>(display));
673 mWriter.selectLayer(translate<int64_t>(layer));
674 mWriter.setLayerPlaneAlpha(alpha);
675 return Error::NONE;
676}
677
678Error AidlComposer::setLayerSidebandStream(Display display, Layer layer,
679 const native_handle_t* stream) {
680 mWriter.selectDisplay(translate<int64_t>(display));
681 mWriter.selectLayer(translate<int64_t>(layer));
682 mWriter.setLayerSidebandStream(stream);
683 return Error::NONE;
684}
685
686Error AidlComposer::setLayerSourceCrop(Display display, Layer layer,
687 const IComposerClient::FRect& crop) {
688 mWriter.selectDisplay(translate<int64_t>(display));
689 mWriter.selectLayer(translate<int64_t>(layer));
690 mWriter.setLayerSourceCrop(translate<AidlFRect>(crop));
691 return Error::NONE;
692}
693
694Error AidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
695 mWriter.selectDisplay(translate<int64_t>(display));
696 mWriter.selectLayer(translate<int64_t>(layer));
697 mWriter.setLayerTransform(translate<AidlTransform>(transform));
698 return Error::NONE;
699}
700
701Error AidlComposer::setLayerVisibleRegion(Display display, Layer layer,
702 const std::vector<IComposerClient::Rect>& visible) {
703 mWriter.selectDisplay(translate<int64_t>(display));
704 mWriter.selectLayer(translate<int64_t>(layer));
705 mWriter.setLayerVisibleRegion(translate<AidlRect>(visible));
706 return Error::NONE;
707}
708
709Error AidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
710 mWriter.selectDisplay(translate<int64_t>(display));
711 mWriter.selectLayer(translate<int64_t>(layer));
712 mWriter.setLayerZOrder(z);
713 return Error::NONE;
714}
715
716Error AidlComposer::execute() {
717 // prepare input command queue
718 bool queueChanged = false;
719 int32_t commandLength = 0;
720 std::vector<aidl::android::hardware::common::NativeHandle> commandHandles;
721 if (!mWriter.writeQueue(&queueChanged, &commandLength, &commandHandles)) {
722 mWriter.reset();
723 return Error::NO_RESOURCES;
724 }
725
726 // set up new input command queue if necessary
727 if (queueChanged) {
728 const auto status = mAidlComposerClient->setInputCommandQueue(mWriter.getMQDescriptor());
729 if (!status.isOk()) {
730 ALOGE("setInputCommandQueue failed %s", status.getDescription().c_str());
731 mWriter.reset();
732 return static_cast<Error>(status.getServiceSpecificError());
733 }
734 }
735
736 if (commandLength == 0) {
737 mWriter.reset();
738 return Error::NONE;
739 }
740
741 AidlExecuteCommandsStatus commandStatus;
742 auto status =
743 mAidlComposerClient->executeCommands(commandLength, commandHandles, &commandStatus);
744 // executeCommands can fail because of out-of-fd and we do not want to
745 // abort() in that case
746 if (!status.isOk()) {
747 ALOGE("executeCommands failed %s", status.getDescription().c_str());
748 return static_cast<Error>(status.getServiceSpecificError());
749 }
750
751 // set up new output command queue if necessary
752 if (commandStatus.queueChanged) {
753 ::aidl::android::hardware::common::fmq::MQDescriptor<
754 int32_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
755 outputCommandQueue;
756 status = mAidlComposerClient->getOutputCommandQueue(&outputCommandQueue);
757 if (!status.isOk()) {
758 ALOGE("getOutputCommandQueue failed %s", status.getDescription().c_str());
759 return static_cast<Error>(status.getServiceSpecificError());
760 }
761
762 mReader.setMQDescriptor(outputCommandQueue);
763 }
764
765 Error error;
766 if (mReader.readQueue(commandStatus.length, std::move(commandStatus.handles))) {
767 error = static_cast<Error>(mReader.parse());
768 mReader.reset();
769 } else {
770 error = Error::NO_RESOURCES;
771 }
772
773 if (error == Error::NONE) {
774 std::vector<AidlCommandReader::CommandError> commandErrors = mReader.takeErrors();
775
776 for (const auto& cmdErr : commandErrors) {
777 auto command = mWriter.getCommand(cmdErr.location);
778 if (command == Command::VALIDATE_DISPLAY || command == Command::PRESENT_DISPLAY ||
779 command == Command::PRESENT_OR_VALIDATE_DISPLAY) {
780 error = cmdErr.error;
781 } else {
782 ALOGW("command 0x%x generated error %d", command, cmdErr.error);
783 }
784 }
785 }
786
787 mWriter.reset();
788
789 return error;
790}
791
792Error AidlComposer::setLayerPerFrameMetadata(
793 Display display, Layer layer,
794 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
795 mWriter.selectDisplay(translate<int64_t>(display));
796 mWriter.selectLayer(translate<int64_t>(layer));
797 mWriter.setLayerPerFrameMetadata(translate<AidlPerFrameMetadata>(perFrameMetadatas));
798 return Error::NONE;
799}
800
801std::vector<IComposerClient::PerFrameMetadataKey> AidlComposer::getPerFrameMetadataKeys(
802 Display display) {
803 std::vector<AidlPerFrameMetadataKey> keys;
804 const auto status =
805 mAidlComposerClient->getPerFrameMetadataKeys(translate<int64_t>(display), &keys);
806 if (!status.isOk()) {
807 ALOGE("getPerFrameMetadataKeys failed %s", status.getDescription().c_str());
808 return {};
809 }
810 return translate<IComposerClient::PerFrameMetadataKey>(keys);
811}
812
813Error AidlComposer::getRenderIntents(Display display, ColorMode colorMode,
814 std::vector<RenderIntent>* outRenderIntents) {
815 std::vector<AidlRenderIntent> renderIntents;
816 const auto status = mAidlComposerClient->getRenderIntents(translate<int64_t>(display),
817 translate<AidlColorMode>(colorMode),
818 &renderIntents);
819 if (!status.isOk()) {
820 ALOGE("getRenderIntents failed %s", status.getDescription().c_str());
821 return static_cast<Error>(status.getServiceSpecificError());
822 }
823 *outRenderIntents = translate<RenderIntent>(renderIntents);
824 return Error::NONE;
825}
826
827Error AidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
828 std::vector<float> matrix;
829 const auto status =
830 mAidlComposerClient->getDataspaceSaturationMatrix(translate<AidlDataspace>(dataspace),
831 &matrix);
832 if (!status.isOk()) {
833 ALOGE("getDataspaceSaturationMatrix failed %s", status.getDescription().c_str());
834 return static_cast<Error>(status.getServiceSpecificError());
835 }
836 *outMatrix = makeMat4(matrix);
837 return Error::NONE;
838}
839
840Error AidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
841 std::vector<uint8_t>* outData) {
842 AidlDisplayIdentification displayIdentification;
843 const auto status =
844 mAidlComposerClient->getDisplayIdentificationData(translate<int64_t>(display),
845 &displayIdentification);
846 if (!status.isOk()) {
847 ALOGE("getDisplayIdentificationData failed %s", status.getDescription().c_str());
848 return static_cast<Error>(status.getServiceSpecificError());
849 }
850
851 *outPort = static_cast<uint8_t>(displayIdentification.port);
852 *outData = displayIdentification.data;
853
854 return Error::NONE;
855}
856
857Error AidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
858 mWriter.selectDisplay(translate<int64_t>(display));
859 mWriter.selectLayer(translate<int64_t>(layer));
860 mWriter.setLayerColorTransform(matrix);
861 return Error::NONE;
862}
863
864Error AidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
865 Dataspace* outDataspace,
866 uint8_t* outComponentMask) {
867 if (!outFormat || !outDataspace || !outComponentMask) {
868 return Error::BAD_PARAMETER;
869 }
870
871 AidlDisplayContentSamplingAttributes attributes;
872 const auto status =
873 mAidlComposerClient->getDisplayedContentSamplingAttributes(translate<int64_t>(display),
874 &attributes);
875 if (!status.isOk()) {
876 ALOGE("getDisplayedContentSamplingAttributes failed %s", status.getDescription().c_str());
877 return static_cast<Error>(status.getServiceSpecificError());
878 }
879
880 *outFormat = translate<PixelFormat>(attributes.format);
881 *outDataspace = translate<Dataspace>(attributes.dataspace);
882 *outComponentMask = static_cast<uint8_t>(attributes.componentMask);
883 return Error::NONE;
884}
885
886Error AidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
887 uint8_t componentMask, uint64_t maxFrames) {
888 const auto status =
889 mAidlComposerClient
890 ->setDisplayedContentSamplingEnabled(translate<int64_t>(display), enabled,
891 static_cast<AidlFormatColorComponent>(
892 componentMask),
893 static_cast<int64_t>(maxFrames));
894 if (!status.isOk()) {
895 ALOGE("setDisplayedContentSamplingEnabled failed %s", status.getDescription().c_str());
896 return static_cast<Error>(status.getServiceSpecificError());
897 }
898 return Error::NONE;
899}
900
901Error AidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
902 uint64_t timestamp, DisplayedFrameStats* outStats) {
903 if (!outStats) {
904 return Error::BAD_PARAMETER;
905 }
906
907 AidlDisplayContentSample sample;
908 const auto status =
909 mAidlComposerClient->getDisplayedContentSample(translate<int64_t>(display),
910 static_cast<int64_t>(maxFrames),
911 static_cast<int64_t>(timestamp),
912 &sample);
913 if (!status.isOk()) {
914 ALOGE("getDisplayedContentSample failed %s", status.getDescription().c_str());
915 return static_cast<Error>(status.getServiceSpecificError());
916 }
917 *outStats = translate<DisplayedFrameStats>(sample);
918 return Error::NONE;
919}
920
921Error AidlComposer::setLayerPerFrameMetadataBlobs(
922 Display display, Layer layer,
923 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
924 mWriter.selectDisplay(translate<int64_t>(display));
925 mWriter.selectLayer(translate<int64_t>(layer));
926 mWriter.setLayerPerFrameMetadataBlobs(translate<AidlPerFrameMetadataBlob>(metadata));
927 return Error::NONE;
928}
929
930Error AidlComposer::setDisplayBrightness(Display display, float brightness) {
931 const auto status =
932 mAidlComposerClient->setDisplayBrightness(translate<int64_t>(display), brightness);
933 if (!status.isOk()) {
934 ALOGE("setDisplayBrightness failed %s", status.getDescription().c_str());
935 return static_cast<Error>(status.getServiceSpecificError());
936 }
937 return Error::NONE;
938}
939
940Error AidlComposer::getDisplayCapabilities(Display display,
941 std::vector<DisplayCapability>* outCapabilities) {
942 std::vector<AidlDisplayCapability> capabilities;
943 const auto status =
944 mAidlComposerClient->getDisplayCapabilities(translate<int64_t>(display), &capabilities);
945 if (!status.isOk()) {
946 ALOGE("getDisplayCapabilities failed %s", status.getDescription().c_str());
947 return static_cast<Error>(status.getServiceSpecificError());
948 }
949 *outCapabilities = translate<DisplayCapability>(capabilities);
950 return Error::NONE;
951}
952
953V2_4::Error AidlComposer::getDisplayConnectionType(
954 Display display, IComposerClient::DisplayConnectionType* outType) {
955 AidlDisplayConnectionType type;
956 const auto status =
957 mAidlComposerClient->getDisplayConnectionType(translate<int64_t>(display), &type);
958 if (!status.isOk()) {
959 ALOGE("getDisplayConnectionType failed %s", status.getDescription().c_str());
960 return static_cast<V2_4::Error>(status.getServiceSpecificError());
961 }
962 *outType = translate<IComposerClient::DisplayConnectionType>(type);
963 return V2_4::Error::NONE;
964}
965
966V2_4::Error AidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
967 int32_t vsyncPeriod;
968 const auto status =
969 mAidlComposerClient->getDisplayVsyncPeriod(translate<int64_t>(display), &vsyncPeriod);
970 if (!status.isOk()) {
971 ALOGE("getDisplayVsyncPeriod failed %s", status.getDescription().c_str());
972 return static_cast<V2_4::Error>(status.getServiceSpecificError());
973 }
974 *outVsyncPeriod = translate<VsyncPeriodNanos>(vsyncPeriod);
975 return V2_4::Error::NONE;
976}
977
978V2_4::Error AidlComposer::setActiveConfigWithConstraints(
979 Display display, Config config,
980 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
981 VsyncPeriodChangeTimeline* outTimeline) {
982 AidlVsyncPeriodChangeTimeline timeline;
983 const auto status =
984 mAidlComposerClient
985 ->setActiveConfigWithConstraints(translate<int64_t>(display),
986 translate<int32_t>(config),
987 translate<AidlVsyncPeriodChangeConstraints>(
988 vsyncPeriodChangeConstraints),
989 &timeline);
990 if (!status.isOk()) {
991 ALOGE("setActiveConfigWithConstraints failed %s", status.getDescription().c_str());
992 return static_cast<V2_4::Error>(status.getServiceSpecificError());
993 }
994 *outTimeline = translate<VsyncPeriodChangeTimeline>(timeline);
995 return V2_4::Error::NONE;
996}
997
998V2_4::Error AidlComposer::setAutoLowLatencyMode(Display display, bool on) {
999 const auto status = mAidlComposerClient->setAutoLowLatencyMode(translate<int64_t>(display), on);
1000 if (!status.isOk()) {
1001 ALOGE("setAutoLowLatencyMode failed %s", status.getDescription().c_str());
1002 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1003 }
1004 return V2_4::Error::NONE;
1005}
1006
1007V2_4::Error AidlComposer::getSupportedContentTypes(
1008 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1009 std::vector<AidlContentType> types;
1010 const auto status =
1011 mAidlComposerClient->getSupportedContentTypes(translate<int64_t>(displayId), &types);
1012 if (!status.isOk()) {
1013 ALOGE("getSupportedContentTypes failed %s", status.getDescription().c_str());
1014 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1015 }
1016 *outSupportedContentTypes = translate<IComposerClient::ContentType>(types);
1017 return V2_4::Error::NONE;
1018}
1019
1020V2_4::Error AidlComposer::setContentType(Display display,
1021 IComposerClient::ContentType contentType) {
1022 const auto status =
1023 mAidlComposerClient->setContentType(translate<int64_t>(display),
1024 translate<AidlContentType>(contentType));
1025 if (!status.isOk()) {
1026 ALOGE("setContentType failed %s", status.getDescription().c_str());
1027 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1028 }
1029 return V2_4::Error::NONE;
1030}
1031
1032V2_4::Error AidlComposer::setLayerGenericMetadata(Display display, Layer layer,
1033 const std::string& key, bool mandatory,
1034 const std::vector<uint8_t>& value) {
1035 mWriter.selectDisplay(translate<int64_t>(display));
1036 mWriter.selectLayer(translate<int64_t>(layer));
1037 mWriter.setLayerGenericMetadata(key, mandatory, value);
1038 return V2_4::Error::NONE;
1039}
1040
1041V2_4::Error AidlComposer::getLayerGenericMetadataKeys(
1042 std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) {
1043 std::vector<AidlLayerGenericMetadataKey> keys;
1044 const auto status = mAidlComposerClient->getLayerGenericMetadataKeys(&keys);
1045 if (!status.isOk()) {
1046 ALOGE("getLayerGenericMetadataKeys failed %s", status.getDescription().c_str());
1047 return static_cast<V2_4::Error>(status.getServiceSpecificError());
1048 }
1049 *outKeys = translate<IComposerClient::LayerGenericMetadataKey>(keys);
1050 return V2_4::Error::NONE;
1051}
1052
1053Error AidlComposer::getClientTargetProperty(
1054 Display display, IComposerClient::ClientTargetProperty* outClientTargetProperty) {
1055 mReader.takeClientTargetProperty(display, outClientTargetProperty);
1056 return Error::NONE;
1057}
1058
1059AidlCommandReader::~AidlCommandReader() {
1060 resetData();
1061}
1062
1063int AidlCommandReader::parse() {
1064 resetData();
1065
1066 Command command;
1067 uint16_t length = 0;
1068
1069 while (!isEmpty()) {
1070 if (!beginCommand(&command, &length)) {
1071 break;
1072 }
1073
1074 bool parsed = false;
1075 switch (command) {
1076 case Command::SELECT_DISPLAY:
1077 parsed = parseSelectDisplay(length);
1078 break;
1079 case Command::SET_ERROR:
1080 parsed = parseSetError(length);
1081 break;
1082 case Command::SET_CHANGED_COMPOSITION_TYPES:
1083 parsed = parseSetChangedCompositionTypes(length);
1084 break;
1085 case Command::SET_DISPLAY_REQUESTS:
1086 parsed = parseSetDisplayRequests(length);
1087 break;
1088 case Command::SET_PRESENT_FENCE:
1089 parsed = parseSetPresentFence(length);
1090 break;
1091 case Command::SET_RELEASE_FENCES:
1092 parsed = parseSetReleaseFences(length);
1093 break;
1094 case Command ::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT:
1095 parsed = parseSetPresentOrValidateDisplayResult(length);
1096 break;
1097 case Command::SET_CLIENT_TARGET_PROPERTY:
1098 parsed = parseSetClientTargetProperty(length);
1099 break;
1100 default:
1101 parsed = false;
1102 break;
1103 }
1104
1105 endCommand();
1106
1107 if (!parsed) {
1108 ALOGE("failed to parse command 0x%x length %" PRIu16, command, length);
1109 break;
1110 }
1111 }
1112
1113 return isEmpty() ? 0 : AidlIComposerClient::EX_NO_RESOURCES;
1114}
1115
1116bool AidlCommandReader::parseSelectDisplay(uint16_t length) {
1117 if (length != CommandWriterBase::kSelectDisplayLength) {
1118 return false;
1119 }
1120
1121 mCurrentReturnData = &mReturnData[read64()];
1122
1123 return true;
1124}
1125
1126bool AidlCommandReader::parseSetError(uint16_t length) {
1127 if (length != CommandWriterBase::kSetErrorLength) {
1128 return false;
1129 }
1130
1131 auto location = read();
1132 auto error = static_cast<Error>(readSigned());
1133
1134 mErrors.emplace_back(CommandError{location, error});
1135
1136 return true;
1137}
1138
1139bool AidlCommandReader::parseSetChangedCompositionTypes(uint16_t length) {
1140 // (layer id [64bit], composition type [32bit]) pairs
1141 static constexpr int kCommandWords = 3;
1142
1143 if (length % kCommandWords != 0 || !mCurrentReturnData) {
1144 return false;
1145 }
1146
1147 uint32_t count = length / kCommandWords;
1148 mCurrentReturnData->changedLayers.reserve(count);
1149 mCurrentReturnData->compositionTypes.reserve(count);
1150 while (count > 0) {
1151 auto layer = read64();
1152 auto type = static_cast<IComposerClient::Composition>(readSigned());
1153
1154 mCurrentReturnData->changedLayers.push_back(layer);
1155 mCurrentReturnData->compositionTypes.push_back(type);
1156
1157 count--;
1158 }
1159
1160 return true;
1161}
1162
1163bool AidlCommandReader::parseSetDisplayRequests(uint16_t length) {
1164 // display requests [32 bit] followed by
1165 // (layer id [64bit], layer requests [32bit]) pairs
1166 static constexpr int kDisplayRequestsWords = 1;
1167 static constexpr int kCommandWords = 3;
1168 if (length % kCommandWords != kDisplayRequestsWords || !mCurrentReturnData) {
1169 return false;
1170 }
1171
1172 mCurrentReturnData->displayRequests = read();
1173
1174 uint32_t count = (length - kDisplayRequestsWords) / kCommandWords;
1175 mCurrentReturnData->requestedLayers.reserve(count);
1176 mCurrentReturnData->requestMasks.reserve(count);
1177 while (count > 0) {
1178 auto layer = read64();
1179 auto layerRequestMask = read();
1180
1181 mCurrentReturnData->requestedLayers.push_back(layer);
1182 mCurrentReturnData->requestMasks.push_back(layerRequestMask);
1183
1184 count--;
1185 }
1186
1187 return true;
1188}
1189
1190bool AidlCommandReader::parseSetPresentFence(uint16_t length) {
1191 if (length != CommandWriterBase::kSetPresentFenceLength || !mCurrentReturnData) {
1192 return false;
1193 }
1194
1195 if (mCurrentReturnData->presentFence >= 0) {
1196 close(mCurrentReturnData->presentFence);
1197 }
1198 mCurrentReturnData->presentFence = readFence();
1199
1200 return true;
1201}
1202
1203bool AidlCommandReader::parseSetReleaseFences(uint16_t length) {
1204 // (layer id [64bit], release fence index [32bit]) pairs
1205 static constexpr int kCommandWords = 3;
1206
1207 if (length % kCommandWords != 0 || !mCurrentReturnData) {
1208 return false;
1209 }
1210
1211 uint32_t count = length / kCommandWords;
1212 mCurrentReturnData->releasedLayers.reserve(count);
1213 mCurrentReturnData->releaseFences.reserve(count);
1214 while (count > 0) {
1215 auto layer = read64();
1216 auto fence = readFence();
1217
1218 mCurrentReturnData->releasedLayers.push_back(layer);
1219 mCurrentReturnData->releaseFences.push_back(fence);
1220
1221 count--;
1222 }
1223
1224 return true;
1225}
1226
1227bool AidlCommandReader::parseSetPresentOrValidateDisplayResult(uint16_t length) {
1228 if (length != CommandWriterBase::kPresentOrValidateDisplayResultLength || !mCurrentReturnData) {
1229 return false;
1230 }
1231 mCurrentReturnData->presentOrValidateState = read();
1232 return true;
1233}
1234
1235bool AidlCommandReader::parseSetClientTargetProperty(uint16_t length) {
1236 if (length != CommandWriterBase::kSetClientTargetPropertyLength || !mCurrentReturnData) {
1237 return false;
1238 }
1239 mCurrentReturnData->clientTargetProperty.pixelFormat = static_cast<PixelFormat>(readSigned());
1240 mCurrentReturnData->clientTargetProperty.dataspace = static_cast<Dataspace>(readSigned());
1241 return true;
1242}
1243
1244void AidlCommandReader::resetData() {
1245 mErrors.clear();
1246
1247 for (auto& data : mReturnData) {
1248 if (data.second.presentFence >= 0) {
1249 close(data.second.presentFence);
1250 }
1251 for (auto fence : data.second.releaseFences) {
1252 if (fence >= 0) {
1253 close(fence);
1254 }
1255 }
1256 }
1257
1258 mReturnData.clear();
1259 mCurrentReturnData = nullptr;
1260}
1261
1262std::vector<AidlCommandReader::CommandError> AidlCommandReader::takeErrors() {
1263 return std::move(mErrors);
1264}
1265
1266bool AidlCommandReader::hasChanges(Display display, uint32_t* outNumChangedCompositionTypes,
1267 uint32_t* outNumLayerRequestMasks) const {
1268 auto found = mReturnData.find(display);
1269 if (found == mReturnData.end()) {
1270 *outNumChangedCompositionTypes = 0;
1271 *outNumLayerRequestMasks = 0;
1272 return false;
1273 }
1274
1275 const ReturnData& data = found->second;
1276
1277 *outNumChangedCompositionTypes = static_cast<uint32_t>(data.compositionTypes.size());
1278 *outNumLayerRequestMasks = static_cast<uint32_t>(data.requestMasks.size());
1279
1280 return !(data.compositionTypes.empty() && data.requestMasks.empty());
1281}
1282
1283void AidlCommandReader::takeChangedCompositionTypes(
1284 Display display, std::vector<Layer>* outLayers,
1285 std::vector<IComposerClient::Composition>* outTypes) {
1286 auto found = mReturnData.find(display);
1287 if (found == mReturnData.end()) {
1288 outLayers->clear();
1289 outTypes->clear();
1290 return;
1291 }
1292
1293 ReturnData& data = found->second;
1294
1295 *outLayers = std::move(data.changedLayers);
1296 *outTypes = std::move(data.compositionTypes);
1297}
1298
1299void AidlCommandReader::takeDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
1300 std::vector<Layer>* outLayers,
1301 std::vector<uint32_t>* outLayerRequestMasks) {
1302 auto found = mReturnData.find(display);
1303 if (found == mReturnData.end()) {
1304 *outDisplayRequestMask = 0;
1305 outLayers->clear();
1306 outLayerRequestMasks->clear();
1307 return;
1308 }
1309
1310 ReturnData& data = found->second;
1311
1312 *outDisplayRequestMask = data.displayRequests;
1313 *outLayers = std::move(data.requestedLayers);
1314 *outLayerRequestMasks = std::move(data.requestMasks);
1315}
1316
1317void AidlCommandReader::takeReleaseFences(Display display, std::vector<Layer>* outLayers,
1318 std::vector<int>* outReleaseFences) {
1319 auto found = mReturnData.find(display);
1320 if (found == mReturnData.end()) {
1321 outLayers->clear();
1322 outReleaseFences->clear();
1323 return;
1324 }
1325
1326 ReturnData& data = found->second;
1327
1328 *outLayers = std::move(data.releasedLayers);
1329 *outReleaseFences = std::move(data.releaseFences);
1330}
1331
1332void AidlCommandReader::takePresentFence(Display display, int* outPresentFence) {
1333 auto found = mReturnData.find(display);
1334 if (found == mReturnData.end()) {
1335 *outPresentFence = -1;
1336 return;
1337 }
1338
1339 ReturnData& data = found->second;
1340
1341 *outPresentFence = data.presentFence;
1342 data.presentFence = -1;
1343}
1344
1345void AidlCommandReader::takePresentOrValidateStage(Display display, uint32_t* state) {
1346 auto found = mReturnData.find(display);
1347 if (found == mReturnData.end()) {
1348 *state = static_cast<uint32_t>(-1);
1349 return;
1350 }
1351 ReturnData& data = found->second;
1352 *state = data.presentOrValidateState;
1353}
1354
1355void AidlCommandReader::takeClientTargetProperty(
1356 Display display, IComposerClient::ClientTargetProperty* outClientTargetProperty) {
1357 auto found = mReturnData.find(display);
1358
1359 // If not found, return the default values.
1360 if (found == mReturnData.end()) {
1361 outClientTargetProperty->pixelFormat = PixelFormat::RGBA_8888;
1362 outClientTargetProperty->dataspace = Dataspace::UNKNOWN;
1363 return;
1364 }
1365
1366 ReturnData& data = found->second;
1367 *outClientTargetProperty = data.clientTargetProperty;
1368}
1369
Ady Abrahame7385f72021-09-05 00:54:25 -07001370} // namespace Hwc2
1371} // namespace android