summaryrefslogtreecommitdiff
path: root/src/drm/cairo-drm-gallium-surface.c
blob: 164ab03cee53f22b1442660bd982115a0f633448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/* Cairo - a vector graphics library with display and print output
 *
 * Copyright © 2009 Chris Wilson
 * Copyright © 2009 Eric Anholt
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Chris Wilson.
 */

#include "cairoint.h"

#include "cairo-drm-private.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"

#include <dlfcn.h>

#include <state_tracker/drm_api.h>
#include <pipe/p_format.h>
#include <pipe/p_screen.h>
#include <pipe/p_context.h>
#include <pipe/p_state.h>

#include <util/u_inlines.h>

typedef struct _gallium_surface gallium_surface_t;
typedef struct _gallium_device gallium_device_t;

struct _gallium_device {
    cairo_drm_device_t drm;

    void *dlhandle;
    struct drm_api *api;

    struct pipe_screen *screen;
    struct pipe_context *pipe;

    int max_size;
};

struct _gallium_surface {
    cairo_drm_surface_t drm;

    enum pipe_format pipe_format;

    struct pipe_resource *texture;
    struct pipe_transfer *map_transfer;

    cairo_surface_t *fallback;
};

static cairo_surface_t *
gallium_surface_create_internal (gallium_device_t *device,
				 enum pipe_format format,
				 int width, int height);

static inline gallium_device_t *
gallium_device (gallium_surface_t *surface)
{
    return (gallium_device_t *) surface->drm.base.device;
}

static cairo_format_t
_cairo_format_from_pipe_format (enum pipe_format format)
{
    switch ((int) format) {
    case PIPE_FORMAT_A8_UNORM:
	return CAIRO_FORMAT_A8;
    case PIPE_FORMAT_A8R8G8B8_UNORM:
	return CAIRO_FORMAT_ARGB32;
    default:
	return CAIRO_FORMAT_INVALID;
    }
}

static enum pipe_format
pipe_format_from_format (cairo_format_t format)
{
    switch ((int) format) {
    case CAIRO_FORMAT_A8:
	return PIPE_FORMAT_A8_UNORM;
    case CAIRO_FORMAT_ARGB32:
	return PIPE_FORMAT_A8R8G8B8_UNORM;
    default:
	return (enum pipe_format) -1;
    }
}

static enum pipe_format
pipe_format_from_content (cairo_content_t content)
{
    if (content == CAIRO_CONTENT_ALPHA)
	return PIPE_FORMAT_A8_UNORM;
    else
	return PIPE_FORMAT_A8R8G8B8_UNORM;
}

static cairo_bool_t
format_is_supported_destination (gallium_device_t *device,
	                         enum pipe_format format)
{
    if (format == (enum pipe_format) -1)
	return FALSE;

    return device->screen->is_format_supported (device->screen,
					        format,
						0,
						PIPE_BIND_RENDER_TARGET,
						0);
}

#if 0
static cairo_bool_t
format_is_supported_source (gallium_device_t *device,
	                    enum pipe_format format)
{
    return device->screen->is_format_supported (device->screen,
					        format,
						0,
						PIPE_BIND_SAMPLER_VIEW,
						0);
}
#endif

static cairo_surface_t *
gallium_surface_create_similar (void			*abstract_src,
				cairo_content_t		 content,
				int			 width,
				int			 height)
{
    gallium_surface_t *other = abstract_src;
    gallium_device_t *device = gallium_device (other);
    enum pipe_format pipe_format;
    cairo_surface_t *surface = NULL;
    cairo_status_t status;

    status = cairo_device_acquire (&device->drm.base);
    if (unlikely (status))
	return _cairo_surface_create_in_error (status);

    if (MAX (width, height) > device->max_size)
	goto RELEASE;

    if (content == other->drm.base.content)
	pipe_format = other->pipe_format;
    else
	pipe_format = pipe_format_from_content (content);

    if (! format_is_supported_destination (device, pipe_format))
	goto RELEASE;

    surface = gallium_surface_create_internal (device,
					       pipe_format,
					       width, height);

RELEASE:
    cairo_device_release (&device->drm.base);

    return surface;
}

