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.
7 #include "cros_gralloc_driver.h"
10 #include "i915_private_android.h"
16 cros_gralloc_driver::cros_gralloc_driver() : drv_(nullptr)
20 cros_gralloc_driver::~cros_gralloc_driver()
31 int32_t cros_gralloc_driver::init()
34 * Create a driver from rendernode while filtering out
35 * the specified undesired driver.
37 * TODO(gsingh): Enable render nodes on udl/evdi.
41 drmVersionPtr version;
42 char const *str = "%s/renderD%d";
43 const char *undesired[2] = { "vgem", nullptr };
44 uint32_t num_nodes = 63;
45 uint32_t min_node = 128;
46 uint32_t max_node = (min_node + num_nodes);
48 for (uint32_t i = 0; i < ARRAY_SIZE(undesired); i++) {
49 for (uint32_t j = min_node; j < max_node; j++) {
51 if (asprintf(&node, str, DRM_DIR_NAME, j) < 0)
54 fd = open(node, O_RDWR, 0);
60 version = drmGetVersion(fd);
64 if (undesired[i] && !strcmp(version->name, undesired[i])) {
65 drmFreeVersion(version);
69 drmFreeVersion(version);
70 drv_ = drv_create(fd);
79 bool cros_gralloc_driver::is_supported(const struct cros_gralloc_buffer_descriptor *descriptor)
81 struct combination *combo;
82 uint32_t resolved_format;
83 resolved_format = drv_resolve_format(drv_, descriptor->drm_format, descriptor->use_flags);
84 combo = drv_get_combination(drv_, resolved_format, descriptor->use_flags);
85 return (combo != nullptr);
88 int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descriptor *descriptor,
89 buffer_handle_t *out_handle)
94 uint32_t resolved_format;
97 struct cros_gralloc_handle *hnd;
99 resolved_format = drv_resolve_format(drv_, descriptor->drm_format, descriptor->use_flags);
100 bo = drv_bo_create(drv_, descriptor->width, descriptor->height, resolved_format,
101 descriptor->use_flags);
103 cros_gralloc_error("Failed to create bo.");
108 * If there is a desire for more than one kernel buffer, this can be
109 * removed once the ArcCodec and Wayland service have the ability to
110 * send more than one fd. GL/Vulkan drivers may also have to modified.
112 if (drv_num_buffers_per_bo(bo) != 1) {
114 cros_gralloc_error("Can only support one buffer per bo.");
118 hnd = new cros_gralloc_handle();
119 num_planes = drv_bo_get_num_planes(bo);
121 hnd->base.version = sizeof(hnd->base);
122 hnd->base.numFds = num_planes;
123 hnd->base.numInts = handle_data_size - num_planes;
125 for (size_t plane = 0; plane < num_planes; plane++) {
126 hnd->fds[plane] = drv_bo_get_plane_fd(bo, plane);
127 hnd->strides[plane] = drv_bo_get_plane_stride(bo, plane);
128 hnd->offsets[plane] = drv_bo_get_plane_offset(bo, plane);
130 mod = drv_bo_get_plane_format_modifier(bo, plane);
131 hnd->format_modifiers[2 * plane] = static_cast<uint32_t>(mod >> 32);
132 hnd->format_modifiers[2 * plane + 1] = static_cast<uint32_t>(mod);
135 hnd->width = drv_bo_get_width(bo);
136 hnd->height = drv_bo_get_height(bo);
137 hnd->format = drv_bo_get_format(bo);
138 hnd->use_flags[0] = static_cast<uint32_t>(descriptor->use_flags >> 32);
139 hnd->use_flags[1] = static_cast<uint32_t>(descriptor->use_flags);
140 hnd->pixel_stride = drv_bo_get_stride_in_pixels(bo);
141 hnd->magic = cros_gralloc_magic;
142 int32_t format = i915_private_invert_format(hnd->format);
144 format = descriptor->droid_format;
146 hnd->droid_format = format;
147 hnd->usage = descriptor->producer_usage;
148 hnd->producer_usage = descriptor->producer_usage;
149 hnd->consumer_usage = descriptor->consumer_usage;
151 id = drv_bo_get_plane_handle(bo, 0).u32;
152 auto buffer = new cros_gralloc_buffer(id, bo, hnd);
154 SCOPED_SPIN_LOCK(mutex_);
155 buffers_.emplace(id, buffer);
156 handles_.emplace(hnd, std::make_pair(buffer, 1));
157 *out_handle = &hnd->base;
161 int32_t cros_gralloc_driver::retain(buffer_handle_t handle)
164 SCOPED_SPIN_LOCK(mutex_);
166 auto hnd = cros_gralloc_convert_handle(handle);
168 cros_gralloc_error("Invalid handle.");
172 auto buffer = get_buffer(hnd);
174 handles_[hnd].second++;
175 buffer->increase_refcount();
179 if (drmPrimeFDToHandle(drv_get_fd(drv_), hnd->fds[0], &id)) {
180 cros_gralloc_error("drmPrimeFDToHandle failed.");
184 if (buffers_.count(id)) {
185 buffer = buffers_[id];
186 buffer->increase_refcount();
189 struct drv_import_fd_data data;
190 data.format = hnd->format;
191 data.width = hnd->width;
192 data.height = hnd->height;
193 data.use_flags = static_cast<uint64_t>(hnd->use_flags[0]) << 32;
194 data.use_flags |= hnd->use_flags[1];
196 memcpy(data.fds, hnd->fds, sizeof(data.fds));
197 memcpy(data.strides, hnd->strides, sizeof(data.strides));
198 memcpy(data.offsets, hnd->offsets, sizeof(data.offsets));
199 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++) {
200 data.format_modifiers[plane] =
201 static_cast<uint64_t>(hnd->format_modifiers[2 * plane]) << 32;
202 data.format_modifiers[plane] |= hnd->format_modifiers[2 * plane + 1];
205 bo = drv_bo_import(drv_, &data);
209 id = drv_bo_get_plane_handle(bo, 0).u32;
211 buffer = new cros_gralloc_buffer(id, bo, nullptr);
212 buffers_.emplace(id, buffer);
215 handles_.emplace(hnd, std::make_pair(buffer, 1));
219 int32_t cros_gralloc_driver::release(buffer_handle_t handle)
221 SCOPED_SPIN_LOCK(mutex_);
223 auto hnd = cros_gralloc_convert_handle(handle);
225 cros_gralloc_error("Invalid handle.");
229 auto buffer = get_buffer(hnd);
231 cros_gralloc_error("Invalid Reference.");
235 if (!--handles_[hnd].second)
238 if (buffer->decrease_refcount() == 0) {
239 buffers_.erase(buffer->get_id());
246 int32_t cros_gralloc_driver::lock(buffer_handle_t handle, int32_t acquire_fence, uint32_t map_flags,
247 uint8_t *addr[DRV_MAX_PLANES])
249 int32_t ret = cros_gralloc_sync_wait(acquire_fence);
253 SCOPED_SPIN_LOCK(mutex_);
254 auto hnd = cros_gralloc_convert_handle(handle);
256 cros_gralloc_error("Invalid handle.");
260 auto buffer = get_buffer(hnd);
262 cros_gralloc_error("Invalid Reference.");
266 return buffer->lock(map_flags, addr);
269 int32_t cros_gralloc_driver::unlock(buffer_handle_t handle, int32_t *release_fence)
271 SCOPED_SPIN_LOCK(mutex_);;
273 auto hnd = cros_gralloc_convert_handle(handle);
275 cros_gralloc_error("Invalid handle.");
279 auto buffer = get_buffer(hnd);
281 cros_gralloc_error("Invalid Reference.");
286 * From the ANativeWindow::dequeueBuffer documentation:
288 * "A value of -1 indicates that the caller may access the buffer immediately without
289 * waiting on a fence."
292 return buffer->unlock();
295 int32_t cros_gralloc_driver::get_backing_store(buffer_handle_t handle, uint64_t *out_store)
297 SCOPED_SPIN_LOCK(mutex_);
299 auto hnd = cros_gralloc_convert_handle(handle);
301 cros_gralloc_error("Invalid handle.");
305 auto buffer = get_buffer(hnd);
307 cros_gralloc_error("Invalid Reference.");
311 *out_store = static_cast<uint64_t>(buffer->get_id());
315 cros_gralloc_buffer *cros_gralloc_driver::get_buffer(cros_gralloc_handle_t hnd)
317 /* Assumes driver mutex is held. */
318 if (handles_.count(hnd))
319 return handles_[hnd].first;