blob: b5200a5c2d2358f196aa169c0cef7fbf5feaa136 [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
Marissa Wall2a24a302019-11-25 11:19:18 -080021#include <mutex>
22
Praveen Chavan34493f12021-02-18 00:51:50 -080023#include <aidl/android/hardware/graphics/common/PlaneLayoutComponentType.h>
Marissa Wall2a24a302019-11-25 11:19:18 -080024#include <android/hardware/graphics/common/1.2/types.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080025#include <cutils/native_handle.h>
Praveen Chavan34493f12021-02-18 00:51:50 -080026#include <gralloctypes/Gralloc4.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080027#include <hardware/gralloc.h>
Marissa Wall2a24a302019-11-25 11:19:18 -080028#include <ui/GraphicBufferAllocator.h>
29#include <ui/GraphicBufferMapper.h>
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +090030#include <ui/Rect.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031
32#include <C2AllocatorGralloc.h>
33#include <C2Buffer.h>
Praveen Chavan34493f12021-02-18 00:51:50 -080034#include <C2Debug.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080035#include <C2PlatformSupport.h>
36
Marissa Wall2a24a302019-11-25 11:19:18 -080037using ::android::hardware::hidl_handle;
38using PixelFormat4 = ::android::hardware::graphics::common::V1_2::PixelFormat;
39
Pawin Vongmasa36653902018-11-15 00:10:25 -080040namespace android {
41
Pawin Vongmasad032f2d2019-05-15 08:42:44 -070042namespace /* unnamed */ {
Pawin Vongmasa36653902018-11-15 00:10:25 -080043 enum : uint64_t {
44 /**
45 * Usage mask that is passed through from gralloc to Codec 2.0 usage.
46 */
47 PASSTHROUGH_USAGE_MASK =
Charlie Chen49d3fe72021-03-25 20:02:37 +080048 ~static_cast<uint64_t>(GRALLOC_USAGE_SW_READ_MASK |
49 GRALLOC_USAGE_SW_WRITE_MASK |
50 GRALLOC_USAGE_PROTECTED)
Pawin Vongmasa36653902018-11-15 00:10:25 -080051 };
52
53 // verify that passthrough mask is within the platform mask
54 static_assert((~C2MemoryUsage::PLATFORM_MASK & PASSTHROUGH_USAGE_MASK) == 0, "");
Pawin Vongmasad032f2d2019-05-15 08:42:44 -070055} // unnamed
Pawin Vongmasa36653902018-11-15 00:10:25 -080056
57C2MemoryUsage C2AndroidMemoryUsage::FromGrallocUsage(uint64_t usage) {
58 // gralloc does not support WRITE_PROTECTED
59 return C2MemoryUsage(
60 ((usage & GRALLOC_USAGE_SW_READ_MASK) ? C2MemoryUsage::CPU_READ : 0) |
61 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ? C2MemoryUsage::CPU_WRITE : 0) |
62 ((usage & GRALLOC_USAGE_PROTECTED) ? C2MemoryUsage::READ_PROTECTED : 0) |
63 (usage & PASSTHROUGH_USAGE_MASK));
64}
65
66uint64_t C2AndroidMemoryUsage::asGrallocUsage() const {
67 // gralloc does not support WRITE_PROTECTED
68 return (((expected & C2MemoryUsage::CPU_READ) ? GRALLOC_USAGE_SW_READ_OFTEN : 0) |
69 ((expected & C2MemoryUsage::CPU_WRITE) ? GRALLOC_USAGE_SW_WRITE_OFTEN : 0) |
70 ((expected & C2MemoryUsage::READ_PROTECTED) ? GRALLOC_USAGE_PROTECTED : 0) |
71 (expected & PASSTHROUGH_USAGE_MASK));
72}
73
Pawin Vongmasad032f2d2019-05-15 08:42:44 -070074namespace /* unnamed */ {
75
Pawin Vongmasa36653902018-11-15 00:10:25 -080076/* ===================================== GRALLOC ALLOCATION ==================================== */
Pawin Vongmasa36653902018-11-15 00:10:25 -080077bool native_handle_is_invalid(const native_handle_t *const handle) {
78 // perform basic validation of a native handle
79 if (handle == nullptr) {
80 // null handle is considered valid
81 return false;
82 }
83 return ((size_t)handle->version != sizeof(native_handle_t) ||
84 handle->numFds < 0 ||
85 handle->numInts < 0 ||
86 // for sanity assume handles must occupy less memory than INT_MAX bytes
87 handle->numFds > int((INT_MAX - handle->version) / sizeof(int)) - handle->numInts);
88}
89
90class C2HandleGralloc : public C2Handle {
91private:
92 struct ExtraData {
93 uint32_t width;
94 uint32_t height;
95 uint32_t format;
96 uint32_t usage_lo;
97 uint32_t usage_hi;
98 uint32_t stride;
99 uint32_t generation;
100 uint32_t igbp_id_lo;
101 uint32_t igbp_id_hi;
102 uint32_t igbp_slot;
103 uint32_t magic;
104 };
105
106 enum {
107 NUM_INTS = sizeof(ExtraData) / sizeof(int),
108 };
109 const static uint32_t MAGIC = '\xc2gr\x00';
110
111 static
John Stultz653ddd12020-09-19 05:26:24 +0000112 const ExtraData* GetExtraData(const C2Handle *const handle) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800113 if (handle == nullptr
114 || native_handle_is_invalid(handle)
115 || handle->numInts < NUM_INTS) {
116 return nullptr;
117 }
118 return reinterpret_cast<const ExtraData*>(
119 &handle->data[handle->numFds + handle->numInts - NUM_INTS]);
120 }
121
122 static
John Stultz653ddd12020-09-19 05:26:24 +0000123 ExtraData *GetExtraData(C2Handle *const handle) {
124 return const_cast<ExtraData *>(GetExtraData(const_cast<const C2Handle *const>(handle)));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800125 }
126
127public:
128 void getIgbpData(uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) const {
John Stultz653ddd12020-09-19 05:26:24 +0000129 const ExtraData *ed = GetExtraData(this);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800130 *generation = ed->generation;
131 *igbp_id = unsigned(ed->igbp_id_lo) | uint64_t(unsigned(ed->igbp_id_hi)) << 32;
132 *igbp_slot = ed->igbp_slot;
133 }
134
John Stultz653ddd12020-09-19 05:26:24 +0000135 static bool IsValid(const C2Handle *const o) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800136 if (o == nullptr) { // null handle is always valid
137 return true;
138 }
John Stultz653ddd12020-09-19 05:26:24 +0000139 const ExtraData *xd = GetExtraData(o);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800140 // we cannot validate width/height/format/usage without accessing gralloc driver
141 return xd != nullptr && xd->magic == MAGIC;
142 }
143
Sungtak Leea4d13be2019-01-23 15:24:46 -0800144 static C2HandleGralloc* WrapAndMoveNativeHandle(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800145 const native_handle_t *const handle,
146 uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
147 uint32_t stride, uint32_t generation, uint64_t igbp_id = 0, uint32_t igbp_slot = 0) {
148 //CHECK(handle != nullptr);
149 if (native_handle_is_invalid(handle) ||
150 handle->numInts > int((INT_MAX - handle->version) / sizeof(int)) - NUM_INTS - handle->numFds) {
151 return nullptr;
152 }
153 ExtraData xd = {
154 width, height, format, uint32_t(usage & 0xFFFFFFFF), uint32_t(usage >> 32),
155 stride, generation, uint32_t(igbp_id & 0xFFFFFFFF), uint32_t(igbp_id >> 32),
156 igbp_slot, MAGIC
157 };
158 native_handle_t *res = native_handle_create(handle->numFds, handle->numInts + NUM_INTS);
159 if (res != nullptr) {
160 memcpy(&res->data, &handle->data, sizeof(int) * (handle->numFds + handle->numInts));
John Stultz653ddd12020-09-19 05:26:24 +0000161 *GetExtraData(res) = xd;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800162 }
163 return reinterpret_cast<C2HandleGralloc *>(res);
164 }
165
Sungtak Leea4d13be2019-01-23 15:24:46 -0800166 static C2HandleGralloc* WrapNativeHandle(
167 const native_handle_t *const handle,
168 uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
169 uint32_t stride, uint32_t generation, uint64_t igbp_id = 0, uint32_t igbp_slot = 0) {
170 if (handle == nullptr) {
171 return nullptr;
172 }
173 native_handle_t *clone = native_handle_clone(handle);
174 if (clone == nullptr) {
175 return nullptr;
176 }
177 C2HandleGralloc *res = WrapAndMoveNativeHandle(
178 clone, width, height, format, usage, stride, generation, igbp_id, igbp_slot);
179 if (res == nullptr) {
180 native_handle_close(clone);
181 }
182 native_handle_delete(clone);
183 return res;
184 }
185
Sungtak Lee08515812019-06-05 11:16:32 -0700186 static bool MigrateNativeHandle(
187 native_handle_t *handle,
188 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
John Stultz653ddd12020-09-19 05:26:24 +0000189 if (handle == nullptr || !IsValid(handle)) {
Sungtak Lee08515812019-06-05 11:16:32 -0700190 return false;
191 }
John Stultz653ddd12020-09-19 05:26:24 +0000192 ExtraData *ed = GetExtraData(handle);
Sungtak Lee08515812019-06-05 11:16:32 -0700193 if (!ed) return false;
194 ed->generation = generation;
195 ed->igbp_id_lo = uint32_t(igbp_id & 0xFFFFFFFF);
196 ed->igbp_id_hi = uint32_t(igbp_id >> 32);
197 ed->igbp_slot = igbp_slot;
198 return true;
199 }
200
201
Pawin Vongmasa36653902018-11-15 00:10:25 -0800202 static native_handle_t* UnwrapNativeHandle(
203 const C2Handle *const handle) {
John Stultz653ddd12020-09-19 05:26:24 +0000204 const ExtraData *xd = GetExtraData(handle);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800205 if (xd == nullptr || xd->magic != MAGIC) {
206 return nullptr;
207 }
208 native_handle_t *res = native_handle_create(handle->numFds, handle->numInts - NUM_INTS);
209 if (res != nullptr) {
210 memcpy(&res->data, &handle->data, sizeof(int) * (res->numFds + res->numInts));
211 }
212 return res;
213 }
214
Pawin Vongmasa36653902018-11-15 00:10:25 -0800215 static const C2HandleGralloc* Import(
216 const C2Handle *const handle,
217 uint32_t *width, uint32_t *height, uint32_t *format,
218 uint64_t *usage, uint32_t *stride,
219 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
John Stultz653ddd12020-09-19 05:26:24 +0000220 const ExtraData *xd = GetExtraData(handle);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800221 if (xd == nullptr) {
222 return nullptr;
223 }
224 *width = xd->width;
225 *height = xd->height;
226 *format = xd->format;
227 *usage = xd->usage_lo | (uint64_t(xd->usage_hi) << 32);
228 *stride = xd->stride;
229 *generation = xd->generation;
230 *igbp_id = xd->igbp_id_lo | (uint64_t(xd->igbp_id_hi) << 32);
231 *igbp_slot = xd->igbp_slot;
232 return reinterpret_cast<const C2HandleGralloc *>(handle);
233 }
234};
235
Praveen Chavan34493f12021-02-18 00:51:50 -0800236static
237c2_status_t Gralloc4Mapper_lock(native_handle_t *handle, uint64_t usage, const Rect& bounds,
238 C2PlanarLayout *layout, uint8_t **addr) {
239 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
240
241 std::vector<ui::PlaneLayout> planes;
242 // this method is only supported on Gralloc 4 or later
243 status_t err = mapper.getPlaneLayouts(handle, &planes);
244 if (err != NO_ERROR || planes.empty()) {
245 return C2_CANNOT_DO;
246 }
247
248 uint8_t *pointer = nullptr;
249 err = mapper.lock(handle, usage, bounds, (void **)&pointer, nullptr, nullptr);
250 if (err != NO_ERROR || pointer == nullptr) {
251 return C2_CORRUPTED;
252 }
253
254 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
255 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
256
257 layout->type = C2PlanarLayout::TYPE_YUV;
258 layout->numPlanes = 0;
259 layout->rootPlanes = 0;
260
261 for (const ui::PlaneLayout &plane : planes) {
262 layout->rootPlanes++;
263 uint32_t lastOffsetInBits = 0;
264 uint32_t rootIx = 0;
265
266 for (const PlaneLayoutComponent &component : plane.components) {
267 if (!gralloc4::isStandardPlaneLayoutComponentType(component.type)) {
268 return C2_CANNOT_DO;
269 }
270
271 uint32_t rightShiftBits = component.offsetInBits - lastOffsetInBits;
272 uint32_t allocatedDepthInBits = component.sizeInBits + rightShiftBits;
273 C2PlanarLayout::plane_index_t planeId;
274 C2PlaneInfo::channel_t channel;
275
276 switch (static_cast<PlaneLayoutComponentType>(component.type.value)) {
277 case PlaneLayoutComponentType::Y:
278 planeId = C2PlanarLayout::PLANE_Y;
279 channel = C2PlaneInfo::CHANNEL_Y;
280 break;
281 case PlaneLayoutComponentType::CB:
282 planeId = C2PlanarLayout::PLANE_U;
283 channel = C2PlaneInfo::CHANNEL_CB;
284 break;
285 case PlaneLayoutComponentType::CR:
286 planeId = C2PlanarLayout::PLANE_V;
287 channel = C2PlaneInfo::CHANNEL_CR;
288 break;
289 default:
290 return C2_CORRUPTED;
291 }
292
293 addr[planeId] = pointer + plane.offsetInBytes + (component.offsetInBits / 8);
294 layout->planes[planeId] = {
295 channel, // channel
296 static_cast<int32_t>(plane.sampleIncrementInBits / 8), // colInc
297 static_cast<int32_t>(plane.strideInBytes), // rowInc
298 static_cast<uint32_t>(plane.horizontalSubsampling), // mColSampling
299 static_cast<uint32_t>(plane.verticalSubsampling), // mRowSampling
300 allocatedDepthInBits, // allocatedDepth (bits)
301 static_cast<uint32_t>(component.sizeInBits), // bitDepth (bits)
302 rightShiftBits, // rightShift (bits)
303 C2PlaneInfo::NATIVE, // endianness
304 rootIx, // rootIx
305 static_cast<uint32_t>(component.offsetInBits / 8), // offset (bytes)
306 };
307
308 layout->numPlanes++;
309 lastOffsetInBits = component.offsetInBits + component.sizeInBits;
310 rootIx++;
311 }
312 }
313 return C2_OK;
314}
315
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700316} // unnamed namespace
317
Praveen Chavan34493f12021-02-18 00:51:50 -0800318
Pawin Vongmasa36653902018-11-15 00:10:25 -0800319native_handle_t *UnwrapNativeCodec2GrallocHandle(const C2Handle *const handle) {
320 return C2HandleGralloc::UnwrapNativeHandle(handle);
321}
322
Pawin Vongmasa36653902018-11-15 00:10:25 -0800323C2Handle *WrapNativeCodec2GrallocHandle(
324 const native_handle_t *const handle,
325 uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
326 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
327 return C2HandleGralloc::WrapNativeHandle(handle, width, height, format, usage, stride,
328 generation, igbp_id, igbp_slot);
329}
330
Sungtak Lee08515812019-06-05 11:16:32 -0700331bool MigrateNativeCodec2GrallocHandle(
332 native_handle_t *handle,
333 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
334 return C2HandleGralloc::MigrateNativeHandle(handle, generation, igbp_id, igbp_slot);
335}
336
337
Pawin Vongmasa36653902018-11-15 00:10:25 -0800338class C2AllocationGralloc : public C2GraphicAllocation {
339public:
340 virtual ~C2AllocationGralloc() override;
341
342 virtual c2_status_t map(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900343 C2Rect c2Rect, C2MemoryUsage usage, C2Fence *fence,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800344 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) override;
345 virtual c2_status_t unmap(
346 uint8_t **addr /* nonnull */, C2Rect rect, C2Fence *fence /* nullable */) override;
347 virtual C2Allocator::id_t getAllocatorId() const override { return mAllocatorId; }
348 virtual const C2Handle *handle() const override { return mLockedHandle ? : mHandle; }
349 virtual bool equals(const std::shared_ptr<const C2GraphicAllocation> &other) const override;
350
351 // internal methods
352 // |handle| will be moved.
Marissa Wall2a24a302019-11-25 11:19:18 -0800353
Pawin Vongmasa36653902018-11-15 00:10:25 -0800354 C2AllocationGralloc(
Marissa Wall2a24a302019-11-25 11:19:18 -0800355 uint32_t width, uint32_t height,
356 uint32_t format, uint32_t layerCount,
357 uint64_t grallocUsage, uint32_t stride,
Marissa Wall8806edc2019-06-21 09:50:47 -0700358 hidl_handle &hidlHandle,
359 const C2HandleGralloc *const handle,
360 C2Allocator::id_t allocatorId);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800361 int dup() const;
362 c2_status_t status() const;
363
364private:
Marissa Wall2a24a302019-11-25 11:19:18 -0800365 const uint32_t mWidth;
366 const uint32_t mHeight;
367 const uint32_t mFormat;
368 const uint32_t mLayerCount;
369 const uint64_t mGrallocUsage;
370 const uint32_t mStride;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800371 const hidl_handle mHidlHandle;
372 const C2HandleGralloc *mHandle;
373 buffer_handle_t mBuffer;
374 const C2HandleGralloc *mLockedHandle;
375 bool mLocked;
376 C2Allocator::id_t mAllocatorId;
377 std::mutex mMappedLock;
378};
379
380C2AllocationGralloc::C2AllocationGralloc(
Marissa Wall2a24a302019-11-25 11:19:18 -0800381 uint32_t width, uint32_t height,
382 uint32_t format, uint32_t layerCount,
383 uint64_t grallocUsage, uint32_t stride,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800384 hidl_handle &hidlHandle,
385 const C2HandleGralloc *const handle,
386 C2Allocator::id_t allocatorId)
Marissa Wall2a24a302019-11-25 11:19:18 -0800387 : C2GraphicAllocation(width, height),
388 mWidth(width),
389 mHeight(height),
390 mFormat(format),
391 mLayerCount(layerCount),
392 mGrallocUsage(grallocUsage),
393 mStride(stride),
Marissa Wall8806edc2019-06-21 09:50:47 -0700394 mHidlHandle(std::move(hidlHandle)),
395 mHandle(handle),
396 mBuffer(nullptr),
397 mLockedHandle(nullptr),
398 mLocked(false),
399 mAllocatorId(allocatorId) {
400}
401
Pawin Vongmasa36653902018-11-15 00:10:25 -0800402C2AllocationGralloc::~C2AllocationGralloc() {
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800403 if (mBuffer && mLocked) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800404 // implementation ignores addresss and rect
405 uint8_t* addr[C2PlanarLayout::MAX_NUM_PLANES] = {};
406 unmap(addr, C2Rect(), nullptr);
407 }
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800408 if (mBuffer) {
Marissa Wall2a24a302019-11-25 11:19:18 -0800409 status_t err = GraphicBufferMapper::get().freeBuffer(mBuffer);
410 if (err) {
411 ALOGE("failed transaction: freeBuffer");
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700412 }
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800413 }
414 if (mHandle) {
415 native_handle_delete(
416 const_cast<native_handle_t *>(reinterpret_cast<const native_handle_t *>(mHandle)));
417 }
Sungtak Lee2729dcf2019-01-18 13:15:07 -0800418 if (mLockedHandle) {
419 native_handle_delete(
420 const_cast<native_handle_t *>(
421 reinterpret_cast<const native_handle_t *>(mLockedHandle)));
422 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800423}
424
425c2_status_t C2AllocationGralloc::map(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900426 C2Rect c2Rect, C2MemoryUsage usage, C2Fence *fence,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800427 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) {
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900428 const Rect rect{(int32_t)c2Rect.left, (int32_t)c2Rect.top,
429 (int32_t)(c2Rect.left + c2Rect.width) /* right */,
430 (int32_t)(c2Rect.top + c2Rect.height) /* bottom */};
431
Pawin Vongmasa36653902018-11-15 00:10:25 -0800432 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
433 ALOGV("mapping buffer with usage %#llx => %#llx",
434 (long long)usage.expected, (long long)grallocUsage);
435
436 // TODO
Marissa Wall2a24a302019-11-25 11:19:18 -0800437 (void)fence;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800438
439 std::lock_guard<std::mutex> lock(mMappedLock);
440 if (mBuffer && mLocked) {
441 ALOGD("already mapped");
442 return C2_DUPLICATE;
443 }
444 if (!layout || !addr) {
445 ALOGD("wrong param");
446 return C2_BAD_VALUE;
447 }
448
Pawin Vongmasa36653902018-11-15 00:10:25 -0800449 if (!mBuffer) {
Marissa Wall2a24a302019-11-25 11:19:18 -0800450 status_t err = GraphicBufferMapper::get().importBuffer(
451 mHidlHandle.getNativeHandle(), mWidth, mHeight, mLayerCount,
452 mFormat, mGrallocUsage, mStride, &mBuffer);
453 if (err) {
454 ALOGE("failed transaction: importBuffer");
455 return C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800456 }
457 if (mBuffer == nullptr) {
458 ALOGD("importBuffer returned null buffer");
459 return C2_CORRUPTED;
460 }
461 uint32_t generation = 0;
462 uint64_t igbp_id = 0;
463 uint32_t igbp_slot = 0;
464 if (mHandle) {
465 mHandle->getIgbpData(&generation, &igbp_id, &igbp_slot);
466 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800467
Marissa Wall2a24a302019-11-25 11:19:18 -0800468 mLockedHandle = C2HandleGralloc::WrapAndMoveNativeHandle(
469 mBuffer, mWidth, mHeight, mFormat, mGrallocUsage,
470 mStride, generation, igbp_id, igbp_slot);
Marissa Wall8806edc2019-06-21 09:50:47 -0700471 }
Praveen Chavan34493f12021-02-18 00:51:50 -0800472
473 // 'NATIVE' on Android means LITTLE_ENDIAN
474 constexpr C2PlaneInfo::endianness_t kEndianness = C2PlaneInfo::NATIVE;
475
Marissa Wall2a24a302019-11-25 11:19:18 -0800476 switch (mFormat) {
477 case static_cast<uint32_t>(PixelFormat4::RGBA_1010102): {
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700478 // TRICKY: this is used for media as YUV444 in the case when it is queued directly to a
479 // Surface. In all other cases it is RGBA. We don't know which case it is here, so
480 // default to YUV for now.
481 void *pointer = nullptr;
Marissa Wall2a24a302019-11-25 11:19:18 -0800482 // TODO: fence
483 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900484 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
Marissa Wall2a24a302019-11-25 11:19:18 -0800485 if (err) {
486 ALOGE("failed transaction: lock(RGBA_1010102)");
487 return C2_CORRUPTED;
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700488 }
489 // treat as 32-bit values
490 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)pointer;
491 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)pointer;
492 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)pointer;
493 addr[C2PlanarLayout::PLANE_A] = (uint8_t *)pointer;
494 layout->type = C2PlanarLayout::TYPE_YUVA;
495 layout->numPlanes = 4;
496 layout->rootPlanes = 1;
497 layout->planes[C2PlanarLayout::PLANE_Y] = {
498 C2PlaneInfo::CHANNEL_Y, // channel
499 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800500 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700501 1, // mColSampling
502 1, // mRowSampling
503 32, // allocatedDepth
504 10, // bitDepth
505 10, // rightShift
506 C2PlaneInfo::LITTLE_END, // endianness
507 C2PlanarLayout::PLANE_Y, // rootIx
508 0, // offset
509 };
510 layout->planes[C2PlanarLayout::PLANE_U] = {
511 C2PlaneInfo::CHANNEL_CB, // channel
512 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800513 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700514 1, // mColSampling
515 1, // mRowSampling
516 32, // allocatedDepth
517 10, // bitDepth
518 0, // rightShift
519 C2PlaneInfo::LITTLE_END, // endianness
520 C2PlanarLayout::PLANE_Y, // rootIx
521 0, // offset
522 };
523 layout->planes[C2PlanarLayout::PLANE_V] = {
524 C2PlaneInfo::CHANNEL_CR, // channel
525 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800526 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700527 1, // mColSampling
528 1, // mRowSampling
529 32, // allocatedDepth
530 10, // bitDepth
531 20, // rightShift
532 C2PlaneInfo::LITTLE_END, // endianness
533 C2PlanarLayout::PLANE_Y, // rootIx
534 0, // offset
535 };
536 layout->planes[C2PlanarLayout::PLANE_A] = {
537 C2PlaneInfo::CHANNEL_A, // channel
538 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800539 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700540 1, // mColSampling
541 1, // mRowSampling
542 32, // allocatedDepth
543 2, // bitDepth
544 30, // rightShift
545 C2PlaneInfo::LITTLE_END, // endianness
546 C2PlanarLayout::PLANE_Y, // rootIx
547 0, // offset
548 };
549 break;
550 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800551
Marissa Wall2a24a302019-11-25 11:19:18 -0800552 case static_cast<uint32_t>(PixelFormat4::RGBA_8888):
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700553 // TODO: alpha channel
554 // fall-through
Marissa Wall2a24a302019-11-25 11:19:18 -0800555 case static_cast<uint32_t>(PixelFormat4::RGBX_8888): {
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700556 void *pointer = nullptr;
Marissa Wall2a24a302019-11-25 11:19:18 -0800557 // TODO: fence
558 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900559 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &pointer);
Marissa Wall2a24a302019-11-25 11:19:18 -0800560 if (err) {
561 ALOGE("failed transaction: lock(RGBA_8888)");
562 return C2_CORRUPTED;
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700563 }
564 addr[C2PlanarLayout::PLANE_R] = (uint8_t *)pointer;
565 addr[C2PlanarLayout::PLANE_G] = (uint8_t *)pointer + 1;
566 addr[C2PlanarLayout::PLANE_B] = (uint8_t *)pointer + 2;
567 layout->type = C2PlanarLayout::TYPE_RGB;
568 layout->numPlanes = 3;
569 layout->rootPlanes = 1;
570 layout->planes[C2PlanarLayout::PLANE_R] = {
571 C2PlaneInfo::CHANNEL_R, // channel
572 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800573 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700574 1, // mColSampling
575 1, // mRowSampling
576 8, // allocatedDepth
577 8, // bitDepth
578 0, // rightShift
579 C2PlaneInfo::NATIVE, // endianness
580 C2PlanarLayout::PLANE_R, // rootIx
581 0, // offset
582 };
583 layout->planes[C2PlanarLayout::PLANE_G] = {
584 C2PlaneInfo::CHANNEL_G, // channel
585 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800586 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700587 1, // mColSampling
588 1, // mRowSampling
589 8, // allocatedDepth
590 8, // bitDepth
591 0, // rightShift
592 C2PlaneInfo::NATIVE, // endianness
593 C2PlanarLayout::PLANE_R, // rootIx
594 1, // offset
595 };
596 layout->planes[C2PlanarLayout::PLANE_B] = {
597 C2PlaneInfo::CHANNEL_B, // channel
598 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800599 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700600 1, // mColSampling
601 1, // mRowSampling
602 8, // allocatedDepth
603 8, // bitDepth
604 0, // rightShift
605 C2PlaneInfo::NATIVE, // endianness
606 C2PlanarLayout::PLANE_R, // rootIx
607 2, // offset
608 };
609 break;
610 }
611
David Stevens1cadc75d2020-04-03 10:50:51 +0900612 case static_cast<uint32_t>(PixelFormat4::BLOB): {
613 void *pointer = nullptr;
614 // TODO: fence
615 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900616 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &pointer);
David Stevens1cadc75d2020-04-03 10:50:51 +0900617 if (err) {
618 ALOGE("failed transaction: lock(BLOB)");
619 return C2_CORRUPTED;
620 }
621 *addr = (uint8_t *)pointer;
622 break;
623 }
624
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700625 case static_cast<uint32_t>(PixelFormat4::YCBCR_422_SP):
626 // fall-through
627 case static_cast<uint32_t>(PixelFormat4::YCRCB_420_SP):
628 // fall-through
629 case static_cast<uint32_t>(PixelFormat4::YCBCR_422_I):
630 // fall-through
Marissa Wall2a24a302019-11-25 11:19:18 -0800631 case static_cast<uint32_t>(PixelFormat4::YCBCR_420_888):
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700632 // fall-through
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700633 case static_cast<uint32_t>(PixelFormat4::YV12): {
Marissa Wall2a24a302019-11-25 11:19:18 -0800634 android_ycbcr ycbcrLayout;
635
636 status_t err = GraphicBufferMapper::get().lockYCbCr(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900637 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &ycbcrLayout);
Marissa Wall2a24a302019-11-25 11:19:18 -0800638 if (err) {
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800639 ALOGE("failed transaction: lockYCbCr (err=%d)", err);
640 return C2_CORRUPTED;
641 }
642 if (!ycbcrLayout.y || !ycbcrLayout.cb || !ycbcrLayout.cr
643 || ycbcrLayout.ystride == 0
644 || ycbcrLayout.cstride == 0
645 || ycbcrLayout.chroma_step == 0) {
646 ALOGE("invalid layout: lockYCbCr (y=%s cb=%s cr=%s "
647 "ystride=%zu cstride=%zu chroma_step=%zu)",
648 ycbcrLayout.y ? "(non-null)" : "(null)",
649 ycbcrLayout.cb ? "(non-null)" : "(null)",
650 ycbcrLayout.cr ? "(non-null)" : "(null)",
651 ycbcrLayout.ystride, ycbcrLayout.cstride, ycbcrLayout.chroma_step);
Marissa Wall469b90a2019-11-06 10:12:43 -0800652 return C2_CORRUPTED;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700653 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800654
Pawin Vongmasa36653902018-11-15 00:10:25 -0800655 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)ycbcrLayout.y;
656 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)ycbcrLayout.cb;
657 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)ycbcrLayout.cr;
658 layout->type = C2PlanarLayout::TYPE_YUV;
659 layout->numPlanes = 3;
660 layout->rootPlanes = 3;
661 layout->planes[C2PlanarLayout::PLANE_Y] = {
662 C2PlaneInfo::CHANNEL_Y, // channel
663 1, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800664 (int32_t)ycbcrLayout.ystride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800665 1, // mColSampling
666 1, // mRowSampling
667 8, // allocatedDepth
668 8, // bitDepth
669 0, // rightShift
670 C2PlaneInfo::NATIVE, // endianness
671 C2PlanarLayout::PLANE_Y, // rootIx
672 0, // offset
673 };
674 layout->planes[C2PlanarLayout::PLANE_U] = {
675 C2PlaneInfo::CHANNEL_CB, // channel
Marissa Wall2a24a302019-11-25 11:19:18 -0800676 (int32_t)ycbcrLayout.chroma_step, // colInc
677 (int32_t)ycbcrLayout.cstride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800678 2, // mColSampling
679 2, // mRowSampling
680 8, // allocatedDepth
681 8, // bitDepth
682 0, // rightShift
683 C2PlaneInfo::NATIVE, // endianness
684 C2PlanarLayout::PLANE_U, // rootIx
685 0, // offset
686 };
687 layout->planes[C2PlanarLayout::PLANE_V] = {
688 C2PlaneInfo::CHANNEL_CR, // channel
Marissa Wall2a24a302019-11-25 11:19:18 -0800689 (int32_t)ycbcrLayout.chroma_step, // colInc
690 (int32_t)ycbcrLayout.cstride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800691 2, // mColSampling
692 2, // mRowSampling
693 8, // allocatedDepth
694 8, // bitDepth
695 0, // rightShift
696 C2PlaneInfo::NATIVE, // endianness
697 C2PlanarLayout::PLANE_V, // rootIx
698 0, // offset
699 };
700 // handle interleaved formats
701 intptr_t uvOffset = addr[C2PlanarLayout::PLANE_V] - addr[C2PlanarLayout::PLANE_U];
Marissa Wall2a24a302019-11-25 11:19:18 -0800702 if (uvOffset > 0 && uvOffset < (intptr_t)ycbcrLayout.chroma_step) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800703 layout->rootPlanes = 2;
704 layout->planes[C2PlanarLayout::PLANE_V].rootIx = C2PlanarLayout::PLANE_U;
705 layout->planes[C2PlanarLayout::PLANE_V].offset = uvOffset;
Marissa Wall2a24a302019-11-25 11:19:18 -0800706 } else if (uvOffset < 0 && uvOffset > -(intptr_t)ycbcrLayout.chroma_step) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800707 layout->rootPlanes = 2;
708 layout->planes[C2PlanarLayout::PLANE_U].rootIx = C2PlanarLayout::PLANE_V;
709 layout->planes[C2PlanarLayout::PLANE_U].offset = -uvOffset;
710 }
711 break;
712 }
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700713
714 case static_cast<uint32_t>(PixelFormat4::YCBCR_P010): {
715 void *pointer = nullptr;
716 status_t err = GraphicBufferMapper::get().lock(
717 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
718 if (err) {
719 ALOGE("failed transaction: lock(YCBCR_P010)");
720 return C2_CORRUPTED;
721 }
722 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)pointer;
723 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)pointer + mStride * 2 * rect.height();
724 addr[C2PlanarLayout::PLANE_V] = addr[C2PlanarLayout::PLANE_U] + 2;
725 layout->type = C2PlanarLayout::TYPE_YUV;
726 layout->numPlanes = 3;
727 layout->rootPlanes = 2;
728 layout->planes[C2PlanarLayout::PLANE_Y] = {
729 C2PlaneInfo::CHANNEL_Y, // channel
730 2, // colInc
731 static_cast<int32_t>(2 * mStride), // rowInc
732 1, // mColSampling
733 1, // mRowSampling
734 16, // allocatedDepth
735 10, // bitDepth
736 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800737 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700738 C2PlanarLayout::PLANE_Y, // rootIx
739 0, // offset
740 };
741 layout->planes[C2PlanarLayout::PLANE_U] = {
742 C2PlaneInfo::CHANNEL_CB, // channel
743 4, // colInc
744 static_cast<int32_t>(2 * mStride), // rowInc
745 2, // mColSampling
746 2, // mRowSampling
747 16, // allocatedDepth
748 10, // bitDepth
749 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800750 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700751 C2PlanarLayout::PLANE_U, // rootIx
752 0, // offset
753 };
754 layout->planes[C2PlanarLayout::PLANE_V] = {
755 C2PlaneInfo::CHANNEL_CR, // channel
756 4, // colInc
757 static_cast<int32_t>(2 * mStride), // rowInc
758 2, // mColSampling
759 2, // mRowSampling
760 16, // allocatedDepth
761 10, // bitDepth
762 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800763 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700764 C2PlanarLayout::PLANE_U, // rootIx
765 2, // offset
766 };
767 break;
768 }
769
770 default: {
Praveen Chavan34493f12021-02-18 00:51:50 -0800771 // We don't know what it is, let's try to lock it with gralloc4
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700772 android_ycbcr ycbcrLayout;
Praveen Chavan34493f12021-02-18 00:51:50 -0800773 c2_status_t status = Gralloc4Mapper_lock(
774 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, layout, addr);
775 if (status == C2_OK) {
776 break;
777 }
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700778
Praveen Chavan34493f12021-02-18 00:51:50 -0800779 // fallback to lockYCbCr
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700780 status_t err = GraphicBufferMapper::get().lockYCbCr(
781 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &ycbcrLayout);
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800782 if (err == OK && ycbcrLayout.y && ycbcrLayout.cb && ycbcrLayout.cr
783 && ycbcrLayout.ystride > 0
784 && ycbcrLayout.cstride > 0
785 && ycbcrLayout.chroma_step > 0) {
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700786 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)ycbcrLayout.y;
787 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)ycbcrLayout.cb;
788 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)ycbcrLayout.cr;
789 layout->type = C2PlanarLayout::TYPE_YUV;
790 layout->numPlanes = 3;
791 layout->rootPlanes = 3;
792 layout->planes[C2PlanarLayout::PLANE_Y] = {
793 C2PlaneInfo::CHANNEL_Y, // channel
794 1, // colInc
795 (int32_t)ycbcrLayout.ystride, // rowInc
796 1, // mColSampling
797 1, // mRowSampling
798 8, // allocatedDepth
799 8, // bitDepth
800 0, // rightShift
801 C2PlaneInfo::NATIVE, // endianness
802 C2PlanarLayout::PLANE_Y, // rootIx
803 0, // offset
804 };
805 layout->planes[C2PlanarLayout::PLANE_U] = {
806 C2PlaneInfo::CHANNEL_CB, // channel
807 (int32_t)ycbcrLayout.chroma_step, // colInc
808 (int32_t)ycbcrLayout.cstride, // rowInc
809 2, // mColSampling
810 2, // mRowSampling
811 8, // allocatedDepth
812 8, // bitDepth
813 0, // rightShift
814 C2PlaneInfo::NATIVE, // endianness
815 C2PlanarLayout::PLANE_U, // rootIx
816 0, // offset
817 };
818 layout->planes[C2PlanarLayout::PLANE_V] = {
819 C2PlaneInfo::CHANNEL_CR, // channel
820 (int32_t)ycbcrLayout.chroma_step, // colInc
821 (int32_t)ycbcrLayout.cstride, // rowInc
822 2, // mColSampling
823 2, // mRowSampling
824 8, // allocatedDepth
825 8, // bitDepth
826 0, // rightShift
827 C2PlaneInfo::NATIVE, // endianness
828 C2PlanarLayout::PLANE_V, // rootIx
829 0, // offset
830 };
831 // handle interleaved formats
832 intptr_t uvOffset = addr[C2PlanarLayout::PLANE_V] - addr[C2PlanarLayout::PLANE_U];
833 if (uvOffset > 0 && uvOffset < (intptr_t)ycbcrLayout.chroma_step) {
834 layout->rootPlanes = 2;
835 layout->planes[C2PlanarLayout::PLANE_V].rootIx = C2PlanarLayout::PLANE_U;
836 layout->planes[C2PlanarLayout::PLANE_V].offset = uvOffset;
837 } else if (uvOffset < 0 && uvOffset > -(intptr_t)ycbcrLayout.chroma_step) {
838 layout->rootPlanes = 2;
839 layout->planes[C2PlanarLayout::PLANE_U].rootIx = C2PlanarLayout::PLANE_V;
840 layout->planes[C2PlanarLayout::PLANE_U].offset = -uvOffset;
841 }
842 break;
843 }
844
845 // We really don't know what this is; lock the buffer and pass it through ---
846 // the client may know how to interpret it.
Lajos Molnar8faebbc2021-06-30 17:07:42 -0700847
848 // unlock previous allocation if it was successful
849 if (err == OK) {
850 err = GraphicBufferMapper::get().unlock(mBuffer);
851 if (err) {
852 ALOGE("failed transaction: unlock");
853 return C2_CORRUPTED;
854 }
855 }
856
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700857 void *pointer = nullptr;
858 err = GraphicBufferMapper::get().lock(
859 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
860 if (err) {
861 ALOGE("failed transaction: lock(??? %x)", mFormat);
862 return C2_CORRUPTED;
863 }
864 addr[0] = (uint8_t *)pointer;
865 layout->type = C2PlanarLayout::TYPE_UNKNOWN;
866 layout->numPlanes = 1;
867 layout->rootPlanes = 1;
868 layout->planes[0] = {
869 // TODO: CHANNEL_UNKNOWN?
870 C2PlaneInfo::channel_t(0xFF), // channel
871 1, // colInc
872 int32_t(mStride), // rowInc
873 1, // mColSampling
874 1, // mRowSampling
875 8, // allocatedDepth
876 8, // bitDepth
877 0, // rightShift
878 C2PlaneInfo::NATIVE, // endianness
879 0, // rootIx
880 0, // offset
881 };
882 break;
883 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800884 }
885 mLocked = true;
886
887 return C2_OK;
888}
889
890c2_status_t C2AllocationGralloc::unmap(
891 uint8_t **addr, C2Rect rect, C2Fence *fence /* nullable */) {
892 // TODO: check addr and size, use fence
893 (void)addr;
894 (void)rect;
Marissa Wall2a24a302019-11-25 11:19:18 -0800895 (void)fence;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800896
897 std::lock_guard<std::mutex> lock(mMappedLock);
Marissa Wall2a24a302019-11-25 11:19:18 -0800898 // TODO: fence
899 status_t err = GraphicBufferMapper::get().unlock(mBuffer);
900 if (err) {
901 ALOGE("failed transaction: unlock");
902 return C2_CORRUPTED;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700903 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800904
905 mLocked = false;
906 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800907}
908
909bool C2AllocationGralloc::equals(const std::shared_ptr<const C2GraphicAllocation> &other) const {
910 return other && other->handle() == handle();
911}
912
913/* ===================================== GRALLOC ALLOCATOR ==================================== */
914class C2AllocatorGralloc::Impl {
915public:
916 Impl(id_t id, bool bufferQueue);
917
918 id_t getId() const {
919 return mTraits->id;
920 }
921
922 C2String getName() const {
923 return mTraits->name;
924 }
925
926 std::shared_ptr<const C2Allocator::Traits> getTraits() const {
927 return mTraits;
928 }
929
930 c2_status_t newGraphicAllocation(
931 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
932 std::shared_ptr<C2GraphicAllocation> *allocation);
933
934 c2_status_t priorGraphicAllocation(
935 const C2Handle *handle,
936 std::shared_ptr<C2GraphicAllocation> *allocation);
937
938 c2_status_t status() const { return mInit; }
939
940private:
941 std::shared_ptr<C2Allocator::Traits> mTraits;
942 c2_status_t mInit;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800943 const bool mBufferQueue;
944};
945
946void _UnwrapNativeCodec2GrallocMetadata(
947 const C2Handle *const handle,
948 uint32_t *width, uint32_t *height, uint32_t *format,uint64_t *usage, uint32_t *stride,
949 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
950 (void)C2HandleGralloc::Import(handle, width, height, format, usage, stride,
951 generation, igbp_id, igbp_slot);
952}
953
954C2AllocatorGralloc::Impl::Impl(id_t id, bool bufferQueue)
955 : mInit(C2_OK), mBufferQueue(bufferQueue) {
956 // TODO: get this from allocator
957 C2MemoryUsage minUsage = { 0, 0 }, maxUsage = { ~(uint64_t)0, ~(uint64_t)0 };
958 Traits traits = { "android.allocator.gralloc", id, C2Allocator::GRAPHIC, minUsage, maxUsage };
959 mTraits = std::make_shared<C2Allocator::Traits>(traits);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800960}
961
962c2_status_t C2AllocatorGralloc::Impl::newGraphicAllocation(
963 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
964 std::shared_ptr<C2GraphicAllocation> *allocation) {
965 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
966 ALOGV("allocating buffer with usage %#llx => %#llx",
967 (long long)usage.expected, (long long)grallocUsage);
968
Marissa Wall2a24a302019-11-25 11:19:18 -0800969 buffer_handle_t buffer;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700970
Marissa Wall2a24a302019-11-25 11:19:18 -0800971 uint32_t stride = 0;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700972
Marissa Wall2a24a302019-11-25 11:19:18 -0800973 status_t err = GraphicBufferAllocator::get().allocateRawHandle(width, height, format,
974 1u /* layer count */, grallocUsage, &buffer, &stride, "C2GrallocAllocation");
975 if (err) {
976 ALOGE("failed transaction: allocate");
977 return C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800978 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800979
980 hidl_handle hidlHandle;
981 hidlHandle.setTo(const_cast<native_handle_t*>(buffer), true);
982
983 allocation->reset(new C2AllocationGralloc(
984 width, height, format, 1u /* layer count */, grallocUsage, stride, hidlHandle,
985 C2HandleGralloc::WrapAndMoveNativeHandle(
986 hidlHandle, width, height,
987 format, grallocUsage, stride,
988 0, 0, mBufferQueue ? ~0 : 0),
989 mTraits->id));
990 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800991}
992
993c2_status_t C2AllocatorGralloc::Impl::priorGraphicAllocation(
994 const C2Handle *handle,
995 std::shared_ptr<C2GraphicAllocation> *allocation) {
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700996
Marissa Wall2a24a302019-11-25 11:19:18 -0800997 uint32_t generation;
998 uint64_t igbp_id;
999 uint32_t igbp_slot;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -07001000
Marissa Wall2a24a302019-11-25 11:19:18 -08001001 uint32_t width;
1002 uint32_t height;
1003 uint32_t format;
1004 uint32_t layerCount = 1;
1005 uint64_t grallocUsage;
1006 uint32_t stride;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -07001007
Marissa Wall2a24a302019-11-25 11:19:18 -08001008 const C2HandleGralloc *grallocHandle = C2HandleGralloc::Import(
1009 handle, &width, &height, &format, &grallocUsage, &stride,
1010 &generation, &igbp_id, &igbp_slot);
1011 if (grallocHandle == nullptr) {
1012 return C2_BAD_VALUE;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001013 }
Marissa Wall2a24a302019-11-25 11:19:18 -08001014
1015 hidl_handle hidlHandle;
1016 hidlHandle.setTo(C2HandleGralloc::UnwrapNativeHandle(grallocHandle), true);
1017
1018 allocation->reset(new C2AllocationGralloc(
1019 width, height, format, layerCount,
1020 grallocUsage, stride, hidlHandle, grallocHandle, mTraits->id));
1021 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001022}
1023
1024C2AllocatorGralloc::C2AllocatorGralloc(id_t id, bool bufferQueue)
1025 : mImpl(new Impl(id, bufferQueue)) {}
1026
1027C2AllocatorGralloc::~C2AllocatorGralloc() { delete mImpl; }
1028
1029C2Allocator::id_t C2AllocatorGralloc::getId() const {
1030 return mImpl->getId();
1031}
1032
1033C2String C2AllocatorGralloc::getName() const {
1034 return mImpl->getName();
1035}
1036
1037std::shared_ptr<const C2Allocator::Traits> C2AllocatorGralloc::getTraits() const {
1038 return mImpl->getTraits();
1039}
1040
1041c2_status_t C2AllocatorGralloc::newGraphicAllocation(
1042 uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
1043 std::shared_ptr<C2GraphicAllocation> *allocation) {
1044 return mImpl->newGraphicAllocation(width, height, format, usage, allocation);
1045}
1046
1047c2_status_t C2AllocatorGralloc::priorGraphicAllocation(
1048 const C2Handle *handle,
1049 std::shared_ptr<C2GraphicAllocation> *allocation) {
1050 return mImpl->priorGraphicAllocation(handle, allocation);
1051}
1052
1053c2_status_t C2AllocatorGralloc::status() const {
1054 return mImpl->status();
1055}
1056
John Stultz653ddd12020-09-19 05:26:24 +00001057// static
1058bool C2AllocatorGralloc::CheckHandle(const C2Handle* const o) {
1059 return C2HandleGralloc::IsValid(o);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001060}
1061
1062} // namespace android