static cairo_status_t
gallium_surface_finish (void *abstract_surface)
{
    gallium_surface_t *surface = abstract_surface;
    gallium_device_t *device = gallium_device (surface);
    cairo_status_t status;

    status = cairo_device_acquire (&device->drm.base);
    if (likely (status == CAIRO_STATUS_SUCCESS)) {
	pipe_resource_reference (&surface->texture, NULL);
	cairo_device_release (&device->drm.base);
    }

    return _cairo_drm_surface_finish (&surface->drm);
}

static cairo_surface_t *
gallium_surface_map_to_image (gallium_surface_t *surface)
{
    gallium_device_t *device = gallium_device (surface);
    cairo_status_t status;
    void *ptr = NULL;

    status = cairo_device_acquire (&device->drm.base);
    if (unlikely (status))
	return _cairo_surface_create_in_error (status);

    surface->map_transfer =
	  pipe_get_transfer (device->pipe,
			     surface->texture, 0, 0, 0,
			     PIPE_TRANSFER_MAP_DIRECTLY |
			     PIPE_TRANSFER_READ_WRITE,
			     0, 0,
			     surface->drm.width,
			     surface->drm.height);
    if (likely (surface->map_transfer != NULL))
	ptr = device->pipe->transfer_map (device->pipe, surface->map_transfer);

    cairo_device_release (&device->drm.base);

    if (unlikely (ptr == NULL)) {
	if (surface->map_transfer != NULL) {
	    device->pipe->transfer_destroy (device->pipe,
					    surface->map_transfer);
	    surface->map_transfer = NULL;
	}
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
    }

    return cairo_image_surface_create_for_data (ptr,
						surface->drm.format,
						surface->drm.width,
						surface->drm.height,
						surface->map_transfer->stride);
}

static cairo_status_t
gallium_surface_acquire_source_image (void *abstract_surface,
				      cairo_image_surface_t **image_out,
				      void **image_extra)
{
    gallium_surface_t *surface = abstract_surface;
    gallium_device_t *device = gallium_device (surface);
    cairo_format_t format;
    cairo_surface_t *image;
    cairo_status_t status;
    struct pipe_transfer *transfer;
    void *ptr;

    if (surface->fallback != NULL) {
	*image_out = (cairo_image_surface_t *)
	    cairo_surface_reference (surface->fallback);
	*image_extra = NULL;
	return CAIRO_STATUS_SUCCESS;
    }

    if (unlikely (surface->drm.width == 0 || surface->drm.height == 0)) {
	image = cairo_image_surface_create (surface->drm.format, 0, 0);
	if (unlikely (image->status))
	    return image->status;

	*image_out = (cairo_image_surface_t *) image;
	*image_extra = NULL;
	return CAIRO_STATUS_SUCCESS;
    }

    format = _cairo_format_from_pipe_format (surface->pipe_format);
    if (format == CAIRO_FORMAT_INVALID)
	return CAIRO_INT_STATUS_UNSUPPORTED;

    status = cairo_device_acquire (&device->drm.base);
    if (unlikely (status))
	return status;

    transfer = pipe_get_transfer (device->pipe,
				  surface->texture, 0, 0, 0,
				  PIPE_TRANSFER_READ,
				  0, 0,
				  surface->drm.width,
				  surface->drm.height);
    ptr = device->pipe->transfer_map (device->pipe, transfer);
    cairo_device_release (&device->drm.base);

    image = cairo_image_surface_create_for_data (ptr, format,
						 surface->drm.width,
						 surface->drm.height,
						 surface->drm.stride);
    if (unlikely (image->status))
	return image->status;

    *image_out = (cairo_image_surface_t *) image;
    *image_extra = transfer;
    return CAIRO_STATUS_SUCCESS;
}

static void
gallium_surface_release_source_image (void *abstract_surface,
				      cairo_image_surface_t *image,
				      void *image_extra)
{
    cairo_surface_destroy (&image->base);

    if (image_extra != NULL) {
	gallium_device_t *device = gallium_device (abstract_surface);

	device->pipe->transfer_unmap (device->pipe, image_extra);
	device->pipe->transfer_destroy (device->pipe, image_extra);
    }
}

