1 /* Cairo - a vector graphics library with display and print output
2 *
3 * Copyright © 2009 Chris Wilson
4 * Copyright © 2009 Eric Anholt
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 * The Original Code is the cairo graphics library.
30 *
31 * The Initial Developer of the Original Code is Chris Wilson.
32 */
33
34 #include "cairoint.h"
35
36 #include "cairo-drm-private.h"
37 #include "cairo-default-context-private.h"
38 #include "cairo-error-private.h"
39
40 #include <dlfcn.h>
41
42 #include <state_tracker/drm_api.h>
43 #include <pipe/p_format.h>
44 #include <pipe/p_screen.h>
45 #include <pipe/p_context.h>
46 #include <pipe/p_state.h>
47
48 #include <util/u_inlines.h>
49
50 typedef struct _gallium_surface gallium_surface_t;
51 typedef struct _gallium_device gallium_device_t;
52
53 struct _gallium_device {
54 cairo_drm_device_t drm;
55
56 void *dlhandle;
57 struct drm_api *api;
58
59 struct pipe_screen *screen;
60 struct pipe_context *pipe;
61
62 int max_size;
63 };
64
65 struct _gallium_surface {
66 cairo_drm_surface_t drm;
67
68 enum pipe_format pipe_format;
69
70 struct pipe_resource *texture;
71 struct pipe_transfer *map_transfer;
72
73 cairo_surface_t *fallback;
74 };
75
76 static cairo_surface_t *
77 gallium_surface_create_internal (gallium_device_t *device,
78 enum pipe_format format,
79 int width, int height);
80
81 static inline gallium_device_t *
gallium_device(gallium_surface_t * surface)82 gallium_device (gallium_surface_t *surface)
83 {
84 return (gallium_device_t *) surface->drm.base.device;
85 }
86
87 static cairo_format_t
_cairo_format_from_pipe_format(enum pipe_format format)88 _cairo_format_from_pipe_format (enum pipe_format format)
89 {
90 switch ((int) format) {
91 case PIPE_FORMAT_A8_UNORM:
92 return CAIRO_FORMAT_A8;
93 case PIPE_FORMAT_A8R8G8B8_UNORM:
94 return CAIRO_FORMAT_ARGB32;
95 default:
96 return CAIRO_FORMAT_INVALID;
97 }
98 }
99
100 static enum pipe_format
pipe_format_from_format(cairo_format_t format)101 pipe_format_from_format (cairo_format_t format)
102 {
103 switch ((int) format) {
104 case CAIRO_FORMAT_A8:
105 return PIPE_FORMAT_A8_UNORM;
106 case CAIRO_FORMAT_ARGB32:
107 return PIPE_FORMAT_A8R8G8B8_UNORM;
108 default:
109 return (enum pipe_format) -1;
110 }
111 }
112
113 static enum pipe_format
pipe_format_from_content(cairo_content_t content)114 pipe_format_from_content (cairo_content_t content)
115 {
116 if (content == CAIRO_CONTENT_ALPHA)
117 return PIPE_FORMAT_A8_UNORM;
118 else
119 return PIPE_FORMAT_A8R8G8B8_UNORM;
120 }
121
122 static cairo_bool_t
format_is_supported_destination(gallium_device_t * device,enum pipe_format format)123 format_is_supported_destination (gallium_device_t *device,
124 enum pipe_format format)
125 {
126 if (format == (enum pipe_format) -1)
127 return FALSE;
128
129 return device->screen->is_format_supported (device->screen,
130 format,
131 0,
132 PIPE_BIND_RENDER_TARGET,
133 0);
134 }
135
136 #if 0
137 static cairo_bool_t
138 format_is_supported_source (gallium_device_t *device,
139 enum pipe_format format)
140 {
141 return device->screen->is_format_supported (device->screen,
142 format,
143 0,
144 PIPE_BIND_SAMPLER_VIEW,
145 0);
146 }
147 #endif
148
149 static cairo_surface_t *
gallium_surface_create_similar(void * abstract_src,cairo_content_t content,int width,int height)150 gallium_surface_create_similar (void *abstract_src,
151 cairo_content_t content,
152 int width,
153 int height)
154 {
155 gallium_surface_t *other = abstract_src;
156 gallium_device_t *device = gallium_device (other);
157 enum pipe_format pipe_format;
158 cairo_surface_t *surface = NULL;
159 cairo_status_t status;
160
161 status = cairo_device_acquire (&device->drm.base);
162 if (unlikely (status))
163 return _cairo_surface_create_in_error (status);
164
165 if (MAX (width, height) > device->max_size)
166 goto RELEASE;
167
168 if (content == other->drm.base.content)
169 pipe_format = other->pipe_format;
170 else
171 pipe_format = pipe_format_from_content (content);
172
173 if (! format_is_supported_destination (device, pipe_format))
174 goto RELEASE;
175
176 surface = gallium_surface_create_internal (device,
177 pipe_format,
178 width, height);
179
180 RELEASE:
181 cairo_device_release (&device->drm.base);
182
183 return surface;
184 }
185
186 static cairo_status_t
gallium_surface_finish(void * abstract_surface)187 gallium_surface_finish (void *abstract_surface)
188 {
189 gallium_surface_t *surface = abstract_surface;
190 gallium_device_t *device = gallium_device (surface);
191 cairo_status_t status;
192
193 status = cairo_device_acquire (&device->drm.base);
194 if (likely (status == CAIRO_STATUS_SUCCESS)) {
195 pipe_resource_reference (&surface->texture, NULL);
196 cairo_device_release (&device->drm.base);
197 }
198
199 return _cairo_drm_surface_finish (&surface->drm);
200 }
201
202 static cairo_surface_t *
gallium_surface_map_to_image(gallium_surface_t * surface)203 gallium_surface_map_to_image (gallium_surface_t *surface)
204 {
205 gallium_device_t *device = gallium_device (surface);
206 cairo_status_t status;
207 void *ptr = NULL;
208
209 status = cairo_device_acquire (&device->drm.base);
210 if (unlikely (status))
211 return _cairo_surface_create_in_error (status);
212
213 surface->map_transfer =
214 pipe_get_transfer (device->pipe,
215 surface->texture, 0, 0, 0,
216 PIPE_TRANSFER_MAP_DIRECTLY |
217 PIPE_TRANSFER_READ_WRITE,
218 0, 0,
219 surface->drm.width,
220 surface->drm.height);
221 if (likely (surface->map_transfer != NULL))
222 ptr = device->pipe->transfer_map (device->pipe, surface->map_transfer);
223
224 cairo_device_release (&device->drm.base);
225
226 if (unlikely (ptr == NULL)) {
227 if (surface->map_transfer != NULL) {
228 device->pipe->transfer_destroy (device->pipe,
229 surface->map_transfer);
230 surface->map_transfer = NULL;
231 }
232 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
233 }
234
235 return cairo_image_surface_create_for_data (ptr,
236 surface->drm.format,
237 surface->drm.width,
238 surface->drm.height,
239 surface->map_transfer->stride);
240 }
241
242 static cairo_status_t
gallium_surface_acquire_source_image(void * abstract_surface,cairo_image_surface_t ** image_out,void ** image_extra)243 gallium_surface_acquire_source_image (void *abstract_surface,
244 cairo_image_surface_t **image_out,
245 void **image_extra)
246 {
247 gallium_surface_t *surface = abstract_surface;
248 gallium_device_t *device = gallium_device (surface);
249 cairo_format_t format;
250 cairo_surface_t *image;
251 cairo_status_t status;
252 struct pipe_transfer *transfer;
253 void *ptr;
254
255 if (surface->fallback != NULL) {
256 *image_out = (cairo_image_surface_t *)
257 cairo_surface_reference (surface->fallback);
258 *image_extra = NULL;
259 return CAIRO_STATUS_SUCCESS;
260 }
261
262 if (unlikely (surface->drm.width == 0 || surface->drm.height == 0)) {
263 image = cairo_image_surface_create (surface->drm.format, 0, 0);
264 if (unlikely (image->status))
265 return image->status;
266
267 *image_out = (cairo_image_surface_t *) image;
268 *image_extra = NULL;
269 return CAIRO_STATUS_SUCCESS;
270 }
271
272 format = _cairo_format_from_pipe_format (surface->pipe_format);
273 if (format == CAIRO_FORMAT_INVALID)
274 return CAIRO_INT_STATUS_UNSUPPORTED;
275
276 status = cairo_device_acquire (&device->drm.base);
277 if (unlikely (status))
278 return status;
279
280 transfer = pipe_get_transfer (device->pipe,
281 surface->texture, 0, 0, 0,
282 PIPE_TRANSFER_READ,
283 0, 0,
284 surface->drm.width,
285 surface->drm.height);
286 ptr = device->pipe->transfer_map (device->pipe, transfer);
287 cairo_device_release (&device->drm.base);
288
289 image = cairo_image_surface_create_for_data (ptr, format,
290 surface->drm.width,
291 surface->drm.height,
292 surface->drm.stride);
293 if (unlikely (image->status))
294 return image->status;
295
296 *image_out = (cairo_image_surface_t *) image;
297 *image_extra = transfer;
298 return CAIRO_STATUS_SUCCESS;
299 }
300
301 static void
gallium_surface_release_source_image(void * abstract_surface,cairo_image_surface_t * image,void * image_extra)302 gallium_surface_release_source_image (void *abstract_surface,
303 cairo_image_surface_t *image,
304 void *image_extra)
305 {
306 cairo_surface_destroy (&image->base);
307
308 if (image_extra != NULL) {
309 gallium_device_t *device = gallium_device (abstract_surface);
310
311 device->pipe->transfer_unmap (device->pipe, image_extra);
312 device->pipe->transfer_destroy (device->pipe, image_extra);
313 }
314 }
315
316 static cairo_status_t
gallium_surface_flush(void * abstract_surface,unsigned flags)317 gallium_surface_flush (void *abstract_surface,
318 unsigned flags)
319 {
320 gallium_surface_t *surface = abstract_surface;
321 gallium_device_t *device = gallium_device (surface);
322 cairo_status_t status;
323
324 if (flags)
325 return CAIRO_STATUS_SUCCESS;
326
327 if (surface->fallback == NULL) {
328 device->pipe->flush (device->pipe,
329 PIPE_FLUSH_RENDER_CACHE,
330 NULL);
331 return CAIRO_STATUS_SUCCESS;
332 }
333
334 /* kill any outstanding maps */
335 cairo_surface_finish (surface->fallback);
336
337 status = cairo_device_acquire (&device->drm.base);
338 if (likely (status == CAIRO_STATUS_SUCCESS)) {
339 device->pipe->transfer_unmap (device->pipe,
340 surface->map_transfer);
341 device->pipe->transfer_destroy (device->pipe,
342 surface->map_transfer);
343 surface->map_transfer = NULL;
344 cairo_device_release (&device->drm.base);
345 }
346
347 status = cairo_surface_status (surface->fallback);
348 cairo_surface_destroy (surface->fallback);
349 surface->fallback = NULL;
350
351 return status;
352 }
353
354 static cairo_int_status_t
gallium_surface_paint(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_clip_t * clip)355 gallium_surface_paint (void *abstract_surface,
356 cairo_operator_t op,
357 const cairo_pattern_t *source,
358 cairo_clip_t *clip)
359 {
360 gallium_surface_t *surface = abstract_surface;
361
362 if (surface->fallback == NULL) {
363 /* XXX insert magic */
364 surface->fallback = gallium_surface_map_to_image (surface);
365 }
366
367 return _cairo_surface_paint (surface->fallback, op, source, clip);
368 }
369
370 static cairo_int_status_t
gallium_surface_mask(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_pattern_t * mask,cairo_clip_t * clip)371 gallium_surface_mask (void *abstract_surface,
372 cairo_operator_t op,
373 const cairo_pattern_t *source,
374 const cairo_pattern_t *mask,
375 cairo_clip_t *clip)
376 {
377 gallium_surface_t *surface = abstract_surface;
378
379 if (surface->fallback == NULL) {
380 /* XXX insert magic */
381 surface->fallback = gallium_surface_map_to_image (surface);
382 }
383
384 return _cairo_surface_mask (surface->fallback,
385 op, source, mask,
386 clip);
387 }
388
389 static cairo_int_status_t
gallium_surface_stroke(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_path_fixed_t * path,const cairo_stroke_style_t * style,const cairo_matrix_t * ctm,const cairo_matrix_t * ctm_inverse,double tolerance,cairo_antialias_t antialias,cairo_clip_t * clip)390 gallium_surface_stroke (void *abstract_surface,
391 cairo_operator_t op,
392 const cairo_pattern_t *source,
393 cairo_path_fixed_t *path,
394 const cairo_stroke_style_t *style,
395 const cairo_matrix_t *ctm,
396 const cairo_matrix_t *ctm_inverse,
397 double tolerance,
398 cairo_antialias_t antialias,
399 cairo_clip_t *clip)
400 {
401 gallium_surface_t *surface = abstract_surface;
402
403 if (surface->fallback == NULL) {
404 /* XXX insert magic */
405 surface->fallback = gallium_surface_map_to_image (surface);
406 }
407
408 return _cairo_surface_stroke (surface->fallback,
409 op, source,
410 path, style,
411 ctm, ctm_inverse,
412 tolerance, antialias,
413 clip);
414 }
415
416 static cairo_int_status_t
gallium_surface_fill(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_path_fixed_t * path,cairo_fill_rule_t fill_rule,double tolerance,cairo_antialias_t antialias,cairo_clip_t * clip)417 gallium_surface_fill (void *abstract_surface,
418 cairo_operator_t op,
419 const cairo_pattern_t *source,
420 cairo_path_fixed_t *path,
421 cairo_fill_rule_t fill_rule,
422 double tolerance,
423 cairo_antialias_t antialias,
424 cairo_clip_t *clip)
425 {
426 gallium_surface_t *surface = abstract_surface;
427
428 if (surface->fallback == NULL) {
429 /* XXX insert magic */
430 surface->fallback = gallium_surface_map_to_image (surface);
431 }
432
433 return _cairo_surface_fill (surface->fallback,
434 op, source,
435 path, fill_rule,
436 tolerance, antialias,
437 clip);
438 }
439
440 static cairo_int_status_t
gallium_surface_glyphs(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_glyph_t * glyphs,int num_glyphs,cairo_scaled_font_t * scaled_font,cairo_clip_t * clip,int * num_remaining)441 gallium_surface_glyphs (void *abstract_surface,
442 cairo_operator_t op,
443 const cairo_pattern_t *source,
444 cairo_glyph_t *glyphs,
445 int num_glyphs,
446 cairo_scaled_font_t *scaled_font,
447 cairo_clip_t *clip,
448 int *num_remaining)
449 {
450 gallium_surface_t *surface = abstract_surface;
451
452 *num_remaining = 0;
453
454 if (surface->fallback == NULL) {
455 /* XXX insert magic */
456 surface->fallback = gallium_surface_map_to_image (surface);
457 }
458
459 return _cairo_surface_show_text_glyphs (surface->fallback,
460 op, source,
461 NULL, 0,
462 glyphs, num_glyphs,
463 NULL, 0, 0,
464 scaled_font,
465 clip);
466 }
467
468 static const cairo_surface_backend_t gallium_surface_backend = {
469 CAIRO_SURFACE_TYPE_DRM,
470 _cairo_default_context_create,
471
472 gallium_surface_create_similar,
473 gallium_surface_finish,
474
475 NULL,
476 gallium_surface_acquire_source_image,
477 gallium_surface_release_source_image,
478
479 NULL, //gallium_surface_acquire_dest_image,
480 NULL, //gallium_surface_release_dest_image,
481 NULL, //gallium_surface_clone_similar,
482 NULL, //gallium_surface_composite,
483 NULL, //gallium_surface_fill_rectangles,
484 NULL, //gallium_surface_composite_trapezoids,
485 NULL, //gallium_surface_create_span_renderer,
486 NULL, //gallium_surface_check_span_renderer,
487 NULL, /* copy_page */
488 NULL, /* show_page */
489 _cairo_drm_surface_get_extents,
490 NULL, /* old_show_glyphs */
491 _cairo_drm_surface_get_font_options,
492 gallium_surface_flush,
493 NULL, /* mark_dirty_rectangle */
494 NULL, //gallium_surface_scaled_font_fini,
495 NULL, //gallium_surface_scaled_glyph_fini,
496
497 gallium_surface_paint,
498 gallium_surface_mask,
499 gallium_surface_stroke,
500 gallium_surface_fill,
501 gallium_surface_glyphs,
502
503 NULL, /* snapshot */
504
505 NULL, /* is_similar */
506
507 NULL, /* reset */
508 };
509
510 static int
gallium_format_stride_for_width(enum pipe_format format,int width)511 gallium_format_stride_for_width (enum pipe_format format, int width)
512 {
513 int stride;
514
515 stride = 1024; /* XXX fugly */
516 while (stride < width)
517 stride *= 2;
518
519 if (format == PIPE_FORMAT_A8R8G8B8_UNORM)
520 stride *= 4;
521
522 return stride;
523 }
524
525 static cairo_drm_bo_t *
_gallium_fake_bo_create(uint32_t size,uint32_t name)526 _gallium_fake_bo_create (uint32_t size, uint32_t name)
527 {
528 cairo_drm_bo_t *bo;
529
530 /* XXX integrate with winsys handle */
531
532 bo = _cairo_malloc (sizeof (cairo_drm_bo_t));
533
534 CAIRO_REFERENCE_COUNT_INIT (&bo->ref_count, 1);
535 bo->name = name;
536 bo->handle = 0;
537 bo->size = size;
538
539 return bo;
540 }
541
542 static void
_gallium_fake_bo_release(void * dev,void * bo)543 _gallium_fake_bo_release (void *dev, void *bo)
544 {
545 free (bo);
546 }
547
548 static cairo_surface_t *
gallium_surface_create_internal(gallium_device_t * device,enum pipe_format pipe_format,int width,int height)549 gallium_surface_create_internal (gallium_device_t *device,
550 enum pipe_format pipe_format,
551 int width, int height)
552 {
553 gallium_surface_t *surface;
554 struct pipe_resource template;
555 cairo_status_t status;
556 cairo_format_t format;
557 int stride, size;
558
559 surface = _cairo_malloc (sizeof (gallium_surface_t));
560 if (unlikely (surface == NULL))
561 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
562
563 format = _cairo_format_from_pipe_format (pipe_format);
564 _cairo_surface_init (&surface->drm.base,
565 &gallium_surface_backend,
566 &device->drm.base,
567 _cairo_content_from_format (format));
568 _cairo_drm_surface_init (&surface->drm, format, width, height);
569
570 stride = gallium_format_stride_for_width (pipe_format, width);
571 size = stride * height;
572
573 surface->drm.stride = stride;
574 surface->drm.bo = _gallium_fake_bo_create (size, 0);
575
576 memset(&template, 0, sizeof(template));
577 template.target = PIPE_TEXTURE_2D;
578 template.format = pipe_format;
579 template.width0 = width;
580 template.height0 = height;
581 template.depth0 = 1;
582 template.last_level = 0;
583 template.bind = PIPE_BIND_RENDER_TARGET;
584 surface->texture = device->screen->resource_create (device->screen,
585 &template);
586
587 if (unlikely (surface->texture == NULL)) {
588 status = _cairo_drm_surface_finish (&surface->drm);
589 free (surface);
590 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
591 }
592
593 surface->pipe_format = pipe_format;
594 surface->texture = NULL;
595
596 return &surface->drm.base;
597 }
598
599 static cairo_surface_t *
gallium_surface_create(cairo_drm_device_t * base_dev,cairo_format_t format,int width,int height)600 gallium_surface_create (cairo_drm_device_t *base_dev,
601 cairo_format_t format,
602 int width, int height)
603 {
604 gallium_device_t *device = (gallium_device_t *) base_dev;
605 cairo_surface_t *surface;
606 enum pipe_format pipe_format;
607 cairo_status_t status;
608
609 status = cairo_device_acquire (&device->drm.base);
610
611 if (MAX (width, height) > device->max_size) {
612 surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
613 goto RELEASE;
614 }
615
616 pipe_format = pipe_format_from_format (format);
617 if (! format_is_supported_destination (device, pipe_format)) {
618 surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
619 goto RELEASE;
620 }
621
622 surface = gallium_surface_create_internal (device,
623 pipe_format,
624 width, height);
625
626 RELEASE:
627 cairo_device_release (&device->drm.base);
628
629 return surface;
630 }
631
632 #if 0
633 static cairo_surface_t *
634 gallium_surface_create_for_name (cairo_drm_device_t *base_dev,
635 unsigned int name,
636 cairo_format_t format,
637 int width, int height, int stride)
638 {
639 gallium_device_t *device;
640 gallium_surface_t *surface;
641 cairo_status_t status;
642 cairo_content_t content;
643
644 surface = _cairo_malloc (sizeof (gallium_surface_t));
645 if (unlikely (surface == NULL))
646 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
647
648 switch (format) {
649 default:
650 case CAIRO_FORMAT_INVALID:
651 case CAIRO_FORMAT_A1:
652 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
653 case CAIRO_FORMAT_A8:
654 surface->pipe_format = PIPE_FORMAT_A8_UNORM;
655 break;
656 case CAIRO_FORMAT_RGB24:
657 case CAIRO_FORMAT_ARGB32:
658 surface->pipe_format = PIPE_FORMAT_A8R8G8B8_UNORM;
659 break;
660 }
661
662 status = cairo_device_acquire (&device->drm.base);
663
664 if (MAX (width, height) > device->max_size) {
665 cairo_device_release (&device->drm.base);
666 free (surface);
667 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
668 }
669
670 if (! format_is_supported_destination (device, surface->pipe_format)) {
671 cairo_device_release (&device->drm.base);
672 free (surface);
673 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
674 }
675
676 content = _cairo_content_from_format (format);
677 _cairo_surface_init (&surface->drm.base,
678 &gallium_surface_backend,
679 content);
680 _cairo_drm_surface_init (&surface->drm, base_dev);
681
682 surface->drm.bo = _gallium_fake_bo_create (height * stride, name);
683
684 surface->drm.width = width;
685 surface->drm.height = height;
686 surface->drm.stride = stride;
687
688 #if 0
689 /* XXX screen->create_from_handle */
690 surface->buffer = device->api->buffer_from_handle (device->api,
691 device->screen,
692 "cairo-gallium alien",
693 name);
694 if (unlikely (surface->buffer == NULL)) {
695 status = _cairo_drm_surface_finish (&surface->drm);
696 cairo_device_release (&device->drm.base);
697 free (surface);
698 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
699 }
700 #endif
701
702 surface->texture = NULL;
703
704 surface->fallback = NULL;
705
706 cairo_device_release (&device->drm.base);
707
708 return &surface->drm.base;
709 }
710
711 static cairo_int_status_t
712 gallium_surface_flink (void *abstract_surface)
713 {
714 gallium_surface_t *surface = abstract_surface;
715 gallium_device_t *device;
716 cairo_status_t status = CAIRO_STATUS_SUCCESS;
717
718 status = cairo_device_acquire (&device->drm.base);
719 if (! device->api->global_handle_from_buffer (device->api,
720 device->screen,
721 surface->buffer,
722 &surface->drm.bo->name))
723 {
724 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
725 }
726 cairo_device_release (&device->drm.base);
727
728 return status;
729 }
730 #endif
731
732 static void
gallium_device_destroy(void * abstract_device)733 gallium_device_destroy (void *abstract_device)
734 {
735 gallium_device_t *device = abstract_device;
736
737 device->pipe->destroy (device->pipe);
738 device->screen->destroy (device->screen);
739 device->api->destroy (device->api);
740
741 dlclose (device->dlhandle);
742 free (device);
743 }
744
745 cairo_drm_device_t *
_cairo_drm_gallium_device_create(int fd,dev_t dev,int vendor_id,int chip_id)746 _cairo_drm_gallium_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
747 {
748 gallium_device_t *device;
749 cairo_status_t status;
750 void *handle;
751 const char *libdir;
752 char buf[4096];
753 struct drm_api *(*ctor) (void);
754
755 /* XXX need search path + probe */
756 libdir = getenv ("CAIRO_GALLIUM_LIBDIR");
757 if (libdir == NULL)
758 libdir = "/usr/lib/dri";
759 buf[snprintf (buf, sizeof (buf)-1, "%s/i915_dri.so", libdir)] = '\0';
760
761 handle = dlopen (buf, RTLD_LAZY);
762 if (handle == NULL)
763 return NULL;
764
765 ctor = dlsym (handle, "drm_api_create");
766 if (ctor == NULL) {
767 dlclose (handle);
768 return NULL;
769 }
770
771 device = _cairo_malloc (sizeof (gallium_device_t));
772 if (device == NULL) {
773 dlclose (handle);
774 return _cairo_drm_device_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
775 }
776
777 device->dlhandle = handle;
778
779 device->drm.surface.create = gallium_surface_create;
780 device->drm.surface.create_for_name = NULL;
781 //device->drm.surface.create_for_name = gallium_surface_create_for_name;
782 device->drm.surface.enable_scan_out = NULL;
783 //device->drm.surface.flink = gallium_surface_flink;
784 device->drm.surface.flink = NULL;
785
786 device->drm.device.flush = NULL;
787 device->drm.device.throttle = NULL;
788 device->drm.device.destroy = gallium_device_destroy;
789
790 device->drm.bo.release = _gallium_fake_bo_release;
791
792 device->api = ctor ();
793 if (device->api == NULL) {
794 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
795 goto CLEANUP;
796 }
797
798 device->screen = device->api->create_screen (device->api, fd, NULL);
799 if (device->screen == NULL) {
800 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
801 goto CLEANUP_API;
802 }
803
804 device->max_size = 1 << device->screen->get_param (device->screen,
805 PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
806
807 device->pipe = device->screen->context_create (device->screen, device);
808 if (device->pipe == NULL) {
809 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
810 goto CLEANUP_SCREEN;
811 }
812
813 return _cairo_drm_device_init (&device->drm,
814 fd, dev,
815 0, 0,
816 device->max_size);
817
818 CLEANUP_SCREEN:
819 device->screen->destroy (device->screen);
820 CLEANUP_API:
821 device->api->destroy (device->api);
822 CLEANUP:
823 free (device);
824 dlclose (handle);
825 return _cairo_drm_device_create_in_error (status);
826 }
827