summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/freedreno/freedreno_query_acc.c
blob: 2cb1a4ddbfad304d92f743eb134a1c9592f1e7b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#include "util/u_memory.h"
#include "util/u_inlines.h"

#include "freedreno_query_acc.h"
#include "freedreno_context.h"
#include "freedreno_resource.h"
#include "freedreno_util.h"


static bool
is_active(struct fd_acc_query *aq, enum fd_render_stage stage)
{
	return !!(aq->provider->active & stage);
}

static void
fd_acc_destroy_query(struct fd_context *ctx, struct fd_query *q)
{
	struct fd_acc_query *aq = fd_acc_query(q);

	DBG("%p: active=%d", q, q->active);

	pipe_resource_reference(&aq->prsc, NULL);
	list_del(&aq->node);

	free(aq);
}

static void
realloc_query_bo(struct fd_context *ctx, struct fd_acc_query *aq)
{
	struct fd_resource *rsc;
	void *map;

	pipe_resource_reference(&aq->prsc, NULL);

	aq->prsc = pipe_buffer_create(&ctx->screen->base,
			PIPE_BIND_QUERY_BUFFER, 0, 0x1000);

	/* don't assume the buffer is zero-initialized: */
	rsc = fd_resource(aq->prsc);

	fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_WRITE);

	map = fd_bo_map(rsc->bo);
	memset(map, 0, aq->provider->size);
	fd_bo_cpu_fini(rsc->bo);
}

static boolean
fd_acc_begin_query(struct fd_context *ctx, struct fd_query *q)
{
	struct fd_batch *batch = ctx->batch;
	struct fd_acc_query *aq = fd_acc_query(q);
	const struct fd_acc_sample_provider *p = aq->provider;

	DBG("%p: active=%d", q, q->active);

	/* ->begin_query() discards previous results, so realloc bo: */
	realloc_query_bo(ctx, aq);

	/* then resume query if needed to collect first sample: */
	if (batch && is_active(aq, batch->stage))
		p->resume(aq, batch);

	/* add to active list: */
	assert(list_empty(&aq->node));
	list_addtail(&aq->node, &ctx->acc_active_queries);

	return true;
}

static void
fd_acc_end_query(struct fd_context *ctx, struct fd_query *q)
{
	struct fd_batch *batch = ctx->batch;
	struct fd_acc_query *aq = fd_acc_query(q);
	const struct fd_acc_sample_provider *p = aq->provider;

	DBG("%p: active=%d", q, q->active);

	if (batch && is_active(aq, batch->stage))
		p->pause(aq, batch);

	/* remove from active list: */
	list_delinit(&aq->node);
}

static boolean
fd_acc_get_query_result(struct fd_context *ctx, struct fd_query *q,
		boolean wait, union pipe_query_result *result)
{
	struct fd_acc_query *aq = fd_acc_query(q);
	const struct fd_acc_sample_provider *p = aq->provider;
	struct fd_resource *rsc = fd_resource(aq->prsc);

	DBG("%p: wait=%d, active=%d", q, wait, q->active);

	assert(LIST_IS_EMPTY(&aq->node));

	/* if !wait, then check the last sample (the one most likely to
	 * not be ready yet) and bail if it is not ready:
	 */
	if (!wait) {
		int ret;

		if (pending(rsc, false)) {
			/* piglit spec@arb_occlusion_query@occlusion_query_conform
			 * test, and silly apps perhaps, get stuck in a loop trying
			 * to get  query result forever with wait==false..  we don't
			 * wait to flush unnecessarily but we also don't want to
			 * spin forever:
			 */
			if (aq->no_wait_cnt++ > 5)
				fd_batch_flush(rsc->write_batch, false, false);
			return false;
		}

		ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe,
				DRM_FREEDRENO_PREP_READ | DRM_FREEDRENO_PREP_NOSYNC);
		if (ret)
			return false;

		fd_bo_cpu_fini(rsc->bo);
	}

	if (rsc->write_batch)
		fd_batch_flush(rsc->write_batch, true, false);

	/* get the result: */
	fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_READ);

	void *ptr = fd_bo_map(rsc->bo);
	p->result(ctx, ptr, result);
	fd_bo_cpu_fini(rsc->bo);

	return true;
}

static const struct fd_query_funcs acc_query_funcs = {
		.destroy_query    = fd_acc_destroy_query,
		.begin_query      = fd_acc_begin_query,
		.end_query        = fd_acc_end_query,
		.get_query_result = fd_acc_get_query_result,
};

struct fd_query *
fd_acc_create_query(struct fd_context *ctx, unsigned query_type)
{
	struct fd_acc_query *aq;
	struct fd_query *q;
	int idx = pidx(query_type);

	if ((idx < 0) || !ctx->acc_sample_providers[idx])
		return NULL;

	aq = CALLOC_STRUCT(fd_acc_query);
	if (!aq)
		return NULL;

	DBG("%p: query_type=%u", aq, query_type);

	aq->provider = ctx->acc_sample_providers[idx];

	list_inithead(&aq->node);

	q = &aq->base;
	q->funcs = &acc_query_funcs;
	q->type = query_type;

	return q;
}

void
fd_acc_query_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
{
	if (stage != batch->stage) {
		struct fd_acc_query *aq;
		LIST_FOR_EACH_ENTRY(aq, &batch->ctx->acc_active_queries, node) {
			const struct fd_acc_sample_provider *p = aq->provider;

			bool was_active = is_active(aq, batch->stage);
			bool now_active = is_active(aq, stage);

			if (now_active && !was_active)
				p->resume(aq, batch);
			else if (was_active && !now_active)
				p->pause(aq, batch);
		}
	}
}

void
fd_acc_query_register_provider(struct pipe_context *pctx,
		const struct fd_acc_sample_provider *provider)
{
	struct fd_context *ctx = fd_context(pctx);
	int idx = pidx(provider->query_type);

	assert((0 <= idx) && (idx < MAX_HW_SAMPLE_PROVIDERS));
	assert(!ctx->acc_sample_providers[idx]);

	ctx->acc_sample_providers[idx] = provider;
}