gfx/2d/FilterProcessingScalar.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: 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 #define FILTER_PROCESSING_SCALAR
michael@0 7
michael@0 8 #include "FilterProcessingSIMD-inl.h"
michael@0 9
michael@0 10 namespace mozilla {
michael@0 11 namespace gfx {
michael@0 12
michael@0 13 void
michael@0 14 FilterProcessing::ExtractAlpha_Scalar(const IntSize& size, uint8_t* sourceData, int32_t sourceStride, uint8_t* alphaData, int32_t alphaStride)
michael@0 15 {
michael@0 16 for (int32_t y = 0; y < size.height; y++) {
michael@0 17 for (int32_t x = 0; x < size.width; x++) {
michael@0 18 int32_t sourceIndex = y * sourceStride + 4 * x;
michael@0 19 int32_t targetIndex = y * alphaStride + x;
michael@0 20 alphaData[targetIndex] = sourceData[sourceIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A];
michael@0 21 }
michael@0 22 }
michael@0 23 }
michael@0 24
michael@0 25 TemporaryRef<DataSourceSurface>
michael@0 26 FilterProcessing::ConvertToB8G8R8A8_Scalar(SourceSurface* aSurface)
michael@0 27 {
michael@0 28 return ConvertToB8G8R8A8_SIMD<simd::Scalaru8x16_t>(aSurface);
michael@0 29 }
michael@0 30
michael@0 31 template<BlendMode aBlendMode>
michael@0 32 static TemporaryRef<DataSourceSurface>
michael@0 33 ApplyBlending_Scalar(DataSourceSurface* aInput1, DataSourceSurface* aInput2)
michael@0 34 {
michael@0 35 IntSize size = aInput1->GetSize();
michael@0 36 RefPtr<DataSourceSurface> target =
michael@0 37 Factory::CreateDataSourceSurface(size, SurfaceFormat::B8G8R8A8);
michael@0 38 if (!target) {
michael@0 39 return nullptr;
michael@0 40 }
michael@0 41
michael@0 42 uint8_t* source1Data = aInput1->GetData();
michael@0 43 uint8_t* source2Data = aInput2->GetData();
michael@0 44 uint8_t* targetData = target->GetData();
michael@0 45 uint32_t targetStride = target->Stride();
michael@0 46 uint32_t source1Stride = aInput1->Stride();
michael@0 47 uint32_t source2Stride = aInput2->Stride();
michael@0 48
michael@0 49 for (int32_t y = 0; y < size.height; y++) {
michael@0 50 for (int32_t x = 0; x < size.width; x++) {
michael@0 51 uint32_t targetIndex = y * targetStride + 4 * x;
michael@0 52 uint32_t source1Index = y * source1Stride + 4 * x;
michael@0 53 uint32_t source2Index = y * source2Stride + 4 * x;
michael@0 54 uint32_t qa = source1Data[source1Index + B8G8R8A8_COMPONENT_BYTEOFFSET_A];
michael@0 55 uint32_t qb = source2Data[source2Index + B8G8R8A8_COMPONENT_BYTEOFFSET_A];
michael@0 56 for (int32_t i = std::min(B8G8R8A8_COMPONENT_BYTEOFFSET_B, B8G8R8A8_COMPONENT_BYTEOFFSET_R);
michael@0 57 i <= std::max(B8G8R8A8_COMPONENT_BYTEOFFSET_B, B8G8R8A8_COMPONENT_BYTEOFFSET_R); i++) {
michael@0 58 uint32_t ca = source1Data[source1Index + i];
michael@0 59 uint32_t cb = source2Data[source2Index + i];
michael@0 60 uint32_t val;
michael@0 61 switch (aBlendMode) {
michael@0 62 case BLEND_MODE_MULTIPLY:
michael@0 63 val = ((255 - qa) * cb + (255 - qb + cb) * ca);
michael@0 64 break;
michael@0 65 case BLEND_MODE_SCREEN:
michael@0 66 val = 255 * (cb + ca) - ca * cb;
michael@0 67 break;
michael@0 68 case BLEND_MODE_DARKEN:
michael@0 69 val = umin((255 - qa) * cb + 255 * ca,
michael@0 70 (255 - qb) * ca + 255 * cb);
michael@0 71 break;
michael@0 72 case BLEND_MODE_LIGHTEN:
michael@0 73 val = umax((255 - qa) * cb + 255 * ca,
michael@0 74 (255 - qb) * ca + 255 * cb);
michael@0 75 break;
michael@0 76 default:
michael@0 77 MOZ_CRASH();
michael@0 78 }
michael@0 79 val = umin(FilterProcessing::FastDivideBy255<unsigned>(val), 255U);
michael@0 80 targetData[targetIndex + i] = static_cast<uint8_t>(val);
michael@0 81 }
michael@0 82 uint32_t alpha = 255 * 255 - (255 - qa) * (255 - qb);
michael@0 83 targetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A] =
michael@0 84 FilterProcessing::FastDivideBy255<uint8_t>(alpha);
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 return target;
michael@0 89 }
michael@0 90
michael@0 91 TemporaryRef<DataSourceSurface>
michael@0 92 FilterProcessing::ApplyBlending_Scalar(DataSourceSurface* aInput1, DataSourceSurface* aInput2,
michael@0 93 BlendMode aBlendMode)
michael@0 94 {
michael@0 95 switch (aBlendMode) {
michael@0 96 case BLEND_MODE_MULTIPLY:
michael@0 97 return gfx::ApplyBlending_Scalar<BLEND_MODE_MULTIPLY>(aInput1, aInput2);
michael@0 98 case BLEND_MODE_SCREEN:
michael@0 99 return gfx::ApplyBlending_Scalar<BLEND_MODE_SCREEN>(aInput1, aInput2);
michael@0 100 case BLEND_MODE_DARKEN:
michael@0 101 return gfx::ApplyBlending_Scalar<BLEND_MODE_DARKEN>(aInput1, aInput2);
michael@0 102 case BLEND_MODE_LIGHTEN:
michael@0 103 return gfx::ApplyBlending_Scalar<BLEND_MODE_LIGHTEN>(aInput1, aInput2);
michael@0 104 default:
michael@0 105 return nullptr;
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 template<MorphologyOperator Operator>
michael@0 110 static void
michael@0 111 ApplyMorphologyHorizontal_Scalar(uint8_t* aSourceData, int32_t aSourceStride,
michael@0 112 uint8_t* aDestData, int32_t aDestStride,
michael@0 113 const IntRect& aDestRect, int32_t aRadius)
michael@0 114 {
michael@0 115 static_assert(Operator == MORPHOLOGY_OPERATOR_ERODE ||
michael@0 116 Operator == MORPHOLOGY_OPERATOR_DILATE,
michael@0 117 "unexpected morphology operator");
michael@0 118
michael@0 119 for (int32_t y = aDestRect.y; y < aDestRect.YMost(); y++) {
michael@0 120 int32_t startX = aDestRect.x - aRadius;
michael@0 121 int32_t endX = aDestRect.x + aRadius;
michael@0 122 for (int32_t x = aDestRect.x; x < aDestRect.XMost(); x++, startX++, endX++) {
michael@0 123 int32_t sourceIndex = y * aSourceStride + 4 * startX;
michael@0 124 uint8_t u[4];
michael@0 125 for (size_t i = 0; i < 4; i++) {
michael@0 126 u[i] = aSourceData[sourceIndex + i];
michael@0 127 }
michael@0 128 sourceIndex += 4;
michael@0 129 for (int32_t ix = startX + 1; ix <= endX; ix++, sourceIndex += 4) {
michael@0 130 for (size_t i = 0; i < 4; i++) {
michael@0 131 if (Operator == MORPHOLOGY_OPERATOR_ERODE) {
michael@0 132 u[i] = umin(u[i], aSourceData[sourceIndex + i]);
michael@0 133 } else {
michael@0 134 u[i] = umax(u[i], aSourceData[sourceIndex + i]);
michael@0 135 }
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 int32_t destIndex = y * aDestStride + 4 * x;
michael@0 140 for (size_t i = 0; i < 4; i++) {
michael@0 141 aDestData[destIndex+i] = u[i];
michael@0 142 }
michael@0 143 }
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 void
michael@0 148 FilterProcessing::ApplyMorphologyHorizontal_Scalar(uint8_t* aSourceData, int32_t aSourceStride,
michael@0 149 uint8_t* aDestData, int32_t aDestStride,
michael@0 150 const IntRect& aDestRect, int32_t aRadius,
michael@0 151 MorphologyOperator aOp)
michael@0 152 {
michael@0 153 if (aOp == MORPHOLOGY_OPERATOR_ERODE) {
michael@0 154 gfx::ApplyMorphologyHorizontal_Scalar<MORPHOLOGY_OPERATOR_ERODE>(
michael@0 155 aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius);
michael@0 156 } else {
michael@0 157 gfx::ApplyMorphologyHorizontal_Scalar<MORPHOLOGY_OPERATOR_DILATE>(
michael@0 158 aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius);
michael@0 159 }
michael@0 160 }
michael@0 161
michael@0 162 template<MorphologyOperator Operator>
michael@0 163 static void ApplyMorphologyVertical_Scalar(uint8_t* aSourceData, int32_t aSourceStride,
michael@0 164 uint8_t* aDestData, int32_t aDestStride,
michael@0 165 const IntRect& aDestRect, int32_t aRadius)
michael@0 166 {
michael@0 167 static_assert(Operator == MORPHOLOGY_OPERATOR_ERODE ||
michael@0 168 Operator == MORPHOLOGY_OPERATOR_DILATE,
michael@0 169 "unexpected morphology operator");
michael@0 170
michael@0 171 int32_t startY = aDestRect.y - aRadius;
michael@0 172 int32_t endY = aDestRect.y + aRadius;
michael@0 173 for (int32_t y = aDestRect.y; y < aDestRect.YMost(); y++, startY++, endY++) {
michael@0 174 for (int32_t x = aDestRect.x; x < aDestRect.XMost(); x++) {
michael@0 175 int32_t sourceIndex = startY * aSourceStride + 4 * x;
michael@0 176 uint8_t u[4];
michael@0 177 for (size_t i = 0; i < 4; i++) {
michael@0 178 u[i] = aSourceData[sourceIndex + i];
michael@0 179 }
michael@0 180 sourceIndex += aSourceStride;
michael@0 181 for (int32_t iy = startY + 1; iy <= endY; iy++, sourceIndex += aSourceStride) {
michael@0 182 for (size_t i = 0; i < 4; i++) {
michael@0 183 if (Operator == MORPHOLOGY_OPERATOR_ERODE) {
michael@0 184 u[i] = umin(u[i], aSourceData[sourceIndex + i]);
michael@0 185 } else {
michael@0 186 u[i] = umax(u[i], aSourceData[sourceIndex + i]);
michael@0 187 }
michael@0 188 }
michael@0 189 }
michael@0 190
michael@0 191 int32_t destIndex = y * aDestStride + 4 * x;
michael@0 192 for (size_t i = 0; i < 4; i++) {
michael@0 193 aDestData[destIndex+i] = u[i];
michael@0 194 }
michael@0 195 }
michael@0 196 }
michael@0 197 }
michael@0 198
michael@0 199 void
michael@0 200 FilterProcessing::ApplyMorphologyVertical_Scalar(uint8_t* aSourceData, int32_t aSourceStride,
michael@0 201 uint8_t* aDestData, int32_t aDestStride,
michael@0 202 const IntRect& aDestRect, int32_t aRadius,
michael@0 203 MorphologyOperator aOp)
michael@0 204 {
michael@0 205 if (aOp == MORPHOLOGY_OPERATOR_ERODE) {
michael@0 206 gfx::ApplyMorphologyVertical_Scalar<MORPHOLOGY_OPERATOR_ERODE>(
michael@0 207 aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius);
michael@0 208 } else {
michael@0 209 gfx::ApplyMorphologyVertical_Scalar<MORPHOLOGY_OPERATOR_DILATE>(
michael@0 210 aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius);
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 TemporaryRef<DataSourceSurface>
michael@0 215 FilterProcessing::ApplyColorMatrix_Scalar(DataSourceSurface* aInput, const Matrix5x4 &aMatrix)
michael@0 216 {
michael@0 217 return ApplyColorMatrix_SIMD<simd::Scalari32x4_t,simd::Scalari16x8_t,simd::Scalaru8x16_t>(aInput, aMatrix);
michael@0 218 }
michael@0 219
michael@0 220 void
michael@0 221 FilterProcessing::ApplyComposition_Scalar(DataSourceSurface* aSource, DataSourceSurface* aDest,
michael@0 222 CompositeOperator aOperator)
michael@0 223 {
michael@0 224 return ApplyComposition_SIMD<simd::Scalari32x4_t,simd::Scalaru16x8_t,simd::Scalaru8x16_t>(aSource, aDest, aOperator);
michael@0 225 }
michael@0 226
michael@0 227 void
michael@0 228 FilterProcessing::SeparateColorChannels_Scalar(const IntSize &size, uint8_t* sourceData, int32_t sourceStride, uint8_t* channel0Data, uint8_t* channel1Data, uint8_t* channel2Data, uint8_t* channel3Data, int32_t channelStride)
michael@0 229 {
michael@0 230 for (int32_t y = 0; y < size.height; y++) {
michael@0 231 for (int32_t x = 0; x < size.width; x++) {
michael@0 232 int32_t sourceIndex = y * sourceStride + 4 * x;
michael@0 233 int32_t targetIndex = y * channelStride + x;
michael@0 234 channel0Data[targetIndex] = sourceData[sourceIndex];
michael@0 235 channel1Data[targetIndex] = sourceData[sourceIndex+1];
michael@0 236 channel2Data[targetIndex] = sourceData[sourceIndex+2];
michael@0 237 channel3Data[targetIndex] = sourceData[sourceIndex+3];
michael@0 238 }
michael@0 239 }
michael@0 240 }
michael@0 241
michael@0 242 void
michael@0 243 FilterProcessing::CombineColorChannels_Scalar(const IntSize &size, int32_t resultStride, uint8_t* resultData, int32_t channelStride, uint8_t* channel0Data, uint8_t* channel1Data, uint8_t* channel2Data, uint8_t* channel3Data)
michael@0 244 {
michael@0 245 for (int32_t y = 0; y < size.height; y++) {
michael@0 246 for (int32_t x = 0; x < size.width; x++) {
michael@0 247 int32_t resultIndex = y * resultStride + 4 * x;
michael@0 248 int32_t channelIndex = y * channelStride + x;
michael@0 249 resultData[resultIndex] = channel0Data[channelIndex];
michael@0 250 resultData[resultIndex+1] = channel1Data[channelIndex];
michael@0 251 resultData[resultIndex+2] = channel2Data[channelIndex];
michael@0 252 resultData[resultIndex+3] = channel3Data[channelIndex];
michael@0 253 }
michael@0 254 }
michael@0 255 }
michael@0 256
michael@0 257 void
michael@0 258 FilterProcessing::DoPremultiplicationCalculation_Scalar(const IntSize& aSize,
michael@0 259 uint8_t* aTargetData, int32_t aTargetStride,
michael@0 260 uint8_t* aSourceData, int32_t aSourceStride)
michael@0 261 {
michael@0 262 for (int32_t y = 0; y < aSize.height; y++) {
michael@0 263 for (int32_t x = 0; x < aSize.width; x++) {
michael@0 264 int32_t inputIndex = y * aSourceStride + 4 * x;
michael@0 265 int32_t targetIndex = y * aTargetStride + 4 * x;
michael@0 266 uint8_t alpha = aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A];
michael@0 267 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_R] =
michael@0 268 FastDivideBy255<uint8_t>(aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_R] * alpha);
michael@0 269 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_G] =
michael@0 270 FastDivideBy255<uint8_t>(aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_G] * alpha);
michael@0 271 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_B] =
michael@0 272 FastDivideBy255<uint8_t>(aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_B] * alpha);
michael@0 273 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A] = alpha;
michael@0 274 }
michael@0 275 }
michael@0 276 }
michael@0 277
michael@0 278 void
michael@0 279 FilterProcessing::DoUnpremultiplicationCalculation_Scalar(
michael@0 280 const IntSize& aSize,
michael@0 281 uint8_t* aTargetData, int32_t aTargetStride,
michael@0 282 uint8_t* aSourceData, int32_t aSourceStride)
michael@0 283 {
michael@0 284 for (int32_t y = 0; y < aSize.height; y++) {
michael@0 285 for (int32_t x = 0; x < aSize.width; x++) {
michael@0 286 int32_t inputIndex = y * aSourceStride + 4 * x;
michael@0 287 int32_t targetIndex = y * aTargetStride + 4 * x;
michael@0 288 uint8_t alpha = aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A];
michael@0 289 uint16_t alphaFactor = sAlphaFactors[alpha];
michael@0 290 // inputColor * alphaFactor + 128 is guaranteed to fit into uint16_t
michael@0 291 // because the input is premultiplied and thus inputColor <= inputAlpha.
michael@0 292 // The maximum value this can attain is 65520 (which is less than 65535)
michael@0 293 // for color == alpha == 244:
michael@0 294 // 244 * sAlphaFactors[244] + 128 == 244 * 268 + 128 == 65520
michael@0 295 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_R] =
michael@0 296 (aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_R] * alphaFactor + 128) >> 8;
michael@0 297 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_G] =
michael@0 298 (aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_G] * alphaFactor + 128) >> 8;
michael@0 299 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_B] =
michael@0 300 (aSourceData[inputIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_B] * alphaFactor + 128) >> 8;
michael@0 301 aTargetData[targetIndex + B8G8R8A8_COMPONENT_BYTEOFFSET_A] = alpha;
michael@0 302 }
michael@0 303 }
michael@0 304 }
michael@0 305
michael@0 306 TemporaryRef<DataSourceSurface>
michael@0 307 FilterProcessing::RenderTurbulence_Scalar(const IntSize &aSize, const Point &aOffset, const Size &aBaseFrequency,
michael@0 308 int32_t aSeed, int aNumOctaves, TurbulenceType aType, bool aStitch, const Rect &aTileRect)
michael@0 309 {
michael@0 310 return RenderTurbulence_SIMD<simd::Scalarf32x4_t,simd::Scalari32x4_t,simd::Scalaru8x16_t>(
michael@0 311 aSize, aOffset, aBaseFrequency, aSeed, aNumOctaves, aType, aStitch, aTileRect);
michael@0 312 }
michael@0 313
michael@0 314 TemporaryRef<DataSourceSurface>
michael@0 315 FilterProcessing::ApplyArithmeticCombine_Scalar(DataSourceSurface* aInput1, DataSourceSurface* aInput2, Float aK1, Float aK2, Float aK3, Float aK4)
michael@0 316 {
michael@0 317 return ApplyArithmeticCombine_SIMD<simd::Scalari32x4_t,simd::Scalari16x8_t,simd::Scalaru8x16_t>(aInput1, aInput2, aK1, aK2, aK3, aK4);
michael@0 318 }
michael@0 319
michael@0 320 } // namespace mozilla
michael@0 321 } // namespace gfx

mercurial