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