blob: 59399607d5604078c733e66646a4e1263a649933 [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>
shannon.woods@transgaming.coma9b96d02013-01-25 21:56:18 +000013
14#include "common/system.h"
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000015#include <d3d9.h>
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000016
17namespace gl
18{
19
20typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR);
21
22static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg)
23{
24#if !defined(ANGLE_DISABLE_PERF)
25 if (perfActive())
26 {
27 char message[32768];
28 int len = vsprintf_s(message, format, vararg);
29 if (len < 0)
30 {
31 return;
32 }
33
34 // There are no ASCII variants of these D3DPERF functions.
35 wchar_t wideMessage[32768];
36 for (int i = 0; i < len; ++i)
37 {
38 wideMessage[i] = message[i];
39 }
40 wideMessage[len] = 0;
41
42 perfFunc(0, wideMessage);
43 }
44#endif
45
46#if !defined(ANGLE_DISABLE_TRACE)
47#if defined(NDEBUG)
48 if (traceFileDebugOnly)
49 {
50 return;
51 }
52#endif
53
54 FILE* file = fopen(TRACE_OUTPUT_FILE, "a");
55 if (file)
56 {
57 vfprintf(file, format, vararg);
58 fclose(file);
59 }
60#endif
61}
62
63void trace(bool traceFileDebugOnly, const char *format, ...)
64{
65 va_list vararg;
66 va_start(vararg, format);
67#if defined(ANGLE_DISABLE_PERF)
68 output(traceFileDebugOnly, NULL, format, vararg);
69#else
70 output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg);
71#endif
72 va_end(vararg);
73}
74
75bool perfActive()
76{
77#if defined(ANGLE_DISABLE_PERF)
78 return false;
79#else
80 static bool active = D3DPERF_GetStatus() != 0;
81 return active;
82#endif
83}
84
85ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
86{
87#if !defined(ANGLE_DISABLE_PERF)
daniel@transgaming.com5dc3b8b2012-11-28 19:43:49 +000088#if defined(ANGLE_DISABLE_TRACE)
89 if (!perfActive())
90 {
91 return;
92 }
93#endif
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000094 va_list vararg;
95 va_start(vararg, format);
96 output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg);
97 va_end(vararg);
98#endif
99}
100
101ScopedPerfEventHelper::~ScopedPerfEventHelper()
102{
103#if !defined(ANGLE_DISABLE_PERF)
104 if (perfActive())
105 {
106 D3DPERF_EndEvent();
107 }
108#endif
109}
110}