Bug 1744524: part 2) Add `WindowContext::GetUserGestureStart` and remove `WindowConte...
[gecko.git] / image / Downscaler.cpp
blob5bf15f1469d15f953075e0a040a7950e4ab20469
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 = gfx::MatrixScalesDouble(
86 double(mOriginalSize.width) / mTargetSize.width,
87 double(mOriginalSize.height) / mTargetSize.height);
88 mOutputBuffer = aOutputBuffer;
89 mHasAlpha = aHasAlpha;
90 mFlipVertically = aFlipVertically;
92 ReleaseWindow();
94 auto resizeMethod = gfx::ConvolutionFilter::ResizeMethod::LANCZOS3;
95 if (!mXFilter.ComputeResizeFilter(resizeMethod, mOriginalSize.width,
96 mTargetSize.width) ||
97 !mYFilter.ComputeResizeFilter(resizeMethod, mOriginalSize.height,
98 mTargetSize.height)) {
99 NS_WARNING("Failed to compute filters for image downscaling");
100 return NS_ERROR_OUT_OF_MEMORY;
103 // Allocate the buffer, which contains scanlines of the original image.
104 // pad to handle overreads by the simd code
105 size_t bufferLen = gfx::ConvolutionFilter::PadBytesForSIMD(
106 mOriginalSize.width * sizeof(uint32_t));
107 mRowBuffer.reset(new (fallible) uint8_t[bufferLen]);
108 if (MOZ_UNLIKELY(!mRowBuffer)) {
109 return NS_ERROR_OUT_OF_MEMORY;
112 // Zero buffer to keep valgrind happy.
113 memset(mRowBuffer.get(), 0, bufferLen);
115 // Allocate the window, which contains horizontally downscaled scanlines. (We
116 // can store scanlines which are already downscale because our downscaling
117 // filter is separable.)
118 mWindowCapacity = mYFilter.MaxFilter();
119 mWindow.reset(new (fallible) uint8_t*[mWindowCapacity]);
120 if (MOZ_UNLIKELY(!mWindow)) {
121 return NS_ERROR_OUT_OF_MEMORY;
124 bool anyAllocationFailed = false;
125 // pad to handle overreads by the simd code
126 const size_t rowSize = gfx::ConvolutionFilter::PadBytesForSIMD(
127 mTargetSize.width * sizeof(uint32_t));
128 for (int32_t i = 0; i < mWindowCapacity; ++i) {
129 mWindow[i] = new (fallible) uint8_t[rowSize];
130 anyAllocationFailed = anyAllocationFailed || mWindow[i] == nullptr;
133 if (MOZ_UNLIKELY(anyAllocationFailed)) {
134 // We intentionally iterate through the entire array even if an allocation
135 // fails, to ensure that all the pointers in it are either valid or nullptr.
136 // That in turn ensures that ReleaseWindow() can clean up correctly.
137 return NS_ERROR_OUT_OF_MEMORY;
140 ResetForNextProgressivePass();
142 return NS_OK;
145 void Downscaler::SkipToRow(int32_t aRow) {
146 if (mCurrentInLine < aRow) {
147 ClearRow();
148 do {
149 CommitRow();
150 } while (mCurrentInLine < aRow);
154 void Downscaler::ResetForNextProgressivePass() {
155 mPrevInvalidatedLine = 0;
156 mCurrentOutLine = 0;
157 mCurrentInLine = 0;
158 mLinesInBuffer = 0;
160 if (mFrameRect.IsEmpty()) {
161 // Our frame rect is zero size; commit rows until the end of the image.
162 SkipToRow(mOriginalSize.height - 1);
163 } else {
164 // If we have a vertical offset, commit rows to shift us past it.
165 SkipToRow(mFrameRect.Y());
169 void Downscaler::ClearRestOfRow(uint32_t aStartingAtCol) {
170 MOZ_ASSERT(int64_t(aStartingAtCol) <= int64_t(mOriginalSize.width));
171 uint32_t bytesToClear =
172 (mOriginalSize.width - aStartingAtCol) * sizeof(uint32_t);
173 memset(mRowBuffer.get() + (aStartingAtCol * sizeof(uint32_t)), 0,
174 bytesToClear);
177 void Downscaler::CommitRow() {
178 MOZ_ASSERT(mOutputBuffer, "Should have a current frame");
179 MOZ_ASSERT(mCurrentInLine < mOriginalSize.height, "Past end of input");
181 if (mCurrentOutLine < mTargetSize.height) {
182 int32_t filterOffset = 0;
183 int32_t filterLength = 0;
184 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
185 &filterLength);
187 int32_t inLineToRead = filterOffset + mLinesInBuffer;
188 MOZ_ASSERT(mCurrentInLine <= inLineToRead, "Reading past end of input");
189 if (mCurrentInLine == inLineToRead) {
190 MOZ_RELEASE_ASSERT(mLinesInBuffer < mWindowCapacity,
191 "Need more rows than capacity!");
192 mXFilter.ConvolveHorizontally(mRowBuffer.get(), mWindow[mLinesInBuffer++],
193 mHasAlpha);
196 MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
197 "Writing past end of output");
199 while (mLinesInBuffer >= filterLength) {
200 DownscaleInputLine();
202 if (mCurrentOutLine == mTargetSize.height) {
203 break; // We're done.
206 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
207 &filterLength);
211 mCurrentInLine += 1;
213 // If we're at the end of the part of the original image that has data, commit
214 // rows to shift us to the end.
215 if (mCurrentInLine == (mFrameRect.Y() + mFrameRect.Height())) {
216 SkipToRow(mOriginalSize.height - 1);
220 bool Downscaler::HasInvalidation() const {
221 return mCurrentOutLine > mPrevInvalidatedLine;
224 DownscalerInvalidRect Downscaler::TakeInvalidRect() {
225 if (MOZ_UNLIKELY(!HasInvalidation())) {
226 return DownscalerInvalidRect();
229 DownscalerInvalidRect invalidRect;
231 // Compute the target size invalid rect.
232 if (mFlipVertically) {
233 // We need to flip it. This will implicitly flip the original size invalid
234 // rect, since we compute it by scaling this rect.
235 invalidRect.mTargetSizeRect =
236 IntRect(0, mTargetSize.height - mCurrentOutLine, mTargetSize.width,
237 mCurrentOutLine - mPrevInvalidatedLine);
238 } else {
239 invalidRect.mTargetSizeRect =
240 IntRect(0, mPrevInvalidatedLine, mTargetSize.width,
241 mCurrentOutLine - mPrevInvalidatedLine);
244 mPrevInvalidatedLine = mCurrentOutLine;
246 // Compute the original size invalid rect.
247 invalidRect.mOriginalSizeRect = invalidRect.mTargetSizeRect;
248 invalidRect.mOriginalSizeRect.ScaleRoundOut(mScale.xScale, mScale.yScale);
250 return invalidRect;
253 void Downscaler::DownscaleInputLine() {
254 MOZ_ASSERT(mOutputBuffer);
255 MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
256 "Writing past end of output");
258 int32_t filterOffset = 0;
259 int32_t filterLength = 0;
260 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
261 &filterLength);
263 int32_t currentOutLine = mFlipVertically
264 ? mTargetSize.height - (mCurrentOutLine + 1)
265 : mCurrentOutLine;
266 MOZ_ASSERT(currentOutLine >= 0);
268 uint8_t* outputLine =
269 &mOutputBuffer[currentOutLine * mTargetSize.width * sizeof(uint32_t)];
270 mYFilter.ConvolveVertically(mWindow.get(), outputLine, mCurrentOutLine,
271 mXFilter.NumValues(), mHasAlpha);
273 mCurrentOutLine += 1;
275 if (mCurrentOutLine == mTargetSize.height) {
276 // We're done.
277 return;
280 int32_t newFilterOffset = 0;
281 int32_t newFilterLength = 0;
282 mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &newFilterOffset,
283 &newFilterLength);
285 int diff = newFilterOffset - filterOffset;
286 MOZ_ASSERT(diff >= 0, "Moving backwards in the filter?");
288 // Shift the buffer. We're just moving pointers here, so this is cheap.
289 mLinesInBuffer -= diff;
290 mLinesInBuffer = std::min(std::max(mLinesInBuffer, 0), mWindowCapacity);
292 // If we already have enough rows to satisfy the filter, there is no need
293 // to swap as we won't be writing more before the next convolution.
294 if (filterLength > mLinesInBuffer) {
295 for (int32_t i = 0; i < mLinesInBuffer; ++i) {
296 swap(mWindow[i], mWindow[filterLength - mLinesInBuffer + i]);
301 } // namespace image
302 } // namespace mozilla