blob: ea7321e24ca2861eae9cca8b9e692f0567818336 [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 }
Craig Donnere6ecb922017-12-27 14:59:29 -080051 return bits;
52 }();
53 return valid10UsageBits;
54}
55
56uint64_t getValid11UsageBits() {
57 static const uint64_t valid11UsageBits = []() -> uint64_t {
58 using hardware::graphics::common::V1_1::BufferUsage;
59 uint64_t bits = 0;
Steven Moreland3cde8752018-05-01 16:54:17 -070060 for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) {
Craig Donnere6ecb922017-12-27 14:59:29 -080061 bits = bits | bit;
62 }
Chia-I Wu4f55f162018-01-16 21:58:18 -080063 return bits;
Craig Donnere6ecb922017-12-27 14:59:29 -080064 }();
65 return valid11UsageBits;
66}
67
Marissa Walld380e2c2018-12-29 14:17:29 -080068static inline IMapper::Rect sGralloc2Rect(const Rect& rect) {
69 IMapper::Rect outRect{};
Marissa Wall1e779252018-12-29 12:01:57 -080070 outRect.left = rect.left;
71 outRect.top = rect.top;
72 outRect.width = rect.width();
73 outRect.height = rect.height();
74 return outRect;
75}
76
Craig Donnere6ecb922017-12-27 14:59:29 -080077} // anonymous namespace
78
Marissa Walld380e2c2018-12-29 14:17:29 -080079void Gralloc2Mapper::preload() {
Jesse Hall5dac7812017-07-06 14:02:29 -070080 android::hardware::preloadPassthroughService<hardware::graphics::mapper::V2_0::IMapper>();
81}
82
Marissa Walld380e2c2018-12-29 14:17:29 -080083Gralloc2Mapper::Gralloc2Mapper() {
Chia-I Wu4f55f162018-01-16 21:58:18 -080084 mMapper = hardware::graphics::mapper::V2_0::IMapper::getService();
Chia-I Wudbbe33b2017-09-27 15:22:21 -070085 if (mMapper == nullptr) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080086 ALOGW("mapper 2.x is not supported");
87 return;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070088 }
89 if (mMapper->isRemote()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -070090 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
91 }
Chia-I Wudbbe33b2017-09-27 15:22:21 -070092
93 // IMapper 2.1 is optional
Chia-I Wu4f55f162018-01-16 21:58:18 -080094 mMapperV2_1 = IMapper::castFrom(mMapper);
Chia-I Wu5bac7f32017-04-06 12:34:32 -070095}
96
Marissa Wall925bf7f2018-12-29 14:27:11 -080097bool Gralloc2Mapper::isSupported() const {
98 return mMapper != nullptr;
99}
100
Marissa Walld380e2c2018-12-29 14:17:29 -0800101status_t Gralloc2Mapper::validateBufferDescriptorInfo(
102 IMapper::BufferDescriptorInfo* descriptorInfo) const {
Craig Donnere6ecb922017-12-27 14:59:29 -0800103 uint64_t validUsageBits = getValid10UsageBits();
104 if (mMapperV2_1 != nullptr) {
105 validUsageBits = validUsageBits | getValid11UsageBits();
106 }
107
Marissa Wall1e779252018-12-29 12:01:57 -0800108 if (descriptorInfo->usage & ~validUsageBits) {
Craig Donnere6ecb922017-12-27 14:59:29 -0800109 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
Marissa Wall1e779252018-12-29 12:01:57 -0800110 descriptorInfo->usage & ~validUsageBits);
111 return BAD_VALUE;
Craig Donnere6ecb922017-12-27 14:59:29 -0800112 }
Marissa Wall1e779252018-12-29 12:01:57 -0800113 return NO_ERROR;
Craig Donnere6ecb922017-12-27 14:59:29 -0800114}
115
Marissa Walld380e2c2018-12-29 14:17:29 -0800116status_t Gralloc2Mapper::createDescriptor(void* bufferDescriptorInfo,
117 void* outBufferDescriptor) const {
Marissa Wall1e779252018-12-29 12:01:57 -0800118 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;
Craig Donnere6ecb922017-12-27 14:59:29 -0800125 }
126
Marissa Wall1e779252018-12-29 12:01:57 -0800127 Error error;
Chia-I Wu4f55f162018-01-16 21:58:18 -0800128 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor)
129 {
130 error = tmpError;
131 if (error != Error::NONE) {
132 return;
133 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700134
Chia-I Wu4f55f162018-01-16 21:58:18 -0800135 *outDescriptor = tmpDescriptor;
136 };
137
138 hardware::Return<void> ret;
139 if (mMapperV2_1 != nullptr) {
Marissa Wall1e779252018-12-29 12:01:57 -0800140 ret = mMapperV2_1->createDescriptor_2_1(*descriptorInfo, hidl_cb);
Chia-I Wu4f55f162018-01-16 21:58:18 -0800141 } else {
142 const hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo info = {
Marissa Wall1e779252018-12-29 12:01:57 -0800143 descriptorInfo->width,
144 descriptorInfo->height,
145 descriptorInfo->layerCount,
146 static_cast<hardware::graphics::common::V1_0::PixelFormat>(descriptorInfo->format),
147 descriptorInfo->usage,
Chia-I Wu4f55f162018-01-16 21:58:18 -0800148 };
149 ret = mMapper->createDescriptor(info, hidl_cb);
150 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700151
Marissa Wall1e779252018-12-29 12:01:57 -0800152 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700153}
154
Marissa Walld380e2c2018-12-29 14:17:29 -0800155status_t Gralloc2Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
156 buffer_handle_t* outBufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700157 Error error;
158 auto ret = mMapper->importBuffer(rawHandle,
159 [&](const auto& tmpError, const auto& tmpBuffer)
160 {
161 error = tmpError;
162 if (error != Error::NONE) {
163 return;
164 }
165
166 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
167 });
168
Marissa Wall1e779252018-12-29 12:01:57 -0800169 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700170}
171
Marissa Walld380e2c2018-12-29 14:17:29 -0800172void Gralloc2Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700173 auto buffer = const_cast<native_handle_t*>(bufferHandle);
174 auto ret = mMapper->freeBuffer(buffer);
175
176 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
177 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d",
178 buffer, error);
179}
180
Marissa Walld380e2c2018-12-29 14:17:29 -0800181status_t Gralloc2Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
182 uint32_t height, android::PixelFormat format,
183 uint32_t layerCount, uint64_t usage,
184 uint32_t stride) const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700185 if (mMapperV2_1 == nullptr) {
Marissa Wall1e779252018-12-29 12:01:57 -0800186 return NO_ERROR;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700187 }
188
Marissa Wall1e779252018-12-29 12:01:57 -0800189 IMapper::BufferDescriptorInfo descriptorInfo = {};
190 descriptorInfo.width = width;
191 descriptorInfo.height = height;
192 descriptorInfo.layerCount = layerCount;
193 descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format);
194 descriptorInfo.usage = usage;
195
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700196 auto buffer = const_cast<native_handle_t*>(bufferHandle);
197 auto ret = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
198
Marissa Wall1e779252018-12-29 12:01:57 -0800199 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700200}
201
Marissa Walld380e2c2018-12-29 14:17:29 -0800202void Gralloc2Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
203 uint32_t* outNumInts) const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700204 *outNumFds = uint32_t(bufferHandle->numFds);
205 *outNumInts = uint32_t(bufferHandle->numInts);
206
207 if (mMapperV2_1 == nullptr) {
208 return;
209 }
210
211 Error error;
212 auto buffer = const_cast<native_handle_t*>(bufferHandle);
213 auto ret = mMapperV2_1->getTransportSize(buffer,
214 [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
215 error = tmpError;
216 if (error != Error::NONE) {
217 return;
218 }
219
220 *outNumFds = tmpNumFds;
221 *outNumInts = tmpNumInts;
222 });
223
Marissa Wall1e779252018-12-29 12:01:57 -0800224 error = (ret.isOk()) ? error : kTransactionError;
225
226 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700227}
228
Marissa Walld380e2c2018-12-29 14:17:29 -0800229status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
230 int acquireFence, void** outData) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700231 auto buffer = const_cast<native_handle_t*>(bufferHandle);
232
Marissa Wall1e779252018-12-29 12:01:57 -0800233 IMapper::Rect accessRegion = sGralloc2Rect(bounds);
234
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700235 // put acquireFence in a hidl_handle
236 hardware::hidl_handle acquireFenceHandle;
237 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
238 if (acquireFence >= 0) {
239 auto h = native_handle_init(acquireFenceStorage, 1, 0);
240 h->data[0] = acquireFence;
241 acquireFenceHandle = h;
242 }
243
244 Error error;
245 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
246 [&](const auto& tmpError, const auto& tmpData)
247 {
248 error = tmpError;
249 if (error != Error::NONE) {
250 return;
251 }
252
253 *outData = tmpData;
254 });
255
256 // we own acquireFence even on errors
257 if (acquireFence >= 0) {
258 close(acquireFence);
259 }
260
Marissa Wall1e779252018-12-29 12:01:57 -0800261 error = (ret.isOk()) ? error : kTransactionError;
262
263 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
264
265 return static_cast<status_t>(error);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700266}
267
Marissa Walld380e2c2018-12-29 14:17:29 -0800268status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
269 int acquireFence, android_ycbcr* ycbcr) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700270 auto buffer = const_cast<native_handle_t*>(bufferHandle);
271
Marissa Wall1e779252018-12-29 12:01:57 -0800272 IMapper::Rect accessRegion = sGralloc2Rect(bounds);
273
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700274 // put acquireFence in a hidl_handle
275 hardware::hidl_handle acquireFenceHandle;
276 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
277 if (acquireFence >= 0) {
278 auto h = native_handle_init(acquireFenceStorage, 1, 0);
279 h->data[0] = acquireFence;
280 acquireFenceHandle = h;
281 }
282
Marissa Wall1e779252018-12-29 12:01:57 -0800283 YCbCrLayout layout;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700284 Error error;
285 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion,
286 acquireFenceHandle,
287 [&](const auto& tmpError, const auto& tmpLayout)
288 {
289 error = tmpError;
290 if (error != Error::NONE) {
291 return;
292 }
293
Marissa Wall1e779252018-12-29 12:01:57 -0800294 layout = tmpLayout;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700295 });
296
Marissa Wall1e779252018-12-29 12:01:57 -0800297 if (error == Error::NONE) {
298 ycbcr->y = layout.y;
299 ycbcr->cb = layout.cb;
300 ycbcr->cr = layout.cr;
301 ycbcr->ystride = static_cast<size_t>(layout.yStride);
302 ycbcr->cstride = static_cast<size_t>(layout.cStride);
303 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
304 }
305
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700306 // we own acquireFence even on errors
307 if (acquireFence >= 0) {
308 close(acquireFence);
309 }
310
Marissa Wall1e779252018-12-29 12:01:57 -0800311 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700312}
313
Marissa Walld380e2c2018-12-29 14:17:29 -0800314int Gralloc2Mapper::unlock(buffer_handle_t bufferHandle) const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700315 auto buffer = const_cast<native_handle_t*>(bufferHandle);
316
317 int releaseFence = -1;
318 Error error;
319 auto ret = mMapper->unlock(buffer,
320 [&](const auto& tmpError, const auto& tmpReleaseFence)
321 {
322 error = tmpError;
323 if (error != Error::NONE) {
324 return;
325 }
326
327 auto fenceHandle = tmpReleaseFence.getNativeHandle();
328 if (fenceHandle && fenceHandle->numFds == 1) {
329 int fd = dup(fenceHandle->data[0]);
330 if (fd >= 0) {
331 releaseFence = fd;
332 } else {
333 ALOGD("failed to dup unlock release fence");
334 sync_wait(fenceHandle->data[0], -1);
335 }
336 }
337 });
338
Marissa Wall1e779252018-12-29 12:01:57 -0800339 error = (ret.isOk()) ? error : kTransactionError;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700340 if (error != Error::NONE) {
341 ALOGE("unlock(%p) failed with %d", buffer, error);
342 }
343
344 return releaseFence;
345}
346
Marissa Walld380e2c2018-12-29 14:17:29 -0800347Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(mapper) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700348 mAllocator = IAllocator::getService();
349 if (mAllocator == nullptr) {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800350 ALOGW("allocator 2.x is not supported");
351 return;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700352 }
353}
354
Marissa Wall925bf7f2018-12-29 14:27:11 -0800355bool Gralloc2Allocator::isSupported() const {
356 return mAllocator != nullptr;
357}
358
Marissa Walld380e2c2018-12-29 14:17:29 -0800359std::string Gralloc2Allocator::dumpDebugInfo() const {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700360 std::string debugInfo;
361
362 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) {
363 debugInfo = tmpDebugInfo.c_str();
364 });
365
366 return debugInfo;
367}
368
Marissa Walld380e2c2018-12-29 14:17:29 -0800369status_t Gralloc2Allocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
370 uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
371 uint32_t* outStride, buffer_handle_t* outBufferHandles) const {
Marissa Wall1e779252018-12-29 12:01:57 -0800372 IMapper::BufferDescriptorInfo descriptorInfo = {};
373 descriptorInfo.width = width;
374 descriptorInfo.height = height;
375 descriptorInfo.layerCount = layerCount;
376 descriptorInfo.format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format);
377 descriptorInfo.usage = usage;
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700378
Marissa Wall1e779252018-12-29 12:01:57 -0800379 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 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700385
Marissa Wall1e779252018-12-29 12:01:57 -0800386 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
394 // import buffers
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;
404 }
405 }
406
407 *outStride = tmpStride;
408 });
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700409
Chia-I Wud8091b92017-05-16 14:30:34 -0700410 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
411 hardware::IPCThreadState::self()->flushCommands();
412
Marissa Wall1e779252018-12-29 12:01:57 -0800413 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700414}
415
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700416} // namespace android