1 /*
2 * Copyright 2016 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #undef LOG_TAG
22 #define LOG_TAG "HwcComposer"
23 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
24
25 #include "HidlComposerHal.h"
26
27 #include <SurfaceFlingerProperties.h>
28 #include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
29 #include <android/binder_manager.h>
30 #include <composer-command-buffer/2.2/ComposerCommandBuffer.h>
31 #include <hidl/HidlTransportSupport.h>
32 #include <hidl/HidlTransportUtils.h>
33 #include <log/log.h>
34 #include <utils/Trace.h>
35
36 #include "HWC2.h"
37 #include "Hal.h"
38
39 #include <algorithm>
40 #include <cinttypes>
41
42 using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
43 using aidl::android::hardware::graphics::common::HdrConversionCapability;
44 using aidl::android::hardware::graphics::common::HdrConversionStrategy;
45 using aidl::android::hardware::graphics::composer3::Capability;
46 using aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness;
47 using aidl::android::hardware::graphics::composer3::DimmingStage;
48 using aidl::android::hardware::graphics::composer3::DisplayCapability;
49 using aidl::android::hardware::graphics::composer3::OverlayProperties;
50
51 namespace android {
52
53 using hardware::hidl_handle;
54 using hardware::hidl_vec;
55 using hardware::Return;
56
57 namespace Hwc2 {
58 namespace {
59
60 using android::hardware::Return;
61 using android::hardware::Void;
62 using android::HWC2::ComposerCallback;
63
64 class ComposerCallbackBridge : public IComposerCallback {
65 public:
ComposerCallbackBridge(ComposerCallback & callback,bool vsyncSwitchingSupported)66 ComposerCallbackBridge(ComposerCallback& callback, bool vsyncSwitchingSupported)
67 : mCallback(callback), mVsyncSwitchingSupported(vsyncSwitchingSupported) {}
68
69 // For code sharing purposes, `ComposerCallback` (implemented by SurfaceFlinger)
70 // replaced `onComposerHalHotplug` with `onComposerHalHotplugEvent` by converting
71 // from HIDL's connection into an AIDL DisplayHotplugEvent.
onHotplug(Display display,Connection connection)72 Return<void> onHotplug(Display display, Connection connection) override {
73 const auto event = connection == Connection::CONNECTED ? DisplayHotplugEvent::CONNECTED
74 : DisplayHotplugEvent::DISCONNECTED;
75 mCallback.onComposerHalHotplugEvent(display, event);
76 return Void();
77 }
78
onRefresh(Display display)79 Return<void> onRefresh(Display display) override {
80 mCallback.onComposerHalRefresh(display);
81 return Void();
82 }
83
onVsync(Display display,int64_t timestamp)84 Return<void> onVsync(Display display, int64_t timestamp) override {
85 if (!mVsyncSwitchingSupported) {
86 mCallback.onComposerHalVsync(display, timestamp, std::nullopt);
87 } else {
88 ALOGW("Unexpected onVsync callback on composer >= 2.4, ignoring.");
89 }
90 return Void();
91 }
92
onVsync_2_4(Display display,int64_t timestamp,VsyncPeriodNanos vsyncPeriodNanos)93 Return<void> onVsync_2_4(Display display, int64_t timestamp,
94 VsyncPeriodNanos vsyncPeriodNanos) override {
95 if (mVsyncSwitchingSupported) {
96 mCallback.onComposerHalVsync(display, timestamp, vsyncPeriodNanos);
97 } else {
98 ALOGW("Unexpected onVsync_2_4 callback on composer <= 2.3, ignoring.");
99 }
100 return Void();
101 }
102
onVsyncPeriodTimingChanged(Display display,const VsyncPeriodChangeTimeline & timeline)103 Return<void> onVsyncPeriodTimingChanged(Display display,
104 const VsyncPeriodChangeTimeline& timeline) override {
105 mCallback.onComposerHalVsyncPeriodTimingChanged(display, timeline);
106 return Void();
107 }
108
onSeamlessPossible(Display display)109 Return<void> onSeamlessPossible(Display display) override {
110 mCallback.onComposerHalSeamlessPossible(display);
111 return Void();
112 }
113
114 private:
115 ComposerCallback& mCallback;
116 const bool mVsyncSwitchingSupported;
117 };
118
119 } // namespace
120
121 HidlComposer::~HidlComposer() = default;
122
123 namespace {
124
125 class BufferHandle {
126 public:
BufferHandle(const native_handle_t * buffer)127 explicit BufferHandle(const native_handle_t* buffer) {
128 // nullptr is not a valid handle to HIDL
129 mHandle = (buffer) ? buffer : native_handle_init(mStorage, 0, 0);
130 }
131
operator const hidl_handle&() const132 operator const hidl_handle&() const // NOLINT(google-explicit-constructor)
133 {
134 return mHandle;
135 }
136
137 private:
138 NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 0, 0);
139 hidl_handle mHandle;
140 };
141
142 class FenceHandle {
143 public:
FenceHandle(int fd,bool owned)144 FenceHandle(int fd, bool owned) : mOwned(owned) {
145 native_handle_t* handle;
146 if (fd >= 0) {
147 handle = native_handle_init(mStorage, 1, 0);
148 handle->data[0] = fd;
149 } else {
150 // nullptr is not a valid handle to HIDL
151 handle = native_handle_init(mStorage, 0, 0);
152 }
153 mHandle = handle;
154 }
155
~FenceHandle()156 ~FenceHandle() {
157 if (mOwned) {
158 native_handle_close(mHandle);
159 }
160 }
161
operator const hidl_handle&() const162 operator const hidl_handle&() const // NOLINT(google-explicit-constructor)
163 {
164 return mHandle;
165 }
166
167 private:
168 bool mOwned;
169 NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 1, 0);
170 hidl_handle mHandle;
171 };
172
173 // assume NO_RESOURCES when Status::isOk returns false
174 constexpr Error kDefaultError = Error::NO_RESOURCES;
175 constexpr V2_4::Error kDefaultError_2_4 = static_cast<V2_4::Error>(kDefaultError);
176
177 template <typename T, typename U>
unwrapRet(Return<T> & ret,const U & default_val)178 T unwrapRet(Return<T>& ret, const U& default_val) {
179 return (ret.isOk()) ? static_cast<T>(ret) : static_cast<T>(default_val);
180 }
181
unwrapRet(Return<Error> & ret)182 Error unwrapRet(Return<Error>& ret) {
183 return unwrapRet(ret, kDefaultError);
184 }
185
186 template <typename To, typename From>
translate(From x)187 To translate(From x) {
188 return static_cast<To>(x);
189 }
190
191 template <typename To, typename From>
translate(const hidl_vec<From> & in)192 std::vector<To> translate(const hidl_vec<From>& in) {
193 std::vector<To> out;
194 out.reserve(in.size());
195 std::transform(in.begin(), in.end(), std::back_inserter(out),
196 [](From x) { return translate<To>(x); });
197 return out;
198 }
199
allocateClearSlotBuffer()200 sp<GraphicBuffer> allocateClearSlotBuffer() {
201 if (!sysprop::clear_slots_with_set_layer_buffer(false)) {
202 return nullptr;
203 }
204 sp<GraphicBuffer> buffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
205 GraphicBuffer::USAGE_HW_COMPOSER |
206 GraphicBuffer::USAGE_SW_READ_OFTEN |
207 GraphicBuffer::USAGE_SW_WRITE_OFTEN,
208 "HidlComposer");
209 if (!buffer || buffer->initCheck() != ::android::OK) {
210 return nullptr;
211 }
212 return std::move(buffer);
213 }
214
215 } // anonymous namespace
216
HidlComposer(const std::string & serviceName)217 HidlComposer::HidlComposer(const std::string& serviceName)
218 : mClearSlotBuffer(allocateClearSlotBuffer()), mWriter(kWriterInitialSize) {
219 mComposer = V2_1::IComposer::getService(serviceName);
220
221 if (mComposer == nullptr) {
222 LOG_ALWAYS_FATAL("failed to get hwcomposer service");
223 }
224
225 if (sp<IComposer> composer_2_4 = IComposer::castFrom(mComposer)) {
226 composer_2_4->createClient_2_4([&](const auto& tmpError, const auto& tmpClient) {
227 if (tmpError == V2_4::Error::NONE) {
228 mClient = tmpClient;
229 mClient_2_2 = tmpClient;
230 mClient_2_3 = tmpClient;
231 mClient_2_4 = tmpClient;
232 }
233 });
234 } else if (sp<V2_3::IComposer> composer_2_3 = V2_3::IComposer::castFrom(mComposer)) {
235 composer_2_3->createClient_2_3([&](const auto& tmpError, const auto& tmpClient) {
236 if (tmpError == Error::NONE) {
237 mClient = tmpClient;
238 mClient_2_2 = tmpClient;
239 mClient_2_3 = tmpClient;
240 }
241 });
242 } else {
243 mComposer->createClient([&](const auto& tmpError, const auto& tmpClient) {
244 if (tmpError != Error::NONE) {
245 return;
246 }
247
248 mClient = tmpClient;
249 if (sp<V2_2::IComposer> composer_2_2 = V2_2::IComposer::castFrom(mComposer)) {
250 mClient_2_2 = V2_2::IComposerClient::castFrom(mClient);
251 LOG_ALWAYS_FATAL_IF(mClient_2_2 == nullptr,
252 "IComposer 2.2 did not return IComposerClient 2.2");
253 }
254 });
255 }
256
257 if (mClient == nullptr) {
258 LOG_ALWAYS_FATAL("failed to create composer client");
259 }
260
261 if (!mClearSlotBuffer && sysprop::clear_slots_with_set_layer_buffer(false)) {
262 LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
263 return;
264 }
265 }
266
isSupported(OptionalFeature feature) const267 bool HidlComposer::isSupported(OptionalFeature feature) const {
268 switch (feature) {
269 case OptionalFeature::RefreshRateSwitching:
270 return mClient_2_4 != nullptr;
271 case OptionalFeature::ExpectedPresentTime:
272 case OptionalFeature::DisplayBrightnessCommand:
273 case OptionalFeature::KernelIdleTimer:
274 case OptionalFeature::PhysicalDisplayOrientation:
275 return false;
276 }
277 }
278
isVrrSupported() const279 bool HidlComposer::isVrrSupported() const {
280 // VRR is not supported on the HIDL composer.
281 return false;
282 };
283
getCapabilities()284 std::vector<Capability> HidlComposer::getCapabilities() {
285 std::vector<Capability> capabilities;
286 mComposer->getCapabilities([&](const auto& tmpCapabilities) {
287 capabilities = translate<Capability>(tmpCapabilities);
288 });
289 return capabilities;
290 }
291
dumpDebugInfo()292 std::string HidlComposer::dumpDebugInfo() {
293 std::string info;
294 info += std::string(mComposer->descriptor) + "\n";
295 mComposer->dumpDebugInfo([&](const auto& tmpInfo) { info = tmpInfo.c_str(); });
296
297 return info;
298 }
299
registerCallback(const sp<IComposerCallback> & callback)300 void HidlComposer::registerCallback(const sp<IComposerCallback>& callback) {
301 android::hardware::setMinSchedulerPolicy(callback, SCHED_FIFO, 2);
302
303 auto ret = [&]() {
304 if (mClient_2_4) {
305 return mClient_2_4->registerCallback_2_4(callback);
306 }
307 return mClient->registerCallback(callback);
308 }();
309 if (!ret.isOk()) {
310 ALOGE("failed to register IComposerCallback");
311 }
312 }
313
executeCommands(Display)314 Error HidlComposer::executeCommands(Display) {
315 return execute();
316 }
317
getMaxVirtualDisplayCount()318 uint32_t HidlComposer::getMaxVirtualDisplayCount() {
319 auto ret = mClient->getMaxVirtualDisplayCount();
320 return unwrapRet(ret, 0);
321 }
322
createVirtualDisplay(uint32_t width,uint32_t height,PixelFormat * format,Display * outDisplay)323 Error HidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
324 Display* outDisplay) {
325 const uint32_t bufferSlotCount = 1;
326 Error error = kDefaultError;
327 if (mClient_2_2) {
328 mClient_2_2->createVirtualDisplay_2_2(width, height,
329 static_cast<types::V1_1::PixelFormat>(*format),
330 bufferSlotCount,
331 [&](const auto& tmpError, const auto& tmpDisplay,
332 const auto& tmpFormat) {
333 error = tmpError;
334 if (error != Error::NONE) {
335 return;
336 }
337
338 *outDisplay = tmpDisplay;
339 *format = static_cast<types::V1_2::PixelFormat>(
340 tmpFormat);
341 });
342 } else {
343 mClient->createVirtualDisplay(width, height, static_cast<types::V1_0::PixelFormat>(*format),
344 bufferSlotCount,
345 [&](const auto& tmpError, const auto& tmpDisplay,
346 const auto& tmpFormat) {
347 error = tmpError;
348 if (error != Error::NONE) {
349 return;
350 }
351
352 *outDisplay = tmpDisplay;
353 *format = static_cast<PixelFormat>(tmpFormat);
354 });
355 }
356
357 return error;
358 }
359
destroyVirtualDisplay(Display display)360 Error HidlComposer::destroyVirtualDisplay(Display display) {
361 auto ret = mClient->destroyVirtualDisplay(display);
362 return unwrapRet(ret);
363 }
364
acceptDisplayChanges(Display display)365 Error HidlComposer::acceptDisplayChanges(Display display) {
366 mWriter.selectDisplay(display);
367 mWriter.acceptDisplayChanges();
368 return Error::NONE;
369 }
370
createLayer(Display display,Layer * outLayer)371 Error HidlComposer::createLayer(Display display, Layer* outLayer) {
372 Error error = kDefaultError;
373 mClient->createLayer(display, kMaxLayerBufferCount,
374 [&](const auto& tmpError, const auto& tmpLayer) {
375 error = tmpError;
376 if (error != Error::NONE) {
377 return;
378 }
379
380 *outLayer = tmpLayer;
381 });
382
383 return error;
384 }
385
destroyLayer(Display display,Layer layer)386 Error HidlComposer::destroyLayer(Display display, Layer layer) {
387 auto ret = mClient->destroyLayer(display, layer);
388 return unwrapRet(ret);
389 }
390
getActiveConfig(Display display,Config * outConfig)391 Error HidlComposer::getActiveConfig(Display display, Config* outConfig) {
392 Error error = kDefaultError;
393 mClient->getActiveConfig(display, [&](const auto& tmpError, const auto& tmpConfig) {
394 error = tmpError;
395 if (error != Error::NONE) {
396 return;
397 }
398
399 *outConfig = tmpConfig;
400 });
401
402 return error;
403 }
404
getChangedCompositionTypes(Display display,std::vector<Layer> * outLayers,std::vector<aidl::android::hardware::graphics::composer3::Composition> * outTypes)405 Error HidlComposer::getChangedCompositionTypes(
406 Display display, std::vector<Layer>* outLayers,
407 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
408 mReader.takeChangedCompositionTypes(display, outLayers, outTypes);
409 return Error::NONE;
410 }
411
getColorModes(Display display,std::vector<ColorMode> * outModes)412 Error HidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) {
413 Error error = kDefaultError;
414
415 if (mClient_2_3) {
416 mClient_2_3->getColorModes_2_3(display, [&](const auto& tmpError, const auto& tmpModes) {
417 error = tmpError;
418 if (error != Error::NONE) {
419 return;
420 }
421
422 *outModes = tmpModes;
423 });
424 } else if (mClient_2_2) {
425 mClient_2_2->getColorModes_2_2(display, [&](const auto& tmpError, const auto& tmpModes) {
426 error = tmpError;
427 if (error != Error::NONE) {
428 return;
429 }
430
431 for (types::V1_1::ColorMode colorMode : tmpModes) {
432 outModes->push_back(static_cast<ColorMode>(colorMode));
433 }
434 });
435 } else {
436 mClient->getColorModes(display, [&](const auto& tmpError, const auto& tmpModes) {
437 error = tmpError;
438 if (error != Error::NONE) {
439 return;
440 }
441 for (types::V1_0::ColorMode colorMode : tmpModes) {
442 outModes->push_back(static_cast<ColorMode>(colorMode));
443 }
444 });
445 }
446
447 return error;
448 }
449
getDisplayAttribute(Display display,Config config,IComposerClient::Attribute attribute,int32_t * outValue)450 Error HidlComposer::getDisplayAttribute(Display display, Config config,
451 IComposerClient::Attribute attribute, int32_t* outValue) {
452 Error error = kDefaultError;
453 if (mClient_2_4) {
454 mClient_2_4->getDisplayAttribute_2_4(display, config, attribute,
455 [&](const auto& tmpError, const auto& tmpValue) {
456 error = static_cast<Error>(tmpError);
457 if (error != Error::NONE) {
458 return;
459 }
460
461 *outValue = tmpValue;
462 });
463 } else {
464 mClient->getDisplayAttribute(display, config,
465 static_cast<V2_1::IComposerClient::Attribute>(attribute),
466 [&](const auto& tmpError, const auto& tmpValue) {
467 error = tmpError;
468 if (error != Error::NONE) {
469 return;
470 }
471
472 *outValue = tmpValue;
473 });
474 }
475
476 return error;
477 }
478
getDisplayConfigs(Display display,std::vector<Config> * outConfigs)479 Error HidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) {
480 Error error = kDefaultError;
481 mClient->getDisplayConfigs(display, [&](const auto& tmpError, const auto& tmpConfigs) {
482 error = tmpError;
483 if (error != Error::NONE) {
484 return;
485 }
486
487 *outConfigs = tmpConfigs;
488 });
489
490 return error;
491 }
492
getDisplayConfigurations(Display,int32_t,std::vector<DisplayConfiguration> *)493 Error HidlComposer::getDisplayConfigurations(Display, int32_t /*maxFrameIntervalNs*/,
494 std::vector<DisplayConfiguration>*) {
495 LOG_ALWAYS_FATAL("getDisplayConfigurations should not have been called on this, as "
496 "it's a HWC3 interface version 3 feature");
497 }
498
getDisplayName(Display display,std::string * outName)499 Error HidlComposer::getDisplayName(Display display, std::string* outName) {
500 Error error = kDefaultError;
501 mClient->getDisplayName(display, [&](const auto& tmpError, const auto& tmpName) {
502 error = tmpError;
503 if (error != Error::NONE) {
504 return;
505 }
506
507 *outName = tmpName.c_str();
508 });
509
510 return error;
511 }
512
getDisplayRequests(Display display,uint32_t * outDisplayRequestMask,std::vector<Layer> * outLayers,std::vector<uint32_t> * outLayerRequestMasks)513 Error HidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
514 std::vector<Layer>* outLayers,
515 std::vector<uint32_t>* outLayerRequestMasks) {
516 mReader.takeDisplayRequests(display, outDisplayRequestMask, outLayers, outLayerRequestMasks);
517 return Error::NONE;
518 }
519
getDozeSupport(Display display,bool * outSupport)520 Error HidlComposer::getDozeSupport(Display display, bool* outSupport) {
521 Error error = kDefaultError;
522 mClient->getDozeSupport(display, [&](const auto& tmpError, const auto& tmpSupport) {
523 error = tmpError;
524 if (error != Error::NONE) {
525 return;
526 }
527
528 *outSupport = tmpSupport;
529 });
530
531 return error;
532 }
533
hasDisplayIdleTimerCapability(Display,bool *)534 Error HidlComposer::hasDisplayIdleTimerCapability(Display, bool*) {
535 LOG_ALWAYS_FATAL("hasDisplayIdleTimerCapability should have never been called on this as "
536 "OptionalFeature::KernelIdleTimer is not supported on HIDL");
537 }
538
getHdrCapabilities(Display display,std::vector<Hdr> * outHdrTypes,float * outMaxLuminance,float * outMaxAverageLuminance,float * outMinLuminance)539 Error HidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outHdrTypes,
540 float* outMaxLuminance, float* outMaxAverageLuminance,
541 float* outMinLuminance) {
542 Error error = kDefaultError;
543 if (mClient_2_3) {
544 mClient_2_3->getHdrCapabilities_2_3(display,
545 [&](const auto& tmpError, const auto& tmpHdrTypes,
546 const auto& tmpMaxLuminance,
547 const auto& tmpMaxAverageLuminance,
548 const auto& tmpMinLuminance) {
549 error = tmpError;
550 if (error != Error::NONE) {
551 return;
552 }
553 *outHdrTypes = translate<ui::Hdr>(tmpHdrTypes);
554
555 *outMaxLuminance = tmpMaxLuminance;
556 *outMaxAverageLuminance = tmpMaxAverageLuminance;
557 *outMinLuminance = tmpMinLuminance;
558 });
559 } else {
560 mClient->getHdrCapabilities(display,
561 [&](const auto& tmpError, const auto& tmpHdrTypes,
562 const auto& tmpMaxLuminance,
563 const auto& tmpMaxAverageLuminance,
564 const auto& tmpMinLuminance) {
565 error = tmpError;
566 if (error != Error::NONE) {
567 return;
568 }
569 *outHdrTypes = translate<ui::Hdr>(tmpHdrTypes);
570
571 *outMaxLuminance = tmpMaxLuminance;
572 *outMaxAverageLuminance = tmpMaxAverageLuminance;
573 *outMinLuminance = tmpMinLuminance;
574 });
575 }
576
577 return error;
578 }
579
getOverlaySupport(OverlayProperties *)580 Error HidlComposer::getOverlaySupport(OverlayProperties* /*outProperties*/) {
581 return Error::NONE;
582 }
583
getReleaseFences(Display display,std::vector<Layer> * outLayers,std::vector<int> * outReleaseFences)584 Error HidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers,
585 std::vector<int>* outReleaseFences) {
586 mReader.takeReleaseFences(display, outLayers, outReleaseFences);
587 return Error::NONE;
588 }
589
presentDisplay(Display display,int * outPresentFence)590 Error HidlComposer::presentDisplay(Display display, int* outPresentFence) {
591 ATRACE_NAME("HwcPresentDisplay");
592 mWriter.selectDisplay(display);
593 mWriter.presentDisplay();
594
595 Error error = execute();
596 if (error != Error::NONE) {
597 return error;
598 }
599
600 mReader.takePresentFence(display, outPresentFence);
601
602 return Error::NONE;
603 }
604
setActiveConfig(Display display,Config config)605 Error HidlComposer::setActiveConfig(Display display, Config config) {
606 auto ret = mClient->setActiveConfig(display, config);
607 return unwrapRet(ret);
608 }
609
setClientTarget(Display display,uint32_t slot,const sp<GraphicBuffer> & target,int acquireFence,Dataspace dataspace,const std::vector<IComposerClient::Rect> & damage,float)610 Error HidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
611 int acquireFence, Dataspace dataspace,
612 const std::vector<IComposerClient::Rect>& damage,
613 float /*hdrSdrRatio*/) {
614 mWriter.selectDisplay(display);
615
616 const native_handle_t* handle = nullptr;
617 if (target.get()) {
618 handle = target->getNativeBuffer()->handle;
619 }
620
621 mWriter.setClientTarget(slot, handle, acquireFence, dataspace, damage);
622 return Error::NONE;
623 }
624
setColorMode(Display display,ColorMode mode,RenderIntent renderIntent)625 Error HidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) {
626 hardware::Return<Error> ret(kDefaultError);
627 if (mClient_2_3) {
628 ret = mClient_2_3->setColorMode_2_3(display, mode, renderIntent);
629 } else if (mClient_2_2) {
630 ret = mClient_2_2->setColorMode_2_2(display, static_cast<types::V1_1::ColorMode>(mode),
631 renderIntent);
632 } else {
633 ret = mClient->setColorMode(display, static_cast<types::V1_0::ColorMode>(mode));
634 }
635 return unwrapRet(ret);
636 }
637
setColorTransform(Display display,const float * matrix)638 Error HidlComposer::setColorTransform(Display display, const float* matrix) {
639 mWriter.selectDisplay(display);
640 const bool isIdentity = (mat4(matrix) == mat4());
641 mWriter.setColorTransform(matrix,
642 isIdentity ? ColorTransform::IDENTITY
643 : ColorTransform::ARBITRARY_MATRIX);
644 return Error::NONE;
645 }
646
setOutputBuffer(Display display,const native_handle_t * buffer,int releaseFence)647 Error HidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer,
648 int releaseFence) {
649 mWriter.selectDisplay(display);
650 mWriter.setOutputBuffer(0, buffer, dup(releaseFence));
651 return Error::NONE;
652 }
653
setPowerMode(Display display,IComposerClient::PowerMode mode)654 Error HidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) {
655 Return<Error> ret(Error::UNSUPPORTED);
656 if (mClient_2_2) {
657 ret = mClient_2_2->setPowerMode_2_2(display, mode);
658 } else if (mode != IComposerClient::PowerMode::ON_SUSPEND) {
659 ret = mClient->setPowerMode(display, static_cast<V2_1::IComposerClient::PowerMode>(mode));
660 }
661
662 return unwrapRet(ret);
663 }
664
setVsyncEnabled(Display display,IComposerClient::Vsync enabled)665 Error HidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) {
666 auto ret = mClient->setVsyncEnabled(display, enabled);
667 return unwrapRet(ret);
668 }
669
setClientTargetSlotCount(Display display)670 Error HidlComposer::setClientTargetSlotCount(Display display) {
671 const uint32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
672 auto ret = mClient->setClientTargetSlotCount(display, bufferSlotCount);
673 return unwrapRet(ret);
674 }
675
validateDisplay(Display display,nsecs_t,int32_t,uint32_t * outNumTypes,uint32_t * outNumRequests)676 Error HidlComposer::validateDisplay(Display display, nsecs_t /*expectedPresentTime*/,
677 int32_t /*frameIntervalNs*/, uint32_t* outNumTypes,
678 uint32_t* outNumRequests) {
679 ATRACE_NAME("HwcValidateDisplay");
680 mWriter.selectDisplay(display);
681 mWriter.validateDisplay();
682
683 Error error = execute();
684 if (error != Error::NONE) {
685 return error;
686 }
687
688 mReader.hasChanges(display, outNumTypes, outNumRequests);
689
690 return Error::NONE;
691 }
692
presentOrValidateDisplay(Display display,nsecs_t,int32_t,uint32_t * outNumTypes,uint32_t * outNumRequests,int * outPresentFence,uint32_t * state)693 Error HidlComposer::presentOrValidateDisplay(Display display, nsecs_t /*expectedPresentTime*/,
694 int32_t /*frameIntervalNs*/, uint32_t* outNumTypes,
695 uint32_t* outNumRequests, int* outPresentFence,
696 uint32_t* state) {
697 ATRACE_NAME("HwcPresentOrValidateDisplay");
698 mWriter.selectDisplay(display);
699 mWriter.presentOrvalidateDisplay();
700
701 Error error = execute();
702 if (error != Error::NONE) {
703 return error;
704 }
705
706 mReader.takePresentOrValidateStage(display, state);
707
708 if (*state == 1) { // Present succeeded
709 mReader.takePresentFence(display, outPresentFence);
710 }
711
712 if (*state == 0) { // Validate succeeded.
713 mReader.hasChanges(display, outNumTypes, outNumRequests);
714 }
715
716 return Error::NONE;
717 }
718
setCursorPosition(Display display,Layer layer,int32_t x,int32_t y)719 Error HidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) {
720 mWriter.selectDisplay(display);
721 mWriter.selectLayer(layer);
722 mWriter.setLayerCursorPosition(x, y);
723 return Error::NONE;
724 }
725
setLayerBuffer(Display display,Layer layer,uint32_t slot,const sp<GraphicBuffer> & buffer,int acquireFence)726 Error HidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot,
727 const sp<GraphicBuffer>& buffer, int acquireFence) {
728 mWriter.selectDisplay(display);
729 mWriter.selectLayer(layer);
730
731 const native_handle_t* handle = nullptr;
732 if (buffer.get()) {
733 handle = buffer->getNativeBuffer()->handle;
734 }
735
736 mWriter.setLayerBuffer(slot, handle, acquireFence);
737 return Error::NONE;
738 }
739
setLayerBufferSlotsToClear(Display display,Layer layer,const std::vector<uint32_t> & slotsToClear,uint32_t activeBufferSlot)740 Error HidlComposer::setLayerBufferSlotsToClear(Display display, Layer layer,
741 const std::vector<uint32_t>& slotsToClear,
742 uint32_t activeBufferSlot) {
743 if (slotsToClear.empty()) {
744 return Error::NONE;
745 }
746 // This can be null when the HAL hasn't explicitly enabled this feature.
747 if (mClearSlotBuffer == nullptr) {
748 return Error::NONE;
749 }
750 // Backwards compatible way of clearing buffer is to set the layer buffer with a placeholder
751 // buffer, using the slot that needs to cleared... tricky.
752 for (uint32_t slot : slotsToClear) {
753 // Don't clear the active buffer slot because we need to restore the active buffer after
754 // setting the requested buffer slots with a placeholder buffer.
755 if (slot != activeBufferSlot) {
756 mWriter.selectDisplay(display);
757 mWriter.selectLayer(layer);
758 mWriter.setLayerBuffer(slot, mClearSlotBuffer->handle, /*fence*/ -1);
759 }
760 }
761 // Since we clear buffers by setting them to a placeholder buffer, we want to make sure that the
762 // last setLayerBuffer command is sent with the currently active buffer, not the placeholder
763 // buffer, so that there is no perceptual change.
764 mWriter.selectDisplay(display);
765 mWriter.selectLayer(layer);
766 mWriter.setLayerBuffer(activeBufferSlot, /*buffer*/ nullptr, /*fence*/ -1);
767 return Error::NONE;
768 }
769
setLayerSurfaceDamage(Display display,Layer layer,const std::vector<IComposerClient::Rect> & damage)770 Error HidlComposer::setLayerSurfaceDamage(Display display, Layer layer,
771 const std::vector<IComposerClient::Rect>& damage) {
772 mWriter.selectDisplay(display);
773 mWriter.selectLayer(layer);
774 mWriter.setLayerSurfaceDamage(damage);
775 return Error::NONE;
776 }
777
setLayerBlendMode(Display display,Layer layer,IComposerClient::BlendMode mode)778 Error HidlComposer::setLayerBlendMode(Display display, Layer layer,
779 IComposerClient::BlendMode mode) {
780 mWriter.selectDisplay(display);
781 mWriter.selectLayer(layer);
782 mWriter.setLayerBlendMode(mode);
783 return Error::NONE;
784 }
785
to_hidl_type(aidl::android::hardware::graphics::composer3::Color color)786 static IComposerClient::Color to_hidl_type(
787 aidl::android::hardware::graphics::composer3::Color color) {
788 const auto floatColorToUint8Clamped = [](float val) -> uint8_t {
789 const auto intVal = static_cast<uint64_t>(std::round(255.0f * val));
790 const auto minVal = static_cast<uint64_t>(0);
791 const auto maxVal = static_cast<uint64_t>(255);
792 return std::clamp(intVal, minVal, maxVal);
793 };
794
795 return IComposerClient::Color{
796 floatColorToUint8Clamped(color.r),
797 floatColorToUint8Clamped(color.g),
798 floatColorToUint8Clamped(color.b),
799 floatColorToUint8Clamped(color.a),
800 };
801 }
802
setLayerColor(Display display,Layer layer,const aidl::android::hardware::graphics::composer3::Color & color)803 Error HidlComposer::setLayerColor(
804 Display display, Layer layer,
805 const aidl::android::hardware::graphics::composer3::Color& color) {
806 mWriter.selectDisplay(display);
807 mWriter.selectLayer(layer);
808 mWriter.setLayerColor(to_hidl_type(color));
809 return Error::NONE;
810 }
811
to_hidl_type(aidl::android::hardware::graphics::composer3::Composition type)812 static IComposerClient::Composition to_hidl_type(
813 aidl::android::hardware::graphics::composer3::Composition type) {
814 LOG_ALWAYS_FATAL_IF(static_cast<int32_t>(type) >
815 static_cast<int32_t>(IComposerClient::Composition::SIDEBAND),
816 "Trying to use %s, which is not supported by HidlComposer!",
817 android::to_string(type).c_str());
818
819 return static_cast<IComposerClient::Composition>(type);
820 }
821
setLayerCompositionType(Display display,Layer layer,aidl::android::hardware::graphics::composer3::Composition type)822 Error HidlComposer::setLayerCompositionType(
823 Display display, Layer layer,
824 aidl::android::hardware::graphics::composer3::Composition type) {
825 mWriter.selectDisplay(display);
826 mWriter.selectLayer(layer);
827 mWriter.setLayerCompositionType(to_hidl_type(type));
828 return Error::NONE;
829 }
830
setLayerDataspace(Display display,Layer layer,Dataspace dataspace)831 Error HidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) {
832 mWriter.selectDisplay(display);
833 mWriter.selectLayer(layer);
834 mWriter.setLayerDataspace(dataspace);
835 return Error::NONE;
836 }
837
setLayerDisplayFrame(Display display,Layer layer,const IComposerClient::Rect & frame)838 Error HidlComposer::setLayerDisplayFrame(Display display, Layer layer,
839 const IComposerClient::Rect& frame) {
840 mWriter.selectDisplay(display);
841 mWriter.selectLayer(layer);
842 mWriter.setLayerDisplayFrame(frame);
843 return Error::NONE;
844 }
845
setLayerPlaneAlpha(Display display,Layer layer,float alpha)846 Error HidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) {
847 mWriter.selectDisplay(display);
848 mWriter.selectLayer(layer);
849 mWriter.setLayerPlaneAlpha(alpha);
850 return Error::NONE;
851 }
852
setLayerSidebandStream(Display display,Layer layer,const native_handle_t * stream)853 Error HidlComposer::setLayerSidebandStream(Display display, Layer layer,
854 const native_handle_t* stream) {
855 mWriter.selectDisplay(display);
856 mWriter.selectLayer(layer);
857 mWriter.setLayerSidebandStream(stream);
858 return Error::NONE;
859 }
860
setLayerSourceCrop(Display display,Layer layer,const IComposerClient::FRect & crop)861 Error HidlComposer::setLayerSourceCrop(Display display, Layer layer,
862 const IComposerClient::FRect& crop) {
863 mWriter.selectDisplay(display);
864 mWriter.selectLayer(layer);
865 mWriter.setLayerSourceCrop(crop);
866 return Error::NONE;
867 }
868
setLayerTransform(Display display,Layer layer,Transform transform)869 Error HidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) {
870 mWriter.selectDisplay(display);
871 mWriter.selectLayer(layer);
872 mWriter.setLayerTransform(transform);
873 return Error::NONE;
874 }
875
setLayerVisibleRegion(Display display,Layer layer,const std::vector<IComposerClient::Rect> & visible)876 Error HidlComposer::setLayerVisibleRegion(Display display, Layer layer,
877 const std::vector<IComposerClient::Rect>& visible) {
878 mWriter.selectDisplay(display);
879 mWriter.selectLayer(layer);
880 mWriter.setLayerVisibleRegion(visible);
881 return Error::NONE;
882 }
883
setLayerZOrder(Display display,Layer layer,uint32_t z)884 Error HidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) {
885 mWriter.selectDisplay(display);
886 mWriter.selectLayer(layer);
887 mWriter.setLayerZOrder(z);
888 return Error::NONE;
889 }
890
execute()891 Error HidlComposer::execute() {
892 // prepare input command queue
893 bool queueChanged = false;
894 uint32_t commandLength = 0;
895 hidl_vec<hidl_handle> commandHandles;
896 if (!mWriter.writeQueue(&queueChanged, &commandLength, &commandHandles)) {
897 mWriter.reset();
898 return Error::NO_RESOURCES;
899 }
900
901 // set up new input command queue if necessary
902 if (queueChanged) {
903 auto ret = mClient->setInputCommandQueue(*mWriter.getMQDescriptor());
904 auto error = unwrapRet(ret);
905 if (error != Error::NONE) {
906 mWriter.reset();
907 return error;
908 }
909 }
910
911 if (commandLength == 0) {
912 mWriter.reset();
913 return Error::NONE;
914 }
915
916 Error error = kDefaultError;
917 hardware::Return<void> ret;
918 auto hidl_callback = [&](const auto& tmpError, const auto& tmpOutChanged,
919 const auto& tmpOutLength, const auto& tmpOutHandles) {
920 error = tmpError;
921
922 // set up new output command queue if necessary
923 if (error == Error::NONE && tmpOutChanged) {
924 error = kDefaultError;
925 mClient->getOutputCommandQueue([&](const auto& tmpError, const auto& tmpDescriptor) {
926 error = tmpError;
927 if (error != Error::NONE) {
928 return;
929 }
930
931 mReader.setMQDescriptor(tmpDescriptor);
932 });
933 }
934
935 if (error != Error::NONE) {
936 return;
937 }
938
939 if (mReader.readQueue(tmpOutLength, tmpOutHandles)) {
940 error = mReader.parse();
941 mReader.reset();
942 } else {
943 error = Error::NO_RESOURCES;
944 }
945 };
946 if (mClient_2_2) {
947 ret = mClient_2_2->executeCommands_2_2(commandLength, commandHandles, hidl_callback);
948 } else {
949 ret = mClient->executeCommands(commandLength, commandHandles, hidl_callback);
950 }
951 // executeCommands can fail because of out-of-fd and we do not want to
952 // abort() in that case
953 if (!ret.isOk()) {
954 ALOGE("executeCommands failed because of %s", ret.description().c_str());
955 }
956
957 if (error == Error::NONE) {
958 std::vector<CommandReader::CommandError> commandErrors = mReader.takeErrors();
959
960 for (const auto& cmdErr : commandErrors) {
961 auto command =
962 static_cast<IComposerClient::Command>(mWriter.getCommand(cmdErr.location));
963
964 if (command == IComposerClient::Command::VALIDATE_DISPLAY ||
965 command == IComposerClient::Command::PRESENT_DISPLAY ||
966 command == IComposerClient::Command::PRESENT_OR_VALIDATE_DISPLAY) {
967 error = cmdErr.error;
968 } else {
969 ALOGW("command 0x%x generated error %d", command, cmdErr.error);
970 }
971 }
972 }
973
974 mWriter.reset();
975
976 return error;
977 }
978
979 // Composer HAL 2.2
980
setLayerPerFrameMetadata(Display display,Layer layer,const std::vector<IComposerClient::PerFrameMetadata> & perFrameMetadatas)981 Error HidlComposer::setLayerPerFrameMetadata(
982 Display display, Layer layer,
983 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) {
984 if (!mClient_2_2) {
985 return Error::UNSUPPORTED;
986 }
987
988 mWriter.selectDisplay(display);
989 mWriter.selectLayer(layer);
990 mWriter.setLayerPerFrameMetadata(perFrameMetadatas);
991 return Error::NONE;
992 }
993
getPerFrameMetadataKeys(Display display)994 std::vector<IComposerClient::PerFrameMetadataKey> HidlComposer::getPerFrameMetadataKeys(
995 Display display) {
996 std::vector<IComposerClient::PerFrameMetadataKey> keys;
997 if (!mClient_2_2) {
998 return keys;
999 }
1000
1001 Error error = kDefaultError;
1002 if (mClient_2_3) {
1003 mClient_2_3->getPerFrameMetadataKeys_2_3(display,
1004 [&](const auto& tmpError, const auto& tmpKeys) {
1005 error = tmpError;
1006 if (error != Error::NONE) {
1007 ALOGW("getPerFrameMetadataKeys failed "
1008 "with %d",
1009 tmpError);
1010 return;
1011 }
1012 keys = tmpKeys;
1013 });
1014 } else {
1015 mClient_2_2
1016 ->getPerFrameMetadataKeys(display, [&](const auto& tmpError, const auto& tmpKeys) {
1017 error = tmpError;
1018 if (error != Error::NONE) {
1019 ALOGW("getPerFrameMetadataKeys failed with %d", tmpError);
1020 return;
1021 }
1022
1023 keys.clear();
1024 for (auto key : tmpKeys) {
1025 keys.push_back(static_cast<IComposerClient::PerFrameMetadataKey>(key));
1026 }
1027 });
1028 }
1029
1030 return keys;
1031 }
1032
getRenderIntents(Display display,ColorMode colorMode,std::vector<RenderIntent> * outRenderIntents)1033 Error HidlComposer::getRenderIntents(Display display, ColorMode colorMode,
1034 std::vector<RenderIntent>* outRenderIntents) {
1035 if (!mClient_2_2) {
1036 outRenderIntents->push_back(RenderIntent::COLORIMETRIC);
1037 return Error::NONE;
1038 }
1039
1040 Error error = kDefaultError;
1041
1042 auto getRenderIntentsLambda = [&](const auto& tmpError, const auto& tmpKeys) {
1043 error = tmpError;
1044 if (error != Error::NONE) {
1045 return;
1046 }
1047
1048 *outRenderIntents = tmpKeys;
1049 };
1050
1051 if (mClient_2_3) {
1052 mClient_2_3->getRenderIntents_2_3(display, colorMode, getRenderIntentsLambda);
1053 } else {
1054 mClient_2_2->getRenderIntents(display, static_cast<types::V1_1::ColorMode>(colorMode),
1055 getRenderIntentsLambda);
1056 }
1057
1058 return error;
1059 }
1060
getDataspaceSaturationMatrix(Dataspace dataspace,mat4 * outMatrix)1061 Error HidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) {
1062 if (!mClient_2_2) {
1063 *outMatrix = mat4();
1064 return Error::NONE;
1065 }
1066
1067 Error error = kDefaultError;
1068 mClient_2_2->getDataspaceSaturationMatrix(static_cast<types::V1_1::Dataspace>(dataspace),
1069 [&](const auto& tmpError, const auto& tmpMatrix) {
1070 error = tmpError;
1071 if (error != Error::NONE) {
1072 return;
1073 }
1074 *outMatrix = mat4(tmpMatrix.data());
1075 });
1076
1077 return error;
1078 }
1079
1080 // Composer HAL 2.3
1081
getDisplayIdentificationData(Display display,uint8_t * outPort,std::vector<uint8_t> * outData)1082 Error HidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort,
1083 std::vector<uint8_t>* outData) {
1084 if (!mClient_2_3) {
1085 return Error::UNSUPPORTED;
1086 }
1087
1088 Error error = kDefaultError;
1089 mClient_2_3->getDisplayIdentificationData(display,
1090 [&](const auto& tmpError, const auto& tmpPort,
1091 const auto& tmpData) {
1092 error = tmpError;
1093 if (error != Error::NONE) {
1094 return;
1095 }
1096
1097 *outPort = tmpPort;
1098 *outData = tmpData;
1099 });
1100
1101 return error;
1102 }
1103
setLayerColorTransform(Display display,Layer layer,const float * matrix)1104 Error HidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) {
1105 if (!mClient_2_3) {
1106 return Error::UNSUPPORTED;
1107 }
1108
1109 mWriter.selectDisplay(display);
1110 mWriter.selectLayer(layer);
1111 mWriter.setLayerColorTransform(matrix);
1112 return Error::NONE;
1113 }
1114
getDisplayedContentSamplingAttributes(Display display,PixelFormat * outFormat,Dataspace * outDataspace,uint8_t * outComponentMask)1115 Error HidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
1116 Dataspace* outDataspace,
1117 uint8_t* outComponentMask) {
1118 if (!outFormat || !outDataspace || !outComponentMask) {
1119 return Error::BAD_PARAMETER;
1120 }
1121 if (!mClient_2_3) {
1122 return Error::UNSUPPORTED;
1123 }
1124 Error error = kDefaultError;
1125 mClient_2_3->getDisplayedContentSamplingAttributes(display,
1126 [&](const auto tmpError,
1127 const auto& tmpFormat,
1128 const auto& tmpDataspace,
1129 const auto& tmpComponentMask) {
1130 error = tmpError;
1131 if (error == Error::NONE) {
1132 *outFormat = tmpFormat;
1133 *outDataspace = tmpDataspace;
1134 *outComponentMask =
1135 static_cast<uint8_t>(
1136 tmpComponentMask);
1137 }
1138 });
1139 return error;
1140 }
1141
setDisplayContentSamplingEnabled(Display display,bool enabled,uint8_t componentMask,uint64_t maxFrames)1142 Error HidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled,
1143 uint8_t componentMask, uint64_t maxFrames) {
1144 if (!mClient_2_3) {
1145 return Error::UNSUPPORTED;
1146 }
1147
1148 auto enable = enabled ? V2_3::IComposerClient::DisplayedContentSampling::ENABLE
1149 : V2_3::IComposerClient::DisplayedContentSampling::DISABLE;
1150 return mClient_2_3->setDisplayedContentSamplingEnabled(display, enable, componentMask,
1151 maxFrames);
1152 }
1153
getDisplayedContentSample(Display display,uint64_t maxFrames,uint64_t timestamp,DisplayedFrameStats * outStats)1154 Error HidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames,
1155 uint64_t timestamp, DisplayedFrameStats* outStats) {
1156 if (!outStats) {
1157 return Error::BAD_PARAMETER;
1158 }
1159 if (!mClient_2_3) {
1160 return Error::UNSUPPORTED;
1161 }
1162 Error error = kDefaultError;
1163 mClient_2_3->getDisplayedContentSample(display, maxFrames, timestamp,
1164 [&](const auto tmpError, auto tmpNumFrames,
1165 const auto& tmpSamples0, const auto& tmpSamples1,
1166 const auto& tmpSamples2, const auto& tmpSamples3) {
1167 error = tmpError;
1168 if (error == Error::NONE) {
1169 outStats->numFrames = tmpNumFrames;
1170 outStats->component_0_sample = tmpSamples0;
1171 outStats->component_1_sample = tmpSamples1;
1172 outStats->component_2_sample = tmpSamples2;
1173 outStats->component_3_sample = tmpSamples3;
1174 }
1175 });
1176 return error;
1177 }
1178
setLayerPerFrameMetadataBlobs(Display display,Layer layer,const std::vector<IComposerClient::PerFrameMetadataBlob> & metadata)1179 Error HidlComposer::setLayerPerFrameMetadataBlobs(
1180 Display display, Layer layer,
1181 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) {
1182 if (!mClient_2_3) {
1183 return Error::UNSUPPORTED;
1184 }
1185
1186 mWriter.selectDisplay(display);
1187 mWriter.selectLayer(layer);
1188 mWriter.setLayerPerFrameMetadataBlobs(metadata);
1189 return Error::NONE;
1190 }
1191
setDisplayBrightness(Display display,float brightness,float,const DisplayBrightnessOptions &)1192 Error HidlComposer::setDisplayBrightness(Display display, float brightness, float,
1193 const DisplayBrightnessOptions&) {
1194 if (!mClient_2_3) {
1195 return Error::UNSUPPORTED;
1196 }
1197 return mClient_2_3->setDisplayBrightness(display, brightness);
1198 }
1199
1200 // Composer HAL 2.4
1201
getDisplayCapabilities(Display display,std::vector<DisplayCapability> * outCapabilities)1202 Error HidlComposer::getDisplayCapabilities(Display display,
1203 std::vector<DisplayCapability>* outCapabilities) {
1204 if (!mClient_2_3) {
1205 return Error::UNSUPPORTED;
1206 }
1207
1208 V2_4::Error error = kDefaultError_2_4;
1209 if (mClient_2_4) {
1210 mClient_2_4->getDisplayCapabilities_2_4(display,
1211 [&](const auto& tmpError, const auto& tmpCaps) {
1212 error = tmpError;
1213 if (error != V2_4::Error::NONE) {
1214 return;
1215 }
1216 *outCapabilities =
1217 translate<DisplayCapability>(tmpCaps);
1218 });
1219 } else {
1220 mClient_2_3
1221 ->getDisplayCapabilities(display, [&](const auto& tmpError, const auto& tmpCaps) {
1222 error = static_cast<V2_4::Error>(tmpError);
1223 if (error != V2_4::Error::NONE) {
1224 return;
1225 }
1226
1227 *outCapabilities = translate<DisplayCapability>(tmpCaps);
1228 });
1229 }
1230
1231 return static_cast<Error>(error);
1232 }
1233
getDisplayConnectionType(Display display,IComposerClient::DisplayConnectionType * outType)1234 V2_4::Error HidlComposer::getDisplayConnectionType(
1235 Display display, IComposerClient::DisplayConnectionType* outType) {
1236 using Error = V2_4::Error;
1237 if (!mClient_2_4) {
1238 return Error::UNSUPPORTED;
1239 }
1240
1241 Error error = kDefaultError_2_4;
1242 mClient_2_4->getDisplayConnectionType(display, [&](const auto& tmpError, const auto& tmpType) {
1243 error = tmpError;
1244 if (error != V2_4::Error::NONE) {
1245 return;
1246 }
1247
1248 *outType = tmpType;
1249 });
1250
1251 return error;
1252 }
1253
getDisplayVsyncPeriod(Display display,VsyncPeriodNanos * outVsyncPeriod)1254 V2_4::Error HidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
1255 using Error = V2_4::Error;
1256 if (!mClient_2_4) {
1257 return Error::UNSUPPORTED;
1258 }
1259
1260 Error error = kDefaultError_2_4;
1261 mClient_2_4->getDisplayVsyncPeriod(display,
1262 [&](const auto& tmpError, const auto& tmpVsyncPeriod) {
1263 error = tmpError;
1264 if (error != Error::NONE) {
1265 return;
1266 }
1267
1268 *outVsyncPeriod = tmpVsyncPeriod;
1269 });
1270
1271 return error;
1272 }
1273
setActiveConfigWithConstraints(Display display,Config config,const IComposerClient::VsyncPeriodChangeConstraints & vsyncPeriodChangeConstraints,VsyncPeriodChangeTimeline * outTimeline)1274 V2_4::Error HidlComposer::setActiveConfigWithConstraints(
1275 Display display, Config config,
1276 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
1277 VsyncPeriodChangeTimeline* outTimeline) {
1278 using Error = V2_4::Error;
1279 if (!mClient_2_4) {
1280 return Error::UNSUPPORTED;
1281 }
1282
1283 Error error = kDefaultError_2_4;
1284 mClient_2_4->setActiveConfigWithConstraints(display, config, vsyncPeriodChangeConstraints,
1285 [&](const auto& tmpError, const auto& tmpTimeline) {
1286 error = tmpError;
1287 if (error != Error::NONE) {
1288 return;
1289 }
1290
1291 *outTimeline = tmpTimeline;
1292 });
1293
1294 return error;
1295 }
1296
setAutoLowLatencyMode(Display display,bool on)1297 V2_4::Error HidlComposer::setAutoLowLatencyMode(Display display, bool on) {
1298 using Error = V2_4::Error;
1299 if (!mClient_2_4) {
1300 return Error::UNSUPPORTED;
1301 }
1302
1303 return mClient_2_4->setAutoLowLatencyMode(display, on);
1304 }
1305
getSupportedContentTypes(Display displayId,std::vector<IComposerClient::ContentType> * outSupportedContentTypes)1306 V2_4::Error HidlComposer::getSupportedContentTypes(
1307 Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) {
1308 using Error = V2_4::Error;
1309 if (!mClient_2_4) {
1310 return Error::UNSUPPORTED;
1311 }
1312
1313 Error error = kDefaultError_2_4;
1314 mClient_2_4->getSupportedContentTypes(displayId,
1315 [&](const auto& tmpError,
1316 const auto& tmpSupportedContentTypes) {
1317 error = tmpError;
1318 if (error != Error::NONE) {
1319 return;
1320 }
1321
1322 *outSupportedContentTypes = tmpSupportedContentTypes;
1323 });
1324 return error;
1325 }
1326
setContentType(Display display,IComposerClient::ContentType contentType)1327 V2_4::Error HidlComposer::setContentType(Display display,
1328 IComposerClient::ContentType contentType) {
1329 using Error = V2_4::Error;
1330 if (!mClient_2_4) {
1331 return Error::UNSUPPORTED;
1332 }
1333
1334 return mClient_2_4->setContentType(display, contentType);
1335 }
1336
setLayerGenericMetadata(Display display,Layer layer,const std::string & key,bool mandatory,const std::vector<uint8_t> & value)1337 V2_4::Error HidlComposer::setLayerGenericMetadata(Display display, Layer layer,
1338 const std::string& key, bool mandatory,
1339 const std::vector<uint8_t>& value) {
1340 using Error = V2_4::Error;
1341 if (!mClient_2_4) {
1342 return Error::UNSUPPORTED;
1343 }
1344 mWriter.selectDisplay(display);
1345 mWriter.selectLayer(layer);
1346 mWriter.setLayerGenericMetadata(key, mandatory, value);
1347 return Error::NONE;
1348 }
1349
getLayerGenericMetadataKeys(std::vector<IComposerClient::LayerGenericMetadataKey> * outKeys)1350 V2_4::Error HidlComposer::getLayerGenericMetadataKeys(
1351 std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) {
1352 using Error = V2_4::Error;
1353 if (!mClient_2_4) {
1354 return Error::UNSUPPORTED;
1355 }
1356 Error error = kDefaultError_2_4;
1357 mClient_2_4->getLayerGenericMetadataKeys([&](const auto& tmpError, const auto& tmpKeys) {
1358 error = tmpError;
1359 if (error != Error::NONE) {
1360 return;
1361 }
1362
1363 *outKeys = tmpKeys;
1364 });
1365 return error;
1366 }
1367
setBootDisplayConfig(Display,Config)1368 Error HidlComposer::setBootDisplayConfig(Display /*displayId*/, Config) {
1369 return Error::UNSUPPORTED;
1370 }
1371
clearBootDisplayConfig(Display)1372 Error HidlComposer::clearBootDisplayConfig(Display /*displayId*/) {
1373 return Error::UNSUPPORTED;
1374 }
1375
getPreferredBootDisplayConfig(Display,Config *)1376 Error HidlComposer::getPreferredBootDisplayConfig(Display /*displayId*/, Config*) {
1377 return Error::UNSUPPORTED;
1378 }
1379
getHdrConversionCapabilities(std::vector<HdrConversionCapability> *)1380 Error HidlComposer::getHdrConversionCapabilities(std::vector<HdrConversionCapability>*) {
1381 return Error::UNSUPPORTED;
1382 }
1383
setHdrConversionStrategy(HdrConversionStrategy,Hdr *)1384 Error HidlComposer::setHdrConversionStrategy(HdrConversionStrategy, Hdr*) {
1385 return Error::UNSUPPORTED;
1386 }
1387
setRefreshRateChangedCallbackDebugEnabled(Display,bool)1388 Error HidlComposer::setRefreshRateChangedCallbackDebugEnabled(Display, bool) {
1389 return Error::UNSUPPORTED;
1390 }
1391
notifyExpectedPresent(Display,nsecs_t,int32_t)1392 Error HidlComposer::notifyExpectedPresent(Display, nsecs_t, int32_t) {
1393 return Error::UNSUPPORTED;
1394 }
1395
getClientTargetProperty(Display display,ClientTargetPropertyWithBrightness * outClientTargetProperty)1396 Error HidlComposer::getClientTargetProperty(
1397 Display display, ClientTargetPropertyWithBrightness* outClientTargetProperty) {
1398 IComposerClient::ClientTargetProperty property;
1399 mReader.takeClientTargetProperty(display, &property);
1400 outClientTargetProperty->display = display;
1401 outClientTargetProperty->clientTargetProperty.dataspace =
1402 static_cast<::aidl::android::hardware::graphics::common::Dataspace>(property.dataspace);
1403 outClientTargetProperty->clientTargetProperty.pixelFormat =
1404 static_cast<::aidl::android::hardware::graphics::common::PixelFormat>(
1405 property.pixelFormat);
1406 outClientTargetProperty->brightness = 1.f;
1407 outClientTargetProperty->dimmingStage = DimmingStage::NONE;
1408 return Error::NONE;
1409 }
1410
setLayerBrightness(Display,Layer,float)1411 Error HidlComposer::setLayerBrightness(Display, Layer, float) {
1412 return Error::NONE;
1413 }
1414
setLayerBlockingRegion(Display,Layer,const std::vector<IComposerClient::Rect> &)1415 Error HidlComposer::setLayerBlockingRegion(Display, Layer,
1416 const std::vector<IComposerClient::Rect>&) {
1417 return Error::NONE;
1418 }
1419
getDisplayDecorationSupport(Display,std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport> * support)1420 Error HidlComposer::getDisplayDecorationSupport(
1421 Display,
1422 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
1423 support) {
1424 support->reset();
1425 return Error::UNSUPPORTED;
1426 }
1427
setIdleTimerEnabled(Display,std::chrono::milliseconds)1428 Error HidlComposer::setIdleTimerEnabled(Display, std::chrono::milliseconds) {
1429 LOG_ALWAYS_FATAL("setIdleTimerEnabled should have never been called on this as "
1430 "OptionalFeature::KernelIdleTimer is not supported on HIDL");
1431 }
1432
getPhysicalDisplayOrientation(Display,AidlTransform *)1433 Error HidlComposer::getPhysicalDisplayOrientation(Display, AidlTransform*) {
1434 LOG_ALWAYS_FATAL("getPhysicalDisplayOrientation should have never been called on this as "
1435 "OptionalFeature::PhysicalDisplayOrientation is not supported on HIDL");
1436 }
1437
registerCallback(ComposerCallback & callback)1438 void HidlComposer::registerCallback(ComposerCallback& callback) {
1439 const bool vsyncSwitchingSupported =
1440 isSupported(Hwc2::Composer::OptionalFeature::RefreshRateSwitching);
1441
1442 registerCallback(sp<ComposerCallbackBridge>::make(callback, vsyncSwitchingSupported));
1443 }
1444
onHotplugConnect(Display)1445 void HidlComposer::onHotplugConnect(Display) {}
onHotplugDisconnect(Display)1446 void HidlComposer::onHotplugDisconnect(Display) {}
1447
~CommandReader()1448 CommandReader::~CommandReader() {
1449 resetData();
1450 }
1451
parse()1452 Error CommandReader::parse() {
1453 resetData();
1454
1455 IComposerClient::Command command;
1456 uint16_t length = 0;
1457
1458 while (!isEmpty()) {
1459 if (!beginCommand(&command, &length)) {
1460 break;
1461 }
1462
1463 bool parsed = false;
1464 switch (command) {
1465 case IComposerClient::Command::SELECT_DISPLAY:
1466 parsed = parseSelectDisplay(length);
1467 break;
1468 case IComposerClient::Command::SET_ERROR:
1469 parsed = parseSetError(length);
1470 break;
1471 case IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES:
1472 parsed = parseSetChangedCompositionTypes(length);
1473 break;
1474 case IComposerClient::Command::SET_DISPLAY_REQUESTS:
1475 parsed = parseSetDisplayRequests(length);
1476 break;
1477 case IComposerClient::Command::SET_PRESENT_FENCE:
1478 parsed = parseSetPresentFence(length);
1479 break;
1480 case IComposerClient::Command::SET_RELEASE_FENCES:
1481 parsed = parseSetReleaseFences(length);
1482 break;
1483 case IComposerClient::Command ::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT:
1484 parsed = parseSetPresentOrValidateDisplayResult(length);
1485 break;
1486 case IComposerClient::Command::SET_CLIENT_TARGET_PROPERTY:
1487 parsed = parseSetClientTargetProperty(length);
1488 break;
1489 default:
1490 parsed = false;
1491 break;
1492 }
1493
1494 endCommand();
1495
1496 if (!parsed) {
1497 ALOGE("failed to parse command 0x%x length %" PRIu16, command, length);
1498 break;
1499 }
1500 }
1501
1502 return isEmpty() ? Error::NONE : Error::NO_RESOURCES;
1503 }
1504
parseSelectDisplay(uint16_t length)1505 bool CommandReader::parseSelectDisplay(uint16_t length) {
1506 if (length != CommandWriterBase::kSelectDisplayLength) {
1507 return false;
1508 }
1509
1510 mCurrentReturnData = &mReturnData[read64()];
1511
1512 return true;
1513 }
1514
parseSetError(uint16_t length)1515 bool CommandReader::parseSetError(uint16_t length) {
1516 if (length != CommandWriterBase::kSetErrorLength) {
1517 return false;
1518 }
1519
1520 auto location = read();
1521 auto error = static_cast<Error>(readSigned());
1522
1523 mErrors.emplace_back(CommandError{location, error});
1524
1525 return true;
1526 }
1527
parseSetChangedCompositionTypes(uint16_t length)1528 bool CommandReader::parseSetChangedCompositionTypes(uint16_t length) {
1529 // (layer id, composition type) pairs
1530 if (length % 3 != 0 || !mCurrentReturnData) {
1531 return false;
1532 }
1533
1534 uint32_t count = length / 3;
1535 mCurrentReturnData->changedLayers.reserve(count);
1536 mCurrentReturnData->compositionTypes.reserve(count);
1537 while (count > 0) {
1538 auto layer = read64();
1539 auto type = static_cast<aidl::android::hardware::graphics::composer3::Composition>(
1540 readSigned());
1541
1542 mCurrentReturnData->changedLayers.push_back(layer);
1543 mCurrentReturnData->compositionTypes.push_back(type);
1544
1545 count--;
1546 }
1547
1548 return true;
1549 }
1550
parseSetDisplayRequests(uint16_t length)1551 bool CommandReader::parseSetDisplayRequests(uint16_t length) {
1552 // display requests followed by (layer id, layer requests) pairs
1553 if (length % 3 != 1 || !mCurrentReturnData) {
1554 return false;
1555 }
1556
1557 mCurrentReturnData->displayRequests = read();
1558
1559 uint32_t count = (length - 1) / 3;
1560 mCurrentReturnData->requestedLayers.reserve(count);
1561 mCurrentReturnData->requestMasks.reserve(count);
1562 while (count > 0) {
1563 auto layer = read64();
1564 auto layerRequestMask = read();
1565
1566 mCurrentReturnData->requestedLayers.push_back(layer);
1567 mCurrentReturnData->requestMasks.push_back(layerRequestMask);
1568
1569 count--;
1570 }
1571
1572 return true;
1573 }
1574
parseSetPresentFence(uint16_t length)1575 bool CommandReader::parseSetPresentFence(uint16_t length) {
1576 if (length != CommandWriterBase::kSetPresentFenceLength || !mCurrentReturnData) {
1577 return false;
1578 }
1579
1580 if (mCurrentReturnData->presentFence >= 0) {
1581 close(mCurrentReturnData->presentFence);
1582 }
1583 mCurrentReturnData->presentFence = readFence();
1584
1585 return true;
1586 }
1587
parseSetReleaseFences(uint16_t length)1588 bool CommandReader::parseSetReleaseFences(uint16_t length) {
1589 // (layer id, release fence index) pairs
1590 if (length % 3 != 0 || !mCurrentReturnData) {
1591 return false;
1592 }
1593
1594 uint32_t count = length / 3;
1595 mCurrentReturnData->releasedLayers.reserve(count);
1596 mCurrentReturnData->releaseFences.reserve(count);
1597 while (count > 0) {
1598 auto layer = read64();
1599 auto fence = readFence();
1600
1601 mCurrentReturnData->releasedLayers.push_back(layer);
1602 mCurrentReturnData->releaseFences.push_back(fence);
1603
1604 count--;
1605 }
1606
1607 return true;
1608 }
1609
parseSetPresentOrValidateDisplayResult(uint16_t length)1610 bool CommandReader::parseSetPresentOrValidateDisplayResult(uint16_t length) {
1611 if (length != CommandWriterBase::kPresentOrValidateDisplayResultLength || !mCurrentReturnData) {
1612 return false;
1613 }
1614 mCurrentReturnData->presentOrValidateState = read();
1615 return true;
1616 }
1617
parseSetClientTargetProperty(uint16_t length)1618 bool CommandReader::parseSetClientTargetProperty(uint16_t length) {
1619 if (length != CommandWriterBase::kSetClientTargetPropertyLength || !mCurrentReturnData) {
1620 return false;
1621 }
1622 mCurrentReturnData->clientTargetProperty.pixelFormat = static_cast<PixelFormat>(readSigned());
1623 mCurrentReturnData->clientTargetProperty.dataspace = static_cast<Dataspace>(readSigned());
1624 return true;
1625 }
1626
resetData()1627 void CommandReader::resetData() {
1628 mErrors.clear();
1629
1630 for (auto& data : mReturnData) {
1631 if (data.second.presentFence >= 0) {
1632 close(data.second.presentFence);
1633 }
1634 for (auto fence : data.second.releaseFences) {
1635 if (fence >= 0) {
1636 close(fence);
1637 }
1638 }
1639 }
1640
1641 mReturnData.clear();
1642 mCurrentReturnData = nullptr;
1643 }
1644
takeErrors()1645 std::vector<CommandReader::CommandError> CommandReader::takeErrors() {
1646 return std::move(mErrors);
1647 }
1648
hasChanges(Display display,uint32_t * outNumChangedCompositionTypes,uint32_t * outNumLayerRequestMasks) const1649 bool CommandReader::hasChanges(Display display, uint32_t* outNumChangedCompositionTypes,
1650 uint32_t* outNumLayerRequestMasks) const {
1651 auto found = mReturnData.find(display);
1652 if (found == mReturnData.end()) {
1653 *outNumChangedCompositionTypes = 0;
1654 *outNumLayerRequestMasks = 0;
1655 return false;
1656 }
1657
1658 const ReturnData& data = found->second;
1659
1660 *outNumChangedCompositionTypes = data.compositionTypes.size();
1661 *outNumLayerRequestMasks = data.requestMasks.size();
1662
1663 return !(data.compositionTypes.empty() && data.requestMasks.empty());
1664 }
1665
takeChangedCompositionTypes(Display display,std::vector<Layer> * outLayers,std::vector<aidl::android::hardware::graphics::composer3::Composition> * outTypes)1666 void CommandReader::takeChangedCompositionTypes(
1667 Display display, std::vector<Layer>* outLayers,
1668 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) {
1669 auto found = mReturnData.find(display);
1670 if (found == mReturnData.end()) {
1671 outLayers->clear();
1672 outTypes->clear();
1673 return;
1674 }
1675
1676 ReturnData& data = found->second;
1677
1678 *outLayers = std::move(data.changedLayers);
1679 *outTypes = std::move(data.compositionTypes);
1680 }
1681
takeDisplayRequests(Display display,uint32_t * outDisplayRequestMask,std::vector<Layer> * outLayers,std::vector<uint32_t> * outLayerRequestMasks)1682 void CommandReader::takeDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
1683 std::vector<Layer>* outLayers,
1684 std::vector<uint32_t>* outLayerRequestMasks) {
1685 auto found = mReturnData.find(display);
1686 if (found == mReturnData.end()) {
1687 *outDisplayRequestMask = 0;
1688 outLayers->clear();
1689 outLayerRequestMasks->clear();
1690 return;
1691 }
1692
1693 ReturnData& data = found->second;
1694
1695 *outDisplayRequestMask = data.displayRequests;
1696 *outLayers = std::move(data.requestedLayers);
1697 *outLayerRequestMasks = std::move(data.requestMasks);
1698 }
1699
takeReleaseFences(Display display,std::vector<Layer> * outLayers,std::vector<int> * outReleaseFences)1700 void CommandReader::takeReleaseFences(Display display, std::vector<Layer>* outLayers,
1701 std::vector<int>* outReleaseFences) {
1702 auto found = mReturnData.find(display);
1703 if (found == mReturnData.end()) {
1704 outLayers->clear();
1705 outReleaseFences->clear();
1706 return;
1707 }
1708
1709 ReturnData& data = found->second;
1710
1711 *outLayers = std::move(data.releasedLayers);
1712 *outReleaseFences = std::move(data.releaseFences);
1713 }
1714
takePresentFence(Display display,int * outPresentFence)1715 void CommandReader::takePresentFence(Display display, int* outPresentFence) {
1716 auto found = mReturnData.find(display);
1717 if (found == mReturnData.end()) {
1718 *outPresentFence = -1;
1719 return;
1720 }
1721
1722 ReturnData& data = found->second;
1723
1724 *outPresentFence = data.presentFence;
1725 data.presentFence = -1;
1726 }
1727
takePresentOrValidateStage(Display display,uint32_t * state)1728 void CommandReader::takePresentOrValidateStage(Display display, uint32_t* state) {
1729 auto found = mReturnData.find(display);
1730 if (found == mReturnData.end()) {
1731 *state = -1;
1732 return;
1733 }
1734 ReturnData& data = found->second;
1735 *state = data.presentOrValidateState;
1736 }
1737
takeClientTargetProperty(Display display,IComposerClient::ClientTargetProperty * outClientTargetProperty)1738 void CommandReader::takeClientTargetProperty(
1739 Display display, IComposerClient::ClientTargetProperty* outClientTargetProperty) {
1740 auto found = mReturnData.find(display);
1741
1742 // If not found, return the default values.
1743 if (found == mReturnData.end()) {
1744 outClientTargetProperty->pixelFormat = PixelFormat::RGBA_8888;
1745 outClientTargetProperty->dataspace = Dataspace::UNKNOWN;
1746 return;
1747 }
1748
1749 ReturnData& data = found->second;
1750 *outClientTargetProperty = data.clientTargetProperty;
1751 }
1752
1753 } // namespace Hwc2
1754 } // namespace android
1755
1756 // TODO(b/129481165): remove the #pragma below and fix conversion issues
1757 #pragma clang diagnostic pop // ignored "-Wconversion"
1758