static cairo_status_t
gallium_surface_flush (void *abstract_surface,
		       unsigned flags)
{
    gallium_surface_t *surface = abstract_surface;
    gallium_device_t *device = gallium_device (surface);
    cairo_status_t status;

    if (flags)
	return CAIRO_STATUS_SUCCESS;

    if (surface->fallback == NULL) {
	device->pipe->flush (device->pipe,
			     PIPE_FLUSH_RENDER_CACHE,
			     NULL);
	return CAIRO_STATUS_SUCCESS;
    }

    /* kill any outstanding maps */
    cairo_surface_finish (surface->fallback);

    status = cairo_device_acquire (&device->drm.base);
    if (likely (status == CAIRO_STATUS_SUCCESS)) {
	device->pipe->transfer_unmap (device->pipe,
				      surface->map_transfer);
	device->pipe->transfer_destroy (device->pipe,
					surface->map_transfer);
	surface->map_transfer = NULL;
	cairo_device_release (&device->drm.base);
    }

    status = cairo_surface_status (surface->fallback);
    cairo_surface_destroy (surface->fallback);
    surface->fallback = NULL;

    return status;
}

static cairo_int_status_t
gallium_surface_paint (void			*abstract_surface,
			  cairo_operator_t	 op,
			  const cairo_pattern_t	*source,
			  cairo_clip_t		*clip)
{
    gallium_surface_t *surface = abstract_surface;

    if (surface->fallback == NULL) {
	/* XXX insert magic */
	surface->fallback = gallium_surface_map_to_image (surface);
    }

    return _cairo_surface_paint (surface->fallback, op, source, clip);
}

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)
{
    gallium_surface_t *surface = abstract_surface;

    if (surface->fallback == NULL) {
	/* XXX insert magic */
	surface->fallback = gallium_surface_map_to_image (surface);
    }

    return _cairo_surface_mask (surface->fallback,
				op, source, mask,
				clip);
}

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)
{
    gallium_surface_t *surface = abstract_surface;

    if (surface->fallback == NULL) {
	/* XXX insert magic */
	surface->fallback = gallium_surface_map_to_image (surface);
    }

    return _cairo_surface_stroke (surface->fallback,
				  op, source,
				  path, style,
				  ctm, ctm_inverse,
				  tolerance, antialias,
				  clip);
}

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)
{
    gallium_surface_t *surface = abstract_surface;

    if (surface->fallback == NULL) {
	/* XXX insert magic */
	surface->fallback = gallium_surface_map_to_image (surface);
    }

    return _cairo_surface_fill (surface->fallback,
				op, source,
				path, fill_rule,
				tolerance, antialias,
				clip);
}

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)
{
    gallium_surface_t *surface = abstract_surface;

    *num_remaining = 0;

    if (surface->fallback == NULL) {
	/* XXX insert magic */
	surface->fallback = gallium_surface_map_to_image (surface);
    }

    return _cairo_surface_show_text_glyphs (surface->fallback,
					    op, source,
					    NULL, 0,
					    glyphs, num_glyphs,
					    NULL, 0, 0,
					    scaled_font,
					    clip);
}

static const cairo_surface_backend_t gallium_surface_backend = {
    CAIRO_SURFACE_TYPE_DRM,
    _cairo_default_context_create,

    gallium_surface_create_similar,
    gallium_surface_finish,

    NULL,
    gallium_surface_acquire_source_image,
    gallium_surface_release_source_image,

    NULL, //gallium_surface_acquire_dest_image,
    NULL, //gallium_surface_release_dest_image,
    NULL, //gallium_surface_clone_similar,
    NULL, //gallium_surface_composite,
    NULL, //gallium_surface_fill_rectangles,
    NULL, //gallium_surface_composite_trapezoids,
    NULL, //gallium_surface_create_span_renderer,
    NULL, //gallium_surface_check_span_renderer,
    NULL, /* copy_page */
    NULL, /* show_page */
    _cairo_drm_surface_get_extents,
    NULL, /* old_show_glyphs */
    _cairo_drm_surface_get_font_options,
    gallium_surface_flush,
    NULL, /* mark_dirty_rectangle */
    NULL, //gallium_surface_scaled_font_fini,
    NULL, //gallium_surface_scaled_glyph_fini,

    gallium_surface_paint,
    gallium_surface_mask,
    gallium_surface_stroke,
    gallium_surface_fill,
    gallium_surface_glyphs,

    NULL, /* snapshot */

    NULL, /* is_similar */

    NULL, /* reset */
};

