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 | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 11 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 12 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 13 | #include <d3d9.h> |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 14 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 15 | |
| 16 | namespace gl |
| 17 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 18 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 19 | typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR); |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 20 | #else |
| 21 | typedef void (*PerfOutputFunction)(unsigned int, const wchar_t*); |
| 22 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 23 | |
| 24 | static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg) |
| 25 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 26 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 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 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 46 | #endif // ANGLE_ENABLE_PERF |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 47 | |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 48 | #if defined(ANGLE_ENABLE_TRACE) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 49 | #if defined(NDEBUG) |
| 50 | if (traceFileDebugOnly) |
| 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 | |
| 56 | FILE* file = fopen(TRACE_OUTPUT_FILE, "a"); |
| 57 | if (file) |
| 58 | { |
| 59 | vfprintf(file, format, vararg); |
| 60 | fclose(file); |
| 61 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 62 | #endif // ANGLE_ENABLE_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void trace(bool traceFileDebugOnly, const char *format, ...) |
| 66 | { |
| 67 | va_list vararg; |
| 68 | va_start(vararg, format); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 69 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 70 | output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 71 | #else |
| 72 | output(traceFileDebugOnly, NULL, format, vararg); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 73 | #endif |
| 74 | va_end(vararg); |
| 75 | } |
| 76 | |
| 77 | bool perfActive() |
| 78 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 79 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 80 | static bool active = D3DPERF_GetStatus() != 0; |
| 81 | return active; |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 82 | #else |
| 83 | return false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 84 | #endif |
| 85 | } |
| 86 | |
| 87 | ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) |
| 88 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 89 | #if defined(ANGLE_ENABLE_PERF) |
| 90 | #if !defined(ANGLE_ENABLE_TRACE) |
daniel@transgaming.com | 5dc3b8b | 2012-11-28 19:43:49 +0000 | [diff] [blame] | 91 | if (!perfActive()) |
| 92 | { |
| 93 | return; |
| 94 | } |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 95 | #endif // !ANGLE_ENABLE_TRACE |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 96 | va_list vararg; |
| 97 | va_start(vararg, format); |
| 98 | output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg); |
| 99 | va_end(vararg); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 100 | #endif // ANGLE_ENABLE_PERF |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | ScopedPerfEventHelper::~ScopedPerfEventHelper() |
| 104 | { |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 105 | #if defined(ANGLE_ENABLE_PERF) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 106 | if (perfActive()) |
| 107 | { |
| 108 | D3DPERF_EndEvent(); |
| 109 | } |
| 110 | #endif |
| 111 | } |
| 112 | } |