1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/Tools.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef MOZILLA_GFX_TOOLS_H_ 1.10 +#define MOZILLA_GFX_TOOLS_H_ 1.11 + 1.12 +#include "Types.h" 1.13 +#include "Point.h" 1.14 +#include <math.h> 1.15 +#if defined(_MSC_VER) && (_MSC_VER < 1600) 1.16 +#define hypotf _hypotf 1.17 +#endif 1.18 + 1.19 +namespace mozilla { 1.20 +namespace gfx { 1.21 + 1.22 +static inline bool 1.23 +IsOperatorBoundByMask(CompositionOp aOp) { 1.24 + switch (aOp) { 1.25 + case CompositionOp::OP_IN: 1.26 + case CompositionOp::OP_OUT: 1.27 + case CompositionOp::OP_DEST_IN: 1.28 + case CompositionOp::OP_DEST_ATOP: 1.29 + case CompositionOp::OP_SOURCE: 1.30 + return false; 1.31 + default: 1.32 + return true; 1.33 + } 1.34 +} 1.35 + 1.36 +template <class T> 1.37 +struct ClassStorage 1.38 +{ 1.39 + char bytes[sizeof(T)]; 1.40 + 1.41 + const T *addr() const { return (const T *)bytes; } 1.42 + T *addr() { return (T *)(void *)bytes; } 1.43 +}; 1.44 + 1.45 +static inline bool 1.46 +FuzzyEqual(Float aA, Float aB, Float aErr) 1.47 +{ 1.48 + if ((aA + aErr >= aB) && (aA - aErr <= aB)) { 1.49 + return true; 1.50 + } 1.51 + return false; 1.52 +} 1.53 + 1.54 +static inline void 1.55 +NudgeToInteger(float *aVal) 1.56 +{ 1.57 + float r = floorf(*aVal + 0.5f); 1.58 + // The error threshold should be proportional to the rounded value. This 1.59 + // bounds the relative error introduced by the nudge operation. However, 1.60 + // when the rounded value is 0, the error threshold can't be proportional 1.61 + // to the rounded value (we'd never round), so we just choose the same 1.62 + // threshold as for a rounded value of 1. 1.63 + if (FuzzyEqual(r, *aVal, r == 0.0f ? 1e-6f : fabs(r*1e-6f))) { 1.64 + *aVal = r; 1.65 + } 1.66 +} 1.67 + 1.68 +static inline Float 1.69 +Distance(Point aA, Point aB) 1.70 +{ 1.71 + return hypotf(aB.x - aA.x, aB.y - aA.y); 1.72 +} 1.73 + 1.74 +static inline int 1.75 +BytesPerPixel(SurfaceFormat aFormat) 1.76 +{ 1.77 + switch (aFormat) { 1.78 + case SurfaceFormat::A8: 1.79 + return 1; 1.80 + case SurfaceFormat::R5G6B5: 1.81 + return 2; 1.82 + default: 1.83 + return 4; 1.84 + } 1.85 +} 1.86 + 1.87 +template<typename T, int alignment = 16> 1.88 +struct AlignedArray 1.89 +{ 1.90 + AlignedArray() 1.91 + : mStorage(nullptr) 1.92 + , mPtr(nullptr) 1.93 + { 1.94 + } 1.95 + 1.96 + MOZ_ALWAYS_INLINE AlignedArray(size_t aSize) 1.97 + : mStorage(nullptr) 1.98 + { 1.99 + Realloc(aSize); 1.100 + } 1.101 + 1.102 + MOZ_ALWAYS_INLINE ~AlignedArray() 1.103 + { 1.104 + delete [] mStorage; 1.105 + } 1.106 + 1.107 + void Dealloc() 1.108 + { 1.109 + delete [] mStorage; 1.110 + mStorage = mPtr = nullptr; 1.111 + } 1.112 + 1.113 + MOZ_ALWAYS_INLINE void Realloc(size_t aSize) 1.114 + { 1.115 + delete [] mStorage; 1.116 + mStorage = new (std::nothrow) T[aSize + (alignment - 1)]; 1.117 + if (uintptr_t(mStorage) % alignment) { 1.118 + // Our storage does not start at a <alignment>-byte boundary. Make sure mData does! 1.119 + mPtr = (T*)(uintptr_t(mStorage) + 1.120 + (alignment - (uintptr_t(mStorage) % alignment))); 1.121 + } else { 1.122 + mPtr = mStorage; 1.123 + } 1.124 + } 1.125 + 1.126 + MOZ_ALWAYS_INLINE operator T*() 1.127 + { 1.128 + return mPtr; 1.129 + } 1.130 + 1.131 + T *mStorage; 1.132 + T *mPtr; 1.133 +}; 1.134 + 1.135 +template<int alignment> 1.136 +int32_t GetAlignedStride(int32_t aStride) 1.137 +{ 1.138 + if (aStride % alignment) { 1.139 + return aStride + (alignment - (aStride % alignment)); 1.140 + } 1.141 + 1.142 + return aStride; 1.143 +} 1.144 + 1.145 +} 1.146 +} 1.147 + 1.148 +#endif /* MOZILLA_GFX_TOOLS_H_ */