Drew Davenport | 5951b11 | 2024-08-05 09:44:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | #include <unordered_map> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "Utils.h" |
| 21 | #include "aidl/android/hardware/graphics/composer3/CommandError.h" |
| 22 | #include "aidl/android/hardware/graphics/composer3/CommandResultPayload.h" |
| 23 | #include "aidl/android/hardware/graphics/composer3/PresentFence.h" |
| 24 | #include "aidl/android/hardware/graphics/composer3/PresentOrValidate.h" |
| 25 | #include "aidl/android/hardware/graphics/composer3/ReleaseFences.h" |
| 26 | |
| 27 | namespace aidl::android::hardware::graphics::composer3 { |
| 28 | |
| 29 | struct DisplayChanges { |
| 30 | std::optional<ChangedCompositionTypes> composition_changes; |
| 31 | std::optional<DisplayRequest> display_request_changes; |
| 32 | |
| 33 | void AddLayerCompositionChange(int64_t display_id, int64_t layer_id, |
| 34 | Composition layer_composition) { |
| 35 | if (!composition_changes) { |
| 36 | composition_changes.emplace(); |
| 37 | composition_changes->display = display_id; |
| 38 | } |
| 39 | |
| 40 | ChangedCompositionLayer composition_change; |
| 41 | composition_change.layer = layer_id; |
| 42 | composition_change.composition = layer_composition; |
| 43 | composition_changes->layers.emplace_back(composition_change); |
| 44 | } |
| 45 | |
| 46 | void ClearLayerCompositionChanges() { |
| 47 | composition_changes.reset(); |
| 48 | } |
| 49 | |
| 50 | bool HasAnyChanges() const { |
| 51 | return composition_changes.has_value() || |
| 52 | display_request_changes.has_value(); |
| 53 | } |
| 54 | |
| 55 | void Reset() { |
| 56 | composition_changes.reset(); |
| 57 | display_request_changes.reset(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class CommandResultWriter { |
| 62 | public: |
| 63 | explicit CommandResultWriter(std::vector<CommandResultPayload>* results) |
| 64 | : results_(results) { |
| 65 | } |
| 66 | |
| 67 | bool HasError() const { |
| 68 | return has_error_; |
| 69 | } |
| 70 | |
| 71 | void IncrementCommand() { |
| 72 | index_++; |
| 73 | has_error_ = false; |
| 74 | } |
| 75 | |
| 76 | void AddError(hwc3::Error error) { |
| 77 | CommandError command_error; |
| 78 | command_error.errorCode = static_cast<int32_t>(error); |
| 79 | command_error.commandIndex = static_cast<int32_t>(index_); |
| 80 | |
| 81 | results_->emplace_back(command_error); |
| 82 | has_error_ = true; |
| 83 | } |
| 84 | |
| 85 | void AddPresentFence(int64_t display_id, ::android::base::unique_fd fence) { |
| 86 | if (!fence.ok()) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | PresentFence present_fence; |
| 91 | present_fence.fence = ::ndk::ScopedFileDescriptor(fence.release()); |
| 92 | present_fence.display = display_id; |
| 93 | results_->emplace_back(std::move(present_fence)); |
| 94 | } |
| 95 | |
| 96 | void AddReleaseFence( |
| 97 | int64_t display_id, |
| 98 | std::unordered_map<int64_t, ::android::base::unique_fd>& layer_fences) { |
| 99 | ReleaseFences release_fences; |
| 100 | release_fences.display = display_id; |
| 101 | for (auto& [layer, fence] : layer_fences) { |
| 102 | if (!fence.ok()) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | ReleaseFences::Layer layer_result; |
| 107 | layer_result.layer = layer; |
| 108 | layer_result.fence = ::ndk::ScopedFileDescriptor(fence.release()); |
| 109 | |
| 110 | release_fences.layers.emplace_back(std::move(layer_result)); |
| 111 | } |
| 112 | |
| 113 | results_->emplace_back(std::move(release_fences)); |
| 114 | } |
| 115 | |
| 116 | void AddChanges(const DisplayChanges& changes) { |
| 117 | if (changes.composition_changes) { |
| 118 | results_->emplace_back(*changes.composition_changes); |
| 119 | } |
| 120 | if (changes.display_request_changes) { |
| 121 | results_->emplace_back(*changes.display_request_changes); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void AddPresentOrValidateResult(int64_t display_id, |
| 126 | const PresentOrValidate::Result& pov_result) { |
| 127 | PresentOrValidate pov_command; |
| 128 | pov_command.display = display_id; |
| 129 | pov_command.result = pov_result; |
| 130 | |
| 131 | results_->emplace_back(pov_command); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | size_t index_{0}; |
| 136 | bool has_error_{false}; |
| 137 | std::vector<CommandResultPayload>* results_{nullptr}; |
| 138 | }; |
| 139 | }; // namespace aidl::android::hardware::graphics::composer3 |