xref: /aosp_15_r20/external/drm_hwcomposer/hwc3/ComposerResources.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 
2 /*
3  * Copyright (C) 2024 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define LOG_TAG "drmhwc"
19 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
20 
21 #include "ComposerResources.h"
22 
23 #include <aidlcommonsupport/NativeHandle.h>
24 
25 #include "hardware/hwcomposer2.h"
26 #include "hwc3/Utils.h"
27 
28 namespace aidl::android::hardware::graphics::composer3::impl {
29 
ToHwc2Display(uint64_t display_id)30 ::android::hardware::graphics::composer::V2_1::Display ToHwc2Display(
31     uint64_t display_id) {
32   return static_cast<::android::hardware::graphics::composer::V2_1::Display>(
33       display_id);
34 }
35 
ToHwc2Layer(int64_t layer_id)36 ::android::hardware::graphics::composer::V2_1::Layer ToHwc2Layer(
37     int64_t layer_id) {
38   return static_cast<::android::hardware::graphics::composer::V2_1::Layer>(
39       layer_id);
40 }
41 
42 std::unique_ptr<ComposerResourceReleaser>
CreateResourceReleaser(bool is_buffer)43 ComposerResources::CreateResourceReleaser(bool is_buffer) {
44   return std::make_unique<ComposerResourceReleaser>(is_buffer);
45 }
46 
Create()47 std::unique_ptr<ComposerResources> ComposerResources::Create() {
48   auto instance = std::unique_ptr<ComposerResources>(new ComposerResources);
49   if (instance->resources_ == nullptr) {
50     ALOGE("%s: Failed to initialise ComposerResources", __func__);
51     return nullptr;
52   }
53 
54   return instance;
55 }
56 
GetLayerBuffer(uint64_t display_id,int64_t layer_id,const Buffer & buffer,buffer_handle_t * out_buffer_handle,ComposerResourceReleaser * buf_releaser)57 hwc3::Error ComposerResources::GetLayerBuffer(
58     uint64_t display_id, int64_t layer_id, const Buffer& buffer,
59     buffer_handle_t* out_buffer_handle,
60     ComposerResourceReleaser* buf_releaser) {
61   auto display = ToHwc2Display(display_id);
62   auto layer = ToHwc2Layer(layer_id);
63 
64   const bool use_cache = !buffer.handle.has_value();
65   buffer_handle_t buffer_handle = nullptr;
66   if (buffer.handle.has_value()) {
67     buffer_handle = ::android::makeFromAidl(*buffer.handle);
68   }
69 
70   auto err = resources_->getLayerBuffer(display, layer, buffer.slot, use_cache,
71                                         buffer_handle, out_buffer_handle,
72                                         buf_releaser->GetReplacedHandle());
73 
74   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
75 }
76 
GetLayerSidebandStream(uint64_t display_id,int64_t layer_id,const aidl::android::hardware::common::NativeHandle & handle,buffer_handle_t * out_handle,ComposerResourceReleaser * releaser)77 hwc3::Error ComposerResources::GetLayerSidebandStream(
78     uint64_t display_id, int64_t layer_id,
79     const aidl::android::hardware::common::NativeHandle& handle,
80     buffer_handle_t* out_handle, ComposerResourceReleaser* releaser) {
81   auto display = ToHwc2Display(display_id);
82   auto layer = ToHwc2Layer(layer_id);
83 
84   auto err = resources_->getLayerSidebandStream(display, layer,
85                                                 ::android::makeFromAidl(handle),
86                                                 out_handle,
87                                                 releaser->GetReplacedHandle());
88 
89   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
90 }
91 
AddLayer(uint64_t display_id,int64_t layer_id,uint32_t buffer_cache_size)92 hwc3::Error ComposerResources::AddLayer(uint64_t display_id, int64_t layer_id,
93                                         uint32_t buffer_cache_size) {
94   auto display = ToHwc2Display(display_id);
95   auto layer = ToHwc2Layer(layer_id);
96 
97   auto err = resources_->addLayer(display, layer, buffer_cache_size);
98   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
99 }
100 
RemoveLayer(uint64_t display_id,int64_t layer_id)101 hwc3::Error ComposerResources::RemoveLayer(uint64_t display_id,
102                                            int64_t layer_id) {
103   auto display = ToHwc2Display(display_id);
104   auto layer = ToHwc2Layer(layer_id);
105 
106   auto err = resources_->removeLayer(display, layer);
107   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
108 }
109 
HasDisplay(uint64_t display_id)110 bool ComposerResources::HasDisplay(uint64_t display_id) {
111   auto display = ToHwc2Display(display_id);
112   return resources_->hasDisplay(display);
113 }
114 
AddPhysicalDisplay(uint64_t display_id)115 hwc3::Error ComposerResources::AddPhysicalDisplay(uint64_t display_id) {
116   auto display = ToHwc2Display(display_id);
117   auto err = resources_->addPhysicalDisplay(display);
118   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
119 }
120 
AddVirtualDisplay(uint64_t display,uint32_t output_buffer_cache_size)121 hwc3::Error ComposerResources::AddVirtualDisplay(
122     uint64_t display, uint32_t output_buffer_cache_size) {
123   auto err = resources_->addVirtualDisplay(display, output_buffer_cache_size);
124   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
125 }
126 
RemoveDisplay(uint64_t display_id)127 hwc3::Error ComposerResources::RemoveDisplay(uint64_t display_id) {
128   auto display = ToHwc2Display(display_id);
129   auto err = resources_->removeDisplay(display);
130   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
131 }
132 
SetDisplayMustValidateState(uint64_t display_id,bool must_validate)133 void ComposerResources::SetDisplayMustValidateState(uint64_t display_id,
134                                                     bool must_validate) {
135   auto display = ToHwc2Display(display_id);
136   resources_->setDisplayMustValidateState(display, must_validate);
137 }
138 
MustValidateDisplay(uint64_t display_id)139 bool ComposerResources::MustValidateDisplay(uint64_t display_id) {
140   auto display = ToHwc2Display(display_id);
141   return resources_->mustValidateDisplay(display);
142 }
143 
GetDisplayClientTarget(uint64_t display_id,const Buffer & buffer,buffer_handle_t * out_handle,ComposerResourceReleaser * releaser)144 hwc3::Error ComposerResources::GetDisplayClientTarget(
145     uint64_t display_id, const Buffer& buffer, buffer_handle_t* out_handle,
146     ComposerResourceReleaser* releaser) {
147   auto display = ToHwc2Display(display_id);
148 
149   const bool use_cache = !buffer.handle.has_value();
150   buffer_handle_t buffer_handle = nullptr;
151   if (buffer.handle.has_value()) {
152     buffer_handle = ::android::makeFromAidl(*buffer.handle);
153   }
154 
155   auto err = resources_->getDisplayClientTarget(display, buffer.slot, use_cache,
156                                                 buffer_handle, out_handle,
157                                                 releaser->GetReplacedHandle());
158   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
159 }
160 
SetDisplayClientTargetCacheSize(uint64_t display_id,uint32_t client_target_cache_size)161 hwc3::Error ComposerResources::SetDisplayClientTargetCacheSize(
162     uint64_t display_id, uint32_t client_target_cache_size) {
163   auto display = ToHwc2Display(display_id);
164   auto err = resources_
165                  ->setDisplayClientTargetCacheSize(display,
166                                                    client_target_cache_size);
167   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
168 }
169 
GetDisplayClientTargetCacheSize(uint64_t display_id,size_t * out_cache_size)170 hwc3::Error ComposerResources::GetDisplayClientTargetCacheSize(
171     uint64_t display_id, size_t* out_cache_size) {
172   auto display = ToHwc2Display(display_id);
173   auto err = resources_->getDisplayClientTargetCacheSize(display,
174                                                          out_cache_size);
175   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
176 }
177 
GetDisplayOutputBufferCacheSize(uint64_t display_id,size_t * out_cache_size)178 hwc3::Error ComposerResources::GetDisplayOutputBufferCacheSize(
179     uint64_t display_id, size_t* out_cache_size) {
180   auto display = ToHwc2Display(display_id);
181   auto err = resources_->getDisplayOutputBufferCacheSize(display,
182                                                          out_cache_size);
183   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
184 }
185 
GetDisplayOutputBuffer(uint64_t display_id,const Buffer & buffer,buffer_handle_t * out_handle,ComposerResourceReleaser * releaser)186 hwc3::Error ComposerResources::GetDisplayOutputBuffer(
187     uint64_t display_id, const Buffer& buffer, buffer_handle_t* out_handle,
188     ComposerResourceReleaser* releaser) {
189   auto display = ToHwc2Display(display_id);
190   const bool use_cache = !buffer.handle.has_value();
191 
192   buffer_handle_t buffer_handle = nullptr;
193   if (buffer.handle.has_value()) {
194     buffer_handle = ::android::makeFromAidl(*buffer.handle);
195   }
196 
197   auto err = resources_->getDisplayOutputBuffer(display, buffer.slot, use_cache,
198                                                 buffer_handle, out_handle,
199                                                 releaser->GetReplacedHandle());
200   return Hwc2toHwc3Error(static_cast<HWC2::Error>(err));
201 }
202 }  // namespace aidl::android::hardware::graphics::composer3::impl