1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
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"
41 static struct fd_ringbuffer
*next_rb(struct fd_context
*ctx
)
43 struct fd_ringbuffer
*ring
;
46 /* grab next ringbuffer: */
47 ring
= ctx
->rings
[(ctx
->rings_idx
++) % ARRAY_SIZE(ctx
->rings
)];
49 /* wait for new rb to be idle: */
50 ts
= fd_ringbuffer_timestamp(ring
);
53 fd_pipe_wait(ctx
->screen
->pipe
, ts
);
56 fd_ringbuffer_reset(ring
);
62 fd_context_next_rb(struct pipe_context
*pctx
)
64 struct fd_context
*ctx
= fd_context(pctx
);
65 struct fd_ringbuffer
*ring
;
67 fd_ringmarker_del(ctx
->draw_start
);
68 fd_ringmarker_del(ctx
->draw_end
);
72 ctx
->draw_start
= fd_ringmarker_new(ring
);
73 ctx
->draw_end
= fd_ringmarker_new(ring
);
75 fd_ringbuffer_set_parent(ring
, NULL
);
78 fd_ringmarker_del(ctx
->binning_start
);
79 fd_ringmarker_del(ctx
->binning_end
);
83 ctx
->binning_start
= fd_ringmarker_new(ring
);
84 ctx
->binning_end
= fd_ringmarker_new(ring
);
86 fd_ringbuffer_set_parent(ring
, ctx
->ring
);
87 ctx
->binning_ring
= ring
;
90 /* emit accumulated render cmds, needed for example if render target has
91 * changed, or for flush()
94 fd_context_render(struct pipe_context
*pctx
)
96 struct fd_context
*ctx
= fd_context(pctx
);
97 struct fd_resource
*rsc
, *rsc_tmp
;
99 DBG("needs_flush: %d", ctx
->needs_flush
);
101 if (!ctx
->needs_flush
)
104 fd_gmem_render_tiles(ctx
);
106 DBG("%p/%p/%p", ctx
->ring
->start
, ctx
->ring
->cur
, ctx
->ring
->end
);
108 /* if size in dwords is more than half the buffer size, then wait and
111 if ((ctx
->ring
->cur
- ctx
->ring
->start
) > ctx
->ring
->size
/8)
112 fd_context_next_rb(pctx
);
114 ctx
->needs_flush
= false;
115 ctx
->cleared
= ctx
->partial_cleared
= ctx
->restore
= ctx
->resolve
= 0;
116 ctx
->gmem_reason
= 0;
119 /* go through all the used resources and clear their reading flag */
120 LIST_FOR_EACH_ENTRY_SAFE(rsc
, rsc_tmp
, &ctx
->used_resources
, list
) {
121 debug_assert(rsc
->status
!= 0);
123 rsc
->pending_ctx
= NULL
;
124 list_delinit(&rsc
->list
);
127 assert(LIST_IS_EMPTY(&ctx
->used_resources
));
131 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
134 struct fd_ringbuffer
*ring
= fd_context(pctx
)->ring
;
136 fd_context_render(pctx
);
139 fd_screen_fence_ref(pctx
->screen
, fence
, NULL
);
140 *fence
= fd_fence_create(pctx
, fd_ringbuffer_timestamp(ring
));
145 * emit marker string as payload of a no-op packet, which can be
146 * decoded by cffdump.
149 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
151 struct fd_context
*ctx
= fd_context(pctx
);
152 struct fd_ringbuffer
*ring
= ctx
->ring
;
153 const uint32_t *buf
= (const void *)string
;
155 /* max packet size is 0x3fff dwords: */
156 len
= MIN2(len
, 0x3fff * 4);
158 OUT_PKT3(ring
, CP_NOP
, align(len
, 4) / 4);
160 OUT_RING(ring
, *buf
);
165 /* copy remainder bytes without reading past end of input string: */
168 memcpy(&w
, buf
, len
);
174 fd_context_destroy(struct pipe_context
*pctx
)
176 struct fd_context
*ctx
= fd_context(pctx
);
182 fd_hw_query_fini(pctx
);
184 util_dynarray_fini(&ctx
->draw_patches
);
187 util_blitter_destroy(ctx
->blitter
);
189 if (ctx
->primconvert
)
190 util_primconvert_destroy(ctx
->primconvert
);
192 util_slab_destroy(&ctx
->transfer_pool
);
194 fd_ringmarker_del(ctx
->draw_start
);
195 fd_ringmarker_del(ctx
->draw_end
);
196 fd_ringmarker_del(ctx
->binning_start
);
197 fd_ringmarker_del(ctx
->binning_end
);
199 for (i
= 0; i
< ARRAY_SIZE(ctx
->rings
); i
++)
200 fd_ringbuffer_del(ctx
->rings
[i
]);
202 for (i
= 0; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
203 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
209 fd_device_del(ctx
->dev
);
215 fd_set_debug_callback(struct pipe_context
*pctx
,
216 const struct pipe_debug_callback
*cb
)
218 struct fd_context
*ctx
= fd_context(pctx
);
223 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
226 struct pipe_context
*
227 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
228 const uint8_t *primtypes
, void *priv
)
230 struct fd_screen
*screen
= fd_screen(pscreen
);
231 struct pipe_context
*pctx
;
234 ctx
->screen
= screen
;
236 ctx
->primtypes
= primtypes
;
237 ctx
->primtype_mask
= 0;
238 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
240 ctx
->primtype_mask
|= (1 << i
);
242 /* need some sane default in case state tracker doesn't
245 ctx
->sample_mask
= 0xffff;
248 pctx
->screen
= pscreen
;
250 pctx
->flush
= fd_context_flush
;
251 pctx
->emit_string_marker
= fd_emit_string_marker
;
252 pctx
->set_debug_callback
= fd_set_debug_callback
;
254 for (i
= 0; i
< ARRAY_SIZE(ctx
->rings
); i
++) {
255 ctx
->rings
[i
] = fd_ringbuffer_new(screen
->pipe
, 0x100000);
260 fd_context_next_rb(pctx
);
263 util_dynarray_init(&ctx
->draw_patches
);
265 util_slab_create(&ctx
->transfer_pool
, sizeof(struct fd_transfer
),
266 16, UTIL_SLAB_SINGLETHREADED
);
269 fd_resource_context_init(pctx
);
270 fd_query_context_init(pctx
);
271 fd_texture_init(pctx
);
273 fd_hw_query_init(pctx
);
275 ctx
->blitter
= util_blitter_create(pctx
);
279 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
280 if (!ctx
->primconvert
)