freedreno: push resource tracking down into batch
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "freedreno_context.h"
30 #include "freedreno_draw.h"
31 #include "freedreno_fence.h"
32 #include "freedreno_program.h"
33 #include "freedreno_resource.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_state.h"
36 #include "freedreno_gmem.h"
37 #include "freedreno_query.h"
38 #include "freedreno_query_hw.h"
39 #include "freedreno_util.h"
40
41 /* emit accumulated render cmds, needed for example if render target has
42 * changed, or for flush()
43 */
44 void
45 fd_context_render(struct pipe_context *pctx)
46 {
47 struct fd_context *ctx = fd_context(pctx);
48
49 DBG("needs_flush: %d", ctx->needs_flush);
50
51 if (!ctx->needs_flush)
52 return;
53
54 fd_batch_flush(ctx->batch);
55
56 fd_batch_reference(&ctx->batch, NULL);
57 ctx->batch = fd_batch_create(ctx);
58
59 ctx->needs_flush = false;
60 ctx->cleared = ctx->partial_cleared = ctx->restore = ctx->resolve = 0;
61 ctx->gmem_reason = 0;
62 ctx->num_draws = 0;
63 }
64
65 static void
66 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
67 unsigned flags)
68 {
69 struct fd_batch *batch = NULL;
70
71 fd_batch_reference(&batch, fd_context(pctx)->batch);
72
73 fd_context_render(pctx);
74
75 if (fence) {
76 fd_screen_fence_ref(pctx->screen, fence, NULL);
77 *fence = fd_fence_create(pctx, fd_ringbuffer_timestamp(batch->gmem));
78 }
79
80 fd_batch_reference(&batch, NULL);
81 }
82
83 /**
84 * emit marker string as payload of a no-op packet, which can be
85 * decoded by cffdump.
86 */
87 static void
88 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
89 {
90 struct fd_context *ctx = fd_context(pctx);
91 struct fd_ringbuffer *ring = ctx->batch->draw;
92 const uint32_t *buf = (const void *)string;
93
94 /* max packet size is 0x3fff dwords: */
95 len = MIN2(len, 0x3fff * 4);
96
97 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
98 while (len >= 4) {
99 OUT_RING(ring, *buf);
100 buf++;
101 len -= 4;
102 }
103
104 /* copy remainder bytes without reading past end of input string: */
105 if (len > 0) {
106 uint32_t w = 0;
107 memcpy(&w, buf, len);
108 OUT_RING(ring, w);
109 }
110 }
111
112 void
113 fd_context_destroy(struct pipe_context *pctx)
114 {
115 struct fd_context *ctx = fd_context(pctx);
116 unsigned i;
117
118 DBG("");
119
120 fd_prog_fini(pctx);
121 fd_hw_query_fini(pctx);
122
123 util_dynarray_fini(&ctx->draw_patches);
124
125 if (ctx->blitter)
126 util_blitter_destroy(ctx->blitter);
127
128 if (ctx->primconvert)
129 util_primconvert_destroy(ctx->primconvert);
130
131 util_slab_destroy(&ctx->transfer_pool);
132
133 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
134
135 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
136 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
137 if (!pipe->bo)
138 break;
139 fd_bo_del(pipe->bo);
140 }
141
142 fd_device_del(ctx->dev);
143
144 FREE(ctx);
145 }
146
147 static void
148 fd_set_debug_callback(struct pipe_context *pctx,
149 const struct pipe_debug_callback *cb)
150 {
151 struct fd_context *ctx = fd_context(pctx);
152
153 if (cb)
154 ctx->debug = *cb;
155 else
156 memset(&ctx->debug, 0, sizeof(ctx->debug));
157 }
158
159 struct pipe_context *
160 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
161 const uint8_t *primtypes, void *priv)
162 {
163 struct fd_screen *screen = fd_screen(pscreen);
164 struct pipe_context *pctx;
165 int i;
166
167 ctx->screen = screen;
168
169 ctx->primtypes = primtypes;
170 ctx->primtype_mask = 0;
171 for (i = 0; i < PIPE_PRIM_MAX; i++)
172 if (primtypes[i])
173 ctx->primtype_mask |= (1 << i);
174
175 /* need some sane default in case state tracker doesn't
176 * set some state:
177 */
178 ctx->sample_mask = 0xffff;
179
180 ctx->stage = FD_STAGE_NULL;
181
182 pctx = &ctx->base;
183 pctx->screen = pscreen;
184 pctx->priv = priv;
185 pctx->flush = fd_context_flush;
186 pctx->emit_string_marker = fd_emit_string_marker;
187 pctx->set_debug_callback = fd_set_debug_callback;
188
189 ctx->batch = fd_batch_create(ctx);
190
191 fd_reset_wfi(ctx);
192
193 util_dynarray_init(&ctx->draw_patches);
194
195 util_slab_create(&ctx->transfer_pool, sizeof(struct fd_transfer),
196 16, UTIL_SLAB_SINGLETHREADED);
197
198 fd_draw_init(pctx);
199 fd_resource_context_init(pctx);
200 fd_query_context_init(pctx);
201 fd_texture_init(pctx);
202 fd_state_init(pctx);
203 fd_hw_query_init(pctx);
204
205 ctx->blitter = util_blitter_create(pctx);
206 if (!ctx->blitter)
207 goto fail;
208
209 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
210 if (!ctx->primconvert)
211 goto fail;
212
213 return pctx;
214
215 fail:
216 pctx->destroy(pctx);
217 return NULL;
218 }