static int
gallium_format_stride_for_width (enum pipe_format format, int width)
{
    int stride;

    stride = 1024; /* XXX fugly */
    while (stride < width)
	stride *= 2;

    if (format == PIPE_FORMAT_A8R8G8B8_UNORM)
	stride *= 4;

    return stride;
}

static cairo_drm_bo_t *
_gallium_fake_bo_create (uint32_t size, uint32_t name)
{
    cairo_drm_bo_t *bo;

    /* XXX integrate with winsys handle */

    bo = malloc (sizeof (cairo_drm_bo_t));

    CAIRO_REFERENCE_COUNT_INIT (&bo->ref_count, 1);
    bo->name = name;
    bo->handle = 0;
    bo->size = size;

    return bo;
}

static void
_gallium_fake_bo_release (void *dev, void *bo)
{
    free (bo);
}

static cairo_surface_t *
gallium_surface_create_internal (gallium_device_t *device,
				 enum pipe_format pipe_format,
				 int width, int height)
{
    gallium_surface_t *surface;
    struct pipe_resource template;
    cairo_status_t status;
    cairo_format_t format;
    int stride, size;

    surface = malloc (sizeof (gallium_surface_t));
    if (unlikely (surface == NULL))
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));

    format = _cairo_format_from_pipe_format (pipe_format);
    _cairo_surface_init (&surface->drm.base,
			 &gallium_surface_backend,
			 &device->drm.base,
			 _cairo_content_from_format (format));
    _cairo_drm_surface_init (&surface->drm, format, width, height);

    stride = gallium_format_stride_for_width (pipe_format, width);
    size = stride * height;

    surface->drm.stride = stride;
    surface->drm.bo = _gallium_fake_bo_create (size, 0);

    memset(&template, 0, sizeof(template));
    template.target = PIPE_TEXTURE_2D;
    template.format = pipe_format;
    template.width0 = width;
    template.height0 = height;
    template.depth0 = 1;
    template.last_level = 0;
    template.bind = PIPE_BIND_RENDER_TARGET;
    surface->texture = device->screen->resource_create (device->screen,
							&template);

    if (unlikely (surface->texture == NULL)) {
	status = _cairo_drm_surface_finish (&surface->drm);
	free (surface);
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
    }

    surface->pipe_format = pipe_format;
    surface->texture = NULL;

    return &surface->drm.base;
}

static cairo_surface_t *
gallium_surface_create (cairo_drm_device_t *base_dev,
			cairo_format_t format,
			int width, int height)
{
    gallium_device_t *device = (gallium_device_t *) base_dev;
    cairo_surface_t *surface;
    enum pipe_format pipe_format;
    cairo_status_t status;

    status = cairo_device_acquire (&device->drm.base);

    if (MAX (width, height) > device->max_size) {
	surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
	goto RELEASE;
    }

    pipe_format = pipe_format_from_format (format);
    if (! format_is_supported_destination (device, pipe_format)) {
	surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
	goto RELEASE;
    }

    surface = gallium_surface_create_internal (device,
					       pipe_format,
					       width, height);

RELEASE:
    cairo_device_release (&device->drm.base);

    return surface;
}

#if 0
static cairo_surface_t *
gallium_surface_create_for_name (cairo_drm_device_t *base_dev,
				 unsigned int name,
				 cairo_format_t format,
				 int width, int height, int stride)
{
    gallium_device_t *device;
    gallium_surface_t *surface;
    cairo_status_t status;
    cairo_content_t content;

    surface = malloc (sizeof (gallium_surface_t));
    if (unlikely (surface == NULL))
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));

    switch (format) {
    default:
    case CAIRO_FORMAT_INVALID:
    case CAIRO_FORMAT_A1:
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
    case CAIRO_FORMAT_A8:
	surface->pipe_format = PIPE_FORMAT_A8_UNORM;
	break;
    case CAIRO_FORMAT_RGB24:
    case CAIRO_FORMAT_ARGB32:
	surface->pipe_format = PIPE_FORMAT_A8R8G8B8_UNORM;
	break;
    }

    status = cairo_device_acquire (&device->drm.base);

    if (MAX (width, height) > device->max_size) {
	cairo_device_release (&device->drm.base);
	free (surface);
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
    }

    if (! format_is_supported_destination (device, surface->pipe_format)) {
	cairo_device_release (&device->drm.base);
	free (surface);
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
    }

    content = _cairo_content_from_format (format);
    _cairo_surface_init (&surface->drm.base,
			 &gallium_surface_backend,
			 content);
    _cairo_drm_surface_init (&surface->drm, base_dev);

    surface->drm.bo = _gallium_fake_bo_create (height * stride, name);

    surface->drm.width  = width;
    surface->drm.height = height;
    surface->drm.stride = stride;

