SkPostConfig.h revision 469a9732c5e75c70e73f51a5e4e0657b0129fdc7
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkPostConfig_DEFINED
9#define SkPostConfig_DEFINED
10
11#if defined(SK_BUILD_FOR_WIN32) || defined(SK_BUILD_FOR_WINCE)
12#  define SK_BUILD_FOR_WIN
13#endif
14
15#if defined(SK_DEBUG) && defined(SK_RELEASE)
16#  error "cannot define both SK_DEBUG and SK_RELEASE"
17#elif !defined(SK_DEBUG) && !defined(SK_RELEASE)
18#  error "must define either SK_DEBUG or SK_RELEASE"
19#endif
20
21#if defined(SK_SUPPORT_UNITTEST) && !defined(SK_DEBUG)
22#  error "can't have unittests without debug"
23#endif
24
25#if defined(SK_SCALAR_IS_FIXED) && defined(SK_SCALAR_IS_FLOAT)
26#  error "cannot define both SK_SCALAR_IS_FIXED and SK_SCALAR_IS_FLOAT"
27#elif !defined(SK_SCALAR_IS_FIXED) && !defined(SK_SCALAR_IS_FLOAT)
28#  define SK_SCALAR_IS_FLOAT
29#endif
30
31/**
32 * Matrix calculations may be float or double.
33 * The default is double, as that is faster given our impl uses doubles
34 * for intermediate calculations.
35 */
36#if defined(SK_MSCALAR_IS_DOUBLE) && defined(SK_MSCALAR_IS_FLOAT)
37#  error "cannot define both SK_MSCALAR_IS_DOUBLE and SK_MSCALAR_IS_FLOAT"
38#elif !defined(SK_MSCALAR_IS_DOUBLE) && !defined(SK_MSCALAR_IS_FLOAT)
39#  define SK_MSCALAR_IS_DOUBLE
40#endif
41
42#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN)
43#  error "cannot define both SK_CPU_LENDIAN and SK_CPU_BENDIAN"
44#elif !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN)
45#  error "must define either SK_CPU_LENDIAN or SK_CPU_BENDIAN"
46#endif
47
48/**
49 * Ensure the port has defined all of SK_X32_SHIFT, or none of them.
50 */
51#ifdef SK_A32_SHIFT
52#  if !defined(SK_R32_SHIFT) || !defined(SK_G32_SHIFT) || !defined(SK_B32_SHIFT)
53#    error "all or none of the 32bit SHIFT amounts must be defined"
54#  endif
55#else
56#  if defined(SK_R32_SHIFT) || defined(SK_G32_SHIFT) || defined(SK_B32_SHIFT)
57#    error "all or none of the 32bit SHIFT amounts must be defined"
58#  endif
59#endif
60
61#if !defined(SK_HAS_COMPILER_FEATURE)
62#  if defined(__has_feature)
63#    define SK_HAS_COMPILER_FEATURE(x) __has_feature(x)
64#  else
65#    define SK_HAS_COMPILER_FEATURE(x) 0
66#  endif
67#endif
68
69#if !defined(SK_ATTRIBUTE)
70#  if defined(__clang__) || defined(__GNUC__)
71#    define SK_ATTRIBUTE(attr) __attribute__((attr))
72#  else
73#    define SK_ATTRIBUTE(attr)
74#  endif
75#endif
76
77#if !defined(SK_SUPPORT_GPU)
78#  define SK_SUPPORT_GPU 1
79#endif
80
81/**
82 * The clang static analyzer likes to know that when the program is not
83 * expected to continue (crash, assertion failure, etc). It will notice that
84 * some combination of parameters lead to a function call that does not return.
85 * It can then make appropriate assumptions about the parameters in code
86 * executed only if the non-returning function was *not* called.
87 */
88#if !defined(SkNO_RETURN_HINT)
89#  if SK_HAS_COMPILER_FEATURE(attribute_analyzer_noreturn)
90     static inline void SkNO_RETURN_HINT() __attribute__((analyzer_noreturn));
91     static inline void SkNO_RETURN_HINT() {}
92#  else
93#    define SkNO_RETURN_HINT() do {} while (false)
94#  endif
95#endif
96
97#if defined(SK_ZLIB_INCLUDE) && defined(SK_SYSTEM_ZLIB)
98#  error "cannot define both SK_ZLIB_INCLUDE and SK_SYSTEM_ZLIB"
99#elif defined(SK_ZLIB_INCLUDE) || defined(SK_SYSTEM_ZLIB)
100#  define SK_HAS_ZLIB
101#endif
102
103///////////////////////////////////////////////////////////////////////////////
104
105#ifndef SkNEW
106#  define SkNEW(type_name)                           (new type_name)
107#  define SkNEW_ARGS(type_name, args)                (new type_name args)
108#  define SkNEW_ARRAY(type_name, count)              (new type_name[(count)])
109#  define SkNEW_PLACEMENT(buf, type_name)            (new (buf) type_name)
110#  define SkNEW_PLACEMENT_ARGS(buf, type_name, args) (new (buf) type_name args)
111#  define SkDELETE(obj)                              (delete (obj))
112#  define SkDELETE_ARRAY(array)                      (delete[] (array))
113#endif
114
115#ifndef SK_CRASH
116#  if 1   // set to 0 for infinite loop, which can help connecting gdb
117#    define SK_CRASH() do { SkNO_RETURN_HINT(); *(int *)(uintptr_t)0xbbadbeef = 0; } while (false)
118#  else
119#    define SK_CRASH() do { SkNO_RETURN_HINT(); } while (true)
120#  endif
121#endif
122
123///////////////////////////////////////////////////////////////////////////////
124
125/**
126 * SK_ENABLE_INST_COUNT controlls printing how many reference counted objects
127 * are still held on exit.
128 * Defaults to 1 in DEBUG and 0 in RELEASE.
129 */
130#ifndef SK_ENABLE_INST_COUNT
131#  ifdef SK_DEBUG
132#    define SK_ENABLE_INST_COUNT 1
133#  else
134#    define SK_ENABLE_INST_COUNT 0
135#  endif
136#endif
137
138///////////////////////////////////////////////////////////////////////////////
139
140#if defined(SK_SOFTWARE_FLOAT) && defined(SK_SCALAR_IS_FLOAT)
141   // if this is defined, we convert floats to 2s compliment ints for compares.
142#  ifndef SK_SCALAR_SLOW_COMPARES
143#    define SK_SCALAR_SLOW_COMPARES
144#  endif
145#  ifndef SK_USE_FLOATBITS
146#    define SK_USE_FLOATBITS
147#  endif
148#endif
149
150#ifdef SK_BUILD_FOR_WIN
151#  ifndef WIN32_LEAN_AND_MEAN
152#    define WIN32_LEAN_AND_MEAN
153#    define WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
154#  endif
155#  ifndef NOMINMAX
156#    define NOMINMAX
157#    define NOMINMAX_WAS_LOCALLY_DEFINED
158#  endif
159#
160#  include <windows.h>
161#
162#  ifdef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
163#    undef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
164#    undef WIN32_LEAN_AND_MEAN
165#  endif
166#  ifdef NOMINMAX_WAS_LOCALLY_DEFINED
167#    undef NOMINMAX_WAS_LOCALLY_DEFINED
168#    undef NOMINMAX
169#  endif
170#
171#  ifndef SK_DEBUGBREAK
172#    define SK_DEBUGBREAK(p) do { if (!(p)) { SkNO_RETURN_HINT(); __debugbreak(); }} while (false)
173#  endif
174#
175#  ifndef SK_A32_SHIFT
176#    define SK_A32_SHIFT 24
177#    define SK_R32_SHIFT 16
178#    define SK_G32_SHIFT 8
179#    define SK_B32_SHIFT 0
180#  endif
181#
182#else
183#  ifdef SK_DEBUG
184#    include <stdio.h>
185#    ifndef SK_DEBUGBREAK
186#      define SK_DEBUGBREAK(cond) do { if (cond) break; \
187                SkDebugf("%s:%d: failed assertion \"%s\"\n", \
188                __FILE__, __LINE__, #cond); SK_CRASH(); } while (false)
189#    endif
190#  endif
191#endif
192
193/**
194 *  We check to see if the SHIFT value has already been defined.
195 *  if not, we define it ourself to some default values. We default to OpenGL
196 *  order (in memory: r,g,b,a)
197 */
198#ifndef SK_A32_SHIFT
199#  ifdef SK_CPU_BENDIAN
200#    define SK_R32_SHIFT    24
201#    define SK_G32_SHIFT    16
202#    define SK_B32_SHIFT    8
203#    define SK_A32_SHIFT    0
204#  else
205#    define SK_R32_SHIFT    0
206#    define SK_G32_SHIFT    8
207#    define SK_B32_SHIFT    16
208#    define SK_A32_SHIFT    24
209#  endif
210#endif
211
212/**
213 * SkColor has well defined shift values, but SkPMColor is configurable. This
214 * macro is a convenience that returns true if the shift values are equal while
215 * ignoring the machine's endianness.
216 */
217#define SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER \
218    (SK_A32_SHIFT == 24 && SK_R32_SHIFT == 16 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 0)
219
220/**
221 * SK_PMCOLOR_BYTE_ORDER can be used to query the byte order of SkPMColor at compile time. The
222 * relationship between the byte order and shift values depends on machine endianness. If the shift
223 * order is R=0, G=8, B=16, A=24 then ((char*)&pmcolor)[0] will produce the R channel on a little
224 * endian machine and the A channel on a big endian machine. Thus, given those shifts values,
225 * SK_PMCOLOR_BYTE_ORDER(R,G,B,A) will be true on a little endian machine and
226 * SK_PMCOLOR_BYTE_ORDER(A,B,G,R) will be true on a big endian machine.
227 */
228#ifdef SK_CPU_BENDIAN
229#  define SK_PMCOLOR_BYTE_ORDER(C0, C1, C2, C3)     \
230        (SK_ ## C3 ## 32_SHIFT == 0  &&             \
231         SK_ ## C2 ## 32_SHIFT == 8  &&             \
232         SK_ ## C1 ## 32_SHIFT == 16 &&             \
233         SK_ ## C0 ## 32_SHIFT == 24)
234#else
235#  define SK_PMCOLOR_BYTE_ORDER(C0, C1, C2, C3)     \
236        (SK_ ## C0 ## 32_SHIFT == 0  &&             \
237         SK_ ## C1 ## 32_SHIFT == 8  &&             \
238         SK_ ## C2 ## 32_SHIFT == 16 &&             \
239         SK_ ## C3 ## 32_SHIFT == 24)
240#endif
241
242//////////////////////////////////////////////////////////////////////
243
244#if defined(SK_BUILD_FOR_WIN32) || defined(SK_BUILD_FOR_MAC)
245#  ifndef SkLONGLONG
246#    ifdef SK_BUILD_FOR_WIN32
247#      define SkLONGLONG __int64
248#    else
249#      define SkLONGLONG long long
250#    endif
251#  endif
252#endif
253
254//////////////////////////////////////////////////////////////////////////////////////////////
255#ifndef SK_BUILD_FOR_WINCE
256#  include <string.h>
257#  include <stdlib.h>
258#else
259#  define _CMNINTRIN_DECLARE_ONLY
260#  include "cmnintrin.h"
261#endif
262
263#if defined SK_DEBUG && defined SK_BUILD_FOR_WIN32
264#  ifdef free
265#    undef free
266#  endif
267#  include <crtdbg.h>
268#  undef free
269#
270#  ifdef SK_DEBUGx
271#    if defined(SK_SIMULATE_FAILED_MALLOC) && defined(__cplusplus)
272       void * operator new(
273           size_t cb,
274           int nBlockUse,
275           const char * szFileName,
276           int nLine,
277           int foo
278           );
279       void * operator new[](
280           size_t cb,
281           int nBlockUse,
282           const char * szFileName,
283           int nLine,
284           int foo
285           );
286       void operator delete(
287           void *pUserData,
288           int, const char*, int, int
289           );
290       void operator delete(
291           void *pUserData
292           );
293       void operator delete[]( void * p );
294#      define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__, 0)
295#    else
296#      define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
297#    endif
298#    define new DEBUG_CLIENTBLOCK
299#  else
300#    define DEBUG_CLIENTBLOCK
301#  endif
302#endif
303
304//////////////////////////////////////////////////////////////////////
305
306#ifndef SK_OVERRIDE
307#  if defined(_MSC_VER)
308#    define SK_OVERRIDE override
309#  elif defined(__clang__)
310     // Using __attribute__((override)) on clang does not appear to always work.
311     // Clang defaults to C++03 and warns about using override. Squelch that. Intentionally no
312     // push/pop here so all users of SK_OVERRIDE ignore the warning too. This is like passing
313     // -Wno-c++11-extensions, except that GCC won't die (because it won't see this pragma).
314#    pragma clang diagnostic ignored "-Wc++11-extensions"
315#
316#    if __has_feature(cxx_override_control)
317#      define SK_OVERRIDE override
318#    elif defined(__has_extension) && __has_extension(cxx_override_control)
319#      define SK_OVERRIDE override
320#    endif
321#  endif
322#  ifndef SK_OVERRIDE
323#    define SK_OVERRIDE
324#  endif
325#endif
326
327//////////////////////////////////////////////////////////////////////
328
329#if !defined(SK_UNUSED)
330#  define SK_UNUSED SK_ATTRIBUTE(unused)
331#endif
332
333#if !defined(SK_ATTR_DEPRECATED)
334   // FIXME: we ignore msg for now...
335#  define SK_ATTR_DEPRECATED(msg) SK_ATTRIBUTE(deprecated)
336#endif
337
338/**
339 * If your judgment is better than the compiler's (i.e. you've profiled it),
340 * you can use SK_ALWAYS_INLINE to force inlining. E.g.
341 *     inline void someMethod() { ... }             // may not be inlined
342 *     SK_ALWAYS_INLINE void someMethod() { ... }   // should always be inlined
343 */
344#if !defined(SK_ALWAYS_INLINE)
345#  if defined(SK_BUILD_FOR_WIN)
346#    define SK_ALWAYS_INLINE __forceinline
347#  else
348#    define SK_ALWAYS_INLINE SK_ATTRIBUTE(always_inline) inline
349#  endif
350#endif
351
352//////////////////////////////////////////////////////////////////////
353
354#if defined(__clang__) || defined(__GNUC__)
355#  define SK_PREFETCH(ptr) __builtin_prefetch(ptr)
356#  define SK_WRITE_PREFETCH(ptr) __builtin_prefetch(ptr, 1)
357#else
358#  define SK_PREFETCH(ptr)
359#  define SK_WRITE_PREFETCH(ptr)
360#endif
361
362//////////////////////////////////////////////////////////////////////
363
364#ifndef SK_PRINTF_LIKE
365#  if defined(__clang__) || defined(__GNUC__)
366#    define SK_PRINTF_LIKE(A, B) __attribute__((format(printf, (A), (B))))
367#  else
368#    define SK_PRINTF_LIKE(A, B)
369#  endif
370#endif
371
372//////////////////////////////////////////////////////////////////////
373
374#ifndef SK_SIZE_T_SPECIFIER
375#  if defined(_MSC_VER)
376#    define SK_SIZE_T_SPECIFIER "%Iu"
377#  else
378#    define SK_SIZE_T_SPECIFIER "%zu"
379#  endif
380#endif
381
382//////////////////////////////////////////////////////////////////////
383
384#ifndef SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
385#  define SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 1
386#endif
387
388#endif // SkPostConfig_DEFINED
389