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