Bug 1668452 require an exact match of Window, rate, and device when selecting a Media...
[gecko.git] / image / DownscalingFilter.h
blob4f1f071e0438f3b5a5b472bfb7e03bd681fcf89d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * DownscalingSurfaceFilter is a SurfaceFilter implementation for use with
9 * SurfacePipe which performs Lanczos downscaling.
11 * It's in this header file, separated from the other SurfaceFilters, because
12 * some preprocessor magic is necessary to ensure that there aren't compilation
13 * issues on platforms where Skia is unavailable.
16 #ifndef mozilla_image_DownscalingFilter_h
17 #define mozilla_image_DownscalingFilter_h
19 #include <algorithm>
20 #include <ctime>
21 #include <stdint.h>
23 #include "mozilla/Maybe.h"
24 #include "mozilla/UniquePtr.h"
25 #include "mozilla/gfx/2D.h"
27 #include "mozilla/gfx/ConvolutionFilter.h"
29 #include "SurfacePipe.h"
31 namespace mozilla {
32 namespace image {
34 //////////////////////////////////////////////////////////////////////////////
35 // DownscalingFilter
36 //////////////////////////////////////////////////////////////////////////////
38 template <typename Next>
39 class DownscalingFilter;
41 /**
42 * A configuration struct for DownscalingConfig.
44 struct DownscalingConfig {
45 template <typename Next>
46 using Filter = DownscalingFilter<Next>;
47 gfx::IntSize mInputSize; /// The size of the input image. We'll downscale
48 /// from this size to the input size of the next
49 /// SurfaceFilter in the chain.
50 gfx::SurfaceFormat mFormat; /// The pixel format - BGRA or BGRX. (BGRX has
51 /// slightly better performance.)
54 /**
55 * DownscalingFilter performs Lanczos downscaling, taking image input data at
56 * one size and outputting it rescaled to a different size.
58 * The 'Next' template parameter specifies the next filter in the chain.
60 template <typename Next>
61 class DownscalingFilter final : public SurfaceFilter {
62 public:
63 DownscalingFilter()
64 : mWindowCapacity(0),
65 mRowsInWindow(0),
66 mInputRow(0),
67 mOutputRow(0),
68 mHasAlpha(true) {}
70 ~DownscalingFilter() { ReleaseWindow(); }
72 template <typename... Rest>
73 nsresult Configure(const DownscalingConfig& aConfig, const Rest&... aRest) {
74 nsresult rv = mNext.Configure(aRest...);
75 if (NS_FAILED(rv)) {
76 return rv;
79 if (mNext.InputSize() == aConfig.mInputSize) {
80 NS_WARNING("Created a downscaler, but not downscaling?");
81 return NS_ERROR_INVALID_ARG;
83 if (mNext.InputSize().width > aConfig.mInputSize.width) {
84 NS_WARNING("Created a downscaler, but width is larger");
85 return NS_ERROR_INVALID_ARG;
87 if (mNext.InputSize().height > aConfig.mInputSize.height) {
88 NS_WARNING("Created a downscaler, but height is larger");
89 return NS_ERROR_INVALID_ARG;
91 if (aConfig.mInputSize.width <= 0 || aConfig.mInputSize.height <= 0) {
92 NS_WARNING("Invalid input size for DownscalingFilter");
93 return NS_ERROR_INVALID_ARG;
96 mInputSize = aConfig.mInputSize;
97 gfx::IntSize outputSize = mNext.InputSize();
98 mScale = gfxSize(double(mInputSize.width) / outputSize.width,
99 double(mInputSize.height) / outputSize.height);
100 mHasAlpha = aConfig.mFormat == gfx::SurfaceFormat::OS_RGBA;
102 ReleaseWindow();
104 auto resizeMethod = gfx::ConvolutionFilter::ResizeMethod::LANCZOS3;
105 if (!mXFilter.ComputeResizeFilter(resizeMethod, mInputSize.width,
106 outputSize.width) ||
107 !mYFilter.ComputeResizeFilter(resizeMethod, mInputSize.height,
108 outputSize.height)) {
109 NS_WARNING("Failed to compute filters for image downscaling");
110 return NS_ERROR_OUT_OF_MEMORY;
113 // Allocate the buffer, which contains scanlines of the input image.
114 mRowBuffer.reset(new (fallible)
115 uint8_t[PaddedWidthInBytes(mInputSize.width)]);
116 if (MOZ_UNLIKELY(!mRowBuffer)) {
117 return NS_ERROR_OUT_OF_MEMORY;
120 // Clear the buffer to avoid writing uninitialized memory to the output.
121 memset(mRowBuffer.get(), 0, PaddedWidthInBytes(mInputSize.width));
123 // Allocate the window, which contains horizontally downscaled scanlines.
124 // (We can store scanlines which are already downscaled because our
125 // downscaling filter is separable.)
126 mWindowCapacity = mYFilter.MaxFilter();
127 mWindow.reset(new (fallible) uint8_t*[mWindowCapacity]);
128 if (MOZ_UNLIKELY(!mWindow)) {
129 return NS_ERROR_OUT_OF_MEMORY;
132 // Allocate the "window" of recent rows that we keep in memory as input for
133 // the downscaling code. We intentionally iterate through the entire array
134 // even if an allocation fails, to ensure that all the pointers in it are
135 // either valid or nullptr. That in turn ensures that ReleaseWindow() can
136 // clean up correctly.
137 bool anyAllocationFailed = false;
138 const size_t windowRowSizeInBytes = PaddedWidthInBytes(outputSize.width);
139 for (int32_t i = 0; i < mWindowCapacity; ++i) {
140 mWindow[i] = new (fallible) uint8_t[windowRowSizeInBytes];
141 anyAllocationFailed = anyAllocationFailed || mWindow[i] == nullptr;
144 if (MOZ_UNLIKELY(anyAllocationFailed)) {
145 return NS_ERROR_OUT_OF_MEMORY;
148 ConfigureFilter(mInputSize, sizeof(uint32_t));
149 return NS_OK;
152 Maybe<SurfaceInvalidRect> TakeInvalidRect() override {
153 Maybe<SurfaceInvalidRect> invalidRect = mNext.TakeInvalidRect();
155 if (invalidRect) {
156 // Compute the input space invalid rect by scaling.
157 invalidRect->mInputSpaceRect.ScaleRoundOut(mScale.width, mScale.height);
160 return invalidRect;
163 protected:
164 uint8_t* DoResetToFirstRow() override {
165 mNext.ResetToFirstRow();
167 mInputRow = 0;
168 mOutputRow = 0;
169 mRowsInWindow = 0;
171 return GetRowPointer();
174 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) override {
175 if (mInputRow >= mInputSize.height) {
176 NS_WARNING("Advancing DownscalingFilter past the end of the input");
177 return nullptr;
180 if (mOutputRow >= mNext.InputSize().height) {
181 NS_WARNING("Advancing DownscalingFilter past the end of the output");
182 return nullptr;
185 int32_t filterOffset = 0;
186 int32_t filterLength = 0;
187 mYFilter.GetFilterOffsetAndLength(mOutputRow, &filterOffset, &filterLength);
189 int32_t inputRowToRead = filterOffset + mRowsInWindow;
190 MOZ_ASSERT(mInputRow <= inputRowToRead, "Reading past end of input");
191 if (mInputRow == inputRowToRead) {
192 MOZ_RELEASE_ASSERT(mRowsInWindow < mWindowCapacity,
193 "Need more rows than capacity!");
194 mXFilter.ConvolveHorizontally(aInputRow, mWindow[mRowsInWindow++],
195 mHasAlpha);
198 MOZ_ASSERT(mOutputRow < mNext.InputSize().height,
199 "Writing past end of output");
201 while (mRowsInWindow >= filterLength) {
202 DownscaleInputRow();
204 if (mOutputRow == mNext.InputSize().height) {
205 break; // We're done.
208 mYFilter.GetFilterOffsetAndLength(mOutputRow, &filterOffset,
209 &filterLength);
212 mInputRow++;
214 return mInputRow < mInputSize.height ? GetRowPointer() : nullptr;
217 uint8_t* DoAdvanceRow() override {
218 return DoAdvanceRowFromBuffer(mRowBuffer.get());
221 private:
222 uint8_t* GetRowPointer() const { return mRowBuffer.get(); }
224 static size_t PaddedWidthInBytes(size_t aLogicalWidth) {
225 // Convert from width in BGRA/BGRX pixels to width in bytes, padding
226 // to handle overreads by the SIMD code inside Skia.
227 return gfx::ConvolutionFilter::PadBytesForSIMD(aLogicalWidth *
228 sizeof(uint32_t));
231 void DownscaleInputRow() {
232 MOZ_ASSERT(mOutputRow < mNext.InputSize().height,
233 "Writing past end of output");
235 int32_t filterOffset = 0;
236 int32_t filterLength = 0;
237 mYFilter.GetFilterOffsetAndLength(mOutputRow, &filterOffset, &filterLength);
239 mNext.template WriteUnsafeComputedRow<uint32_t>([&](uint32_t* aRow,
240 uint32_t aLength) {
241 mYFilter.ConvolveVertically(mWindow.get(),
242 reinterpret_cast<uint8_t*>(aRow), mOutputRow,
243 mXFilter.NumValues(), mHasAlpha);
246 mOutputRow++;
248 if (mOutputRow == mNext.InputSize().height) {
249 return; // We're done.
252 int32_t newFilterOffset = 0;
253 int32_t newFilterLength = 0;
254 mYFilter.GetFilterOffsetAndLength(mOutputRow, &newFilterOffset,
255 &newFilterLength);
257 int diff = newFilterOffset - filterOffset;
258 MOZ_ASSERT(diff >= 0, "Moving backwards in the filter?");
260 // Shift the buffer. We're just moving pointers here, so this is cheap.
261 mRowsInWindow -= diff;
262 mRowsInWindow = std::min(std::max(mRowsInWindow, 0), mWindowCapacity);
264 // If we already have enough rows to satisfy the filter, there is no need
265 // to swap as we won't be writing more before the next convolution.
266 if (filterLength > mRowsInWindow) {
267 for (int32_t i = 0; i < mRowsInWindow; ++i) {
268 std::swap(mWindow[i], mWindow[filterLength - mRowsInWindow + i]);
273 void ReleaseWindow() {
274 if (!mWindow) {
275 return;
278 for (int32_t i = 0; i < mWindowCapacity; ++i) {
279 delete[] mWindow[i];
282 mWindow = nullptr;
283 mWindowCapacity = 0;
286 Next mNext; /// The next SurfaceFilter in the chain.
288 gfx::IntSize mInputSize; /// The size of the input image.
289 gfxSize mScale; /// The scale factors in each dimension.
290 /// Computed from @mInputSize and
291 /// the next filter's input size.
293 UniquePtr<uint8_t[]> mRowBuffer; /// The buffer into which input is written.
294 UniquePtr<uint8_t*[]> mWindow; /// The last few rows which were written.
296 gfx::ConvolutionFilter mXFilter; /// The Lanczos filter in X.
297 gfx::ConvolutionFilter mYFilter; /// The Lanczos filter in Y.
299 int32_t mWindowCapacity; /// How many rows the window contains.
301 int32_t mRowsInWindow; /// How many rows we've buffered in the window.
302 int32_t mInputRow; /// The current row we're reading. (0-indexed)
303 int32_t mOutputRow; /// The current row we're writing. (0-indexed)
305 bool mHasAlpha; /// If true, the image has transparency.
308 } // namespace image
309 } // namespace mozilla
311 #endif // mozilla_image_DownscalingFilter_h