blob: 15c60bcadfed4bcd0429e9c76e731746d95800e8 [file] [log] [blame]
Marissa Wall925bf7f2018-12-29 14:27:11 -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 "Gralloc3"
18
19#include <hidl/ServiceManagement.h>
20#include <hwbinder/IPCThreadState.h>
21#include <ui/Gralloc3.h>
22
23#include <inttypes.h>
24#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
30using android::hardware::graphics::allocator::V3_0::IAllocator;
Marissa Wall63429912019-04-09 14:05:06 -070031using android::hardware::graphics::common::V1_2::BufferUsage;
Marissa Wall925bf7f2018-12-29 14:27:11 -080032using android::hardware::graphics::mapper::V3_0::BufferDescriptor;
33using android::hardware::graphics::mapper::V3_0::Error;
34using android::hardware::graphics::mapper::V3_0::IMapper;
35using android::hardware::graphics::mapper::V3_0::YCbCrLayout;
36
37namespace android {
38
39namespace {
40
41static constexpr Error kTransactionError = Error::NO_RESOURCES;
42
43uint64_t getValidUsageBits() {
44 static const uint64_t validUsageBits = []() -> uint64_t {
45 uint64_t bits = 0;
46 for (const auto bit :
Marissa Wall63429912019-04-09 14:05:06 -070047 hardware::hidl_enum_range<hardware::graphics::common::V1_2::BufferUsage>()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080048 bits = bits | bit;
49 }
50 return bits;
51 }();
52 return validUsageBits;
53}
54
55static inline IMapper::Rect sGralloc3Rect(const Rect& rect) {
56 IMapper::Rect outRect{};
57 outRect.left = rect.left;
58 outRect.top = rect.top;
59 outRect.width = rect.width();
60 outRect.height = rect.height();
61 return outRect;
62}
63static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height,
64 android::PixelFormat format, uint32_t layerCount,
65 uint64_t usage,
66 IMapper::BufferDescriptorInfo* outDescriptorInfo) {
67 outDescriptorInfo->width = width;
68 outDescriptorInfo->height = height;
69 outDescriptorInfo->layerCount = layerCount;
Marissa Wall63429912019-04-09 14:05:06 -070070 outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_2::PixelFormat>(format);
Marissa Wall925bf7f2018-12-29 14:27:11 -080071 outDescriptorInfo->usage = usage;
72}
73
74} // anonymous namespace
75
76void Gralloc3Mapper::preload() {
77 android::hardware::preloadPassthroughService<IMapper>();
78}
79
80Gralloc3Mapper::Gralloc3Mapper() {
81 mMapper = IMapper::getService();
82 if (mMapper == nullptr) {
83 ALOGW("mapper 3.x is not supported");
84 return;
85 }
86 if (mMapper->isRemote()) {
87 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
88 }
89}
90
Valerie Hau250c6542019-01-31 14:23:43 -080091bool Gralloc3Mapper::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -080092 return mMapper != nullptr;
93}
94
95status_t Gralloc3Mapper::validateBufferDescriptorInfo(
96 IMapper::BufferDescriptorInfo* descriptorInfo) const {
97 uint64_t validUsageBits = getValidUsageBits();
98
99 if (descriptorInfo->usage & ~validUsageBits) {
100 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
101 descriptorInfo->usage & ~validUsageBits);
102 return BAD_VALUE;
103 }
Chris Forbes6c09ee72022-01-26 18:48:55 +1300104
105 // Gralloc3 implementations never understand non-BLOB with GPU_DATA_BUFFER
106 // and do not reliably reject it.
107 if (descriptorInfo->usage & BufferUsage::GPU_DATA_BUFFER &&
108 descriptorInfo->format != hardware::graphics::common::V1_2::PixelFormat::BLOB) {
109 ALOGE("gralloc3 does not support non-BLOB pixel formats with GPU_DATA_BUFFER usage");
110 return BAD_VALUE;
111 }
112
Marissa Wall925bf7f2018-12-29 14:27:11 -0800113 return NO_ERROR;
114}
115
116status_t Gralloc3Mapper::createDescriptor(void* bufferDescriptorInfo,
117 void* outBufferDescriptor) const {
118 IMapper::BufferDescriptorInfo* descriptorInfo =
119 static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo);
120 BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor);
121
122 status_t status = validateBufferDescriptorInfo(descriptorInfo);
123 if (status != NO_ERROR) {
124 return status;
125 }
126
127 Error error;
128 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) {
129 error = tmpError;
130 if (error != Error::NONE) {
131 return;
132 }
133 *outDescriptor = tmpDescriptor;
134 };
135
136 hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb);
137
138 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
139}
140
141status_t Gralloc3Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
142 buffer_handle_t* outBufferHandle) const {
143 Error error;
144 auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
145 error = tmpError;
146 if (error != Error::NONE) {
147 return;
148 }
149 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
150 });
151
152 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
153}
154
155void Gralloc3Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
156 auto buffer = const_cast<native_handle_t*>(bufferHandle);
157 auto ret = mMapper->freeBuffer(buffer);
158
159 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
160 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error);
161}
162
163status_t Gralloc3Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
164 uint32_t height, android::PixelFormat format,
165 uint32_t layerCount, uint64_t usage,
166 uint32_t stride) const {
167 IMapper::BufferDescriptorInfo descriptorInfo;
168 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
169
170 auto buffer = const_cast<native_handle_t*>(bufferHandle);
171 auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
172
173 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
174}
175
176void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
177 uint32_t* outNumInts) const {
178 *outNumFds = uint32_t(bufferHandle->numFds);
179 *outNumInts = uint32_t(bufferHandle->numInts);
180
181 Error error;
182 auto buffer = const_cast<native_handle_t*>(bufferHandle);
183 auto ret = mMapper->getTransportSize(buffer,
184 [&](const auto& tmpError, const auto& tmpNumFds,
185 const auto& tmpNumInts) {
186 error = tmpError;
187 if (error != Error::NONE) {
188 return;
189 }
190 *outNumFds = tmpNumFds;
191 *outNumInts = tmpNumInts;
192 });
193
194 error = (ret.isOk()) ? error : kTransactionError;
195
196 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
197}
198
199status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800200 int acquireFence, void** outData, int32_t* outBytesPerPixel,
201 int32_t* outBytesPerStride) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800202 auto buffer = const_cast<native_handle_t*>(bufferHandle);
203
204 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
205
206 // put acquireFence in a hidl_handle
207 hardware::hidl_handle acquireFenceHandle;
208 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
209 if (acquireFence >= 0) {
210 auto h = native_handle_init(acquireFenceStorage, 1, 0);
211 h->data[0] = acquireFence;
212 acquireFenceHandle = h;
213 }
214
215 Error error;
216 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800217 [&](const auto& tmpError, const auto& tmpData,
218 const auto& tmpBytesPerPixel, const auto& tmpBytesPerStride) {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800219 error = tmpError;
220 if (error != Error::NONE) {
221 return;
222 }
223 *outData = tmpData;
Valerie Hau0c9fc362019-01-22 09:17:19 -0800224 if (outBytesPerPixel) {
225 *outBytesPerPixel = tmpBytesPerPixel;
226 }
227 if (outBytesPerStride) {
228 *outBytesPerStride = tmpBytesPerStride;
229 }
Marissa Wall925bf7f2018-12-29 14:27:11 -0800230 });
231
232 // we own acquireFence even on errors
233 if (acquireFence >= 0) {
234 close(acquireFence);
235 }
236
237 error = (ret.isOk()) ? error : kTransactionError;
238
239 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
240
241 return static_cast<status_t>(error);
242}
243
244status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
245 int acquireFence, android_ycbcr* ycbcr) const {
246 auto buffer = const_cast<native_handle_t*>(bufferHandle);
247
248 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
249
250 // put acquireFence in a hidl_handle
251 hardware::hidl_handle acquireFenceHandle;
252 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
253 if (acquireFence >= 0) {
254 auto h = native_handle_init(acquireFenceStorage, 1, 0);
255 h->data[0] = acquireFence;
256 acquireFenceHandle = h;
257 }
258
259 YCbCrLayout layout;
260 Error error;
261 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle,
262 [&](const auto& tmpError, const auto& tmpLayout) {
263 error = tmpError;
264 if (error != Error::NONE) {
265 return;
266 }
267
268 layout = tmpLayout;
269 });
270
271 if (error == Error::NONE) {
272 ycbcr->y = layout.y;
273 ycbcr->cb = layout.cb;
274 ycbcr->cr = layout.cr;
275 ycbcr->ystride = static_cast<size_t>(layout.yStride);
276 ycbcr->cstride = static_cast<size_t>(layout.cStride);
277 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
278 }
279
280 // we own acquireFence even on errors
281 if (acquireFence >= 0) {
282 close(acquireFence);
283 }
284
285 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
286}
287
288int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const {
289 auto buffer = const_cast<native_handle_t*>(bufferHandle);
290
291 int releaseFence = -1;
292 Error error;
293 auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
294 error = tmpError;
295 if (error != Error::NONE) {
296 return;
297 }
298
299 auto fenceHandle = tmpReleaseFence.getNativeHandle();
300 if (fenceHandle && fenceHandle->numFds == 1) {
301 int fd = dup(fenceHandle->data[0]);
302 if (fd >= 0) {
303 releaseFence = fd;
304 } else {
305 ALOGD("failed to dup unlock release fence");
306 sync_wait(fenceHandle->data[0], -1);
307 }
308 }
309 });
310
311 if (!ret.isOk()) {
312 error = kTransactionError;
313 }
314
315 if (error != Error::NONE) {
316 ALOGE("unlock(%p) failed with %d", buffer, error);
317 }
318
319 return releaseFence;
320}
321
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800322status_t Gralloc3Mapper::isSupported(uint32_t width, uint32_t height, android::PixelFormat format,
323 uint32_t layerCount, uint64_t usage,
324 bool* outSupported) const {
325 IMapper::BufferDescriptorInfo descriptorInfo;
326 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
327
328 Error error;
329 auto ret = mMapper->isSupported(descriptorInfo,
330 [&](const auto& tmpError, const auto& tmpSupported) {
331 error = tmpError;
332 if (error != Error::NONE) {
333 return;
334 }
335 if (outSupported) {
336 *outSupported = tmpSupported;
337 }
338 });
339
340 if (!ret.isOk()) {
341 error = kTransactionError;
342 }
343
344 if (error != Error::NONE) {
345 ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount,
346 error);
347 }
348
349 return static_cast<status_t>(error);
350}
351
Marissa Wall925bf7f2018-12-29 14:27:11 -0800352Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) {
353 mAllocator = IAllocator::getService();
354 if (mAllocator == nullptr) {
355 ALOGW("allocator 3.x is not supported");
356 return;
357 }
358}
359
Valerie Hau250c6542019-01-31 14:23:43 -0800360bool Gralloc3Allocator::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800361 return mAllocator != nullptr;
362}
363
Marissa Wall22b2de12019-12-02 18:11:43 -0800364std::string Gralloc3Allocator::dumpDebugInfo(bool /*less*/) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800365 std::string debugInfo;
366
367 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
368
369 return debugInfo;
370}
371
Marissa Wall22b2de12019-12-02 18:11:43 -0800372status_t Gralloc3Allocator::allocate(std::string /*requestorName*/, uint32_t width, uint32_t height,
373 android::PixelFormat format, uint32_t layerCount,
374 uint64_t usage, uint32_t bufferCount, uint32_t* outStride,
375 buffer_handle_t* outBufferHandles, bool importBuffers) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800376 IMapper::BufferDescriptorInfo descriptorInfo;
377 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
378
379 BufferDescriptor descriptor;
380 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
381 static_cast<void*>(&descriptor));
382 if (error != NO_ERROR) {
383 return error;
384 }
385
386 auto ret = mAllocator->allocate(descriptor, bufferCount,
387 [&](const auto& tmpError, const auto& tmpStride,
388 const auto& tmpBuffers) {
389 error = static_cast<status_t>(tmpError);
390 if (tmpError != Error::NONE) {
391 return;
392 }
393
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800394 if (importBuffers) {
395 for (uint32_t i = 0; i < bufferCount; i++) {
396 error = mMapper.importBuffer(tmpBuffers[i],
397 &outBufferHandles[i]);
398 if (error != NO_ERROR) {
399 for (uint32_t j = 0; j < i; j++) {
400 mMapper.freeBuffer(outBufferHandles[j]);
401 outBufferHandles[j] = nullptr;
402 }
403 return;
Marissa Wall925bf7f2018-12-29 14:27:11 -0800404 }
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800405 }
406 } else {
407 for (uint32_t i = 0; i < bufferCount; i++) {
408 outBufferHandles[i] = native_handle_clone(
409 tmpBuffers[i].getNativeHandle());
410 if (!outBufferHandles[i]) {
411 for (uint32_t j = 0; j < i; j++) {
412 auto buffer = const_cast<native_handle_t*>(
413 outBufferHandles[j]);
414 native_handle_close(buffer);
415 native_handle_delete(buffer);
416 outBufferHandles[j] = nullptr;
417 }
418 }
Marissa Wall925bf7f2018-12-29 14:27:11 -0800419 }
420 }
421 *outStride = tmpStride;
422 });
423
424 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
425 hardware::IPCThreadState::self()->flushCommands();
426
427 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
428}
429
430} // namespace android