1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Chris Wilson
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.
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
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/
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.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Chris Wilson.
35 #include "cairo-drm-private.h"
37 #include "cairo-device-private.h"
38 #include "cairo-error-private.h"
40 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
43 #include <unistd.h> /* open(), close() */
45 static cairo_drm_device_t *_cairo_drm_known_devices;
46 static cairo_drm_device_t *_cairo_drm_default_device;
49 get_udev_property(struct udev_device *device, const char *name)
51 struct udev_list_entry *entry;
53 udev_list_entry_foreach (entry,
54 udev_device_get_properties_list_entry (device))
56 if (strcmp (udev_list_entry_get_name (entry), name) == 0)
57 return udev_list_entry_get_value (entry);
64 _device_flush (void *abstract_device)
66 cairo_drm_device_t *device = abstract_device;
68 device->device.flush (device);
72 _device_finish (void *abstract_device)
74 cairo_drm_device_t *device = abstract_device;
76 CAIRO_MUTEX_LOCK (_cairo_drm_device_mutex);
77 if (device->prev != NULL)
78 device->prev->next = device->next;
80 _cairo_drm_known_devices = device->next;
81 if (device->next != NULL)
82 device->next->prev = device->prev;
84 CAIRO_MUTEX_UNLOCK (_cairo_drm_device_mutex);
86 if (_cairo_atomic_ptr_cmpxchg (&_cairo_drm_default_device,
89 cairo_device_destroy (&device->base);
94 _device_destroy (void *abstract_device)
96 cairo_drm_device_t *device = abstract_device;
98 device->device.destroy (device);
101 static const cairo_device_backend_t _cairo_drm_device_backend = {
102 CAIRO_DEVICE_TYPE_DRM,
104 NULL, NULL, /* lock, unlock */
112 _cairo_drm_device_init (cairo_drm_device_t *dev,
117 int max_surface_size)
119 assert (CAIRO_MUTEX_IS_LOCKED (_cairo_drm_device_mutex));
121 _cairo_device_init (&dev->base, &_cairo_drm_device_backend);
124 dev->vendor_id = vendor_id;
125 dev->chip_id = chip_id;
128 dev->max_surface_size = max_surface_size;
131 dev->next = _cairo_drm_known_devices;
132 if (_cairo_drm_known_devices != NULL)
133 _cairo_drm_known_devices->prev = dev;
134 _cairo_drm_known_devices = dev;
136 if (_cairo_drm_default_device == NULL)
137 _cairo_drm_default_device = (cairo_drm_device_t *) cairo_device_reference (&dev->base);
143 cairo_drm_device_get (struct udev_device *device)
145 static const struct dri_driver_entry {
148 cairo_drm_device_create_func_t create_func;
150 { 0x8086, 0x29a2, _cairo_drm_i965_device_create }, /* I965_G */
151 { 0x8086, 0x2982, _cairo_drm_i965_device_create }, /* G35_G */
152 { 0x8086, 0x2992, _cairo_drm_i965_device_create }, /* I965_Q */
153 { 0x8086, 0x2972, _cairo_drm_i965_device_create }, /* I946_GZ */
154 { 0x8086, 0x2a02, _cairo_drm_i965_device_create }, /* I965_GM */
155 { 0x8086, 0x2a12, _cairo_drm_i965_device_create }, /* I965_GME */
156 { 0x8086, 0x2e02, _cairo_drm_i965_device_create }, /* IGD_E_G */
157 { 0x8086, 0x2e22, _cairo_drm_i965_device_create }, /* G45_G */
158 { 0x8086, 0x2e12, _cairo_drm_i965_device_create }, /* Q45_G */
159 { 0x8086, 0x2e32, _cairo_drm_i965_device_create }, /* G41_G */
160 { 0x8086, 0x2a42, _cairo_drm_i965_device_create }, /* GM45_GM */
162 { 0x8086, 0x2582, _cairo_drm_i915_device_create }, /* I915_G */
163 { 0x8086, 0x2592, _cairo_drm_i915_device_create }, /* I915_GM */
164 { 0x8086, 0x258a, _cairo_drm_i915_device_create }, /* E7221_G */
165 { 0x8086, 0x2772, _cairo_drm_i915_device_create }, /* I945_G */
166 { 0x8086, 0x27a2, _cairo_drm_i915_device_create }, /* I945_GM */
167 { 0x8086, 0x27ae, _cairo_drm_i915_device_create }, /* I945_GME */
168 { 0x8086, 0x29c2, _cairo_drm_i915_device_create }, /* G33_G */
169 { 0x8086, 0x29b2, _cairo_drm_i915_device_create }, /* Q35_G */
170 { 0x8086, 0x29d2, _cairo_drm_i915_device_create }, /* Q33_G */
171 { 0x8086, 0xa011, _cairo_drm_i915_device_create }, /* IGD_GM */
172 { 0x8086, 0xa001, _cairo_drm_i915_device_create }, /* IGD_G */
176 { 0x8086, ~0, _cairo_drm_intel_device_create },
178 { 0x1002, ~0, _cairo_drm_radeon_device_create },
179 #if CAIRO_HAS_GALLIUM_SURFACE
180 { ~0, ~0, _cairo_drm_gallium_device_create },
184 cairo_drm_device_t *dev;
186 struct udev_device *parent;
188 uint32_t vendor_id, chip_id;
192 devid = udev_device_get_devnum (device);
194 CAIRO_MUTEX_LOCK (_cairo_drm_device_mutex);
195 for (dev = _cairo_drm_known_devices; dev != NULL; dev = dev->next) {
196 if (dev->id == devid) {
197 dev = (cairo_drm_device_t *) cairo_device_reference (&dev->base);
202 parent = udev_device_get_parent (device);
203 pci_id = get_udev_property (parent, "PCI_ID");
204 if (pci_id == NULL || sscanf (pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
205 dev = (cairo_drm_device_t *)
206 _cairo_device_create_in_error (CAIRO_STATUS_DEVICE_ERROR);
210 #if CAIRO_HAS_GALLIUM_SURFACE
211 if (getenv ("CAIRO_GALLIUM_FORCE"))
213 i = ARRAY_LENGTH (driver_map) - 1;
218 for (i = 0; i < ARRAY_LENGTH (driver_map); i++) {
219 if (driver_map[i].vendor_id == ~0U)
222 if (driver_map[i].vendor_id == vendor_id &&
223 (driver_map[i].chip_id == ~0U || driver_map[i].chip_id == chip_id))
227 if (i == ARRAY_LENGTH (driver_map)) {
228 dev = (cairo_drm_device_t *)
229 _cairo_device_create_in_error (CAIRO_STATUS_DEVICE_ERROR);
234 path = udev_device_get_devnode (device);
236 path = "/dev/dri/card0"; /* XXX buggy udev? */
238 fd = open (path, O_RDWR);
240 /* XXX more likely to be a permissions issue... */
241 _cairo_error_throw (CAIRO_STATUS_FILE_NOT_FOUND);
245 dev = driver_map[i].create_func (fd, devid, vendor_id, chip_id);
250 CAIRO_MUTEX_UNLOCK (_cairo_drm_device_mutex);
254 slim_hidden_def (cairo_drm_device_get);
257 cairo_drm_device_get_for_fd (int fd)
261 struct udev_device *device;
262 cairo_device_t *dev = NULL;
264 if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode)) {
265 //_cairo_error_throw (CAIRO_STATUS_INVALID_DEVICE);
266 return _cairo_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
271 device = udev_device_new_from_devnum (udev, 'c', st.st_rdev);
272 if (device != NULL) {
273 dev = cairo_drm_device_get (device);
274 udev_device_unref (device);
281 slim_hidden_def (cairo_drm_device_get_for_fd);
284 cairo_drm_device_default (void)
287 struct udev_enumerate *e;
288 struct udev_list_entry *entry;
291 /* optimistic atomic pointer read */
292 dev = &_cairo_drm_default_device->base;
298 return _cairo_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
300 e = udev_enumerate_new (udev);
301 udev_enumerate_add_match_subsystem (e, "drm");
302 udev_enumerate_scan_devices (e);
303 udev_list_entry_foreach (entry, udev_enumerate_get_list_entry (e)) {
304 struct udev_device *device;
307 udev_device_new_from_syspath (udev,
308 udev_list_entry_get_name (entry));
310 dev = cairo_drm_device_get (device);
312 udev_device_unref (device);
315 if (((cairo_drm_device_t *) dev)->fd == -1) {
316 /* try again, we may find a usable card */
317 cairo_device_destroy (dev);
323 udev_enumerate_unref (e);
326 cairo_device_destroy (dev); /* owned by _cairo_drm_default_device */
329 slim_hidden_def (cairo_drm_device_default);
332 _cairo_drm_device_reset_static_data (void)
334 if (_cairo_drm_default_device != NULL) {
335 cairo_device_t *device = &_cairo_drm_default_device->base;
336 _cairo_drm_default_device = NULL;
337 cairo_device_destroy (device);
342 cairo_drm_device_get_fd (cairo_device_t *abstract_device)
344 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
346 if (device->base.status)
353 _cairo_drm_device_fini (cairo_drm_device_t *device)
355 if (device->fd != -1)
360 cairo_drm_device_throttle (cairo_device_t *abstract_device)
362 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
363 cairo_status_t status;
365 if (unlikely (device->base.status))
368 if (device->device.throttle == NULL)
371 status = device->device.throttle (device);
372 if (unlikely (status))
373 _cairo_status_set_error (&device->base.status, status);
377 _cairo_drm_size_is_valid (cairo_device_t *abstract_device,
378 int width, int height)
380 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
382 if (unlikely (device->base.status))
385 return width <= device->max_surface_size &&
386 height <= device->max_surface_size;