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" |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdarg.h> |
shannon.woods@transgaming.com | a9b96d0 | 2013-01-25 21:56:18 +0000 | [diff] [blame^] | 13 | |
| 14 | #include "common/system.h" |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 15 | #include <d3d9.h> |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 16 | |
| 17 | namespace gl |
| 18 | { |
| 19 | |
| 20 | typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR); |
| 21 | |
| 22 | static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg) |
| 23 | { |
| 24 | #if !defined(ANGLE_DISABLE_PERF) |
| 25 | if (perfActive()) |
| 26 | { |
| 27 | char message[32768]; |
| 28 | int len = vsprintf_s(message, format, vararg); |
| 29 | if (len < 0) |
| 30 | { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | // There are no ASCII variants of these D3DPERF functions. |
| 35 | wchar_t wideMessage[32768]; |
| 36 | for (int i = 0; i < len; ++i) |
| 37 | { |
| 38 | wideMessage[i] = message[i]; |
| 39 | } |
| 40 | wideMessage[len] = 0; |
| 41 | |
| 42 | perfFunc(0, wideMessage); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | #if !defined(ANGLE_DISABLE_TRACE) |
| 47 | #if defined(NDEBUG) |
| 48 | if (traceFileDebugOnly) |
| 49 | { |
| 50 | return; |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | FILE* file = fopen(TRACE_OUTPUT_FILE, "a"); |
| 55 | if (file) |
| 56 | { |
| 57 | vfprintf(file, format, vararg); |
| 58 | fclose(file); |
| 59 | } |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | void trace(bool traceFileDebugOnly, const char *format, ...) |
| 64 | { |
| 65 | va_list vararg; |
| 66 | va_start(vararg, format); |
| 67 | #if defined(ANGLE_DISABLE_PERF) |
| 68 | output(traceFileDebugOnly, NULL, format, vararg); |
| 69 | #else |
| 70 | output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg); |
| 71 | #endif |
| 72 | va_end(vararg); |
| 73 | } |
| 74 | |
| 75 | bool perfActive() |
| 76 | { |
| 77 | #if defined(ANGLE_DISABLE_PERF) |
| 78 | return false; |
| 79 | #else |
| 80 | static bool active = D3DPERF_GetStatus() != 0; |
| 81 | return active; |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) |
| 86 | { |
| 87 | #if !defined(ANGLE_DISABLE_PERF) |
daniel@transgaming.com | 5dc3b8b | 2012-11-28 19:43:49 +0000 | [diff] [blame] | 88 | #if defined(ANGLE_DISABLE_TRACE) |
| 89 | if (!perfActive()) |
| 90 | { |
| 91 | return; |
| 92 | } |
| 93 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 94 | va_list vararg; |
| 95 | va_start(vararg, format); |
| 96 | output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg); |
| 97 | va_end(vararg); |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | ScopedPerfEventHelper::~ScopedPerfEventHelper() |
| 102 | { |
| 103 | #if !defined(ANGLE_DISABLE_PERF) |
| 104 | if (perfActive()) |
| 105 | { |
| 106 | D3DPERF_EndEvent(); |
| 107 | } |
| 108 | #endif |
| 109 | } |
| 110 | } |