blob: e698bf4b9ad070c7281a5e88b63bf6517c459348 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 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_NDEBUG 0
18#define LOG_TAG "C2AllocatorGralloc"
19#include <utils/Log.h>
20
21#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
22#include <android/hardware/graphics/mapper/2.0/IMapper.h>
23#include <cutils/native_handle.h>
24#include <hardware/gralloc.h>
25
26#include <C2AllocatorGralloc.h>
27#include <C2Buffer.h>
28#include <C2PlatformSupport.h>
29
30namespace android {
31
32namespace {
33 enum : uint64_t {
34 /**
35 * Usage mask that is passed through from gralloc to Codec 2.0 usage.
36 */
37 PASSTHROUGH_USAGE_MASK =
38 ~(GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_PROTECTED)
39 };
40
41 // verify that passthrough mask is within the platform mask
42 static_assert((~C2MemoryUsage::PLATFORM_MASK & PASSTHROUGH_USAGE_MASK) == 0, "");
43}
44
45C2MemoryUsage C2AndroidMemoryUsage::FromGrallocUsage(uint64_t usage) {
46 // gralloc does not support WRITE_PROTECTED
47 return C2MemoryUsage(
48 ((usage & GRALLOC_USAGE_SW_READ_MASK) ? C2MemoryUsage::CPU_READ : 0) |
49 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ? C2MemoryUsage::CPU_WRITE : 0) |
50 ((usage & GRALLOC_USAGE_PROTECTED) ? C2MemoryUsage::READ_PROTECTED : 0) |
51 (usage & PASSTHROUGH_USAGE_MASK));
52}
53
54uint64_t C2AndroidMemoryUsage::asGrallocUsage() const {
55 // gralloc does not support WRITE_PROTECTED
56 return (((expected & C2MemoryUsage::CPU_READ) ? GRALLOC_USAGE_SW_READ_OFTEN : 0) |
57 ((expected & C2MemoryUsage::CPU_WRITE) ? GRALLOC_USAGE_SW_WRITE_OFTEN : 0) |
58 ((expected & C2MemoryUsage::READ_PROTECTED) ? GRALLOC_USAGE_PROTECTED : 0) |
59 (expected & PASSTHROUGH_USAGE_MASK));
60}
61
62using ::android::hardware::graphics::allocator::V2_0::IAllocator;
63using ::android::hardware::graphics::common::V1_0::BufferUsage;
64using ::android::hardware::graphics::common::V1_0::PixelFormat;
65using ::android::hardware::graphics::mapper::V2_0::BufferDescriptor;
66using ::android::hardware::graphics::mapper::V2_0::Error;
67using ::android::hardware::graphics::mapper::V2_0::IMapper;
68using ::android::hardware::graphics::mapper::V2_0::YCbCrLayout;
69using ::android::hardware::hidl_handle;
70using ::android::hardware::hidl_vec;
71
72namespace {
73
74struct BufferDescriptorInfo {
75 IMapper::BufferDescriptorInfo mapperInfo;
76 uint32_t stride;
77};
78
79}
80
81/* ===================================== GRALLOC ALLOCATION ==================================== */
82static c2_status_t maperr2error(Error maperr) {
83 switch (maperr) {
84 case Error::NONE: return C2_OK;
85 case Error::BAD_DESCRIPTOR: return C2_BAD_VALUE;
86 case Error::BAD_BUFFER: return C2_BAD_VALUE;
87 case Error::BAD_VALUE: return C2_BAD_VALUE;
88 case Error::NO_RESOURCES: return C2_NO_MEMORY;
89 case Error::UNSUPPORTED: return C2_CANNOT_DO;
90 }
91 return C2_CORRUPTED;
92}
93
94static
95bool native_handle_is_invalid(const native_handle_t *const handle) {
96 // perform basic validation of a native handle
97 if (handle == nullptr) {
98 // null handle is considered valid
99 return false;
100 }
101 return ((size_t)handle->version != sizeof(native_handle_t) ||
102 handle->numFds < 0 ||
103 handle->numInts < 0 ||
104 // for sanity assume handles must occupy less memory than INT_MAX bytes
105 handle->numFds > int((INT_MAX - handle->version) / sizeof(int)) - handle->numInts);
106}
107
108class C2HandleGralloc : public C2Handle {
109private:
110 struct ExtraData {
111 uint32_t width;
112 uint32_t height;
113 uint32_t format;
114 uint32_t usage_lo;
115 uint32_t usage_hi;
116 uint32_t stride;
117 uint32_t generation;
118 uint32_t igbp_id_lo;
119 uint32_t igbp_id_hi;
120 uint32_t igbp_slot;
121 uint32_t magic;
122 };
123
124 enum {
125 NUM_INTS = sizeof(ExtraData) / sizeof(int),
126 };
127 const static uint32_t MAGIC = '\xc2gr\x00';
128
129 static
130 const ExtraData* getExtraData(const C2Handle *const handle) {
131 if (handle == nullptr
132 || native_handle_is_invalid(handle)
133 || handle->numInts < NUM_INTS) {
134 return nullptr;
135 }
136 return reinterpret_cast<const ExtraData*>(
137 &handle->data[handle->numFds + handle->numInts - NUM_INTS]);
138 }
139
140 static
141 ExtraData *getExtraData(C2Handle *const handle) {
142 return const_cast<ExtraData *>(getExtraData(const_cast<const C2Handle *const>(handle)));
143 }
144
145public:
146 void getIgbpData(uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) const {
147 const ExtraData *ed = getExtraData(this);
148 *generation = ed->generation;
149 *igbp_id = unsigned(ed->igbp_id_lo) | uint64_t(unsigned(ed->igbp_id_hi)) << 32;
150 *igbp_slot = ed->igbp_slot;
151 }
152
153 static bool isValid(const C2Handle *const o) {
154 if (o == nullptr) { // null handle is always valid
155 return true;
156 }
157 const ExtraData *xd = getExtraData(o);
158 // we cannot validate width/height/format/usage without accessing gralloc driver
159 return xd != nullptr && xd->magic == MAGIC;
160 }
161
Sungtak Leea4d13be2019-01-23 15:24:46 -0800162 static C2HandleGralloc* WrapAndMoveNativeHandle(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800163 const native_handle_t *const handle,
164 uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
165 uint32_t stride, uint32_t generation, uint64_t igbp_id = 0, uint32_t igbp_slot = 0) {
166 //CHECK(handle != nullptr);
167 if (native_handle_is_invalid(handle) ||
168 handle->numInts > int((INT_MAX - handle->version) / sizeof(int)) - NUM_INTS - handle->numFds) {
169 return nullptr;
170 }
171 ExtraData xd = {
172 width, height, format, uint32_t(usage & 0xFFFFFFFF), uint32_t(usage >> 32),
173 stride, generation, uint32_t(igbp_id & 0xFFFFFFFF), uint32_t(igbp_id >> 32),
174 igbp_slot, MAGIC
175 };
176 native_handle_t *res = native_handle_create(handle->numFds, handle->numInts + NUM_INTS);
177 if (res != nullptr) {
178 memcpy(&res->data, &handle->data, sizeof(int) * (handle->numFds + handle->numInts));
179 *getExtraData(res) = xd;
180 }
181 return reinterpret_cast<C2HandleGralloc *>(res);
182 }
183
Sungtak Leea4d13be2019-01-23 15:24:46 -0800184 static C2HandleGralloc* WrapNativeHandle(
185 const native_handle_t *const handle,
186 uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
187 uint32_t stride, uint32_t generation, uint64_t igbp_id = 0, uint32_t igbp_slot = 0) {
188 if (handle == nullptr) {
189 return nullptr;
190 }
191 native_handle_t *clone = native_handle_clone(handle);
192 if (clone == nullptr) {
193 return nullptr;
194 }
195 C2HandleGralloc *res = WrapAndMoveNativeHandle(
196 clone, width, height, format, usage, stride, generation, igbp_id, igbp_slot);
197 if (res == nullptr) {
198 native_handle_close(clone);
199 }
200 native_handle_delete(clone);
201 return res;
202 }
203
Pawin Vongmasa36653902018-11-15 00:10:25 -0800204 static native_handle_t* UnwrapNativeHandle(
205 const C2Handle *const handle) {
206 const ExtraData *xd = getExtraData(handle);
207 if (xd == nullptr || xd->magic != MAGIC) {
208 return nullptr;
209 }
210 native_handle_t *res = native_handle_create(handle->numFds, handle->numInts - NUM_INTS);
211 if (res != nullptr) {
212 memcpy(&res->data, &handle->data, sizeof(int) * (res->numFds + res->numInts));
213 }
214 return res;
215 }
216
217 static native_handle_t* UnwrapNativeHandle(
218 const C2Handle *const handle,
219 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
220 const ExtraData *xd = getExtraData(handle);
221 if (xd == nullptr || xd->magic != MAGIC) {
222 return nullptr;
223 }
224 *generation = xd->generation;
225 *igbp_id = unsigned(xd->igbp_id_lo) | uint64_t(unsigned(xd->igbp_id_hi)) << 32;
226 *igbp_slot = xd->igbp_slot;
227 native_handle_t *res = native_handle_create(handle->numFds, handle->numInts - NUM_INTS);
228 if (res != nullptr) {
229 memcpy(&res->data, &handle->data, sizeof(int) * (res->numFds + res->numInts));
230 }
231 return res;
232 }
233
234 static const C2HandleGralloc* Import(
235 const C2Handle *const handle,
236 uint32_t *width, uint32_t *height, uint32_t *format,
237 uint64_t *usage, uint32_t *stride,
238 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
239 const ExtraData *xd = getExtraData(handle);
240 if (xd == nullptr) {
241 return nullptr;
242 }
243 *width = xd->width;
244 *height = xd->height;
245 *format = xd->format;
246 *usage = xd->usage_lo | (uint64_t(xd->usage_hi) << 32);
247 *stride = xd->stride;
248 *generation = xd->generation;
249 *igbp_id = xd->igbp_id_lo | (uint64_t(xd->igbp_id_hi) << 32);
250 *igbp_slot = xd->igbp_slot;
251 return reinterpret_cast<const C2HandleGralloc *>(handle);
252 }
253};
254
255native_handle_t *UnwrapNativeCodec2GrallocHandle(const C2Handle *const handle) {
256 return C2HandleGralloc::UnwrapNativeHandle(handle);
257}
258
259native_handle_t *UnwrapNativeCodec2GrallocHandle(
260 const C2Handle *const handle,
261 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
262 return C2HandleGralloc::UnwrapNativeHandle(handle, generation, igbp_id, igbp_slot);
263}
264
265C2Handle *WrapNativeCodec2GrallocHandle(
266 const native_handle_t *const handle,
267 uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
268 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
269 return C2HandleGralloc::WrapNativeHandle(handle, width, height, format, usage, stride,
270 generation, igbp_id, igbp_slot);
271}
272
273class C2AllocationGralloc : public C2GraphicAllocation {
274public:
275 virtual ~C2AllocationGralloc() override;
276
277 virtual c2_status_t map(
278 C2Rect rect, C2MemoryUsage usage, C2Fence *fence,
279 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) override;
280 virtual c2_status_t unmap(
281 uint8_t **addr /* nonnull */, C2Rect rect, C2Fence *fence /* nullable */) override;
282 virtual C2Allocator::id_t getAllocatorId() const override { return mAllocatorId; }
283 virtual const C2Handle *handle() const override { return mLockedHandle ? : mHandle; }
284 virtual bool equals(const std::shared_ptr<const C2GraphicAllocation> &other) const override;
285
286 // internal methods
287 // |handle| will be moved.
288 C2AllocationGralloc(
289 const BufferDescriptorInfo &info,
290 const sp<IMapper> &mapper,
291 hidl_handle &hidlHandle,
292 const C2HandleGralloc *const handle,
293 C2Allocator::id_t allocatorId);
294 int dup() const;
295 c2_status_t status() const;
296
297private:
298 const BufferDescriptorInfo mInfo;
299 const sp<IMapper> mMapper;
300 const hidl_handle mHidlHandle;
301 const C2HandleGralloc *mHandle;
302 buffer_handle_t mBuffer;
303 const C2HandleGralloc *mLockedHandle;
304 bool mLocked;
305 C2Allocator::id_t mAllocatorId;
306 std::mutex mMappedLock;
307};
308
309C2AllocationGralloc::C2AllocationGralloc(
310 const BufferDescriptorInfo &info,
311 const sp<IMapper> &mapper,
312 hidl_handle &hidlHandle,
313 const C2HandleGralloc *const handle,
314 C2Allocator::id_t allocatorId)
315 : C2GraphicAllocation(info.mapperInfo.width, info.mapperInfo.height),
316 mInfo(info),
317 mMapper(mapper),
318 mHidlHandle(std::move(hidlHandle)),
319 mHandle(handle),
320 mBuffer(nullptr),
321 mLockedHandle(nullptr),
322 mLocked(false),
323 mAllocatorId(allocatorId) {
324}
325
326C2AllocationGralloc::~C2AllocationGralloc() {
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800327 if (mBuffer && mLocked) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800328 // implementation ignores addresss and rect
329 uint8_t* addr[C2PlanarLayout::MAX_NUM_PLANES] = {};
330 unmap(addr, C2Rect(), nullptr);
331 }
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800332 if (mBuffer) {
333 mMapper->freeBuffer(const_cast<native_handle_t *>(mBuffer));
334 }
335 if (mHandle) {
336 native_handle_delete(
337 const_cast<native_handle_t *>(reinterpret_cast<const native_handle_t *>(mHandle)));
338 }
Sungtak Lee2729dcf2019-01-18 13:15:07 -0800339 if (mLockedHandle) {
340 native_handle_delete(
341 const_cast<native_handle_t *>(
342 reinterpret_cast<const native_handle_t *>(mLockedHandle)));
343 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800344}
345
346c2_status_t C2AllocationGralloc::map(
347 C2Rect rect, C2MemoryUsage usage, C2Fence *fence,
348 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) {
349 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
350 ALOGV("mapping buffer with usage %#llx => %#llx",
351 (long long)usage.expected, (long long)grallocUsage);
352
353 // TODO
354 (void) fence;
355
356 std::lock_guard<std::mutex> lock(mMappedLock);
357 if (mBuffer && mLocked) {
358 ALOGD("already mapped");
359 return C2_DUPLICATE;
360 }
361 if (!layout || !addr) {
362 ALOGD("wrong param");
363 return C2_BAD_VALUE;
364 }
365
366 c2_status_t err = C2_OK;
367 if (!mBuffer) {
368 mMapper->importBuffer(
369 mHidlHandle, [&err, this](const auto &maperr, const auto &buffer) {
370 err = maperr2error(maperr);
371 if (err == C2_OK) {
372 mBuffer = static_cast<buffer_handle_t>(buffer);
373 }
374 });
375 if (err != C2_OK) {
376 ALOGD("importBuffer failed: %d", err);
377 return err;
378 }
379 if (mBuffer == nullptr) {
380 ALOGD("importBuffer returned null buffer");
381 return C2_CORRUPTED;
382 }
383 uint32_t generation = 0;
384 uint64_t igbp_id = 0;
385 uint32_t igbp_slot = 0;
386 if (mHandle) {
387 mHandle->getIgbpData(&generation, &igbp_id, &igbp_slot);
388 }
Sungtak Leea4d13be2019-01-23 15:24:46 -0800389 mLockedHandle = C2HandleGralloc::WrapAndMoveNativeHandle(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800390 mBuffer, mInfo.mapperInfo.width, mInfo.mapperInfo.height,
391 (uint32_t)mInfo.mapperInfo.format, mInfo.mapperInfo.usage, mInfo.stride,
392 generation, igbp_id, igbp_slot);
393 }
394
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700395 switch (mInfo.mapperInfo.format) {
396 case PixelFormat::RGBA_1010102: {
397 // TRICKY: this is used for media as YUV444 in the case when it is queued directly to a
398 // Surface. In all other cases it is RGBA. We don't know which case it is here, so
399 // default to YUV for now.
400 void *pointer = nullptr;
401 mMapper->lock(
402 const_cast<native_handle_t *>(mBuffer),
403 grallocUsage,
404 { (int32_t)rect.left, (int32_t)rect.top, (int32_t)rect.width, (int32_t)rect.height },
405 // TODO: fence
406 hidl_handle(),
407 [&err, &pointer](const auto &maperr, const auto &mapPointer) {
408 err = maperr2error(maperr);
409 if (err == C2_OK) {
410 pointer = mapPointer;
411 }
412 });
413 if (err != C2_OK) {
414 ALOGD("lock failed: %d", err);
415 return err;
416 }
417 // treat as 32-bit values
418 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)pointer;
419 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)pointer;
420 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)pointer;
421 addr[C2PlanarLayout::PLANE_A] = (uint8_t *)pointer;
422 layout->type = C2PlanarLayout::TYPE_YUVA;
423 layout->numPlanes = 4;
424 layout->rootPlanes = 1;
425 layout->planes[C2PlanarLayout::PLANE_Y] = {
426 C2PlaneInfo::CHANNEL_Y, // channel
427 4, // colInc
428 4 * (int32_t)mInfo.stride, // rowInc
429 1, // mColSampling
430 1, // mRowSampling
431 32, // allocatedDepth
432 10, // bitDepth
433 10, // rightShift
434 C2PlaneInfo::LITTLE_END, // endianness
435 C2PlanarLayout::PLANE_Y, // rootIx
436 0, // offset
437 };
438 layout->planes[C2PlanarLayout::PLANE_U] = {
439 C2PlaneInfo::CHANNEL_CB, // channel
440 4, // colInc
441 4 * (int32_t)mInfo.stride, // rowInc
442 1, // mColSampling
443 1, // mRowSampling
444 32, // allocatedDepth
445 10, // bitDepth
446 0, // rightShift
447 C2PlaneInfo::LITTLE_END, // endianness
448 C2PlanarLayout::PLANE_Y, // rootIx
449 0, // offset
450 };
451 layout->planes[C2PlanarLayout::PLANE_V] = {
452 C2PlaneInfo::CHANNEL_CR, // channel
453 4, // colInc
454 4 * (int32_t)mInfo.stride, // rowInc
455 1, // mColSampling
456 1, // mRowSampling
457 32, // allocatedDepth
458 10, // bitDepth
459 20, // rightShift
460 C2PlaneInfo::LITTLE_END, // endianness
461 C2PlanarLayout::PLANE_Y, // rootIx
462 0, // offset
463 };
464 layout->planes[C2PlanarLayout::PLANE_A] = {
465 C2PlaneInfo::CHANNEL_A, // channel
466 4, // colInc
467 4 * (int32_t)mInfo.stride, // rowInc
468 1, // mColSampling
469 1, // mRowSampling
470 32, // allocatedDepth
471 2, // bitDepth
472 30, // rightShift
473 C2PlaneInfo::LITTLE_END, // endianness
474 C2PlanarLayout::PLANE_Y, // rootIx
475 0, // offset
476 };
477 break;
478 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800479
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700480 case PixelFormat::RGBA_8888:
481 // TODO: alpha channel
482 // fall-through
483 case PixelFormat::RGBX_8888: {
484 void *pointer = nullptr;
485 mMapper->lock(
486 const_cast<native_handle_t *>(mBuffer),
487 grallocUsage,
488 { (int32_t)rect.left, (int32_t)rect.top, (int32_t)rect.width, (int32_t)rect.height },
489 // TODO: fence
490 hidl_handle(),
491 [&err, &pointer](const auto &maperr, const auto &mapPointer) {
492 err = maperr2error(maperr);
493 if (err == C2_OK) {
494 pointer = mapPointer;
495 }
496 });
497 if (err != C2_OK) {
498 ALOGD("lock failed: %d", err);
499 return err;
500 }
501 addr[C2PlanarLayout::PLANE_R] = (uint8_t *)pointer;
502 addr[C2PlanarLayout::PLANE_G] = (uint8_t *)pointer + 1;
503 addr[C2PlanarLayout::PLANE_B] = (uint8_t *)pointer + 2;
504 layout->type = C2PlanarLayout::TYPE_RGB;
505 layout->numPlanes = 3;
506 layout->rootPlanes = 1;
507 layout->planes[C2PlanarLayout::PLANE_R] = {
508 C2PlaneInfo::CHANNEL_R, // channel
509 4, // colInc
510 4 * (int32_t)mInfo.stride, // rowInc
511 1, // mColSampling
512 1, // mRowSampling
513 8, // allocatedDepth
514 8, // bitDepth
515 0, // rightShift
516 C2PlaneInfo::NATIVE, // endianness
517 C2PlanarLayout::PLANE_R, // rootIx
518 0, // offset
519 };
520 layout->planes[C2PlanarLayout::PLANE_G] = {
521 C2PlaneInfo::CHANNEL_G, // channel
522 4, // colInc
523 4 * (int32_t)mInfo.stride, // rowInc
524 1, // mColSampling
525 1, // mRowSampling
526 8, // allocatedDepth
527 8, // bitDepth
528 0, // rightShift
529 C2PlaneInfo::NATIVE, // endianness
530 C2PlanarLayout::PLANE_R, // rootIx
531 1, // offset
532 };
533 layout->planes[C2PlanarLayout::PLANE_B] = {
534 C2PlaneInfo::CHANNEL_B, // channel
535 4, // colInc
536 4 * (int32_t)mInfo.stride, // rowInc
537 1, // mColSampling
538 1, // mRowSampling
539 8, // allocatedDepth
540 8, // bitDepth
541 0, // rightShift
542 C2PlaneInfo::NATIVE, // endianness
543 C2PlanarLayout::PLANE_R, // rootIx
544 2, // offset
545 };
546 break;
547 }
548
Pawin Vongmasa36653902018-11-15 00:10:25 -0800549 case PixelFormat::YCBCR_420_888:
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700550 // fall-through
551 case PixelFormat::YV12:
552 // fall-through
553 default: {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800554 YCbCrLayout ycbcrLayout;
555 mMapper->lockYCbCr(
556 const_cast<native_handle_t *>(mBuffer), grallocUsage,
557 { (int32_t)rect.left, (int32_t)rect.top, (int32_t)rect.width, (int32_t)rect.height },
558 // TODO: fence
559 hidl_handle(),
560 [&err, &ycbcrLayout](const auto &maperr, const auto &mapLayout) {
561 err = maperr2error(maperr);
562 if (err == C2_OK) {
563 ycbcrLayout = mapLayout;
564 }
565 });
566 if (err != C2_OK) {
567 ALOGD("lockYCbCr failed: %d", err);
568 return err;
569 }
570 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)ycbcrLayout.y;
571 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)ycbcrLayout.cb;
572 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)ycbcrLayout.cr;
573 layout->type = C2PlanarLayout::TYPE_YUV;
574 layout->numPlanes = 3;
575 layout->rootPlanes = 3;
576 layout->planes[C2PlanarLayout::PLANE_Y] = {
577 C2PlaneInfo::CHANNEL_Y, // channel
578 1, // colInc
579 (int32_t)ycbcrLayout.yStride, // rowInc
580 1, // mColSampling
581 1, // mRowSampling
582 8, // allocatedDepth
583 8, // bitDepth
584 0, // rightShift
585 C2PlaneInfo::NATIVE, // endianness
586 C2PlanarLayout::PLANE_Y, // rootIx
587 0, // offset
588 };
589 layout->planes[C2PlanarLayout::PLANE_U] = {
590 C2PlaneInfo::CHANNEL_CB, // channel
591 (int32_t)ycbcrLayout.chromaStep, // colInc
592 (int32_t)ycbcrLayout.cStride, // rowInc
593 2, // mColSampling
594 2, // mRowSampling
595 8, // allocatedDepth
596 8, // bitDepth
597 0, // rightShift
598 C2PlaneInfo::NATIVE, // endianness
599 C2PlanarLayout::PLANE_U, // rootIx
600 0, // offset
601 };
602 layout->planes[C2PlanarLayout::PLANE_V] = {
603 C2PlaneInfo::CHANNEL_CR, // channel
604 (int32_t)ycbcrLayout.chromaStep, // colInc
605 (int32_t)ycbcrLayout.cStride, // rowInc
606 2, // mColSampling
607 2, // mRowSampling
608 8, // allocatedDepth
609 8, // bitDepth
610 0, // rightShift
611 C2PlaneInfo::NATIVE, // endianness
612 C2PlanarLayout::PLANE_V, // rootIx
613 0, // offset
614 };
615 // handle interleaved formats
616 intptr_t uvOffset = addr[C2PlanarLayout::PLANE_V] - addr[C2PlanarLayout::PLANE_U];
617 if (uvOffset > 0 && uvOffset < (intptr_t)ycbcrLayout.chromaStep) {
618 layout->rootPlanes = 2;
619 layout->planes[C2PlanarLayout::PLANE_V].rootIx = C2PlanarLayout::PLANE_U;
620 layout->planes[C2PlanarLayout::PLANE_V].offset = uvOffset;
621 } else if (uvOffset < 0 && uvOffset > -(intptr_t)ycbcrLayout.chromaStep) {
622 layout->rootPlanes = 2;
623 layout->planes[C2PlanarLayout::PLANE_U].rootIx = C2PlanarLayout::PLANE_V;
624 layout->planes[C2PlanarLayout::PLANE_U].offset = -uvOffset;
625 }
626 break;
627 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800628 }
629 mLocked = true;
630
631 return C2_OK;
632}
633
634c2_status_t C2AllocationGralloc::unmap(
635 uint8_t **addr, C2Rect rect, C2Fence *fence /* nullable */) {
636 // TODO: check addr and size, use fence
637 (void)addr;
638 (void)rect;
639
640 std::lock_guard<std::mutex> lock(mMappedLock);
641 c2_status_t err = C2_OK;
642 mMapper->unlock(
643 const_cast<native_handle_t *>(mBuffer),
644 [&err, &fence](const auto &maperr, const auto &releaseFence) {
645 // TODO
646 (void) fence;
647 (void) releaseFence;
648 err = maperr2error(maperr);
649 if (err == C2_OK) {
650 // TODO: fence
651 }
652 });
653 if (err == C2_OK) {
654 mLocked = false;
655 }
656 return err;
657}
658
659bool C2AllocationGralloc::equals(const std::shared_ptr<const C2GraphicAllocation> &other) const {
660 return other && other->handle() == handle();
661}
662
663/* ===================================== GRALLOC ALLOCATOR ==================================== */
664class C2AllocatorGralloc::Impl {
665public:
666 Impl(id_t id, bool bufferQueue);
667
668 id_t getId() const {
669 return mTraits->id;
670 }
671
672 C2String getName() const {
673 return mTraits->name;
674 }
675
676 std::shared_ptr<const C2Allocator::Traits> getTraits() const {
677 return mTraits;
678 }
679
680 c2_status_t newGraphicAllocation(
681 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
682 std::shared_ptr<C2GraphicAllocation> *allocation);
683
684 c2_status_t priorGraphicAllocation(
685 const C2Handle *handle,
686 std::shared_ptr<C2GraphicAllocation> *allocation);
687
688 c2_status_t status() const { return mInit; }
689
690private:
691 std::shared_ptr<C2Allocator::Traits> mTraits;
692 c2_status_t mInit;
693 sp<IAllocator> mAllocator;
694 sp<IMapper> mMapper;
695 const bool mBufferQueue;
696};
697
698void _UnwrapNativeCodec2GrallocMetadata(
699 const C2Handle *const handle,
700 uint32_t *width, uint32_t *height, uint32_t *format,uint64_t *usage, uint32_t *stride,
701 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
702 (void)C2HandleGralloc::Import(handle, width, height, format, usage, stride,
703 generation, igbp_id, igbp_slot);
704}
705
706C2AllocatorGralloc::Impl::Impl(id_t id, bool bufferQueue)
707 : mInit(C2_OK), mBufferQueue(bufferQueue) {
708 // TODO: get this from allocator
709 C2MemoryUsage minUsage = { 0, 0 }, maxUsage = { ~(uint64_t)0, ~(uint64_t)0 };
710 Traits traits = { "android.allocator.gralloc", id, C2Allocator::GRAPHIC, minUsage, maxUsage };
711 mTraits = std::make_shared<C2Allocator::Traits>(traits);
712
713 // gralloc allocator is a singleton, so all objects share a global service
714 mAllocator = IAllocator::getService();
715 mMapper = IMapper::getService();
716 if (mAllocator == nullptr || mMapper == nullptr) {
717 mInit = C2_CORRUPTED;
718 }
719}
720
721c2_status_t C2AllocatorGralloc::Impl::newGraphicAllocation(
722 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
723 std::shared_ptr<C2GraphicAllocation> *allocation) {
724 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
725 ALOGV("allocating buffer with usage %#llx => %#llx",
726 (long long)usage.expected, (long long)grallocUsage);
727
728 BufferDescriptorInfo info = {
729 {
730 width,
731 height,
732 1u, // layerCount
733 (PixelFormat)format,
734 grallocUsage,
735 },
736 0u, // stride placeholder
737 };
738 c2_status_t err = C2_OK;
739 BufferDescriptor desc;
740 mMapper->createDescriptor(
741 info.mapperInfo, [&err, &desc](const auto &maperr, const auto &descriptor) {
742 err = maperr2error(maperr);
743 if (err == C2_OK) {
744 desc = descriptor;
745 }
746 });
747 if (err != C2_OK) {
748 return err;
749 }
750
751 // IAllocator shares IMapper error codes.
752 hidl_handle buffer;
753 mAllocator->allocate(
754 desc,
755 1u,
756 [&err, &buffer, &info](const auto &maperr, const auto &stride, auto &buffers) {
757 err = maperr2error(maperr);
758 if (err != C2_OK) {
759 return;
760 }
761 if (buffers.size() != 1u) {
762 err = C2_CORRUPTED;
763 return;
764 }
765 info.stride = stride;
Sungtak Leea4d13be2019-01-23 15:24:46 -0800766 buffer = buffers[0];
Pawin Vongmasa36653902018-11-15 00:10:25 -0800767 });
768 if (err != C2_OK) {
769 return err;
770 }
771
772
773 allocation->reset(new C2AllocationGralloc(
774 info, mMapper, buffer,
Sungtak Leea4d13be2019-01-23 15:24:46 -0800775 C2HandleGralloc::WrapAndMoveNativeHandle(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800776 buffer.getNativeHandle(),
777 info.mapperInfo.width, info.mapperInfo.height,
778 (uint32_t)info.mapperInfo.format, info.mapperInfo.usage, info.stride,
779 0, 0, mBufferQueue ? ~0 : 0),
780 mTraits->id));
781 return C2_OK;
782}
783
784c2_status_t C2AllocatorGralloc::Impl::priorGraphicAllocation(
785 const C2Handle *handle,
786 std::shared_ptr<C2GraphicAllocation> *allocation) {
787 BufferDescriptorInfo info;
788 info.mapperInfo.layerCount = 1u;
789 uint32_t generation;
790 uint64_t igbp_id;
791 uint32_t igbp_slot;
792 const C2HandleGralloc *grallocHandle = C2HandleGralloc::Import(
793 handle,
794 &info.mapperInfo.width, &info.mapperInfo.height,
795 (uint32_t *)&info.mapperInfo.format, (uint64_t *)&info.mapperInfo.usage, &info.stride,
796 &generation, &igbp_id, &igbp_slot);
797 if (grallocHandle == nullptr) {
798 return C2_BAD_VALUE;
799 }
800
801 hidl_handle hidlHandle;
802 hidlHandle.setTo(C2HandleGralloc::UnwrapNativeHandle(grallocHandle), true);
803
804 allocation->reset(new C2AllocationGralloc(info, mMapper, hidlHandle, grallocHandle, mTraits->id));
805 return C2_OK;
806}
807
808C2AllocatorGralloc::C2AllocatorGralloc(id_t id, bool bufferQueue)
809 : mImpl(new Impl(id, bufferQueue)) {}
810
811C2AllocatorGralloc::~C2AllocatorGralloc() { delete mImpl; }
812
813C2Allocator::id_t C2AllocatorGralloc::getId() const {
814 return mImpl->getId();
815}
816
817C2String C2AllocatorGralloc::getName() const {
818 return mImpl->getName();
819}
820
821std::shared_ptr<const C2Allocator::Traits> C2AllocatorGralloc::getTraits() const {
822 return mImpl->getTraits();
823}
824
825c2_status_t C2AllocatorGralloc::newGraphicAllocation(
826 uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
827 std::shared_ptr<C2GraphicAllocation> *allocation) {
828 return mImpl->newGraphicAllocation(width, height, format, usage, allocation);
829}
830
831c2_status_t C2AllocatorGralloc::priorGraphicAllocation(
832 const C2Handle *handle,
833 std::shared_ptr<C2GraphicAllocation> *allocation) {
834 return mImpl->priorGraphicAllocation(handle, allocation);
835}
836
837c2_status_t C2AllocatorGralloc::status() const {
838 return mImpl->status();
839}
840
841bool C2AllocatorGralloc::isValid(const C2Handle* const o) {
842 return C2HandleGralloc::isValid(o);
843}
844
845} // namespace android