summaryrefslogtreecommitdiffstats
path: root/image/DownscalingFilter.h
blob: 013c4d877ad433a667f90141fa72105a9c0e92a2 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* -*- Mode: C++; tab-width: 8; 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/. */

/**
 * DownscalingSurfaceFilter is a SurfaceFilter implementation for use with
 * SurfacePipe which performs Lanczos downscaling.
 *
 * It's in this header file, separated from the other SurfaceFilters, because
 * some preprocessor magic is necessary to ensure that there aren't compilation
 * issues on platforms where Skia is unavailable.
 */

#ifndef mozilla_image_DownscalingFilter_h
#define mozilla_image_DownscalingFilter_h

#include <algorithm>
#include <ctime>
#include <stdint.h>

#include "mozilla/Maybe.h"
#include "mozilla/SSE.h"
#include "mozilla/mips.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/gfx/2D.h"
#include "gfxPrefs.h"

#ifdef MOZ_ENABLE_SKIA
#include "convolver.h"
#include "image_operations.h"
#include "skia/include/core/SkTypes.h"
#endif

#include "SurfacePipe.h"

namespace mozilla {
namespace image {

//////////////////////////////////////////////////////////////////////////////
// DownscalingFilter
//////////////////////////////////////////////////////////////////////////////

template <typename Next> class DownscalingFilter;

/**
 * A configuration struct for DownscalingConfig.
 */
struct DownscalingConfig
{
  template <typename Next> using Filter = DownscalingFilter<Next>;
  gfx::IntSize mInputSize;     /// The size of the input image. We'll downscale
                               /// from this size to the input size of the next
                               /// SurfaceFilter in the chain.
  gfx::SurfaceFormat mFormat;  /// The pixel format - BGRA or BGRX. (BGRX has
                               /// slightly better performance.)
};

#ifndef MOZ_ENABLE_SKIA

/**
 * DownscalingFilter requires Skia. This is a fallback implementation for
 * non-Skia builds that fails when Configure() is called (which will prevent
 * SurfacePipeFactory from returning an instance of it) and crashes if a caller
 * manually constructs an instance and attempts to actually use it. Callers
 * should avoid this by ensuring that they do not request downscaling in
 * non-Skia builds.
 */
template <typename Next>
class DownscalingFilter final : public SurfaceFilter
{
public:
  Maybe<SurfaceInvalidRect> TakeInvalidRect() override { return Nothing(); }

  template <typename... Rest>
  nsresult Configure(const DownscalingConfig& aConfig, const Rest&... aRest)
  {
    return NS_ERROR_FAILURE;
  }

protected:
  uint8_t* DoResetToFirstRow() override { MOZ_CRASH(); return nullptr; }
  uint8_t* DoAdvanceRow() override { MOZ_CRASH(); return nullptr; }
};

#else

/**
 * DownscalingFilter performs Lanczos downscaling, taking image input data at one size
 * and outputting it rescaled to a different size.
 *
 * The 'Next' template parameter specifies the next filter in the chain.
 */
template <typename Next>
class DownscalingFilter final : public SurfaceFilter
{
public:
  DownscalingFilter()
    : mXFilter(MakeUnique<skia::ConvolutionFilter1D>())
    , mYFilter(MakeUnique<skia::ConvolutionFilter1D>())
    , mWindowCapacity(0)
    , mRowsInWindow(0)
    , mInputRow(0)
    , mOutputRow(0)
    , mHasAlpha(true)
  {
    MOZ_ASSERT(gfxPrefs::ImageDownscaleDuringDecodeEnabled(),
               "Downscaling even though downscale-during-decode is disabled?");
  }

  ~DownscalingFilter()
  {
    ReleaseWindow();
  }

