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.
32 #include "cairo-drm-private.h"
33 #include "cairo-drm-radeon-private.h"
35 #include "cairo-default-context-private.h"
36 #include "cairo-error-private.h"
38 /* Basic stub surface for radeon chipsets */
42 typedef struct _radeon_surface {
43 cairo_drm_surface_t base;
46 static inline radeon_device_t *
47 to_radeon_device (cairo_device_t *device)
49 return (radeon_device_t *) device;
52 static inline radeon_bo_t *
53 to_radeon_bo (cairo_drm_bo_t *bo)
55 return (radeon_bo_t *) bo;
58 static cairo_surface_t *
59 radeon_surface_create_similar (void *abstract_surface,
60 cairo_content_t content,
64 return cairo_image_surface_create (_cairo_format_from_content (content),
69 radeon_surface_finish (void *abstract_surface)
71 radeon_surface_t *surface = abstract_surface;
73 return _cairo_drm_surface_finish (&surface->base);
77 radeon_surface_acquire_source_image (void *abstract_surface,
78 cairo_image_surface_t **image_out,
81 radeon_surface_t *surface = abstract_surface;
82 cairo_surface_t *image;
83 cairo_status_t status;
87 if (surface->base.fallback != NULL) {
88 image = surface->base.fallback;
92 image = _cairo_surface_has_snapshot (&surface->base.base,
93 &_cairo_image_surface_backend);
97 if (surface->base.base.backend->flush != NULL) {
98 status = surface->base.base.backend->flush (surface);
99 if (unlikely (status))
103 image = radeon_bo_get_image (to_radeon_device (surface->base.base.device),
104 to_radeon_bo (surface->base.bo),
106 status = image->status;
107 if (unlikely (status))
110 _cairo_surface_attach_snapshot (&surface->base.base, image, cairo_surface_destroy);
113 *image_out = (cairo_image_surface_t *) cairo_surface_reference (image);
115 return CAIRO_STATUS_SUCCESS;
119 radeon_surface_release_source_image (void *abstract_surface,
120 cairo_image_surface_t *image,
123 cairo_surface_destroy (&image->base);
126 static cairo_surface_t *
127 radeon_surface_map_to_image (radeon_surface_t *surface)
129 if (surface->base.fallback == NULL) {
130 cairo_surface_t *image;
131 cairo_status_t status;
134 if (surface->base.base.backend->flush != NULL) {
135 status = surface->base.base.backend->flush (surface);
136 if (unlikely (status))
137 return _cairo_surface_create_in_error (status);
140 ptr = radeon_bo_map (to_radeon_device (surface->base.base.device),
141 to_radeon_bo (surface->base.bo));
142 if (unlikely (ptr == NULL))
143 return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
145 image = cairo_image_surface_create_for_data (ptr,
146 surface->base.format,
148 surface->base.height,
149 surface->base.stride);
150 if (unlikely (image->status)) {
151 radeon_bo_unmap (to_radeon_bo (surface->base.bo));
155 surface->base.fallback = image;
158 return surface->base.fallback;
161 static cairo_status_t
162 radeon_surface_flush (void *abstract_surface,
165 radeon_surface_t *surface = abstract_surface;
166 cairo_status_t status;
169 return CAIRO_STATUS_SUCCESS;
171 if (surface->base.fallback == NULL)
172 return CAIRO_STATUS_SUCCESS;
174 /* kill any outstanding maps */
175 cairo_surface_finish (surface->base.fallback);
177 status = cairo_surface_status (surface->base.fallback);
178 cairo_surface_destroy (surface->base.fallback);
179 surface->base.fallback = NULL;
181 radeon_bo_unmap (to_radeon_bo (surface->base.bo));
186 static cairo_int_status_t
187 radeon_surface_paint (void *abstract_surface,
189 const cairo_pattern_t *source,
192 return _cairo_surface_paint (radeon_surface_map_to_image (abstract_surface),
196 static cairo_int_status_t
197 radeon_surface_mask (void *abstract_surface,
199 const cairo_pattern_t *source,
200 const cairo_pattern_t *mask,
203 return _cairo_surface_mask (radeon_surface_map_to_image (abstract_surface),
204 op, source, mask, clip);
207 static cairo_int_status_t
208 radeon_surface_stroke (void *abstract_surface,
210 const cairo_pattern_t *source,
211 cairo_path_fixed_t *path,
212 const cairo_stroke_style_t *stroke_style,
213 const cairo_matrix_t *ctm,
214 const cairo_matrix_t *ctm_inverse,
216 cairo_antialias_t antialias,
219 return _cairo_surface_stroke (radeon_surface_map_to_image (abstract_surface),
220 op, source, path, stroke_style, ctm, ctm_inverse,
221 tolerance, antialias, clip);
224 static cairo_int_status_t
225 radeon_surface_fill (void *abstract_surface,
227 const cairo_pattern_t *source,
228 cairo_path_fixed_t *path,
229 cairo_fill_rule_t fill_rule,
231 cairo_antialias_t antialias,
234 return _cairo_surface_fill (radeon_surface_map_to_image (abstract_surface),
235 op, source, path, fill_rule,
236 tolerance, antialias, clip);
239 static cairo_int_status_t
240 radeon_surface_glyphs (void *abstract_surface,
242 const cairo_pattern_t *source,
243 cairo_glyph_t *glyphs,
245 cairo_scaled_font_t *scaled_font,
250 return _cairo_surface_show_text_glyphs (radeon_surface_map_to_image (abstract_surface),
258 static const cairo_surface_backend_t radeon_surface_backend = {
259 CAIRO_SURFACE_TYPE_DRM,
260 _cairo_default_context_create,
262 radeon_surface_create_similar,
263 radeon_surface_finish,
266 radeon_surface_acquire_source_image,
267 radeon_surface_release_source_image,
270 NULL, /* composite */
272 NULL, /* trapezoids */
274 NULL, /* check-span */
276 NULL, /* copy_page */
277 NULL, /* show_page */
278 _cairo_drm_surface_get_extents,
279 NULL, /* old-glyphs */
280 _cairo_drm_surface_get_font_options,
282 radeon_surface_flush,
283 NULL, /* mark dirty */
284 NULL, NULL, /* font/glyph fini */
286 radeon_surface_paint,
288 radeon_surface_stroke,
290 radeon_surface_glyphs,
294 radeon_surface_init (radeon_surface_t *surface,
295 cairo_drm_device_t *device,
296 cairo_format_t format,
297 int width, int height)
299 _cairo_surface_init (&surface->base.base,
300 &radeon_surface_backend,
302 _cairo_content_from_format (format));
303 _cairo_drm_surface_init (&surface->base, format, width, height);
306 static cairo_surface_t *
307 radeon_surface_create_internal (cairo_drm_device_t *device,
308 cairo_format_t format,
309 int width, int height)
311 radeon_surface_t *surface;
312 cairo_status_t status;
314 surface = malloc (sizeof (radeon_surface_t));
315 if (unlikely (surface == NULL))
316 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
318 radeon_surface_init (surface, device, format, width, height);
320 if (width && height) {
321 surface->base.stride =
322 cairo_format_stride_for_width (surface->base.format, width);
324 surface->base.bo = radeon_bo_create (to_radeon_device (&device->base),
325 surface->base.stride * height,
326 RADEON_GEM_DOMAIN_GTT);
328 if (unlikely (surface->base.bo == NULL)) {
329 status = _cairo_drm_surface_finish (&surface->base);
331 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
335 return &surface->base.base;
338 static cairo_surface_t *
339 radeon_surface_create (cairo_drm_device_t *device,
340 cairo_format_t format,
341 int width, int height)
345 case CAIRO_FORMAT_INVALID:
346 case CAIRO_FORMAT_A1:
347 case CAIRO_FORMAT_RGB16_565:
348 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
349 case CAIRO_FORMAT_ARGB32:
350 case CAIRO_FORMAT_RGB24:
351 case CAIRO_FORMAT_A8:
355 return radeon_surface_create_internal (device, format, width, height);
358 static cairo_surface_t *
359 radeon_surface_create_for_name (cairo_drm_device_t *device,
361 cairo_format_t format,
362 int width, int height, int stride)
364 radeon_surface_t *surface;
365 cairo_status_t status;
369 case CAIRO_FORMAT_INVALID:
370 case CAIRO_FORMAT_A1:
371 case CAIRO_FORMAT_RGB16_565:
372 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
373 case CAIRO_FORMAT_ARGB32:
374 case CAIRO_FORMAT_RGB24:
375 case CAIRO_FORMAT_A8:
379 if (stride < cairo_format_stride_for_width (format, width))
380 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
382 surface = malloc (sizeof (radeon_surface_t));
383 if (unlikely (surface == NULL))
384 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
386 radeon_surface_init (surface, device, format, width, height);
388 if (width && height) {
389 surface->base.stride = stride;
391 surface->base.bo = radeon_bo_create_for_name (to_radeon_device (&device->base),
394 if (unlikely (surface->base.bo == NULL)) {
395 status = _cairo_drm_surface_finish (&surface->base);
397 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
401 return &surface->base.base;
405 radeon_device_destroy (void *data)
407 radeon_device_t *device = data;
409 radeon_device_fini (device);
415 _cairo_drm_radeon_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
417 radeon_device_t *device;
418 uint64_t gart_size, vram_size;
419 cairo_status_t status;
421 if (! radeon_info (fd, &gart_size, &vram_size))
424 device = malloc (sizeof (radeon_device_t));
426 return _cairo_drm_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
428 status = radeon_device_init (device, fd);
429 if (unlikely (status)) {
431 return _cairo_drm_device_create_in_error (status);
434 device->base.surface.create = radeon_surface_create;
435 device->base.surface.create_for_name = radeon_surface_create_for_name;
436 device->base.surface.create_from_cacheable_image = NULL;
437 device->base.surface.flink = _cairo_drm_surface_flink;
438 device->base.surface.enable_scan_out = NULL;
440 device->base.device.flush = NULL;
441 device->base.device.throttle = NULL;
442 device->base.device.destroy = radeon_device_destroy;
444 device->vram_limit = vram_size;
445 device->gart_limit = gart_size;
447 return _cairo_drm_device_init (&device->base, fd, dev, vendor_id, chip_id, MAX_SIZE);