Bug 1635342 [wpt PR 23400] - [EventTiming] Add a test for auxclick, a=testonly
[gecko.git] / image / Downscaler.cpp
blob17a7351fc04601610ccd930b20ea6aefd17332a1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 #include "Downscaler.h"
9 #include <algorithm>
10 #include <ctime>
12 #include "mozilla/gfx/2D.h"
14 using std::max;
15 using std::swap;
17 namespace mozilla {
19 using gfx::IntRect;
21 namespace image {
23 Downscaler::Downscaler(const nsIntSize& aTargetSize)
24 : mTargetSize(aTargetSize),
25 mOutputBuffer(nullptr),
26 mWindowCapacity(0),
27 mLinesInBuffer(0),
28 mPrevInvalidatedLine(0),
29 mCurrentOutLine(0),
30 mCurrentInLine(0),
31 mHasAlpha(true),
32 mFlipVertically(false) {
33 MOZ_ASSERT(mTargetSize.width > 0 && mTargetSize.height > 0,
34 "Invalid target size");
37 Downscaler::~Downscaler() { ReleaseWindow(); }
39 void Downscaler::ReleaseWindow() {
40 if (!mWindow) {
41 return;
44 for (int32_t i = 0; i < mWindowCapacity; ++i) {
45 delete[] mWindow[i];
48 mWindow = nullptr;
49 mWindowCapacity = 0;
52 nsresult Downscaler::BeginFrame(const nsIntSize& aOriginalSize,
53 const Maybe<nsIntRect>& aFrameRect,
54 uint8_t* aOutputBuffer, bool aHasAlpha,
55 bool aFlipVertically /* = false */) {
56 MOZ_ASSERT(aOutputBuffer);
57 MOZ_ASSERT(mTargetSize != aOriginalSize,
58 "Created a downscaler, but not downscaling?");
59 MOZ_ASSERT(mTargetSize.width <= aOriginalSize.width,
60 "Created a downscaler, but width is larger");
61 MOZ_ASSERT(mTargetSize.height <= aOriginalSize.height,
62 "Created a downscaler, but height is larger");
63 MOZ_ASSERT(aOriginalSize.width > 0 && aOriginalSize.height > 0,
64 "Invalid original size");
66 // Only downscale from reasonable sizes to avoid using too much memory/cpu
67 // downscaling and decoding. 1 << 20 == 1,048,576 seems a reasonable limit.
68 if (aOriginalSize.width > (1 << 20) || aOriginalSize.height > (1 << 20)) {
69 NS_WARNING("Trying to downscale image frame that is too large");
70 return NS_ERROR_INVALID_ARG;
73 mFrameRect = aFrameRect.valueOr(nsIntRect(nsIntPoint(), aOriginalSize));
74 MOZ_ASSERT(mFrameRect.X() >= 0 && mFrameRect.Y() >= 0 &&
75 mFrameRect.Width() >= 0 && mFrameRect.Height() >= 0,
76 "Frame rect must have non-negative components");
77 MOZ_ASSERT(nsIntRect(0, 0, aOriginalSize.width, aOriginalSize.height)
78 .Contains(mFrameRect),
79 "Frame rect must fit inside image");
80 MOZ_ASSERT_IF(!nsIntRect(0, 0, aOriginalSize.width, aOriginalSize.height)
81 .IsEqualEdges(mFrameRect),
82 aHasAlpha);
84 mOriginalSize = aOriginalSize;
85 mScale = gfxSize(double(mOriginalSize.width) / mTargetSize.width,
86 double(mOriginalSize.height) / mTargetSize.height);
87 mOutputBuffer = aOutputBuffer;
88 mHasAlpha = aHasAlpha;
89 mFlipVertically = aFlipVertically;
91 ReleaseWindow();
93 auto resizeMethod = gfx::ConvolutionFilter::ResizeMethod::LANCZOS3;
94 if (!mXFilter.ComputeResizeFilter(resizeMethod, mOriginalSize.width,
95 mTargetSize.width) ||
96 !mYFilter.ComputeResizeFilter(resizeMethod, mOriginalSize.height,
97 mTargetSize.height)) {
98 NS_WARNING("Failed to compute filters for image downscaling");
99 return NS_ERROR_OUT_OF_MEMORY;
102 // Allocate the buffer, which contains scanlines of the original image.
103 // pad to handle overreads by the simd code
104 size_t bufferLen = gfx::ConvolutionFilter::PadBytesForSIMD(
105 mOriginalSize.width * sizeof(uint32_t));
106 mRowBuffer.reset(new (fallible) uint8_t[bufferLen]);
107 if (MOZ_UNLIKELY(!mRowBuffer)) {
108 return NS_ERROR_OUT_OF_MEMORY;
111 // Zero buffer to keep valgrind happy.
112 memset(mRowBuffer.get(), 0, bufferLen);
114 // Allocate the window, which contains horizontally downscaled scanlines. (We
115 // can store scanlines which are already downscale because our downscaling
116 // filter is separable.)
117 mWindowCapacity = mYFilter.MaxFilter();
118 mWindow.reset(new (fallible) uint8_t*[mWindowCapacity]);
119 if (MOZ_UNLIKELY(!mWindow)) {
120 return NS_ERROR_OUT_OF_MEMORY;
123 bool anyAllocationFailed = false;
124 // pad to handle overreads by the simd code
125 const size_t rowSize = gfx::ConvolutionFilter::PadBytesForSIMD(
126 mTargetSize.width * sizeof(uint32_t));
127 for (int32_t i = 0; i < mWindowCapacity; ++i) {
128 mWindow[i] = new (fallible) uint8_t[rowSize];
129 anyAllocationFailed = anyAllocationFailed || mWindow[i] == nullptr;
132 if (MOZ_UNLIKELY(anyAllocationFailed)) {
133 // We intentionally iterate through the entire array even if an allocation
134 // fails, to ensure that all the pointers in it are either valid or nullptr.
135 // That in turn ensures that ReleaseWindow() can clean up correctly.
136 return NS_ERROR_OUT_OF_MEMORY;
139 ResetForNextProgressivePass();
141 return NS_OK;
144 void Downscaler::SkipToRow(int32_t aRow) {
145 if (mCurrentInLine < aRow) {
146 ClearRow();
147 do {
148 CommitRow();
149 } while (mCurrentInLine < aRow);
153 void Downscaler::ResetForNextProgressivePass() {
154 mPrevInvalidatedLine = 0;
155 mCurrentOutLine = 0;
156 mCurrentInLine = 0;
157 mLinesInBuffer = 0;
159 if (mFrameRect.IsEmpty()) {
160 // Our frame rect is zero size; commit rows until the end of the image.
161 SkipToRow(mOriginalSize.height - 1);
162 } else {
163 // If we have a vertical offset, commit rows to shift us past it.
164 SkipToRow(mFrameRect.Y());
168 void Downscaler::ClearRestOfRow(uint32_t aStartingAtCol) {
169 MOZ_ASSERT(int64_t(aStartingAtCol) <= int64_t(mOriginalSize.width));
170 uint32_t bytesToClear =
171 (mOriginalSize.width - aStartingAtCol) * sizeof(uint32_t);
172 memset(mRowBuffer.get() + (aStartingAtCol * sizeof(uint32_t)), 0,
173 bytesToClear);
176 void Downscaler::CommitRow() {
177 MOZ_ASSERT(mOutputBuffer, "Should have a current frame");
178 MOZ_ASSERT(mCurrentInLine < mOriginalSize.height, "Past end of input");
180 if (mCurrentOutLine < mTargetSize.height) {
181 int32_t filterOffset = 0;
182 int32_t filterLength = 0;
183 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
184 &filterLength);
186 int32_t inLineToRead = filterOffset + mLinesInBuffer;
187 MOZ_ASSERT(mCurrentInLine <= inLineToRead, "Reading past end of input");
188 if (mCurrentInLine == inLineToRead) {
189 MOZ_RELEASE_ASSERT(mLinesInBuffer < mWindowCapacity,
190 "Need more rows than capacity!");
191 mXFilter.ConvolveHorizontally(mRowBuffer.get(), mWindow[mLinesInBuffer++],
192 mHasAlpha);
195 MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
196 "Writing past end of output");
198 while (mLinesInBuffer >= filterLength) {
199 DownscaleInputLine();
201 if (mCurrentOutLine == mTargetSize.height) {
202 break; // We're done.
205 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
206 &filterLength);
210 mCurrentInLine += 1;
212 // If we're at the end of the part of the original image that has data, commit
213 // rows to shift us to the end.
214 if (mCurrentInLine == (mFrameRect.Y() + mFrameRect.Height())) {
215 SkipToRow(mOriginalSize.height - 1);
219 bool Downscaler::HasInvalidation() const {
220 return mCurrentOutLine > mPrevInvalidatedLine;
223 DownscalerInvalidRect Downscaler::TakeInvalidRect() {
224 if (MOZ_UNLIKELY(!HasInvalidation())) {
225 return DownscalerInvalidRect();
228 DownscalerInvalidRect invalidRect;
230 // Compute the target size invalid rect.
231 if (mFlipVertically) {
232 // We need to flip it. This will implicitly flip the original size invalid
233 // rect, since we compute it by scaling this rect.
234 invalidRect.mTargetSizeRect =
235 IntRect(0, mTargetSize.height - mCurrentOutLine, mTargetSize.width,
236 mCurrentOutLine - mPrevInvalidatedLine);
237 } else {
238 invalidRect.mTargetSizeRect =
239 IntRect(0, mPrevInvalidatedLine, mTargetSize.width,
240 mCurrentOutLine - mPrevInvalidatedLine);
243 mPrevInvalidatedLine = mCurrentOutLine;
245 // Compute the original size invalid rect.
246 invalidRect.mOriginalSizeRect = invalidRect.mTargetSizeRect;
247 invalidRect.mOriginalSizeRect.ScaleRoundOut(mScale.width, mScale.height);
249 return invalidRect;
252 void Downscaler::DownscaleInputLine() {
253 MOZ_ASSERT(mOutputBuffer);
254 MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
255 "Writing past end of output");
257 int32_t filterOffset = 0;
258 int32_t filterLength = 0;
259 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
260 &filterLength);
262 int32_t currentOutLine = mFlipVertically
263 ? mTargetSize.height - (mCurrentOutLine + 1)
264 : mCurrentOutLine;
265 MOZ_ASSERT(currentOutLine >= 0);
267 uint8_t* outputLine =
268 &mOutputBuffer[currentOutLine * mTargetSize.width * sizeof(uint32_t)];
269 mYFilter.ConvolveVertically(mWindow.get(), outputLine, mCurrentOutLine,
270 mXFilter.NumValues(), mHasAlpha);
272 mCurrentOutLine += 1;
274 if (mCurrentOutLine == mTargetSize.height) {
275 // We're done.
276 return;
279 int32_t newFilterOffset = 0;
280 int32_t newFilterLength = 0;
281 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &newFilterOffset,
282 &newFilterLength);
284 int diff = newFilterOffset - filterOffset;
285 MOZ_ASSERT(diff >= 0, "Moving backwards in the filter?");
287 // Shift the buffer. We're just moving pointers here, so this is cheap.
288 mLinesInBuffer -= diff;
289 mLinesInBuffer = std::min(std::max(mLinesInBuffer, 0), mWindowCapacity);
291 // If we already have enough rows to satisfy the filter, there is no need
292 // to swap as we won't be writing more before the next convolution.
293 if (filterLength > mLinesInBuffer) {
294 for (int32_t i = 0; i < mLinesInBuffer; ++i) {
295 swap(mWindow[i], mWindow[filterLength - mLinesInBuffer + i]);
300 } // namespace image
301 } // namespace mozilla