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