daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 1 | // |
| 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 Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 10 | #include "common/platform.h" |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 11 | #include "common/angleutils.h" |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 12 | |
Geoff Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 13 | #include <stdarg.h> |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 14 | #include <vector> |
| 15 | #include <fstream> |
| 16 | #include <cstdio> |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 17 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 18 | namespace gl |
| 19 | { |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 20 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 21 | typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR); |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 22 | #else |
| 23 | typedef void (*PerfOutputFunction)(unsigned int, const wchar_t*); |
| 24 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 25 | |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 26 | static void output(bool traceInDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 27 | { |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 28 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) || defined(ANGLE_ENABLE_DEBUG_TRACE) |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 29 | std::string formattedMessage = FormatString(format, vararg); |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 30 | #endif |
| 31 | |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 32 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 33 | if (perfActive()) |
| 34 | { |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 35 | // The perf function only accepts wide strings, widen the ascii message |
| 36 | static std::wstring wideMessage; |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 37 | if (wideMessage.capacity() < formattedMessage.length()) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 38 | { |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 39 | wideMessage.reserve(formattedMessage.size()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 42 | wideMessage.assign(formattedMessage.begin(), formattedMessage.end()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 43 | |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 44 | perfFunc(0, wideMessage.c_str()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 45 | } |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 46 | #endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 47 | |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 48 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 49 | #if defined(NDEBUG) |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 50 | if (traceInDebugOnly) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 51 | { |
| 52 | return; |
| 53 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 54 | #endif // NDEBUG |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 55 | |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 56 | static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 57 | if (file) |
| 58 | { |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 59 | file.write(formattedMessage.c_str(), formattedMessage.length()); |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 60 | file.flush(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 61 | } |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 62 | |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 63 | #if defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER) |
| 64 | OutputDebugStringA(formattedMessage.c_str()); |
| 65 | #endif // ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER |
| 66 | |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 67 | #endif // ANGLE_ENABLE_DEBUG_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 70 | void trace(bool traceInDebugOnly, const char *format, ...) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 71 | { |
| 72 | va_list vararg; |
| 73 | va_start(vararg, format); |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 74 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 75 | output(traceInDebugOnly, D3DPERF_SetMarker, format, vararg); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 76 | #else |
Austin Kinross | fe14d45 | 2014-10-20 14:36:18 -0700 | [diff] [blame] | 77 | output(traceInDebugOnly, NULL, format, vararg); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 78 | #endif |
| 79 | va_end(vararg); |
| 80 | } |
| 81 | |
| 82 | bool perfActive() |
| 83 | { |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 84 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 85 | static bool active = D3DPERF_GetStatus() != 0; |
| 86 | return active; |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 87 | #else |
| 88 | return false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
| 92 | ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) |
| 93 | { |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 94 | #if !defined(ANGLE_ENABLE_DEBUG_TRACE) |
daniel@transgaming.com | 5dc3b8b | 2012-11-28 19:43:49 +0000 | [diff] [blame] | 95 | if (!perfActive()) |
| 96 | { |
| 97 | return; |
| 98 | } |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 99 | #endif // !ANGLE_ENABLE_DEBUG_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 100 | va_list vararg; |
| 101 | va_start(vararg, format); |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 102 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 103 | output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg); |
Jamie Madill | b60fe31 | 2014-09-26 14:56:41 -0400 | [diff] [blame] | 104 | #else |
| 105 | output(true, NULL, format, vararg); |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 106 | #endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS |
Jamie Madill | b60fe31 | 2014-09-26 14:56:41 -0400 | [diff] [blame] | 107 | va_end(vararg); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ScopedPerfEventHelper::~ScopedPerfEventHelper() |
| 111 | { |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 112 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 113 | if (perfActive()) |
| 114 | { |
| 115 | D3DPERF_EndEvent(); |
| 116 | } |
| 117 | #endif |
| 118 | } |
| 119 | } |