1 /*
2  * Copyright (C) 2022 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 "ComposerResources.h"
18 
19 #include <aidlcommonsupport/NativeHandle.h>
20 
21 namespace aidl::android::hardware::graphics::composer3::impl {
22 namespace {
23 
toHwc3Error(::android::hardware::graphics::composer::V2_1::Error error)24 HWC3::Error toHwc3Error(::android::hardware::graphics::composer::V2_1::Error error) {
25     switch (error) {
26         case ::android::hardware::graphics::composer::V2_1::Error::NONE:
27             return HWC3::Error::None;
28         case ::android::hardware::graphics::composer::V2_1::Error::BAD_CONFIG:
29             return HWC3::Error::BadConfig;
30         case ::android::hardware::graphics::composer::V2_1::Error::BAD_DISPLAY:
31             return HWC3::Error::BadDisplay;
32         case ::android::hardware::graphics::composer::V2_1::Error::BAD_LAYER:
33             return HWC3::Error::BadLayer;
34         case ::android::hardware::graphics::composer::V2_1::Error::BAD_PARAMETER:
35             return HWC3::Error::BadParameter;
36         case ::android::hardware::graphics::composer::V2_1::Error::NO_RESOURCES:
37             return HWC3::Error::NoResources;
38         case ::android::hardware::graphics::composer::V2_1::Error::NOT_VALIDATED:
39             return HWC3::Error::NotValidated;
40         case ::android::hardware::graphics::composer::V2_1::Error::UNSUPPORTED:
41             return HWC3::Error::Unsupported;
42     }
43 }
44 
toHwc2Display(int64_t displayId)45 ::android::hardware::graphics::composer::V2_1::Display toHwc2Display(int64_t displayId) {
46     return static_cast<::android::hardware::graphics::composer::V2_1::Display>(displayId);
47 }
48 
toHwc2Layer(int64_t layerId)49 ::android::hardware::graphics::composer::V2_1::Layer toHwc2Layer(int64_t layerId) {
50     return static_cast<::android::hardware::graphics::composer::V2_1::Layer>(layerId);
51 }
52 
53 }  // namespace
54 
createReleaser(bool isBuffer)55 std::unique_ptr<ComposerResourceReleaser> ComposerResources::createReleaser(bool isBuffer) {
56     return std::make_unique<ComposerResourceReleaser>(isBuffer);
57 }
58 
init()59 HWC3::Error ComposerResources::init() {
60     mImpl = ::android::hardware::graphics::composer::V2_2::hal::ComposerResources::create();
61     if (!mImpl) {
62         ALOGE("%s: failed to create underlying ComposerResources.", __FUNCTION__);
63         return HWC3::Error::NoResources;
64     }
65     return HWC3::Error::None;
66 }
67 
clear(::android::hardware::graphics::composer::V2_2::hal::ComposerResources::RemoveDisplay removeDisplay)68 void ComposerResources::clear(
69     ::android::hardware::graphics::composer::V2_2::hal::ComposerResources::RemoveDisplay
70         removeDisplay) {
71     mImpl->clear(removeDisplay);
72 }
73 
hasDisplay(int64_t displayId)74 bool ComposerResources::hasDisplay(int64_t displayId) {
75     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
76     return mImpl->hasDisplay(display);
77 }
78 
addPhysicalDisplay(int64_t displayId)79 HWC3::Error ComposerResources::addPhysicalDisplay(int64_t displayId) {
80     DEBUG_LOG("%s: display:%" PRId64, __FUNCTION__, displayId);
81     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
82     return toHwc3Error(mImpl->addPhysicalDisplay(display));
83 }
84 
addVirtualDisplay(int64_t displayId,uint32_t outputBufferCacheSize)85 HWC3::Error ComposerResources::addVirtualDisplay(int64_t displayId,
86                                                  uint32_t outputBufferCacheSize) {
87     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
88     return toHwc3Error(mImpl->addVirtualDisplay(display, outputBufferCacheSize));
89 }
90 
removeDisplay(int64_t displayId)91 HWC3::Error ComposerResources::removeDisplay(int64_t displayId) {
92     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
93     return toHwc3Error(mImpl->removeDisplay(display));
94 }
95 
setDisplayClientTargetCacheSize(int64_t displayId,uint32_t clientTargetCacheSize)96 HWC3::Error ComposerResources::setDisplayClientTargetCacheSize(int64_t displayId,
97                                                                uint32_t clientTargetCacheSize) {
98     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
99     return toHwc3Error(mImpl->setDisplayClientTargetCacheSize(display, clientTargetCacheSize));
100 }
101 
getDisplayClientTargetCacheSize(int64_t displayId,size_t * outCacheSize)102 HWC3::Error ComposerResources::getDisplayClientTargetCacheSize(int64_t displayId,
103                                                                size_t* outCacheSize) {
104     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
105     return toHwc3Error(mImpl->getDisplayClientTargetCacheSize(display, outCacheSize));
106 }
107 
getDisplayOutputBufferCacheSize(int64_t displayId,size_t * outCacheSize)108 HWC3::Error ComposerResources::getDisplayOutputBufferCacheSize(int64_t displayId,
109                                                                size_t* outCacheSize) {
110     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
111     return toHwc3Error(mImpl->getDisplayOutputBufferCacheSize(display, outCacheSize));
112 }
113 
addLayer(int64_t displayId,int64_t layerId,uint32_t bufferCacheSize)114 HWC3::Error ComposerResources::addLayer(int64_t displayId, int64_t layerId,
115                                         uint32_t bufferCacheSize) {
116     DEBUG_LOG("%s: display:%" PRId64 " layer:%" PRId64, __FUNCTION__, displayId, layerId);
117 
118     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
119     ::android::hardware::graphics::composer::V2_1::Layer layer = toHwc2Layer(layerId);
120     return toHwc3Error(mImpl->addLayer(display, layer, bufferCacheSize));
121 }
122 
removeLayer(int64_t displayId,int64_t layerId)123 HWC3::Error ComposerResources::removeLayer(int64_t displayId, int64_t layerId) {
124     DEBUG_LOG("%s: display:%" PRId64 " layer:%" PRId64, __FUNCTION__, displayId, layerId);
125 
126     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
127     ::android::hardware::graphics::composer::V2_1::Layer layer = toHwc2Layer(layerId);
128 
129     return toHwc3Error(mImpl->removeLayer(display, layer));
130 }
131 
setDisplayMustValidateState(int64_t displayId,bool mustValidate)132 void ComposerResources::setDisplayMustValidateState(int64_t displayId, bool mustValidate) {
133     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
134     mImpl->setDisplayMustValidateState(display, mustValidate);
135 }
136 
mustValidateDisplay(int64_t displayId)137 bool ComposerResources::mustValidateDisplay(int64_t displayId) {
138     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
139     return mImpl->mustValidateDisplay(display);
140 }
141 
getDisplayReadbackBuffer(int64_t displayId,const aidl::android::hardware::common::NativeHandle & handle,buffer_handle_t * outHandle,ComposerResourceReleaser * releaser)142 HWC3::Error ComposerResources::getDisplayReadbackBuffer(
143     int64_t displayId, const aidl::android::hardware::common::NativeHandle& handle,
144     buffer_handle_t* outHandle, ComposerResourceReleaser* releaser) {
145     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
146     return toHwc3Error(mImpl->getDisplayReadbackBuffer(display, ::android::makeFromAidl(handle),
147                                                        outHandle, releaser->getReplacedHandle()));
148 }
149 
getDisplayClientTarget(int64_t displayId,const Buffer & buffer,buffer_handle_t * outHandle,ComposerResourceReleaser * releaser)150 HWC3::Error ComposerResources::getDisplayClientTarget(int64_t displayId, const Buffer& buffer,
151                                                       buffer_handle_t* outHandle,
152                                                       ComposerResourceReleaser* releaser) {
153     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
154 
155     const bool useCache = !buffer.handle.has_value();
156 
157     buffer_handle_t bufferHandle = nullptr;
158     if (buffer.handle.has_value()) {
159         bufferHandle = ::android::makeFromAidl(*buffer.handle);
160     }
161 
162     return toHwc3Error(mImpl->getDisplayClientTarget(display, static_cast<uint32_t>(buffer.slot),
163                                                      useCache, bufferHandle, outHandle,
164                                                      releaser->getReplacedHandle()));
165 }
166 
getDisplayOutputBuffer(int64_t displayId,const Buffer & buffer,buffer_handle_t * outHandle,ComposerResourceReleaser * releaser)167 HWC3::Error ComposerResources::getDisplayOutputBuffer(int64_t displayId, const Buffer& buffer,
168                                                       buffer_handle_t* outHandle,
169                                                       ComposerResourceReleaser* releaser) {
170     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
171 
172     const bool useCache = !buffer.handle.has_value();
173 
174     buffer_handle_t bufferHandle = nullptr;
175     if (buffer.handle.has_value()) {
176         bufferHandle = ::android::makeFromAidl(*buffer.handle);
177     }
178 
179     return toHwc3Error(mImpl->getDisplayOutputBuffer(display, static_cast<uint32_t>(buffer.slot),
180                                                      useCache, bufferHandle, outHandle,
181                                                      releaser->getReplacedHandle()));
182 }
183 
getLayerBuffer(int64_t displayId,int64_t layerId,const Buffer & buffer,buffer_handle_t * outHandle,ComposerResourceReleaser * releaser)184 HWC3::Error ComposerResources::getLayerBuffer(int64_t displayId, int64_t layerId,
185                                               const Buffer& buffer, buffer_handle_t* outHandle,
186                                               ComposerResourceReleaser* releaser) {
187     DEBUG_LOG("%s: display:%" PRId64 " layer:%" PRId64, __FUNCTION__, displayId, layerId);
188 
189     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
190     ::android::hardware::graphics::composer::V2_1::Layer layer = toHwc2Layer(layerId);
191 
192     const bool useCache = !buffer.handle.has_value();
193 
194     buffer_handle_t bufferHandle = nullptr;
195     if (buffer.handle.has_value()) {
196         bufferHandle = ::android::makeFromAidl(*buffer.handle);
197     }
198 
199     DEBUG_LOG("%s fromCache:%s", __FUNCTION__, (useCache ? "yes" : "no"));
200     return toHwc3Error(mImpl->getLayerBuffer(display, layer, static_cast<uint32_t>(buffer.slot),
201                                              useCache, bufferHandle, outHandle,
202                                              releaser->getReplacedHandle()));
203 }
204 
getLayerSidebandStream(int64_t displayId,int64_t layerId,const aidl::android::hardware::common::NativeHandle & handle,buffer_handle_t * outHandle,ComposerResourceReleaser * releaser)205 HWC3::Error ComposerResources::getLayerSidebandStream(
206     int64_t displayId, int64_t layerId, const aidl::android::hardware::common::NativeHandle& handle,
207     buffer_handle_t* outHandle, ComposerResourceReleaser* releaser) {
208     DEBUG_LOG("%s: display:%" PRId64 " layer:%" PRId64, __FUNCTION__, displayId, layerId);
209 
210     ::android::hardware::graphics::composer::V2_1::Display display = toHwc2Display(displayId);
211     ::android::hardware::graphics::composer::V2_1::Layer layer = toHwc2Layer(layerId);
212     return toHwc3Error(mImpl->getLayerSidebandStream(
213         display, layer, ::android::makeFromAidl(handle), outHandle, releaser->getReplacedHandle()));
214 }
215 
216 }  // namespace aidl::android::hardware::graphics::composer3::impl