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 | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 10 | #include <stdarg.h> |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 11 | #include <vector> |
| 12 | #include <fstream> |
| 13 | #include <cstdio> |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 14 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 15 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 16 | #include <d3d9.h> |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 17 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 18 | |
| 19 | namespace gl |
| 20 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 21 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 22 | typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR); |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 23 | #else |
| 24 | typedef void (*PerfOutputFunction)(unsigned int, const wchar_t*); |
| 25 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 26 | |
| 27 | static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg) |
| 28 | { |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 29 | #if defined(ANGLE_ENABLE_PERF) || defined(ANGLE_ENABLE_TRACE) |
| 30 | static std::vector<char> asciiMessageBuffer(512); |
| 31 | |
| 32 | // Attempt to just print to the current buffer |
| 33 | int len = std::vsnprintf(&asciiMessageBuffer[0], asciiMessageBuffer.size(), format, vararg); |
| 34 | if (len < 0 || static_cast<size_t>(len) >= asciiMessageBuffer.size()) |
| 35 | { |
| 36 | // Buffer was not large enough, calculate the required size and resize the buffer |
| 37 | len = std::vsnprintf(NULL, 0, format, vararg); |
| 38 | asciiMessageBuffer.resize(len + 1); |
| 39 | |
| 40 | // Print again |
| 41 | std::vsnprintf(&asciiMessageBuffer[0], asciiMessageBuffer.size(), format, vararg); |
| 42 | } |
| 43 | |
| 44 | // NULL terminate the buffer to be safe |
| 45 | asciiMessageBuffer[len] = '\0'; |
| 46 | #endif |
| 47 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 48 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 49 | if (perfActive()) |
| 50 | { |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 51 | // The perf function only accepts wide strings, widen the ascii message |
| 52 | static std::wstring wideMessage; |
| 53 | if (wideMessage.capacity() < asciiMessageBuffer.size()) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 54 | { |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 55 | wideMessage.reserve(asciiMessageBuffer.size()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 58 | wideMessage.assign(asciiMessageBuffer.begin(), asciiMessageBuffer.begin() + len); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 59 | |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 60 | perfFunc(0, wideMessage.c_str()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 61 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 62 | #endif // ANGLE_ENABLE_PERF |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 63 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 64 | #if defined(ANGLE_ENABLE_TRACE) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 65 | #if defined(NDEBUG) |
| 66 | if (traceFileDebugOnly) |
| 67 | { |
| 68 | return; |
| 69 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 70 | #endif // NDEBUG |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 71 | |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 72 | static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 73 | if (file) |
| 74 | { |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 75 | file.write(&asciiMessageBuffer[0], len); |
| 76 | file.flush(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 77 | } |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame^] | 78 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 79 | #endif // ANGLE_ENABLE_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void trace(bool traceFileDebugOnly, const char *format, ...) |
| 83 | { |
| 84 | va_list vararg; |
| 85 | va_start(vararg, format); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 86 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 87 | output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 88 | #else |
| 89 | output(traceFileDebugOnly, NULL, format, vararg); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 90 | #endif |
| 91 | va_end(vararg); |
| 92 | } |
| 93 | |
| 94 | bool perfActive() |
| 95 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 96 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 97 | static bool active = D3DPERF_GetStatus() != 0; |
| 98 | return active; |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 99 | #else |
| 100 | return false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 101 | #endif |
| 102 | } |
| 103 | |
| 104 | ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) |
| 105 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 106 | #if defined(ANGLE_ENABLE_PERF) |
| 107 | #if !defined(ANGLE_ENABLE_TRACE) |
daniel@transgaming.com | 5dc3b8b | 2012-11-28 19:43:49 +0000 | [diff] [blame] | 108 | if (!perfActive()) |
| 109 | { |
| 110 | return; |
| 111 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 112 | #endif // !ANGLE_ENABLE_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 113 | va_list vararg; |
| 114 | va_start(vararg, format); |
| 115 | output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg); |
| 116 | va_end(vararg); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 117 | #endif // ANGLE_ENABLE_PERF |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | ScopedPerfEventHelper::~ScopedPerfEventHelper() |
| 121 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 122 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 123 | if (perfActive()) |
| 124 | { |
| 125 | D3DPERF_EndEvent(); |
| 126 | } |
| 127 | #endif |
| 128 | } |
| 129 | } |