1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_memory.h"
28 #include "util/u_inlines.h"
29
30 #include "freedreno_query_acc.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_util.h"
34
35
36 static bool
is_active(struct fd_acc_query * aq,enum fd_render_stage stage)37 is_active(struct fd_acc_query *aq, enum fd_render_stage stage)
38 {
39 return !!(aq->provider->active & stage);
40 }
41
42 static void
fd_acc_destroy_query(struct fd_context * ctx,struct fd_query * q)43 fd_acc_destroy_query(struct fd_context *ctx, struct fd_query *q)
44 {
45 struct fd_acc_query *aq = fd_acc_query(q);
46
47 DBG("%p: active=%d", q, q->active);
48
49 pipe_resource_reference(&aq->prsc, NULL);
50 list_del(&aq->node);
51
52 free(aq);
53 }
54
55 static void
realloc_query_bo(struct fd_context * ctx,struct fd_acc_query * aq)56 realloc_query_bo(struct fd_context *ctx, struct fd_acc_query *aq)
57 {
58 struct fd_resource *rsc;
59 void *map;
60
61 pipe_resource_reference(&aq->prsc, NULL);
62
63 aq->prsc = pipe_buffer_create(&ctx->screen->base,
64 PIPE_BIND_QUERY_BUFFER, 0, 0x1000);
65
66 /* don't assume the buffer is zero-initialized: */
67 rsc = fd_resource(aq->prsc);
68
69 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_WRITE);
70
71 map = fd_bo_map(rsc->bo);
72 memset(map, 0, aq->provider->size);
73 fd_bo_cpu_fini(rsc->bo);
74 }
75
76 static boolean
fd_acc_begin_query(struct fd_context * ctx,struct fd_query * q)77 fd_acc_begin_query(struct fd_context *ctx, struct fd_query *q)
78 {
79 struct fd_batch *batch = ctx->batch;
80 struct fd_acc_query *aq = fd_acc_query(q);
81 const struct fd_acc_sample_provider *p = aq->provider;
82
83 DBG("%p: active=%d", q, q->active);
84
85 /* ->begin_query() discards previous results, so realloc bo: */
86 realloc_query_bo(ctx, aq);
87
88 /* then resume query if needed to collect first sample: */
89 if (batch && is_active(aq, batch->stage))
90 p->resume(aq, batch);
91
92 /* add to active list: */
93 assert(list_empty(&aq->node));
94 list_addtail(&aq->node, &ctx->acc_active_queries);
95
96 return true;
97 }
98
99 static void
fd_acc_end_query(struct fd_context * ctx,struct fd_query * q)100 fd_acc_end_query(struct fd_context *ctx, struct fd_query *q)
101 {
102 struct fd_batch *batch = ctx->batch;
103 struct fd_acc_query *aq = fd_acc_query(q);
104 const struct fd_acc_sample_provider *p = aq->provider;
105
106 DBG("%p: active=%d", q, q->active);
107
108 if (batch && is_active(aq, batch->stage))
109 p->pause(aq, batch);
110
111 /* remove from active list: */
112 list_delinit(&aq->node);
113 }
114
115 static boolean
fd_acc_get_query_result(struct fd_context * ctx,struct fd_query * q,boolean wait,union pipe_query_result * result)116 fd_acc_get_query_result(struct fd_context *ctx, struct fd_query *q,
117 boolean wait, union pipe_query_result *result)
118 {
119 struct fd_acc_query *aq = fd_acc_query(q);
120 const struct fd_acc_sample_provider *p = aq->provider;
121 struct fd_resource *rsc = fd_resource(aq->prsc);
122
123 DBG("%p: wait=%d, active=%d", q, wait, q->active);
124
125 assert(LIST_IS_EMPTY(&aq->node));
126
127 /* if !wait, then check the last sample (the one most likely to
128 * not be ready yet) and bail if it is not ready:
129 */
130 if (!wait) {
131 int ret;
132
133 if (pending(rsc, false)) {
134 /* piglit spec@arb_occlusion_query@occlusion_query_conform
135 * test, and silly apps perhaps, get stuck in a loop trying
136 * to get query result forever with wait==false.. we don't
137 * wait to flush unnecessarily but we also don't want to
138 * spin forever:
139 */
140 if (aq->no_wait_cnt++ > 5)
141 fd_batch_flush(rsc->write_batch, false, false);
142 return false;
143 }
144
145 ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe,
146 DRM_FREEDRENO_PREP_READ | DRM_FREEDRENO_PREP_NOSYNC);
147 if (ret)
148 return false;
149
150 fd_bo_cpu_fini(rsc->bo);
151 }
152
153 if (rsc->write_batch)
154 fd_batch_flush(rsc->write_batch, true, false);
155
156 /* get the result: */
157 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_READ);
158
159 void *ptr = fd_bo_map(rsc->bo);
160 p->result(ctx, ptr, result);
161 fd_bo_cpu_fini(rsc->bo);
162
163 return true;
164 }
165
166 static const struct fd_query_funcs acc_query_funcs = {
167 .destroy_query = fd_acc_destroy_query,
168 .begin_query = fd_acc_begin_query,
169 .end_query = fd_acc_end_query,
170 .get_query_result = fd_acc_get_query_result,
171 };
172
173 struct fd_query *
fd_acc_create_query(struct fd_context * ctx,unsigned query_type)174 fd_acc_create_query(struct fd_context *ctx, unsigned query_type)
175 {
176 struct fd_acc_query *aq;
177 struct fd_query *q;
178 int idx = pidx(query_type);
179
180 if ((idx < 0) || !ctx->acc_sample_providers[idx])
181 return NULL;
182
183 aq = CALLOC_STRUCT(fd_acc_query);
184 if (!aq)
185 return NULL;
186
187 DBG("%p: query_type=%u", aq, query_type);
188
189 aq->provider = ctx->acc_sample_providers[idx];
190
191 list_inithead(&aq->node);
192
193 q = &aq->base;
194 q->funcs = &acc_query_funcs;
195 q->type = query_type;
196
197 return q;
198 }
199
200 void
fd_acc_query_set_stage(struct fd_batch * batch,enum fd_render_stage stage)201 fd_acc_query_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
202 {
203 if (stage != batch->stage) {
204 struct fd_acc_query *aq;
205 LIST_FOR_EACH_ENTRY(aq, &batch->ctx->acc_active_queries, node) {
206 const struct fd_acc_sample_provider *p = aq->provider;
207
208 bool was_active = is_active(aq, batch->stage);
209 bool now_active = is_active(aq, stage);
210
211 if (now_active && !was_active)
212 p->resume(aq, batch);
213 else if (was_active && !now_active)
214 p->pause(aq, batch);
215 }
216 }
217 }
218
219 void
fd_acc_query_register_provider(struct pipe_context * pctx,const struct fd_acc_sample_provider * provider)220 fd_acc_query_register_provider(struct pipe_context *pctx,
221 const struct fd_acc_sample_provider *provider)
222 {
223 struct fd_context *ctx = fd_context(pctx);
224 int idx = pidx(provider->query_type);
225
226 assert((0 <= idx) && (idx < MAX_HW_SAMPLE_PROVIDERS));
227 assert(!ctx->acc_sample_providers[idx]);
228
229 ctx->acc_sample_providers[idx] = provider;
230 }
231