commit 22f8d81

Michael Forney  ·  2014-01-13 22:58:35 +0000 UTC
parent ea66021
drm: Use exporters
6 files changed,  +68, -153
M drm.c
M drm.h
M dumb.c
+0, -10
 1@@ -26,9 +26,6 @@
 2 
 3 #include "wld-private.h"
 4 
 5-typedef int (* drm_export_func_t)(struct wld_drawable * drawable);
 6-typedef uint32_t (* drm_get_handle_func_t)(struct wld_drawable * drawable);
 7-
 8 struct drm_driver
 9 {
10     const char * name;
11@@ -36,13 +33,6 @@ struct drm_driver
12     struct wld_context * (* create_context)(int drm_fd);
13 };
14 
15-struct drm_drawable_impl
16-{
17-    struct wld_drawable_impl base;
18-    drm_export_func_t export;
19-    drm_get_handle_func_t get_handle;
20-};
21-
22 #if WITH_DRM_INTEL
23 extern const struct drm_driver intel_drm_driver;
24 #endif
M drm.c
+0, -12
 1@@ -88,15 +88,3 @@ bool wld_drm_is_dumb(struct wld_context * context)
 2     return context->impl == dumb_context_impl;
 3 }
 4 
 5-EXPORT
 6-int wld_drm_export(struct wld_drawable * drawable)
 7-{
 8-    return ((struct drm_drawable_impl *) drawable->impl)->export(drawable);
 9-}
