4ddb925b3479119bea2c7e665ce173b09d06d726
[mesa.git] / src / gallium / drivers / virgl / virgl_query.c
1 /*
2 * Copyright 2014, 2015 Red Hat.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "util/u_memory.h"
25 #include "util/u_inlines.h"
26 #include "virgl_context.h"
27 #include "virgl_encode.h"
28 #include "virgl_protocol.h"
29 #include "virgl_resource.h"
30
31 struct virgl_query {
32 uint32_t handle;
33 struct virgl_resource *buf;
34
35 unsigned index;
36 unsigned type;
37 unsigned result_size;
38 unsigned result_gotten_sent;
39 };
40
41 static inline struct virgl_query *virgl_query(struct pipe_query *q)
42 {
43 return (struct virgl_query *)q;
44 }
45
46 static void virgl_render_condition(struct pipe_context *ctx,
47 struct pipe_query *q,
48 boolean condition,
49 uint mode)
50 {
51 struct virgl_context *vctx = virgl_context(ctx);
52 struct virgl_query *query = virgl_query(q);
53 uint32_t handle = 0;
54 if (q)
55 handle = query->handle;
56 virgl_encoder_render_condition(vctx, handle, condition, mode);
57 }
58
59 static struct pipe_query *virgl_create_query(struct pipe_context *ctx,
60 unsigned query_type, unsigned index)
61 {
62 struct virgl_context *vctx = virgl_context(ctx);
63 struct virgl_query *query;
64 uint32_t handle;
65
66 query = CALLOC_STRUCT(virgl_query);
67 if (!query)
68 return NULL;
69
70 query->buf = (struct virgl_resource *)pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM,
71 PIPE_USAGE_STAGING, sizeof(struct virgl_host_query_state));
72 if (!query->buf) {
73 FREE(query);
74 return NULL;
75 }
76
77 handle = virgl_object_assign_handle();
78 query->type = query_type;
79 query->index = index;
80 query->handle = handle;
81 query->buf->clean = FALSE;
82 virgl_encoder_create_query(vctx, handle, query_type, index, query->buf, 0);
83
84 return (struct pipe_query *)query;
85 }
86
87 static void virgl_destroy_query(struct pipe_context *ctx,
88 struct pipe_query *q)
89 {
90 struct virgl_context *vctx = virgl_context(ctx);
91 struct virgl_query *query = virgl_query(q);
92
93 virgl_encode_delete_object(vctx, query->handle, VIRGL_OBJECT_QUERY);
94
95 pipe_resource_reference((struct pipe_resource **)&query->buf, NULL);
96 FREE(query);
97 }
98
99 static boolean virgl_begin_query(struct pipe_context *ctx,
100 struct pipe_query *q)
101 {
102 struct virgl_context *vctx = virgl_context(ctx);
103 struct virgl_query *query = virgl_query(q);
104
105 query->buf->clean = FALSE;
106 virgl_encoder_begin_query(vctx, query->handle);
107 return true;
108 }
109
110 static bool virgl_end_query(struct pipe_context *ctx,
111 struct pipe_query *q)
112 {
113 struct virgl_context *vctx = virgl_context(ctx);
114 struct virgl_query *query = virgl_query(q);
115 struct pipe_box box;
116
117 uint32_t qs = VIRGL_QUERY_STATE_WAIT_HOST;
118 u_box_1d(0, 4, &box);
119 virgl_transfer_inline_write(ctx, &query->buf->u.b, 0, PIPE_TRANSFER_WRITE,
120 &box, &qs, 0, 0);
121
122
123 virgl_encoder_end_query(vctx, query->handle);
124 return true;
125 }
126
127 static boolean virgl_get_query_result(struct pipe_context *ctx,
128 struct pipe_query *q,
129 boolean wait,
130 union pipe_query_result *result)
131 {
132 struct virgl_context *vctx = virgl_context(ctx);
133 struct virgl_query *query = virgl_query(q);
134 struct pipe_transfer *transfer;
135 struct virgl_host_query_state *host_state;
136
137 /* ask host for query result */
138 if (!query->result_gotten_sent) {
139 query->result_gotten_sent = 1;
140 virgl_encoder_get_query_result(vctx, query->handle, 0);
141 ctx->flush(ctx, NULL, 0);
142 }
143
144 /* do we have to flush? */
145 /* now we can do the transfer to get the result back? */
146 remap:
147 host_state = pipe_buffer_map(ctx, &query->buf->u.b,
148 PIPE_TRANSFER_READ, &transfer);
149
150 if (host_state->query_state != VIRGL_QUERY_STATE_DONE) {
151 pipe_buffer_unmap(ctx, transfer);
152 if (wait)
153 goto remap;
154 else
155 return FALSE;
156 }
157
158 if (query->type == PIPE_QUERY_TIMESTAMP || query->type == PIPE_QUERY_TIME_ELAPSED)
159 result->u64 = host_state->result;
160 else
161 result->u64 = (uint32_t)host_state->result;
162
163 pipe_buffer_unmap(ctx, transfer);
164 query->result_gotten_sent = 0;
165 return TRUE;
166 }
167
168 static void
169 virgl_set_active_query_state(struct pipe_context *pipe, boolean enable)
170 {
171 }
172
173 void virgl_init_query_functions(struct virgl_context *vctx)
174 {
175 vctx->base.render_condition = virgl_render_condition;
176 vctx->base.create_query = virgl_create_query;
177 vctx->base.destroy_query = virgl_destroy_query;
178 vctx->base.begin_query = virgl_begin_query;
179 vctx->base.end_query = virgl_end_query;
180 vctx->base.get_query_result = virgl_get_query_result;
181 vctx->base.set_active_query_state = virgl_set_active_query_state;
182 }