summaryrefslogtreecommitdiff
path: root/src/drm/cairo-drm-gallium-surface.c
blob: 4d92a97e0f502b6b1f86f44323d4ad64b2716599 (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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
/* 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-error-private.h"

#include <dlfcn.h>

#include <drivers/softpipe/sp_public.h>
#include <winsys/sw/null/null_sw_winsys.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>
#include <util/u_format.h>
#include <util/u_tile.h>
#include <util/u_simple_shaders.h>
#include <util/u_draw_quad.h>

#include <tgsi/tgsi_ureg.h>
#include <tgsi/tgsi_build.h>
#include <tgsi/tgsi_parse.h>
#include <tgsi/tgsi_util.h>

#include <cso_cache/cso_context.h>
#include <cso_cache/cso_hash.h>


#define MAX_CONTANTS 20

typedef enum _shader_type shadertype_t;

typedef struct _gallium_paint   gallium_paint_t;
typedef struct _gallium_surface gallium_surface_t;
typedef struct _gallium_shader  gallium_shader_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;
    struct cso_context  *cso;

    struct pipe_framebuffer_state framebuffer;
    struct pipe_viewport_state viewport;

    int max_size;

    gallium_shader_t *shader;
};

struct _gallium_paint {

    float solid_color[4];
    //XXX: add gradient infrastructure
};

struct _gallium_shader {
    gallium_device_t *device;

    float constants[MAX_CONTANTS];
    struct pipe_resource *cbuf;
    struct cso_hash *fs;
    struct cso_hash *vs;
};

struct _gallium_surface {
    cairo_drm_surface_t drm;

    enum pipe_format pipe_format;

    struct pipe_resource *texture;
    struct pipe_transfer *map_transfer;
    gallium_paint_t paint;

    struct pipe_resource *vbuf;
    struct pipe_vertex_element velem[2];
    struct pipe_blend_state blend;
    struct pipe_rasterizer_state rasterizer;
    struct pipe_depth_stencil_alpha_state depthstencil;

    cairo_surface_t *fallback;
};

enum _shader_type {
    SOLID_FILL = 0,
};

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,
						PIPE_TEXTURE_2D,
						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,
						PIPE_TEXTURE_2D,
						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 gallium_shader_t *
shader_create(gallium_device_t *device)
{
    gallium_shader_t *shader = 0;

    shader = malloc (sizeof(gallium_shader_t));
    shader->device = device;
    shader->fs  = cso_hash_create();

    return shader;
}

static void
shader_solid_fill(struct ureg_program *ureg,
                  struct ureg_src *constant,
                  struct ureg_dst *out)
{
    ureg_MOV(ureg, *out, constant[0]);
}

static void
setup_constant_buffer(gallium_shader_t *shader, float *constants, int size)
{
    struct pipe_context *ctx    = shader->device->pipe;
    struct pipe_resource  *buffer = shader->cbuf;

    pipe_resource_reference (&buffer, NULL);

    memcpy (shader->constants, constants, size);

    buffer = pipe_user_buffer_create (ctx->screen,
				      &shader->constants,
				      sizeof(shader->constants),
				      PIPE_BIND_VERTEX_BUFFER);

    ctx->set_constant_buffer (ctx, PIPE_SHADER_FRAGMENT, 0, buffer);
}

static void *
create_fs(struct pipe_context *ctx, shadertype_t type)
{
    void *ret;
    struct ureg_program *ureg;
    struct ureg_dst out;
    struct ureg_src constant;
    struct ureg_src sampler;
    struct ureg_src in;

    ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
    if (!ureg)
        return NULL;

    out = ureg_DECL_output(ureg,
                           TGSI_SEMANTIC_COLOR,
                           0);

    constant = ureg_DECL_constant(ureg, 0);

    in = ureg_DECL_fs_input(ureg,
                            TGSI_SEMANTIC_POSITION,
                            0,
                            TGSI_INTERPOLATE_LINEAR);

    sampler = ureg_DECL_sampler(ureg, 0);

    switch (type) {
    case SOLID_FILL:
        shader_solid_fill(ureg, &constant, &out);
        break;
    }

    ureg_END(ureg);

    ret = ureg_create_shader_and_destroy(ureg, ctx);

    return ret;
}

static INLINE void *
shader_from_cache(gallium_device_t *device,
                  shadertype_t key,
                  struct cso_hash *hash)
{
    void *shader;
    struct cso_hash_iter iter = cso_hash_find(hash, key);

    if (cso_hash_iter_is_null(iter)) {
        shader = create_fs(device->pipe, key);
        cso_hash_insert(hash, key, shader);
    } else {
        shader = (void *)cso_hash_iter_data(iter);
    }

    return shader;
}

static void
shader_cache_destroy(gallium_shader_t *cairo_shader)
{
    struct cso_hash_iter iter = cso_hash_first_node(cairo_shader->fs);

    while(!cso_hash_iter_is_null(iter)) {
        void *shader = (void *)cso_hash_iter_data(iter);
        cso_delete_fragment_shader(cairo_shader->device->cso, shader);
        iter = cso_hash_erase(cairo_shader->fs, iter);
    }

    cso_hash_delete(cairo_shader->fs);
}

static void
shader_destroy(gallium_shader_t *shader)
{
    shader_cache_destroy(shader);
    free(shader);
}

static void
setup_shader(gallium_shader_t *shader, shadertype_t type)
{
    {
        const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
                                       TGSI_SEMANTIC_GENERIC };
        const uint semantic_indexes[] = {0, 0};

        shader->vs = util_make_vertex_passthrough_shader(shader->device->pipe,
                                                         2,
                                                         semantic_names,
                                                         semantic_indexes);
    }

    cso_set_vertex_shader_handle(shader->device->cso, shader->vs);

    shader->fs = shader_from_cache(shader->device, type, shader->fs);
    cso_set_fragment_shader_handle(shader->device->cso, shader->fs);
}


static INLINE void
unpack_color (const unsigned *src,
	      unsigned w, unsigned h,
	      unsigned char *p,
	      unsigned dst_stride)
{
    unsigned i, j;

    for (i = 0; i < h; i++) {
        unsigned char *pRow = p;
        for (j = 0; j < w; j++, pRow += 4) {
            const unsigned pixel = *src++;
            pRow[0] = (pixel >>  0) & 0xff;
            pRow[1] = (pixel >>  8) & 0xff;
            pRow[2] = (pixel >> 16) & 0xff;
            pRow[3] = (pixel >> 24) & 0xff;
        }
        p += dst_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 *rgba_packed;
    unsigned char *color_unpacked;
    int stride;

    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);

    rgba_packed =
        malloc (util_format_get_nblocks(surface->texture->format,
					surface->drm.width,
					surface->drm.height) *
		util_format_get_blocksize(surface->texture->format));

    rgba_packed = malloc (surface->drm.width*surface->drm.height*4);

    stride = util_format_get_stride(surface->texture->format, surface->drm.width);

    pipe_get_tile_raw (device->pipe,
		       transfer,
		       0, 0,
		       surface->drm.width,
		       surface->drm.height,
		       rgba_packed,
		       stride);

    unpack_color (rgba_packed, surface->drm.width, surface->drm.height, color_unpacked, stride);
    cairo_device_release (&device->drm.base);

    image = cairo_image_surface_create_for_data (color_unpacked, 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;

    free(rgba_packed);
    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)
{
    gallium_surface_t *surface = abstract_surface;
    gallium_device_t *device = gallium_device (surface);
    cairo_status_t status;


    {
	cso_set_framebuffer (device->cso, &device->framebuffer);
	cso_set_blend (device->cso, &surface->blend);
	cso_set_depth_stencil_alpha (device->cso, &surface->depthstencil);
	cso_set_rasterizer (device->cso, &surface->rasterizer);
	cso_set_viewport (device->cso, &device->viewport);

	cso_set_vertex_elements (device->cso, 2, surface->velem);

	util_draw_vertex_buffer (device->pipe,
				 surface->vbuf, 0,
				 PIPE_PRIM_QUADS,
				 4,
				 2);
    }

    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 void
_gallium_surface_paint_solid(gallium_surface_t *surface, const cairo_pattern_t *pattern)
{
    cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *)pattern;

    surface->paint.solid_color[0] = solid->color.red;
    surface->paint.solid_color[1] = solid->color.green;
    surface->paint.solid_color[2] = solid->color.blue;
    surface->paint.solid_color[3] = solid->color.alpha;
}

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;
    gallium_shader_t  *shader  = gallium_device(surface)->shader;
    cairo_status_t status;

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

    if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
	_gallium_surface_paint_solid(surface, source);
	setup_constant_buffer(shader, &surface->paint.solid_color[0], 4*sizeof(float));
	setup_shader(shader, SOLID_FILL);
	status = CAIRO_STATUS_SUCCESS;
    } else {
	status = _cairo_surface_paint (surface->fallback, op, source, clip);
    }

    return status;
}

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,
    gallium_surface_create_similar,
    gallium_surface_finish,

    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));
    }

    {
	float vertices[4][2][4] = {
            {
                { 0.0f, 0.0f, 0.0f, 1.0f },
                { 0.0f, 0.0f, 0.0f, 1.0f }
            },
            {
                { 0.0f, 1.0f, 0.0f, 1.0f },
                { 0.0f, 0.0f, 0.0f, 1.0f }
            },
            {
                { 1.0f, 1.0f, 0.0f, 1.0f },
                { 0.0f, 0.0f, 0.0f, 1.0f }
            },
            {
                { 1.0f, 0.0f, 0.0f, 1.0f },
                { 0.0f, 0.0f, 0.0f, 1.0f }
            }
        };

        surface->vbuf = pipe_buffer_create(device->pipe->screen, PIPE_BIND_CONSTANT_BUFFER, sizeof(vertices));
        pipe_buffer_write(device->pipe, surface->vbuf, 0, sizeof(vertices), vertices);
	device->pipe->set_constant_buffer(device->pipe, PIPE_SHADER_VERTEX, 0, surface->vbuf);
    }

    memset(&device->framebuffer, 0, sizeof(device->framebuffer));
    device->framebuffer.width  = width;
    device->framebuffer.height = height;
    device->framebuffer.nr_cbufs = 1;
    device->framebuffer.cbufs[0] =
	device->screen->get_tex_surface(device->screen,
					surface->texture,
					0, 0, 0,
					PIPE_BIND_RENDER_TARGET);

    device->viewport.scale[0] = width;
    device->viewport.scale[1] = height;
    device->viewport.scale[2] = 1.0f;
    device->viewport.scale[3] = 1.0f;

    device->viewport.translate[0] = 0.0f;
    device->viewport.translate[1] = 0.0f;
    device->viewport.translate[2] = 0.0f;
    device->viewport.translate[3] = 0.0f;

    memset(&surface->blend, 0, sizeof(surface->blend));
    surface->blend.rt[0].colormask = PIPE_MASK_RGBA;

    memset(&surface->depthstencil, 0, sizeof(surface->depthstencil));

    memset(&surface->rasterizer, 0, sizeof(surface->rasterizer));
    surface->rasterizer.gl_rasterization_rules = 1;

    memset(surface->velem, 0, sizeof(surface->velem));
    surface->velem[0].src_offset = 0 * 4 * sizeof(float);
    surface->velem[0].instance_divisor = 0;
    surface->velem[0].vertex_buffer_index = 0;
    surface->velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;

    surface->velem[1].src_offset = 1 * 4 * sizeof(float);
    surface->velem[1].instance_divisor = 0;
    surface->velem[1].vertex_buffer_index = 0;
    surface->velem[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;

    surface->pipe_format = pipe_format;

    device->shader = shader_create(device);

    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;

    shader_destroy (device->shader);
    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);
    cairo_bool_t has_dri_driver = true;

    /* 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) {
	has_dri_driver = false;
    }

    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;

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

	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);
    } else{
	device->screen = softpipe_create_screen(null_sw_create());
    }
    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);
}