blob: c45b2a1ce9106a6bc8cfe866703ff2ce2bd8fd18 [file] [log] [blame]
daniel@transgaming.com95a758f2012-07-12 15:17:06 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// debug.cpp: Debugging utilities.
8
9#include "common/debug.h"
10
11#include <stdio.h>
12#include <stdarg.h>
13#include <d3d9.h>
14#include <windows.h>
15
16namespace gl
17{
18
19typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR);
20
21static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg)
22{
23#if !defined(ANGLE_DISABLE_PERF)
24 if (perfActive())
25 {
26 char message[32768];
27 int len = vsprintf_s(message, format, vararg);
28 if (len < 0)
29 {
30 return;
31 }
32
33 // There are no ASCII variants of these D3DPERF functions.
34 wchar_t wideMessage[32768];
35 for (int i = 0; i < len; ++i)
36 {
37 wideMessage[i] = message[i];
38 }
39 wideMessage[len] = 0;
40
41 perfFunc(0, wideMessage);
42 }
43#endif
44
45#if !defined(ANGLE_DISABLE_TRACE)
46#if defined(NDEBUG)
47 if (traceFileDebugOnly)
48 {
49 return;
50 }
51#endif
52
53 FILE* file = fopen(TRACE_OUTPUT_FILE, "a");
54 if (file)
55 {
56 vfprintf(file, format, vararg);
57 fclose(file);
58 }
59#endif
60}
61
62void trace(bool traceFileDebugOnly, const char *format, ...)
63{
64 va_list vararg;
65 va_start(vararg, format);
66#if defined(ANGLE_DISABLE_PERF)
67 output(traceFileDebugOnly, NULL, format, vararg);
68#else
69 output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg);
70#endif
71 va_end(vararg);
72}
73
74bool perfActive()
75{
76#if defined(ANGLE_DISABLE_PERF)
77 return false;
78#else
79 static bool active = D3DPERF_GetStatus() != 0;
80 return active;
81#endif
82}
83
84ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
85{
86#if !defined(ANGLE_DISABLE_PERF)
daniel@transgaming.com5dc3b8b2012-11-28 19:43:49 +000087#if defined(ANGLE_DISABLE_TRACE)
88 if (!perfActive())
89 {
90 return;
91 }
92#endif
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000093 va_list vararg;
94 va_start(vararg, format);
95 output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg);
96 va_end(vararg);
97#endif
98}
99
100ScopedPerfEventHelper::~ScopedPerfEventHelper()
101{
102#if !defined(ANGLE_DISABLE_PERF)
103 if (perfActive())
104 {
105 D3DPERF_EndEvent();
106 }
107#endif
108}
109}