1 /*
2 * Copyright (C) 2016 Christian Gmeiner <[email protected]>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Christian Gmeiner <[email protected]>
25 */
26
27 #include "renderonly/renderonly.h"
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <xf86drm.h>
33
34 #include "frontend/drm_driver.h"
35 #include "pipe/p_screen.h"
36 #include "util/format/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39
40 void
renderonly_scanout_destroy(struct renderonly_scanout * scanout,struct renderonly * ro)41 renderonly_scanout_destroy(struct renderonly_scanout *scanout,
42 struct renderonly *ro)
43 {
44 struct drm_mode_destroy_dumb destroy_dumb = {0};
45
46 assert(p_atomic_read(&scanout->refcnt) > 0);
47 if (p_atomic_dec_return(&scanout->refcnt))
48 return;
49
50 simple_mtx_lock(&ro->bo_map_lock);
51
52 /* Someone might have imported this BO while we were waiting for the
53 * lock, let's make sure it's still not referenced before freeing it.
54 */
55 if (p_atomic_read(&scanout->refcnt) == 0 && ro->kms_fd != -1) {
56 destroy_dumb.handle = scanout->handle;
57 scanout->handle = 0;
58 scanout->stride = 0;
59 drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
60 }
61
62 simple_mtx_unlock(&ro->bo_map_lock);
63 }
64
65 struct renderonly_scanout *
renderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource * rsc,struct renderonly * ro,struct winsys_handle * out_handle)66 renderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,
67 struct renderonly *ro,
68 struct winsys_handle *out_handle)
69 {
70 struct renderonly_scanout *scanout = NULL;
71 int err;
72 struct drm_mode_create_dumb create_dumb = {
73 .width = rsc->width0,
74 .height = rsc->height0,
75 .bpp = util_format_get_blocksizebits(rsc->format),
76 };
77 struct drm_mode_destroy_dumb destroy_dumb = {0};
78
79 /* create dumb buffer at scanout GPU */
80 err = drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
81 if (err < 0) {
82 fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n",
83 strerror(errno));
84 return NULL;
85 }
86
87 simple_mtx_lock(&ro->bo_map_lock);
88 scanout = util_sparse_array_get(&ro->bo_map, create_dumb.handle);
89 simple_mtx_unlock(&ro->bo_map_lock);
90
91 if (!scanout)
92 goto free_dumb;
93
94 scanout->handle = create_dumb.handle;
95 scanout->stride = create_dumb.pitch;
96
97 assert(p_atomic_read(&scanout->refcnt) == 0);
98 p_atomic_set(&scanout->refcnt, 1);
99
100 if (!out_handle)
101 return scanout;
102
103 /* fill in winsys handle */
104 memset(out_handle, 0, sizeof(*out_handle));
105 out_handle->type = WINSYS_HANDLE_TYPE_FD;
106 out_handle->stride = create_dumb.pitch;
107
108 err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC,
109 (int *)&out_handle->handle);
110 if (err < 0) {
111 fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno));
112 goto free_dumb;
113 }
114
115 return scanout;
116
117 free_dumb:
118 /* If an error occured, make sure we reset the scanout object before
119 * leaving.
120 */
121 if (scanout)
122 memset(scanout, 0, sizeof(*scanout));
123
124 destroy_dumb.handle = create_dumb.handle;
125 drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
126
127 return NULL;
128 }
129
130 struct renderonly_scanout *
renderonly_create_gpu_import_for_resource(struct pipe_resource * rsc,struct renderonly * ro,struct winsys_handle * out_handle)131 renderonly_create_gpu_import_for_resource(struct pipe_resource *rsc,
132 struct renderonly *ro,
133 struct winsys_handle *out_handle)
134 {
135 struct pipe_screen *screen = rsc->screen;
136 struct renderonly_scanout *scanout = NULL;
137 bool status;
138 uint32_t scanout_handle;
139 int fd, err;
140 struct winsys_handle handle = {
141 .type = WINSYS_HANDLE_TYPE_FD
142 };
143
144 status = screen->resource_get_handle(screen, NULL, rsc, &handle,
145 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
146 if (!status)
147 return NULL;
148
149 fd = handle.handle;
150
151 simple_mtx_lock(&ro->bo_map_lock);
152 err = drmPrimeFDToHandle(ro->kms_fd, fd, &scanout_handle);
153 close(fd);
154
155 if (err < 0)
156 goto err_unlock;
157
158 scanout = util_sparse_array_get(&ro->bo_map, scanout_handle);
159 if (!scanout)
160 goto err_unlock;
161
162 if (p_atomic_inc_return(&scanout->refcnt) == 1) {
163 scanout->handle = scanout_handle;
164 scanout->stride = handle.stride;
165 }
166
167 err_unlock:
168 simple_mtx_unlock(&ro->bo_map_lock);
169
170 return scanout;
171 }
172
173