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 #undef LOG_TAG
18 #define LOG_TAG "HwcComposer"
19
20 #include <inttypes.h>
21 #include <log/log.h>
22 #include <gui/BufferQueue.h>
23
24 #include "ComposerHal.h"
25
26 namespace android {
27
28 using hardware::Return;
29 using hardware::hidl_vec;
30 using hardware::hidl_handle;
31
32 namespace Hwc2 {
33
34 namespace {
35
36 class BufferHandle {
37 public:
BufferHandle(const native_handle_t * buffer)38 BufferHandle(const native_handle_t* buffer)
39 {
40 // nullptr is not a valid handle to HIDL
41 mHandle = (buffer) ? buffer : native_handle_init(mStorage, 0, 0);
42 }
43
operator const hidl_handle&() const44 operator const hidl_handle&() const
45 {
46 return mHandle;
47 }
48
49 private:
50 NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 0, 0);
51 hidl_handle mHandle;
52 };
53
54 class FenceHandle
55 {
56 public:
FenceHandle(int fd,bool owned)57 FenceHandle(int fd, bool owned)
58 : mOwned(owned)
59 {
60 native_handle_t* handle;
61 if (fd >= 0) {
62 handle = native_handle_init(mStorage, 1, 0);
63 handle->data[0] = fd;
64 } else {
65 // nullptr is not a valid handle to HIDL
66 handle = native_handle_init(mStorage, 0, 0);
67 }
68 mHandle = handle;
69 }
70
~FenceHandle()71 ~FenceHandle()
72 {
73 if (mOwned) {
74 native_handle_close(mHandle);
75 }
76 }
77
operator const hidl_handle&() const78 operator const hidl_handle&() const
79 {
80 return mHandle;
81 }
82
83 private:
84 bool mOwned;
85 NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 1, 0);
86 hidl_handle mHandle;
87 };
88
89 // assume NO_RESOURCES when Status::isOk returns false
90 constexpr Error kDefaultError = Error::NO_RESOURCES;
91
92 template<typename T, typename U>
unwrapRet(Return<T> & ret,const U & default_val)93 T unwrapRet(Return<T>& ret, const U& default_val)
94 {
95 return (ret.isOk()) ? static_cast<T>(ret) :
96 static_cast<T>(default_val);
97 }
98
unwrapRet(Return<Error> & ret)99 Error unwrapRet(Return<Error>& ret)
100 {
101 return unwrapRet(ret, kDefaultError);
102 }
103
104 } // anonymous namespace
105
CommandWriter(uint32_t initialMaxSize)106 Composer::CommandWriter::CommandWriter(uint32_t initialMaxSize)
107 : CommandWriterBase(initialMaxSize) {}
108
~CommandWriter()109 Composer::CommandWriter::~CommandWriter()
110 {
111 }
112
setLayerInfo(uint32_t type,uint32_t appId)113 void Composer::CommandWriter::setLayerInfo(uint32_t type, uint32_t appId)
114 {
115 constexpr uint16_t kSetLayerInfoLength = 2;
116 beginCommand(
117 static_cast<IComposerClient::Command>(
118 IVrComposerClient::VrCommand::SET_LAYER_INFO),
119 kSetLayerInfoLength);
120 write(type);
121 write(appId);
122 endCommand();
123 }
124
setClientTargetMetadata(const IVrComposerClient::BufferMetadata & metadata)125 void Composer::CommandWriter::setClientTargetMetadata(
126 const IVrComposerClient::BufferMetadata& metadata)
127 {
128 constexpr uint16_t kSetClientTargetMetadataLength = 7;
129 beginCommand(
130 static_cast<IComposerClient::Command>(
131 IVrComposerClient::VrCommand::SET_CLIENT_TARGET_METADATA),
132 kSetClientTargetMetadataLength);
133 writeBufferMetadata(metadata);
134 endCommand();
135 }
136
setLayerBufferMetadata(const IVrComposerClient::BufferMetadata & metadata)137 void Composer::CommandWriter::setLayerBufferMetadata(
138 const IVrComposerClient::BufferMetadata& metadata)
139 {
140 constexpr uint16_t kSetLayerBufferMetadataLength = 7;
141 beginCommand(
142 static_cast<IComposerClient::Command>(
143 IVrComposerClient::VrCommand::SET_LAYER_BUFFER_METADATA),
144 kSetLayerBufferMetadataLength);
145 writeBufferMetadata(metadata);
146 endCommand();
147 }
148
writeBufferMetadata(const IVrComposerClient::BufferMetadata & metadata)149 void Composer::CommandWriter::writeBufferMetadata(
150 const IVrComposerClient::BufferMetadata& metadata)
151 {
152 write(metadata.width);
153 write(metadata.height);
154 write(metadata.stride);
155 write(metadata.layerCount);
156 writeSigned(static_cast<int32_t>(metadata.format));
157 write64(metadata.usage);
158 }
159
Composer(bool useVrComposer)160 Composer::Composer(bool useVrComposer)
161 : mWriter(kWriterInitialSize),
162 mIsUsingVrComposer(useVrComposer)
163 {
164 if (mIsUsingVrComposer) {
165 mComposer = IComposer::getService("vr");
166 } else {
167 mComposer = IComposer::getService(); // use default name
168 }
169
170 if (mComposer == nullptr) {
171 LOG_ALWAYS_FATAL("failed to get hwcomposer service");
172 }
173
174 mComposer->createClient(
175 [&](const auto& tmpError, const auto& tmpClient)
176 {
177 if (tmpError == Error::NONE) {
178 mClient = tmpClient;
179 }
180 });
181 if (mClient == nullptr) {
182 LOG_ALWAYS_FATAL("failed to create composer client");
183 }
184 }
185
getCapabilities()186 std::vector<IComposer::Capability> Composer::getCapabilities()
187 {
188 std::vector<IComposer::Capability> capabilities;
189 mComposer->getCapabilities(
190 [&](const auto& tmpCapabilities) {
191 capabilities = tmpCapabilities;
192 });
193
194 return capabilities;
195 }
196
dumpDebugInfo()197 std::string Composer::dumpDebugInfo()
198 {
199 std::string info;
200 mComposer->dumpDebugInfo([&](const auto& tmpInfo) {
201 info = tmpInfo.c_str();
202 });
203
204 return info;
205 }
206
registerCallback(const sp<IComposerCallback> & callback)207 void Composer::registerCallback(const sp<IComposerCallback>& callback)
208 {
209 auto ret = mClient->registerCallback(callback);
210 if (!ret.isOk()) {
211 ALOGE("failed to register IComposerCallback");
212 }
213 }
214
resetCommands()215 void Composer::resetCommands() {
216 mWriter.reset();
217 }
218
getMaxVirtualDisplayCount()219 uint32_t Composer::getMaxVirtualDisplayCount()
220 {
221 auto ret = mClient->getMaxVirtualDisplayCount();
222 return unwrapRet(ret, 0);
223 }
224
createVirtualDisplay(uint32_t width,uint32_t height,PixelFormat * format,Display * outDisplay)225 Error Composer::createVirtualDisplay(uint32_t width, uint32_t height,
226 PixelFormat* format, Display* outDisplay)
227 {
228 const uint32_t bufferSlotCount = 1;
229 Error error = kDefaultError;
230 mClient->createVirtualDisplay(width, height, *format, bufferSlotCount,
231 [&](const auto& tmpError, const auto& tmpDisplay,
232 const auto& tmpFormat) {
233 error = tmpError;
234 if (error != Error::NONE) {
235 return;
236 }
237
238 *outDisplay = tmpDisplay;
239 *format = tmpFormat;
240 });
241
242 return error;
243 }
244
destroyVirtualDisplay(Display display)245 Error Composer::destroyVirtualDisplay(Display display)
246 {
247 auto ret = mClient->destroyVirtualDisplay(display);
248 return unwrapRet(ret);
249 }
250
acceptDisplayChanges(Display display)251 Error Composer::acceptDisplayChanges(Display display)
252 {
253 mWriter.selectDisplay(display);
254 mWriter.acceptDisplayChanges();
255 return Error::NONE;
256 }
257
createLayer(Display display,Layer * outLayer)258 Error Composer::createLayer(Display display, Layer* outLayer)
259 {
260 Error error = kDefaultError;
261 mClient->createLayer(display, BufferQueue::NUM_BUFFER_SLOTS,
262 [&](const auto& tmpError, const auto& tmpLayer) {
263 error = tmpError;
264 if (error != Error::NONE) {
265 return;
266 }
267
268 *outLayer = tmpLayer;
269 });
270
271 return error;
272 }
273
destroyLayer(Display display,Layer layer)274 Error Composer::destroyLayer(Display display, Layer layer)
275 {
276 auto ret = mClient->destroyLayer(display, layer);
277 return unwrapRet(ret);
278 }
279
getActiveConfig(Display display,Config * outConfig)280 Error Composer::getActiveConfig(Display display, Config* outConfig)
281 {
282 Error error = kDefaultError;
283 mClient->getActiveConfig(display,
284 [&](const auto& tmpError, const auto& tmpConfig) {
285 error = tmpError;
286 if (error != Error::NONE) {
287 return;
288 }
289
290 *outConfig = tmpConfig;
291 });
292
293 return error;
294 }
295
getChangedCompositionTypes(Display display,std::vector<Layer> * outLayers,std::vector<IComposerClient::Composition> * outTypes)296 Error Composer::getChangedCompositionTypes(Display display,
297 std::vector<Layer>* outLayers,
298 std::vector<IComposerClient::Composition>* outTypes)
299 {
300 mReader.takeChangedCompositionTypes(display, outLayers, outTypes);
301 return Error::NONE;
302 }
303
getColorModes(Display display,std::vector<ColorMode> * outModes)304 Error Composer::getColorModes(Display display,
305 std::vector<ColorMode>* outModes)
306 {
307 Error error = kDefaultError;
308 mClient->getColorModes(display,
309 [&](const auto& tmpError, const auto& tmpModes) {
310 error = tmpError;
311 if (error != Error::NONE) {
312 return;
313 }
314
315 *outModes = tmpModes;
316 });
317
318 return error;
319 }
320
getDisplayAttribute(Display display,Config config,IComposerClient::Attribute attribute,int32_t * outValue)321 Error Composer::getDisplayAttribute(Display display, Config config,
322 IComposerClient::Attribute attribute, int32_t* outValue)
323 {
324 Error error = kDefaultError;
325 mClient->getDisplayAttribute(display, config, attribute,
326 [&](const auto& tmpError, const auto& tmpValue) {
327 error = tmpError;
328 if (error != Error::NONE) {
329 return;
330 }
331
332 *outValue = tmpValue;
333 });
334
335 return error;
336 }
337
getDisplayConfigs(Display display,std::vector<Config> * outConfigs)338 Error Composer::getDisplayConfigs(Display display,
339 std::vector<Config>* outConfigs)
340 {
341 Error error = kDefaultError;
342 mClient->getDisplayConfigs(display,
343 [&](const auto& tmpError, const auto& tmpConfigs) {
344 error = tmpError;
345 if (error != Error::NONE) {
346 return;
347 }
348
349 *outConfigs = tmpConfigs;
350 });
351
352 return error;
353 }
354
getDisplayName(Display display,std::string * outName)355 Error Composer::getDisplayName(Display display, std::string* outName)
356 {
357 Error error = kDefaultError;
358 mClient->getDisplayName(display,
359 [&](const auto& tmpError, const auto& tmpName) {
360 error = tmpError;
361 if (error != Error::NONE) {
362 return;
363 }
364
365 *outName = tmpName.c_str();
366 });
367
368 return error;
369 }
370
getDisplayRequests(Display display,uint32_t * outDisplayRequestMask,std::vector<Layer> * outLayers,std::vector<uint32_t> * outLayerRequestMasks)371 Error Composer::getDisplayRequests(Display display,
372 uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers,
373 std::vector<uint32_t>* outLayerRequestMasks)
374 {
375 mReader.takeDisplayRequests(display, outDisplayRequestMask,
376 outLayers, outLayerRequestMasks);
377 return Error::NONE;
378 }
379
getDisplayType(Display display,IComposerClient::DisplayType * outType)380 Error Composer::getDisplayType(Display display,
381 IComposerClient::DisplayType* outType)
382 {
383 Error error = kDefaultError;
384 mClient->getDisplayType(display,
385 [&](const auto& tmpError, const auto& tmpType) {
386 error = tmpError;
387 if (error != Error::NONE) {
388 return;
389 }
390
391 *outType = tmpType;
392 });
393
394 return error;
395 }
396
getDozeSupport(Display display,bool * outSupport)397 Error Composer::getDozeSupport(Display display, bool* outSupport)
398 {
399 Error error = kDefaultError;
400 mClient->getDozeSupport(display,
401 [&](const auto& tmpError, const auto& tmpSupport) {
402 error = tmpError;
403 if (error != Error::NONE) {
404 return;
405 }
406
407 *outSupport = tmpSupport;
408 });
409
410 return error;
411 }
412
getHdrCapabilities(Display display,std::vector<Hdr> * outTypes,float * outMaxLuminance,float * outMaxAverageLuminance,float * outMinLuminance)413 Error Composer::getHdrCapabilities(Display display,
414 std::vector<Hdr>* outTypes, float* outMaxLuminance,
415 float* outMaxAverageLuminance, float* outMinLuminance)
416 {
417 Error error = kDefaultError;
418 mClient->getHdrCapabilities(display,
419 [&](const auto& tmpError, const auto& tmpTypes,
420 const auto& tmpMaxLuminance,
421 const auto& tmpMaxAverageLuminance,
422 const auto& tmpMinLuminance) {
423 error = tmpError;
424 if (error != Error::NONE) {
425 return;
426 }
427
428 *outTypes = tmpTypes;
429 *outMaxLuminance = tmpMaxLuminance;
430 *outMaxAverageLuminance = tmpMaxAverageLuminance;
431 *outMinLuminance = tmpMinLuminance;
432 });
433
434 return error;
435 }
436
getReleaseFences(Display display,std::vector<Layer> * outLayers,std::vector<int> * outReleaseFences)437 Error Composer::getReleaseFences(Display display,
438 std::vector<Layer>* outLayers, std::vector<int>* outReleaseFences)
439 {
440 mReader.takeReleaseFences(display, outLayers, outReleaseFences);
441 return Error::NONE;
442 }
443
presentDisplay(Display display,int * outPresentFence)444 Error Composer::presentDisplay(Display display, int* outPresentFence)
445 {
446 mWriter.selectDisplay(display);
447 mWriter.presentDisplay();
448
449 Error error = execute();
450 if (error != Error::NONE) {
451 return error;
452 }
453
454 mReader.takePresentFence(display, outPresentFence);
455
456 return Error::NONE;
457 }
458
setActiveConfig(Display display,Config config)459 Error Composer::setActiveConfig(Display display, Config config)
460 {
461 auto ret = mClient->setActiveConfig(display, config);
462 return unwrapRet(ret);
463 }
464
setClientTarget(Display display,uint32_t slot,const sp<GraphicBuffer> & target,int acquireFence,Dataspace dataspace,const std::vector<IComposerClient::Rect> & damage)465 Error Composer::setClientTarget(Display display, uint32_t slot,
466 const sp<GraphicBuffer>& target,
467 int acquireFence, Dataspace dataspace,
468 const std::vector<IComposerClient::Rect>& damage)
469 {
470 mWriter.selectDisplay(display);
471 if (mIsUsingVrComposer && target.get()) {
472 IVrComposerClient::BufferMetadata metadata = {
473 .width = target->getWidth(),
474 .height = target->getHeight(),
475 .stride = target->getStride(),
476 .layerCount = target->getLayerCount(),
477 .format = static_cast<PixelFormat>(target->getPixelFormat()),
478 .usage = target->getUsage(),
479 };
480 mWriter.setClientTargetMetadata(metadata);
481 }
482
483 const native_handle_t* handle = nullptr;
484 if (target.get()) {
485 handle = target->getNativeBuffer()->handle;
486 }
487
488 mWriter.setClientTarget(slot, handle, acquireFence, dataspace, damage);
489 return Error::NONE;
490 }
491
setColorMode(Display display,ColorMode mode)492 Error Composer::setColorMode(Display display, ColorMode mode)
493 {
494 auto ret = mClient->setColorMode(display, mode);
495 return unwrapRet(ret);
496 }
497
setColorTransform(Display display,const float * matrix,ColorTransform hint)498 Error Composer::setColorTransform(Display display, const float* matrix,
499 ColorTransform hint)
500 {
501 mWriter.selectDisplay(display);
502 mWriter.setColorTransform(matrix, hint);
503 return Error::NONE;
504 }
505
setOutputBuffer(Display display,const native_handle_t * buffer,int releaseFence)506 Error Composer::setOutputBuffer(Display display, const native_handle_t* buffer,
507 int releaseFence)
508 {
509 mWriter.selectDisplay(display);
510 mWriter.setOutputBuffer(0, buffer, dup(releaseFence));
511 return Error::NONE;
512 }
513
setPowerMode(Display display,IComposerClient::PowerMode mode)514 Error Composer::setPowerMode(Display display, IComposerClient::PowerMode mode)
515 {
516 auto ret = mClient->setPowerMode(display, mode);
517 return unwrapRet(ret);
518 }
519
setVsyncEnabled(Display display,IComposerClient::Vsync enabled)520 Error Composer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled)
521 {
522 auto ret = mClient->setVsyncEnabled(display, enabled);
523 return unwrapRet(ret);
524 }
525
setClientTargetSlotCount(Display display)526 Error Composer::setClientTargetSlotCount(Display display)
527 {
528 const uint32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS;
529 auto ret = mClient->setClientTargetSlotCount(display, bufferSlotCount);
530 return unwrapRet(ret);
531 }
532
validateDisplay(Display display,uint32_t * outNumTypes,uint32_t * outNumRequests)533 Error Composer::validateDisplay(Display display, uint32_t* outNumTypes,
534 uint32_t* outNumRequests)
535 {
536 mWriter.selectDisplay(display);
537 mWriter.validateDisplay();
538
539 Error error = execute();
540 if (error != Error::NONE) {
541 return error;
542 }
543
544 mReader.hasChanges(display, outNumTypes, outNumRequests);
545
546 return Error::NONE;
547 }
548
presentOrValidateDisplay(Display display,uint32_t * outNumTypes,uint32_t * outNumRequests,int * outPresentFence,uint32_t * state)549 Error Composer::presentOrValidateDisplay(Display display, uint32_t* outNumTypes,
550 uint32_t* outNumRequests, int* outPresentFence, uint32_t* state) {
551 mWriter.selectDisplay(display);
552 mWriter.presentOrvalidateDisplay();
553
554 Error error = execute();
555 if (error != Error::NONE) {
556 return error;
557 }
558
559 mReader.takePresentOrValidateStage(display, state);
560
561 if (*state == 1) { // Present succeeded
562 mReader.takePresentFence(display, outPresentFence);
563 }
564
565 if (*state == 0) { // Validate succeeded.
566 mReader.hasChanges(display, outNumTypes, outNumRequests);
567 }
568
569 return Error::NONE;
570 }
571
setCursorPosition(Display display,Layer layer,int32_t x,int32_t y)572 Error Composer::setCursorPosition(Display display, Layer layer,
573 int32_t x, int32_t y)
574 {
575 mWriter.selectDisplay(display);
576 mWriter.selectLayer(layer);
577 mWriter.setLayerCursorPosition(x, y);
578 return Error::NONE;
579 }
580
setLayerBuffer(Display display,Layer layer,uint32_t slot,const sp<GraphicBuffer> & buffer,int acquireFence)581 Error Composer::setLayerBuffer(Display display, Layer layer,
582 uint32_t slot, const sp<GraphicBuffer>& buffer, int acquireFence)
583 {
584 mWriter.selectDisplay(display);
585 mWriter.selectLayer(layer);
586 if (mIsUsingVrComposer && buffer.get()) {
587 IVrComposerClient::BufferMetadata metadata = {
588 .width = buffer->getWidth(),
589 .height = buffer->getHeight(),
590 .stride = buffer->getStride(),
591 .layerCount = buffer->getLayerCount(),
592 .format = static_cast<PixelFormat>(buffer->getPixelFormat()),
593 .usage = buffer->getUsage(),
594 };
595 mWriter.setLayerBufferMetadata(metadata);
596 }
597
598 const native_handle_t* handle = nullptr;
599 if (buffer.get()) {
600 handle = buffer->getNativeBuffer()->handle;
601 }
602
603 mWriter.setLayerBuffer(slot, handle, acquireFence);
604 return Error::NONE;
605 }
606
setLayerSurfaceDamage(Display display,Layer layer,const std::vector<IComposerClient::Rect> & damage)607 Error Composer::setLayerSurfaceDamage(Display display, Layer layer,
608 const std::vector<IComposerClient::Rect>& damage)
609 {
610 mWriter.selectDisplay(display);
611 mWriter.selectLayer(layer);
612 mWriter.setLayerSurfaceDamage(damage);
613 return Error::NONE;
614 }
615
setLayerBlendMode(Display display,Layer layer,IComposerClient::BlendMode mode)616 Error Composer::setLayerBlendMode(Display display, Layer layer,
617 IComposerClient::BlendMode mode)
618 {
619 mWriter.selectDisplay(display);
620 mWriter.selectLayer(layer);
621 mWriter.setLayerBlendMode(mode);
622 return Error::NONE;
623 }
624
setLayerColor(Display display,Layer layer,const IComposerClient::Color & color)625 Error Composer::setLayerColor(Display display, Layer layer,
626 const IComposerClient::Color& color)
627 {
628 mWriter.selectDisplay(display);
629 mWriter.selectLayer(layer);
630 mWriter.setLayerColor(color);
631 return Error::NONE;
632 }
633
setLayerCompositionType(Display display,Layer layer,IComposerClient::Composition type)634 Error Composer::setLayerCompositionType(Display display, Layer layer,
635 IComposerClient::Composition type)
636 {
637 mWriter.selectDisplay(display);
638 mWriter.selectLayer(layer);
639 mWriter.setLayerCompositionType(type);
640 return Error::NONE;
641 }
642
setLayerDataspace(Display display,Layer layer,Dataspace dataspace)643 Error Composer::setLayerDataspace(Display display, Layer layer,
644 Dataspace dataspace)
645 {
646 mWriter.selectDisplay(display);
647 mWriter.selectLayer(layer);
648 mWriter.setLayerDataspace(dataspace);
649 return Error::NONE;
650 }
651
setLayerDisplayFrame(Display display,Layer layer,const IComposerClient::Rect & frame)652 Error Composer::setLayerDisplayFrame(Display display, Layer layer,
653 const IComposerClient::Rect& frame)
654 {
655 mWriter.selectDisplay(display);
656 mWriter.selectLayer(layer);
657 mWriter.setLayerDisplayFrame(frame);
658 return Error::NONE;
659 }
660
setLayerPlaneAlpha(Display display,Layer layer,float alpha)661 Error Composer::setLayerPlaneAlpha(Display display, Layer layer,
662 float alpha)
663 {
664 mWriter.selectDisplay(display);
665 mWriter.selectLayer(layer);
666 mWriter.setLayerPlaneAlpha(alpha);
667 return Error::NONE;
668 }
669
setLayerSidebandStream(Display display,Layer layer,const native_handle_t * stream)670 Error Composer::setLayerSidebandStream(Display display, Layer layer,
671 const native_handle_t* stream)
672 {
673 mWriter.selectDisplay(display);
674 mWriter.selectLayer(layer);
675 mWriter.setLayerSidebandStream(stream);
676 return Error::NONE;
677 }
678
setLayerSourceCrop(Display display,Layer layer,const IComposerClient::FRect & crop)679 Error Composer::setLayerSourceCrop(Display display, Layer layer,
680 const IComposerClient::FRect& crop)
681 {
682 mWriter.selectDisplay(display);
683 mWriter.selectLayer(layer);
684 mWriter.setLayerSourceCrop(crop);
685 return Error::NONE;
686 }
687
setLayerTransform(Display display,Layer layer,Transform transform)688 Error Composer::setLayerTransform(Display display, Layer layer,
689 Transform transform)
690 {
691 mWriter.selectDisplay(display);
692 mWriter.selectLayer(layer);
693 mWriter.setLayerTransform(transform);
694 return Error::NONE;
695 }
696
setLayerVisibleRegion(Display display,Layer layer,const std::vector<IComposerClient::Rect> & visible)697 Error Composer::setLayerVisibleRegion(Display display, Layer layer,
698 const std::vector<IComposerClient::Rect>& visible)
699 {
700 mWriter.selectDisplay(display);
701 mWriter.selectLayer(layer);
702 mWriter.setLayerVisibleRegion(visible);
703 return Error::NONE;
704 }
705
setLayerZOrder(Display display,Layer layer,uint32_t z)706 Error Composer::setLayerZOrder(Display display, Layer layer, uint32_t z)
707 {
708 mWriter.selectDisplay(display);
709 mWriter.selectLayer(layer);
710 mWriter.setLayerZOrder(z);
711 return Error::NONE;
712 }
713
setLayerInfo(Display display,Layer layer,uint32_t type,uint32_t appId)714 Error Composer::setLayerInfo(Display display, Layer layer, uint32_t type,
715 uint32_t appId)
716 {
717 if (mIsUsingVrComposer) {
718 mWriter.selectDisplay(display);
719 mWriter.selectLayer(layer);
720 mWriter.setLayerInfo(type, appId);
721 }
722 return Error::NONE;
723 }
724
execute()725 Error Composer::execute()
726 {
727 // prepare input command queue
728 bool queueChanged = false;
729 uint32_t commandLength = 0;
730 hidl_vec<hidl_handle> commandHandles;
731 if (!mWriter.writeQueue(&queueChanged, &commandLength, &commandHandles)) {
732 mWriter.reset();
733 return Error::NO_RESOURCES;
734 }
735
736 // set up new input command queue if necessary
737 if (queueChanged) {
738 auto ret = mClient->setInputCommandQueue(*mWriter.getMQDescriptor());
739 auto error = unwrapRet(ret);
740 if (error != Error::NONE) {
741 mWriter.reset();
742 return error;
743 }
744 }
745
746 Error error = kDefaultError;
747 mClient->executeCommands(commandLength, commandHandles,
748 [&](const auto& tmpError, const auto& tmpOutChanged,
749 const auto& tmpOutLength, const auto& tmpOutHandles)
750 {
751 error = tmpError;
752
753 // set up new output command queue if necessary
754 if (error == Error::NONE && tmpOutChanged) {
755 error = kDefaultError;
756 mClient->getOutputCommandQueue(
757 [&](const auto& tmpError,
758 const auto& tmpDescriptor)
759 {
760 error = tmpError;
761 if (error != Error::NONE) {
762 return;
763 }
764
765 mReader.setMQDescriptor(tmpDescriptor);
766 });
767 }
768
769 if (error != Error::NONE) {
770 return;
771 }
772
773 if (mReader.readQueue(tmpOutLength, tmpOutHandles)) {
774 error = mReader.parse();
775 mReader.reset();
776 } else {
777 error = Error::NO_RESOURCES;
778 }
779 });
780
781 if (error == Error::NONE) {
782 std::vector<CommandReader::CommandError> commandErrors =
783 mReader.takeErrors();
784
785 for (const auto& cmdErr : commandErrors) {
786 auto command = mWriter.getCommand(cmdErr.location);
787
788 if (command == IComposerClient::Command::VALIDATE_DISPLAY ||
789 command == IComposerClient::Command::PRESENT_DISPLAY ||
790 command == IComposerClient::Command::PRESENT_OR_VALIDATE_DISPLAY) {
791 error = cmdErr.error;
792 } else {
793 ALOGW("command 0x%x generated error %d",
794 command, cmdErr.error);
795 }
796 }
797 }
798
799 mWriter.reset();
800
801 return error;
802 }
803
~CommandReader()804 CommandReader::~CommandReader()
805 {
806 resetData();
807 }
808
parse()809 Error CommandReader::parse()
810 {
811 resetData();
812
813 IComposerClient::Command command;
814 uint16_t length = 0;
815
816 while (!isEmpty()) {
817 if (!beginCommand(&command, &length)) {
818 break;
819 }
820
821 bool parsed = false;
822 switch (command) {
823 case IComposerClient::Command::SELECT_DISPLAY:
824 parsed = parseSelectDisplay(length);
825 break;
826 case IComposerClient::Command::SET_ERROR:
827 parsed = parseSetError(length);
828 break;
829 case IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES:
830 parsed = parseSetChangedCompositionTypes(length);
831 break;
832 case IComposerClient::Command::SET_DISPLAY_REQUESTS:
833 parsed = parseSetDisplayRequests(length);
834 break;
835 case IComposerClient::Command::SET_PRESENT_FENCE:
836 parsed = parseSetPresentFence(length);
837 break;
838 case IComposerClient::Command::SET_RELEASE_FENCES:
839 parsed = parseSetReleaseFences(length);
840 break;
841 case IComposerClient::Command ::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT:
842 parsed = parseSetPresentOrValidateDisplayResult(length);
843 break;
844 default:
845 parsed = false;
846 break;
847 }
848
849 endCommand();
850
851 if (!parsed) {
852 ALOGE("failed to parse command 0x%x length %" PRIu16,
853 command, length);
854 break;
855 }
856 }
857
858 return isEmpty() ? Error::NONE : Error::NO_RESOURCES;
859 }
860
parseSelectDisplay(uint16_t length)861 bool CommandReader::parseSelectDisplay(uint16_t length)
862 {
863 if (length != CommandWriterBase::kSelectDisplayLength) {
864 return false;
865 }
866
867 mCurrentReturnData = &mReturnData[read64()];
868
869 return true;
870 }
871
parseSetError(uint16_t length)872 bool CommandReader::parseSetError(uint16_t length)
873 {
874 if (length != CommandWriterBase::kSetErrorLength) {
875 return false;
876 }
877
878 auto location = read();
879 auto error = static_cast<Error>(readSigned());
880
881 mErrors.emplace_back(CommandError{location, error});
882
883 return true;
884 }
885
parseSetChangedCompositionTypes(uint16_t length)886 bool CommandReader::parseSetChangedCompositionTypes(uint16_t length)
887 {
888 // (layer id, composition type) pairs
889 if (length % 3 != 0 || !mCurrentReturnData) {
890 return false;
891 }
892
893 uint32_t count = length / 3;
894 mCurrentReturnData->changedLayers.reserve(count);
895 mCurrentReturnData->compositionTypes.reserve(count);
896 while (count > 0) {
897 auto layer = read64();
898 auto type = static_cast<IComposerClient::Composition>(readSigned());
899
900 mCurrentReturnData->changedLayers.push_back(layer);
901 mCurrentReturnData->compositionTypes.push_back(type);
902
903 count--;
904 }
905
906 return true;
907 }
908
parseSetDisplayRequests(uint16_t length)909 bool CommandReader::parseSetDisplayRequests(uint16_t length)
910 {
911 // display requests followed by (layer id, layer requests) pairs
912 if (length % 3 != 1 || !mCurrentReturnData) {
913 return false;
914 }
915
916 mCurrentReturnData->displayRequests = read();
917
918 uint32_t count = (length - 1) / 3;
919 mCurrentReturnData->requestedLayers.reserve(count);
920 mCurrentReturnData->requestMasks.reserve(count);
921 while (count > 0) {
922 auto layer = read64();
923 auto layerRequestMask = read();
924
925 mCurrentReturnData->requestedLayers.push_back(layer);
926 mCurrentReturnData->requestMasks.push_back(layerRequestMask);
927
928 count--;
929 }
930
931 return true;
932 }
933
parseSetPresentFence(uint16_t length)934 bool CommandReader::parseSetPresentFence(uint16_t length)
935 {
936 if (length != CommandWriterBase::kSetPresentFenceLength ||
937 !mCurrentReturnData) {
938 return false;
939 }
940
941 if (mCurrentReturnData->presentFence >= 0) {
942 close(mCurrentReturnData->presentFence);
943 }
944 mCurrentReturnData->presentFence = readFence();
945
946 return true;
947 }
948
parseSetReleaseFences(uint16_t length)949 bool CommandReader::parseSetReleaseFences(uint16_t length)
950 {
951 // (layer id, release fence index) pairs
952 if (length % 3 != 0 || !mCurrentReturnData) {
953 return false;
954 }
955
956 uint32_t count = length / 3;
957 mCurrentReturnData->releasedLayers.reserve(count);
958 mCurrentReturnData->releaseFences.reserve(count);
959 while (count > 0) {
960 auto layer = read64();
961 auto fence = readFence();
962
963 mCurrentReturnData->releasedLayers.push_back(layer);
964 mCurrentReturnData->releaseFences.push_back(fence);
965
966 count--;
967 }
968
969 return true;
970 }
971
parseSetPresentOrValidateDisplayResult(uint16_t length)972 bool CommandReader::parseSetPresentOrValidateDisplayResult(uint16_t length)
973 {
974 if (length != CommandWriterBase::kPresentOrValidateDisplayResultLength || !mCurrentReturnData) {
975 return false;
976 }
977 mCurrentReturnData->presentOrValidateState = read();
978 return true;
979 }
980
resetData()981 void CommandReader::resetData()
982 {
983 mErrors.clear();
984
985 for (auto& data : mReturnData) {
986 if (data.second.presentFence >= 0) {
987 close(data.second.presentFence);
988 }
989 for (auto fence : data.second.releaseFences) {
990 if (fence >= 0) {
991 close(fence);
992 }
993 }
994 }
995
996 mReturnData.clear();
997 mCurrentReturnData = nullptr;
998 }
999
takeErrors()1000 std::vector<CommandReader::CommandError> CommandReader::takeErrors()
1001 {
1002 return std::move(mErrors);
1003 }
1004
hasChanges(Display display,uint32_t * outNumChangedCompositionTypes,uint32_t * outNumLayerRequestMasks) const1005 bool CommandReader::hasChanges(Display display,
1006 uint32_t* outNumChangedCompositionTypes,
1007 uint32_t* outNumLayerRequestMasks) const
1008 {
1009 auto found = mReturnData.find(display);
1010 if (found == mReturnData.end()) {
1011 *outNumChangedCompositionTypes = 0;
1012 *outNumLayerRequestMasks = 0;
1013 return false;
1014 }
1015
1016 const ReturnData& data = found->second;
1017
1018 *outNumChangedCompositionTypes = data.compositionTypes.size();
1019 *outNumLayerRequestMasks = data.requestMasks.size();
1020
1021 return !(data.compositionTypes.empty() && data.requestMasks.empty());
1022 }
1023
takeChangedCompositionTypes(Display display,std::vector<Layer> * outLayers,std::vector<IComposerClient::Composition> * outTypes)1024 void CommandReader::takeChangedCompositionTypes(Display display,
1025 std::vector<Layer>* outLayers,
1026 std::vector<IComposerClient::Composition>* outTypes)
1027 {
1028 auto found = mReturnData.find(display);
1029 if (found == mReturnData.end()) {
1030 outLayers->clear();
1031 outTypes->clear();
1032 return;
1033 }
1034
1035 ReturnData& data = found->second;
1036
1037 *outLayers = std::move(data.changedLayers);
1038 *outTypes = std::move(data.compositionTypes);
1039 }
1040
takeDisplayRequests(Display display,uint32_t * outDisplayRequestMask,std::vector<Layer> * outLayers,std::vector<uint32_t> * outLayerRequestMasks)1041 void CommandReader::takeDisplayRequests(Display display,
1042 uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers,
1043 std::vector<uint32_t>* outLayerRequestMasks)
1044 {
1045 auto found = mReturnData.find(display);
1046 if (found == mReturnData.end()) {
1047 *outDisplayRequestMask = 0;
1048 outLayers->clear();
1049 outLayerRequestMasks->clear();
1050 return;
1051 }
1052
1053 ReturnData& data = found->second;
1054
1055 *outDisplayRequestMask = data.displayRequests;
1056 *outLayers = std::move(data.requestedLayers);
1057 *outLayerRequestMasks = std::move(data.requestMasks);
1058 }
1059
takeReleaseFences(Display display,std::vector<Layer> * outLayers,std::vector<int> * outReleaseFences)1060 void CommandReader::takeReleaseFences(Display display,
1061 std::vector<Layer>* outLayers, std::vector<int>* outReleaseFences)
1062 {
1063 auto found = mReturnData.find(display);
1064 if (found == mReturnData.end()) {
1065 outLayers->clear();
1066 outReleaseFences->clear();
1067 return;
1068 }
1069
1070 ReturnData& data = found->second;
1071
1072 *outLayers = std::move(data.releasedLayers);
1073 *outReleaseFences = std::move(data.releaseFences);
1074 }
1075
takePresentFence(Display display,int * outPresentFence)1076 void CommandReader::takePresentFence(Display display, int* outPresentFence)
1077 {
1078 auto found = mReturnData.find(display);
1079 if (found == mReturnData.end()) {
1080 *outPresentFence = -1;
1081 return;
1082 }
1083
1084 ReturnData& data = found->second;
1085
1086 *outPresentFence = data.presentFence;
1087 data.presentFence = -1;
1088 }
1089
takePresentOrValidateStage(Display display,uint32_t * state)1090 void CommandReader::takePresentOrValidateStage(Display display, uint32_t* state) {
1091 auto found = mReturnData.find(display);
1092 if (found == mReturnData.end()) {
1093 *state= -1;
1094 return;
1095 }
1096 ReturnData& data = found->second;
1097 *state = data.presentOrValidateState;
1098 }
1099
1100 } // namespace Hwc2
1101
1102 } // namespace android
1103