gfx/2d/Tools.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZILLA_GFX_TOOLS_H_
michael@0 7 #define MOZILLA_GFX_TOOLS_H_
michael@0 8
michael@0 9 #include "Types.h"
michael@0 10 #include "Point.h"
michael@0 11 #include <math.h>
michael@0 12 #if defined(_MSC_VER) && (_MSC_VER < 1600)
michael@0 13 #define hypotf _hypotf
michael@0 14 #endif
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace gfx {
michael@0 18
michael@0 19 static inline bool
michael@0 20 IsOperatorBoundByMask(CompositionOp aOp) {
michael@0 21 switch (aOp) {
michael@0 22 case CompositionOp::OP_IN:
michael@0 23 case CompositionOp::OP_OUT:
michael@0 24 case CompositionOp::OP_DEST_IN:
michael@0 25 case CompositionOp::OP_DEST_ATOP:
michael@0 26 case CompositionOp::OP_SOURCE:
michael@0 27 return false;
michael@0 28 default:
michael@0 29 return true;
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 template <class T>
michael@0 34 struct ClassStorage
michael@0 35 {
michael@0 36 char bytes[sizeof(T)];
michael@0 37
michael@0 38 const T *addr() const { return (const T *)bytes; }
michael@0 39 T *addr() { return (T *)(void *)bytes; }
michael@0 40 };
michael@0 41
michael@0 42 static inline bool
michael@0 43 FuzzyEqual(Float aA, Float aB, Float aErr)
michael@0 44 {
michael@0 45 if ((aA + aErr >= aB) && (aA - aErr <= aB)) {
michael@0 46 return true;
michael@0 47 }
michael@0 48 return false;
michael@0 49 }
michael@0 50
michael@0 51 static inline void
michael@0 52 NudgeToInteger(float *aVal)
michael@0 53 {
michael@0 54 float r = floorf(*aVal + 0.5f);
michael@0 55 // The error threshold should be proportional to the rounded value. This
michael@0 56 // bounds the relative error introduced by the nudge operation. However,
michael@0 57 // when the rounded value is 0, the error threshold can't be proportional
michael@0 58 // to the rounded value (we'd never round), so we just choose the same
michael@0 59 // threshold as for a rounded value of 1.
michael@0 60 if (FuzzyEqual(r, *aVal, r == 0.0f ? 1e-6f : fabs(r*1e-6f))) {
michael@0 61 *aVal = r;
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 static inline Float
michael@0 66 Distance(Point aA, Point aB)
michael@0 67 {
michael@0 68 return hypotf(aB.x - aA.x, aB.y - aA.y);
michael@0 69 }
michael@0 70
michael@0 71 static inline int
michael@0 72 BytesPerPixel(SurfaceFormat aFormat)
michael@0 73 {
michael@0 74 switch (aFormat) {
michael@0 75 case SurfaceFormat::A8:
michael@0 76 return 1;
michael@0 77 case SurfaceFormat::R5G6B5:
michael@0 78 return 2;
michael@0 79 default:
michael@0 80 return 4;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 template<typename T, int alignment = 16>
michael@0 85 struct AlignedArray
michael@0 86 {
michael@0 87 AlignedArray()
michael@0 88 : mStorage(nullptr)
michael@0 89 , mPtr(nullptr)
michael@0 90 {
michael@0 91 }
michael@0 92
michael@0 93 MOZ_ALWAYS_INLINE AlignedArray(size_t aSize)
michael@0 94 : mStorage(nullptr)
michael@0 95 {
michael@0 96 Realloc(aSize);
michael@0 97 }
michael@0 98
michael@0 99 MOZ_ALWAYS_INLINE ~AlignedArray()
michael@0 100 {
michael@0 101 delete [] mStorage;
michael@0 102 }
michael@0 103
michael@0 104 void Dealloc()
michael@0 105 {
michael@0 106 delete [] mStorage;
michael@0 107 mStorage = mPtr = nullptr;
michael@0 108 }
michael@0 109
michael@0 110 MOZ_ALWAYS_INLINE void Realloc(size_t aSize)
michael@0 111 {
michael@0 112 delete [] mStorage;
michael@0 113 mStorage = new (std::nothrow) T[aSize + (alignment - 1)];
michael@0 114 if (uintptr_t(mStorage) % alignment) {
michael@0 115 // Our storage does not start at a <alignment>-byte boundary. Make sure mData does!
michael@0 116 mPtr = (T*)(uintptr_t(mStorage) +
michael@0 117 (alignment - (uintptr_t(mStorage) % alignment)));
michael@0 118 } else {
michael@0 119 mPtr = mStorage;
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 MOZ_ALWAYS_INLINE operator T*()
michael@0 124 {
michael@0 125 return mPtr;
michael@0 126 }
michael@0 127
michael@0 128 T *mStorage;
michael@0 129 T *mPtr;
michael@0 130 };
michael@0 131
michael@0 132 template<int alignment>
michael@0 133 int32_t GetAlignedStride(int32_t aStride)
michael@0 134 {
michael@0 135 if (aStride % alignment) {
michael@0 136 return aStride + (alignment - (aStride % alignment));
michael@0 137 }
michael@0 138
michael@0 139 return aStride;
michael@0 140 }
michael@0 141
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 #endif /* MOZILLA_GFX_TOOLS_H_ */

mercurial