1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2016 Etnaviv Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci *    Christian Gmeiner <christian.gmeiner@gmail.com>
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/os_time.h"
29bf215546Sopenharmony_ci#include "pipe/p_state.h"
30bf215546Sopenharmony_ci#include "util/u_memory.h"
31bf215546Sopenharmony_ci#include "util/u_string.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "etnaviv_context.h"
34bf215546Sopenharmony_ci#include "etnaviv_query_sw.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic void
37bf215546Sopenharmony_cietna_sw_destroy_query(struct etna_context *ctx, struct etna_query *q)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   struct etna_sw_query *sq = etna_sw_query(q);
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   FREE(sq);
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic uint64_t
45bf215546Sopenharmony_ciread_counter(struct etna_context *ctx, unsigned type)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   switch (type) {
48bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_GENERATED:
49bf215546Sopenharmony_ci      return ctx->stats.prims_generated;
50bf215546Sopenharmony_ci   case ETNA_QUERY_DRAW_CALLS:
51bf215546Sopenharmony_ci      return ctx->stats.draw_calls;
52bf215546Sopenharmony_ci   case ETNA_QUERY_RS_OPERATIONS:
53bf215546Sopenharmony_ci      return ctx->stats.rs_operations;
54bf215546Sopenharmony_ci   }
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   return 0;
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistatic void
60bf215546Sopenharmony_cietna_sw_begin_query(struct etna_context *ctx, struct etna_query *q)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   struct etna_sw_query *sq = etna_sw_query(q);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   sq->begin_value = read_counter(ctx, q->type);
65bf215546Sopenharmony_ci}
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic void
68bf215546Sopenharmony_cietna_sw_end_query(struct etna_context *ctx, struct etna_query *q)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   struct etna_sw_query *sq = etna_sw_query(q);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   sq->end_value = read_counter(ctx, q->type);
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic bool
76bf215546Sopenharmony_cietna_sw_get_query_result(struct etna_context *ctx, struct etna_query *q,
77bf215546Sopenharmony_ci                         bool wait, union pipe_query_result *result)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   struct etna_sw_query *sq = etna_sw_query(q);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   result->u64 = sq->end_value - sq->begin_value;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   return true;
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistatic const struct etna_query_funcs sw_query_funcs = {
87bf215546Sopenharmony_ci   .destroy_query = etna_sw_destroy_query,
88bf215546Sopenharmony_ci   .begin_query = etna_sw_begin_query,
89bf215546Sopenharmony_ci   .end_query = etna_sw_end_query,
90bf215546Sopenharmony_ci   .get_query_result = etna_sw_get_query_result,
91bf215546Sopenharmony_ci};
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_cistruct etna_query *
94bf215546Sopenharmony_cietna_sw_create_query(struct etna_context *ctx, unsigned query_type)
95bf215546Sopenharmony_ci{
96bf215546Sopenharmony_ci   struct etna_sw_query *sq;
97bf215546Sopenharmony_ci   struct etna_query *q;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   switch (query_type) {
100bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_GENERATED:
101bf215546Sopenharmony_ci   case ETNA_QUERY_DRAW_CALLS:
102bf215546Sopenharmony_ci   case ETNA_QUERY_RS_OPERATIONS:
103bf215546Sopenharmony_ci      break;
104bf215546Sopenharmony_ci   default:
105bf215546Sopenharmony_ci      return NULL;
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   sq = CALLOC_STRUCT(etna_sw_query);
109bf215546Sopenharmony_ci   if (!sq)
110bf215546Sopenharmony_ci      return NULL;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   q = &sq->base;
113bf215546Sopenharmony_ci   q->funcs = &sw_query_funcs;
114bf215546Sopenharmony_ci   q->type = query_type;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   return q;
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_cistatic const struct pipe_driver_query_info list[] = {
120bf215546Sopenharmony_ci   {"prims-generated", PIPE_QUERY_PRIMITIVES_GENERATED, { 0 }},
121bf215546Sopenharmony_ci   {"draw-calls", ETNA_QUERY_DRAW_CALLS, { 0 }},
122bf215546Sopenharmony_ci   {"rs-operations", ETNA_QUERY_RS_OPERATIONS, { 0 }},
123bf215546Sopenharmony_ci};
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ciint
126bf215546Sopenharmony_cietna_sw_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,
127bf215546Sopenharmony_ci                              struct pipe_driver_query_info *info)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   if (!info)
130bf215546Sopenharmony_ci      return ARRAY_SIZE(list);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   if (index >= ARRAY_SIZE(list))
133bf215546Sopenharmony_ci      return 0;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   *info = list[index];
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   return 1;
138bf215546Sopenharmony_ci}
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ciint
141bf215546Sopenharmony_cietna_sw_get_driver_query_group_info(struct pipe_screen *pscreen,
142bf215546Sopenharmony_ci                                    unsigned index,
143bf215546Sopenharmony_ci                                    struct pipe_driver_query_group_info *info)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci   if (!info)
146bf215546Sopenharmony_ci      return 1;
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   if (index != 0)
149bf215546Sopenharmony_ci      return 0;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   info->name = "driver";
152bf215546Sopenharmony_ci   info->max_active_queries = ARRAY_SIZE(list);
153bf215546Sopenharmony_ci   info->num_queries = ARRAY_SIZE(list);
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   return 1;
156bf215546Sopenharmony_ci}
157