blob: b9e9040e8e23eb53e892fe3627f7a70ea89f1061 [file] [log] [blame]
Chia-I Wu9ba189d2016-09-22 17:13:08 +08001/*
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#define LOG_TAG "GrallocMapper"
18
19#include <array>
20#include <string>
21
22#include <log/log.h>
23#include <ui/GrallocMapper.h>
24
25namespace android {
26
27namespace Gralloc2 {
28
Chia-I Wu31669472016-12-07 14:55:24 +080029static constexpr Error kDefaultError = Error::NO_RESOURCES;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080030
31Mapper::Mapper()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080032{
Chia-I Wu31669472016-12-07 14:55:24 +080033 mMapper = IMapper::getService("gralloc-mapper");
34 if (mMapper != nullptr && mMapper->isRemote()) {
35 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
Chia-I Wu9ba189d2016-09-22 17:13:08 +080036 }
37}
38
Chia-I Wu31669472016-12-07 14:55:24 +080039Error Mapper::retain(buffer_handle_t handle) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080040{
Chia-I Wu31669472016-12-07 14:55:24 +080041 auto ret = mMapper->retain(handle);
42 return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080043}
44
45void Mapper::release(buffer_handle_t handle) const
46{
Chia-I Wu31669472016-12-07 14:55:24 +080047 auto ret = mMapper->release(handle);
48
49 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080050 ALOGE_IF(error != Error::NONE,
51 "release(%p) failed with %d", handle, error);
52}
53
Craig Donner58a1ef22017-02-02 12:40:05 -080054Error Mapper::getDimensions(buffer_handle_t handle,
55 uint32_t* outWidth, uint32_t* outHeight) const
56{
57 Error error = kDefaultError;
58 mMapper->getDimensions(handle,
59 [&](const auto& tmpError, const auto& tmpWidth,
60 const auto& tmpHeight)
61 {
62 error = tmpError;
63 if (error != Error::NONE) {
64 return;
65 }
66
67 *outWidth = tmpWidth;
68 *outHeight = tmpHeight;
69 });
70
71 return error;
72}
73
74Error Mapper::getFormat(buffer_handle_t handle, int32_t* outFormat) const
75{
76 Error error = kDefaultError;
77 mMapper->getFormat(handle,
78 [&](const auto& tmpError, const auto& tmpFormat)
79 {
80 error = tmpError;
81 if (error != Error::NONE) {
82 return;
83 }
84
85 *outFormat = static_cast<int32_t>(tmpFormat);
86 });
87
88 return error;
89}
90
91Error Mapper::getLayerCount(buffer_handle_t handle,
92 uint32_t* outLayerCount) const
93{
94 Error error = kDefaultError;
95 mMapper->getLayerCount(handle,
96 [&](const auto& tmpError, const auto& tmpLayerCount)
97 {
98 error = tmpError;
99 if (error != Error::NONE) {
100 return;
101 }
102
103 *outLayerCount = tmpLayerCount;
104 });
105
106 return error;
107}
108
109Error Mapper::getProducerUsage(buffer_handle_t handle,
110 uint64_t* outProducerUsage) const
111{
112 Error error = kDefaultError;
113 mMapper->getProducerUsageMask(handle,
114 [&](const auto& tmpError, const auto& tmpProducerUsage)
115 {
116 error = tmpError;
117 if (error != Error::NONE) {
118 return;
119 }
120
121 *outProducerUsage = tmpProducerUsage;
122 });
123
124 return error;
125}
126
127Error Mapper::getConsumerUsage(buffer_handle_t handle,
128 uint64_t* outConsumerUsage) const
129{
130 Error error = kDefaultError;
131 mMapper->getConsumerUsageMask(handle,
132 [&](const auto& tmpError, const auto& tmpConsumerUsage)
133 {
134 error = tmpError;
135 if (error != Error::NONE) {
136 return;
137 }
138
139 *outConsumerUsage = tmpConsumerUsage;
140 });
141
142 return error;
143}
144
145Error Mapper::getBackingStore(buffer_handle_t handle,
146 uint64_t* outBackingStore) const
147{
148 Error error = kDefaultError;
149 mMapper->getBackingStore(handle,
150 [&](const auto& tmpError, const auto& tmpStore)
151 {
152 error = tmpError;
153 if (error != Error::NONE) {
154 return;
155 }
156
157 *outBackingStore = tmpStore;
158 });
159
160 return error;
161}
162
Chia-I Wu31669472016-12-07 14:55:24 +0800163Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const
164{
165 Error error = kDefaultError;
166 mMapper->getStride(handle,
167 [&](const auto& tmpError, const auto& tmpStride)
168 {
169 error = tmpError;
170 if (error != Error::NONE) {
171 return;
172 }
173
174 *outStride = tmpStride;
175 });
176
177 return error;
178}
179
180Error Mapper::lock(buffer_handle_t handle,
Craig Donner58a1ef22017-02-02 12:40:05 -0800181 uint64_t producerUsage,
182 uint64_t consumerUsage,
Chia-I Wu31669472016-12-07 14:55:24 +0800183 const IMapper::Rect& accessRegion,
184 int acquireFence, void** outData) const
185{
186 hardware::hidl_handle acquireFenceHandle;
187
188 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
189 if (acquireFence >= 0) {
190 auto h = native_handle_init(acquireFenceStorage, 1, 0);
191 h->data[0] = acquireFence;
192 acquireFenceHandle = h;
193 }
194
195 Error error = kDefaultError;
Craig Donner58a1ef22017-02-02 12:40:05 -0800196 mMapper->lock(handle, producerUsage, consumerUsage,
Chia-I Wu31669472016-12-07 14:55:24 +0800197 accessRegion, acquireFenceHandle,
198 [&](const auto& tmpError, const auto& tmpData)
199 {
200 error = tmpError;
201 if (error != Error::NONE) {
202 return;
203 }
204
205 *outData = tmpData;
206 });
207
208 if (error == Error::NONE && acquireFence >= 0) {
209 close(acquireFence);
210 }
211
212 return error;
213}
214
215Error Mapper::lock(buffer_handle_t handle,
Craig Donner58a1ef22017-02-02 12:40:05 -0800216 uint64_t producerUsage,
217 uint64_t consumerUsage,
Chia-I Wu31669472016-12-07 14:55:24 +0800218 const IMapper::Rect& accessRegion,
219 int acquireFence, FlexLayout* outLayout) const
220{
221 hardware::hidl_handle acquireFenceHandle;
222
223 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
224 if (acquireFence >= 0) {
225 auto h = native_handle_init(acquireFenceStorage, 1, 0);
226 h->data[0] = acquireFence;
227 acquireFenceHandle = h;
228 }
229
230 Error error = kDefaultError;
Craig Donner58a1ef22017-02-02 12:40:05 -0800231 mMapper->lockFlex(handle, producerUsage, consumerUsage,
Chia-I Wu31669472016-12-07 14:55:24 +0800232 accessRegion, acquireFenceHandle,
233 [&](const auto& tmpError, const auto& tmpLayout)
234 {
235 error = tmpError;
236 if (error != Error::NONE) {
237 return;
238 }
239
240 *outLayout = tmpLayout;
241 });
242
243 if (error == Error::NONE && acquireFence >= 0) {
244 close(acquireFence);
245 }
246
247 return error;
248}
249
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800250int Mapper::unlock(buffer_handle_t handle) const
251{
Fabien Sanglard521698f2017-01-10 13:45:45 -0800252 int releaseFence = -1;
Chia-I Wu31669472016-12-07 14:55:24 +0800253
254 Error error = kDefaultError;
255 mMapper->unlock(handle,
256 [&](const auto& tmpError, const auto& tmpReleaseFence)
257 {
258 error = tmpError;
259 if (error != Error::NONE) {
260 return;
261 }
262
263 auto fenceHandle = tmpReleaseFence.getNativeHandle();
264 if (fenceHandle && fenceHandle->numFds == 1) {
265 int fd = dup(fenceHandle->data[0]);
266 if (fd >= 0) {
267 releaseFence = fd;
268 } else {
269 error = Error::NO_RESOURCES;
270 }
271 } else {
272 releaseFence = -1;
273 }
274 });
275
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800276 if (error != Error::NONE) {
277 ALOGE("unlock(%p) failed with %d", handle, error);
278 releaseFence = -1;
279 }
280
281 return releaseFence;
282}
283
284} // namespace Gralloc2
285
286} // namespace android