freedreno: Fix detection of being in a blit for acc queries.

The batch might not have stage == FD_STAGE_BLIT set because
fd_blitter_pipe_begin was sticking the stage on some random batch (or none
at all) rather than the one that would be used in the meta operation.

What we actually wanted to be looking at was set_active_query_state(),
which is already called by util_blitter and whose state we just needed to
track.

Fixes piglit occlusion_query_meta_no_fragments.  I haven't changed
query_hw.c's stage handling to clean the rest up because I don't have a
db410c/db820c at home to iterate over the piglit tests.

Tested-by: Marge Bot <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4356>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4356>
diff --git a/src/gallium/drivers/freedreno/a2xx/fd2_query.c b/src/gallium/drivers/freedreno/a2xx/fd2_query.c
index b51903f..5261a69 100644
--- a/src/gallium/drivers/freedreno/a2xx/fd2_query.c
+++ b/src/gallium/drivers/freedreno/a2xx/fd2_query.c
@@ -151,7 +151,7 @@
 
 static const struct fd_acc_sample_provider perfcntr = {
 		.query_type = FD_QUERY_FIRST_PERFCNTR,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.resume = perfcntr_resume,
 		.pause = perfcntr_pause,
 		.result = perfcntr_accumulate_result,
diff --git a/src/gallium/drivers/freedreno/a5xx/fd5_query.c b/src/gallium/drivers/freedreno/a5xx/fd5_query.c
index 6cf9e2d..ac11dbb 100644
--- a/src/gallium/drivers/freedreno/a5xx/fd5_query.c
+++ b/src/gallium/drivers/freedreno/a5xx/fd5_query.c
@@ -134,7 +134,6 @@
 
 static const struct fd_acc_sample_provider occlusion_counter = {
 		.query_type = PIPE_QUERY_OCCLUSION_COUNTER,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd5_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -143,7 +142,6 @@
 
 static const struct fd_acc_sample_provider occlusion_predicate = {
 		.query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd5_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -152,7 +150,6 @@
 
 static const struct fd_acc_sample_provider occlusion_predicate_conservative = {
 		.query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd5_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -229,7 +226,7 @@
 
 static const struct fd_acc_sample_provider time_elapsed = {
 		.query_type = PIPE_QUERY_TIME_ELAPSED,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.size = sizeof(struct fd5_query_sample),
 		.resume = timestamp_resume,
 		.pause = timestamp_pause,
@@ -245,7 +242,7 @@
 
 static const struct fd_acc_sample_provider timestamp = {
 		.query_type = PIPE_QUERY_TIMESTAMP,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.size = sizeof(struct fd5_query_sample),
 		.resume = timestamp_resume,
 		.pause = timestamp_pause,
@@ -366,7 +363,7 @@
 
 static const struct fd_acc_sample_provider perfcntr = {
 		.query_type = FD_QUERY_FIRST_PERFCNTR,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.resume = perfcntr_resume,
 		.pause = perfcntr_pause,
 		.result = perfcntr_accumulate_result,
diff --git a/src/gallium/drivers/freedreno/a6xx/fd6_query.c b/src/gallium/drivers/freedreno/a6xx/fd6_query.c
index 6354e03..90b9328 100644
--- a/src/gallium/drivers/freedreno/a6xx/fd6_query.c
+++ b/src/gallium/drivers/freedreno/a6xx/fd6_query.c
@@ -134,7 +134,6 @@
 
 static const struct fd_acc_sample_provider occlusion_counter = {
 		.query_type = PIPE_QUERY_OCCLUSION_COUNTER,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd6_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -143,7 +142,6 @@
 
 static const struct fd_acc_sample_provider occlusion_predicate = {
 		.query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd6_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -152,7 +150,6 @@
 
 static const struct fd_acc_sample_provider occlusion_predicate_conservative = {
 		.query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
-		.active = FD_STAGE_DRAW,
 		.size = sizeof(struct fd6_query_sample),
 		.resume = occlusion_resume,
 		.pause = occlusion_pause,
@@ -235,7 +232,7 @@
 
 static const struct fd_acc_sample_provider time_elapsed = {
 		.query_type = PIPE_QUERY_TIME_ELAPSED,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.size = sizeof(struct fd6_query_sample),
 		.resume = timestamp_resume,
 		.pause = time_elapsed_pause,
@@ -251,7 +248,7 @@
 
 static const struct fd_acc_sample_provider timestamp = {
 		.query_type = PIPE_QUERY_TIMESTAMP,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.size = sizeof(struct fd6_query_sample),
 		.resume = timestamp_resume,
 		.pause = timestamp_pause,
@@ -377,7 +374,6 @@
 
 static const struct fd_acc_sample_provider primitives_generated = {
 	.query_type = PIPE_QUERY_PRIMITIVES_GENERATED,
-	.active = FD_STAGE_DRAW,
 	.size = sizeof(struct fd6_primitives_sample),
 	.resume = primitives_generated_resume,
 	.pause = primitives_generated_pause,
@@ -432,7 +428,6 @@
 
 static const struct fd_acc_sample_provider primitives_emitted = {
 	.query_type = PIPE_QUERY_PRIMITIVES_EMITTED,
-	.active = FD_STAGE_DRAW,
 	.size = sizeof(struct fd6_primitives_sample),
 	.resume = primitives_emitted_resume,
 	.pause = primitives_emitted_pause,
@@ -553,7 +548,7 @@
 
 static const struct fd_acc_sample_provider perfcntr = {
 		.query_type = FD_QUERY_FIRST_PERFCNTR,
-		.active = FD_STAGE_ALL,
+		.always = true,
 		.resume = perfcntr_resume,
 		.pause = perfcntr_pause,
 		.result = perfcntr_accumulate_result,
diff --git a/src/gallium/drivers/freedreno/freedreno_blitter.c b/src/gallium/drivers/freedreno/freedreno_blitter.c
index 63906c3..237e86f 100644
--- a/src/gallium/drivers/freedreno/freedreno_blitter.c
+++ b/src/gallium/drivers/freedreno/freedreno_blitter.c
@@ -120,8 +120,6 @@
 static void
 fd_blitter_pipe_end(struct fd_context *ctx)
 {
-	if (ctx->batch)
-		fd_batch_set_stage(ctx->batch, FD_STAGE_NULL);
 	ctx->in_discard_blit = false;
 }
 
diff --git a/src/gallium/drivers/freedreno/freedreno_context.c b/src/gallium/drivers/freedreno/freedreno_context.c
index fc7ecff..bc2b145 100644
--- a/src/gallium/drivers/freedreno/freedreno_context.c
+++ b/src/gallium/drivers/freedreno/freedreno_context.c
@@ -367,6 +367,7 @@
 	 * set some state:
 	 */
 	ctx->sample_mask = 0xffff;
+	ctx->active_queries = true;
 
 	pctx = &ctx->base;
 	pctx->screen = pscreen;
diff --git a/src/gallium/drivers/freedreno/freedreno_context.h b/src/gallium/drivers/freedreno/freedreno_context.h
index e2edf15..f724d7b 100644
--- a/src/gallium/drivers/freedreno/freedreno_context.h
+++ b/src/gallium/drivers/freedreno/freedreno_context.h
@@ -207,6 +207,11 @@
 	 */
 	bool update_active_queries;
 
+	/* Current state of pctx->set_active_query_state() (i.e. "should drawing
+	 * be counted against non-perfcounter queries")
+	 */
+	bool active_queries;
+
 	/* table with PIPE_PRIM_MAX entries mapping PIPE_PRIM_x to
 	 * DI_PT_x value to use for draw initiator.  There are some
 	 * slight differences between generation:
@@ -468,15 +473,6 @@
 {
 	struct fd_context *ctx = batch->ctx;
 
-	/* special case: internal blits (like mipmap level generation)
-	 * go through normal draw path (via util_blitter_blit()).. but
-	 * we need to ignore the FD_STAGE_DRAW which will be set, so we
-	 * don't enable queries which should be paused during internal
-	 * blits:
-	 */
-	if (batch->stage == FD_STAGE_BLIT && stage != FD_STAGE_NULL)
-		stage = FD_STAGE_BLIT;
-
 	if (ctx->query_set_stage)
 		ctx->query_set_stage(batch, stage);
 
diff --git a/src/gallium/drivers/freedreno/freedreno_query.c b/src/gallium/drivers/freedreno/freedreno_query.c
index 1ac7e1c..010d01b 100644
--- a/src/gallium/drivers/freedreno/freedreno_query.c
+++ b/src/gallium/drivers/freedreno/freedreno_query.c
@@ -176,8 +176,11 @@
 }
 
 static void
-fd_set_active_query_state(struct pipe_context *pipe, bool enable)
+fd_set_active_query_state(struct pipe_context *pctx, bool enable)
 {
+	struct fd_context *ctx = fd_context(pctx);
+	ctx->active_queries = enable;
+	ctx->update_active_queries = true;
 }
 
 static enum pipe_driver_query_type
diff --git a/src/gallium/drivers/freedreno/freedreno_query_acc.c b/src/gallium/drivers/freedreno/freedreno_query_acc.c
index 3bcf9ac..46efe61 100644
--- a/src/gallium/drivers/freedreno/freedreno_query_acc.c
+++ b/src/gallium/drivers/freedreno/freedreno_query_acc.c
@@ -32,13 +32,6 @@
 #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)
 {
@@ -245,7 +238,8 @@
 		LIST_FOR_EACH_ENTRY(aq, &ctx->acc_active_queries, node) {
 			bool batch_change = aq->batch != batch;
 			bool was_active = aq->batch != NULL;
-			bool now_active = is_active(aq, stage);
+			bool now_active = stage != FD_STAGE_NULL &&
+				(ctx->active_queries || aq->provider->always);
 
 			if (was_active && (!now_active || batch_change))
 				fd_acc_query_pause(aq);
diff --git a/src/gallium/drivers/freedreno/freedreno_query_acc.h b/src/gallium/drivers/freedreno/freedreno_query_acc.h
index 15d5b88..2092aba 100644
--- a/src/gallium/drivers/freedreno/freedreno_query_acc.h
+++ b/src/gallium/drivers/freedreno/freedreno_query_acc.h
@@ -59,8 +59,8 @@
 struct fd_acc_sample_provider {
 	unsigned query_type;
 
-	/* stages applicable to the query type: */
-	enum fd_render_stage active;
+	/* Set if the provider should still count while !ctx->active_queries */
+	bool always;
 
 	unsigned size;
 
diff --git a/src/gallium/drivers/freedreno/freedreno_query_hw.c b/src/gallium/drivers/freedreno/freedreno_query_hw.c
index 8d44fd4..961e9c6 100644
--- a/src/gallium/drivers/freedreno/freedreno_query_hw.c
+++ b/src/gallium/drivers/freedreno/freedreno_query_hw.c
@@ -379,6 +379,15 @@
 void
 fd_hw_query_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
 {
+	/* special case: internal blits (like mipmap level generation)
+	 * go through normal draw path (via util_blitter_blit()).. but
+	 * we need to ignore the FD_STAGE_DRAW which will be set, so we
+	 * don't enable queries which should be paused during internal
+	 * blits:
+	 */
+	if (batch->stage == FD_STAGE_BLIT && stage != FD_STAGE_NULL)
+		stage = FD_STAGE_BLIT;
+
 	if (stage != batch->stage) {
 		struct fd_hw_query *hq;
 		LIST_FOR_EACH_ENTRY(hq, &batch->ctx->hw_active_queries, list) {