gallium: added a4r4g4b4_put_tile_rgba()
[mesa.git] / src / gallium / auxiliary / util / p_debug_prof.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Poor-man profiling.
31 *
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
33 *
34 * @sa http://blogs.msdn.com/joshpoley/archive/2008/03/12/poor-man-s-profiler.aspx
35 * @sa http://www.johnpanzer.com/aci_cuj/index.html
36 */
37
38 #include "pipe/p_config.h"
39
40 #if defined(PROFILE) && defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
41
42 #include <windows.h>
43 #include <winddi.h>
44
45 #include "pipe/p_debug.h"
46 #include "util/u_string.h"
47
48
49 #define PROFILE_FILE_SIZE 4*1024*1024
50 #define FILE_NAME_SIZE 256
51
52 static WCHAR wFileName[FILE_NAME_SIZE];
53 static ULONG_PTR iFile = 0;
54 static void *pMap = NULL;
55 static void *pMapEnd = NULL;
56
57
58 /**
59 * Called at the start of every method or function.
60 *
61 * @sa http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx
62 */
63 void __declspec(naked) __cdecl
64 _penter(void) {
65 _asm {
66 push ebx
67 mov ebx, [pMap]
68 test ebx, ebx
69 jz done
70 cmp ebx, [pMapEnd]
71 je done
72 push eax
73 push edx
74 mov eax, [esp+12]
75 and eax, 0xfffffffe
76 mov [ebx], eax
77 add ebx, 4
78 rdtsc
79 mov [ebx], eax
80 add ebx, 4
81 mov [pMap], ebx
82 pop edx
83 pop eax
84 done:
85 pop ebx
86 ret
87 }
88 }
89
90
91 /**
92 * Called at the end of Calls the end of every method or function.
93 *
94 * @sa http://msdn.microsoft.com/en-us/library/xc11y76y.aspx
95 */
96 void __declspec(naked) __cdecl
97 _pexit(void) {
98 _asm {
99 push ebx
100 mov ebx, [pMap]
101 test ebx, ebx
102 jz done
103 cmp ebx, [pMapEnd]
104 je done
105 push eax
106 push edx
107 mov eax, [esp+12]
108 or eax, 0x00000001
109 mov [ebx], eax
110 add ebx, 4
111 rdtsc
112 mov [ebx], eax
113 add ebx, 4
114 mov [pMap], ebx
115 pop edx
116 pop eax
117 done:
118 pop ebx
119 ret
120 }
121 }
122
123
124 void __declspec(naked)
125 __debug_profile_reference1(void) {
126 _asm {
127 call _penter
128 call _pexit
129 ret
130 }
131 }
132
133
134 void __declspec(naked)
135 __debug_profile_reference2(void) {
136 _asm {
137 call _penter
138 call __debug_profile_reference1
139 call _pexit
140 ret
141 }
142 }
143
144
145 void
146 debug_profile_start(void)
147 {
148 static unsigned no = 0;
149 char filename[FILE_NAME_SIZE];
150 unsigned i;
151
152 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u.prof", ++no);
153 for(i = 0; i < FILE_NAME_SIZE; ++i)
154 wFileName[i] = (WCHAR)filename[i];
155
156 pMap = EngMapFile(wFileName, PROFILE_FILE_SIZE, &iFile);
157 if(pMap) {
158 pMapEnd = (unsigned char*)pMap + PROFILE_FILE_SIZE;
159 /* reference functions for calibration purposes */
160 __debug_profile_reference2();
161 }
162 }
163
164 void
165 debug_profile_stop(void)
166 {
167 if(iFile) {
168 EngUnmapFile(iFile);
169 /* TODO: truncate file */
170 }
171 iFile = 0;
172 pMapEnd = pMap = NULL;
173 }
174
175 #endif /* PROFILE */