2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include "freedreno_context.h"
28 #include "freedreno_blitter.h"
29 #include "freedreno_draw.h"
30 #include "freedreno_fence.h"
31 #include "freedreno_log.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 #include "util/u_upload_mgr.h"
43 #include "util/u_process.h"
45 #include <sys/types.h>
49 fd_context_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fencep
,
52 struct fd_context
*ctx
= fd_context(pctx
);
53 struct pipe_fence_handle
*fence
= NULL
;
54 // TODO we want to lookup batch if it exists, but not create one if not.
55 struct fd_batch
*batch
= fd_context_batch(ctx
);
57 DBG("%p: flush: flags=%x\n", ctx
->batch
, flags
);
59 /* In some sequence of events, we can end up with a last_fence that is
60 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
64 if (flags
& PIPE_FLUSH_FENCE_FD
)
65 fd_fence_ref(&ctx
->last_fence
, NULL
);
67 /* if no rendering since last flush, ie. app just decided it needed
68 * a fence, re-use the last one:
70 if (ctx
->last_fence
) {
71 fd_fence_ref(&fence
, ctx
->last_fence
);
78 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
79 fd_fence_ref(&fence
, batch
->fence
);
81 if (flags
& PIPE_FLUSH_FENCE_FD
)
82 batch
->needs_out_fence_fd
= true;
84 if (!ctx
->screen
->reorder
) {
85 fd_batch_flush(batch
);
86 } else if (flags
& PIPE_FLUSH_DEFERRED
) {
87 fd_bc_flush_deferred(&ctx
->screen
->batch_cache
, ctx
);
89 fd_bc_flush(&ctx
->screen
->batch_cache
, ctx
);
94 fd_fence_ref(fencep
, fence
);
96 fd_fence_ref(&ctx
->last_fence
, fence
);
98 fd_fence_ref(&fence
, NULL
);
100 if (flags
& PIPE_FLUSH_END_OF_FRAME
)
105 fd_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
107 if (flags
== PIPE_TEXTURE_BARRIER_FRAMEBUFFER
) {
108 struct fd_context
*ctx
= fd_context(pctx
);
110 if (ctx
->framebuffer_barrier
) {
111 ctx
->framebuffer_barrier(ctx
);
116 /* On devices that could sample from GMEM we could possibly do better.
117 * Or if we knew that we were doing GMEM bypass we could just emit a
118 * cache flush, perhaps? But we don't know if future draws would cause
119 * us to use GMEM, and a flush in bypass isn't the end of the world.
121 fd_context_flush(pctx
, NULL
, 0);
125 fd_memory_barrier(struct pipe_context
*pctx
, unsigned flags
)
127 if (!(flags
& ~PIPE_BARRIER_UPDATE
))
130 fd_context_flush(pctx
, NULL
, 0);
131 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
135 * emit marker string as payload of a no-op packet, which can be
136 * decoded by cffdump.
139 fd_emit_string_marker(struct pipe_context
*pctx
, const char *string
, int len
)
141 struct fd_context
*ctx
= fd_context(pctx
);
142 struct fd_ringbuffer
*ring
;
143 const uint32_t *buf
= (const void *)string
;
148 ctx
->batch
->needs_flush
= true;
150 ring
= ctx
->batch
->draw
;
152 /* max packet size is 0x3fff dwords: */
153 len
= MIN2(len
, 0x3fff * 4);
155 if (ctx
->screen
->gpu_id
>= 500)
156 OUT_PKT7(ring
, CP_NOP
, align(len
, 4) / 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
);
181 fd_log_process(ctx
, true);
182 assert(list_is_empty(&ctx
->log_chunks
));
184 fd_fence_ref(&ctx
->last_fence
, NULL
);
186 util_copy_framebuffer_state(&ctx
->framebuffer
, NULL
);
187 fd_batch_reference(&ctx
->batch
, NULL
); /* unref current batch */
188 fd_bc_invalidate_context(ctx
);
193 util_blitter_destroy(ctx
->blitter
);
195 if (pctx
->stream_uploader
)
196 u_upload_destroy(pctx
->stream_uploader
);
198 if (ctx
->clear_rs_state
)
199 pctx
->delete_rasterizer_state(pctx
, ctx
->clear_rs_state
);
201 if (ctx
->primconvert
)
202 util_primconvert_destroy(ctx
->primconvert
);
204 slab_destroy_child(&ctx
->transfer_pool
);
206 for (i
= 0; i
< ARRAY_SIZE(ctx
->vsc_pipe_bo
); i
++) {
207 if (!ctx
->vsc_pipe_bo
[i
])
209 fd_bo_del(ctx
->vsc_pipe_bo
[i
]);
212 fd_device_del(ctx
->dev
);
213 fd_pipe_del(ctx
->pipe
);
215 mtx_destroy(&ctx
->gmem_lock
);
217 if (fd_mesa_debug
& (FD_DBG_BSTAT
| FD_DBG_MSGS
)) {
218 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
219 (uint32_t)ctx
->stats
.batch_total
, (uint32_t)ctx
->stats
.batch_sysmem
,
220 (uint32_t)ctx
->stats
.batch_gmem
, (uint32_t)ctx
->stats
.batch_nondraw
,
221 (uint32_t)ctx
->stats
.batch_restore
);
226 fd_set_debug_callback(struct pipe_context
*pctx
,
227 const struct pipe_debug_callback
*cb
)
229 struct fd_context
*ctx
= fd_context(pctx
);
234 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
238 fd_get_reset_count(struct fd_context
*ctx
, bool per_context
)
241 enum fd_param_id param
=
242 per_context
? FD_CTX_FAULTS
: FD_GLOBAL_FAULTS
;
243 int ret
= fd_pipe_get_param(ctx
->pipe
, param
, &val
);
248 static enum pipe_reset_status
249 fd_get_device_reset_status(struct pipe_context
*pctx
)
251 struct fd_context
*ctx
= fd_context(pctx
);
252 int context_faults
= fd_get_reset_count(ctx
, true);
253 int global_faults
= fd_get_reset_count(ctx
, false);
254 enum pipe_reset_status status
;
256 if (context_faults
!= ctx
->context_reset_count
) {
257 status
= PIPE_GUILTY_CONTEXT_RESET
;
258 } else if (global_faults
!= ctx
->global_reset_count
) {
259 status
= PIPE_INNOCENT_CONTEXT_RESET
;
261 status
= PIPE_NO_RESET
;
264 ctx
->context_reset_count
= context_faults
;
265 ctx
->global_reset_count
= global_faults
;
270 /* TODO we could combine a few of these small buffers (solid_vbuf,
271 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
272 * save a tiny bit of memory
275 static struct pipe_resource
*
276 create_solid_vertexbuf(struct pipe_context
*pctx
)
278 static const float init_shader_const
[] = {
279 -1.000000, +1.000000, +1.000000,
280 +1.000000, -1.000000, +1.000000,
282 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
283 PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, sizeof(init_shader_const
));
284 pipe_buffer_write(pctx
, prsc
, 0,
285 sizeof(init_shader_const
), init_shader_const
);
289 static struct pipe_resource
*
290 create_blit_texcoord_vertexbuf(struct pipe_context
*pctx
)
292 struct pipe_resource
*prsc
= pipe_buffer_create(pctx
->screen
,
293 PIPE_BIND_CUSTOM
, PIPE_USAGE_DYNAMIC
, 16);
298 fd_context_setup_common_vbos(struct fd_context
*ctx
)
300 struct pipe_context
*pctx
= &ctx
->base
;
302 ctx
->solid_vbuf
= create_solid_vertexbuf(pctx
);
303 ctx
->blit_texcoord_vbuf
= create_blit_texcoord_vertexbuf(pctx
);
305 /* setup solid_vbuf_state: */
306 ctx
->solid_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
307 pctx
, 1, (struct pipe_vertex_element
[]){{
308 .vertex_buffer_index
= 0,
310 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
312 ctx
->solid_vbuf_state
.vertexbuf
.count
= 1;
313 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].stride
= 12;
314 ctx
->solid_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->solid_vbuf
;
316 /* setup blit_vbuf_state: */
317 ctx
->blit_vbuf_state
.vtx
= pctx
->create_vertex_elements_state(
318 pctx
, 2, (struct pipe_vertex_element
[]){{
319 .vertex_buffer_index
= 0,
321 .src_format
= PIPE_FORMAT_R32G32_FLOAT
,
323 .vertex_buffer_index
= 1,
325 .src_format
= PIPE_FORMAT_R32G32B32_FLOAT
,
327 ctx
->blit_vbuf_state
.vertexbuf
.count
= 2;
328 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].stride
= 8;
329 ctx
->blit_vbuf_state
.vertexbuf
.vb
[0].buffer
.resource
= ctx
->blit_texcoord_vbuf
;
330 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].stride
= 12;
331 ctx
->blit_vbuf_state
.vertexbuf
.vb
[1].buffer
.resource
= ctx
->solid_vbuf
;
335 fd_context_cleanup_common_vbos(struct fd_context
*ctx
)
337 struct pipe_context
*pctx
= &ctx
->base
;
339 pctx
->delete_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
340 pctx
->delete_vertex_elements_state(pctx
, ctx
->blit_vbuf_state
.vtx
);
342 pipe_resource_reference(&ctx
->solid_vbuf
, NULL
);
343 pipe_resource_reference(&ctx
->blit_texcoord_vbuf
, NULL
);
346 struct pipe_context
*
347 fd_context_init(struct fd_context
*ctx
, struct pipe_screen
*pscreen
,
348 const uint8_t *primtypes
, void *priv
, unsigned flags
)
350 struct fd_screen
*screen
= fd_screen(pscreen
);
351 struct pipe_context
*pctx
;
355 /* lower numerical value == higher priority: */
356 if (fd_mesa_debug
& FD_DBG_HIPRIO
)
358 else if (flags
& PIPE_CONTEXT_HIGH_PRIORITY
)
360 else if (flags
& PIPE_CONTEXT_LOW_PRIORITY
)
363 ctx
->screen
= screen
;
364 ctx
->pipe
= fd_pipe_new2(screen
->dev
, FD_PIPE_3D
, prio
);
366 if (fd_device_version(screen
->dev
) >= FD_VERSION_ROBUSTNESS
) {
367 ctx
->context_reset_count
= fd_get_reset_count(ctx
, true);
368 ctx
->global_reset_count
= fd_get_reset_count(ctx
, false);
371 ctx
->primtypes
= primtypes
;
372 ctx
->primtype_mask
= 0;
373 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++)
375 ctx
->primtype_mask
|= (1 << i
);
377 (void) mtx_init(&ctx
->gmem_lock
, mtx_plain
);
379 /* need some sane default in case state tracker doesn't
382 ctx
->sample_mask
= 0xffff;
383 ctx
->active_queries
= true;
386 pctx
->screen
= pscreen
;
388 pctx
->flush
= fd_context_flush
;
389 pctx
->emit_string_marker
= fd_emit_string_marker
;
390 pctx
->set_debug_callback
= fd_set_debug_callback
;
391 pctx
->get_device_reset_status
= fd_get_device_reset_status
;
392 pctx
->create_fence_fd
= fd_create_fence_fd
;
393 pctx
->fence_server_sync
= fd_fence_server_sync
;
394 pctx
->texture_barrier
= fd_texture_barrier
;
395 pctx
->memory_barrier
= fd_memory_barrier
;
397 pctx
->stream_uploader
= u_upload_create_default(pctx
);
398 if (!pctx
->stream_uploader
)
400 pctx
->const_uploader
= pctx
->stream_uploader
;
402 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);
405 fd_resource_context_init(pctx
);
406 fd_query_context_init(pctx
);
407 fd_texture_init(pctx
);
410 ctx
->blitter
= util_blitter_create(pctx
);
414 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->primtype_mask
);
415 if (!ctx
->primconvert
)
418 list_inithead(&ctx
->hw_active_queries
);
419 list_inithead(&ctx
->acc_active_queries
);
420 list_inithead(&ctx
->log_chunks
);
422 ctx
->log_out
= stdout
;
424 if ((fd_mesa_debug
& FD_DBG_LOG
) &&
425 !(ctx
->record_timestamp
&& ctx
->ts_to_ns
)) {
426 printf("logging not supported!\n");
427 fd_mesa_debug
&= ~FD_DBG_LOG
;
430 #if DETECT_OS_ANDROID
431 if (fd_mesa_debug
&& FD_DBG_LOG
) {
432 static unsigned idx
= 0;
434 asprintf(&p
, "/data/fdlog/%s-%d.log", util_get_process_name(), idx
++);
436 FILE *f
= fopen(p
, "w");