1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2016 Etnaviv Project
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the
12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
13b8e80941Smrg * of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Rob Clark <robclark@freedesktop.org>
25b8e80941Smrg *    Christian Gmeiner <christian.gmeiner@gmail.com>
26b8e80941Smrg */
27b8e80941Smrg
28b8e80941Smrg#include "pipe/p_screen.h"
29b8e80941Smrg#include "util/u_inlines.h"
30b8e80941Smrg
31b8e80941Smrg#include "etnaviv_context.h"
32b8e80941Smrg#include "etnaviv_query.h"
33b8e80941Smrg#include "etnaviv_query_hw.h"
34b8e80941Smrg#include "etnaviv_query_sw.h"
35b8e80941Smrg#include "etnaviv_query_pm.h"
36b8e80941Smrg
37b8e80941Smrgstatic struct pipe_query *
38b8e80941Smrgetna_create_query(struct pipe_context *pctx, unsigned query_type,
39b8e80941Smrg                  unsigned index)
40b8e80941Smrg{
41b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
42b8e80941Smrg   struct etna_query *q;
43b8e80941Smrg
44b8e80941Smrg   q = etna_sw_create_query(ctx, query_type);
45b8e80941Smrg   if (!q)
46b8e80941Smrg      q = etna_hw_create_query(ctx, query_type);
47b8e80941Smrg   if (!q)
48b8e80941Smrg      q = etna_pm_create_query(ctx, query_type);
49b8e80941Smrg
50b8e80941Smrg   return (struct pipe_query *)q;
51b8e80941Smrg}
52b8e80941Smrg
53b8e80941Smrgstatic void
54b8e80941Smrgetna_destroy_query(struct pipe_context *pctx, struct pipe_query *pq)
55b8e80941Smrg{
56b8e80941Smrg   struct etna_query *q = etna_query(pq);
57b8e80941Smrg
58b8e80941Smrg   q->funcs->destroy_query(etna_context(pctx), q);
59b8e80941Smrg}
60b8e80941Smrg
61b8e80941Smrgstatic boolean
62b8e80941Smrgetna_begin_query(struct pipe_context *pctx, struct pipe_query *pq)
63b8e80941Smrg{
64b8e80941Smrg   struct etna_query *q = etna_query(pq);
65b8e80941Smrg   boolean ret;
66b8e80941Smrg
67b8e80941Smrg   if (q->active)
68b8e80941Smrg      return false;
69b8e80941Smrg
70b8e80941Smrg   ret = q->funcs->begin_query(etna_context(pctx), q);
71b8e80941Smrg   q->active = ret;
72b8e80941Smrg
73b8e80941Smrg   return ret;
74b8e80941Smrg}
75b8e80941Smrg
76b8e80941Smrgstatic bool
77b8e80941Smrgetna_end_query(struct pipe_context *pctx, struct pipe_query *pq)
78b8e80941Smrg{
79b8e80941Smrg   struct etna_query *q = etna_query(pq);
80b8e80941Smrg
81b8e80941Smrg   if (!q->active)
82b8e80941Smrg      return false;
83b8e80941Smrg
84b8e80941Smrg   q->funcs->end_query(etna_context(pctx), q);
85b8e80941Smrg   q->active = false;
86b8e80941Smrg
87b8e80941Smrg   return true;
88b8e80941Smrg}
89b8e80941Smrg
90b8e80941Smrgstatic boolean
91b8e80941Smrgetna_get_query_result(struct pipe_context *pctx, struct pipe_query *pq,
92b8e80941Smrg                      boolean wait, union pipe_query_result *result)
93b8e80941Smrg{
94b8e80941Smrg   struct etna_query *q = etna_query(pq);
95b8e80941Smrg
96b8e80941Smrg   if (q->active)
97b8e80941Smrg      return false;
98b8e80941Smrg
99b8e80941Smrg   util_query_clear_result(result, q->type);
100b8e80941Smrg
101b8e80941Smrg   return q->funcs->get_query_result(etna_context(pctx), q, wait, result);
102b8e80941Smrg}
103b8e80941Smrg
104b8e80941Smrgstatic int
105b8e80941Smrgetna_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,
106b8e80941Smrg                           struct pipe_driver_query_info *info)
107b8e80941Smrg{
108b8e80941Smrg   int nr_sw_queries = etna_sw_get_driver_query_info(pscreen, 0, NULL);
109b8e80941Smrg   int nr_pm_queries = etna_pm_get_driver_query_info(pscreen, 0, NULL);
110b8e80941Smrg
111b8e80941Smrg   if (!info)
112b8e80941Smrg      return nr_sw_queries + nr_pm_queries;
113b8e80941Smrg
114b8e80941Smrg   if (index < nr_sw_queries)
115b8e80941Smrg      return etna_sw_get_driver_query_info(pscreen, index, info);
116b8e80941Smrg
117b8e80941Smrg   return etna_pm_get_driver_query_info(pscreen, index - nr_sw_queries, info);
118b8e80941Smrg}
119b8e80941Smrg
120b8e80941Smrgstatic int
121b8e80941Smrgetna_get_driver_query_group_info(struct pipe_screen *pscreen, unsigned index,
122b8e80941Smrg                                 struct pipe_driver_query_group_info *info)
123b8e80941Smrg{
124b8e80941Smrg   int nr_sw_groups = etna_sw_get_driver_query_group_info(pscreen, 0, NULL);
125b8e80941Smrg   int nr_pm_groups = etna_pm_get_driver_query_group_info(pscreen, 0, NULL);
126b8e80941Smrg
127b8e80941Smrg   if (!info)
128b8e80941Smrg      return nr_sw_groups + nr_pm_groups;
129b8e80941Smrg
130b8e80941Smrg   if (index < nr_sw_groups)
131b8e80941Smrg      return etna_sw_get_driver_query_group_info(pscreen, index, info);
132b8e80941Smrg
133b8e80941Smrg   return etna_pm_get_driver_query_group_info(pscreen, index, info);
134b8e80941Smrg}
135b8e80941Smrg
136b8e80941Smrgstatic void
137b8e80941Smrgetna_set_active_query_state(struct pipe_context *pipe, boolean enable)
138b8e80941Smrg{
139b8e80941Smrg}
140b8e80941Smrg
141b8e80941Smrgvoid
142b8e80941Smrgetna_query_screen_init(struct pipe_screen *pscreen)
143b8e80941Smrg{
144b8e80941Smrg   pscreen->get_driver_query_info = etna_get_driver_query_info;
145b8e80941Smrg   pscreen->get_driver_query_group_info = etna_get_driver_query_group_info;
146b8e80941Smrg}
147b8e80941Smrg
148b8e80941Smrgvoid
149b8e80941Smrgetna_query_context_init(struct pipe_context *pctx)
150b8e80941Smrg{
151b8e80941Smrg   pctx->create_query = etna_create_query;
152b8e80941Smrg   pctx->destroy_query = etna_destroy_query;
153b8e80941Smrg   pctx->begin_query = etna_begin_query;
154b8e80941Smrg   pctx->end_query = etna_end_query;
155b8e80941Smrg   pctx->get_query_result = etna_get_query_result;
156b8e80941Smrg   pctx->set_active_query_state = etna_set_active_query_state;
157b8e80941Smrg}
158