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 #include "cairo-drm-private.h"
32 #include "cairo-error-private.h"
33 
34 #include <sys/ioctl.h>
35 #include <errno.h>
36 #include <libdrm/drm.h>
37 
38 #define ERR_DEBUG(x) x
39 
40 cairo_status_t
_cairo_drm_bo_open_for_name(const cairo_drm_device_t * dev,cairo_drm_bo_t * bo,uint32_t name)41 _cairo_drm_bo_open_for_name (const cairo_drm_device_t *dev,
42 			     cairo_drm_bo_t *bo,
43 			     uint32_t name)
44 {
45     struct drm_gem_open open;
46     int ret;
47 
48     open.name = name;
49     open.handle = 0;
50     open.size = 0;
51     do {
52 	ret = ioctl (dev->fd, DRM_IOCTL_GEM_OPEN, &open);
53     } while (ret == -1 && errno == EINTR);
54     if (ret == -1) {
55 	ERR_DEBUG((fprintf (stderr, "Failed to open bo for name %d: %s\n",
56 			    name, strerror (errno))));
57 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
58     }
59 
60     bo->name = name;
61     bo->size = open.size;
62     bo->handle = open.handle;
63 
64     return CAIRO_STATUS_SUCCESS;
65 }
66 
67 cairo_status_t
_cairo_drm_bo_flink(const cairo_drm_device_t * dev,cairo_drm_bo_t * bo)68 _cairo_drm_bo_flink (const cairo_drm_device_t *dev,
69 		     cairo_drm_bo_t *bo)
70 {
71     struct drm_gem_flink flink;
72     int ret;
73 
74     memset (&flink, 0, sizeof (flink));
75     flink.handle = bo->handle;
76     ret = ioctl (dev->fd, DRM_IOCTL_GEM_FLINK, &flink);
77     if (ret == -1) {
78 	ERR_DEBUG((fprintf (stderr, "Failed to flink bo: %s\n",
79 			    strerror (errno))));
80 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
81     }
82 
83     bo->name = flink.name;
84 
85     return CAIRO_STATUS_SUCCESS;
86 }
87 
88 void
_cairo_drm_bo_close(const cairo_drm_device_t * dev,cairo_drm_bo_t * bo)89 _cairo_drm_bo_close (const cairo_drm_device_t *dev,
90 		     cairo_drm_bo_t *bo)
91 {
92     struct drm_gem_close close;
93     int ret;
94 
95     close.handle = bo->handle;
96     do {
97 	ret = ioctl (dev->fd, DRM_IOCTL_GEM_CLOSE, &close);
98     } while (ret == -1 && errno == EINTR);
99 }
100