broadcom/vc4: Native fence fd support
[mesa.git] / src / gallium / drivers / vc4 / vc4_context.c
1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <xf86drm.h>
25 #include <err.h>
26
27 #include "pipe/p_defines.h"
28 #include "util/ralloc.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "util/u_upload_mgr.h"
33 #include "indices/u_primconvert.h"
34 #include "pipe/p_screen.h"
35
36 #include "vc4_screen.h"
37 #include "vc4_context.h"
38 #include "vc4_resource.h"
39
40 void
41 vc4_flush(struct pipe_context *pctx)
42 {
43 struct vc4_context *vc4 = vc4_context(pctx);
44
45 struct hash_entry *entry;
46 hash_table_foreach(vc4->jobs, entry) {
47 struct vc4_job *job = entry->data;
48 vc4_job_submit(vc4, job);
49 }
50 }
51
52 static void
53 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
54 unsigned flags)
55 {
56 struct vc4_context *vc4 = vc4_context(pctx);
57
58 vc4_flush(pctx);
59
60 if (fence) {
61 struct pipe_screen *screen = pctx->screen;
62 int fd = -1;
63
64 if (flags & PIPE_FLUSH_FENCE_FD) {
65 /* The vc4_fence takes ownership of the returned fd. */
66 drmSyncobjExportSyncFile(vc4->fd, vc4->job_syncobj,
67 &fd);
68 }
69
70 struct vc4_fence *f = vc4_fence_create(vc4->screen,
71 vc4->last_emit_seqno,
72 fd);
73 screen->fence_reference(screen, fence, NULL);
74 *fence = (struct pipe_fence_handle *)f;
75 }
76 }
77
78 /* We can't flush the texture cache within rendering a tile, so we have to
79 * flush all rendering to the kernel so that the next job reading from the
80 * tile gets a flushed cache.
81 */
82 static void
83 vc4_texture_barrier(struct pipe_context *pctx, unsigned flags)
84 {
85 vc4_flush(pctx);
86 }
87
88 static void
89 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
90 {
91 struct vc4_context *vc4 = vc4_context(pctx);
92 struct vc4_resource *rsc = vc4_resource(prsc);
93
94 rsc->initialized_buffers = 0;
95
96 struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,
97 prsc);
98 if (!entry)
99 return;
100
101 struct vc4_job *job = entry->data;
102 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
103 job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
104 }
105
106 static void
107 vc4_context_destroy(struct pipe_context *pctx)
108 {
109 struct vc4_context *vc4 = vc4_context(pctx);
110
111 vc4_flush(pctx);
112
113 if (vc4->blitter)
114 util_blitter_destroy(vc4->blitter);
115
116 if (vc4->primconvert)
117 util_primconvert_destroy(vc4->primconvert);
118
119 if (vc4->uploader)
120 u_upload_destroy(vc4->uploader);
121
122 slab_destroy_child(&vc4->transfer_pool);
123
124 pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
125 pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
126
127 if (vc4->yuv_linear_blit_vs)
128 pctx->delete_vs_state(pctx, vc4->yuv_linear_blit_vs);
129 if (vc4->yuv_linear_blit_fs_8bit)
130 pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_8bit);
131 if (vc4->yuv_linear_blit_fs_16bit)
132 pctx->delete_fs_state(pctx, vc4->yuv_linear_blit_fs_16bit);
133
134 vc4_program_fini(pctx);
135
136 if (vc4->screen->has_syncobj) {
137 drmSyncobjDestroy(vc4->fd, vc4->job_syncobj);
138 drmSyncobjDestroy(vc4->fd, vc4->in_syncobj);
139 }
140 if (vc4->in_fence_fd >= 0)
141 close(vc4->in_fence_fd);
142
143 ralloc_free(vc4);
144 }
145
146 struct pipe_context *
147 vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
148 {
149 struct vc4_screen *screen = vc4_screen(pscreen);
150 struct vc4_context *vc4;
151 int err;
152
153 /* Prevent dumping of the shaders built during context setup. */
154 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
155 vc4_debug &= ~VC4_DEBUG_SHADERDB;
156
157 vc4 = rzalloc(NULL, struct vc4_context);
158 if (!vc4)
159 return NULL;
160 struct pipe_context *pctx = &vc4->base;
161
162 vc4->screen = screen;
163
164 pctx->screen = pscreen;
165 pctx->priv = priv;
166 pctx->destroy = vc4_context_destroy;
167 pctx->flush = vc4_pipe_flush;
168 pctx->invalidate_resource = vc4_invalidate_resource;
169 pctx->texture_barrier = vc4_texture_barrier;
170
171 vc4_draw_init(pctx);
172 vc4_state_init(pctx);
173 vc4_program_init(pctx);
174 vc4_query_init(pctx);
175 vc4_resource_context_init(pctx);
176
177 vc4->fd = screen->fd;
178
179 err = vc4_job_init(vc4);
180 if (err)
181 goto fail;
182
183 err = vc4_fence_context_init(vc4);
184 if (err)
185 goto fail;
186
187 slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);
188
189 vc4->uploader = u_upload_create_default(&vc4->base);
190 vc4->base.stream_uploader = vc4->uploader;
191 vc4->base.const_uploader = vc4->uploader;
192
193 vc4->blitter = util_blitter_create(pctx);
194 if (!vc4->blitter)
195 goto fail;
196
197 vc4->primconvert = util_primconvert_create(pctx,
198 (1 << PIPE_PRIM_QUADS) - 1);
199 if (!vc4->primconvert)
200 goto fail;
201
202 vc4_debug |= saved_shaderdb_flag;
203
204 vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
205
206 return &vc4->base;
207
208 fail:
209 pctx->destroy(pctx);
210 return NULL;
211 }