Bug 1641886 [wpt PR 23851] - Support interpolating contain-intrinsic-size, a=testonly
[gecko.git] / gfx / 2d / Tools.h
blob64dbcb9b314d48815028148b35b6b463ac145f1f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_TOOLS_H_
8 #define MOZILLA_GFX_TOOLS_H_
10 #include <math.h>
12 #include <utility>
14 #include "Point.h"
15 #include "Types.h"
16 #include "mozilla/CheckedInt.h"
17 #include "mozilla/MemoryReporting.h" // for MallocSizeOf
18 #include "mozilla/TypeTraits.h"
20 namespace mozilla {
21 namespace gfx {
23 static inline bool 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 {
38 char bytes[sizeof(T)];
40 const T* addr() const { return (const T*)bytes; }
41 T* addr() { return (T*)(void*)bytes; }
44 static inline bool FuzzyEqual(Float aA, Float aB, Float aErr) {
45 if ((aA + aErr >= aB) && (aA - aErr <= aB)) {
46 return true;
48 return false;
51 static inline void NudgeToInteger(float* aVal) {
52 float r = floorf(*aVal + 0.5f);
53 // The error threshold should be proportional to the rounded value. This
54 // bounds the relative error introduced by the nudge operation. However,
55 // when the rounded value is 0, the error threshold can't be proportional
56 // to the rounded value (we'd never round), so we just choose the same
57 // threshold as for a rounded value of 1.
58 if (FuzzyEqual(r, *aVal, r == 0.0f ? 1e-6f : fabs(r * 1e-6f))) {
59 *aVal = r;
63 static inline void NudgeToInteger(float* aVal, float aErr) {
64 float r = floorf(*aVal + 0.5f);
65 if (FuzzyEqual(r, *aVal, aErr)) {
66 *aVal = r;
70 static inline void NudgeToInteger(double* aVal) {
71 float f = float(*aVal);
72 NudgeToInteger(&f);
73 *aVal = f;
76 static inline Float Distance(Point aA, Point aB) {
77 return hypotf(aB.x - aA.x, aB.y - aA.y);
80 template <typename T, int alignment = 16>
81 struct AlignedArray final {
82 typedef T value_type;
84 AlignedArray() : mPtr(nullptr), mStorage(nullptr), mCount(0) {}
86 explicit MOZ_ALWAYS_INLINE AlignedArray(size_t aCount, bool aZero = false)
87 : mPtr(nullptr), mStorage(nullptr), mCount(0) {
88 Realloc(aCount, aZero);
91 MOZ_ALWAYS_INLINE ~AlignedArray() { Dealloc(); }
93 void Dealloc() {
94 // If we fail this assert we'll need to uncomment the loop below to make
95 // sure dtors are properly invoked. If we do that, we should check that the
96 // comment about compiler dead code elimination is in fact true for all the
97 // compilers that we care about.
98 static_assert(mozilla::IsPod<T>::value,
99 "Destructors must be invoked for this type");
100 #if 0
101 for (size_t i = 0; i < mCount; ++i) {
102 // Since we used the placement |operator new| function to construct the
103 // elements of this array we need to invoke their destructors manually.
104 // For types where the destructor does nothing the compiler's dead code
105 // elimination step should optimize this loop away.
106 mPtr[i].~T();
108 #endif
110 free(mStorage);
111 mStorage = nullptr;
112 mPtr = nullptr;
115 MOZ_ALWAYS_INLINE void Realloc(size_t aCount, bool aZero = false) {
116 free(mStorage);
117 CheckedInt32 storageByteCount =
118 CheckedInt32(sizeof(T)) * aCount + (alignment - 1);
119 if (!storageByteCount.isValid()) {
120 mStorage = nullptr;
121 mPtr = nullptr;
122 mCount = 0;
123 return;
125 // We don't create an array of T here, since we don't want ctors to be
126 // invoked at the wrong places if we realign below.
127 if (aZero) {
128 // calloc can be more efficient than new[] for large chunks,
129 // so we use calloc/malloc/free for everything.
130 mStorage = static_cast<uint8_t*>(calloc(1u, storageByteCount.value()));
131 } else {
132 mStorage = static_cast<uint8_t*>(malloc(storageByteCount.value()));
134 if (!mStorage) {
135 mStorage = nullptr;
136 mPtr = nullptr;
137 mCount = 0;
138 return;
140 if (uintptr_t(mStorage) % alignment) {
141 // Our storage does not start at a <alignment>-byte boundary. Make sure
142 // mPtr does!
143 mPtr = (T*)(uintptr_t(mStorage) + alignment -
144 (uintptr_t(mStorage) % alignment));
145 } else {
146 mPtr = (T*)(mStorage);
148 // Now that mPtr is pointing to the aligned position we can use placement
149 // |operator new| to invoke any ctors at the correct positions. For types
150 // that have a no-op default constructor the compiler's dead code
151 // elimination step should optimize this away.
152 mPtr = new (mPtr) T[aCount];
153 mCount = aCount;
156 void Swap(AlignedArray<T, alignment>& aOther) {
157 std::swap(mPtr, aOther.mPtr);
158 std::swap(mStorage, aOther.mStorage);
159 std::swap(mCount, aOther.mCount);
162 size_t HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
163 return aMallocSizeOf(mStorage);
166 MOZ_ALWAYS_INLINE operator T*() { return mPtr; }
168 T* mPtr;
170 private:
171 uint8_t* mStorage;
172 size_t mCount;
176 * Returns aWidth * aBytesPerPixel increased, if necessary, so that it divides
177 * exactly into |alignment|.
179 * Note that currently |alignment| must be a power-of-2. If for some reason we
180 * want to support NPOT alignment we can revert back to this functions old
181 * implementation.
183 template <int alignment>
184 int32_t GetAlignedStride(int32_t aWidth, int32_t aBytesPerPixel) {
185 static_assert(alignment > 0 && (alignment & (alignment - 1)) == 0,
186 "This implementation currently require power-of-two alignment");
187 const int32_t mask = alignment - 1;
188 CheckedInt32 stride =
189 CheckedInt32(aWidth) * CheckedInt32(aBytesPerPixel) + CheckedInt32(mask);
190 if (stride.isValid()) {
191 return stride.value() & ~mask;
193 return 0;
196 } // namespace gfx
197 } // namespace mozilla
199 #endif /* MOZILLA_GFX_TOOLS_H_ */