blob: e09d5606806c32fdf2b32a0dd993c044a6b0e4a6 [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 Lang44fa7592014-05-30 11:50:07 -040010#include "common/platform.h"
Geoff Langda5777c2014-07-11 09:52:58 -040011#include "common/angleutils.h"
Geoff Lang44fa7592014-05-30 11:50:07 -040012
Geoff Lang83217792014-01-16 09:52:38 -050013#include <stdarg.h>
Geoff Langfeee44b2014-03-05 11:34:31 -050014#include <vector>
15#include <fstream>
16#include <cstdio>
Geoff Lang68509472013-10-07 17:06:30 -040017
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000018namespace gl
19{
Austin Kinross570e83c2014-10-20 14:13:58 -070020#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000021typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR);
Geoff Lang68509472013-10-07 17:06:30 -040022#else
23typedef void (*PerfOutputFunction)(unsigned int, const wchar_t*);
24#endif
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000025
Austin Kinrossfe14d452014-10-20 14:36:18 -070026static void output(bool traceInDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000027{
Austin Kinrossf0360c62014-10-20 14:26:13 -070028#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) || defined(ANGLE_ENABLE_DEBUG_TRACE)
Geoff Langda5777c2014-07-11 09:52:58 -040029 std::string formattedMessage = FormatString(format, vararg);
Geoff Langfeee44b2014-03-05 11:34:31 -050030#endif
31
Austin Kinross570e83c2014-10-20 14:13:58 -070032#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000033 if (perfActive())
34 {
Geoff Langfeee44b2014-03-05 11:34:31 -050035 // The perf function only accepts wide strings, widen the ascii message
36 static std::wstring wideMessage;
Geoff Langda5777c2014-07-11 09:52:58 -040037 if (wideMessage.capacity() < formattedMessage.length())
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000038 {
Geoff Langda5777c2014-07-11 09:52:58 -040039 wideMessage.reserve(formattedMessage.size());
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000040 }
41
Geoff Langda5777c2014-07-11 09:52:58 -040042 wideMessage.assign(formattedMessage.begin(), formattedMessage.end());
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000043
Geoff Langfeee44b2014-03-05 11:34:31 -050044 perfFunc(0, wideMessage.c_str());
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000045 }
Austin Kinross570e83c2014-10-20 14:13:58 -070046#endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000047
Austin Kinrossf0360c62014-10-20 14:26:13 -070048#if defined(ANGLE_ENABLE_DEBUG_TRACE)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000049#if defined(NDEBUG)
Austin Kinrossfe14d452014-10-20 14:36:18 -070050 if (traceInDebugOnly)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000051 {
52 return;
53 }
Geoff Langf5713122013-10-07 17:06:30 -040054#endif // NDEBUG
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000055
Geoff Langfeee44b2014-03-05 11:34:31 -050056 static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000057 if (file)
58 {
Geoff Langda5777c2014-07-11 09:52:58 -040059 file.write(formattedMessage.c_str(), formattedMessage.length());
Geoff Langfeee44b2014-03-05 11:34:31 -050060 file.flush();
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000061 }
Geoff Langfeee44b2014-03-05 11:34:31 -050062
Austin Kinrossfe14d452014-10-20 14:36:18 -070063#if defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER)
64 OutputDebugStringA(formattedMessage.c_str());
65#endif // ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER
66
Austin Kinrossf0360c62014-10-20 14:26:13 -070067#endif // ANGLE_ENABLE_DEBUG_TRACE
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000068}
69
Austin Kinrossfe14d452014-10-20 14:36:18 -070070void trace(bool traceInDebugOnly, const char *format, ...)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000071{
72 va_list vararg;
73 va_start(vararg, format);
Austin Kinross570e83c2014-10-20 14:13:58 -070074#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Austin Kinrossfe14d452014-10-20 14:36:18 -070075 output(traceInDebugOnly, D3DPERF_SetMarker, format, vararg);
Geoff Langf5713122013-10-07 17:06:30 -040076#else
Austin Kinrossfe14d452014-10-20 14:36:18 -070077 output(traceInDebugOnly, NULL, format, vararg);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000078#endif
79 va_end(vararg);
80}
81
82bool perfActive()
83{
Austin Kinross570e83c2014-10-20 14:13:58 -070084#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000085 static bool active = D3DPERF_GetStatus() != 0;
86 return active;
Geoff Langf5713122013-10-07 17:06:30 -040087#else
88 return false;
daniel@transgaming.com95a758f2012-07-12 15:17:06 +000089#endif
90}
91
92ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
93{
Austin Kinrossf0360c62014-10-20 14:26:13 -070094#if !defined(ANGLE_ENABLE_DEBUG_TRACE)
daniel@transgaming.com5dc3b8b2012-11-28 19:43:49 +000095 if (!perfActive())
96 {
97 return;
98 }
Austin Kinrossf0360c62014-10-20 14:26:13 -070099#endif // !ANGLE_ENABLE_DEBUG_TRACE
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000100 va_list vararg;
101 va_start(vararg, format);
Austin Kinross570e83c2014-10-20 14:13:58 -0700102#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000103 output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg);
Jamie Madillb60fe312014-09-26 14:56:41 -0400104#else
105 output(true, NULL, format, vararg);
Austin Kinross570e83c2014-10-20 14:13:58 -0700106#endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS
Jamie Madillb60fe312014-09-26 14:56:41 -0400107 va_end(vararg);
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000108}
109
110ScopedPerfEventHelper::~ScopedPerfEventHelper()
111{
Austin Kinross570e83c2014-10-20 14:13:58 -0700112#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
daniel@transgaming.com95a758f2012-07-12 15:17:06 +0000113 if (perfActive())
114 {
115 D3DPERF_EndEvent();
116 }
117#endif
118}
119}