Tizen 2.0 Release
[framework/graphics/cairo.git] / src / drm / cairo-drm-bo.c
1 /* Cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Chris Wilson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  */
29
30 #include "cairoint.h"
31
32 #include "cairo-drm-private.h"
33 #include "cairo-drm-ioctl-private.h"
34
35 #include "cairo-error-private.h"
36
37 #include <sys/ioctl.h>
38 #include <errno.h>
39
40 #define ERR_DEBUG(x) x
41
42 struct drm_gem_close {
43         /** Handle of the object to be closed. */
44         uint32_t handle;
45         uint32_t pad;
46 };
47
48 struct drm_gem_flink {
49         /** Handle for the object being named */
50         uint32_t handle;
51
52         /** Returned global name */
53         uint32_t name;
54 };
55
56 struct drm_gem_open {
57         /** Name of object being opened */
58         uint32_t name;
59
60         /** Returned handle for the object */
61         uint32_t handle;
62
63         /** Returned size of the object */
64         uint64_t size;
65 };
66
67 #define DRM_IOCTL_GEM_CLOSE             DRM_IOW (0x09, struct drm_gem_close)
68 #define DRM_IOCTL_GEM_FLINK             DRM_IOWR(0x0a, struct drm_gem_flink)
69 #define DRM_IOCTL_GEM_OPEN              DRM_IOWR(0x0b, struct drm_gem_open)
70
71 cairo_status_t
72 _cairo_drm_bo_open_for_name (const cairo_drm_device_t *dev,
73                              cairo_drm_bo_t *bo,
74                              uint32_t name)
75 {
76     struct drm_gem_open open;
77     int ret;
78
79     open.name = name;
80     open.handle = 0;
81     open.size = 0;
82     do {
83         ret = ioctl (dev->fd, DRM_IOCTL_GEM_OPEN, &open);
84     } while (ret == -1 && errno == EINTR);
85     if (ret == -1) {
86         ERR_DEBUG((fprintf (stderr, "Failed to open bo for name %d: %s\n",
87                             name, strerror (errno))));
88         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
89     }
90
91     bo->name = name;
92     bo->size = open.size;
93     bo->handle = open.handle;
94
95     return CAIRO_STATUS_SUCCESS;
96 }
97
98 cairo_status_t
99 _cairo_drm_bo_flink (const cairo_drm_device_t *dev,
100                      cairo_drm_bo_t *bo)
101 {
102     struct drm_gem_flink flink;
103     int ret;
104
105     memset (&flink, 0, sizeof (flink));
106     flink.handle = bo->handle;
107     ret = ioctl (dev->fd, DRM_IOCTL_GEM_FLINK, &flink);
108     if (ret == -1) {
109         ERR_DEBUG((fprintf (stderr, "Failed to flink bo: %s\n",
110                             strerror (errno))));
111         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
112     }
113
114     bo->name = flink.name;
115
116     return CAIRO_STATUS_SUCCESS;
117 }
118
119 void
120 _cairo_drm_bo_close (const cairo_drm_device_t *dev,
121                      cairo_drm_bo_t *bo)
122 {
123     struct drm_gem_close close;
124     int ret;
125
126     close.handle = bo->handle;
127     do {
128         ret = ioctl (dev->fd, DRM_IOCTL_GEM_CLOSE, &close);
129     } while (ret == -1 && errno == EINTR);
130 }