Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include "cros_gralloc_buffer.h" |
| 8 | |
| 9 | #include <assert.h> |
| 10 | #include <sys/mman.h> |
| 11 | |
| 12 | cros_gralloc_buffer::cros_gralloc_buffer(uint32_t id, struct bo *acquirebo_, |
| 13 | struct cros_gralloc_handle *acquire_handle) |
| 14 | : id_(id), bo_(acquirebo_), hnd_(acquire_handle), refcount_(1), lockcount_(0) |
| 15 | { |
| 16 | assert(bo_); |
| 17 | num_planes_ = drv_bo_get_num_planes(bo_); |
| 18 | for (uint32_t plane = 0; plane < num_planes_; plane++) |
| 19 | lock_data_[plane] = nullptr; |
| 20 | } |
| 21 | |
| 22 | cros_gralloc_buffer::~cros_gralloc_buffer() |
| 23 | { |
| 24 | drv_bo_destroy(bo_); |
| 25 | if (hnd_) { |
| 26 | native_handle_close(&hnd_->base); |
| 27 | delete hnd_; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | uint32_t cros_gralloc_buffer::get_id() const |
| 32 | { |
| 33 | return id_; |
| 34 | } |
| 35 | |
| 36 | int32_t cros_gralloc_buffer::increase_refcount() |
| 37 | { |
| 38 | return ++refcount_; |
| 39 | } |
| 40 | |
| 41 | int32_t cros_gralloc_buffer::decrease_refcount() |
| 42 | { |
| 43 | assert(refcount_ > 0); |
| 44 | return --refcount_; |
| 45 | } |
| 46 | |
| 47 | int32_t cros_gralloc_buffer::lock(uint64_t flags, uint8_t *addr[DRV_MAX_PLANES]) |
| 48 | { |
| 49 | /* |
| 50 | * Gralloc consumers don't support more than one kernel buffer per buffer object yet, so |
| 51 | * just use the first kernel buffer. |
| 52 | */ |
| 53 | if (drv_num_buffers_per_bo(bo_) != 1) { |
| 54 | cros_gralloc_error("Can only support one buffer per bo."); |
| 55 | return CROS_GRALLOC_ERROR_NO_RESOURCES; |
| 56 | } |
| 57 | |
| 58 | if (flags) { |
| 59 | void *vaddr; |
| 60 | if (lock_data_[0]) { |
| 61 | vaddr = lock_data_[0]->addr; |
| 62 | } else { |
| 63 | vaddr = drv_bo_map(bo_, 0, 0, drv_bo_get_width(bo_), drv_bo_get_height(bo_), |
Joe Kniss | 6570585 | 2017-06-29 15:02:46 -0700 | [diff] [blame^] | 64 | BO_TRANSFER_READ_WRITE, &lock_data_[0], 0); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (vaddr == MAP_FAILED) { |
| 68 | cros_gralloc_error("Mapping failed."); |
| 69 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 70 | } |
| 71 | |
| 72 | addr[0] = static_cast<uint8_t *>(vaddr); |
| 73 | } |
| 74 | |
| 75 | for (uint32_t plane = 0; plane < num_planes_; plane++) |
| 76 | addr[plane] = addr[0] + drv_bo_get_plane_offset(bo_, plane); |
| 77 | |
| 78 | lockcount_++; |
| 79 | return CROS_GRALLOC_ERROR_NONE; |
| 80 | } |
| 81 | |
| 82 | int32_t cros_gralloc_buffer::unlock() |
| 83 | { |
| 84 | if (lockcount_ <= 0) { |
| 85 | cros_gralloc_error("Buffer was not locked."); |
| 86 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 87 | } |
| 88 | |
| 89 | if (!--lockcount_) { |
| 90 | if (lock_data_[0]) { |
| 91 | drv_bo_unmap(bo_, lock_data_[0]); |
| 92 | lock_data_[0] = nullptr; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return CROS_GRALLOC_ERROR_NONE; |
| 97 | } |