1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2015 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "pipebuffer/pb_bufmgr.h"
27bf215546Sopenharmony_ci#include "util/u_memory.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "vmw_screen.h"
30bf215546Sopenharmony_ci#include "vmw_buffer.h"
31bf215546Sopenharmony_ci#include "vmw_query.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct svga_winsys_gb_query *
36bf215546Sopenharmony_civmw_svga_winsys_query_create(struct svga_winsys_screen *sws,
37bf215546Sopenharmony_ci                             uint32 queryResultLen)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
40bf215546Sopenharmony_ci   struct pb_manager *provider = vws->pools.gmr;
41bf215546Sopenharmony_ci   struct pb_desc desc = {0};
42bf215546Sopenharmony_ci   struct pb_buffer *pb_buf;
43bf215546Sopenharmony_ci   struct svga_winsys_gb_query *query;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   query = CALLOC_STRUCT(svga_winsys_gb_query);
46bf215546Sopenharmony_ci   if (!query)
47bf215546Sopenharmony_ci      return NULL;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   /* Allocate memory to hold queries for this context */
50bf215546Sopenharmony_ci   desc.alignment = 4096;
51bf215546Sopenharmony_ci   pb_buf = provider->create_buffer(provider, queryResultLen, &desc);
52bf215546Sopenharmony_ci   query->buf = vmw_svga_winsys_buffer_wrap(pb_buf);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   if (!query->buf) {
55bf215546Sopenharmony_ci      debug_printf("Failed to allocate memory for queries\n");
56bf215546Sopenharmony_ci      FREE(query);
57bf215546Sopenharmony_ci      query = NULL;
58bf215546Sopenharmony_ci   }
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   return query;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_civoid
66bf215546Sopenharmony_civmw_svga_winsys_query_destroy(struct svga_winsys_screen *sws,
67bf215546Sopenharmony_ci                              struct svga_winsys_gb_query *query)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   vmw_svga_winsys_buffer_destroy(sws, query->buf);
70bf215546Sopenharmony_ci   FREE(query);
71bf215546Sopenharmony_ci}
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ciint
76bf215546Sopenharmony_civmw_svga_winsys_query_init(struct svga_winsys_screen *sws,
77bf215546Sopenharmony_ci                           struct svga_winsys_gb_query *query,
78bf215546Sopenharmony_ci                           unsigned offset,
79bf215546Sopenharmony_ci                           SVGA3dQueryState queryState)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   SVGA3dQueryState *state;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   state = (SVGA3dQueryState *) vmw_svga_winsys_buffer_map(sws,
84bf215546Sopenharmony_ci                                       query->buf,
85bf215546Sopenharmony_ci                                       PIPE_MAP_WRITE);
86bf215546Sopenharmony_ci   if (!state) {
87bf215546Sopenharmony_ci      debug_printf("Failed to map query result memory for initialization\n");
88bf215546Sopenharmony_ci      return -1;
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   /* Initialize the query state for the specified query slot */
92bf215546Sopenharmony_ci   state = (SVGA3dQueryState *)((char *)state + offset);
93bf215546Sopenharmony_ci   *state = queryState;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   vmw_svga_winsys_buffer_unmap(sws, query->buf);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   return 0;
98bf215546Sopenharmony_ci}
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_civoid
103bf215546Sopenharmony_civmw_svga_winsys_query_get_result(struct svga_winsys_screen *sws,
104bf215546Sopenharmony_ci                                 struct svga_winsys_gb_query *query,
105bf215546Sopenharmony_ci                                 unsigned offset,
106bf215546Sopenharmony_ci                                 SVGA3dQueryState *queryState,
107bf215546Sopenharmony_ci                                 void *result, uint32 resultLen)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   SVGA3dQueryState *state;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   state = (SVGA3dQueryState *) vmw_svga_winsys_buffer_map(sws,
112bf215546Sopenharmony_ci                                       query->buf,
113bf215546Sopenharmony_ci                                       PIPE_MAP_READ);
114bf215546Sopenharmony_ci   if (!state) {
115bf215546Sopenharmony_ci      debug_printf("Failed to lock query result memory\n");
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci      if (queryState)
118bf215546Sopenharmony_ci         *queryState = SVGA3D_QUERYSTATE_FAILED;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci      return;
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   state = (SVGA3dQueryState *)((char *)state + offset);
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   if (queryState)
126bf215546Sopenharmony_ci      *queryState = *state;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   if (result) {
129bf215546Sopenharmony_ci      memcpy(result, state + 1, resultLen);
130bf215546Sopenharmony_ci   }
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   vmw_svga_winsys_buffer_unmap(sws, query->buf);
133bf215546Sopenharmony_ci}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cienum pipe_error
137bf215546Sopenharmony_civmw_swc_query_bind(struct svga_winsys_context *swc,
138bf215546Sopenharmony_ci                   struct svga_winsys_gb_query *query,
139bf215546Sopenharmony_ci                   unsigned flags)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci   /* no-op on Linux */
142bf215546Sopenharmony_ci   return PIPE_OK;
143bf215546Sopenharmony_ci}
144bf215546Sopenharmony_ci
145