blob: 2377fbb04dcfd5d5c1e94f715236f405f7f75934 [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;
Wonsik Kimf12bebc2022-03-24 16:25:03 -0700264 uint32_t rootIx = layout->numPlanes;
Praveen Chavan34493f12021-02-18 00:51:50 -0800265
266 for (const PlaneLayoutComponent &component : plane.components) {
267 if (!gralloc4::isStandardPlaneLayoutComponentType(component.type)) {
Praveen Chavan2c580d92022-02-24 12:23:46 -0800268 mapper.unlock(handle);
Praveen Chavan34493f12021-02-18 00:51:50 -0800269 return C2_CANNOT_DO;
270 }
271
272 uint32_t rightShiftBits = component.offsetInBits - lastOffsetInBits;
273 uint32_t allocatedDepthInBits = component.sizeInBits + rightShiftBits;
274 C2PlanarLayout::plane_index_t planeId;
275 C2PlaneInfo::channel_t channel;
276
277 switch (static_cast<PlaneLayoutComponentType>(component.type.value)) {
278 case PlaneLayoutComponentType::Y:
279 planeId = C2PlanarLayout::PLANE_Y;
280 channel = C2PlaneInfo::CHANNEL_Y;
281 break;
282 case PlaneLayoutComponentType::CB:
283 planeId = C2PlanarLayout::PLANE_U;
284 channel = C2PlaneInfo::CHANNEL_CB;
285 break;
286 case PlaneLayoutComponentType::CR:
287 planeId = C2PlanarLayout::PLANE_V;
288 channel = C2PlaneInfo::CHANNEL_CR;
289 break;
290 default:
Praveen Chavan2c580d92022-02-24 12:23:46 -0800291 mapper.unlock(handle);
Praveen Chavan34493f12021-02-18 00:51:50 -0800292 return C2_CORRUPTED;
293 }
294
295 addr[planeId] = pointer + plane.offsetInBytes + (component.offsetInBits / 8);
296 layout->planes[planeId] = {
297 channel, // channel
298 static_cast<int32_t>(plane.sampleIncrementInBits / 8), // colInc
299 static_cast<int32_t>(plane.strideInBytes), // rowInc
300 static_cast<uint32_t>(plane.horizontalSubsampling), // mColSampling
301 static_cast<uint32_t>(plane.verticalSubsampling), // mRowSampling
302 allocatedDepthInBits, // allocatedDepth (bits)
303 static_cast<uint32_t>(component.sizeInBits), // bitDepth (bits)
304 rightShiftBits, // rightShift (bits)
305 C2PlaneInfo::NATIVE, // endianness
306 rootIx, // rootIx
307 static_cast<uint32_t>(component.offsetInBits / 8), // offset (bytes)
308 };
309
310 layout->numPlanes++;
311 lastOffsetInBits = component.offsetInBits + component.sizeInBits;
Praveen Chavan34493f12021-02-18 00:51:50 -0800312 }
313 }
314 return C2_OK;
315}
316
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700317} // unnamed namespace
318
Praveen Chavan34493f12021-02-18 00:51:50 -0800319
Pawin Vongmasa36653902018-11-15 00:10:25 -0800320native_handle_t *UnwrapNativeCodec2GrallocHandle(const C2Handle *const handle) {
321 return C2HandleGralloc::UnwrapNativeHandle(handle);
322}
323
Pawin Vongmasa36653902018-11-15 00:10:25 -0800324C2Handle *WrapNativeCodec2GrallocHandle(
325 const native_handle_t *const handle,
326 uint32_t width, uint32_t height, uint32_t format, uint64_t usage, uint32_t stride,
327 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
328 return C2HandleGralloc::WrapNativeHandle(handle, width, height, format, usage, stride,
329 generation, igbp_id, igbp_slot);
330}
331
Sungtak Lee08515812019-06-05 11:16:32 -0700332bool MigrateNativeCodec2GrallocHandle(
333 native_handle_t *handle,
334 uint32_t generation, uint64_t igbp_id, uint32_t igbp_slot) {
335 return C2HandleGralloc::MigrateNativeHandle(handle, generation, igbp_id, igbp_slot);
336}
337
338
Pawin Vongmasa36653902018-11-15 00:10:25 -0800339class C2AllocationGralloc : public C2GraphicAllocation {
340public:
341 virtual ~C2AllocationGralloc() override;
342
343 virtual c2_status_t map(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900344 C2Rect c2Rect, C2MemoryUsage usage, C2Fence *fence,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800345 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) override;
346 virtual c2_status_t unmap(
347 uint8_t **addr /* nonnull */, C2Rect rect, C2Fence *fence /* nullable */) override;
348 virtual C2Allocator::id_t getAllocatorId() const override { return mAllocatorId; }
349 virtual const C2Handle *handle() const override { return mLockedHandle ? : mHandle; }
350 virtual bool equals(const std::shared_ptr<const C2GraphicAllocation> &other) const override;
351
352 // internal methods
353 // |handle| will be moved.
Marissa Wall2a24a302019-11-25 11:19:18 -0800354
Pawin Vongmasa36653902018-11-15 00:10:25 -0800355 C2AllocationGralloc(
Marissa Wall2a24a302019-11-25 11:19:18 -0800356 uint32_t width, uint32_t height,
357 uint32_t format, uint32_t layerCount,
358 uint64_t grallocUsage, uint32_t stride,
Marissa Wall8806edc2019-06-21 09:50:47 -0700359 hidl_handle &hidlHandle,
360 const C2HandleGralloc *const handle,
361 C2Allocator::id_t allocatorId);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800362 int dup() const;
363 c2_status_t status() const;
364
365private:
Marissa Wall2a24a302019-11-25 11:19:18 -0800366 const uint32_t mWidth;
367 const uint32_t mHeight;
368 const uint32_t mFormat;
369 const uint32_t mLayerCount;
370 const uint64_t mGrallocUsage;
371 const uint32_t mStride;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800372 const hidl_handle mHidlHandle;
373 const C2HandleGralloc *mHandle;
374 buffer_handle_t mBuffer;
375 const C2HandleGralloc *mLockedHandle;
376 bool mLocked;
377 C2Allocator::id_t mAllocatorId;
378 std::mutex mMappedLock;
379};
380
381C2AllocationGralloc::C2AllocationGralloc(
Marissa Wall2a24a302019-11-25 11:19:18 -0800382 uint32_t width, uint32_t height,
383 uint32_t format, uint32_t layerCount,
384 uint64_t grallocUsage, uint32_t stride,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800385 hidl_handle &hidlHandle,
386 const C2HandleGralloc *const handle,
387 C2Allocator::id_t allocatorId)
Marissa Wall2a24a302019-11-25 11:19:18 -0800388 : C2GraphicAllocation(width, height),
389 mWidth(width),
390 mHeight(height),
391 mFormat(format),
392 mLayerCount(layerCount),
393 mGrallocUsage(grallocUsage),
394 mStride(stride),
Marissa Wall8806edc2019-06-21 09:50:47 -0700395 mHidlHandle(std::move(hidlHandle)),
396 mHandle(handle),
397 mBuffer(nullptr),
398 mLockedHandle(nullptr),
399 mLocked(false),
400 mAllocatorId(allocatorId) {
401}
402
Pawin Vongmasa36653902018-11-15 00:10:25 -0800403C2AllocationGralloc::~C2AllocationGralloc() {
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800404 if (mBuffer && mLocked) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800405 // implementation ignores addresss and rect
406 uint8_t* addr[C2PlanarLayout::MAX_NUM_PLANES] = {};
407 unmap(addr, C2Rect(), nullptr);
408 }
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800409 if (mBuffer) {
Marissa Wall2a24a302019-11-25 11:19:18 -0800410 status_t err = GraphicBufferMapper::get().freeBuffer(mBuffer);
411 if (err) {
412 ALOGE("failed transaction: freeBuffer");
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700413 }
Yichi Chenfa94a3b2018-12-08 00:06:25 +0800414 }
415 if (mHandle) {
416 native_handle_delete(
417 const_cast<native_handle_t *>(reinterpret_cast<const native_handle_t *>(mHandle)));
418 }
Sungtak Lee2729dcf2019-01-18 13:15:07 -0800419 if (mLockedHandle) {
420 native_handle_delete(
421 const_cast<native_handle_t *>(
422 reinterpret_cast<const native_handle_t *>(mLockedHandle)));
423 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800424}
425
426c2_status_t C2AllocationGralloc::map(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900427 C2Rect c2Rect, C2MemoryUsage usage, C2Fence *fence,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800428 C2PlanarLayout *layout /* nonnull */, uint8_t **addr /* nonnull */) {
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900429 const Rect rect{(int32_t)c2Rect.left, (int32_t)c2Rect.top,
430 (int32_t)(c2Rect.left + c2Rect.width) /* right */,
431 (int32_t)(c2Rect.top + c2Rect.height) /* bottom */};
432
Pawin Vongmasa36653902018-11-15 00:10:25 -0800433 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
434 ALOGV("mapping buffer with usage %#llx => %#llx",
435 (long long)usage.expected, (long long)grallocUsage);
436
437 // TODO
Marissa Wall2a24a302019-11-25 11:19:18 -0800438 (void)fence;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800439
440 std::lock_guard<std::mutex> lock(mMappedLock);
441 if (mBuffer && mLocked) {
442 ALOGD("already mapped");
443 return C2_DUPLICATE;
444 }
445 if (!layout || !addr) {
446 ALOGD("wrong param");
447 return C2_BAD_VALUE;
448 }
449
Pawin Vongmasa36653902018-11-15 00:10:25 -0800450 if (!mBuffer) {
Marissa Wall2a24a302019-11-25 11:19:18 -0800451 status_t err = GraphicBufferMapper::get().importBuffer(
452 mHidlHandle.getNativeHandle(), mWidth, mHeight, mLayerCount,
453 mFormat, mGrallocUsage, mStride, &mBuffer);
454 if (err) {
455 ALOGE("failed transaction: importBuffer");
456 return C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800457 }
458 if (mBuffer == nullptr) {
459 ALOGD("importBuffer returned null buffer");
460 return C2_CORRUPTED;
461 }
462 uint32_t generation = 0;
463 uint64_t igbp_id = 0;
464 uint32_t igbp_slot = 0;
465 if (mHandle) {
466 mHandle->getIgbpData(&generation, &igbp_id, &igbp_slot);
467 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800468
Marissa Wall2a24a302019-11-25 11:19:18 -0800469 mLockedHandle = C2HandleGralloc::WrapAndMoveNativeHandle(
470 mBuffer, mWidth, mHeight, mFormat, mGrallocUsage,
471 mStride, generation, igbp_id, igbp_slot);
Marissa Wall8806edc2019-06-21 09:50:47 -0700472 }
Praveen Chavan34493f12021-02-18 00:51:50 -0800473
474 // 'NATIVE' on Android means LITTLE_ENDIAN
475 constexpr C2PlaneInfo::endianness_t kEndianness = C2PlaneInfo::NATIVE;
476
Marissa Wall2a24a302019-11-25 11:19:18 -0800477 switch (mFormat) {
478 case static_cast<uint32_t>(PixelFormat4::RGBA_1010102): {
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700479 // TRICKY: this is used for media as YUV444 in the case when it is queued directly to a
480 // Surface. In all other cases it is RGBA. We don't know which case it is here, so
481 // default to YUV for now.
482 void *pointer = nullptr;
Marissa Wall2a24a302019-11-25 11:19:18 -0800483 // TODO: fence
484 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900485 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
Marissa Wall2a24a302019-11-25 11:19:18 -0800486 if (err) {
487 ALOGE("failed transaction: lock(RGBA_1010102)");
488 return C2_CORRUPTED;
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700489 }
490 // treat as 32-bit values
491 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)pointer;
492 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)pointer;
493 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)pointer;
494 addr[C2PlanarLayout::PLANE_A] = (uint8_t *)pointer;
495 layout->type = C2PlanarLayout::TYPE_YUVA;
496 layout->numPlanes = 4;
497 layout->rootPlanes = 1;
498 layout->planes[C2PlanarLayout::PLANE_Y] = {
499 C2PlaneInfo::CHANNEL_Y, // channel
500 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800501 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700502 1, // mColSampling
503 1, // mRowSampling
504 32, // allocatedDepth
505 10, // bitDepth
506 10, // rightShift
507 C2PlaneInfo::LITTLE_END, // endianness
508 C2PlanarLayout::PLANE_Y, // rootIx
509 0, // offset
510 };
511 layout->planes[C2PlanarLayout::PLANE_U] = {
512 C2PlaneInfo::CHANNEL_CB, // channel
513 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800514 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700515 1, // mColSampling
516 1, // mRowSampling
517 32, // allocatedDepth
518 10, // bitDepth
519 0, // rightShift
520 C2PlaneInfo::LITTLE_END, // endianness
521 C2PlanarLayout::PLANE_Y, // rootIx
522 0, // offset
523 };
524 layout->planes[C2PlanarLayout::PLANE_V] = {
525 C2PlaneInfo::CHANNEL_CR, // channel
526 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800527 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700528 1, // mColSampling
529 1, // mRowSampling
530 32, // allocatedDepth
531 10, // bitDepth
532 20, // rightShift
533 C2PlaneInfo::LITTLE_END, // endianness
534 C2PlanarLayout::PLANE_Y, // rootIx
535 0, // offset
536 };
537 layout->planes[C2PlanarLayout::PLANE_A] = {
538 C2PlaneInfo::CHANNEL_A, // channel
539 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800540 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700541 1, // mColSampling
542 1, // mRowSampling
543 32, // allocatedDepth
544 2, // bitDepth
545 30, // rightShift
546 C2PlaneInfo::LITTLE_END, // endianness
547 C2PlanarLayout::PLANE_Y, // rootIx
548 0, // offset
549 };
550 break;
551 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800552
Marissa Wall2a24a302019-11-25 11:19:18 -0800553 case static_cast<uint32_t>(PixelFormat4::RGBA_8888):
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700554 // TODO: alpha channel
555 // fall-through
Marissa Wall2a24a302019-11-25 11:19:18 -0800556 case static_cast<uint32_t>(PixelFormat4::RGBX_8888): {
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700557 void *pointer = nullptr;
Marissa Wall2a24a302019-11-25 11:19:18 -0800558 // TODO: fence
559 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900560 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &pointer);
Marissa Wall2a24a302019-11-25 11:19:18 -0800561 if (err) {
562 ALOGE("failed transaction: lock(RGBA_8888)");
563 return C2_CORRUPTED;
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700564 }
565 addr[C2PlanarLayout::PLANE_R] = (uint8_t *)pointer;
566 addr[C2PlanarLayout::PLANE_G] = (uint8_t *)pointer + 1;
567 addr[C2PlanarLayout::PLANE_B] = (uint8_t *)pointer + 2;
568 layout->type = C2PlanarLayout::TYPE_RGB;
569 layout->numPlanes = 3;
570 layout->rootPlanes = 1;
571 layout->planes[C2PlanarLayout::PLANE_R] = {
572 C2PlaneInfo::CHANNEL_R, // channel
573 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800574 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700575 1, // mColSampling
576 1, // mRowSampling
577 8, // allocatedDepth
578 8, // bitDepth
579 0, // rightShift
580 C2PlaneInfo::NATIVE, // endianness
581 C2PlanarLayout::PLANE_R, // rootIx
582 0, // offset
583 };
584 layout->planes[C2PlanarLayout::PLANE_G] = {
585 C2PlaneInfo::CHANNEL_G, // channel
586 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800587 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700588 1, // mColSampling
589 1, // mRowSampling
590 8, // allocatedDepth
591 8, // bitDepth
592 0, // rightShift
593 C2PlaneInfo::NATIVE, // endianness
594 C2PlanarLayout::PLANE_R, // rootIx
595 1, // offset
596 };
597 layout->planes[C2PlanarLayout::PLANE_B] = {
598 C2PlaneInfo::CHANNEL_B, // channel
599 4, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800600 static_cast<int32_t>(4 * mStride), // rowInc
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700601 1, // mColSampling
602 1, // mRowSampling
603 8, // allocatedDepth
604 8, // bitDepth
605 0, // rightShift
606 C2PlaneInfo::NATIVE, // endianness
607 C2PlanarLayout::PLANE_R, // rootIx
608 2, // offset
609 };
610 break;
611 }
612
David Stevens1cadc75d2020-04-03 10:50:51 +0900613 case static_cast<uint32_t>(PixelFormat4::BLOB): {
614 void *pointer = nullptr;
615 // TODO: fence
616 status_t err = GraphicBufferMapper::get().lock(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900617 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &pointer);
David Stevens1cadc75d2020-04-03 10:50:51 +0900618 if (err) {
619 ALOGE("failed transaction: lock(BLOB)");
620 return C2_CORRUPTED;
621 }
622 *addr = (uint8_t *)pointer;
623 break;
624 }
625
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700626 case static_cast<uint32_t>(PixelFormat4::YCBCR_422_SP):
627 // fall-through
628 case static_cast<uint32_t>(PixelFormat4::YCRCB_420_SP):
629 // fall-through
630 case static_cast<uint32_t>(PixelFormat4::YCBCR_422_I):
631 // fall-through
Marissa Wall2a24a302019-11-25 11:19:18 -0800632 case static_cast<uint32_t>(PixelFormat4::YCBCR_420_888):
Lajos Molnar35d36cb2018-10-12 15:10:56 -0700633 // fall-through
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700634 case static_cast<uint32_t>(PixelFormat4::YV12): {
Marissa Wall2a24a302019-11-25 11:19:18 -0800635 android_ycbcr ycbcrLayout;
636
637 status_t err = GraphicBufferMapper::get().lockYCbCr(
Chih-Yu Huang486aa5d2020-10-13 16:22:58 +0900638 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &ycbcrLayout);
Marissa Wall2a24a302019-11-25 11:19:18 -0800639 if (err) {
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800640 ALOGE("failed transaction: lockYCbCr (err=%d)", err);
641 return C2_CORRUPTED;
642 }
643 if (!ycbcrLayout.y || !ycbcrLayout.cb || !ycbcrLayout.cr
644 || ycbcrLayout.ystride == 0
645 || ycbcrLayout.cstride == 0
646 || ycbcrLayout.chroma_step == 0) {
647 ALOGE("invalid layout: lockYCbCr (y=%s cb=%s cr=%s "
648 "ystride=%zu cstride=%zu chroma_step=%zu)",
649 ycbcrLayout.y ? "(non-null)" : "(null)",
650 ycbcrLayout.cb ? "(non-null)" : "(null)",
651 ycbcrLayout.cr ? "(non-null)" : "(null)",
652 ycbcrLayout.ystride, ycbcrLayout.cstride, ycbcrLayout.chroma_step);
Marissa Wall469b90a2019-11-06 10:12:43 -0800653 return C2_CORRUPTED;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700654 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800655
Pawin Vongmasa36653902018-11-15 00:10:25 -0800656 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)ycbcrLayout.y;
657 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)ycbcrLayout.cb;
658 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)ycbcrLayout.cr;
659 layout->type = C2PlanarLayout::TYPE_YUV;
660 layout->numPlanes = 3;
661 layout->rootPlanes = 3;
662 layout->planes[C2PlanarLayout::PLANE_Y] = {
663 C2PlaneInfo::CHANNEL_Y, // channel
664 1, // colInc
Marissa Wall2a24a302019-11-25 11:19:18 -0800665 (int32_t)ycbcrLayout.ystride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800666 1, // mColSampling
667 1, // mRowSampling
668 8, // allocatedDepth
669 8, // bitDepth
670 0, // rightShift
671 C2PlaneInfo::NATIVE, // endianness
672 C2PlanarLayout::PLANE_Y, // rootIx
673 0, // offset
674 };
675 layout->planes[C2PlanarLayout::PLANE_U] = {
676 C2PlaneInfo::CHANNEL_CB, // channel
Marissa Wall2a24a302019-11-25 11:19:18 -0800677 (int32_t)ycbcrLayout.chroma_step, // colInc
678 (int32_t)ycbcrLayout.cstride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800679 2, // mColSampling
680 2, // mRowSampling
681 8, // allocatedDepth
682 8, // bitDepth
683 0, // rightShift
684 C2PlaneInfo::NATIVE, // endianness
685 C2PlanarLayout::PLANE_U, // rootIx
686 0, // offset
687 };
688 layout->planes[C2PlanarLayout::PLANE_V] = {
689 C2PlaneInfo::CHANNEL_CR, // channel
Marissa Wall2a24a302019-11-25 11:19:18 -0800690 (int32_t)ycbcrLayout.chroma_step, // colInc
691 (int32_t)ycbcrLayout.cstride, // rowInc
Pawin Vongmasa36653902018-11-15 00:10:25 -0800692 2, // mColSampling
693 2, // mRowSampling
694 8, // allocatedDepth
695 8, // bitDepth
696 0, // rightShift
697 C2PlaneInfo::NATIVE, // endianness
698 C2PlanarLayout::PLANE_V, // rootIx
699 0, // offset
700 };
Pawin Vongmasa36653902018-11-15 00:10:25 -0800701 break;
702 }
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700703
704 case static_cast<uint32_t>(PixelFormat4::YCBCR_P010): {
Praveen Chavan6380fff2022-06-22 10:49:48 -0700705 // In Android T, P010 is relaxed to allow arbitrary stride for the Y and UV planes,
706 // try locking with the gralloc4 mapper first.
707 c2_status_t status = Gralloc4Mapper_lock(
708 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, layout, addr);
709 if (status == C2_OK) {
710 break;
711 }
712
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700713 void *pointer = nullptr;
714 status_t err = GraphicBufferMapper::get().lock(
715 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
716 if (err) {
717 ALOGE("failed transaction: lock(YCBCR_P010)");
718 return C2_CORRUPTED;
719 }
720 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)pointer;
721 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)pointer + mStride * 2 * rect.height();
722 addr[C2PlanarLayout::PLANE_V] = addr[C2PlanarLayout::PLANE_U] + 2;
723 layout->type = C2PlanarLayout::TYPE_YUV;
724 layout->numPlanes = 3;
725 layout->rootPlanes = 2;
726 layout->planes[C2PlanarLayout::PLANE_Y] = {
727 C2PlaneInfo::CHANNEL_Y, // channel
728 2, // colInc
729 static_cast<int32_t>(2 * mStride), // rowInc
730 1, // mColSampling
731 1, // mRowSampling
732 16, // allocatedDepth
733 10, // bitDepth
734 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800735 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700736 C2PlanarLayout::PLANE_Y, // rootIx
737 0, // offset
738 };
739 layout->planes[C2PlanarLayout::PLANE_U] = {
740 C2PlaneInfo::CHANNEL_CB, // channel
741 4, // colInc
742 static_cast<int32_t>(2 * mStride), // rowInc
743 2, // mColSampling
744 2, // mRowSampling
745 16, // allocatedDepth
746 10, // bitDepth
747 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800748 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700749 C2PlanarLayout::PLANE_U, // rootIx
750 0, // offset
751 };
752 layout->planes[C2PlanarLayout::PLANE_V] = {
753 C2PlaneInfo::CHANNEL_CR, // channel
754 4, // colInc
755 static_cast<int32_t>(2 * mStride), // rowInc
756 2, // mColSampling
757 2, // mRowSampling
758 16, // allocatedDepth
759 10, // bitDepth
760 6, // rightShift
Praveen Chavan34493f12021-02-18 00:51:50 -0800761 kEndianness, // endianness
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700762 C2PlanarLayout::PLANE_U, // rootIx
763 2, // offset
764 };
765 break;
766 }
767
768 default: {
Praveen Chavan34493f12021-02-18 00:51:50 -0800769 // We don't know what it is, let's try to lock it with gralloc4
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700770 android_ycbcr ycbcrLayout;
Praveen Chavan34493f12021-02-18 00:51:50 -0800771 c2_status_t status = Gralloc4Mapper_lock(
772 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, layout, addr);
773 if (status == C2_OK) {
774 break;
775 }
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700776
Praveen Chavan34493f12021-02-18 00:51:50 -0800777 // fallback to lockYCbCr
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700778 status_t err = GraphicBufferMapper::get().lockYCbCr(
779 const_cast<native_handle_t*>(mBuffer), grallocUsage, rect, &ycbcrLayout);
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800780 if (err == OK && ycbcrLayout.y && ycbcrLayout.cb && ycbcrLayout.cr
781 && ycbcrLayout.ystride > 0
782 && ycbcrLayout.cstride > 0
783 && ycbcrLayout.chroma_step > 0) {
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700784 addr[C2PlanarLayout::PLANE_Y] = (uint8_t *)ycbcrLayout.y;
785 addr[C2PlanarLayout::PLANE_U] = (uint8_t *)ycbcrLayout.cb;
786 addr[C2PlanarLayout::PLANE_V] = (uint8_t *)ycbcrLayout.cr;
787 layout->type = C2PlanarLayout::TYPE_YUV;
788 layout->numPlanes = 3;
789 layout->rootPlanes = 3;
790 layout->planes[C2PlanarLayout::PLANE_Y] = {
791 C2PlaneInfo::CHANNEL_Y, // channel
792 1, // colInc
793 (int32_t)ycbcrLayout.ystride, // rowInc
794 1, // mColSampling
795 1, // mRowSampling
796 8, // allocatedDepth
797 8, // bitDepth
798 0, // rightShift
799 C2PlaneInfo::NATIVE, // endianness
800 C2PlanarLayout::PLANE_Y, // rootIx
801 0, // offset
802 };
803 layout->planes[C2PlanarLayout::PLANE_U] = {
804 C2PlaneInfo::CHANNEL_CB, // channel
805 (int32_t)ycbcrLayout.chroma_step, // colInc
806 (int32_t)ycbcrLayout.cstride, // rowInc
807 2, // mColSampling
808 2, // mRowSampling
809 8, // allocatedDepth
810 8, // bitDepth
811 0, // rightShift
812 C2PlaneInfo::NATIVE, // endianness
813 C2PlanarLayout::PLANE_U, // rootIx
814 0, // offset
815 };
816 layout->planes[C2PlanarLayout::PLANE_V] = {
817 C2PlaneInfo::CHANNEL_CR, // channel
818 (int32_t)ycbcrLayout.chroma_step, // colInc
819 (int32_t)ycbcrLayout.cstride, // rowInc
820 2, // mColSampling
821 2, // mRowSampling
822 8, // allocatedDepth
823 8, // bitDepth
824 0, // rightShift
825 C2PlaneInfo::NATIVE, // endianness
826 C2PlanarLayout::PLANE_V, // rootIx
827 0, // offset
828 };
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700829 break;
830 }
831
832 // We really don't know what this is; lock the buffer and pass it through ---
833 // the client may know how to interpret it.
Lajos Molnar8faebbc2021-06-30 17:07:42 -0700834
835 // unlock previous allocation if it was successful
836 if (err == OK) {
837 err = GraphicBufferMapper::get().unlock(mBuffer);
838 if (err) {
839 ALOGE("failed transaction: unlock");
840 return C2_CORRUPTED;
841 }
842 }
843
Wonsik Kim29e3c4d2020-09-02 12:19:44 -0700844 void *pointer = nullptr;
845 err = GraphicBufferMapper::get().lock(
846 const_cast<native_handle_t *>(mBuffer), grallocUsage, rect, &pointer);
847 if (err) {
848 ALOGE("failed transaction: lock(??? %x)", mFormat);
849 return C2_CORRUPTED;
850 }
851 addr[0] = (uint8_t *)pointer;
852 layout->type = C2PlanarLayout::TYPE_UNKNOWN;
853 layout->numPlanes = 1;
854 layout->rootPlanes = 1;
855 layout->planes[0] = {
856 // TODO: CHANNEL_UNKNOWN?
857 C2PlaneInfo::channel_t(0xFF), // channel
858 1, // colInc
859 int32_t(mStride), // rowInc
860 1, // mColSampling
861 1, // mRowSampling
862 8, // allocatedDepth
863 8, // bitDepth
864 0, // rightShift
865 C2PlaneInfo::NATIVE, // endianness
866 0, // rootIx
867 0, // offset
868 };
869 break;
870 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800871 }
872 mLocked = true;
873
Wonsik Kimf12bebc2022-03-24 16:25:03 -0700874 // handle interleaved formats
875 if (layout->type == C2PlanarLayout::TYPE_YUV && layout->rootPlanes == 3) {
876 intptr_t uvOffset = addr[C2PlanarLayout::PLANE_V] - addr[C2PlanarLayout::PLANE_U];
877 intptr_t uvColInc = layout->planes[C2PlanarLayout::PLANE_U].colInc;
878 if (uvOffset > 0 && uvOffset < uvColInc) {
879 layout->rootPlanes = 2;
880 layout->planes[C2PlanarLayout::PLANE_V].rootIx = C2PlanarLayout::PLANE_U;
881 layout->planes[C2PlanarLayout::PLANE_V].offset = uvOffset;
882 } else if (uvOffset < 0 && uvOffset > -uvColInc) {
883 layout->rootPlanes = 2;
884 layout->planes[C2PlanarLayout::PLANE_U].rootIx = C2PlanarLayout::PLANE_V;
885 layout->planes[C2PlanarLayout::PLANE_U].offset = -uvOffset;
886 }
887 }
888
889 ALOGV("C2AllocationGralloc::map: layout: type=%d numPlanes=%d rootPlanes=%d",
890 layout->type, layout->numPlanes, layout->rootPlanes);
891 for (int i = 0; i < layout->numPlanes; ++i) {
892 const C2PlaneInfo &plane = layout->planes[i];
893 ALOGV("C2AllocationGralloc::map: plane[%d]: colInc=%d rowInc=%d rootIx=%u offset=%u",
894 i, plane.colInc, plane.rowInc, plane.rootIx, plane.offset);
895 }
896
Pawin Vongmasa36653902018-11-15 00:10:25 -0800897 return C2_OK;
898}
899
900c2_status_t C2AllocationGralloc::unmap(
901 uint8_t **addr, C2Rect rect, C2Fence *fence /* nullable */) {
902 // TODO: check addr and size, use fence
903 (void)addr;
904 (void)rect;
Marissa Wall2a24a302019-11-25 11:19:18 -0800905 (void)fence;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800906
907 std::lock_guard<std::mutex> lock(mMappedLock);
Marissa Wall2a24a302019-11-25 11:19:18 -0800908 // TODO: fence
909 status_t err = GraphicBufferMapper::get().unlock(mBuffer);
910 if (err) {
911 ALOGE("failed transaction: unlock");
912 return C2_CORRUPTED;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700913 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800914
915 mLocked = false;
916 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800917}
918
919bool C2AllocationGralloc::equals(const std::shared_ptr<const C2GraphicAllocation> &other) const {
920 return other && other->handle() == handle();
921}
922
923/* ===================================== GRALLOC ALLOCATOR ==================================== */
924class C2AllocatorGralloc::Impl {
925public:
926 Impl(id_t id, bool bufferQueue);
927
928 id_t getId() const {
929 return mTraits->id;
930 }
931
932 C2String getName() const {
933 return mTraits->name;
934 }
935
936 std::shared_ptr<const C2Allocator::Traits> getTraits() const {
937 return mTraits;
938 }
939
940 c2_status_t newGraphicAllocation(
941 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
942 std::shared_ptr<C2GraphicAllocation> *allocation);
943
944 c2_status_t priorGraphicAllocation(
945 const C2Handle *handle,
946 std::shared_ptr<C2GraphicAllocation> *allocation);
947
948 c2_status_t status() const { return mInit; }
949
950private:
951 std::shared_ptr<C2Allocator::Traits> mTraits;
952 c2_status_t mInit;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800953 const bool mBufferQueue;
954};
955
956void _UnwrapNativeCodec2GrallocMetadata(
957 const C2Handle *const handle,
958 uint32_t *width, uint32_t *height, uint32_t *format,uint64_t *usage, uint32_t *stride,
959 uint32_t *generation, uint64_t *igbp_id, uint32_t *igbp_slot) {
960 (void)C2HandleGralloc::Import(handle, width, height, format, usage, stride,
961 generation, igbp_id, igbp_slot);
962}
963
964C2AllocatorGralloc::Impl::Impl(id_t id, bool bufferQueue)
965 : mInit(C2_OK), mBufferQueue(bufferQueue) {
966 // TODO: get this from allocator
967 C2MemoryUsage minUsage = { 0, 0 }, maxUsage = { ~(uint64_t)0, ~(uint64_t)0 };
968 Traits traits = { "android.allocator.gralloc", id, C2Allocator::GRAPHIC, minUsage, maxUsage };
969 mTraits = std::make_shared<C2Allocator::Traits>(traits);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800970}
971
972c2_status_t C2AllocatorGralloc::Impl::newGraphicAllocation(
973 uint32_t width, uint32_t height, uint32_t format, const C2MemoryUsage &usage,
974 std::shared_ptr<C2GraphicAllocation> *allocation) {
975 uint64_t grallocUsage = static_cast<C2AndroidMemoryUsage>(usage).asGrallocUsage();
976 ALOGV("allocating buffer with usage %#llx => %#llx",
977 (long long)usage.expected, (long long)grallocUsage);
978
Marissa Wall2a24a302019-11-25 11:19:18 -0800979 buffer_handle_t buffer;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700980
Marissa Wall2a24a302019-11-25 11:19:18 -0800981 uint32_t stride = 0;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -0700982
Marissa Wall2a24a302019-11-25 11:19:18 -0800983 status_t err = GraphicBufferAllocator::get().allocateRawHandle(width, height, format,
984 1u /* layer count */, grallocUsage, &buffer, &stride, "C2GrallocAllocation");
985 if (err) {
986 ALOGE("failed transaction: allocate");
987 return C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800988 }
Marissa Wall2a24a302019-11-25 11:19:18 -0800989
990 hidl_handle hidlHandle;
991 hidlHandle.setTo(const_cast<native_handle_t*>(buffer), true);
992
993 allocation->reset(new C2AllocationGralloc(
994 width, height, format, 1u /* layer count */, grallocUsage, stride, hidlHandle,
995 C2HandleGralloc::WrapAndMoveNativeHandle(
996 hidlHandle, width, height,
997 format, grallocUsage, stride,
998 0, 0, mBufferQueue ? ~0 : 0),
999 mTraits->id));
1000 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001001}
1002
1003c2_status_t C2AllocatorGralloc::Impl::priorGraphicAllocation(
1004 const C2Handle *handle,
1005 std::shared_ptr<C2GraphicAllocation> *allocation) {
Pawin Vongmasad032f2d2019-05-15 08:42:44 -07001006
Marissa Wall2a24a302019-11-25 11:19:18 -08001007 uint32_t generation;
1008 uint64_t igbp_id;
1009 uint32_t igbp_slot;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -07001010
Marissa Wall2a24a302019-11-25 11:19:18 -08001011 uint32_t width;
1012 uint32_t height;
1013 uint32_t format;
1014 uint32_t layerCount = 1;
1015 uint64_t grallocUsage;
1016 uint32_t stride;
Pawin Vongmasad032f2d2019-05-15 08:42:44 -07001017
Marissa Wall2a24a302019-11-25 11:19:18 -08001018 const C2HandleGralloc *grallocHandle = C2HandleGralloc::Import(
1019 handle, &width, &height, &format, &grallocUsage, &stride,
1020 &generation, &igbp_id, &igbp_slot);
1021 if (grallocHandle == nullptr) {
1022 return C2_BAD_VALUE;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001023 }
Marissa Wall2a24a302019-11-25 11:19:18 -08001024
1025 hidl_handle hidlHandle;
1026 hidlHandle.setTo(C2HandleGralloc::UnwrapNativeHandle(grallocHandle), true);
1027
1028 allocation->reset(new C2AllocationGralloc(
1029 width, height, format, layerCount,
1030 grallocUsage, stride, hidlHandle, grallocHandle, mTraits->id));
1031 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001032}
1033
1034C2AllocatorGralloc::C2AllocatorGralloc(id_t id, bool bufferQueue)
1035 : mImpl(new Impl(id, bufferQueue)) {}
1036
1037C2AllocatorGralloc::~C2AllocatorGralloc() { delete mImpl; }
1038
1039C2Allocator::id_t C2AllocatorGralloc::getId() const {
1040 return mImpl->getId();
1041}
1042
1043C2String C2AllocatorGralloc::getName() const {
1044 return mImpl->getName();
1045}
1046
1047std::shared_ptr<const C2Allocator::Traits> C2AllocatorGralloc::getTraits() const {
1048 return mImpl->getTraits();
1049}
1050
1051c2_status_t C2AllocatorGralloc::newGraphicAllocation(
1052 uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
1053 std::shared_ptr<C2GraphicAllocation> *allocation) {
1054 return mImpl->newGraphicAllocation(width, height, format, usage, allocation);
1055}
1056
1057c2_status_t C2AllocatorGralloc::priorGraphicAllocation(
1058 const C2Handle *handle,
1059 std::shared_ptr<C2GraphicAllocation> *allocation) {
1060 return mImpl->priorGraphicAllocation(handle, allocation);
1061}
1062
1063c2_status_t C2AllocatorGralloc::status() const {
1064 return mImpl->status();
1065}
1066
John Stultz653ddd12020-09-19 05:26:24 +00001067// static
1068bool C2AllocatorGralloc::CheckHandle(const C2Handle* const o) {
1069 return C2HandleGralloc::IsValid(o);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001070}
1071
1072} // namespace android