daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +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.h: Debugging utilities. |
| 8 | |
| 9 | #ifndef COMMON_DEBUG_H_ |
| 10 | #define COMMON_DEBUG_H_ |
| 11 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | #include <assert.h> |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 13 | #include <stdio.h> |
| 14 | #include <string> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 16 | #include "common/angleutils.h" |
| 17 | |
| 18 | #if !defined(TRACE_OUTPUT_FILE) |
| 19 | #define TRACE_OUTPUT_FILE "debug.txt" |
| 20 | #endif |
| 21 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 22 | namespace gl |
| 23 | { |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 24 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 25 | enum MessageType |
| 26 | { |
| 27 | MESSAGE_TRACE, |
| 28 | MESSAGE_FIXME, |
| 29 | MESSAGE_ERR, |
| 30 | MESSAGE_EVENT, |
| 31 | }; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 32 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 33 | // Outputs text to the debugging log, or the debugging window |
| 34 | void trace(bool traceInDebugOnly, MessageType messageType, const char *format, ...); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 35 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 36 | // Pairs a D3D begin event with an end event. |
| 37 | class ScopedPerfEventHelper |
| 38 | { |
| 39 | public: |
| 40 | ScopedPerfEventHelper(const char* format, ...); |
| 41 | ~ScopedPerfEventHelper(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 42 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 43 | private: |
| 44 | DISALLOW_COPY_AND_ASSIGN(ScopedPerfEventHelper); |
| 45 | }; |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 46 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 47 | // Wraps the D3D9/D3D11 debug annotation functions. |
| 48 | class DebugAnnotator |
| 49 | { |
| 50 | public: |
| 51 | DebugAnnotator() { }; |
| 52 | virtual ~DebugAnnotator() { }; |
| 53 | virtual void beginEvent(const std::wstring &eventName) = 0; |
| 54 | virtual void endEvent() = 0; |
| 55 | virtual void setMarker(const std::wstring &markerName) = 0; |
| 56 | virtual bool getStatus() = 0; |
| 57 | |
| 58 | private: |
| 59 | DISALLOW_COPY_AND_ASSIGN(DebugAnnotator); |
| 60 | }; |
| 61 | |
| 62 | void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator); |
| 63 | void UninitializeDebugAnnotations(); |
| 64 | bool DebugAnnotationsActive(); |
| 65 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 68 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 69 | #define ANGLE_TRACE_ENABLED |
| 70 | #endif |
| 71 | |
| 72 | // A macro to output a trace of a function call and its arguments to the debugging log |
| 73 | #if defined(ANGLE_TRACE_ENABLED) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 74 | #define TRACE(message, ...) gl::trace(true, gl::MESSAGE_TRACE, "trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 75 | #else |
| 76 | #define TRACE(message, ...) (void(0)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 79 | // A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing. |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 80 | #if defined(ANGLE_TRACE_ENABLED) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 81 | #define FIXME(message, ...) gl::trace(false, gl::MESSAGE_FIXME, "fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 82 | #else |
| 83 | #define FIXME(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 84 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 85 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 86 | // A macro to output a function call and its arguments to the debugging log, in case of error. |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 87 | #if defined(ANGLE_TRACE_ENABLED) |
Jamie Madill | 14aa40f | 2015-01-07 15:12:47 -0500 | [diff] [blame] | 88 | #define ERR(message, ...) gl::trace(false, gl::MESSAGE_ERR, "err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 89 | #else |
| 90 | #define ERR(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | // A macro to log a performance event around a scope. |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 94 | #if defined(ANGLE_TRACE_ENABLED) |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 95 | #if defined(_MSC_VER) |
Ehsan Akhgari | c1cc4c4 | 2014-07-02 13:09:07 -0400 | [diff] [blame] | 96 | #define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper ## __LINE__("%s" message "\n", __FUNCTION__, __VA_ARGS__); |
daniel@transgaming.com | 1825d8e | 2012-08-27 16:25:29 +0000 | [diff] [blame] | 97 | #else |
| 98 | #define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper(message "\n", ##__VA_ARGS__); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 99 | #endif // _MSC_VER |
| 100 | #else |
| 101 | #define EVENT(message, ...) (void(0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 102 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame^] | 104 | #if defined(ANGLE_TRACE_ENABLED) |
| 105 | #undef ANGLE_TRACE_ENABLED |
| 106 | #endif |
| 107 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 108 | // A macro asserting a condition and outputting failures to the debug log |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 109 | #if !defined(NDEBUG) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 110 | #define ASSERT(expression) do { \ |
| 111 | if(!(expression)) \ |
| 112 | ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \ |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 113 | assert(expression); \ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 114 | } while(0) |
Geoff Lang | 9cd1915 | 2014-05-28 15:54:34 -0400 | [diff] [blame] | 115 | #define UNUSED_ASSERTION_VARIABLE(variable) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 116 | #else |
| 117 | #define ASSERT(expression) (void(0)) |
Geoff Lang | 9cd1915 | 2014-05-28 15:54:34 -0400 | [diff] [blame] | 118 | #define UNUSED_ASSERTION_VARIABLE(variable) ((void)variable) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 119 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 120 | |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 121 | #ifndef ANGLE_ENABLE_DEBUG_TRACE |
Jamie Madill | bae30a0 | 2014-06-17 10:45:02 -0400 | [diff] [blame] | 122 | #define UNUSED_TRACE_VARIABLE(variable) ((void)variable) |
| 123 | #else |
| 124 | #define UNUSED_TRACE_VARIABLE(variable) |
| 125 | #endif |
| 126 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | // A macro to indicate unimplemented functionality |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 128 | |
Shannon Woods | 950cb60 | 2014-09-18 10:01:56 -0400 | [diff] [blame] | 129 | #if defined (ANGLE_TEST_CONFIG) |
| 130 | #define NOASSERT_UNIMPLEMENTED 1 |
| 131 | #endif |
| 132 | |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 133 | // Define NOASSERT_UNIMPLEMENTED to non zero to skip the assert fail in the unimplemented checks |
| 134 | // This will allow us to test with some automated test suites (eg dEQP) without crashing |
| 135 | #ifndef NOASSERT_UNIMPLEMENTED |
| 136 | #define NOASSERT_UNIMPLEMENTED 0 |
| 137 | #endif |
| 138 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 139 | #if !defined(NDEBUG) |
| 140 | #define UNIMPLEMENTED() do { \ |
| 141 | FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \ |
shannonwoods@chromium.org | eafb069 | 2013-05-30 00:20:44 +0000 | [diff] [blame] | 142 | assert(NOASSERT_UNIMPLEMENTED); \ |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 143 | } while(0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 144 | #else |
| 145 | #define UNIMPLEMENTED() FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__) |
| 146 | #endif |
| 147 | |
| 148 | // A macro for code which is not expected to be reached under valid assumptions |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 149 | #if !defined(NDEBUG) |
| 150 | #define UNREACHABLE() do { \ |
| 151 | ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__); \ |
| 152 | assert(false); \ |
| 153 | } while(0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 154 | #else |
| 155 | #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__) |
| 156 | #endif |
| 157 | |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 158 | // A macro that determines whether an object has a given runtime type. |
Geoff Lang | 8690da8 | 2013-10-08 10:57:09 -0400 | [diff] [blame] | 159 | #if !defined(NDEBUG) && (!defined(_MSC_VER) || defined(_CPPRTTI)) && (!defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || defined(__GXX_RTTI)) |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 160 | #define HAS_DYNAMIC_TYPE(type, obj) (dynamic_cast<type >(obj) != NULL) |
| 161 | #else |
| 162 | #define HAS_DYNAMIC_TYPE(type, obj) true |
| 163 | #endif |
| 164 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | // A macro functioning as a compile-time assert to validate constant conditions |
Jamie Madill | fd67b1b | 2015-03-10 16:13:24 -0400 | [diff] [blame] | 166 | |
| 167 | #if (defined(_MSC_VER) && _MSC_VER >= 1600) |
| 168 | #define ANGLE_HAS_STATIC_ASSERT |
| 169 | #elif (defined(__GNUC__) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)) |
| 170 | #define ANGLE_HAS_STATIC_ASSERT |
| 171 | #elif defined(__clang__) && __has_feature(cxx_static_assert) |
| 172 | #define ANGLE_HAS_STATIC_ASSERT |
| 173 | #endif |
| 174 | |
| 175 | #if defined(ANGLE_HAS_STATIC_ASSERT) |
Geoff Lang | d0f8e82 | 2013-09-30 15:21:53 -0400 | [diff] [blame] | 176 | #define META_ASSERT_MSG(condition, msg) static_assert(condition, msg) |
| 177 | #else |
Jamie Madill | 4f26786 | 2014-04-17 15:53:37 -0400 | [diff] [blame] | 178 | #define META_ASSERT_CONCAT(a, b) a ## b |
| 179 | #define META_ASSERT_CONCAT2(a, b) META_ASSERT_CONCAT(a, b) |
| 180 | #define META_ASSERT_MSG(condition, msg) typedef int META_ASSERT_CONCAT2(COMPILE_TIME_ASSERT_, __LINE__)[static_cast<bool>(condition)?1:-1] |
Geoff Lang | d0f8e82 | 2013-09-30 15:21:53 -0400 | [diff] [blame] | 181 | #endif |
| 182 | #define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 183 | |
| 184 | #endif // COMMON_DEBUG_H_ |