Tizen 2.0 Release
[framework/graphics/cairo.git] / src / drm / cairo-drm-radeon-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  */
29
30 #include "cairoint.h"
31
32 #include "cairo-drm-private.h"
33 #include "cairo-drm-radeon-private.h"
34
35 #include "cairo-default-context-private.h"
36 #include "cairo-error-private.h"
37
38 /* Basic stub surface for radeon chipsets */
39
40 #define MAX_SIZE 2048
41
42 typedef struct _radeon_surface {
43     cairo_drm_surface_t base;
44 } radeon_surface_t;
45
46 static inline radeon_device_t *
47 to_radeon_device (cairo_device_t *device)
48 {
49     return (radeon_device_t *) device;
50 }
51
52 static inline radeon_bo_t *
53 to_radeon_bo (cairo_drm_bo_t *bo)
54 {
55     return (radeon_bo_t *) bo;
56 }
57
58 static cairo_surface_t *
59 radeon_surface_create_similar (void                     *abstract_surface,
60                               cairo_content_t            content,
61                               int                        width,
62                               int                        height)
63 {
64     return cairo_image_surface_create (_cairo_format_from_content (content),
65                                        width, height);
66 }
67
68 static cairo_status_t
69 radeon_surface_finish (void *abstract_surface)
70 {
71     radeon_surface_t *surface = abstract_surface;
72
73     return _cairo_drm_surface_finish (&surface->base);
74 }
75
76 static cairo_status_t
77 radeon_surface_acquire_source_image (void *abstract_surface,
78                                      cairo_image_surface_t **image_out,
79                                      void **image_extra)
80 {
81     radeon_surface_t *surface = abstract_surface;
82     cairo_surface_t *image;
83     cairo_status_t status;
84
85     /* XXX batch flush */
86
87     if (surface->base.fallback != NULL) {
88         image = surface->base.fallback;
89         goto DONE;
90     }
91
92     image = _cairo_surface_has_snapshot (&surface->base.base,
93                                          &_cairo_image_surface_backend);
94     if (image != NULL)
95         goto DONE;
96
97     if (surface->base.base.backend->flush != NULL) {
98         status = surface->base.base.backend->flush (surface);
99         if (unlikely (status))
100             return status;
101     }
102
103     image = radeon_bo_get_image (to_radeon_device (surface->base.base.device),
104                                 to_radeon_bo (surface->base.bo),
105                                 &surface->base);
106     status = image->status;
107     if (unlikely (status))
108         return status;
109
110     _cairo_surface_attach_snapshot (&surface->base.base, image, cairo_surface_destroy);
111
112 DONE:
113     *image_out = (cairo_image_surface_t *) cairo_surface_reference (image);
114     *image_extra = NULL;
115     return CAIRO_STATUS_SUCCESS;
116 }
117
118 static void
119 radeon_surface_release_source_image (void *abstract_surface,
120                                      cairo_image_surface_t *image,
121                                      void *image_extra)
122 {
123     cairo_surface_destroy (&image->base);
124 }
125
126 static cairo_surface_t *
127 radeon_surface_map_to_image (radeon_surface_t *surface)
128 {
129     if (surface->base.fallback == NULL) {
130         cairo_surface_t *image;
131         cairo_status_t status;
132         void *ptr;
133
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);
138         }
139
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);
144
145         image = cairo_image_surface_create_for_data (ptr,
146                                                      surface->base.format,
147                                                      surface->base.width,
148                                                      surface->base.height,
149                                                      surface->base.stride);
150         if (unlikely (image->status)) {
151             radeon_bo_unmap (to_radeon_bo (surface->base.bo));
152             return image;
153         }
154
155         surface->base.fallback = image;
156     }
157
158     return surface->base.fallback;
159 }
160
161 static cairo_status_t
162 radeon_surface_flush (void *abstract_surface,
163                       unsigned flags)
164 {
165     radeon_surface_t *surface = abstract_surface;
166     cairo_status_t status;
167
168     if (flags)
169         return CAIRO_STATUS_SUCCESS;
170
171     if (surface->base.fallback == NULL)
172         return CAIRO_STATUS_SUCCESS;
173
174     /* kill any outstanding maps */
175     cairo_surface_finish (surface->base.fallback);
176
177     status = cairo_surface_status (surface->base.fallback);
178     cairo_surface_destroy (surface->base.fallback);
179     surface->base.fallback = NULL;
180
181     radeon_bo_unmap (to_radeon_bo (surface->base.bo));
182
183     return status;
184 }
185
186 static cairo_int_status_t
187 radeon_surface_paint (void *abstract_surface,
188                      cairo_operator_t            op,
189                      const cairo_pattern_t      *source,
190                      cairo_clip_t               *clip)
191 {
192     return _cairo_surface_paint (radeon_surface_map_to_image (abstract_surface),
193                                  op, source, clip);
194 }
195
196 static cairo_int_status_t
197 radeon_surface_mask (void                       *abstract_surface,
198                     cairo_operator_t             op,
199                     const cairo_pattern_t       *source,
200                     const cairo_pattern_t       *mask,
201                     cairo_clip_t                *clip)
202 {
203     return _cairo_surface_mask (radeon_surface_map_to_image (abstract_surface),
204                                 op, source, mask, clip);
205 }
206
207 static cairo_int_status_t
208 radeon_surface_stroke (void                     *abstract_surface,
209                       cairo_operator_t           op,
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,
215                       double                     tolerance,
216                       cairo_antialias_t          antialias,
217                       cairo_clip_t              *clip)
218 {
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);
222 }
223
224 static cairo_int_status_t
225 radeon_surface_fill (void                       *abstract_surface,
226                     cairo_operator_t             op,
227                     const cairo_pattern_t       *source,
228                     cairo_path_fixed_t          *path,
229                     cairo_fill_rule_t            fill_rule,
230                     double                       tolerance,
231                     cairo_antialias_t            antialias,
232                     cairo_clip_t                *clip)
233 {
234     return _cairo_surface_fill (radeon_surface_map_to_image (abstract_surface),
235                                 op, source, path, fill_rule,
236                                 tolerance, antialias, clip);
237 }
238
239 static cairo_int_status_t
240 radeon_surface_glyphs (void                     *abstract_surface,
241                       cairo_operator_t           op,
242                       const cairo_pattern_t     *source,
243                       cairo_glyph_t             *glyphs,
244                       int                        num_glyphs,
245                       cairo_scaled_font_t       *scaled_font,
246                       cairo_clip_t              *clip,
247                       int *num_remaining)
248 {
249     *num_remaining = 0;
250     return _cairo_surface_show_text_glyphs (radeon_surface_map_to_image (abstract_surface),
251                                             op, source,
252                                             NULL, 0,
253                                             glyphs, num_glyphs,
254                                             NULL, 0, 0,
255                                             scaled_font, clip);
256 }
257
258 static const cairo_surface_backend_t radeon_surface_backend = {
259     CAIRO_SURFACE_TYPE_DRM,
260     _cairo_default_context_create,
261
262     radeon_surface_create_similar,
263     radeon_surface_finish,
264
265     NULL,
266     radeon_surface_acquire_source_image,
267     radeon_surface_release_source_image,
268
269     NULL, NULL, NULL,
270     NULL, /* composite */
271     NULL, /* fill */
272     NULL, /* trapezoids */
273     NULL, /* span */
274     NULL, /* check-span */
275
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,
281
282     radeon_surface_flush,
283     NULL, /* mark dirty */
284     NULL, NULL, /* font/glyph fini */
285
286     radeon_surface_paint,
287     radeon_surface_mask,
288     radeon_surface_stroke,
289     radeon_surface_fill,
290     radeon_surface_glyphs,
291 };
292
293 static void
294 radeon_surface_init (radeon_surface_t *surface,
295                      cairo_drm_device_t *device,
296                      cairo_format_t format,
297                      int width, int height)
298 {
299     _cairo_surface_init (&surface->base.base,
300                          &radeon_surface_backend,
301                          &device->base,
302                          _cairo_content_from_format (format));
303     _cairo_drm_surface_init (&surface->base, format, width, height);
304 }
305
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)
310 {
311     radeon_surface_t *surface;
312     cairo_status_t status;
313
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));
317
318     radeon_surface_init (surface, device, format, width, height);
319
320     if (width && height) {
321         surface->base.stride =
322             cairo_format_stride_for_width (surface->base.format, width);
323
324         surface->base.bo = radeon_bo_create (to_radeon_device (&device->base),
325                                              surface->base.stride * height,
326                                              RADEON_GEM_DOMAIN_GTT);
327
328         if (unlikely (surface->base.bo == NULL)) {
329             status = _cairo_drm_surface_finish (&surface->base);
330             free (surface);
331             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
332         }
333     }
334
335     return &surface->base.base;
336 }
337
338 static cairo_surface_t *
339 radeon_surface_create (cairo_drm_device_t *device,
340                        cairo_format_t format,
341                        int width, int height)
342 {
343     switch (format) {
344     default:
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:
352         break;
353     }
354
355     return radeon_surface_create_internal (device, format, width, height);
356 }
357
358 static cairo_surface_t *
359 radeon_surface_create_for_name (cairo_drm_device_t *device,
360                               unsigned int name,
361                               cairo_format_t format,
362                               int width, int height, int stride)
363 {
364     radeon_surface_t *surface;
365     cairo_status_t status;
366
367     switch (format) {
368     default:
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:
376         break;
377     }
378
379     if (stride < cairo_format_stride_for_width (format, width))
380         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
381
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));
385
386     radeon_surface_init (surface, device, format, width, height);
387
388     if (width && height) {
389         surface->base.stride = stride;
390
391         surface->base.bo = radeon_bo_create_for_name (to_radeon_device (&device->base),
392                                                       name);
393
394         if (unlikely (surface->base.bo == NULL)) {
395             status = _cairo_drm_surface_finish (&surface->base);
396             free (surface);
397             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
398         }
399     }
400
401     return &surface->base.base;
402 }
403
404 static void
405 radeon_device_destroy (void *data)
406 {
407     radeon_device_t *device = data;
408
409     radeon_device_fini (device);
410
411     free (data);
412 }
413
414 cairo_drm_device_t *
415 _cairo_drm_radeon_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
416 {
417     radeon_device_t *device;
418     uint64_t gart_size, vram_size;
419     cairo_status_t status;
420
421     if (! radeon_info (fd, &gart_size, &vram_size))
422         return NULL;
423
424     device = malloc (sizeof (radeon_device_t));
425     if (device == NULL)
426         return _cairo_drm_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
427
428     status = radeon_device_init (device, fd);
429     if (unlikely (status)) {
430         free (device);
431         return _cairo_drm_device_create_in_error (status);
432     }
433
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;
439
440     device->base.device.flush = NULL;
441     device->base.device.throttle = NULL;
442     device->base.device.destroy = radeon_device_destroy;
443
444     device->vram_limit = vram_size;
445     device->gart_limit = gart_size;
446
447     return _cairo_drm_device_init (&device->base, fd, dev, vendor_id, chip_id, MAX_SIZE);
448 }