summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/util/u_timed_winsys.c
blob: f237e12d73502894c26a6c8b4fe8fa7ff9736ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**************************************************************************
 * 
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/
/*
 * Authors: Keith Whitwell <keithw-at-tungstengraphics-dot-com>
 */

#include "pipe/internal/p_winsys_screen.h"
#include "u_timed_winsys.h"
#include "util/u_memory.h"
#include "util/u_time.h"


struct timed_winsys {
   struct pipe_winsys base;
   struct pipe_winsys *backend;
   uint64_t last_dump;
   struct {
      const char *name_key;
      double total;
      unsigned calls;
   } funcs[13];
};


static struct timed_winsys *timed_winsys( struct pipe_winsys *winsys )
{
   return (struct timed_winsys *)winsys;
}


static uint64_t time_start( void )
{
   return util_time_micros();
}


static void time_display( struct pipe_winsys *winsys )
{
   struct timed_winsys *tws = timed_winsys(winsys);
   unsigned i;
   double overall = 0;

   for (i = 0; i < Elements(tws->funcs); i++) {
      if (tws->funcs[i].name_key) {
         debug_printf("*** %-25s %5.3fms (%d calls, avg %.3fms)\n", 
                      tws->funcs[i].name_key,
                      tws->funcs[i].total,
                      tws->funcs[i].calls,
                      tws->funcs[i].total / tws->funcs[i].calls);
         overall += tws->funcs[i].total;
         tws->funcs[i].calls = 0;
         tws->funcs[i].total = 0;
      }
   }

   debug_printf("*** %-25s %5.3fms\n", 
                "OVERALL WINSYS",
                overall);
}

static void time_finish( struct pipe_winsys *winsys,
                         long long startval, 
                         unsigned idx,
                         const char *name ) 
{
   struct timed_winsys *tws = timed_winsys(winsys);
   uint64_t endval = util_time_micros();
   double elapsed = (endval - startval)/1000.0;

   if (endval - startval > 1000LL) 
      debug_printf("*** %s %.3f\n", name, elapsed );

   assert( tws->funcs[idx].name_key == name ||
           tws->funcs[idx].name_key == NULL);

   tws->funcs[idx].name_key = name;
   tws->funcs[idx].total += elapsed;
   tws->funcs[idx].calls++;

   if (endval - tws->last_dump > 10LL * 1000LL * 1000LL) {
      time_display( winsys );
      tws->last_dump = endval;
   }
}


/* Pipe has no concept of pools, but the psb driver passes a flag that
 * can be mapped onto pools in the backend.
 */
static struct pipe_buffer *
timed_buffer_create(struct pipe_winsys *winsys, 
                    unsigned alignment, 
                    unsigned usage, 
                    unsigned size )
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   struct pipe_buffer *buf =
      backend->buffer_create( backend, alignment, usage, size );

   time_finish(winsys, start, 0, __FUNCTION__);
   
   return buf;
}




static struct pipe_buffer *
timed_user_buffer_create(struct pipe_winsys *winsys,
                             void *data, 
                             unsigned bytes) 
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   struct pipe_buffer *buf = backend->user_buffer_create( backend, data, bytes );

   time_finish(winsys, start, 1, __FUNCTION__);
   
   return buf;
}


static void *
timed_buffer_map(struct pipe_winsys *winsys,
                     struct pipe_buffer *buf,
                     unsigned flags)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   void *map = backend->buffer_map( backend, buf, flags );

   time_finish(winsys, start, 2, __FUNCTION__);
   
   return map;
}


static void
timed_buffer_unmap(struct pipe_winsys *winsys,
                       struct pipe_buffer *buf)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   backend->buffer_unmap( backend, buf );

   time_finish(winsys, start, 3, __FUNCTION__);
}


static void
timed_buffer_destroy(struct pipe_winsys *winsys,
                         struct pipe_buffer *buf)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   backend->buffer_destroy( backend, buf );

   time_finish(winsys, start, 4, __FUNCTION__);
}


static void
timed_flush_frontbuffer( struct pipe_winsys *winsys,
                         struct pipe_surface *surf,
                         void *context_private)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   backend->flush_frontbuffer( backend, surf, context_private );

   time_finish(winsys, start, 5, __FUNCTION__);
}




static struct pipe_buffer *
timed_surface_buffer_create(struct pipe_winsys *winsys,
                              unsigned width, unsigned height,
                              enum pipe_format format, 
                              unsigned usage,
                              unsigned *stride)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   struct pipe_buffer *ret = backend->surface_buffer_create( backend, width, height, 
                                                             format, usage, stride );

   time_finish(winsys, start, 7, __FUNCTION__);
   
   return ret;
}


static const char *
timed_get_name( struct pipe_winsys *winsys )
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   const char *ret = backend->get_name( backend );

   time_finish(winsys, start, 9, __FUNCTION__);
   
   return ret;
}

static void
timed_fence_reference(struct pipe_winsys *winsys,
                    struct pipe_fence_handle **ptr,
                    struct pipe_fence_handle *fence)
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   backend->fence_reference( backend, ptr, fence );

   time_finish(winsys, start, 10, __FUNCTION__);
}


static int
timed_fence_signalled( struct pipe_winsys *winsys,
                       struct pipe_fence_handle *fence,
                       unsigned flag )
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   int ret = backend->fence_signalled( backend, fence, flag );

   time_finish(winsys, start, 11, __FUNCTION__);
   
   return ret;
}

static int
timed_fence_finish( struct pipe_winsys *winsys,
                     struct pipe_fence_handle *fence,
                     unsigned flag )
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   uint64_t start = time_start();

   int ret = backend->fence_finish( backend, fence, flag );

   time_finish(winsys, start, 12, __FUNCTION__);
   
   return ret;
}

static void
timed_winsys_destroy( struct pipe_winsys *winsys )
{
   struct pipe_winsys *backend = timed_winsys(winsys)->backend;
   backend->destroy( backend );
   FREE(winsys);
}



struct pipe_winsys *u_timed_winsys_create( struct pipe_winsys *backend )
{
   struct timed_winsys *ws = CALLOC_STRUCT(timed_winsys);
   
   ws->base.user_buffer_create = timed_user_buffer_create;
   ws->base.buffer_map = timed_buffer_map;
   ws->base.buffer_unmap = timed_buffer_unmap;
   ws->base.buffer_destroy = timed_buffer_destroy;
   ws->base.buffer_create = timed_buffer_create;
   ws->base.surface_buffer_create = timed_surface_buffer_create;
   ws->base.flush_frontbuffer = timed_flush_frontbuffer;
   ws->base.get_name = timed_get_name;
   ws->base.fence_reference = timed_fence_reference;
   ws->base.fence_signalled = timed_fence_signalled;
   ws->base.fence_finish = timed_fence_finish;
   ws->base.destroy = timed_winsys_destroy;
   
   ws->backend = backend;

   return &ws->base;
}