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