OSDN Git Service

4ee18971656348703eccb33580e2d0ca7dbdfdff
[android-x86/external-minigbm.git] / cros_gralloc / cros_alloc_device.cc
1 /*
2  * Copyright 2016 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.h"
8
9 static struct cros_gralloc_bo *cros_gralloc_bo_create(struct driver *drv, int width, int height,
10                                                       int format, int usage)
11 {
12         uint64_t drv_usage;
13         uint32_t drv_format;
14         struct combination *combo;
15         struct cros_gralloc_bo *bo;
16
17         drv_format = cros_gralloc_convert_format(format);
18         drv_format = drv_resolve_format(drv, drv_format);
19         drv_usage = cros_gralloc_convert_flags(usage);
20
21         combo = drv_get_combination(drv, drv_format, drv_usage);
22
23         if (!combo && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
24                 drv_usage &= ~BO_USE_SCANOUT;
25                 combo = drv_get_combination(drv, drv_format, drv_usage);
26         }
27
28         if (!combo) {
29                 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, "
30                                    "drv_format: %4.4s, drv_flags: %llu",
31                                    format, usage, reinterpret_cast<char *>(&drv_format), drv_usage);
32                 return NULL;
33         }
34
35         bo = new cros_gralloc_bo();
36         memset(bo, 0, sizeof(*bo));
37
38         bo->bo = drv_bo_create(drv, width, height, drv_format, drv_usage);
39         if (!bo->bo) {
40                 delete bo;
41                 cros_gralloc_error("Failed to create bo.");
42                 return NULL;
43         }
44
45         /*
46          * If there is a desire for more than one kernel buffer, this can be
47          * removed once the ArcCodec and Wayland service have the ability to
48          * send more than one fd. GL/Vulkan drivers may also have to modified.
49          */
50         if (drv_num_buffers_per_bo(bo->bo) != 1) {
51                 drv_bo_destroy(bo->bo);
52                 delete bo;
53                 cros_gralloc_error("Can only support one buffer per bo.");
54                 return NULL;
55         }
56
57         bo->refcount = 1;
58
59         return bo;
60 }
61
62 static struct cros_gralloc_handle *cros_gralloc_handle_from_bo(struct bo *bo)
63 {
64         uint64_t mod;
65         size_t num_planes;
66         struct cros_gralloc_handle *hnd;
67
68         hnd = new cros_gralloc_handle();
69         memset(hnd, 0, sizeof(*hnd));
70
71         num_planes = drv_bo_get_num_planes(bo);
72
73         hnd->base.version = sizeof(hnd->base);
74         hnd->base.numFds = num_planes;
75         hnd->base.numInts = num_ints_handle() - num_planes;
76
77         for (size_t p = 0; p < num_planes; p++) {
78                 hnd->fds[p] = drv_bo_get_plane_fd(bo, p);
79                 hnd->strides[p] = drv_bo_get_plane_stride(bo, p);
80                 hnd->offsets[p] = drv_bo_get_plane_offset(bo, p);
81                 hnd->sizes[p] = drv_bo_get_plane_size(bo, p);
82
83                 mod = drv_bo_get_plane_format_modifier(bo, p);
84                 hnd->format_modifiers[p] = static_cast<uint32_t>(mod >> 32);
85                 hnd->format_modifiers[p + 1] = static_cast<uint32_t>(mod);
86         }
87
88         hnd->width = drv_bo_get_width(bo);
89         hnd->height = drv_bo_get_height(bo);
90         hnd->format = drv_bo_get_format(bo);
91         hnd->pixel_stride = drv_bo_get_stride_in_pixels(bo);
92
93         hnd->magic = cros_gralloc_magic();
94
95         return hnd;
96 }
97
98 static int cros_gralloc_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
99                               buffer_handle_t *handle, int *stride)
100 {
101         auto mod = (struct cros_gralloc_module *)dev->common.module;
102         std::lock_guard<std::mutex> lock(mod->mutex);
103
104         auto bo = cros_gralloc_bo_create(mod->drv, w, h, format, usage);
105         if (!bo)
106                 return CROS_GRALLOC_ERROR_NO_RESOURCES;
107
108         auto hnd = cros_gralloc_handle_from_bo(bo->bo);
109         hnd->droid_format = static_cast<int32_t>(format);
110         hnd->usage = static_cast<int32_t>(usage);
111
112         mod->handles[hnd].registrations = 0;
113         mod->handles[hnd].bo = bo;
114         bo->hnd = hnd;
115
116         mod->buffers[drv_bo_get_plane_handle(bo->bo, 0).u32] = bo;
117
118         *stride = static_cast<int>(hnd->pixel_stride);
119         *handle = &hnd->base;
120
121         return CROS_GRALLOC_ERROR_NONE;
122 }
123
124 static int cros_gralloc_free(alloc_device_t *dev, buffer_handle_t handle)
125 {
126         struct cros_gralloc_bo *bo;
127         auto hnd = (struct cros_gralloc_handle *)handle;
128         auto mod = (struct cros_gralloc_module *)dev->common.module;
129         std::lock_guard<std::mutex> lock(mod->mutex);
130
131         if (cros_gralloc_validate_handle(hnd)) {
132                 cros_gralloc_error("Invalid handle.");
133                 return CROS_GRALLOC_ERROR_BAD_HANDLE;
134         }
135
136         if (cros_gralloc_validate_reference(mod, hnd, &bo)) {
137                 cros_gralloc_error("Invalid Reference.");
138                 return CROS_GRALLOC_ERROR_BAD_HANDLE;
139         }
140
141         if (mod->handles[hnd].registrations > 0) {
142                 cros_gralloc_error("Deallocating before unregistering.");
143                 return CROS_GRALLOC_ERROR_BAD_HANDLE;
144         }
145
146         return cros_gralloc_decrement_reference_count(mod, bo);
147 }
148
149 static int cros_gralloc_close(struct hw_device_t *dev)
150 {
151         auto mod = (struct cros_gralloc_module *)dev->module;
152         auto alloc = (struct alloc_device_t *)dev;
153         std::lock_guard<std::mutex> lock(mod->mutex);
154
155         if (mod->drv) {
156                 drv_destroy(mod->drv);
157                 mod->drv = NULL;
158         }
159
160         mod->buffers.clear();
161         mod->handles.clear();
162
163         delete alloc;
164
165         return CROS_GRALLOC_ERROR_NONE;
166 }
167
168 int cros_gralloc_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
169 {
170         auto module = (struct cros_gralloc_module *)mod;
171         std::lock_guard<std::mutex> lock(module->mutex);
172
173         if (module->drv)
174                 return CROS_GRALLOC_ERROR_NONE;
175
176         if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
177                 cros_gralloc_error("Incorrect device name - %s.", name);
178                 return CROS_GRALLOC_ERROR_UNSUPPORTED;
179         }
180
181         if (cros_gralloc_rendernode_open(&module->drv)) {
182                 cros_gralloc_error("Failed to open render node.");
183                 return CROS_GRALLOC_ERROR_NO_RESOURCES;
184         }
185
186         auto alloc = new alloc_device_t();
187         memset(alloc, 0, sizeof(*alloc));
188
189         alloc->alloc = cros_gralloc_alloc;
190         alloc->free = cros_gralloc_free;
191         alloc->common.tag = HARDWARE_DEVICE_TAG;
192         alloc->common.version = 0;
193         alloc->common.module = (hw_module_t *)mod;
194         alloc->common.close = cros_gralloc_close;
195
196         *dev = &alloc->common;
197
198         return CROS_GRALLOC_ERROR_NONE;
199 }