10-
11-EXPORT
12-uint32_t wld_drm_get_handle(struct wld_drawable * drawable)
13-{
14-    return ((struct drm_drawable_impl *) drawable->impl)->get_handle(drawable);
15-}
16-
M drm.h
+3, -16
 1@@ -29,8 +29,9 @@
 2 
 3 enum wld_drm_object_type
 4 {
 5-    WLD_DRM_OBJECT_PRIME_FD = 0x00010000,
 6-    WLD_DRM_OBJECT_GEM_NAME = 0x00010001
 7+    WLD_DRM_OBJECT_HANDLE   = 0x00010000,
 8+    WLD_DRM_OBJECT_PRIME_FD = 0x00010001,
 9+    WLD_DRM_OBJECT_GEM_NAME = 0x00010002
10 };
11 
12 /**
13@@ -40,19 +41,5 @@ struct wld_context * wld_drm_create_context(int fd);
14 
15 bool wld_drm_is_dumb(struct wld_context * context);
16 
17-/**
18- * Export a DRM drawable to a PRIME file descriptor.
19- *
20- * @return A PRIME file descriptor for this drawable
21- */
22-int wld_drm_export(struct wld_drawable * drawable);
23-
24-/**
25- * Get the handle of a DRM drawable.
26- *
27- * This can be used with various DRM ioctls.
28- */
29-uint32_t wld_drm_get_handle(struct wld_drawable * drawable);
30-
31 #endif
32 
M dumb.c
+37, -29
  1@@ -42,27 +42,19 @@ struct dumb_context
  2 struct dumb_drawable
  3 {
  4     struct pixman_drawable pixman;
  5+    struct wld_exporter exporter;
  6     struct dumb_context * context;
  7     uint32_t handle;
  8 };
  9 
 10 #define DRM_DRIVER_NAME dumb
 11 #include "interface/context.h"
 12+#include "interface/exporter.h"
 13 #include "interface/drm.h"
 14 IMPL(dumb, context)
 15 
 16 const struct wld_context_impl * dumb_context_impl = &context_impl;
 17 
 18-/* DRM drawable */
 19-static int drawable_export(struct wld_drawable * drawable);
 20-static uint32_t drawable_get_handle(struct wld_drawable * drawable);
 21-
 22-static struct drm_drawable_impl drawable_impl = {
 23-    .export = &drawable_export,
 24-    .get_handle = &drawable_get_handle
 25-};
 26-static bool draw_initialized;
 27-
 28 bool driver_device_supported(uint32_t vendor_id, uint32_t device_id)
 29 {
 30     return true;
 31@@ -81,12 +73,6 @@ struct wld_context * driver_create_context(int drm_fd)
 32     context_initialize(&context->base, &context_impl);
 33     context->fd = drm_fd;
 34 
 35-    if (!draw_initialized)
 36-    {
 37-        drawable_impl.base = *pixman_drawable_impl;
 38-        draw_initialized = true;
 39-    }
 40-
 41     return &context->base;
 42 
 43   error1:
 44@@ -126,9 +112,10 @@ static struct wld_drawable * new_drawable(struct dumb_context * context,
 45         goto error2;
 46     }
 47 
 48-    drawable->pixman.base.impl = &drawable_impl.base;
 49     drawable->context = context;
 50     drawable->handle = handle;
 51+    exporter_initialize(&drawable->exporter, &exporter_impl);
 52+    drawable_add_exporter(&drawable->pixman.base, &drawable->exporter);
 53 
 54     return &drawable->pixman.base;
 55 
 56@@ -215,24 +202,45 @@ void context_destroy(struct wld_context * base)
 57     free(context);
 58 }
 59 
 60-static int drawable_export(struct wld_drawable * drawable)
 61+/**** Exporter ****/
 62+
 63+bool exporter_export(struct wld_exporter * exporter, struct wld_drawable * base,
 64+                     uint32_t type, union wld_object * object)
 65 {
 66-    struct dumb_drawable * dumb = (void *) drawable;
 67-    int prime_fd, ret;
 68+    struct dumb_drawable * drawable = (void *) base;
 69 
 70-    ret = drmPrimeHandleToFD(dumb->context->fd, dumb->handle,
 71-                             DRM_CLOEXEC, &prime_fd);
 72+    switch (type)
 73+    {
 74+        case WLD_DRM_OBJECT_HANDLE:
 75+            object->u32 = drawable->handle;
 76+            return true;
 77+        case WLD_DRM_OBJECT_PRIME_FD:
 78+            if (drmPrimeHandleToFD(drawable->context->fd, drawable->handle,
 79+                                   DRM_CLOEXEC, &object->i) != 0)
 80+            {
 81+                return false;
 82+            }
 83 
 84-    if (ret != 0)
 85-        return -1;
 86+            return true;
 87+        case WLD_DRM_OBJECT_GEM_NAME:
 88+        {
 89+            struct drm_gem_flink flink = { .handle = drawable->handle };
 90 
 91-    return prime_fd;
 92+            if (drmIoctl(drawable->context->fd, DRM_IOCTL_GEM_FLINK,
 93+                         &flink) != 0)
 94+            {
 95+                return false;
 96+            }
 97+
 98+            object->u32 = flink.name;
 99+            return true;
100+        }
101+        default:
102+            return false;
103+    }
104 }
105 
106-static uint32_t drawable_get_handle(struct wld_drawable * drawable)
107+void exporter_destroy(struct wld_exporter * exporter)
108 {
109-    struct dumb_drawable * dumb = (void *) drawable;
110-
111-    return dumb->handle;
112 }
113 
+28, -12
 1@@ -40,6 +40,7 @@ struct intel_context
 2 struct intel_drawable
 3 {
 4     struct wld_drawable base;
 5+    struct wld_exporter exporter;
 6 
 7     struct intel_context * context;
 8     drm_intel_bo * bo;
 9@@ -47,10 +48,12 @@ struct intel_drawable
10 };
11 
12 #include "interface/context.h"
13+#include "interface/drawable.h"
14+#include "interface/exporter.h"
15 #define DRM_DRIVER_NAME intel
16 #include "interface/drm.h"
17-#include "interface/drm_drawable.h"
18 IMPL(intel, context)
19+IMPL(intel, drawable)
20 
21 bool driver_device_supported(uint32_t vendor_id, uint32_t device_id)
22 {
23@@ -96,10 +99,12 @@ static struct intel_drawable * new_drawable(struct intel_context * context,
24     if (!(intel = malloc(sizeof *intel)))
25         return NULL;
26 
27-    drawable_initialize(&intel->base, &drawable_impl.base,
28+    drawable_initialize(&intel->base, &drawable_impl,
29                         width, height, format, 0);
30     intel->context = context;
31     intel->virtual = NULL;
32+    exporter_initialize(&intel->exporter, &exporter_impl);
33+    drawable_add_exporter(&intel->base, &intel->exporter);
34 
35     return intel;
36 }
37@@ -313,20 +318,31 @@ void drawable_destroy(struct wld_drawable * drawable)
38     free(intel);
39 }
40 
41-int drawable_export(struct wld_drawable * drawable)
42+/**** Exporter ****/
43+bool exporter_export(struct wld_exporter * exporter, struct wld_drawable * base,
44+                     uint32_t type, union wld_object * object)
45 {
46-    struct intel_drawable * intel = (void *) drawable;
47-    int prime_fd;
48-
49-    drm_intel_bo_gem_export_to_prime(intel->bo, &prime_fd);
50+    struct intel_drawable * drawable = intel_drawable(base);
51 
52-    return prime_fd;
53+    switch (type)
54+    {
55+        case WLD_DRM_OBJECT_HANDLE:
56+            object->u32 = drawable->bo->handle;
57+            return true;
58+        case WLD_DRM_OBJECT_PRIME_FD:
59+            if (drm_intel_bo_gem_export_to_prime(drawable->bo, &object->i) != 0)
60+                return false;
61+            return true;
62+        case WLD_DRM_OBJECT_GEM_NAME:
63+            if (drm_intel_bo_flink(drawable->bo, &object->u32) != 0)
64+                return false;
65+            return true;
66+        default:
67+            return false;
68+    }
69 }
70 
71-uint32_t drawable_get_handle(struct wld_drawable * drawable)
72+void exporter_destroy(struct wld_exporter * exporter)
73 {
74-    struct intel_drawable * intel = (void *) drawable;
75-
76-    return intel->bo->handle;
77 }
78 
+0, -74
 1@@ -1,74 +0,0 @@
 2-/* wld: interface/drm_drawable.h
 3- *
 4- * Copyright (c) 2013 Michael Forney
 5- *
 6- * Permission is hereby granted, free of charge, to any person obtaining a copy
 7- * of this software and associated documentation files (the "Software"), to deal
 8- * in the Software without restriction, including without limitation the rights
 9- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10- * copies of the Software, and to permit persons to whom the Software is
11- * furnished to do so, subject to the following conditions:
12- *
13- * The above copyright notice and this permission notice shall be included in
14- * all copies or substantial portions of the Software.
15- *
16- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22- * SOFTWARE.
23- */
24-
25-static void drawable_fill_rectangle(struct wld_drawable * drawable,
26-                                    uint32_t color, int32_t x, int32_t y,
27-                                    uint32_t width, uint32_t height);
28-static void drawable_copy_rectangle(struct wld_drawable * src,
29-                                    struct wld_drawable * dst,
30-                                    int32_t src_x, int32_t src_y,
31-                                    int32_t dst_x, int32_t dst_y,
32-                                    uint32_t width, uint32_t height);
33-#ifdef DRAWABLE_IMPLEMENTS_REGION
34-static void drawable_fill_region(struct wld_drawable * drawable, uint32_t color,
35-                                 pixman_region32_t * region);
36-static void drawable_copy_region(struct wld_drawable * src,
37-                                 struct wld_drawable * drawable,
38-                                 pixman_region32_t * region,
39-                                 int32_t dst_x, int32_t dst_y);
40-#endif
41-static void drawable_draw_text(struct wld_drawable * drawable,
42-                               struct font * font, uint32_t color,
43-                               int32_t x, int32_t y,
44-                               const char * text, int32_t length,
45-                               struct wld_extents * extents);
46-static void drawable_write(struct wld_drawable * drawable,
47-                           const void * data, size_t size);
48-static pixman_image_t * drawable_map(struct wld_drawable * drawable);
49-static void drawable_flush(struct wld_drawable * drawable);
50-static void drawable_destroy(struct wld_drawable * drawable);
51-
52-static int drawable_export(struct wld_drawable * drawable);
53-static uint32_t drawable_get_handle(struct wld_drawable * drawable);
54-
55-static const struct drm_drawable_impl drawable_impl = {
56-    .base = {
57-        .fill_rectangle = &drawable_fill_rectangle,
58-        .copy_rectangle = &drawable_copy_rectangle,
59-#ifdef DRAWABLE_IMPLEMENTS_REGION
60-        .fill_region = &drawable_fill_region,
61-        .copy_region = &drawable_copy_region,
62-#else
63-        .fill_region = &default_fill_region,
64-        .copy_region = &default_copy_region,
65-#endif
66-        .draw_text = &drawable_draw_text,
67-        .write = &drawable_write,
68-        .map = &drawable_map,
69-        .flush = &drawable_flush,
70-        .destroy = &drawable_destroy
71-    },
72-    .export = &drawable_export,
73-    .get_handle = &drawable_get_handle
74-};
75-