1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #include "Downscaler.h"
8 
9 #include <algorithm>
10 #include <ctime>
11 
12 #include "mozilla/gfx/2D.h"
13 
14 using std::max;
15 using std::swap;
16 
17 namespace mozilla {
18 
19 using gfx::IntRect;
20 
21 namespace image {
22 
Downscaler(const nsIntSize & aTargetSize)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");
35 }
36 
~Downscaler()37 Downscaler::~Downscaler() { ReleaseWindow(); }
38 
ReleaseWindow()39 void Downscaler::ReleaseWindow() {
40   if (!mWindow) {
41     return;
42   }
43 
44   for (int32_t i = 0; i < mWindowCapacity; ++i) {
45     delete[] mWindow[i];
46   }
47 
48   mWindow = nullptr;
49   mWindowCapacity = 0;
50 }
51 
BeginFrame(const nsIntSize & aOriginalSize,const Maybe<nsIntRect> & aFrameRect,uint8_t * aOutputBuffer,bool aHasAlpha,bool aFlipVertically)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");
65 
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;
71   }
72 
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);
83 
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;
90 
91   ReleaseWindow();
92 
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;
100   }
101 
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;
109   }
110 
111   // Zero buffer to keep valgrind happy.
112   memset(mRowBuffer.get(), 0, bufferLen);
113 
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;
121   }
122 
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;
130   }
131 
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;
137   }
138 
139   ResetForNextProgressivePass();
140 
141   return NS_OK;
142 }
143 
SkipToRow(int32_t aRow)144 void Downscaler::SkipToRow(int32_t aRow) {
145   if (mCurrentInLine < aRow) {
146     ClearRow();
147     do {
148       CommitRow();
149     } while (mCurrentInLine < aRow);
150   }
151 }
152 
ResetForNextProgressivePass()153 void Downscaler::ResetForNextProgressivePass() {
154   mPrevInvalidatedLine = 0;
155   mCurrentOutLine = 0;
156   mCurrentInLine = 0;
157   mLinesInBuffer = 0;
158 
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());
165   }
166 }
167 
ClearRestOfRow(uint32_t aStartingAtCol)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);
174 }
175 
CommitRow()176 void Downscaler::CommitRow() {
177   MOZ_ASSERT(mOutputBuffer, "Should have a current frame");
178   MOZ_ASSERT(mCurrentInLine < mOriginalSize.height, "Past end of input");
179 
180   if (mCurrentOutLine < mTargetSize.height) {
181     int32_t filterOffset = 0;
182     int32_t filterLength = 0;
183     mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
184                                       &filterLength);
185 
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);
193     }
194 
195     MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
196                "Writing past end of output");
197 
198     while (mLinesInBuffer >= filterLength) {
199       DownscaleInputLine();
200 
201       if (mCurrentOutLine == mTargetSize.height) {
202         break;  // We're done.
203       }
204 
205       mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
206                                         &filterLength);
207     }
208   }
209 
210   mCurrentInLine += 1;
211 
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);
216   }
217 }
218 
HasInvalidation() const219 bool Downscaler::HasInvalidation() const {
220   return mCurrentOutLine > mPrevInvalidatedLine;
221 }
222 
TakeInvalidRect()223 DownscalerInvalidRect Downscaler::TakeInvalidRect() {
224   if (MOZ_UNLIKELY(!HasInvalidation())) {
225     return DownscalerInvalidRect();
226   }
227 
228   DownscalerInvalidRect invalidRect;
229 
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);
241   }
242 
243   mPrevInvalidatedLine = mCurrentOutLine;
244 
245   // Compute the original size invalid rect.
246   invalidRect.mOriginalSizeRect = invalidRect.mTargetSizeRect;
247   invalidRect.mOriginalSizeRect.ScaleRoundOut(mScale.width, mScale.height);
248 
249   return invalidRect;
250 }
251 
DownscaleInputLine()252 void Downscaler::DownscaleInputLine() {
253   MOZ_ASSERT(mOutputBuffer);
254   MOZ_ASSERT(mCurrentOutLine < mTargetSize.height,
255              "Writing past end of output");
256 
257   int32_t filterOffset = 0;
258   int32_t filterLength = 0;
259   mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &filterOffset,
260                                     &filterLength);
261 
262   int32_t currentOutLine = mFlipVertically
263                                ? mTargetSize.height - (mCurrentOutLine + 1)
264                                : mCurrentOutLine;
265   MOZ_ASSERT(currentOutLine >= 0);
266 
267   uint8_t* outputLine =
268       &mOutputBuffer[currentOutLine * mTargetSize.width * sizeof(uint32_t)];
269   mYFilter.ConvolveVertically(mWindow.get(), outputLine, mCurrentOutLine,
270                               mXFilter.NumValues(), mHasAlpha);
271 
272   mCurrentOutLine += 1;
273 
274   if (mCurrentOutLine == mTargetSize.height) {
275     // We're done.
276     return;
277   }
278 
279   int32_t newFilterOffset = 0;
280   int32_t newFilterLength = 0;
281   mYFilter.GetFilterOffsetAndLength(mCurrentOutLine, &newFilterOffset,
282                                     &newFilterLength);
283 
284   int diff = newFilterOffset - filterOffset;
285   MOZ_ASSERT(diff >= 0, "Moving backwards in the filter?");
286 
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);
290 
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]);
296     }
297   }
298 }
299 
300 }  // namespace image
301 }  // namespace mozilla
302