OSDN Git Service

Correct the header file reference
[android-x86/external-minigbm.git] / cros_gralloc / cros_gralloc_buffer.cc
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 <errno.h>
11 #include <sys/mman.h>
12
13 cros_gralloc_buffer::cros_gralloc_buffer(uint32_t id, struct bo *acquire_bo,
14                                          struct cros_gralloc_handle *acquire_handle)
15     : id_(id), bo_(acquire_bo), hnd_(acquire_handle), refcount_(1), lockcount_(0)
16 {
17         assert(bo_);
18         num_planes_ = drv_bo_get_num_planes(bo_);
19         for (uint32_t plane = 0; plane < num_planes_; plane++)
20                 lock_data_[plane] = nullptr;
21 }
22
23 cros_gralloc_buffer::~cros_gralloc_buffer()
24 {
25         drv_bo_destroy(bo_);
26         if (hnd_) {
27                 native_handle_close(&hnd_->base);
28                 delete hnd_;
29         }
30 }
31
32 uint32_t cros_gralloc_buffer::get_id() const
33 {
34         return id_;
35 }
36
37 int32_t cros_gralloc_buffer::increase_refcount()
38 {
39         return ++refcount_;
40 }
41
42 int32_t cros_gralloc_buffer::decrease_refcount()
43 {
44         assert(refcount_ > 0);
45         return --refcount_;
46 }
47
48 int32_t cros_gralloc_buffer::lock(uint32_t map_flags, uint8_t *addr[DRV_MAX_PLANES])
49 {
50         void *vaddr = nullptr;
51
52         memset(addr, 0, DRV_MAX_PLANES * sizeof(*addr));
53
54         /*
55          * Gralloc consumers don't support more than one kernel buffer per buffer object yet, so
56          * just use the first kernel buffer.
57          */
58         if (drv_num_buffers_per_bo(bo_) != 1) {
59                 cros_gralloc_error("Can only support one buffer per bo.");
60                 return -EINVAL;
61         }
62
63         if (map_flags) {
64                 if (lock_data_[0]) {
65                         drv_bo_invalidate(bo_, lock_data_[0]);
66                         vaddr = lock_data_[0]->addr;
67                 } else {
68                         vaddr = drv_bo_map(bo_, 0, 0, drv_bo_get_width(bo_), drv_bo_get_height(bo_),
69                                            map_flags, &lock_data_[0], 0);
70                 }
71
72                 if (vaddr == MAP_FAILED) {
73                         cros_gralloc_error("Mapping failed.");
74                         return -EFAULT;
75                 }
76         }
77
78         for (uint32_t plane = 0; plane < num_planes_; plane++)
79                 addr[plane] = static_cast<uint8_t *>(vaddr) + drv_bo_get_plane_offset(bo_, plane);
80
81         lockcount_++;
82         return 0;
83 }
84
85 int32_t cros_gralloc_buffer::unlock()
86 {
87         if (lockcount_ <= 0) {
88                 cros_gralloc_error("Buffer was not locked.");
89                 return -EINVAL;
90         }
91
92         if (!--lockcount_) {
93                 if (lock_data_[0]) {
94                         drv_bo_unmap(bo_, lock_data_[0]);
95                         lock_data_[0] = nullptr;
96                 }
97         }
98
99         return 0;
100 }