blob: ebff717d884182220edc33372e3fc09d00c464a5 [file] [log] [blame]
Chia-I Wu2ec32d42011-06-12 16:21:30 +08001/*
2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#define LOG_TAG "GRALLOC-DRM"
25
26#include <cutils/log.h>
27#include <cutils/atomic.h>
Sean Paul879cc4e2015-02-09 02:39:09 -050028#include <cutils/properties.h>
Chia-I Wu2ec32d42011-06-12 16:21:30 +080029#include <stdlib.h>
30#include <errno.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34
35#include "gralloc_drm.h"
36#include "gralloc_drm_priv.h"
37
38#define unlikely(x) __builtin_expect(!!(x), 0)
39
Chia-I Wu2ec32d42011-06-12 16:21:30 +080040static int32_t gralloc_drm_pid = 0;
41
42/*
43 * Return the pid of the process.
44 */
45static int gralloc_drm_get_pid(void)
46{
47 if (unlikely(!gralloc_drm_pid))
48 android_atomic_write((int32_t) getpid(), &gralloc_drm_pid);
49
50 return gralloc_drm_pid;
51}
52
53/*
54 * Create the driver for a DRM fd.
55 */
56static struct gralloc_drm_drv_t *
57init_drv_from_fd(int fd)
58{
59 struct gralloc_drm_drv_t *drv = NULL;
60 drmVersionPtr version;
61
62 /* get the kernel module name */
63 version = drmGetVersion(fd);
64 if (!version) {
Charles Johnsonb56dc922012-07-10 17:51:32 -070065 ALOGE("invalid DRM fd");
Chia-I Wu2ec32d42011-06-12 16:21:30 +080066 return NULL;
67 }
68
69 if (version->name) {
Chia-I Wua020bfa2011-06-13 08:48:44 +080070#ifdef ENABLE_PIPE
71 drv = gralloc_drm_drv_create_for_pipe(fd, version->name);
72#endif
73
Chia-I Wu2ec32d42011-06-12 16:21:30 +080074#ifdef ENABLE_INTEL
75 if (!drv && !strcmp(version->name, "i915"))
76 drv = gralloc_drm_drv_create_for_intel(fd);
77#endif
78#ifdef ENABLE_RADEON
79 if (!drv && !strcmp(version->name, "radeon"))
80 drv = gralloc_drm_drv_create_for_radeon(fd);
81#endif
Chia-I Wu64345b42011-06-12 18:43:33 +080082#ifdef ENABLE_NOUVEAU
83 if (!drv && !strcmp(version->name, "nouveau"))
84 drv = gralloc_drm_drv_create_for_nouveau(fd);
85#endif
Chia-I Wu2ec32d42011-06-12 16:21:30 +080086 }
87
88 if (!drv) {
Charles Johnsonb56dc922012-07-10 17:51:32 -070089 ALOGE("unsupported driver: %s", (version->name) ?
Chia-I Wu2ec32d42011-06-12 16:21:30 +080090 version->name : "NULL");
91 }
92
93 drmFreeVersion(version);
94
95 return drv;
96}
97
98/*
99 * Create a DRM device object.
100 */
101struct gralloc_drm_t *gralloc_drm_create(void)
102{
Sean Paul879cc4e2015-02-09 02:39:09 -0500103 char path[PROPERTY_VALUE_MAX];
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800104 struct gralloc_drm_t *drm;
105 int err;
106
Sean Pauld225ab02015-02-09 02:31:58 -0500107 drm = new gralloc_drm_t;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800108 if (!drm)
109 return NULL;
110
Sean Paul879cc4e2015-02-09 02:39:09 -0500111 property_get("gralloc.drm.device", path, "/dev/dri/renderD128");
112 drm->fd = open(path, O_RDWR);
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800113 if (drm->fd < 0) {
Sean Paul879cc4e2015-02-09 02:39:09 -0500114 ALOGE("failed to open %s", path);
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800115 return NULL;
116 }
117
118 drm->drv = init_drv_from_fd(drm->fd);
119 if (!drm->drv) {
120 close(drm->fd);
Sean Pauld225ab02015-02-09 02:31:58 -0500121 delete drm;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800122 return NULL;
123 }
124
125 return drm;
126}
127
128/*
129 * Destroy a DRM device object.
130 */
131void gralloc_drm_destroy(struct gralloc_drm_t *drm)
132{
133 if (drm->drv)
134 drm->drv->destroy(drm->drv);
135 close(drm->fd);
Sean Pauld225ab02015-02-09 02:31:58 -0500136 delete drm;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800137}
138
139/*
140 * Get the file descriptor of a DRM device object.
141 */
142int gralloc_drm_get_fd(struct gralloc_drm_t *drm)
143{
144 return drm->fd;
145}
146
147/*
Chia-I Wu8542de32011-07-31 16:35:21 +0900148 * Validate a buffer handle and return the associated bo.
149 */
150static struct gralloc_drm_bo_t *validate_handle(buffer_handle_t _handle,
151 struct gralloc_drm_t *drm)
152{
153 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
154
155 if (!handle)
156 return NULL;
157
158 /* the buffer handle is passed to a new process */
Sean Paul879cc4e2015-02-09 02:39:09 -0500159 ALOGE("data_owner=%d gralloc_pid=%d data=%p\n", handle->data_owner, gralloc_drm_get_pid(), handle->data);
Chia-I Wu8542de32011-07-31 16:35:21 +0900160 if (unlikely(handle->data_owner != gralloc_drm_pid)) {
161 struct gralloc_drm_bo_t *bo;
162
163 /* check only */
164 if (!drm)
165 return NULL;
166
Sean Paul879cc4e2015-02-09 02:39:09 -0500167 ALOGE("handle: name=%d pfd=%d\n", handle->name,
168 handle->prime_fd);
Chia-I Wu8542de32011-07-31 16:35:21 +0900169 /* create the struct gralloc_drm_bo_t locally */
Sean Paul879cc4e2015-02-09 02:39:09 -0500170 if (handle->name || handle->prime_fd >= 0)
Chia-I Wu8542de32011-07-31 16:35:21 +0900171 bo = drm->drv->alloc(drm->drv, handle);
172 else /* an invalid handle */
173 bo = NULL;
174 if (bo) {
175 bo->drm = drm;
176 bo->imported = 1;
177 bo->handle = handle;
Andy Ross8ab337a2013-06-04 11:32:29 -0700178 bo->refcount = 1;
Chia-I Wu8542de32011-07-31 16:35:21 +0900179 }
180
181 handle->data_owner = gralloc_drm_get_pid();
Chih-Wei Huang68a74eb2014-12-01 01:46:20 +0800182 handle->data = bo;
Chia-I Wu8542de32011-07-31 16:35:21 +0900183 }
184
Chih-Wei Huang68a74eb2014-12-01 01:46:20 +0800185 return handle->data;
Chia-I Wu8542de32011-07-31 16:35:21 +0900186}
187
188/*
189 * Register a buffer handle.
190 */
191int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm)
192{
193 return (validate_handle(handle, drm)) ? 0 : -EINVAL;
194}
195
196/*
197 * Unregister a buffer handle. It is no-op for handles created locally.
198 */
199int gralloc_drm_handle_unregister(buffer_handle_t handle)
200{
201 struct gralloc_drm_bo_t *bo;
202
203 bo = validate_handle(handle, NULL);
204 if (!bo)
205 return -EINVAL;
206
207 if (bo->imported)
Tapani Pällia86ecd92012-08-01 16:06:00 +0300208 gralloc_drm_bo_decref(bo);
Chia-I Wu8542de32011-07-31 16:35:21 +0900209
210 return 0;
211}
212
213/*
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800214 * Create a buffer handle.
215 */
216static struct gralloc_drm_handle_t *create_bo_handle(int width,
217 int height, int format, int usage)
218{
219 struct gralloc_drm_handle_t *handle;
220
Sean Pauld225ab02015-02-09 02:31:58 -0500221 handle = new gralloc_drm_handle_t;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800222 if (!handle)
223 return NULL;
224
225 handle->base.version = sizeof(handle->base);
226 handle->base.numInts = GRALLOC_DRM_HANDLE_NUM_INTS;
227 handle->base.numFds = GRALLOC_DRM_HANDLE_NUM_FDS;
228
229 handle->magic = GRALLOC_DRM_HANDLE_MAGIC;
230 handle->width = width;
231 handle->height = height;
232 handle->format = format;
233 handle->usage = usage;
Sean Paul879cc4e2015-02-09 02:39:09 -0500234 handle->prime_fd = -1;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800235
236 return handle;
237}
238
239/*
240 * Create a bo.
241 */
242struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm,
243 int width, int height, int format, int usage)
244{
245 struct gralloc_drm_bo_t *bo;
246 struct gralloc_drm_handle_t *handle;
247
248 handle = create_bo_handle(width, height, format, usage);
249 if (!handle)
250 return NULL;
251
252 bo = drm->drv->alloc(drm->drv, handle);
253 if (!bo) {
Sean Pauld225ab02015-02-09 02:31:58 -0500254 delete handle;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800255 return NULL;
256 }
257
258 bo->drm = drm;
259 bo->imported = 0;
260 bo->handle = handle;
Tapani Pällia86ecd92012-08-01 16:06:00 +0300261 bo->fb_id = 0;
262 bo->refcount = 1;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800263
264 handle->data_owner = gralloc_drm_get_pid();
Chih-Wei Huang68a74eb2014-12-01 01:46:20 +0800265 handle->data = bo;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800266
267 return bo;
268}
269
270/*
Chia-I Wu2fc5da42011-07-29 19:57:04 +0900271 * Destroy a bo.
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800272 */
Tapani Pällia86ecd92012-08-01 16:06:00 +0300273static void gralloc_drm_bo_destroy(struct gralloc_drm_bo_t *bo)
Chia-I Wu2fc5da42011-07-29 19:57:04 +0900274{
275 struct gralloc_drm_handle_t *handle = bo->handle;
276 int imported = bo->imported;
277
Tapani Pällia86ecd92012-08-01 16:06:00 +0300278 /* gralloc still has a reference */
279 if (bo->refcount)
280 return;
281
Chia-I Wu2fc5da42011-07-29 19:57:04 +0900282 bo->drm->drv->free(bo->drm->drv, bo);
283 if (imported) {
284 handle->data_owner = 0;
285 handle->data = 0;
286 }
287 else {
Sean Pauld225ab02015-02-09 02:31:58 -0500288 delete handle;
Chia-I Wu2fc5da42011-07-29 19:57:04 +0900289 }
290}
291
292/*
Tapani Pällia86ecd92012-08-01 16:06:00 +0300293 * Decrease refcount, if no refs anymore then destroy.
294 */
295void gralloc_drm_bo_decref(struct gralloc_drm_bo_t *bo)
296{
297 if (!--bo->refcount)
298 gralloc_drm_bo_destroy(bo);
299}
300
301/*
Chia-I Wu8542de32011-07-31 16:35:21 +0900302 * Return the bo of a registered handle.
Chia-I Wu2fc5da42011-07-29 19:57:04 +0900303 */
Chia-I Wu8542de32011-07-31 16:35:21 +0900304struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800305{
Chia-I Wu8542de32011-07-31 16:35:21 +0900306 return validate_handle(handle, NULL);
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800307}
308
309/*
Chia-I Wu8542de32011-07-31 16:35:21 +0900310 * Get the buffer handle and stride of a bo.
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800311 */
Chia-I Wu8542de32011-07-31 16:35:21 +0900312buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800313{
Chia-I Wu8542de32011-07-31 16:35:21 +0900314 if (stride)
315 *stride = bo->handle->stride;
316 return &bo->handle->base;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800317}
318
319/*
Tapani Pällia8f03342013-01-18 15:01:43 +0200320 * Query YUV component offsets for a buffer handle
321 */
322void gralloc_drm_resolve_format(buffer_handle_t _handle,
323 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
324{
325 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
Chih-Wei Huang68a74eb2014-12-01 01:46:20 +0800326 struct gralloc_drm_bo_t *bo = handle->data;
Tapani Pällia8f03342013-01-18 15:01:43 +0200327 struct gralloc_drm_t *drm = bo->drm;
328
329 /* if handle exists and driver implements resolve_format */
330 if (handle && drm->drv->resolve_format)
331 drm->drv->resolve_format(drm->drv, bo,
332 pitches, offsets, handles);
333}
334
335/*
Chia-I Wu25e04132011-07-29 20:43:12 +0900336 * Lock a bo. XXX thread-safety?
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800337 */
Chia-I Wu25e04132011-07-29 20:43:12 +0900338int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo,
339 int usage, int x, int y, int w, int h,
340 void **addr)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800341{
Chia-I Wu92eac932011-07-30 16:29:54 +0900342 if ((bo->handle->usage & usage) != usage) {
343 /* make FB special for testing software renderer with */
Adrian Marius Negreanu65a831d2013-01-18 15:12:19 +0200344
345 if (!(bo->handle->usage & GRALLOC_USAGE_HW_FB)
346 && !(bo->handle->usage & GRALLOC_USAGE_HW_TEXTURE)) {
347 ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_FB or GRALLOC_USAGE_HW_TEXTURE"
348 ,bo->handle->usage,usage);
Chia-I Wu92eac932011-07-30 16:29:54 +0900349 return -EINVAL;
Adrian Marius Negreanu65a831d2013-01-18 15:12:19 +0200350 }
Chia-I Wu92eac932011-07-30 16:29:54 +0900351 }
Chia-I Wu25e04132011-07-29 20:43:12 +0900352
353 /* allow multiple locks with compatible usages */
354 if (bo->lock_count && (bo->locked_for & usage) != usage)
355 return -EINVAL;
356
357 usage |= bo->locked_for;
358
359 if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
360 GRALLOC_USAGE_SW_READ_MASK)) {
361 /* the driver is supposed to wait for the bo */
362 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
363 int err = bo->drm->drv->map(bo->drm->drv, bo,
364 x, y, w, h, write, addr);
365 if (err)
366 return err;
367 }
368 else {
369 /* kernel handles the synchronization here */
370 }
371
372 bo->lock_count++;
373 bo->locked_for |= usage;
374
375 return 0;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800376}
377
378/*
Chia-I Wu25e04132011-07-29 20:43:12 +0900379 * Unlock a bo.
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800380 */
Chia-I Wu25e04132011-07-29 20:43:12 +0900381void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo)
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800382{
Chia-I Wu25e04132011-07-29 20:43:12 +0900383 int mapped = bo->locked_for &
384 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
385
386 if (!bo->lock_count)
387 return;
388
389 if (mapped)
390 bo->drm->drv->unmap(bo->drm->drv, bo);
391
392 bo->lock_count--;
393 if (!bo->lock_count)
394 bo->locked_for = 0;
Chia-I Wu2ec32d42011-06-12 16:21:30 +0800395}