blob: dc3ac0a483947483c060e3c16bb05214262b98b0 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.com4f39fd92010-03-08 20:26:45 +000012#include <assert.h>
Jamie Madill2c7c6252015-03-12 14:15:38 -040013#include <stdio.h>
14#include <string>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000016#include "common/angleutils.h"
17
18#if !defined(TRACE_OUTPUT_FILE)
19#define TRACE_OUTPUT_FILE "debug.txt"
20#endif
21
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace gl
23{
Jamie Madill14aa40f2015-01-07 15:12:47 -050024
Jamie Madill2c7c6252015-03-12 14:15:38 -040025enum MessageType
26{
27 MESSAGE_TRACE,
28 MESSAGE_FIXME,
29 MESSAGE_ERR,
30 MESSAGE_EVENT,
31};
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000032
Jamie Madill2c7c6252015-03-12 14:15:38 -040033// Outputs text to the debugging log, or the debugging window
34void trace(bool traceInDebugOnly, MessageType messageType, const char *format, ...);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000035
Jamie Madill2c7c6252015-03-12 14:15:38 -040036// Pairs a D3D begin event with an end event.
37class ScopedPerfEventHelper
38{
39 public:
40 ScopedPerfEventHelper(const char* format, ...);
41 ~ScopedPerfEventHelper();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000042
Jamie Madill2c7c6252015-03-12 14:15:38 -040043 private:
44 DISALLOW_COPY_AND_ASSIGN(ScopedPerfEventHelper);
45};
Austin Kinross922a9fb2014-10-21 14:26:33 -070046
Jamie Madill2c7c6252015-03-12 14:15:38 -040047// Wraps the D3D9/D3D11 debug annotation functions.
48class 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
62void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator);
63void UninitializeDebugAnnotations();
64bool DebugAnnotationsActive();
65
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066}
67
Austin Kinrossf0360c62014-10-20 14:26:13 -070068#if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
Jamie Madill2c7c6252015-03-12 14:15:38 -040069#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 Madill14aa40f2015-01-07 15:12:47 -050074#define TRACE(message, ...) gl::trace(true, gl::MESSAGE_TRACE, "trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040075#else
76#define TRACE(message, ...) (void(0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077#endif
78
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000079// A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
Jamie Madill2c7c6252015-03-12 14:15:38 -040080#if defined(ANGLE_TRACE_ENABLED)
Jamie Madill14aa40f2015-01-07 15:12:47 -050081#define FIXME(message, ...) gl::trace(false, gl::MESSAGE_FIXME, "fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040082#else
83#define FIXME(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000084#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000086// A macro to output a function call and its arguments to the debugging log, in case of error.
Jamie Madill2c7c6252015-03-12 14:15:38 -040087#if defined(ANGLE_TRACE_ENABLED)
Jamie Madill14aa40f2015-01-07 15:12:47 -050088#define ERR(message, ...) gl::trace(false, gl::MESSAGE_ERR, "err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
Geoff Langf5713122013-10-07 17:06:30 -040089#else
90#define ERR(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000091#endif
92
93// A macro to log a performance event around a scope.
Jamie Madill2c7c6252015-03-12 14:15:38 -040094#if defined(ANGLE_TRACE_ENABLED)
Geoff Langf5713122013-10-07 17:06:30 -040095#if defined(_MSC_VER)
Ehsan Akhgaric1cc4c42014-07-02 13:09:07 -040096#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper ## __LINE__("%s" message "\n", __FUNCTION__, __VA_ARGS__);
daniel@transgaming.com1825d8e2012-08-27 16:25:29 +000097#else
98#define EVENT(message, ...) gl::ScopedPerfEventHelper scopedPerfEventHelper(message "\n", ##__VA_ARGS__);
Geoff Langf5713122013-10-07 17:06:30 -040099#endif // _MSC_VER
100#else
101#define EVENT(message, ...) (void(0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000102#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
Jamie Madill2c7c6252015-03-12 14:15:38 -0400104#if defined(ANGLE_TRACE_ENABLED)
105#undef ANGLE_TRACE_ENABLED
106#endif
107
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108// A macro asserting a condition and outputting failures to the debug log
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000109#if !defined(NDEBUG)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110#define ASSERT(expression) do { \
111 if(!(expression)) \
112 ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000113 assert(expression); \
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114 } while(0)
Geoff Lang9cd19152014-05-28 15:54:34 -0400115#define UNUSED_ASSERTION_VARIABLE(variable)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000116#else
117#define ASSERT(expression) (void(0))
Geoff Lang9cd19152014-05-28 15:54:34 -0400118#define UNUSED_ASSERTION_VARIABLE(variable) ((void)variable)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000119#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
Austin Kinrossf0360c62014-10-20 14:26:13 -0700121#ifndef ANGLE_ENABLE_DEBUG_TRACE
Jamie Madillbae30a02014-06-17 10:45:02 -0400122#define UNUSED_TRACE_VARIABLE(variable) ((void)variable)
123#else
124#define UNUSED_TRACE_VARIABLE(variable)
125#endif
126
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127// A macro to indicate unimplemented functionality
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000128
Shannon Woods950cb602014-09-18 10:01:56 -0400129#if defined (ANGLE_TEST_CONFIG)
130#define NOASSERT_UNIMPLEMENTED 1
131#endif
132
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000133// 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.org0f4cefe2011-01-26 19:30:57 +0000139#if !defined(NDEBUG)
140#define UNIMPLEMENTED() do { \
141 FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); \
shannonwoods@chromium.orgeafb0692013-05-30 00:20:44 +0000142 assert(NOASSERT_UNIMPLEMENTED); \
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000143 } while(0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144#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.org0f4cefe2011-01-26 19:30:57 +0000149#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.com4f39fd92010-03-08 20:26:45 +0000154#else
155 #define UNREACHABLE() ERR("\t! Unreachable reached: %s(%d)\n", __FUNCTION__, __LINE__)
156#endif
157
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000158// A macro that determines whether an object has a given runtime type.
Geoff Lang8690da82013-10-08 10:57:09 -0400159#if !defined(NDEBUG) && (!defined(_MSC_VER) || defined(_CPPRTTI)) && (!defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || defined(__GXX_RTTI))
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000160#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.com4f39fd92010-03-08 20:26:45 +0000165// A macro functioning as a compile-time assert to validate constant conditions
Jamie Madillfd67b1b2015-03-10 16:13:24 -0400166
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 Langd0f8e822013-09-30 15:21:53 -0400176#define META_ASSERT_MSG(condition, msg) static_assert(condition, msg)
177#else
Jamie Madill4f267862014-04-17 15:53:37 -0400178#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 Langd0f8e822013-09-30 15:21:53 -0400181#endif
182#define META_ASSERT(condition) META_ASSERT_MSG(condition, "compile time assertion failed.")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183
184#endif // COMMON_DEBUG_H_