aa3f45ebe3a21639c69c8021dc94beb321aa0fb6
[framework/graphics/cairo.git] / src / drm / cairo-drm-gallium-surface.c
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 *
82 gallium_device (gallium_surface_t *surface)
83 {
84     return (gallium_device_t *) surface->drm.base.device;
85 }
86
87 static cairo_format_t
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
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
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
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 *
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
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 *
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
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
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
317 gallium_surface_flush (void *abstract_surface)
318 {
319     gallium_surface_t *surface = abstract_surface;
320     gallium_device_t *device = gallium_device (surface);
321     cairo_status_t status;
322
323     if (surface->fallback == NULL) {
324         device->pipe->flush (device->pipe,
325                              PIPE_FLUSH_RENDER_CACHE,
326                              NULL);
327         return CAIRO_STATUS_SUCCESS;
328     }
329
330     /* kill any outstanding maps */
331     cairo_surface_finish (surface->fallback);
332
333     status = cairo_device_acquire (&device->drm.base);
334     if (likely (status == CAIRO_STATUS_SUCCESS)) {
335         device->pipe->transfer_unmap (device->pipe,
336                                       surface->map_transfer);
337         device->pipe->transfer_destroy (device->pipe,
338                                         surface->map_transfer);
339         surface->map_transfer = NULL;
340         cairo_device_release (&device->drm.base);
341     }
342
343     status = cairo_surface_status (surface->fallback);
344     cairo_surface_destroy (surface->fallback);
345     surface->fallback = NULL;
346
347     return status;
348 }
349
350 static cairo_int_status_t
351 gallium_surface_paint (void                     *abstract_surface,
352                           cairo_operator_t       op,
353                           const cairo_pattern_t *source,
354                           cairo_clip_t          *clip)
355 {
356     gallium_surface_t *surface = abstract_surface;
357
358     if (surface->fallback == NULL) {
359         /* XXX insert magic */
360         surface->fallback = gallium_surface_map_to_image (surface);
361     }
362
363     return _cairo_surface_paint (surface->fallback, op, source, clip);
364 }
365
366 static cairo_int_status_t
367 gallium_surface_mask (void                      *abstract_surface,
368                          cairo_operator_t        op,
369                          const cairo_pattern_t  *source,
370                          const cairo_pattern_t  *mask,
371                          cairo_clip_t           *clip)
372 {
373     gallium_surface_t *surface = abstract_surface;
374
375     if (surface->fallback == NULL) {
376         /* XXX insert magic */
377         surface->fallback = gallium_surface_map_to_image (surface);
378     }
379
380     return _cairo_surface_mask (surface->fallback,
381                                 op, source, mask,
382                                 clip);
383 }
384
385 static cairo_int_status_t
386 gallium_surface_stroke (void                            *abstract_surface,
387                            cairo_operator_t              op,
388                            const cairo_pattern_t        *source,
389                            cairo_path_fixed_t           *path,
390                            const cairo_stroke_style_t   *style,
391                            const cairo_matrix_t         *ctm,
392                            const cairo_matrix_t         *ctm_inverse,
393                            double                        tolerance,
394                            cairo_antialias_t             antialias,
395                            cairo_clip_t                 *clip)
396 {
397     gallium_surface_t *surface = abstract_surface;
398
399     if (surface->fallback == NULL) {
400         /* XXX insert magic */
401         surface->fallback = gallium_surface_map_to_image (surface);
402     }
403
404     return _cairo_surface_stroke (surface->fallback,
405                                   op, source,
406                                   path, style,
407                                   ctm, ctm_inverse,
408                                   tolerance, antialias,
409                                   clip);
410 }
411
412 static cairo_int_status_t
413 gallium_surface_fill (void                      *abstract_surface,
414                          cairo_operator_t        op,
415                          const cairo_pattern_t  *source,
416                          cairo_path_fixed_t     *path,
417                          cairo_fill_rule_t       fill_rule,
418                          double                  tolerance,
419                          cairo_antialias_t       antialias,
420                          cairo_clip_t           *clip)
421 {
422     gallium_surface_t *surface = abstract_surface;
423
424     if (surface->fallback == NULL) {
425         /* XXX insert magic */
426         surface->fallback = gallium_surface_map_to_image (surface);
427     }
428
429     return _cairo_surface_fill (surface->fallback,
430                                 op, source,
431                                 path, fill_rule,
432                                 tolerance, antialias,
433                                 clip);
434 }
435
436 static cairo_int_status_t
437 gallium_surface_glyphs (void                            *abstract_surface,
438                            cairo_operator_t              op,
439                            const cairo_pattern_t        *source,
440                            cairo_glyph_t                *glyphs,
441                            int                           num_glyphs,
442                            cairo_scaled_font_t          *scaled_font,
443                            cairo_clip_t                 *clip,
444                            int *num_remaining)
445 {
446     gallium_surface_t *surface = abstract_surface;
447
448     *num_remaining = 0;
449
450     if (surface->fallback == NULL) {
451         /* XXX insert magic */
452         surface->fallback = gallium_surface_map_to_image (surface);
453     }
454
455     return _cairo_surface_show_text_glyphs (surface->fallback,
456                                             op, source,
457                                             NULL, 0,
458                                             glyphs, num_glyphs,
459                                             NULL, 0, 0,
460                                             scaled_font,
461                                             clip);
462 }
463
464 static const cairo_surface_backend_t gallium_surface_backend = {
465     CAIRO_SURFACE_TYPE_DRM,
466     _cairo_default_context_create,
467
468     gallium_surface_create_similar,
469     gallium_surface_finish,
470
471     NULL,
472     gallium_surface_acquire_source_image,
473     gallium_surface_release_source_image,
474
475     NULL, //gallium_surface_acquire_dest_image,
476     NULL, //gallium_surface_release_dest_image,
477     NULL, //gallium_surface_clone_similar,
478     NULL, //gallium_surface_composite,
479     NULL, //gallium_surface_fill_rectangles,
480     NULL, //gallium_surface_composite_trapezoids,
481     NULL, //gallium_surface_create_span_renderer,
482     NULL, //gallium_surface_check_span_renderer,
483     NULL, /* copy_page */
484     NULL, /* show_page */
485     _cairo_drm_surface_get_extents,
486     NULL, /* old_show_glyphs */
487     _cairo_drm_surface_get_font_options,
488     gallium_surface_flush,
489     NULL, /* mark_dirty_rectangle */
490     NULL, //gallium_surface_scaled_font_fini,
491     NULL, //gallium_surface_scaled_glyph_fini,
492
493     gallium_surface_paint,
494     gallium_surface_mask,
495     gallium_surface_stroke,
496     gallium_surface_fill,
497     gallium_surface_glyphs,
498
499     NULL, /* snapshot */
500
501     NULL, /* is_similar */
502
503     NULL, /* reset */
504 };
505
506 static int
507 gallium_format_stride_for_width (enum pipe_format format, int width)
508 {
509     int stride;
510
511     stride = 1024; /* XXX fugly */
512     while (stride < width)
513         stride *= 2;
514
515     if (format == PIPE_FORMAT_A8R8G8B8_UNORM)
516         stride *= 4;
517
518     return stride;
519 }
520
521 static cairo_drm_bo_t *
522 _gallium_fake_bo_create (uint32_t size, uint32_t name)
523 {
524     cairo_drm_bo_t *bo;
525
526     /* XXX integrate with winsys handle */
527
528     bo = malloc (sizeof (cairo_drm_bo_t));
529
530     CAIRO_REFERENCE_COUNT_INIT (&bo->ref_count, 1);
531     bo->name = name;
532     bo->handle = 0;
533     bo->size = size;
534
535     return bo;
536 }
537
538 static void
539 _gallium_fake_bo_release (void *dev, void *bo)
540 {
541     free (bo);
542 }
543
544 static cairo_surface_t *
545 gallium_surface_create_internal (gallium_device_t *device,
546                                  enum pipe_format pipe_format,
547                                  int width, int height)
548 {
549     gallium_surface_t *surface;
550     struct pipe_resource template;
551     cairo_status_t status;
552     cairo_format_t format;
553     int stride, size;
554
555     surface = malloc (sizeof (gallium_surface_t));
556     if (unlikely (surface == NULL))
557         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
558
559     format = _cairo_format_from_pipe_format (pipe_format);
560     _cairo_surface_init (&surface->drm.base,
561                          &gallium_surface_backend,
562                          &device->drm.base,
563                          _cairo_content_from_format (format));
564     _cairo_drm_surface_init (&surface->drm, format, width, height);
565
566     stride = gallium_format_stride_for_width (pipe_format, width);
567     size = stride * height;
568
569     surface->drm.stride = stride;
570     surface->drm.bo = _gallium_fake_bo_create (size, 0);
571
572     memset(&template, 0, sizeof(template));
573     template.target = PIPE_TEXTURE_2D;
574     template.format = pipe_format;
575     template.width0 = width;
576     template.height0 = height;
577     template.depth0 = 1;
578     template.last_level = 0;
579     template.bind = PIPE_BIND_RENDER_TARGET;
580     surface->texture = device->screen->resource_create (device->screen,
581                                                         &template);
582
583     if (unlikely (surface->texture == NULL)) {
584         status = _cairo_drm_surface_finish (&surface->drm);
585         free (surface);
586         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
587     }
588
589     surface->pipe_format = pipe_format;
590     surface->texture = NULL;
591
592     return &surface->drm.base;
593 }
594
595 static cairo_surface_t *
596 gallium_surface_create (cairo_drm_device_t *base_dev,
597                         cairo_format_t format,
598                         int width, int height)
599 {
600     gallium_device_t *device = (gallium_device_t *) base_dev;
601     cairo_surface_t *surface;
602     enum pipe_format pipe_format;
603     cairo_status_t status;
604
605     status = cairo_device_acquire (&device->drm.base);
606
607     if (MAX (width, height) > device->max_size) {
608         surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
609         goto RELEASE;
610     }
611
612     pipe_format = pipe_format_from_format (format);
613     if (! format_is_supported_destination (device, pipe_format)) {
614         surface = _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
615         goto RELEASE;
616     }
617
618     surface = gallium_surface_create_internal (device,
619                                                pipe_format,
620                                                width, height);
621
622 RELEASE:
623     cairo_device_release (&device->drm.base);
624
625     return surface;
626 }
627
628 #if 0
629 static cairo_surface_t *
630 gallium_surface_create_for_name (cairo_drm_device_t *base_dev,
631                                  unsigned int name,
632                                  cairo_format_t format,
633                                  int width, int height, int stride)
634 {
635     gallium_device_t *device;
636     gallium_surface_t *surface;
637     cairo_status_t status;
638     cairo_content_t content;
639
640     surface = malloc (sizeof (gallium_surface_t));
641     if (unlikely (surface == NULL))
642         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
643
644     switch (format) {
645     default:
646     case CAIRO_FORMAT_INVALID:
647     case CAIRO_FORMAT_A1:
648         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
649     case CAIRO_FORMAT_A8:
650         surface->pipe_format = PIPE_FORMAT_A8_UNORM;
651         break;
652     case CAIRO_FORMAT_RGB24:
653     case CAIRO_FORMAT_ARGB32:
654         surface->pipe_format = PIPE_FORMAT_A8R8G8B8_UNORM;
655         break;
656     }
657
658     status = cairo_device_acquire (&device->drm.base);
659
660     if (MAX (width, height) > device->max_size) {
661         cairo_device_release (&device->drm.base);
662         free (surface);
663         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
664     }
665
666     if (! format_is_supported_destination (device, surface->pipe_format)) {
667         cairo_device_release (&device->drm.base);
668         free (surface);
669         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
670     }
671
672     content = _cairo_content_from_format (format);
673     _cairo_surface_init (&surface->drm.base,
674                          &gallium_surface_backend,
675                          content);
676     _cairo_drm_surface_init (&surface->drm, base_dev);
677
678     surface->drm.bo = _gallium_fake_bo_create (height * stride, name);
679
680     surface->drm.width  = width;
681     surface->drm.height = height;
682     surface->drm.stride = stride;
683
684 #if 0
685     /* XXX screen->create_from_handle */
686     surface->buffer = device->api->buffer_from_handle (device->api,
687                                                        device->screen,
688                                                        "cairo-gallium alien",
689                                                        name);
690     if (unlikely (surface->buffer == NULL)) {
691         status = _cairo_drm_surface_finish (&surface->drm);
692         cairo_device_release (&device->drm.base);
693         free (surface);
694         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
695     }
696 #endif
697
698     surface->texture = NULL;
699
700     surface->fallback = NULL;
701
702     cairo_device_release (&device->drm.base);
703
704     return &surface->drm.base;
705 }
706
707 static cairo_int_status_t
708 gallium_surface_flink (void *abstract_surface)
709 {
710     gallium_surface_t *surface = abstract_surface;
711     gallium_device_t *device;
712     cairo_status_t status = CAIRO_STATUS_SUCCESS;
713
714     status = cairo_device_acquire (&device->drm.base);
715     if (! device->api->global_handle_from_buffer (device->api,
716                                                   device->screen,
717                                                   surface->buffer,
718                                                   &surface->drm.bo->name))
719     {
720         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
721     }
722     cairo_device_release (&device->drm.base);
723
724     return status;
725 }
726 #endif
727
728 static void
729 gallium_device_destroy (void *abstract_device)
730 {
731     gallium_device_t *device = abstract_device;
732
733     device->pipe->destroy (device->pipe);
734     device->screen->destroy (device->screen);
735     device->api->destroy (device->api);
736
737     dlclose (device->dlhandle);
738     free (device);
739 }
740
741 cairo_drm_device_t *
742 _cairo_drm_gallium_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
743 {
744     gallium_device_t *device;
745     cairo_status_t status;
746     void *handle;
747     const char *libdir;
748     char buf[4096];
749     struct drm_api *(*ctor) (void);
750
751     /* XXX need search path + probe */
752     libdir = getenv ("CAIRO_GALLIUM_LIBDIR");
753     if (libdir == NULL)
754         libdir = "/usr/lib/dri";
755     buf[snprintf (buf, sizeof (buf)-1, "%s/i915_dri.so", libdir)] = '\0';
756
757     handle = dlopen (buf, RTLD_LAZY);
758     if (handle == NULL)
759         return NULL;
760
761     ctor = dlsym (handle, "drm_api_create");
762     if (ctor == NULL) {
763         dlclose (handle);
764         return NULL;
765     }
766
767     device = malloc (sizeof (gallium_device_t));
768     if (device == NULL) {
769         dlclose (handle);
770         return _cairo_drm_device_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
771     }
772
773     device->dlhandle = handle;
774
775     device->drm.surface.create = gallium_surface_create;
776     device->drm.surface.create_for_name = NULL;
777     //device->drm.surface.create_for_name = gallium_surface_create_for_name;
778     device->drm.surface.enable_scan_out = NULL;
779     //device->drm.surface.flink = gallium_surface_flink;
780     device->drm.surface.flink = NULL;
781
782     device->drm.device.flush = NULL;
783     device->drm.device.throttle = NULL;
784     device->drm.device.destroy = gallium_device_destroy;
785
786     device->drm.bo.release = _gallium_fake_bo_release;
787
788     device->api = ctor ();
789     if (device->api == NULL) {
790         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
791         goto CLEANUP;
792     }
793
794     device->screen = device->api->create_screen (device->api, fd, NULL);
795     if (device->screen == NULL) {
796         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
797         goto CLEANUP_API;
798     }
799
800     device->max_size = 1 << device->screen->get_param (device->screen,
801                                                        PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
802
803     device->pipe = device->screen->context_create (device->screen, device);
804     if (device->pipe == NULL) {
805         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
806         goto CLEANUP_SCREEN;
807     }
808
809     return _cairo_drm_device_init (&device->drm,
810                                    fd, dev,
811                                    0, 0,
812                                    device->max_size);
813
814 CLEANUP_SCREEN:
815     device->screen->destroy (device->screen);
816 CLEANUP_API:
817     device->api->destroy (device->api);
818 CLEANUP:
819     free (device);
820     dlclose (handle);
821     return _cairo_drm_device_create_in_error (status);
822 }