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