Upload Tizen2.0 source
[framework/graphics/cairo.git] / src / drm / cairo-drm-surface.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  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Chris Wilson.
31  */
32
33 #include "cairoint.h"
34
35 #include "cairo-drm-private.h"
36
37 #include "cairo-error-private.h"
38 #include "cairo-image-surface-inline.h"
39
40 void
41 _cairo_drm_surface_init (cairo_drm_surface_t *surface,
42                          cairo_format_t format,
43                          int width, int height)
44 {
45     surface->bo = NULL;
46     surface->format = format;
47     surface->width  = width;
48     surface->height = height;
49     surface->stride = 0;
50
51     surface->fallback = NULL;
52     surface->map_count = 0;
53 }
54
55 cairo_status_t
56 _cairo_drm_surface_finish (cairo_drm_surface_t *surface)
57 {
58     assert (surface->fallback == NULL);
59
60     if (surface->bo != NULL)
61         cairo_drm_bo_destroy (surface->base.device, surface->bo);
62
63     return CAIRO_STATUS_SUCCESS;
64 }
65
66 void
67 _cairo_drm_surface_get_font_options (void                  *abstract_surface,
68                                      cairo_font_options_t  *options)
69 {
70     _cairo_font_options_init_default (options);
71
72     cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
73 }
74
75 cairo_bool_t
76 _cairo_drm_surface_get_extents (void *abstract_surface,
77                                 cairo_rectangle_int_t *rectangle)
78 {
79     cairo_drm_surface_t *surface = abstract_surface;
80
81     rectangle->x = 0;
82     rectangle->y = 0;
83     rectangle->width  = surface->width;
84     rectangle->height = surface->height;
85
86     return TRUE;
87 }
88
89 cairo_surface_t *
90 cairo_drm_surface_create (cairo_device_t *abstract_device,
91                           cairo_format_t format,
92                           int width, int height)
93 {
94     cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
95     cairo_surface_t *surface;
96
97     if (device != NULL && device->base.status)
98     {
99         surface = _cairo_surface_create_in_error (device->base.status);
100     }
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)
105     {
106         surface = cairo_image_surface_create (format, width, height);
107     }
108     else if (device->base.finished)
109     {
110         surface = _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
111     }
112     else
113     {
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);
117     }
118
119     return surface;
120 }
121
122 cairo_surface_t *
123 cairo_drm_surface_create_for_name (cairo_device_t *abstract_device,
124                                    unsigned int name,
125                                    cairo_format_t format,
126                                    int width, int height, int stride)
127 {
128     cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
129     cairo_surface_t *surface;
130
131     if (! CAIRO_FORMAT_VALID (format))
132         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
133
134     if (device != NULL && device->base.status)
135     {
136         surface = _cairo_surface_create_in_error (device->base.status);
137     }
138     else if (device == NULL || device->surface.create_for_name == NULL)
139     {
140         /* XXX invalid device! */
141         surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
142     }
143     else if (width == 0 || width > device->max_surface_size ||
144              height == 0 || height > device->max_surface_size)
145     {
146         surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
147     }
148     else if (device->base.finished)
149     {
150         surface = _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
151     }
152     else
153     {
154         surface = device->surface.create_for_name (device,
155                                                      name, format,
156                                                      width, height, stride);
157     }
158
159     return surface;
160 }
161 slim_hidden_def (cairo_drm_surface_create_for_name);
162
163 cairo_surface_t *
164 cairo_drm_surface_create_from_cacheable_image (cairo_device_t *abstract_device,
165                                                cairo_surface_t *surface)
166 {
167     cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
168
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);
178     } else {
179         surface = device->surface.create_from_cacheable_image (device, surface);
180     }
181
182     return surface;
183 }
184
185 static cairo_drm_surface_t *
186 _cairo_surface_as_drm (cairo_surface_t *abstract_surface)
187 {
188     if (unlikely (abstract_surface->status))
189         return NULL;
190
191     if (abstract_surface->type != CAIRO_SURFACE_TYPE_DRM)
192         return NULL;
193
194     return (cairo_drm_surface_t *) abstract_surface;
195 }
196
197 cairo_status_t
198 cairo_drm_surface_enable_scan_out (cairo_surface_t *abstract_surface)
199 {
200     cairo_drm_surface_t *surface;
201     cairo_drm_device_t *device;
202
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);
208
209     device = (cairo_drm_device_t *) surface->base.device;
210     if (device->surface.enable_scan_out == NULL)
211         return CAIRO_STATUS_SUCCESS;
212
213     if (unlikely (device->base.finished))
214         return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
215
216     return device->surface.enable_scan_out (abstract_surface);
217 }
218
219 unsigned int
220 cairo_drm_surface_get_handle (cairo_surface_t *abstract_surface)
221 {
222     cairo_drm_surface_t *surface;
223
224     surface = _cairo_surface_as_drm (abstract_surface);
225     if (surface == NULL) {
226         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
227         return 0;
228     }
229
230     return surface->bo->handle;
231 }
232
233 cairo_int_status_t
234 _cairo_drm_surface_flink (void *abstract_surface)
235 {
236     cairo_drm_surface_t *surface = abstract_surface;
237
238     return _cairo_drm_bo_flink ((cairo_drm_device_t *) surface->base.device,
239                                 surface->bo);
240 }
241
242 unsigned int
243 cairo_drm_surface_get_name (cairo_surface_t *abstract_surface)
244 {
245     cairo_drm_surface_t *surface;
246     cairo_drm_device_t *device;
247     cairo_status_t status;
248
249     surface = _cairo_surface_as_drm (abstract_surface);
250     if (surface == NULL) {
251         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
252         return 0;
253     }
254
255     if (surface->bo->name)
256         return surface->bo->name;
257
258     device = (cairo_drm_device_t *) surface->base.device;
259     if (device->surface.flink == NULL)
260         return 0;
261
262     status = device->surface.flink (abstract_surface);
263     if (status) {
264         if (_cairo_status_is_error (status))
265             status = _cairo_surface_set_error (abstract_surface, status);
266
267         return 0;
268     }
269
270     return surface->bo->name;
271 }
272
273 cairo_format_t
274 cairo_drm_surface_get_format (cairo_surface_t *abstract_surface)
275 {
276     cairo_drm_surface_t *surface;
277
278     surface = _cairo_surface_as_drm (abstract_surface);
279     if (surface == NULL)
280         return cairo_image_surface_get_format (abstract_surface);
281
282     return surface->format;
283 }
284
285 int
286 cairo_drm_surface_get_width (cairo_surface_t *abstract_surface)
287 {
288     cairo_drm_surface_t *surface;
289
290     surface = _cairo_surface_as_drm (abstract_surface);
291     if (surface == NULL)
292         return cairo_image_surface_get_width (abstract_surface);
293
294     return surface->width;
295 }
296
297 int
298 cairo_drm_surface_get_height (cairo_surface_t *abstract_surface)
299 {
300     cairo_drm_surface_t *surface;
301
302     surface = _cairo_surface_as_drm (abstract_surface);
303     if (surface == NULL)
304         return cairo_image_surface_get_height (abstract_surface);
305
306     return surface->height;
307 }
308
309 int
310 cairo_drm_surface_get_stride (cairo_surface_t *abstract_surface)
311 {
312     cairo_drm_surface_t *surface;
313
314     surface = _cairo_surface_as_drm (abstract_surface);
315     if (surface == NULL)
316         return cairo_image_surface_get_stride (abstract_surface);
317
318     return surface->stride;
319 }
320
321 /* XXX drm or general surface layer? naming? */
322 cairo_surface_t *
323 cairo_drm_surface_map_to_image (cairo_surface_t *abstract_surface)
324 {
325     cairo_drm_surface_t *surface;
326     cairo_drm_device_t *device;
327     cairo_status_t status;
328
329     if (unlikely (abstract_surface->status))
330         return _cairo_surface_create_in_error (abstract_surface->status);
331
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);
336
337         status = _cairo_surface_set_error (abstract_surface,
338                                            CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
339         return _cairo_surface_create_in_error (status);
340     }
341
342     surface->map_count++;
343     device = (cairo_drm_device_t *) surface->base.device;
344     return cairo_surface_reference (device->surface.map_to_image (surface));
345 }
346
347 void
348 cairo_drm_surface_unmap (cairo_surface_t *abstract_surface,
349                          cairo_surface_t *image)
350 {
351     cairo_drm_surface_t *surface;
352
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);
357         else
358             _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
359         return;
360     }
361
362     /* XXX assert image belongs to drm */
363     //assert (image == drm->fallback);
364     cairo_surface_destroy (image);
365
366     assert (surface->map_count > 0);
367     if (--surface->map_count == 0)
368         cairo_surface_flush (&surface->base);
369 }