freedreno: ctx should hold ref to dev
[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_resource.h"
32 #include "freedreno_texture.h"
33 #include "freedreno_state.h"
34 #include "freedreno_gmem.h"
35 #include "freedreno_query.h"
36 #include "freedreno_util.h"
37
38 static struct fd_ringbuffer *next_rb(struct fd_context *ctx)
39 {
40 struct fd_ringbuffer *ring;
41 uint32_t ts;
42
43 /* grab next ringbuffer: */
44 ring = ctx->rings[(ctx->rings_idx++) % ARRAY_SIZE(ctx->rings)];
45
46 /* wait for new rb to be idle: */
47 ts = fd_ringbuffer_timestamp(ring);
48 if (ts) {
49 DBG("wait: %u", ts);
50 fd_pipe_wait(ctx->screen->pipe, ts);
51 }
52
53 fd_ringbuffer_reset(ring);
54
55 return ring;
56 }
57
58 static void
59 fd_context_next_rb(struct pipe_context *pctx)
60 {
61 struct fd_context *ctx = fd_context(pctx);
62 struct fd_ringbuffer *ring;
63
64 fd_ringmarker_del(ctx->draw_start);
65 fd_ringmarker_del(ctx->draw_end);
66
67 ring = next_rb(ctx);
68
69 ctx->draw_start = fd_ringmarker_new(ring);
70 ctx->draw_end = fd_ringmarker_new(ring);
71
72 fd_ringbuffer_set_parent(ring, NULL);
73 ctx->ring = ring;
74
75 fd_ringmarker_del(ctx->binning_start);
76 fd_ringmarker_del(ctx->binning_end);
77
78 ring = next_rb(ctx);
79
80 ctx->binning_start = fd_ringmarker_new(ring);
81 ctx->binning_end = fd_ringmarker_new(ring);
82
83 fd_ringbuffer_set_parent(ring, ctx->ring);
84 ctx->binning_ring = ring;
85 }
86
87 /* emit accumulated render cmds, needed for example if render target has
88 * changed, or for flush()
89 */
90 void
91 fd_context_render(struct pipe_context *pctx)
92 {
93 struct fd_context *ctx = fd_context(pctx);
94 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
95
96 DBG("needs_flush: %d", ctx->needs_flush);
97
98 if (!ctx->needs_flush)
99 return;
100
101 fd_gmem_render_tiles(pctx);
102
103 DBG("%p/%p/%p", ctx->ring->start, ctx->ring->cur, ctx->ring->end);
104
105 /* if size in dwords is more than half the buffer size, then wait and
106 * wrap around:
107 */
108 if ((ctx->ring->cur - ctx->ring->start) > ctx->ring->size/8)
109 fd_context_next_rb(pctx);
110
111 ctx->needs_flush = false;
112 ctx->cleared = ctx->restore = ctx->resolve = 0;
113 ctx->gmem_reason = 0;
114 ctx->num_draws = 0;
115
116 if (pfb->cbufs[0])
117 fd_resource(pfb->cbufs[0]->texture)->dirty = false;
118 if (pfb->zsbuf)
119 fd_resource(pfb->zsbuf->texture)->dirty = false;
120 }
121
122 static void
123 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
124 unsigned flags)
125 {
126 DBG("fence=%p", fence);
127
128 #if 0
129 if (fence) {
130 fd_fence_ref(ctx->screen->fence.current,
131 (struct fd_fence **)fence);
132 }
133 #endif
134
135 fd_context_render(pctx);
136 }
137
138 void
139 fd_context_destroy(struct pipe_context *pctx)
140 {
141 struct fd_context *ctx = fd_context(pctx);
142 unsigned i;
143
144 DBG("");
145
146 util_slab_destroy(&ctx->transfer_pool);
147
148 util_dynarray_fini(&ctx->draw_patches);
149
150 if (ctx->blitter)
151 util_blitter_destroy(ctx->blitter);
152
153 if (ctx->primconvert)
154 util_primconvert_destroy(ctx->primconvert);
155
156 fd_ringmarker_del(ctx->draw_start);
157 fd_ringmarker_del(ctx->draw_end);
158 fd_ringmarker_del(ctx->binning_start);
159 fd_ringmarker_del(ctx->binning_end);
160
161 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++)
162 fd_ringbuffer_del(ctx->rings[i]);
163
164 for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
165 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
166 if (!pipe->bo)
167 break;
168 fd_bo_del(pipe->bo);
169 }
170
171 fd_device_del(ctx->dev);
172
173 FREE(ctx);
174 }
175
176 struct pipe_context *
177 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
178 const uint8_t *primtypes, void *priv)
179 {
180 struct fd_screen *screen = fd_screen(pscreen);
181 struct pipe_context *pctx;
182 int i;
183
184 ctx->screen = screen;
185
186 ctx->primtypes = primtypes;
187 ctx->primtype_mask = 0;
188 for (i = 0; i < PIPE_PRIM_MAX; i++)
189 if (primtypes[i])
190 ctx->primtype_mask |= (1 << i);
191
192 /* need some sane default in case state tracker doesn't
193 * set some state:
194 */
195 ctx->sample_mask = 0xffff;
196
197 pctx = &ctx->base;
198 pctx->screen = pscreen;
199 pctx->priv = priv;
200 pctx->flush = fd_context_flush;
201
202 for (i = 0; i < ARRAY_SIZE(ctx->rings); i++) {
203 ctx->rings[i] = fd_ringbuffer_new(screen->pipe, 0x400000);
204 if (!ctx->rings[i])
205 goto fail;
206 }
207
208 fd_context_next_rb(pctx);
209 fd_reset_rmw_state(ctx);
210
211 util_dynarray_init(&ctx->draw_patches);
212
213 util_slab_create(&ctx->transfer_pool, sizeof(struct pipe_transfer),
214 16, UTIL_SLAB_SINGLETHREADED);
215
216 fd_draw_init(pctx);
217 fd_resource_context_init(pctx);
218 fd_query_context_init(pctx);
219 fd_texture_init(pctx);
220 fd_state_init(pctx);
221
222 ctx->blitter = util_blitter_create(pctx);
223 if (!ctx->blitter)
224 goto fail;
225
226 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
227 if (!ctx->primconvert)
228 goto fail;
229
230 return pctx;
231
232 fail:
233 pctx->destroy(pctx);
234 return NULL;
235 }