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-error-private.h"
38 #include "cairo-image-surface-inline.h"
41 _cairo_drm_surface_init (cairo_drm_surface_t *surface,
42 cairo_format_t format,
43 int width, int height)
46 surface->format = format;
47 surface->width = width;
48 surface->height = height;
51 surface->fallback = NULL;
52 surface->map_count = 0;
56 _cairo_drm_surface_finish (cairo_drm_surface_t *surface)
58 assert (surface->fallback == NULL);
60 if (surface->bo != NULL)
61 cairo_drm_bo_destroy (surface->base.device, surface->bo);
63 return CAIRO_STATUS_SUCCESS;
67 _cairo_drm_surface_get_font_options (void *abstract_surface,
68 cairo_font_options_t *options)
70 _cairo_font_options_init_default (options);
72 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
76 _cairo_drm_surface_get_extents (void *abstract_surface,
77 cairo_rectangle_int_t *rectangle)
79 cairo_drm_surface_t *surface = abstract_surface;
83 rectangle->width = surface->width;
84 rectangle->height = surface->height;
90 cairo_drm_surface_create (cairo_device_t *abstract_device,
91 cairo_format_t format,
92 int width, int height)
94 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
95 cairo_surface_t *surface;
97 if (device != NULL && device->base.status)
99 surface = _cairo_surface_create_in_error (device->base.status);
101 else if (device == NULL ||
102 device->surface.create == NULL ||
103 width == 0 || width > device->max_surface_size ||
104 height == 0 || height > device->max_surface_size)
106 surface = cairo_image_surface_create (format, width, height);
108 else if (device->base.finished)
110 surface = _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
114 surface = device->surface.create (device, format, width, height);
115 if (surface->status == CAIRO_STATUS_INVALID_SIZE)
116 surface = cairo_image_surface_create (format, width, height);
123 cairo_drm_surface_create_for_name (cairo_device_t *abstract_device,
125 cairo_format_t format,
126 int width, int height, int stride)
128 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
129 cairo_surface_t *surface;
131 if (! CAIRO_FORMAT_VALID (format))
132 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
134 if (device != NULL && device->base.status)
136 surface = _cairo_surface_create_in_error (device->base.status);
138 else if (device == NULL || device->surface.create_for_name == NULL)
140 /* XXX invalid device! */
141 surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
143 else if (width == 0 || width > device->max_surface_size ||
144 height == 0 || height > device->max_surface_size)
146 surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
148 else if (device->base.finished)
150 surface = _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
154 surface = device->surface.create_for_name (device,
156 width, height, stride);
161 slim_hidden_def (cairo_drm_surface_create_for_name);
164 cairo_drm_surface_create_from_cacheable_image (cairo_device_t *abstract_device,
165 cairo_surface_t *surface)
167 cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
169 if (surface->status) {
170 surface = _cairo_surface_create_in_error (surface->status);
171 } else if (device != NULL && device->base.status) {
172 surface = _cairo_surface_create_in_error (device->base.status);
173 } else if (device == NULL || device->surface.create_from_cacheable_image == NULL) {
174 /* XXX invalid device! */
175 surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
176 } else if (device->base.finished) {
177 surface = _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
179 surface = device->surface.create_from_cacheable_image (device, surface);
185 static cairo_drm_surface_t *
186 _cairo_surface_as_drm (cairo_surface_t *abstract_surface)
188 if (unlikely (abstract_surface->status))
191 if (abstract_surface->type != CAIRO_SURFACE_TYPE_DRM)
194 return (cairo_drm_surface_t *) abstract_surface;
198 cairo_drm_surface_enable_scan_out (cairo_surface_t *abstract_surface)
200 cairo_drm_surface_t *surface;
201 cairo_drm_device_t *device;
203 surface = _cairo_surface_as_drm (abstract_surface);
204 if (unlikely (surface == NULL))
205 return _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
206 if (unlikely (surface->base.finished))
207 return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
209 device = (cairo_drm_device_t *) surface->base.device;
210 if (device->surface.enable_scan_out == NULL)
211 return CAIRO_STATUS_SUCCESS;
213 if (unlikely (device->base.finished))
214 return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
216 return device->surface.enable_scan_out (abstract_surface);
220 cairo_drm_surface_get_handle (cairo_surface_t *abstract_surface)
222 cairo_drm_surface_t *surface;
224 surface = _cairo_surface_as_drm (abstract_surface);
225 if (surface == NULL) {
226 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
230 return surface->bo->handle;
234 _cairo_drm_surface_flink (void *abstract_surface)
236 cairo_drm_surface_t *surface = abstract_surface;
238 return _cairo_drm_bo_flink ((cairo_drm_device_t *) surface->base.device,
243 cairo_drm_surface_get_name (cairo_surface_t *abstract_surface)
245 cairo_drm_surface_t *surface;
246 cairo_drm_device_t *device;
247 cairo_status_t status;
249 surface = _cairo_surface_as_drm (abstract_surface);
250 if (surface == NULL) {
251 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
255 if (surface->bo->name)
256 return surface->bo->name;
258 device = (cairo_drm_device_t *) surface->base.device;
259 if (device->surface.flink == NULL)
262 status = device->surface.flink (abstract_surface);
264 if (_cairo_status_is_error (status))
265 status = _cairo_surface_set_error (abstract_surface, status);
270 return surface->bo->name;
274 cairo_drm_surface_get_format (cairo_surface_t *abstract_surface)
276 cairo_drm_surface_t *surface;
278 surface = _cairo_surface_as_drm (abstract_surface);
280 return cairo_image_surface_get_format (abstract_surface);
282 return surface->format;
286 cairo_drm_surface_get_width (cairo_surface_t *abstract_surface)
288 cairo_drm_surface_t *surface;
290 surface = _cairo_surface_as_drm (abstract_surface);
292 return cairo_image_surface_get_width (abstract_surface);
294 return surface->width;
298 cairo_drm_surface_get_height (cairo_surface_t *abstract_surface)
300 cairo_drm_surface_t *surface;
302 surface = _cairo_surface_as_drm (abstract_surface);
304 return cairo_image_surface_get_height (abstract_surface);
306 return surface->height;
310 cairo_drm_surface_get_stride (cairo_surface_t *abstract_surface)
312 cairo_drm_surface_t *surface;
314 surface = _cairo_surface_as_drm (abstract_surface);
316 return cairo_image_surface_get_stride (abstract_surface);
318 return surface->stride;
321 /* XXX drm or general surface layer? naming? */
323 cairo_drm_surface_map_to_image (cairo_surface_t *abstract_surface)
325 cairo_drm_surface_t *surface;
326 cairo_drm_device_t *device;
327 cairo_status_t status;
329 if (unlikely (abstract_surface->status))
330 return _cairo_surface_create_in_error (abstract_surface->status);
332 surface = _cairo_surface_as_drm (abstract_surface);
333 if (surface == NULL) {
334 if (_cairo_surface_is_image (abstract_surface))
335 return cairo_surface_reference (abstract_surface);
337 status = _cairo_surface_set_error (abstract_surface,
338 CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
339 return _cairo_surface_create_in_error (status);
342 surface->map_count++;
343 device = (cairo_drm_device_t *) surface->base.device;
344 return cairo_surface_reference (device->surface.map_to_image (surface));
348 cairo_drm_surface_unmap (cairo_surface_t *abstract_surface,
349 cairo_surface_t *image)
351 cairo_drm_surface_t *surface;
353 surface = _cairo_surface_as_drm (abstract_surface);
354 if (surface == NULL) {
355 if (_cairo_surface_is_image (abstract_surface))
356 cairo_surface_destroy (image);
358 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
362 /* XXX assert image belongs to drm */
363 //assert (image == drm->fallback);
364 cairo_surface_destroy (image);
366 assert (surface->map_count > 0);
367 if (--surface->map_count == 0)
368 cairo_surface_flush (&surface->base);