Merge remote branch 'origin/master' into lp-binning
[mesa.git] / src / gallium / auxiliary / util / u_timed_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28 /*
29 * Authors: Keith Whitwell <keithw-at-tungstengraphics-dot-com>
30 */
31
32 #include "pipe/p_state.h"
33 #include "pipe/internal/p_winsys_screen.h"
34 #include "u_timed_winsys.h"
35 #include "util/u_memory.h"
36 #include "util/u_time.h"
37
38
39 struct timed_winsys {
40 struct pipe_winsys base;
41 struct pipe_winsys *backend;
42 uint64_t last_dump;
43 struct {
44 const char *name_key;
45 double total;
46 unsigned calls;
47 } funcs[13];
48 };
49
50
51 static struct timed_winsys *timed_winsys( struct pipe_winsys *winsys )
52 {
53 return (struct timed_winsys *)winsys;
54 }
55
56
57 static uint64_t time_start( void )
58 {
59 return util_time_micros();
60 }
61
62
63 static void time_display( struct pipe_winsys *winsys )
64 {
65 struct timed_winsys *tws = timed_winsys(winsys);
66 unsigned i;
67 double overall = 0;
68
69 for (i = 0; i < Elements(tws->funcs); i++) {
70 if (tws->funcs[i].name_key) {
71 debug_printf("*** %-25s %5.3fms (%d calls, avg %.3fms)\n",
72 tws->funcs[i].name_key,
73 tws->funcs[i].total,
74 tws->funcs[i].calls,
75 tws->funcs[i].total / tws->funcs[i].calls);
76 overall += tws->funcs[i].total;
77 tws->funcs[i].calls = 0;
78 tws->funcs[i].total = 0;
79 }
80 }
81
82 debug_printf("*** %-25s %5.3fms\n",
83 "OVERALL WINSYS",
84 overall);
85 }
86
87 static void time_finish( struct pipe_winsys *winsys,
88 long long startval,
89 unsigned idx,
90 const char *name )
91 {
92 struct timed_winsys *tws = timed_winsys(winsys);
93 uint64_t endval = util_time_micros();
94 double elapsed = (endval - startval)/1000.0;
95
96 if (endval - startval > 1000LL)
97 debug_printf("*** %s %.3f\n", name, elapsed );
98
99 assert( tws->funcs[idx].name_key == name ||
100 tws->funcs[idx].name_key == NULL);
101
102 tws->funcs[idx].name_key = name;
103 tws->funcs[idx].total += elapsed;
104 tws->funcs[idx].calls++;
105
106 if (endval - tws->last_dump > 10LL * 1000LL * 1000LL) {
107 time_display( winsys );
108 tws->last_dump = endval;
109 }
110 }
111
112
113 /* Pipe has no concept of pools, but the psb driver passes a flag that
114 * can be mapped onto pools in the backend.
115 */
116 static struct pipe_buffer *
117 timed_buffer_create(struct pipe_winsys *winsys,
118 unsigned alignment,
119 unsigned usage,
120 unsigned size )
121 {
122 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
123 uint64_t start = time_start();
124
125 struct pipe_buffer *buf =
126 backend->buffer_create( backend, alignment, usage, size );
127
128 time_finish(winsys, start, 0, __FUNCTION__);
129
130 return buf;
131 }
132
133
134
135
136 static struct pipe_buffer *
137 timed_user_buffer_create(struct pipe_winsys *winsys,
138 void *data,
139 unsigned bytes)
140 {
141 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
142 uint64_t start = time_start();
143
144 struct pipe_buffer *buf = backend->user_buffer_create( backend, data, bytes );
145
146 time_finish(winsys, start, 1, __FUNCTION__);
147
148 return buf;
149 }
150
151
152 static void *
153 timed_buffer_map(struct pipe_winsys *winsys,
154 struct pipe_buffer *buf,
155 unsigned flags)
156 {
157 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
158 uint64_t start = time_start();
159
160 void *map = backend->buffer_map( backend, buf, flags );
161
162 time_finish(winsys, start, 2, __FUNCTION__);
163
164 return map;
165 }
166
167
168 static void
169 timed_buffer_unmap(struct pipe_winsys *winsys,
170 struct pipe_buffer *buf)
171 {
172 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
173 uint64_t start = time_start();
174
175 backend->buffer_unmap( backend, buf );
176
177 time_finish(winsys, start, 3, __FUNCTION__);
178 }
179
180
181 static void
182 timed_buffer_destroy(struct pipe_buffer *buf)
183 {
184 struct pipe_winsys *winsys = buf->screen->winsys;
185 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
186 uint64_t start = time_start();
187
188 backend->buffer_destroy( buf );
189
190 time_finish(winsys, start, 4, __FUNCTION__);
191 }
192
193
194 static void
195 timed_flush_frontbuffer( struct pipe_winsys *winsys,
196 struct pipe_surface *surf,
197 void *context_private)
198 {
199 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
200 uint64_t start = time_start();
201
202 backend->flush_frontbuffer( backend, surf, context_private );
203
204 time_finish(winsys, start, 5, __FUNCTION__);
205 }
206
207
208
209
210 static struct pipe_buffer *
211 timed_surface_buffer_create(struct pipe_winsys *winsys,
212 unsigned width, unsigned height,
213 enum pipe_format format,
214 unsigned usage,
215 unsigned tex_usage,
216 unsigned *stride)
217 {
218 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
219 uint64_t start = time_start();
220
221 struct pipe_buffer *ret = backend->surface_buffer_create( backend, width, height,
222 format, usage, tex_usage, stride );
223
224 time_finish(winsys, start, 7, __FUNCTION__);
225
226 return ret;
227 }
228
229
230 static const char *
231 timed_get_name( struct pipe_winsys *winsys )
232 {
233 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
234 uint64_t start = time_start();
235
236 const char *ret = backend->get_name( backend );
237
238 time_finish(winsys, start, 9, __FUNCTION__);
239
240 return ret;
241 }
242
243 static void
244 timed_fence_reference(struct pipe_winsys *winsys,
245 struct pipe_fence_handle **ptr,
246 struct pipe_fence_handle *fence)
247 {
248 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
249 uint64_t start = time_start();
250
251 backend->fence_reference( backend, ptr, fence );
252
253 time_finish(winsys, start, 10, __FUNCTION__);
254 }
255
256
257 static int
258 timed_fence_signalled( struct pipe_winsys *winsys,
259 struct pipe_fence_handle *fence,
260 unsigned flag )
261 {
262 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
263 uint64_t start = time_start();
264
265 int ret = backend->fence_signalled( backend, fence, flag );
266
267 time_finish(winsys, start, 11, __FUNCTION__);
268
269 return ret;
270 }
271
272 static int
273 timed_fence_finish( struct pipe_winsys *winsys,
274 struct pipe_fence_handle *fence,
275 unsigned flag )
276 {
277 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
278 uint64_t start = time_start();
279
280 int ret = backend->fence_finish( backend, fence, flag );
281
282 time_finish(winsys, start, 12, __FUNCTION__);
283
284 return ret;
285 }
286
287 static void
288 timed_winsys_destroy( struct pipe_winsys *winsys )
289 {
290 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
291 backend->destroy( backend );
292 FREE(winsys);
293 }
294
295
296
297 struct pipe_winsys *u_timed_winsys_create( struct pipe_winsys *backend )
298 {
299 struct timed_winsys *ws = CALLOC_STRUCT(timed_winsys);
300
301 ws->base.user_buffer_create = timed_user_buffer_create;
302 ws->base.buffer_map = timed_buffer_map;
303 ws->base.buffer_unmap = timed_buffer_unmap;
304 ws->base.buffer_destroy = timed_buffer_destroy;
305 ws->base.buffer_create = timed_buffer_create;
306 ws->base.surface_buffer_create = timed_surface_buffer_create;
307 ws->base.flush_frontbuffer = timed_flush_frontbuffer;
308 ws->base.get_name = timed_get_name;
309 ws->base.fence_reference = timed_fence_reference;
310 ws->base.fence_signalled = timed_fence_signalled;
311 ws->base.fence_finish = timed_fence_finish;
312 ws->base.destroy = timed_winsys_destroy;
313
314 ws->backend = backend;
315
316 return &ws->base;
317 }
318