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"
12 #include "mozilla/gfx/2D.h"
22 Downscaler::Downscaler(const nsIntSize
& aTargetSize
)
23 : mTargetSize(aTargetSize
),
24 mOutputBuffer(nullptr),
27 mPrevInvalidatedLine(0),
31 mFlipVertically(false) {
32 MOZ_ASSERT(mTargetSize
.width
> 0 && mTargetSize
.height
> 0,
33 "Invalid target size");
36 Downscaler::~Downscaler() { ReleaseWindow(); }
38 void Downscaler::ReleaseWindow() {
43 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
51 nsresult
Downscaler::BeginFrame(const nsIntSize
& aOriginalSize
,
52 const Maybe
<nsIntRect
>& aFrameRect
,
53 uint8_t* aOutputBuffer
, bool aHasAlpha
,
54 bool aFlipVertically
/* = false */) {
55 MOZ_ASSERT(aOutputBuffer
);
56 MOZ_ASSERT(mTargetSize
!= aOriginalSize
,
57 "Created a downscaler, but not downscaling?");
58 MOZ_ASSERT(mTargetSize
.width
<= aOriginalSize
.width
,
59 "Created a downscaler, but width is larger");
60 MOZ_ASSERT(mTargetSize
.height
<= aOriginalSize
.height
,
61 "Created a downscaler, but height is larger");
62 MOZ_ASSERT(aOriginalSize
.width
> 0 && aOriginalSize
.height
> 0,
63 "Invalid original size");
65 // Only downscale from reasonable sizes to avoid using too much memory/cpu
66 // downscaling and decoding. 1 << 20 == 1,048,576 seems a reasonable limit.
67 if (aOriginalSize
.width
> (1 << 20) || aOriginalSize
.height
> (1 << 20)) {
68 NS_WARNING("Trying to downscale image frame that is too large");
69 return NS_ERROR_INVALID_ARG
;
72 mFrameRect
= aFrameRect
.valueOr(nsIntRect(nsIntPoint(), aOriginalSize
));
73 MOZ_ASSERT(mFrameRect
.X() >= 0 && mFrameRect
.Y() >= 0 &&
74 mFrameRect
.Width() >= 0 && mFrameRect
.Height() >= 0,
75 "Frame rect must have non-negative components");
76 MOZ_ASSERT(nsIntRect(0, 0, aOriginalSize
.width
, aOriginalSize
.height
)
77 .Contains(mFrameRect
),
78 "Frame rect must fit inside image");
79 MOZ_ASSERT_IF(!nsIntRect(0, 0, aOriginalSize
.width
, aOriginalSize
.height
)
80 .IsEqualEdges(mFrameRect
),
83 mOriginalSize
= aOriginalSize
;
84 mScale
= gfx::MatrixScalesDouble(
85 double(mOriginalSize
.width
) / mTargetSize
.width
,
86 double(mOriginalSize
.height
) / mTargetSize
.height
);
87 mOutputBuffer
= aOutputBuffer
;
88 mHasAlpha
= aHasAlpha
;
89 mFlipVertically
= aFlipVertically
;
93 auto resizeMethod
= gfx::ConvolutionFilter::ResizeMethod::LANCZOS3
;
94 if (!mXFilter
.ComputeResizeFilter(resizeMethod
, mOriginalSize
.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();
144 void Downscaler::SkipToRow(int32_t aRow
) {
145 if (mCurrentInLine
< aRow
) {
149 } while (mCurrentInLine
< aRow
);
153 void Downscaler::ResetForNextProgressivePass() {
154 mPrevInvalidatedLine
= 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);
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,
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
,
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
++],
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
,
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
);
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
.xScale
, mScale
.yScale
);
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
,
262 int32_t currentOutLine
= mFlipVertically
263 ? mTargetSize
.height
- (mCurrentOutLine
+ 1)
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
) {
279 int32_t newFilterOffset
= 0;
280 int32_t newFilterLength
= 0;
281 mYFilter
.GetFilterOffsetAndLength(mCurrentOutLine
, &newFilterOffset
,
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
]);
301 } // namespace mozilla