Merge branch 'master' into gallium-0.2
[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_winsys.h"
33 #include "u_timed_winsys.h"
34 #include "util/u_memory.h"
35 #include "util/u_time.h"
36
37
38 struct timed_winsys {
39 struct pipe_winsys base;
40 struct pipe_winsys *backend;
41 uint64_t last_dump;
42 struct {
43 const char *name_key;
44 double total;
45 unsigned calls;
46 } funcs[13];
47 };
48
49
50 static struct timed_winsys *timed_winsys( struct pipe_winsys *winsys )
51 {
52 return (struct timed_winsys *)winsys;
53 }
54
55
56 static uint64_t time_start( void )
57 {
58 return util_time_micros();
59 }
60
61
62 static void time_display( struct pipe_winsys *winsys )
63 {
64 struct timed_winsys *tws = timed_winsys(winsys);
65 unsigned i;
66 double overall = 0;
67
68 for (i = 0; i < Elements(tws->funcs); i++) {
69 if (tws->funcs[i].name_key) {
70 debug_printf("*** %-25s %5.3fms (%d calls, avg %.3fms)\n",
71 tws->funcs[i].name_key,
72 tws->funcs[i].total,
73 tws->funcs[i].calls,
74 tws->funcs[i].total / tws->funcs[i].calls);
75 overall += tws->funcs[i].total;
76 tws->funcs[i].calls = 0;
77 tws->funcs[i].total = 0;
78 }
79 }
80
81 debug_printf("*** %-25s %5.3fms\n",
82 "OVERALL WINSYS",
83 overall);
84 }
85
86 static void time_finish( struct pipe_winsys *winsys,
87 long long startval,
88 unsigned idx,
89 const char *name )
90 {
91 struct timed_winsys *tws = timed_winsys(winsys);
92 uint64_t endval = util_time_micros();
93 double elapsed = (endval - startval)/1000.0;
94
95 if (endval - startval > 1000LL)
96 debug_printf("*** %s %.3f\n", name, elapsed );
97
98 assert( tws->funcs[idx].name_key == name ||
99 tws->funcs[idx].name_key == NULL);
100
101 tws->funcs[idx].name_key = name;
102 tws->funcs[idx].total += elapsed;
103 tws->funcs[idx].calls++;
104
105 if (endval - tws->last_dump > 10LL * 1000LL * 1000LL) {
106 time_display( winsys );
107 tws->last_dump = endval;
108 }
109 }
110
111
112 /* Pipe has no concept of pools, but the psb driver passes a flag that
113 * can be mapped onto pools in the backend.
114 */
115 static struct pipe_buffer *
116 timed_buffer_create(struct pipe_winsys *winsys,
117 unsigned alignment,
118 unsigned usage,
119 unsigned size )
120 {
121 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
122 uint64_t start = time_start();
123
124 struct pipe_buffer *buf = backend->buffer_create( backend, alignment, usage, size );
125
126 time_finish(winsys, start, 0, __FUNCTION__);
127
128 return buf;
129 }
130
131
132
133
134 static struct pipe_buffer *
135 timed_user_buffer_create(struct pipe_winsys *winsys,
136 void *data,
137 unsigned bytes)
138 {
139 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
140 uint64_t start = time_start();
141
142 struct pipe_buffer *buf = backend->user_buffer_create( backend, data, bytes );
143
144 time_finish(winsys, start, 1, __FUNCTION__);
145
146 return buf;
147 }
148
149
150 static void *
151 timed_buffer_map(struct pipe_winsys *winsys,
152 struct pipe_buffer *buf,
153 unsigned flags)
154 {
155 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
156 uint64_t start = time_start();
157
158 void *map = backend->buffer_map( backend, buf, flags );
159
160 time_finish(winsys, start, 2, __FUNCTION__);
161
162 return map;
163 }
164
165
166 static void
167 timed_buffer_unmap(struct pipe_winsys *winsys,
168 struct pipe_buffer *buf)
169 {
170 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
171 uint64_t start = time_start();
172
173 backend->buffer_unmap( backend, buf );
174
175 time_finish(winsys, start, 3, __FUNCTION__);
176 }
177
178
179 static void
180 timed_buffer_destroy(struct pipe_winsys *winsys,
181 struct pipe_buffer *buf)
182 {
183 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
184 uint64_t start = time_start();
185
186 backend->buffer_destroy( backend, buf );
187
188 time_finish(winsys, start, 4, __FUNCTION__);
189 }
190
191
192 static void
193 timed_flush_frontbuffer( struct pipe_winsys *winsys,
194 struct pipe_surface *surf,
195 void *context_private)
196 {
197 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
198 uint64_t start = time_start();
199
200 backend->flush_frontbuffer( backend, surf, context_private );
201
202 time_finish(winsys, start, 5, __FUNCTION__);
203 }
204
205
206
207
208 static struct pipe_surface *
209 timed_surface_alloc(struct pipe_winsys *winsys)
210 {
211 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
212 uint64_t start = time_start();
213
214 struct pipe_surface *surf = backend->surface_alloc( backend );
215
216 time_finish(winsys, start, 6, __FUNCTION__);
217
218 return surf;
219 }
220
221
222
223 static int
224 timed_surface_alloc_storage(struct pipe_winsys *winsys,
225 struct pipe_surface *surf,
226 unsigned width, unsigned height,
227 enum pipe_format format,
228 unsigned flags,
229 unsigned tex_usage)
230 {
231 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
232 uint64_t start = time_start();
233
234 int ret = backend->surface_alloc_storage( backend, surf, width, height,
235 format, flags, tex_usage );
236
237 time_finish(winsys, start, 7, __FUNCTION__);
238
239 return ret;
240 }
241
242
243 static void
244 timed_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
245 {
246 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
247 uint64_t start = time_start();
248
249 backend->surface_release( backend, s );
250
251 time_finish(winsys, start, 8, __FUNCTION__);
252 }
253
254
255
256 static const char *
257 timed_get_name( struct pipe_winsys *winsys )
258 {
259 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
260 uint64_t start = time_start();
261
262 const char *ret = backend->get_name( backend );
263
264 time_finish(winsys, start, 9, __FUNCTION__);
265
266 return ret;
267 }
268
269 static void
270 timed_fence_reference(struct pipe_winsys *winsys,
271 struct pipe_fence_handle **ptr,
272 struct pipe_fence_handle *fence)
273 {
274 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
275 uint64_t start = time_start();
276
277 backend->fence_reference( backend, ptr, fence );
278
279 time_finish(winsys, start, 10, __FUNCTION__);
280 }
281
282
283 static int
284 timed_fence_signalled( struct pipe_winsys *winsys,
285 struct pipe_fence_handle *fence,
286 unsigned flag )
287 {
288 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
289 uint64_t start = time_start();
290
291 int ret = backend->fence_signalled( backend, fence, flag );
292
293 time_finish(winsys, start, 11, __FUNCTION__);
294
295 return ret;
296 }
297
298 static int
299 timed_fence_finish( struct pipe_winsys *winsys,
300 struct pipe_fence_handle *fence,
301 unsigned flag )
302 {
303 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
304 uint64_t start = time_start();
305
306 int ret = backend->fence_finish( backend, fence, flag );
307
308 time_finish(winsys, start, 12, __FUNCTION__);
309
310 return ret;
311 }
312
313 static void
314 timed_winsys_destroy( struct pipe_winsys *winsys )
315 {
316 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
317 backend->destroy( backend );
318 FREE(winsys);
319 }
320
321
322
323 struct pipe_winsys *u_timed_winsys_create( struct pipe_winsys *backend )
324 {
325 struct timed_winsys *ws = CALLOC_STRUCT(timed_winsys);
326
327 ws->base.user_buffer_create = timed_user_buffer_create;
328 ws->base.buffer_map = timed_buffer_map;
329 ws->base.buffer_unmap = timed_buffer_unmap;
330 ws->base.buffer_destroy = timed_buffer_destroy;
331 ws->base.buffer_create = timed_buffer_create;
332 ws->base.flush_frontbuffer = timed_flush_frontbuffer;
333 ws->base.get_name = timed_get_name;
334 ws->base.surface_alloc = timed_surface_alloc;
335 ws->base.surface_alloc_storage = timed_surface_alloc_storage;
336 ws->base.surface_release = timed_surface_release;
337 ws->base.fence_reference = timed_fence_reference;
338 ws->base.fence_signalled = timed_fence_signalled;
339 ws->base.fence_finish = timed_fence_finish;
340 ws->base.destroy = timed_winsys_destroy;
341
342 ws->backend = backend;
343
344 return &ws->base;
345 }
346