1/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24#include "draw/draw_context.h"
25#include "draw/draw_private.h"
26
27#include "util/u_simple_list.h"
28#include "util/u_upload_mgr.h"
29
30#include "os/os_time.h"
31
32#include "r300_context.h"
33#include "r300_cs.h"
34#include "r300_emit.h"
35
36
37static void r300_flush_and_cleanup(struct r300_context *r300, unsigned flags)
38{
39    struct r300_atom *atom;
40
41    r300_emit_hyperz_end(r300);
42    r300_emit_query_end(r300);
43    if (r300->screen->caps.is_r500)
44        r500_emit_index_bias(r300, 0);
45
46    r300->flush_counter++;
47    r300->rws->cs_flush(r300->cs, flags);
48    r300->dirty_hw = 0;
49
50    /* New kitchen sink, baby. */
51    foreach_atom(r300, atom) {
52        if (atom->state || atom->allow_null_state) {
53            r300_mark_atom_dirty(r300, atom);
54        }
55    }
56    r300->vertex_arrays_dirty = TRUE;
57
58    /* Unmark HWTCL state for SWTCL. */
59    if (!r300->screen->caps.has_tcl) {
60        r300->vs_state.dirty = FALSE;
61        r300->vs_constants.dirty = FALSE;
62        r300->clip_state.dirty = FALSE;
63    }
64}
65
66void r300_flush(struct pipe_context *pipe,
67                unsigned flags,
68                struct pipe_fence_handle **fence)
69{
70    struct r300_context *r300 = r300_context(pipe);
71    struct pb_buffer **rfence = (struct pb_buffer**)fence;
72
73    if (r300->draw && !r300->draw_vbo_locked)
74	r300_draw_flush_vbuf(r300);
75
76    if (r300->screen->info.drm_minor >= 12) {
77        flags |= RADEON_FLUSH_KEEP_TILING_FLAGS;
78    }
79
80    if (rfence) {
81        /* Create a fence, which is a dummy BO. */
82        *rfence = r300->rws->buffer_create(r300->rws, 1, 1,
83                                           PIPE_BIND_CUSTOM,
84                                           RADEON_DOMAIN_GTT);
85        /* Add the fence as a dummy relocation. */
86        r300->rws->cs_add_reloc(r300->cs,
87                                r300->rws->buffer_get_cs_handle(*rfence),
88                                RADEON_USAGE_READWRITE, RADEON_DOMAIN_GTT);
89    }
90
91    if (r300->dirty_hw) {
92        r300_flush_and_cleanup(r300, flags);
93    } else {
94        if (rfence) {
95            /* We have to create a fence object, but the command stream is empty
96             * and we cannot emit an empty CS. Let's write to some reg. */
97            CS_LOCALS(r300);
98            OUT_CS_REG(RB3D_COLOR_CHANNEL_MASK, 0);
99            r300->rws->cs_flush(r300->cs, flags);
100        } else {
101            /* Even if hw is not dirty, we should at least reset the CS in case
102             * the space checking failed for the first draw operation. */
103            r300->rws->cs_flush(r300->cs, flags);
104        }
105    }
106
107    /* Update Hyper-Z status. */
108    if (r300->num_z_clears) {
109        r300->hyperz_time_of_last_flush = os_time_get();
110    } else if (!r300->hyperz_time_of_last_flush > 2000000) {
111        /* 2 seconds without a Z clear pretty much means a dead context
112         * for HyperZ. */
113
114        r300->hiz_in_use = FALSE;
115
116        /* Decompress Z buffer. */
117        if (r300->zmask_in_use) {
118            if (r300->locked_zbuffer) {
119                r300_decompress_zmask_locked(r300);
120            } else {
121                r300_decompress_zmask(r300);
122            }
123
124            r300_flush_and_cleanup(r300, flags);
125        }
126
127        /* Release HyperZ. */
128        r300->rws->cs_request_feature(r300->cs, RADEON_FID_R300_HYPERZ_ACCESS,
129                                      FALSE);
130    }
131    r300->num_z_clears = 0;
132}
133
134static void r300_flush_wrapped(struct pipe_context *pipe,
135                               struct pipe_fence_handle **fence)
136{
137    r300_flush(pipe, 0, fence);
138}
139
140void r300_init_flush_functions(struct r300_context* r300)
141{
142    r300->context.flush = r300_flush_wrapped;
143}
144