gfx/layers/LayerUtils.cpp

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: 2; 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 #include "LayerUtils.h"
michael@0 7 #include "PremultiplyTables.h"
michael@0 8 #include "mozilla/Endian.h"
michael@0 9
michael@0 10 namespace mozilla {
michael@0 11 namespace layers {
michael@0 12
michael@0 13 using namespace mozilla::gfx;
michael@0 14
michael@0 15 static inline const uint8_t PremultiplyValue(uint8_t a, uint8_t v) {
michael@0 16 return PremultiplyTable[a*256+v];
michael@0 17 }
michael@0 18
michael@0 19 static inline const uint8_t UnpremultiplyValue(uint8_t a, uint8_t v) {
michael@0 20 return UnpremultiplyTable[a*256+v];
michael@0 21 }
michael@0 22
michael@0 23 #ifdef DEBUG
michael@0 24 static bool IsLittleEndian()
michael@0 25 {
michael@0 26 // Violate strict aliasing, because violating strict aliasing is how
michael@0 27 // we always pack and unpack between uint32_t and uint8_t[].
michael@0 28 uint16_t testShort;
michael@0 29 static const uint8_t testBytes[2] = { 0xAA, 0xBB };
michael@0 30 memcpy(&testShort, testBytes, sizeof(testBytes));
michael@0 31 return testShort == 0xBBAA;
michael@0 32 }
michael@0 33 #endif // DEBUG
michael@0 34
michael@0 35 #ifdef MOZ_LITTLE_ENDIAN
michael@0 36 #define ASSERT_ENDIAN() MOZ_ASSERT(IsLittleEndian(), "Defined as little endian, but actually big!")
michael@0 37 #else
michael@0 38 #define ASSERT_ENDIAN() MOZ_ASSERT(!IsLittleEndian(), "Defined as big endian, but actually little!")
michael@0 39 #endif
michael@0 40
michael@0 41 void
michael@0 42 PremultiplySurface(DataSourceSurface* srcSurface,
michael@0 43 DataSourceSurface* destSurface)
michael@0 44 {
michael@0 45 if (!destSurface)
michael@0 46 destSurface = srcSurface;
michael@0 47
michael@0 48 IntSize srcSize = srcSurface->GetSize();
michael@0 49 MOZ_ASSERT(srcSurface->GetFormat() == destSurface->GetFormat() &&
michael@0 50 srcSize.width == destSurface->GetSize().width &&
michael@0 51 srcSize.height == destSurface->GetSize().height &&
michael@0 52 srcSurface->Stride() == destSurface->Stride(),
michael@0 53 "Source and destination surfaces don't have identical characteristics");
michael@0 54
michael@0 55 MOZ_ASSERT(srcSurface->Stride() == srcSize.width * 4,
michael@0 56 "Source surface stride isn't tightly packed");
michael@0 57
michael@0 58 // Only premultiply ARGB32
michael@0 59 if (srcSurface->GetFormat() != SurfaceFormat::B8G8R8A8) {
michael@0 60 if (destSurface != srcSurface) {
michael@0 61 memcpy(destSurface->GetData(), srcSurface->GetData(),
michael@0 62 srcSurface->Stride() * srcSize.height);
michael@0 63 }
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 uint8_t *src = srcSurface->GetData();
michael@0 68 uint8_t *dst = destSurface->GetData();
michael@0 69
michael@0 70 // Assert that our endian define is correct.
michael@0 71 ASSERT_ENDIAN();
michael@0 72
michael@0 73 uint32_t dim = srcSize.width * srcSize.height;
michael@0 74 for (uint32_t i = 0; i < dim; ++i) {
michael@0 75 #ifdef MOZ_LITTLE_ENDIAN
michael@0 76 uint8_t b = *src++;
michael@0 77 uint8_t g = *src++;
michael@0 78 uint8_t r = *src++;
michael@0 79 uint8_t a = *src++;
michael@0 80
michael@0 81 *dst++ = PremultiplyValue(a, b);
michael@0 82 *dst++ = PremultiplyValue(a, g);
michael@0 83 *dst++ = PremultiplyValue(a, r);
michael@0 84 *dst++ = a;
michael@0 85 #else
michael@0 86 uint8_t a = *src++;
michael@0 87 uint8_t r = *src++;
michael@0 88 uint8_t g = *src++;
michael@0 89 uint8_t b = *src++;
michael@0 90
michael@0 91 *dst++ = a;
michael@0 92 *dst++ = PremultiplyValue(a, r);
michael@0 93 *dst++ = PremultiplyValue(a, g);
michael@0 94 *dst++ = PremultiplyValue(a, b);
michael@0 95 #endif
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99
michael@0 100 }
michael@0 101 }

mercurial