#if 0
    /* XXX screen->create_from_handle */
    surface->buffer = device->api->buffer_from_handle (device->api,
						       device->screen,
						       "cairo-gallium alien",
						       name);
    if (unlikely (surface->buffer == NULL)) {
	status = _cairo_drm_surface_finish (&surface->drm);
	cairo_device_release (&device->drm.base);
	free (surface);
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
    }
#endif

    surface->texture = NULL;

    surface->fallback = NULL;

    cairo_device_release (&device->drm.base);

    return &surface->drm.base;
}

static cairo_int_status_t
gallium_surface_flink (void *abstract_surface)
{
    gallium_surface_t *surface = abstract_surface;
    gallium_device_t *device;
    cairo_status_t status = CAIRO_STATUS_SUCCESS;

    status = cairo_device_acquire (&device->drm.base);
    if (! device->api->global_handle_from_buffer (device->api,
						  device->screen,
						  surface->buffer,
						  &surface->drm.bo->name))
    {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
    cairo_device_release (&device->drm.base);

    return status;
}
#endif

static void
gallium_device_destroy (void *abstract_device)
{
    gallium_device_t *device = abstract_device;

    device->pipe->destroy (device->pipe);
    device->screen->destroy (device->screen);
    device->api->destroy (device->api);

    dlclose (device->dlhandle);
    free (device);
}

cairo_drm_device_t *
_cairo_drm_gallium_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
{
    gallium_device_t *device;
    cairo_status_t status;
    void *handle;
    const char *libdir;
    char buf[4096];
    struct drm_api *(*ctor) (void);

    /* XXX need search path + probe */
    libdir = getenv ("CAIRO_GALLIUM_LIBDIR");
    if (libdir == NULL)
	libdir = "/usr/lib/dri";
    buf[snprintf (buf, sizeof (buf)-1, "%s/i915_dri.so", libdir)] = '\0';

    handle = dlopen (buf, RTLD_LAZY);
    if (handle == NULL)
	return NULL;

    ctor = dlsym (handle, "drm_api_create");
    if (ctor == NULL) {
	dlclose (handle);
	return NULL;
    }

    device = malloc (sizeof (gallium_device_t));
    if (device == NULL) {
	dlclose (handle);
	return _cairo_drm_device_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
    }

    device->dlhandle = handle;

    device->drm.surface.create = gallium_surface_create;
    device->drm.surface.create_for_name = NULL;
    //device->drm.surface.create_for_name = gallium_surface_create_for_name;
    device->drm.surface.enable_scan_out = NULL;
    //device->drm.surface.flink = gallium_surface_flink;
    device->drm.surface.flink = NULL;

    device->drm.device.flush = NULL;
    device->drm.device.throttle = NULL;
    device->drm.device.destroy = gallium_device_destroy;

    device->drm.bo.release = _gallium_fake_bo_release;

    device->api = ctor ();
    if (device->api == NULL) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP;
    }

    device->screen = device->api->create_screen (device->api, fd, NULL);
    if (device->screen == NULL) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP_API;
    }

    device->max_size = 1 << device->screen->get_param (device->screen,
						       PIPE_CAP_MAX_TEXTURE_2D_LEVELS);

    device->pipe = device->screen->context_create (device->screen, device);
    if (device->pipe == NULL) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP_SCREEN;
    }

    return _cairo_drm_device_init (&device->drm,
				   fd, dev,
				   0, 0,
				   device->max_size);

CLEANUP_SCREEN:
    device->screen->destroy (device->screen);
CLEANUP_API:
    device->api->destroy (device->api);
CLEANUP:
    free (device);
    dlclose (handle);
    return _cairo_drm_device_create_in_error (status);
}