blob: 5a0807700274181a02d386398256e0e3d1a79764 [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"
reed@google.comc9218432011-01-25 19:05:12 +000071 #elif (defined(ANDROID_NDK) && ANDROID_NDK) || defined(ANDROID)
reed@google.comac10a2d2010-12-22 21:39:39 +000072 #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"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +000088#endif
89
reed@google.comc9218432011-01-25 19:05:12 +000090// we need both GR_DEBUG and GR_RELEASE to be defined as 0 or 1
91//
92#ifndef GR_DEBUG
93 #ifdef GR_RELEASE
94 #define GR_DEBUG !GR_RELEASE
reed@google.comac10a2d2010-12-22 21:39:39 +000095 #else
reed@google.comc9218432011-01-25 19:05:12 +000096 #ifdef NDEBUG
97 #define GR_DEBUG 0
98 #else
99 #define GR_DEBUG 1
100 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 #endif
reed@google.comc9218432011-01-25 19:05:12 +0000102#endif
103
104#ifndef GR_RELEASE
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 #define GR_RELEASE !GR_DEBUG
106#endif
107
reed@google.comc9218432011-01-25 19:05:12 +0000108#if GR_DEBUG == GR_RELEASE
reed@google.com8752ad72011-01-26 04:55:57 +0000109 #error "GR_DEBUG and GR_RELEASE must not be the same"
reed@google.comc9218432011-01-25 19:05:12 +0000110#endif
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113///////////////////////////////////////////////////////////////////////////////
114
115/*
bsalomon@google.com91826102011-03-21 19:51:57 +0000116 * Include stdint.h with defines that trigger declaration of C99 limit/const
117 * macros here before anyone else has a chance to include stdint.h without
118 * these.
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 */
120#define __STDC_LIMIT_MACROS
121#define __STDC_CONSTANT_MACROS
122#include <stdint.h>
123
124/*
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 * The "user config" file can be empty, and everything should work. It is
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 * meant to store a given platform/client's overrides of our guess-work.
127 *
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000128 * A alternate user config file can be specified by defining
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 * GR_USER_CONFIG_FILE. It should be defined relative to GrConfig.h
130 *
131 * e.g. it can specify GR_DEBUG/GR_RELEASE as it please, change the BUILD
132 * target, or supply its own defines for anything else (e.g. GR_SCALAR)
133 */
134#if !defined(GR_USER_CONFIG_FILE)
135 #include "GrUserConfig.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000136#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 #include GR_USER_CONFIG_FILE
138#endif
139
140
141///////////////////////////////////////////////////////////////////////////////
142///////////////////////////////////////////////////////////////////////////////
143// postconfig section:
144//
bsalomon@google.com91826102011-03-21 19:51:57 +0000145
146// GR_IMPLEMENTATION should be define to 1 when building Gr and 0 when including
147// it in another dependent build. The Gr makefile/ide-project should define this
148// to 1.
149#if !defined(GR_IMPLEMENTATION)
150 #define GR_IMPLEMENTATION 0
151#endif
152
153// If Gr is built as a shared library then GR_DLL should be defined to 1 (both
154// when building Gr and when including its headers in dependent builds). Only
155// currently supported minimally for Chrome's Win32 Multi-DLL build (TODO:
156// correctly exort all of the public API correctly and support shared lib on
157// other platforms).
158#if !defined(GR_DLL)
159 #define GR_DLL 0
160#endif
161
162#if GR_WIN32_BUILD && GR_DLL
163 #if GR_IMPLEMENTATION
164 #define GR_API __declspec(dllexport)
165 #else
166 #define GR_API __declspec(dllimport)
167 #endif
168#else
169 #define GR_API
170#endif
171
reed@google.comac10a2d2010-12-22 21:39:39 +0000172// By now we must have a GR_..._BUILD symbol set to 1, and a decision about
173// debug -vs- release
174//
175
176extern void GrPrintf(const char format[], ...);
177
178/**
179 * GR_STRING makes a string of X where X is expanded before conversion to a string
180 * if X itself contains macros.
181 */
182#define GR_STRING(X) GR_STRING_IMPL(X)
183#define GR_STRING_IMPL(X) #X
184
185/**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000186 * GR_CONCAT concatenates X and Y where each is expanded before
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 * contanenation if either contains macros.
188 */
189#define GR_CONCAT(X,Y) GR_CONCAT_IMPL(X,Y)
190#define GR_CONCAT_IMPL(X,Y) X##Y
191
192/**
193 * Creates a string of the form "<filename>(<linenumber>) : "
194 */
195#define GR_FILE_AND_LINE_STR __FILE__ "(" GR_STRING(__LINE__) ") : "
196
197/**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000198 * Compilers have different ways of issuing warnings. This macro
199 * attempts to abstract them, but may need to be specialized for your
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 * particular compiler.
201 * To insert compiler warnings use "#pragma message GR_WARN(<string>)"
202 */
reed@google.comc9218432011-01-25 19:05:12 +0000203#if defined(_MSC_VER) && _MSC_VER
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 #define GR_WARN(MSG) (GR_FILE_AND_LINE_STR "WARNING: " MSG)
205#else//__GNUC__ - may need other defines for different compilers
206 #define GR_WARN(MSG) ("WARNING: " MSG)
207#endif
208
209/**
210 * GR_ALWAYSBREAK is an unconditional break in all builds.
211 */
212#if !defined(GR_ALWAYSBREAK)
213 #if GR_WIN32_BUILD
214 #define GR_ALWAYSBREAK __debugbreak()
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000215 #else
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 // TODO: do other platforms really not have continuable breakpoints?
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000217 // sign extend for 64bit architectures to be sure this is
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 // in the high address range
219 #define GR_ALWAYSBREAK *((int*)(int64_t)(int32_t)0xbeefcafe) = 0;
220 #endif
221#endif
222
223/**
224 * GR_DEBUGBREAK is an unconditional break in debug builds.
225 */
226#if !defined(GR_DEBUGBREAK)
227 #if GR_DEBUG
228 #define GR_DEBUGBREAK GR_ALWAYSBREAK
229 #else
230 #define GR_DEBUGBREAK
231 #endif
232#endif
233
234/**
235 * GR_ALWAYSASSERT is an assertion in all builds.
236 */
237#if !defined(GR_ALWAYSASSERT)
238 #define GR_ALWAYSASSERT(COND) \
239 do { \
240 if (!(COND)) { \
241 GrPrintf("%s %s failed\n", GR_FILE_AND_LINE_STR, #COND); \
242 GR_ALWAYSBREAK; \
243 } \
244 } while (false)
245#endif
246
247/**
248 * GR_DEBUGASSERT is an assertion in debug builds only.
249 */
250#if !defined(GR_DEBUGASSERT)
251 #if GR_DEBUG
252 #define GR_DEBUGASSERT(COND) GR_ALWAYSASSERT(COND)
253 #else
254 #define GR_DEBUGASSERT(COND)
255 #endif
256#endif
257
258/**
259 * Prettier forms of the above macros.
260 */
261#define GrAssert(COND) GR_DEBUGASSERT(COND)
262#define GrAlwaysAssert(COND) GR_ALWAYSASSERT(COND)
263
264/**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000265 * Crash from unrecoverable condition, optionally with a message.
266 */
267inline void GrCrash() { GrAlwaysAssert(false); }
268inline void GrCrash(const char* msg) { GrPrintf(msg); GrAlwaysAssert(false); }
269
270/**
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000271 * GR_DEBUGCODE compiles the code X in debug builds only
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 */
273#if !defined(GR_DEBUGCODE)
274 #if GR_DEBUG
275 #define GR_DEBUGCODE(X) X
276 #else
277 #define GR_DEBUGCODE(X)
278 #endif
279#endif
280
281/**
282 * GR_STATIC_ASSERT is a compile time assertion. Depending on the platform
283 * it may print the message in the compiler log. Obviously, the condition must
284 * be evaluatable at compile time.
285 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000286// VS 2010 and GCC compiled with c++0x or gnu++0x support the new
reed@google.comac10a2d2010-12-22 21:39:39 +0000287// static_assert.
288#if !defined(GR_STATIC_ASSERT)
reed@google.comc9218432011-01-25 19:05:12 +0000289 #if (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GXX_EXPERIMENTAL_CXX0X__)
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 #define GR_STATIC_ASSERT(CONDITION) static_assert(CONDITION, "bug")
291 #else
292 template <bool> class GR_STATIC_ASSERT_FAILURE;
293 template <> class GR_STATIC_ASSERT_FAILURE<true> {};
294 #define GR_STATIC_ASSERT(CONDITION) \
295 enum {GR_CONCAT(X,__LINE__) = \
296 sizeof(GR_STATIC_ASSERT_FAILURE<CONDITION>)}
297 #endif
298#endif
299
300#if !defined(GR_SCALAR_IS_FLOAT)
301 #define GR_SCALAR_IS_FLOAT 0
302#endif
303#if !defined(GR_SCALAR_IS_FIXED)
304 #define GR_SCALAR_IS_FIXED 0
305#endif
306
307#if !defined(GR_TEXT_SCALAR_TYPE_IS_USHORT)
308 #define GR_TEXT_SCALAR_TYPE_IS_USHORT 0
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000309#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000310#if !defined(GR_TEXT_SCALAR_TYPE_IS_FLOAT)
311 #define GR_TEXT_SCALAR_TYPE_IS_FLOAT 0
312#endif
313#if !defined(GR_TEXT_SCALAR_TYPE_IS_FIXED)
314 #define GR_TEXT_SCALAR_TYPE_IS_FIXED 0
315#endif
316
317#ifndef GR_DUMP_TEXTURE_UPLOAD
318 #define GR_DUMP_TEXTURE_UPLOAD 0
319#endif
320
321/**
322 * GR_COLLECT_STATS controls whether the GrGpu class collects stats.
323 * If not already defined then collect in debug build but not release.
324 */
325#if !defined(GR_COLLECT_STATS)
326 #define GR_COLLECT_STATS GR_DEBUG
327#endif
328
329/**
bsalomon@google.comd16983b2011-02-02 22:42:20 +0000330 * GR_STATIC_RECT_VB controls whether rects are drawn by issuing a vertex
331 * for each corner or using a static vb that is positioned by modifying the
332 * view / texture matrix.
333 */
bsalomon@google.com43333232011-02-02 19:24:54 +0000334#if !defined(GR_STATIC_RECT_VB)
335 #define GR_STATIC_RECT_VB 0
336#endif
337
bsalomon@google.comd16983b2011-02-02 22:42:20 +0000338/**
339 * GR_AGGRESSIVE_SHADER_OPTS controls how aggressively shaders are optimized
340 * for special cases. On systems where program changes are expensive this
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 * may not be advantageous. Consecutive draws may no longer use the same
bsalomon@google.comd16983b2011-02-02 22:42:20 +0000342 * program.
343 */
344#if !defined(GR_AGGRESSIVE_SHADER_OPTS)
345 #define GR_AGGRESSIVE_SHADER_OPTS 0
346#endif
347
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000348/**
349 * GR_GEOM_BUFFER_LOCK_THRESHOLD gives a threshold (in bytes) for when Gr should
350 * lock a GrGeometryBuffer to update its contents. It will use Lock() if the
351 * size of the udpated region is greater than the threshold. Otherwise it will
352 * use updateData() or updateSubData().
353 */
354#if !defined(GR_GEOM_BUFFER_LOCK_THRESHOLD)
355 #define GR_GEOM_BUFFER_LOCK_THRESHOLD (1 << 15)
356#endif
357
reed@google.comac10a2d2010-12-22 21:39:39 +0000358///////////////////////////////////////////////////////////////////////////////
359// tail section:
360//
361// Now we just assert if we are missing some required define, or if we detect
362// and inconsistent combination of defines
363//
364
365
366/**
367 * Only one build target macro should be 1 and the rest should be 0.
368 */
369#define GR_BUILD_SUM (GR_WIN32_BUILD + GR_MAC_BUILD + GR_IOS_BUILD + GR_ANDROID_BUILD + GR_LINUX_BUILD + GR_QNX_BUILD)
370#if 0 == GR_BUILD_SUM
371 #error "Missing a GR_BUILD define"
372#elif 1 != GR_BUILD_SUM
373 #error "More than one GR_BUILD defined"
374#endif
375
376
377#if !GR_SCALAR_IS_FLOAT && !GR_SCALAR_IS_FIXED
378 #undef GR_SCALAR_IS_FLOAT
379 #define GR_SCALAR_IS_FLOAT 1
380 #pragma message GR_WARN("Scalar type not defined, defaulting to float")
381#endif
382
383#if !GR_TEXT_SCALAR_IS_FLOAT && \
384 !GR_TEXT_SCALAR_IS_FIXED && \
385 !GR_TEXT_SCALAR_IS_USHORT
386 #undef GR_TEXT_SCALAR_IS_FLOAT
387 #define GR_TEXT_SCALAR_IS_FLOAT 1
388 #pragma message GR_WARN("Text scalar type not defined, defaulting to float")
389#endif
390
391#if 0
392#if GR_WIN32_BUILD
393// #pragma message GR_WARN("GR_WIN32_BUILD")
394#endif
395#if GR_MAC_BUILD
396// #pragma message GR_WARN("GR_MAC_BUILD")
397#endif
398#if GR_IOS_BUILD
399// #pragma message GR_WARN("GR_IOS_BUILD")
400#endif
401#if GR_ANDROID_BUILD
402// #pragma message GR_WARN("GR_ANDROID_BUILD")
403#endif
404#if GR_LINUX_BUILD
405// #pragma message GR_WARN("GR_LINUX_BUILD")
406#endif
407#if GR_QNX_BUILD
408// #pragma message GR_WARN("GR_QNX_BUILD")
409#endif
410#endif
411
412#endif
413