blob: 9c18f60ef6d2984a5b8eb8f07bd874e1a4181a94 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrConfig_DEFINED
19#define GrConfig_DEFINED
20
21///////////////////////////////////////////////////////////////////////////////
22// preconfig section:
23//
24// All the work before including GrUserConfig.h should center around guessing
25// what platform we're on, and defining low-level symbols based on that.
26//
27// A build environment may have already defined symbols, so we first check
28// for that
29//
30
31// hack to ensure we know what sort of Apple platform we're on
32#if defined(__APPLE_CPP__) || defined(__APPLE_CC__)
33 #include <TargetConditionals.h>
34#endif
35
36/**
37 * Gr defines are set to 0 or 1, rather than being undefined or defined
38 */
39
40#if !defined(GR_ANDROID_BUILD)
41 #define GR_ANDROID_BUILD 0
42#endif
43#if !defined(GR_IOS_BUILD)
44 #define GR_IOS_BUILD 0
45#endif
46#if !defined(GR_LINUX_BUILD)
47 #define GR_LINUX_BUILD 0
48#endif
49#if !defined(GR_MAC_BUILD)
50 #define GR_MAC_BUILD 0
51#endif
52#if !defined(GR_WIN32_BUILD)
53 #define GR_WIN32_BUILD 0
54#endif
55#if !defined(GR_QNX_BUILD)
56 #define GR_QNX_BUILD 0
57#endif
58
59/**
60 * If no build target has been defined, attempt to infer.
61 */
62#if !GR_ANDROID_BUILD && !GR_IOS_BUILD && !GR_LINUX_BUILD && !GR_MAC_BUILD && !GR_WIN32_BUILD && !GR_QNX_BUILD
63 #if defined(_WIN32)
64 #undef GR_WIN32_BUILD
65 #define GR_WIN32_BUILD 1
66// #error "WIN"
67 #elif TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
68 #undef GR_IOS_BUILD
69 #define GR_IOS_BUILD 1
70// #error "IOS"
71 #elif ANDROID_NDK || defined(ANDROID)
72 #undef GR_ANDROID_BUILD
73 #define GR_ANDROID_BUILD 1
74// #error "ANDROID"
75 #elif TARGET_OS_MAC
76 #undef GR_MAC_BUILD
77 #define GR_MAC_BUILD 1
78// #error "MAC"
79 #elif TARGET_OS_QNX || defined(__QNXNTO__)
80 #undef GR_QNX_BUILD
81 #define GR_QNX_BUILD 1
82// #error "QNX"
83 #else
84 #undef GR_LINUX_BUILD
85 #define GR_LINUX_BUILD 1
86// #error "LINUX"
87 #endif
88#endif
89
90#if !defined(GR_DEBUG) && !defined(GR_RELEASE)
91 #ifdef NDEBUG
92 #define GR_DEBUG 0
93 #else
94 #define GR_DEBUG 1
95 #endif
96 #define GR_RELEASE !GR_DEBUG
97#endif
98
99///////////////////////////////////////////////////////////////////////////////
100///////////////////////////////////////////////////////////////////////////////
101
102/*
103 * Pull stdint.h in before user-config, to be sure our __STDC... macros are
104 * defined before anyone else might try to include stdint.h
105 */
106#define __STDC_LIMIT_MACROS
107#define __STDC_CONSTANT_MACROS
108#include <stdint.h>
109
110/*
111 * The "user config" file can be empty, and everything should work. It is
112 * meant to store a given platform/client's overrides of our guess-work.
113 *
114 * A alternate user config file can be specified by defining
115 * GR_USER_CONFIG_FILE. It should be defined relative to GrConfig.h
116 *
117 * e.g. it can specify GR_DEBUG/GR_RELEASE as it please, change the BUILD
118 * target, or supply its own defines for anything else (e.g. GR_SCALAR)
119 */
120#if !defined(GR_USER_CONFIG_FILE)
121 #include "GrUserConfig.h"
122#else
123 #include GR_USER_CONFIG_FILE
124#endif
125
126
127///////////////////////////////////////////////////////////////////////////////
128///////////////////////////////////////////////////////////////////////////////
129// postconfig section:
130//
131// By now we must have a GR_..._BUILD symbol set to 1, and a decision about
132// debug -vs- release
133//
134
135extern void GrPrintf(const char format[], ...);
136
137/**
138 * GR_STRING makes a string of X where X is expanded before conversion to a string
139 * if X itself contains macros.
140 */
141#define GR_STRING(X) GR_STRING_IMPL(X)
142#define GR_STRING_IMPL(X) #X
143
144/**
145 * GR_CONCAT concatenates X and Y where each is expanded before
146 * contanenation if either contains macros.
147 */
148#define GR_CONCAT(X,Y) GR_CONCAT_IMPL(X,Y)
149#define GR_CONCAT_IMPL(X,Y) X##Y
150
151/**
152 * Creates a string of the form "<filename>(<linenumber>) : "
153 */
154#define GR_FILE_AND_LINE_STR __FILE__ "(" GR_STRING(__LINE__) ") : "
155
156/**
157 * Compilers have different ways of issuing warnings. This macro
158 * attempts to abstract them, but may need to be specialized for your
159 * particular compiler.
160 * To insert compiler warnings use "#pragma message GR_WARN(<string>)"
161 */
162#if _MSC_VER
163 #define GR_WARN(MSG) (GR_FILE_AND_LINE_STR "WARNING: " MSG)
164#else//__GNUC__ - may need other defines for different compilers
165 #define GR_WARN(MSG) ("WARNING: " MSG)
166#endif
167
168/**
169 * GR_ALWAYSBREAK is an unconditional break in all builds.
170 */
171#if !defined(GR_ALWAYSBREAK)
172 #if GR_WIN32_BUILD
173 #define GR_ALWAYSBREAK __debugbreak()
174 #else
175 // TODO: do other platforms really not have continuable breakpoints?
176 // sign extend for 64bit architectures to be sure this is
177 // in the high address range
178 #define GR_ALWAYSBREAK *((int*)(int64_t)(int32_t)0xbeefcafe) = 0;
179 #endif
180#endif
181
182/**
183 * GR_DEBUGBREAK is an unconditional break in debug builds.
184 */
185#if !defined(GR_DEBUGBREAK)
186 #if GR_DEBUG
187 #define GR_DEBUGBREAK GR_ALWAYSBREAK
188 #else
189 #define GR_DEBUGBREAK
190 #endif
191#endif
192
193/**
194 * GR_ALWAYSASSERT is an assertion in all builds.
195 */
196#if !defined(GR_ALWAYSASSERT)
197 #define GR_ALWAYSASSERT(COND) \
198 do { \
199 if (!(COND)) { \
200 GrPrintf("%s %s failed\n", GR_FILE_AND_LINE_STR, #COND); \
201 GR_ALWAYSBREAK; \
202 } \
203 } while (false)
204#endif
205
206/**
207 * GR_DEBUGASSERT is an assertion in debug builds only.
208 */
209#if !defined(GR_DEBUGASSERT)
210 #if GR_DEBUG
211 #define GR_DEBUGASSERT(COND) GR_ALWAYSASSERT(COND)
212 #else
213 #define GR_DEBUGASSERT(COND)
214 #endif
215#endif
216
217/**
218 * Prettier forms of the above macros.
219 */
220#define GrAssert(COND) GR_DEBUGASSERT(COND)
221#define GrAlwaysAssert(COND) GR_ALWAYSASSERT(COND)
222
223/**
224 * GR_DEBUGCODE compiles the code X in debug builds only
225 */
226#if !defined(GR_DEBUGCODE)
227 #if GR_DEBUG
228 #define GR_DEBUGCODE(X) X
229 #else
230 #define GR_DEBUGCODE(X)
231 #endif
232#endif
233
234/**
235 * GR_STATIC_ASSERT is a compile time assertion. Depending on the platform
236 * it may print the message in the compiler log. Obviously, the condition must
237 * be evaluatable at compile time.
238 */
239// VS 2010 and GCC compiled with c++0x or gnu++0x support the new
240// static_assert.
241#if !defined(GR_STATIC_ASSERT)
242 #if (_MSC_VER >= 1600) || __GXX_EXPERIMENTAL_CXX0X__
243 #define GR_STATIC_ASSERT(CONDITION) static_assert(CONDITION, "bug")
244 #else
245 template <bool> class GR_STATIC_ASSERT_FAILURE;
246 template <> class GR_STATIC_ASSERT_FAILURE<true> {};
247 #define GR_STATIC_ASSERT(CONDITION) \
248 enum {GR_CONCAT(X,__LINE__) = \
249 sizeof(GR_STATIC_ASSERT_FAILURE<CONDITION>)}
250 #endif
251#endif
252
253#if !defined(GR_SCALAR_IS_FLOAT)
254 #define GR_SCALAR_IS_FLOAT 0
255#endif
256#if !defined(GR_SCALAR_IS_FIXED)
257 #define GR_SCALAR_IS_FIXED 0
258#endif
259
260#if !defined(GR_TEXT_SCALAR_TYPE_IS_USHORT)
261 #define GR_TEXT_SCALAR_TYPE_IS_USHORT 0
262#endif
263#if !defined(GR_TEXT_SCALAR_TYPE_IS_FLOAT)
264 #define GR_TEXT_SCALAR_TYPE_IS_FLOAT 0
265#endif
266#if !defined(GR_TEXT_SCALAR_TYPE_IS_FIXED)
267 #define GR_TEXT_SCALAR_TYPE_IS_FIXED 0
268#endif
269
270#ifndef GR_DUMP_TEXTURE_UPLOAD
271 #define GR_DUMP_TEXTURE_UPLOAD 0
272#endif
273
274/**
275 * GR_COLLECT_STATS controls whether the GrGpu class collects stats.
276 * If not already defined then collect in debug build but not release.
277 */
278#if !defined(GR_COLLECT_STATS)
279 #define GR_COLLECT_STATS GR_DEBUG
280#endif
281
282/**
283 * GR_GL_LOG_CALLS controls whether each GL call is logged.
284 */
285#if !defined(GR_GL_LOG_CALLS)
286 #define GR_GL_LOG_CALLS 0
287#endif
288
289///////////////////////////////////////////////////////////////////////////////
290// tail section:
291//
292// Now we just assert if we are missing some required define, or if we detect
293// and inconsistent combination of defines
294//
295
296
297/**
298 * Only one build target macro should be 1 and the rest should be 0.
299 */
300#define GR_BUILD_SUM (GR_WIN32_BUILD + GR_MAC_BUILD + GR_IOS_BUILD + GR_ANDROID_BUILD + GR_LINUX_BUILD + GR_QNX_BUILD)
301#if 0 == GR_BUILD_SUM
302 #error "Missing a GR_BUILD define"
303#elif 1 != GR_BUILD_SUM
304 #error "More than one GR_BUILD defined"
305#endif
306
307
308#if !GR_SCALAR_IS_FLOAT && !GR_SCALAR_IS_FIXED
309 #undef GR_SCALAR_IS_FLOAT
310 #define GR_SCALAR_IS_FLOAT 1
311 #pragma message GR_WARN("Scalar type not defined, defaulting to float")
312#endif
313
314#if !GR_TEXT_SCALAR_IS_FLOAT && \
315 !GR_TEXT_SCALAR_IS_FIXED && \
316 !GR_TEXT_SCALAR_IS_USHORT
317 #undef GR_TEXT_SCALAR_IS_FLOAT
318 #define GR_TEXT_SCALAR_IS_FLOAT 1
319 #pragma message GR_WARN("Text scalar type not defined, defaulting to float")
320#endif
321
322#if 0
323#if GR_WIN32_BUILD
324// #pragma message GR_WARN("GR_WIN32_BUILD")
325#endif
326#if GR_MAC_BUILD
327// #pragma message GR_WARN("GR_MAC_BUILD")
328#endif
329#if GR_IOS_BUILD
330// #pragma message GR_WARN("GR_IOS_BUILD")
331#endif
332#if GR_ANDROID_BUILD
333// #pragma message GR_WARN("GR_ANDROID_BUILD")
334#endif
335#if GR_LINUX_BUILD
336// #pragma message GR_WARN("GR_LINUX_BUILD")
337#endif
338#if GR_QNX_BUILD
339// #pragma message GR_WARN("GR_QNX_BUILD")
340#endif
341#endif
342
343#endif
344