blob: 2dd11b1413c1997831ccde815aa7e1f6ccde3506 [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
Marissa Wall925bf7f2018-12-29 14:27:11 -080030using android::hardware::graphics::allocator::V2_0::IAllocator;
Marissa Wall1e779252018-12-29 12:01:57 -080031using android::hardware::graphics::common::V1_1::BufferUsage;
32using android::hardware::graphics::common::V1_1::PixelFormat;
33using android::hardware::graphics::mapper::V2_0::BufferDescriptor;
34using android::hardware::graphics::mapper::V2_0::Error;
35using android::hardware::graphics::mapper::V2_0::YCbCrLayout;
Marissa Wall925bf7f2018-12-29 14:27:11 -080036using android::hardware::graphics::mapper::V2_1::IMapper;
Marissa Wall1e779252018-12-29 12:01:57 -080037
Chia-I Wu5bac7f32017-04-06 12:34:32 -070038namespace android {
39
Craig Donnere6ecb922017-12-27 14:59:29 -080040namespace {
41
Chia-I Wu5bac7f32017-04-06 12:34:32 -070042static constexpr Error kTransactionError = Error::NO_RESOURCES;
43
Craig Donnere6ecb922017-12-27 14:59:29 -080044uint64_t getValid10UsageBits() {
45 static const uint64_t valid10UsageBits = []() -> uint64_t {
46 using hardware::graphics::common::V1_0::BufferUsage;
47 uint64_t bits = 0;
Steven Moreland3cde8752018-05-01 16:54:17 -070048 for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) {
Craig Donnere6ecb922017-12-27 14:59:29 -080049 bits = bits | bit;
50 }
Kevin F. Haggerty74c35e62018-08-25 15:00:25 -060051
52#ifdef ADDNL_GRALLOC_10_USAGE_BITS
53 uint64_t addnl_bits = static_cast<uint64_t>(ADDNL_GRALLOC_10_USAGE_BITS);
54 ALOGI("Adding additional valid usage bits: 0x%" PRIx64, addnl_bits);
55 bits = bits | addnl_bits;
56#endif
57
Craig Donnere6ecb922017-12-27 14:59:29 -080058 return bits;
59 }();
60 return valid10UsageBits;
61}
62
63uint64_t getValid11UsageBits() {
64 static const uint64_t valid11UsageBits = []() -> uint64_t {
65 using hardware::graphics::common::V1_1::BufferUsage;
66 uint64_t bits = 0;
Steven Moreland3cde8752018-05-01 16:54:17 -070067 for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) {
Craig Donnere6ecb922017-12-27 14:59:29 -080068 bits = bits | bit;
69 }
Chia-I Wu4f55f162018-01-16 21:58:18 -080070 return bits;
Craig Donnere6ecb922017-12-27 14:59:29 -080071 }();
72 return valid11UsageBits;
73}
74
Marissa Walld380e2c2018-12-29 14:17:29 -080075static inline IMapper::Rect sGralloc2Rect(const Rect& rect) {
76 IMapper::Rect outRect{};
Marissa Wall1e779252018-12-29 12:01:57 -080077 outRect.left = rect.left;
78 outRect.top = rect.top;
79 outRect.width = rect.width();
80 outRect.height = rect.height();
81 return outRect;
82}
83
Craig Donnere6ecb922017-12-27 14:59:29 -080084} // anonymous namespace
85
Marissa Walld380e2c2018-12-29 14:17:29 -080086void Gralloc2Mapper::preload() {
Jesse Hall5dac7812017-07-06 14:02:29 -070087 android::hardware::preloadPassthroughService<hardware::graphics::mapper::V2_0::IMapper>();
88}
89
Marissa Walld380e2c2018-12-29 14:17:29 -080090Gralloc2Mapper::Gralloc2Mapper() {
Chia-I Wu4f55f162018-01-16 21:58:18 -080091 mMapper = hardware::graphics::mapper::V2_0::IMapper::getService();
Chia-I Wudbbe33b2017-09-27 15:22:21 -070092 if (mMapper == nullptr) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080093 ALOGW("mapper 2.x is not supported");
94 return;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070095 }
96 if (mMapper->isRemote()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -070097 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
98 }
Chia-I Wudbbe33b2017-09-27 15:22:21 -070099
100 // IMapper 2.1 is optional
Chia-I Wu4f55f162018-01-16 21:58:18 -0800101 mMapperV2_1 = IMapper::castFrom(mMapper);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700102}
103
Valerie Hau250c6542019-01-31 14:23:43 -0800104bool Gralloc2Mapper::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800105 return mMapper != nullptr;
106}
107
Marissa Walld380e2c2018-12-29 14:17:29 -0800108status_t Gralloc2Mapper::validateBufferDescriptorInfo(
109 IMapper::BufferDescriptorInfo* descriptorInfo) const {
Craig Donnere6ecb922017-12-27 14:59:29 -0800110 uint64_t validUsageBits = getValid10UsageBits();
111 if (mMapperV2_1 != nullptr) {
112 validUsageBits = validUsageBits | getValid11UsageBits();
113 }
114
Marissa Wall1e779252018-12-29 12:01:57 -0800115 if (descriptorInfo->usage & ~validUsageBits) {
Craig Donnere6ecb922017-12-27 14:59:29 -0800116 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
Marissa Wall1e779252018-12-29 12:01:57 -0800117 descriptorInfo->usage & ~validUsageBits);
118 return BAD_VALUE;
Craig Donnere6ecb922017-12-27 14:59:29 -0800119 }
Marissa Wall1e779252018-12-29 12:01:57 -0800120 return NO_ERROR;
Craig Donnere6ecb922017-12-27 14:59:29 -0800121}
122
Marissa Walld380e2c2018-12-29 14:17:29 -0800123status_t Gralloc2Mapper::createDescriptor(void* bufferDescriptorInfo,
124 void* outBufferDescriptor) const {
Marissa Wall1e779252018-12-29 12:01:57 -0800125 IMapper::BufferDescriptorInfo* descriptorInfo =
126 static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo);
127 BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor);
128
129 status_t status = validateBufferDescriptorInfo(descriptorInfo);
130 if (status != NO_ERROR) {
131 return status;
Craig Donnere6ecb922017-12-27 14:59:29 -0800132 }
133
Marissa Wall1e779252018-12-29 12:01:57 -0800134 Error error;
Chia-I Wu4f55f162018-01-16 21:58:18 -0800135 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor)
136 {
137 error = tmpError;
138 if (error != Error::NONE) {
139 return;
140 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700141
Chia-I Wu4f55f162018-01-16 21:58:18 -0800142 *outDescriptor = tmpDescriptor;
143 };
144
145 hardware::Return<void> ret;
146 if (mMapperV2_1 != nullptr) {
Marissa Wall1e779252018-12-29 12:01:57 -0800147 ret = mMapperV2_1->createDescriptor_2_1(*descriptorInfo, hidl_cb);
Chia-I Wu4f55f162018-01-16 21:58:18 -0800148 } else {
149 const hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo info = {
Marissa Wall1e779252018-12-29 12:01:57 -0800150 descriptorInfo->width,
151 descriptorInfo->height,
152 descriptorInfo->layerCount,
153 static_cast<hardware::graphics::common::V1_0::PixelFormat>(descriptorInfo->format),
154 descriptorInfo->usage,
Chia-I Wu4f55f162018-01-16 21:58:18 -0800155 };
156 ret = mMapper->createDescriptor(info, hidl_cb);
157 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700158
Marissa Wall1e779252018-12-29 12:01:57 -0800159 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700160}
161
Marissa Walld380e2c2018-12-29 14:17:29 -0800162status_t Gralloc2Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
163 buffer_handle_t* outBufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700164 Error error;
165 auto ret = mMapper->importBuffer(rawHandle,
166 [&](const auto& tmpError, const auto& tmpBuffer)
167 {
168 error = tmpError;
169 if (error != Error::NONE) {
170 return;
171 }
172
173 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
174 });
175
Marissa Wall1e779252018-12-29 12:01:57 -0800176 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700177}
178
Marissa Walld380e2c2018-12-29 14:17:29 -0800179void Gralloc2Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700180 auto buffer = const_cast<native_handle_t*>(bufferHandle);
181 auto ret = mMapper->freeBuffer(buffer);
182
183 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
184 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d",
185 buffer, error);
186}
187
Marissa Walld380e2c2018-12-29 14:17:29 -0800188status_t Gralloc2Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
189 uint32_t height, android::PixelFormat format,
190 uint32_t layerCount, uint64_t usage,
191 uint32_t stride) const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700192 if (mMapperV2_1 == nullptr) {
Marissa Wall1e779252018-12-29 12:01:57 -0800193 return NO_ERROR;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700194 }
195
Marissa Wall1e779252018-12-29 12:01:57 -0800196 IMapper::BufferDescriptorInfo descriptorInfo = {};
197 descriptorInfo.width = width;
198 descriptorInfo.height = height;
199 descriptorInfo.layerCount = layerCount;
200 descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format);
201 descriptorInfo.usage = usage;
202
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700203 auto buffer = const_cast<native_handle_t*>(bufferHandle);
204 auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
205
Marissa Wall1e779252018-12-29 12:01:57 -0800206 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700207}
208
Marissa Walld380e2c2018-12-29 14:17:29 -0800209void Gralloc2Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
210 uint32_t* outNumInts) const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700211 *outNumFds = uint32_t(bufferHandle->numFds);
212 *outNumInts = uint32_t(bufferHandle->numInts);
213
214 if (mMapperV2_1 == nullptr) {
215 return;
216 }
217
218 Error error;
219 auto buffer = const_cast<native_handle_t*>(bufferHandle);
220 auto ret = mMapperV2_1->getTransportSize(buffer,
221 [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
222 error = tmpError;
223 if (error != Error::NONE) {
224 return;
225 }
226
227 *outNumFds = tmpNumFds;
228 *outNumInts = tmpNumInts;
229 });
230
Marissa Wall1e779252018-12-29 12:01:57 -0800231 error = (ret.isOk()) ? error : kTransactionError;
232
233 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700234}
235
Marissa Walld380e2c2018-12-29 14:17:29 -0800236status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800237 int acquireFence, void** outData, int32_t* outBytesPerPixel,
238 int32_t* outBytesPerStride) const {
239 if (outBytesPerPixel) {
240 *outBytesPerPixel = -1;
241 }
242 if (outBytesPerStride) {
243 *outBytesPerStride = -1;
244 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700245 auto buffer = const_cast<native_handle_t*>(bufferHandle);
246
Marissa Wall1e779252018-12-29 12:01:57 -0800247 IMapper::Rect accessRegion = sGralloc2Rect(bounds);
248
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700249 // put acquireFence in a hidl_handle
250 hardware::hidl_handle acquireFenceHandle;
251 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
252 if (acquireFence >= 0) {
253 auto h = native_handle_init(acquireFenceStorage, 1, 0);
254 h->data[0] = acquireFence;
255 acquireFenceHandle = h;
256 }
257
258 Error error;
259 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
260 [&](const auto& tmpError, const auto& tmpData)
261 {
262 error = tmpError;
263 if (error != Error::NONE) {
264 return;
265 }
266
267 *outData = tmpData;
268 });
269
270 // we own acquireFence even on errors
271 if (acquireFence >= 0) {
272 close(acquireFence);
273 }
274
Marissa Wall1e779252018-12-29 12:01:57 -0800275 error = (ret.isOk()) ? error : kTransactionError;
276
277 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
278
279 return static_cast<status_t>(error);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700280}
281
Marissa Walld380e2c2018-12-29 14:17:29 -0800282status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
283 int acquireFence, android_ycbcr* ycbcr) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700284 auto buffer = const_cast<native_handle_t*>(bufferHandle);
285
Marissa Wall1e779252018-12-29 12:01:57 -0800286 IMapper::Rect accessRegion = sGralloc2Rect(bounds);
287
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700288 // put acquireFence in a hidl_handle
289 hardware::hidl_handle acquireFenceHandle;
290 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
291 if (acquireFence >= 0) {
292 auto h = native_handle_init(acquireFenceStorage, 1, 0);
293 h->data[0] = acquireFence;
294 acquireFenceHandle = h;
295 }
296
Marissa Wall1e779252018-12-29 12:01:57 -0800297 YCbCrLayout layout;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700298 Error error;
299 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion,
300 acquireFenceHandle,
301 [&](const auto& tmpError, const auto& tmpLayout)
302 {
303 error = tmpError;
304 if (error != Error::NONE) {
305 return;
306 }
307
Marissa Wall1e779252018-12-29 12:01:57 -0800308 layout = tmpLayout;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700309 });
310
Marissa Wall1e779252018-12-29 12:01:57 -0800311 if (error == Error::NONE) {
312 ycbcr->y = layout.y;
313 ycbcr->cb = layout.cb;
314 ycbcr->cr = layout.cr;
315 ycbcr->ystride = static_cast<size_t>(layout.yStride);
316 ycbcr->cstride = static_cast<size_t>(layout.cStride);
317 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
318 }
319
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700320 // we own acquireFence even on errors
321 if (acquireFence >= 0) {
322 close(acquireFence);
323 }
324
Marissa Wall1e779252018-12-29 12:01:57 -0800325 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700326}
327
Marissa Walld380e2c2018-12-29 14:17:29 -0800328int Gralloc2Mapper::unlock(buffer_handle_t bufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700329 auto buffer = const_cast<native_handle_t*>(bufferHandle);
330
331 int releaseFence = -1;
332 Error error;
333 auto ret = mMapper->unlock(buffer,
334 [&](const auto& tmpError, const auto& tmpReleaseFence)
335 {
336 error = tmpError;
337 if (error != Error::NONE) {
338 return;
339 }
340
341 auto fenceHandle = tmpReleaseFence.getNativeHandle();
342 if (fenceHandle && fenceHandle->numFds == 1) {
343 int fd = dup(fenceHandle->data[0]);
344 if (fd >= 0) {
345 releaseFence = fd;
346 } else {
347 ALOGD("failed to dup unlock release fence");
348 sync_wait(fenceHandle->data[0], -1);
349 }
350 }
351 });
352
Marissa Wall1e779252018-12-29 12:01:57 -0800353 error = (ret.isOk()) ? error : kTransactionError;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700354 if (error != Error::NONE) {
355 ALOGE("unlock(%p) failed with %d", buffer, error);
356 }
357
358 return releaseFence;
359}
360
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800361status_t Gralloc2Mapper::isSupported(uint32_t /*width*/, uint32_t /*height*/,
362 android::PixelFormat /*format*/, uint32_t /*layerCount*/,
363 uint64_t /*usage*/, bool* /*outSupported*/) const {
364 return INVALID_OPERATION;
365}
366
Marissa Walld380e2c2018-12-29 14:17:29 -0800367Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(mapper) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700368 mAllocator = IAllocator::getService();
369 if (mAllocator == nullptr) {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800370 ALOGW("allocator 2.x is not supported");
371 return;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700372 }
373}
374
Valerie Hau250c6542019-01-31 14:23:43 -0800375bool Gralloc2Allocator::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800376 return mAllocator != nullptr;
377}
378
Marissa Walld380e2c2018-12-29 14:17:29 -0800379std::string Gralloc2Allocator::dumpDebugInfo() const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700380 std::string debugInfo;
381
382 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) {
383 debugInfo = tmpDebugInfo.c_str();
384 });
385
386 return debugInfo;
387}
388
Marissa Walld380e2c2018-12-29 14:17:29 -0800389status_t Gralloc2Allocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
390 uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
391 uint32_t* outStride, buffer_handle_t* outBufferHandles) const {
Marissa Wall1e779252018-12-29 12:01:57 -0800392 IMapper::BufferDescriptorInfo descriptorInfo = {};
393 descriptorInfo.width = width;
394 descriptorInfo.height = height;
395 descriptorInfo.layerCount = layerCount;
396 descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format);
397 descriptorInfo.usage = usage;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700398
Marissa Wall1e779252018-12-29 12:01:57 -0800399 BufferDescriptor descriptor;
400 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
401 static_cast<void*>(&descriptor));
402 if (error != NO_ERROR) {
403 return error;
404 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700405
Marissa Wall1e779252018-12-29 12:01:57 -0800406 auto ret = mAllocator->allocate(descriptor, bufferCount,
407 [&](const auto& tmpError, const auto& tmpStride,
408 const auto& tmpBuffers) {
409 error = static_cast<status_t>(tmpError);
410 if (tmpError != Error::NONE) {
411 return;
412 }
413
414 // import buffers
415 for (uint32_t i = 0; i < bufferCount; i++) {
416 error = mMapper.importBuffer(tmpBuffers[i],
417 &outBufferHandles[i]);
418 if (error != NO_ERROR) {
419 for (uint32_t j = 0; j < i; j++) {
420 mMapper.freeBuffer(outBufferHandles[j]);
421 outBufferHandles[j] = nullptr;
422 }
423 return;
424 }
425 }
426
427 *outStride = tmpStride;
428 });
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700429
Chia-I Wud8091b92017-05-16 14:30:34 -0700430 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
431 hardware::IPCThreadState::self()->flushCommands();
432
Marissa Wall1e779252018-12-29 12:01:57 -0800433 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700434}
435
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700436} // namespace android