blob: 60ec38c459a4b86f9f8d38cf96e2079ad662b224 [file] [log] [blame]
Chia-I Wu5bac7f32017-04-06 12:34:32 -07001/*
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 "Gralloc2"
18
Jesse Hall5dac7812017-07-06 14:02:29 -070019#include <hidl/ServiceManagement.h>
Chia-I Wud8091b92017-05-16 14:30:34 -070020#include <hwbinder/IPCThreadState.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070021#include <ui/Gralloc2.h>
22
Craig Donnere6ecb922017-12-27 14:59:29 -080023#include <inttypes.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070024#include <log/log.h>
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
27#include <sync/sync.h>
28#pragma clang diagnostic pop
29
30namespace android {
31
32namespace Gralloc2 {
33
Craig Donnere6ecb922017-12-27 14:59:29 -080034namespace {
35
Chia-I Wu5bac7f32017-04-06 12:34:32 -070036static constexpr Error kTransactionError = Error::NO_RESOURCES;
37
Craig Donnere6ecb922017-12-27 14:59:29 -080038uint64_t getValid10UsageBits() {
39 static const uint64_t valid10UsageBits = []() -> uint64_t {
40 using hardware::graphics::common::V1_0::BufferUsage;
41 uint64_t bits = 0;
42 for (const auto bit : hardware::hidl_enum_iterator<BufferUsage>()) {
43 bits = bits | bit;
44 }
45 // TODO(b/72323293): Remove this mask for EXTERNAL_DISP.
46 bits = bits | (1 << 13);
47
48 return bits;
49 }();
50 return valid10UsageBits;
51}
52
53uint64_t getValid11UsageBits() {
54 static const uint64_t valid11UsageBits = []() -> uint64_t {
55 using hardware::graphics::common::V1_1::BufferUsage;
56 uint64_t bits = 0;
57 for (const auto bit : hardware::hidl_enum_iterator<BufferUsage>()) {
58 bits = bits | bit;
59 }
60 // Return only the overlapping bits.
61 return bits & ~getValid10UsageBits();
62 }();
63 return valid11UsageBits;
64}
65
66} // anonymous namespace
67
Jesse Hall5dac7812017-07-06 14:02:29 -070068void Mapper::preload() {
69 android::hardware::preloadPassthroughService<hardware::graphics::mapper::V2_0::IMapper>();
70}
71
Chia-I Wu5bac7f32017-04-06 12:34:32 -070072Mapper::Mapper()
73{
74 mMapper = IMapper::getService();
Chia-I Wudbbe33b2017-09-27 15:22:21 -070075 if (mMapper == nullptr) {
76 LOG_ALWAYS_FATAL("gralloc-mapper is missing");
77 }
78 if (mMapper->isRemote()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -070079 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
80 }
Chia-I Wudbbe33b2017-09-27 15:22:21 -070081
82 // IMapper 2.1 is optional
83 mMapperV2_1 = hardware::graphics::mapper::V2_1::IMapper::castFrom(mMapper);
Chia-I Wu5bac7f32017-04-06 12:34:32 -070084}
85
Craig Donnere6ecb922017-12-27 14:59:29 -080086Gralloc2::Error Mapper::validateBufferDescriptorInfo(
87 const IMapper::BufferDescriptorInfo& descriptorInfo) const {
88 uint64_t validUsageBits = getValid10UsageBits();
89 if (mMapperV2_1 != nullptr) {
90 validUsageBits = validUsageBits | getValid11UsageBits();
91 }
92
93 if (descriptorInfo.usage & ~validUsageBits) {
94 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
95 descriptorInfo.usage & ~validUsageBits);
96 return Error::BAD_VALUE;
97 }
98 return Error::NONE;
99}
100
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700101Error Mapper::createDescriptor(
102 const IMapper::BufferDescriptorInfo& descriptorInfo,
103 BufferDescriptor* outDescriptor) const
104{
105 Error error;
Craig Donnere6ecb922017-12-27 14:59:29 -0800106
107 if (descriptorInfo.usage & getValid11UsageBits()) {
108 // TODO(b/66900669): Use mMapperV2_1->createDescriptorV2_1().
109 ALOGW("full support for new usage bits is unimplemented 0x%" PRIx64,
110 descriptorInfo.usage & getValid11UsageBits());
111 return Error::BAD_VALUE;
112 }
113
114 error = validateBufferDescriptorInfo(descriptorInfo);
115 if (error != Error::NONE) {
116 return error;
117 }
118
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700119 auto ret = mMapper->createDescriptor(descriptorInfo,
120 [&](const auto& tmpError, const auto& tmpDescriptor)
121 {
122 error = tmpError;
123 if (error != Error::NONE) {
124 return;
125 }
126
127 *outDescriptor = tmpDescriptor;
128 });
129
130 return (ret.isOk()) ? error : kTransactionError;
131}
132
133Error Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
134 buffer_handle_t* outBufferHandle) const
135{
136 Error error;
137 auto ret = mMapper->importBuffer(rawHandle,
138 [&](const auto& tmpError, const auto& tmpBuffer)
139 {
140 error = tmpError;
141 if (error != Error::NONE) {
142 return;
143 }
144
145 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
146 });
147
148 return (ret.isOk()) ? error : kTransactionError;
149}
150
151void Mapper::freeBuffer(buffer_handle_t bufferHandle) const
152{
153 auto buffer = const_cast<native_handle_t*>(bufferHandle);
154 auto ret = mMapper->freeBuffer(buffer);
155
156 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
157 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d",
158 buffer, error);
159}
160
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700161Error Mapper::validateBufferSize(buffer_handle_t bufferHandle,
162 const IMapper::BufferDescriptorInfo& descriptorInfo,
163 uint32_t stride) const
164{
165 if (mMapperV2_1 == nullptr) {
166 return Error::NONE;
167 }
168
169 auto buffer = const_cast<native_handle_t*>(bufferHandle);
170 auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
171
172 return (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
173}
174
175void Mapper::getTransportSize(buffer_handle_t bufferHandle,
176 uint32_t* outNumFds, uint32_t* outNumInts) const
177{
178 *outNumFds = uint32_t(bufferHandle->numFds);
179 *outNumInts = uint32_t(bufferHandle->numInts);
180
181 if (mMapperV2_1 == nullptr) {
182 return;
183 }
184
185 Error error;
186 auto buffer = const_cast<native_handle_t*>(bufferHandle);
187 auto ret = mMapperV2_1->getTransportSize(buffer,
188 [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
189 error = tmpError;
190 if (error != Error::NONE) {
191 return;
192 }
193
194 *outNumFds = tmpNumFds;
195 *outNumInts = tmpNumInts;
196 });
197
198 if (!ret.isOk()) {
199 error = kTransactionError;
200 }
201 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d",
202 buffer, error);
203}
204
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700205Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage,
206 const IMapper::Rect& accessRegion,
207 int acquireFence, void** outData) const
208{
209 auto buffer = const_cast<native_handle_t*>(bufferHandle);
210
211 // put acquireFence in a hidl_handle
212 hardware::hidl_handle acquireFenceHandle;
213 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
214 if (acquireFence >= 0) {
215 auto h = native_handle_init(acquireFenceStorage, 1, 0);
216 h->data[0] = acquireFence;
217 acquireFenceHandle = h;
218 }
219
220 Error error;
221 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
222 [&](const auto& tmpError, const auto& tmpData)
223 {
224 error = tmpError;
225 if (error != Error::NONE) {
226 return;
227 }
228
229 *outData = tmpData;
230 });
231
232 // we own acquireFence even on errors
233 if (acquireFence >= 0) {
234 close(acquireFence);
235 }
236
237 return (ret.isOk()) ? error : kTransactionError;
238}
239
240Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage,
241 const IMapper::Rect& accessRegion,
242 int acquireFence, YCbCrLayout* outLayout) const
243{
244 auto buffer = const_cast<native_handle_t*>(bufferHandle);
245
246 // put acquireFence in a hidl_handle
247 hardware::hidl_handle acquireFenceHandle;
248 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
249 if (acquireFence >= 0) {
250 auto h = native_handle_init(acquireFenceStorage, 1, 0);
251 h->data[0] = acquireFence;
252 acquireFenceHandle = h;
253 }
254
255 Error error;
256 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion,
257 acquireFenceHandle,
258 [&](const auto& tmpError, const auto& tmpLayout)
259 {
260 error = tmpError;
261 if (error != Error::NONE) {
262 return;
263 }
264
265 *outLayout = tmpLayout;
266 });
267
268 // we own acquireFence even on errors
269 if (acquireFence >= 0) {
270 close(acquireFence);
271 }
272
273 return (ret.isOk()) ? error : kTransactionError;
274}
275
276int Mapper::unlock(buffer_handle_t bufferHandle) const
277{
278 auto buffer = const_cast<native_handle_t*>(bufferHandle);
279
280 int releaseFence = -1;
281 Error error;
282 auto ret = mMapper->unlock(buffer,
283 [&](const auto& tmpError, const auto& tmpReleaseFence)
284 {
285 error = tmpError;
286 if (error != Error::NONE) {
287 return;
288 }
289
290 auto fenceHandle = tmpReleaseFence.getNativeHandle();
291 if (fenceHandle && fenceHandle->numFds == 1) {
292 int fd = dup(fenceHandle->data[0]);
293 if (fd >= 0) {
294 releaseFence = fd;
295 } else {
296 ALOGD("failed to dup unlock release fence");
297 sync_wait(fenceHandle->data[0], -1);
298 }
299 }
300 });
301
302 if (!ret.isOk()) {
303 error = kTransactionError;
304 }
305
306 if (error != Error::NONE) {
307 ALOGE("unlock(%p) failed with %d", buffer, error);
308 }
309
310 return releaseFence;
311}
312
313Allocator::Allocator(const Mapper& mapper)
314 : mMapper(mapper)
315{
Chia-I Wucb8405e2017-04-17 15:20:19 -0700316 mAllocator = IAllocator::getService();
317 if (mAllocator == nullptr) {
318 LOG_ALWAYS_FATAL("gralloc-alloc is missing");
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700319 }
320}
321
322std::string Allocator::dumpDebugInfo() const
323{
324 std::string debugInfo;
325
326 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) {
327 debugInfo = tmpDebugInfo.c_str();
328 });
329
330 return debugInfo;
331}
332
333Error Allocator::allocate(BufferDescriptor descriptor, uint32_t count,
334 uint32_t* outStride, buffer_handle_t* outBufferHandles) const
335{
336 Error error;
337 auto ret = mAllocator->allocate(descriptor, count,
338 [&](const auto& tmpError, const auto& tmpStride,
339 const auto& tmpBuffers) {
340 error = tmpError;
341 if (tmpError != Error::NONE) {
342 return;
343 }
344
345 // import buffers
346 for (uint32_t i = 0; i < count; i++) {
347 error = mMapper.importBuffer(tmpBuffers[i],
348 &outBufferHandles[i]);
349 if (error != Error::NONE) {
350 for (uint32_t j = 0; j < i; j++) {
351 mMapper.freeBuffer(outBufferHandles[j]);
352 outBufferHandles[j] = nullptr;
353 }
354 return;
355 }
356 }
357
358 *outStride = tmpStride;
359 });
360
Chia-I Wud8091b92017-05-16 14:30:34 -0700361 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
362 hardware::IPCThreadState::self()->flushCommands();
363
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700364 return (ret.isOk()) ? error : kTransactionError;
365}
366
367} // namespace Gralloc2
368
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700369} // namespace android