summaryrefslogtreecommitdiffstats
path: root/gfx/2d/FilterProcessing.cpp
blob: da734c01a63e9f475741d75de8f27906c38fa83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FilterProcessing.h"
#include "Logging.h"

namespace mozilla {
namespace gfx {

already_AddRefed<DataSourceSurface>
FilterProcessing::ExtractAlpha(DataSourceSurface* aSource)
{
  IntSize size = aSource->GetSize();
  RefPtr<DataSourceSurface> alpha = Factory::CreateDataSourceSurface(size, SurfaceFormat::A8);
  if (MOZ2D_WARN_IF(!alpha)) {
    return nullptr;
  }

  DataSourceSurface::ScopedMap sourceMap(aSource, DataSourceSurface::READ);
  DataSourceSurface::ScopedMap alphaMap(alpha, DataSourceSurface::WRITE);
  if (MOZ2D_WARN_IF(!sourceMap.IsMapped() || !alphaMap.IsMapped())) {
    return nullptr;
  }

  uint8_t* sourceData = sourceMap.GetData();
  int32_t sourceStride = sourceMap.GetStride();
  uint8_t* alphaData = alphaMap.GetData();
  int32_t alphaStride = alphaMap.GetStride();

  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    ExtractAlpha_SSE2(size, sourceData, sourceStride, alphaData, alphaStride);
#endif
  } else {
    ExtractAlpha_Scalar(size, sourceData, sourceStride, alphaData, alphaStride);
  }

  return alpha.forget();
}

already_AddRefed<DataSourceSurface>
FilterProcessing::ConvertToB8G8R8A8(SourceSurface* aSurface)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    return ConvertToB8G8R8A8_SSE2(aSurface);
#endif
  }
  return ConvertToB8G8R8A8_Scalar(aSurface);
}

already_AddRefed<DataSourceSurface>
FilterProcessing::ApplyBlending(DataSourceSurface* aInput1, DataSourceSurface* aInput2,
                                BlendMode aBlendMode)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    return ApplyBlending_SSE2(aInput1, aInput2, aBlendMode);
#endif
  }
  return nullptr;
}

void
FilterProcessing::ApplyMorphologyHorizontal(uint8_t* aSourceData, int32_t aSourceStride,
                                            uint8_t* aDestData, int32_t aDestStride,
                                            const IntRect& aDestRect, int32_t aRadius,
                                            MorphologyOperator aOp)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    ApplyMorphologyHorizontal_SSE2(
      aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius, aOp);
#endif
  } else {
    ApplyMorphologyHorizontal_Scalar(
      aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius, aOp);
  }
}

void
FilterProcessing::ApplyMorphologyVertical(uint8_t* aSourceData, int32_t aSourceStride,
                                            uint8_t* aDestData, int32_t aDestStride,
                                            const IntRect& aDestRect, int32_t aRadius,
                                            MorphologyOperator aOp)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    ApplyMorphologyVertical_SSE2(
      aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius, aOp);
#endif
  } else {
    ApplyMorphologyVertical_Scalar(
      aSourceData, aSourceStride, aDestData, aDestStride, aDestRect, aRadius, aOp);
  }
}

already_AddRefed<DataSourceSurface>
FilterProcessing::ApplyColorMatrix(DataSourceSurface* aInput, const Matrix5x4 &aMatrix)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    return ApplyColorMatrix_SSE2(aInput, aMatrix);
#endif
  }
  return ApplyColorMatrix_Scalar(aInput, aMatrix);
}

void
FilterProcessing::ApplyComposition(DataSourceSurface* aSource, DataSourceSurface* aDest,
                                   CompositeOperator aOperator)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    ApplyComposition_SSE2(aSource, aDest, aOperator);
#endif
  } else {
    ApplyComposition_Scalar(aSource, aDest, aOperator);
  }
}

void
FilterProcessing::SeparateColorChannels(DataSourceSurface* aSource,
                                        RefPtr<DataSourceSurface>& aChannel0,
                                        RefPtr<DataSourceSurface>& aChannel1,
                                        RefPtr<DataSourceSurface>& aChannel2,
                                        RefPtr<DataSourceSurface>& aChannel3)
{
  IntSize size = aSource->GetSize();
  aChannel0 = Factory::CreateDataSourceSurface(size, SurfaceFormat::A8);
  aChannel1 = Factory::CreateDataSourceSurface(size, SurfaceFormat::A8);
  aChannel2 = Factory::CreateDataSourceSurface(size, SurfaceFormat::A8);
  aChannel3 = Factory::CreateDataSourceSurface(size, SurfaceFormat::A8);
  if (MOZ2D_WARN_IF(!(aChannel0 && aChannel1 && aChannel2 && aChannel3))) {
    return;
  }

  DataSourceSurface::ScopedMap sourceMap(aSource, DataSourceSurface::READ);
  DataSourceSurface::ScopedMap channel0Map(aChannel0, DataSourceSurface::WRITE);
  DataSourceSurface::ScopedMap channel1Map(aChannel1, DataSourceSurface::WRITE);
  DataSourceSurface::ScopedMap channel2Map(aChannel2, DataSourceSurface::WRITE);
  DataSourceSurface::ScopedMap channel3Map(aChannel3, DataSourceSurface::WRITE);
  if (MOZ2D_WARN_IF(!(sourceMap.IsMapped() &&
                      channel0Map.IsMapped() && channel1Map.IsMapped() &&
                      channel2Map.IsMapped() && channel3Map.IsMapped()))) {
    return;
  }
  uint8_t* sourceData = sourceMap.GetData();
  int32_t sourceStride = sourceMap.GetStride();
  uint8_t* channel0Data = channel0Map.GetData();
  uint8_t* channel1Data = channel1Map.GetData();
  uint8_t* channel2Data = channel2Map.GetData();
  uint8_t* channel3Data = channel3Map.GetData();
  int32_t channelStride = channel0Map.GetStride();

  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    SeparateColorChannels_SSE2(size, sourceData, sourceStride, channel0Data, channel1Data, channel2Data, channel3Data, channelStride);
#endif
  } else {
    SeparateColorChannels_Scalar(size, sourceData, sourceStride, channel0Data, channel1Data, channel2Data, channel3Data, channelStride);
  }
}

