Merge branch 'gallium-0.2' into gallium-winsys-private
[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 =
125 backend->_buffer_create( backend, alignment, usage, size );
126
127 time_finish(winsys, start, 0, __FUNCTION__);
128
129 return buf;
130 }
131
132
133
134
135 static struct pipe_buffer *
136 timed_user_buffer_create(struct pipe_winsys *winsys,
137 void *data,
138 unsigned bytes)
139 {
140 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
141 uint64_t start = time_start();
142
143 struct pipe_buffer *buf = backend->_user_buffer_create( backend, data, bytes );
144
145 time_finish(winsys, start, 1, __FUNCTION__);
146
147 return buf;
148 }
149
150
151 static void *
152 timed_buffer_map(struct pipe_winsys *winsys,
153 struct pipe_buffer *buf,
154 unsigned flags)
155 {
156 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
157 uint64_t start = time_start();
158
159 void *map = backend->_buffer_map( backend, buf, flags );
160
161 time_finish(winsys, start, 2, __FUNCTION__);
162
163 return map;
164 }
165
166
167 static void
168 timed_buffer_unmap(struct pipe_winsys *winsys,
169 struct pipe_buffer *buf)
170 {
171 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
172 uint64_t start = time_start();
173
174 backend->_buffer_unmap( backend, buf );
175
176 time_finish(winsys, start, 3, __FUNCTION__);
177 }
178
179
180 static void
181 timed_buffer_destroy(struct pipe_winsys *winsys,
182 struct pipe_buffer *buf)
183 {
184 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
185 uint64_t start = time_start();
186
187 backend->_buffer_destroy( backend, buf );
188
189 time_finish(winsys, start, 4, __FUNCTION__);
190 }
191
192
193 static void
194 timed_flush_frontbuffer( struct pipe_winsys *winsys,
195 struct pipe_surface *surf,
196 void *context_private)
197 {
198 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
199 uint64_t start = time_start();
200
201 backend->flush_frontbuffer( backend, surf, context_private );
202
203 time_finish(winsys, start, 5, __FUNCTION__);
204 }
205
206
207
208
209 static struct pipe_buffer *
210 timed_surface_buffer_create(struct pipe_winsys *winsys,
211 unsigned width, unsigned height,
212 enum pipe_format format,
213 unsigned usage,
214 unsigned *stride)
215 {
216 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
217 uint64_t start = time_start();
218
219 struct pipe_buffer *ret = backend->_surface_buffer_create( backend, width, height,
220 format, usage, stride );
221
222 time_finish(winsys, start, 7, __FUNCTION__);
223
224 return ret;
225 }
226
227
228 static const char *
229 timed_get_name( struct pipe_winsys *winsys )
230 {
231 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
232 uint64_t start = time_start();
233
234 const char *ret = backend->get_name( backend );
235
236 time_finish(winsys, start, 9, __FUNCTION__);
237
238 return ret;
239 }
240
241 static void
242 timed_fence_reference(struct pipe_winsys *winsys,
243 struct pipe_fence_handle **ptr,
244 struct pipe_fence_handle *fence)
245 {
246 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
247 uint64_t start = time_start();
248
249 backend->fence_reference( backend, ptr, fence );
250
251 time_finish(winsys, start, 10, __FUNCTION__);
252 }
253
254
255 static int
256 timed_fence_signalled( struct pipe_winsys *winsys,
257 struct pipe_fence_handle *fence,
258 unsigned flag )
259 {
260 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
261 uint64_t start = time_start();
262
263 int ret = backend->fence_signalled( backend, fence, flag );
264
265 time_finish(winsys, start, 11, __FUNCTION__);
266
267 return ret;
268 }
269
270 static int
271 timed_fence_finish( struct pipe_winsys *winsys,
272 struct pipe_fence_handle *fence,
273 unsigned flag )
274 {
275 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
276 uint64_t start = time_start();
277
278 int ret = backend->fence_finish( backend, fence, flag );
279
280 time_finish(winsys, start, 12, __FUNCTION__);
281
282 return ret;
283 }
284
285 static void
286 timed_winsys_destroy( struct pipe_winsys *winsys )
287 {
288 struct pipe_winsys *backend = timed_winsys(winsys)->backend;
289 backend->destroy( backend );
290 FREE(winsys);
291 }
292
293
294
295 struct pipe_winsys *u_timed_winsys_create( struct pipe_winsys *backend )
296 {
297 struct timed_winsys *ws = CALLOC_STRUCT(timed_winsys);
298
299 ws->base._user_buffer_create = timed_user_buffer_create;
300 ws->base._buffer_map = timed_buffer_map;
301 ws->base._buffer_unmap = timed_buffer_unmap;
302 ws->base._buffer_destroy = timed_buffer_destroy;
303 ws->base._buffer_create = timed_buffer_create;
304 ws->base._surface_buffer_create = timed_surface_buffer_create;
305 ws->base.flush_frontbuffer = timed_flush_frontbuffer;
306 ws->base.get_name = timed_get_name;
307 ws->base.fence_reference = timed_fence_reference;
308 ws->base.fence_signalled = timed_fence_signalled;
309 ws->base.fence_finish = timed_fence_finish;
310 ws->base.destroy = timed_winsys_destroy;
311
312 ws->backend = backend;
313
314 return &ws->base;
315 }
316