blob: 05d3dc62ad49610001259b9b8ba6e31dc99a0733 [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"
Geoff Lang83217792014-01-16 09:52:38 -050010#include <stdarg.h>
Geoff Lang68509472013-10-07 17:06:30 -040011
Geoff Langf5713122013-10-07 17:06:30 -040012#if defined(ANGLE_ENABLE_PERF)
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 Langf5713122013-10-07 17:06:30 -040018#if defined(ANGLE_ENABLE_PERF)
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{
Geoff Langf5713122013-10-07 17:06:30 -040026#if defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000027 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 }
Geoff Langf5713122013-10-07 17:06:30 -040046#endif // ANGLE_ENABLE_PERF
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000047
Geoff Langf5713122013-10-07 17:06:30 -040048#if defined(ANGLE_ENABLE_TRACE)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000049#if defined(NDEBUG)
50 if (traceFileDebugOnly)
51 {
52 return;
53 }
Geoff Langf5713122013-10-07 17:06:30 -040054#endif // NDEBUG
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000055
56 FILE* file = fopen(TRACE_OUTPUT_FILE, "a");
57 if (file)
58 {
59 vfprintf(file, format, vararg);
60 fclose(file);
61 }
Geoff Langf5713122013-10-07 17:06:30 -040062#endif // ANGLE_ENABLE_TRACE
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000063}
64
65void trace(bool traceFileDebugOnly, const char *format, ...)
66{
67 va_list vararg;
68 va_start(vararg, format);
Geoff Langf5713122013-10-07 17:06:30 -040069#if defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000070 output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg);
Geoff Langf5713122013-10-07 17:06:30 -040071#else
72 output(traceFileDebugOnly, NULL, format, vararg);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000073#endif
74 va_end(vararg);
75}
76
77bool perfActive()
78{
Geoff Langf5713122013-10-07 17:06:30 -040079#if defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000080 static bool active = D3DPERF_GetStatus() != 0;
81 return active;
Geoff Langf5713122013-10-07 17:06:30 -040082#else
83 return false;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000084#endif
85}
86
87ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
88{
Geoff Langf5713122013-10-07 17:06:30 -040089#if defined(ANGLE_ENABLE_PERF)
90#if !defined(ANGLE_ENABLE_TRACE)
daniel@transgaming.com5dc3b8b2012-11-28 19:43:49 +000091 if (!perfActive())
92 {
93 return;
94 }
Geoff Langf5713122013-10-07 17:06:30 -040095#endif // !ANGLE_ENABLE_TRACE
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);
Geoff Langf5713122013-10-07 17:06:30 -0400100#endif // ANGLE_ENABLE_PERF
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000101}
102
103ScopedPerfEventHelper::~ScopedPerfEventHelper()
104{
Geoff Langf5713122013-10-07 17:06:30 -0400105#if defined(ANGLE_ENABLE_PERF)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000106 if (perfActive())
107 {
108 D3DPERF_EndEvent();
109 }
110#endif
111}
112}