already_AddRefed<DataSourceSurface>
FilterProcessing::CombineColorChannels(DataSourceSurface* aChannel0, DataSourceSurface* aChannel1,
                                       DataSourceSurface* aChannel2, DataSourceSurface* aChannel3)
{
  IntSize size = aChannel0->GetSize();
  RefPtr<DataSourceSurface> result =
    Factory::CreateDataSourceSurface(size, SurfaceFormat::B8G8R8A8);
  if (MOZ2D_WARN_IF(!result)) {
    return nullptr;
  }
  DataSourceSurface::ScopedMap resultMap(result, DataSourceSurface::WRITE);
  DataSourceSurface::ScopedMap channel0Map(aChannel0, DataSourceSurface::READ);
  DataSourceSurface::ScopedMap channel1Map(aChannel1, DataSourceSurface::READ);
  DataSourceSurface::ScopedMap channel2Map(aChannel2, DataSourceSurface::READ);
  DataSourceSurface::ScopedMap channel3Map(aChannel3, DataSourceSurface::READ);
  if (MOZ2D_WARN_IF(!(resultMap.IsMapped() &&
                      channel0Map.IsMapped() && channel1Map.IsMapped() &&
                      channel2Map.IsMapped() && channel3Map.IsMapped()))) {
    return nullptr;
  }
  int32_t resultStride = resultMap.GetStride();
  uint8_t* resultData = resultMap.GetData();
  int32_t channelStride = channel0Map.GetStride();
  uint8_t* channel0Data = channel0Map.GetData();
  uint8_t* channel1Data = channel1Map.GetData();
  uint8_t* channel2Data = channel2Map.GetData();
  uint8_t* channel3Data = channel3Map.GetData();

  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    CombineColorChannels_SSE2(size, resultStride, resultData, channelStride, channel0Data, channel1Data, channel2Data, channel3Data);
#endif
  } else {
    CombineColorChannels_Scalar(size, resultStride, resultData, channelStride, channel0Data, channel1Data, channel2Data, channel3Data);
  }

  return result.forget();
}

void
FilterProcessing::DoPremultiplicationCalculation(const IntSize& aSize,
                                                 uint8_t* aTargetData, int32_t aTargetStride,
                                                 uint8_t* aSourceData, int32_t aSourceStride)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2 
    DoPremultiplicationCalculation_SSE2(
      aSize, aTargetData, aTargetStride, aSourceData, aSourceStride);
#endif
  } else {
    DoPremultiplicationCalculation_Scalar(
      aSize, aTargetData, aTargetStride, aSourceData, aSourceStride);
  }
}

void
FilterProcessing::DoUnpremultiplicationCalculation(const IntSize& aSize,
                                                   uint8_t* aTargetData, int32_t aTargetStride,
                                                   uint8_t* aSourceData, int32_t aSourceStride)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2 
    DoUnpremultiplicationCalculation_SSE2(
      aSize, aTargetData, aTargetStride, aSourceData, aSourceStride);
#endif
  } else {
    DoUnpremultiplicationCalculation_Scalar(
      aSize, aTargetData, aTargetStride, aSourceData, aSourceStride);
  }
}

already_AddRefed<DataSourceSurface>
FilterProcessing::RenderTurbulence(const IntSize &aSize, const Point &aOffset, const Size &aBaseFrequency,
                                   int32_t aSeed, int aNumOctaves, TurbulenceType aType, bool aStitch, const Rect &aTileRect)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    return RenderTurbulence_SSE2(aSize, aOffset, aBaseFrequency, aSeed, aNumOctaves, aType, aStitch, aTileRect);
#endif
  }
  return RenderTurbulence_Scalar(aSize, aOffset, aBaseFrequency, aSeed, aNumOctaves, aType, aStitch, aTileRect);
}

already_AddRefed<DataSourceSurface>
FilterProcessing::ApplyArithmeticCombine(DataSourceSurface* aInput1, DataSourceSurface* aInput2, Float aK1, Float aK2, Float aK3, Float aK4)
{
  if (Factory::HasSSE2()) {
#ifdef USE_SSE2
    return ApplyArithmeticCombine_SSE2(aInput1, aInput2, aK1, aK2, aK3, aK4);
#endif
  }
  return ApplyArithmeticCombine_Scalar(aInput1, aInput2, aK1, aK2, aK3, aK4);
}

} // namespace gfx
} // namespace mozilla