  template <typename... Rest>
  nsresult Configure(const DownscalingConfig& aConfig, const Rest&... aRest)
  {
    nsresult rv = mNext.Configure(aRest...);
    if (NS_FAILED(rv)) {
      return rv;
    }

    if (mNext.IsValidPalettedPipe()) {
      NS_WARNING("Created a downscaler for a paletted surface?");
      return NS_ERROR_INVALID_ARG;
    }
    if (mNext.InputSize() == aConfig.mInputSize) {
      NS_WARNING("Created a downscaler, but not downscaling?");
      return NS_ERROR_INVALID_ARG;
    }
    if (mNext.InputSize().width > aConfig.mInputSize.width) {
      NS_WARNING("Created a downscaler, but width is larger");
      return NS_ERROR_INVALID_ARG;
    }
    if (mNext.InputSize().height > aConfig.mInputSize.height) {
      NS_WARNING("Created a downscaler, but height is larger");
      return NS_ERROR_INVALID_ARG;
    }
    if (aConfig.mInputSize.width <= 0 || aConfig.mInputSize.height <= 0) {
      NS_WARNING("Invalid input size for DownscalingFilter");
      return NS_ERROR_INVALID_ARG;
    }

    mInputSize = aConfig.mInputSize;
    gfx::IntSize outputSize = mNext.InputSize();
    mScale = gfxSize(double(mInputSize.width) / outputSize.width,
                     double(mInputSize.height) / outputSize.height);
    mHasAlpha = aConfig.mFormat == gfx::SurfaceFormat::B8G8R8A8;

    ReleaseWindow();

    auto resizeMethod = skia::ImageOperations::RESIZE_LANCZOS3;

    skia::resize::ComputeFilters(resizeMethod,
                                 mInputSize.width, outputSize.width,
                                 0, outputSize.width,
                                 mXFilter.get());

    skia::resize::ComputeFilters(resizeMethod,
                                 mInputSize.height, outputSize.height,
                                 0, outputSize.height,
                                 mYFilter.get());

    // Allocate the buffer, which contains scanlines of the input image.
    mRowBuffer.reset(new (fallible)
                       uint8_t[PaddedWidthInBytes(mInputSize.width)]);
    if (MOZ_UNLIKELY(!mRowBuffer)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    // Clear the buffer to avoid writing uninitialized memory to the output.
    memset(mRowBuffer.get(), 0, PaddedWidthInBytes(mInputSize.width));

    // Allocate the window, which contains horizontally downscaled scanlines. (We
    // can store scanlines which are already downscaled because our downscaling
    // filter is separable.)
    mWindowCapacity = mYFilter->max_filter();
    mWindow.reset(new (fallible) uint8_t*[mWindowCapacity]);
    if (MOZ_UNLIKELY(!mWindow)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    // Allocate the "window" of recent rows that we keep in memory as input for
    // the downscaling code. We intentionally iterate through the entire array
    // even if an allocation fails, to ensure that all the pointers in it are
    // either valid or nullptr. That in turn ensures that ReleaseWindow() can
    // clean up correctly.
    bool anyAllocationFailed = false;
    const uint32_t windowRowSizeInBytes = PaddedWidthInBytes(outputSize.width);
    for (int32_t i = 0; i < mWindowCapacity; ++i) {
      mWindow[i] = new (fallible) uint8_t[windowRowSizeInBytes];
      anyAllocationFailed = anyAllocationFailed || mWindow[i] == nullptr;
    }

    if (MOZ_UNLIKELY(anyAllocationFailed)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    ConfigureFilter(mInputSize, sizeof(uint32_t));
    return NS_OK;
  }

  Maybe<SurfaceInvalidRect> TakeInvalidRect() override
  {
    Maybe<SurfaceInvalidRect> invalidRect = mNext.TakeInvalidRect();

    if (invalidRect) {
      // Compute the input space invalid rect by scaling.
      invalidRect->mInputSpaceRect.ScaleRoundOut(mScale.width, mScale.height);
    }

    return invalidRect;
  }

protected:
  uint8_t* DoResetToFirstRow() override
  {
    mNext.ResetToFirstRow();

    mInputRow = 0;
    mOutputRow = 0;
    mRowsInWindow = 0;

    return GetRowPointer();
  }

  uint8_t* DoAdvanceRow() override
  {
    if (mInputRow >= mInputSize.height) {
      NS_WARNING("Advancing DownscalingFilter past the end of the input");
      return nullptr;
    }

    if (mOutputRow >= mNext.InputSize().height) {
      NS_WARNING("Advancing DownscalingFilter past the end of the output");
      return nullptr;
    }

    int32_t filterOffset = 0;
    int32_t filterLength = 0;
    GetFilterOffsetAndLength(mYFilter, mOutputRow,
                             &filterOffset, &filterLength);

    int32_t inputRowToRead = filterOffset + mRowsInWindow;
    MOZ_ASSERT(mInputRow <= inputRowToRead, "Reading past end of input");
    if (mInputRow == inputRowToRead) {
      MOZ_RELEASE_ASSERT(mRowsInWindow < mWindowCapacity, "Need more rows than capacity!");
      skia::ConvolveHorizontally(mRowBuffer.get(), *mXFilter,
                                 mWindow[mRowsInWindow++], mHasAlpha,
                                 supports_sse2() || supports_mmi());
    }

    MOZ_ASSERT(mOutputRow < mNext.InputSize().height,
               "Writing past end of output");

    while (mRowsInWindow >= filterLength) {
      DownscaleInputRow();

      if (mOutputRow == mNext.InputSize().height) {
        break;  // We're done.
      }

      GetFilterOffsetAndLength(mYFilter, mOutputRow,
                               &filterOffset, &filterLength);
    }

    mInputRow++;

    return mInputRow < mInputSize.height ? GetRowPointer()
                                         : nullptr;
  }

private:
  uint8_t* GetRowPointer() const { return mRowBuffer.get(); }

  static uint32_t PaddedWidthInBytes(uint32_t aLogicalWidth)
  {
    // Convert from width in BGRA/BGRX pixels to width in bytes, padding by 15
    // to handle overreads by the SIMD code inside Skia.
    return aLogicalWidth * sizeof(uint32_t) + 15;
  }

  static void
  GetFilterOffsetAndLength(UniquePtr<skia::ConvolutionFilter1D>& aFilter,
                           int32_t aOutputImagePosition,
                           int32_t* aFilterOffsetOut,
                           int32_t* aFilterLengthOut)
  {
    MOZ_ASSERT(aOutputImagePosition < aFilter->num_values());
    aFilter->FilterForValue(aOutputImagePosition,
                            aFilterOffsetOut,
                            aFilterLengthOut);
  }

  void DownscaleInputRow()
  {
    typedef skia::ConvolutionFilter1D::Fixed FilterValue;

    MOZ_ASSERT(mOutputRow < mNext.InputSize().height,
               "Writing past end of output");

    int32_t filterOffset = 0;
    int32_t filterLength = 0;
    MOZ_ASSERT(mOutputRow < mYFilter->num_values());
    auto filterValues =
      mYFilter->FilterForValue(mOutputRow, &filterOffset, &filterLength);

    mNext.template WriteUnsafeComputedRow<uint32_t>([&](uint32_t* aRow,
                                                        uint32_t aLength) {
      skia::ConvolveVertically(static_cast<const FilterValue*>(filterValues),
                               filterLength, mWindow.get(), mXFilter->num_values(),
                               reinterpret_cast<uint8_t*>(aRow), mHasAlpha,
                               supports_sse2() || supports_mmi());
    });

    mOutputRow++;

    if (mOutputRow == mNext.InputSize().height) {
      return;  // We're done.
    }

    int32_t newFilterOffset = 0;
    int32_t newFilterLength = 0;
    GetFilterOffsetAndLength(mYFilter, mOutputRow,
                             &newFilterOffset, &newFilterLength);

    int diff = newFilterOffset - filterOffset;
    MOZ_ASSERT(diff >= 0, "Moving backwards in the filter?");

    // Shift the buffer. We're just moving pointers here, so this is cheap.
    mRowsInWindow -= diff;
    mRowsInWindow = std::min(std::max(mRowsInWindow, 0), mWindowCapacity);

    // If we already have enough rows to satisfy the filter, there is no need
    // to swap as we won't be writing more before the next convolution.
    if (filterLength > mRowsInWindow) {
      for (int32_t i = 0; i < mRowsInWindow; ++i) {
        std::swap(mWindow[i], mWindow[filterLength - mRowsInWindow + i]);
      }
    }
  }

  void ReleaseWindow()
  {
    if (!mWindow) {
      return;
    }

    for (int32_t i = 0; i < mWindowCapacity; ++i) {
      delete[] mWindow[i];
    }

    mWindow = nullptr;
    mWindowCapacity = 0;
  }

  Next mNext;                       /// The next SurfaceFilter in the chain.

  gfx::IntSize mInputSize;          /// The size of the input image.
  gfxSize mScale;                   /// The scale factors in each dimension.
                                    /// Computed from @mInputSize and
                                    /// the next filter's input size.

  UniquePtr<uint8_t[]> mRowBuffer;  /// The buffer into which input is written.
  UniquePtr<uint8_t*[]> mWindow;    /// The last few rows which were written.

  UniquePtr<skia::ConvolutionFilter1D> mXFilter;  /// The Lanczos filter in X.
  UniquePtr<skia::ConvolutionFilter1D> mYFilter;  /// The Lanczos filter in Y.

  int32_t mWindowCapacity;  /// How many rows the window contains.

  int32_t mRowsInWindow;    /// How many rows we've buffered in the window.
  int32_t mInputRow;        /// The current row we're reading. (0-indexed)
  int32_t mOutputRow;       /// The current row we're writing. (0-indexed)

  bool mHasAlpha;           /// If true, the image has transparency.
};

#endif

} // namespace image
} // namespace mozilla

#endif // mozilla_image_DownscalingFilter_h