Bug 1073336 part 14a - Update animation generation when changing animations via the...
[gecko.git] / gfx / 2d / Tools.h
blobd7405c75bce69431ad1b5108b0e355f8df983ce3
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_GFX_TOOLS_H_
7 #define MOZILLA_GFX_TOOLS_H_
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/Move.h"
11 #include "mozilla/TypeTraits.h"
12 #include "Types.h"
13 #include "Point.h"
14 #include <math.h>
15 #if defined(_MSC_VER) && (_MSC_VER < 1600)
16 #define hypotf _hypotf
17 #endif
19 namespace mozilla {
20 namespace gfx {
22 static inline bool
23 IsOperatorBoundByMask(CompositionOp aOp) {
24 switch (aOp) {
25 case CompositionOp::OP_IN:
26 case CompositionOp::OP_OUT:
27 case CompositionOp::OP_DEST_IN:
28 case CompositionOp::OP_DEST_ATOP:
29 case CompositionOp::OP_SOURCE:
30 return false;
31 default:
32 return true;
36 template <class T>
37 struct ClassStorage
39 char bytes[sizeof(T)];
41 const T *addr() const { return (const T *)bytes; }
42 T *addr() { return (T *)(void *)bytes; }
45 static inline bool
46 FuzzyEqual(Float aA, Float aB, Float aErr)
48 if ((aA + aErr >= aB) && (aA - aErr <= aB)) {
49 return true;
51 return false;
54 static inline void
55 NudgeToInteger(float *aVal)
57 float r = floorf(*aVal + 0.5f);
58 // The error threshold should be proportional to the rounded value. This
59 // bounds the relative error introduced by the nudge operation. However,
60 // when the rounded value is 0, the error threshold can't be proportional
61 // to the rounded value (we'd never round), so we just choose the same
62 // threshold as for a rounded value of 1.
63 if (FuzzyEqual(r, *aVal, r == 0.0f ? 1e-6f : fabs(r*1e-6f))) {
64 *aVal = r;
68 static inline void
69 NudgeToInteger(float *aVal, float aErr)
71 float r = floorf(*aVal + 0.5f);
72 if (FuzzyEqual(r, *aVal, aErr)) {
73 *aVal = r;
77 static inline Float
78 Distance(Point aA, Point aB)
80 return hypotf(aB.x - aA.x, aB.y - aA.y);
83 static inline int
84 BytesPerPixel(SurfaceFormat aFormat)
86 switch (aFormat) {
87 case SurfaceFormat::A8:
88 return 1;
89 case SurfaceFormat::R5G6B5:
90 return 2;
91 default:
92 return 4;
96 template<typename T, int alignment = 16>
97 struct AlignedArray
99 typedef T value_type;
101 AlignedArray()
102 : mPtr(nullptr)
103 , mStorage(nullptr)
107 explicit MOZ_ALWAYS_INLINE AlignedArray(size_t aCount, bool aZero = false)
108 : mStorage(nullptr)
109 , mCount(0)
111 Realloc(aCount, aZero);
114 MOZ_ALWAYS_INLINE ~AlignedArray()
116 Dealloc();
119 void Dealloc()
121 // If we fail this assert we'll need to uncomment the loop below to make
122 // sure dtors are properly invoked. If we do that, we should check that the
123 // comment about compiler dead code elimination is in fact true for all the
124 // compilers that we care about.
125 static_assert(mozilla::IsPod<T>::value,
126 "Destructors must be invoked for this type");
127 #if 0
128 for (size_t i = 0; i < mCount; ++i) {
129 // Since we used the placement |operator new| function to construct the
130 // elements of this array we need to invoke their destructors manually.
131 // For types where the destructor does nothing the compiler's dead code
132 // elimination step should optimize this loop away.
133 mPtr[i].~T();
135 #endif
137 moz_free(mStorage);
138 mStorage = nullptr;
139 mPtr = nullptr;
142 MOZ_ALWAYS_INLINE void Realloc(size_t aCount, bool aZero = false)
144 moz_free(mStorage);
145 CheckedInt32 storageByteCount =
146 CheckedInt32(sizeof(T)) * aCount + (alignment - 1);
147 if (!storageByteCount.isValid()) {
148 mStorage = nullptr;
149 mPtr = nullptr;
150 mCount = 0;
151 return;
153 // We don't create an array of T here, since we don't want ctors to be
154 // invoked at the wrong places if we realign below.
155 if (aZero) {
156 // calloc can be more efficient than new[] for large chunks,
157 // so we use calloc/malloc/free for everything.
158 mStorage = static_cast<uint8_t *>(moz_calloc(1, storageByteCount.value()));
159 } else {
160 mStorage = static_cast<uint8_t *>(moz_malloc(storageByteCount.value()));
162 if (!mStorage) {
163 mStorage = nullptr;
164 mPtr = nullptr;
165 mCount = 0;
166 return;
168 if (uintptr_t(mStorage) % alignment) {
169 // Our storage does not start at a <alignment>-byte boundary. Make sure mPtr does!
170 mPtr = (T*)(uintptr_t(mStorage) + alignment - (uintptr_t(mStorage) % alignment));
171 } else {
172 mPtr = (T*)(mStorage);
174 // Now that mPtr is pointing to the aligned position we can use placement
175 // |operator new| to invoke any ctors at the correct positions. For types
176 // that have a no-op default constructor the compiler's dead code
177 // elimination step should optimize this away.
178 mPtr = new (mPtr) T[aCount];
179 mCount = aCount;
182 void Swap(AlignedArray<T, alignment>& aOther)
184 mozilla::Swap(mPtr, aOther.mPtr);
185 mozilla::Swap(mStorage, aOther.mStorage);
186 mozilla::Swap(mCount, aOther.mCount);
189 MOZ_ALWAYS_INLINE operator T*()
191 return mPtr;
194 T *mPtr;
196 private:
197 uint8_t *mStorage;
198 size_t mCount;
202 * Returns aStride increased, if necessary, so that it divides exactly into
203 * |alignment|.
205 * Note that currently |alignment| must be a power-of-2. If for some reason we
206 * want to support NPOT alignment we can revert back to this functions old
207 * implementation.
209 template<int alignment>
210 int32_t GetAlignedStride(int32_t aStride)
212 static_assert(alignment > 0 && (alignment & (alignment-1)) == 0,
213 "This implementation currently require power-of-two alignment");
214 const int32_t mask = alignment - 1;
215 return (aStride + mask) & ~mask;
221 #endif /* MOZILLA_GFX_TOOLS_H_ */