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/. */
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
23 #include "mozilla/Maybe.h"
24 #include "mozilla/UniquePtr.h"
25 #include "mozilla/gfx/2D.h"
28 #ifdef MOZ_ENABLE_SKIA
29 # include "mozilla/gfx/ConvolutionFilter.h"
32 #include "SurfacePipe.h"
37 //////////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////////
41 template <typename Next
>
42 class DownscalingFilter
;
45 * A configuration struct for DownscalingConfig.
47 struct DownscalingConfig
{
48 template <typename Next
>
49 using Filter
= DownscalingFilter
<Next
>;
50 gfx::IntSize mInputSize
; /// The size of the input image. We'll downscale
51 /// from this size to the input size of the next
52 /// SurfaceFilter in the chain.
53 gfx::SurfaceFormat mFormat
; /// The pixel format - BGRA or BGRX. (BGRX has
54 /// slightly better performance.)
57 #ifndef MOZ_ENABLE_SKIA
60 * DownscalingFilter requires Skia. This is a fallback implementation for
61 * non-Skia builds that fails when Configure() is called (which will prevent
62 * SurfacePipeFactory from returning an instance of it) and crashes if a caller
63 * manually constructs an instance and attempts to actually use it. Callers
64 * should avoid this by ensuring that they do not request downscaling in
67 template <typename Next
>
68 class DownscalingFilter final
: public SurfaceFilter
{
70 Maybe
<SurfaceInvalidRect
> TakeInvalidRect() override
{ return Nothing(); }
72 template <typename
... Rest
>
73 nsresult
Configure(const DownscalingConfig
& aConfig
, const Rest
&... aRest
) {
74 return NS_ERROR_FAILURE
;
78 uint8_t* DoResetToFirstRow() override
{
82 uint8_t* DoAdvanceRow() override
{
91 * DownscalingFilter performs Lanczos downscaling, taking image input data at
92 * one size and outputting it rescaled to a different size.
94 * The 'Next' template parameter specifies the next filter in the chain.
96 template <typename Next
>
97 class DownscalingFilter final
: public SurfaceFilter
{
100 : mWindowCapacity(0),
106 ~DownscalingFilter() { ReleaseWindow(); }
108 template <typename
... Rest
>
109 nsresult
Configure(const DownscalingConfig
& aConfig
, const Rest
&... aRest
) {
110 nsresult rv
= mNext
.Configure(aRest
...);
115 if (mNext
.IsValidPalettedPipe()) {
116 NS_WARNING("Created a downscaler for a paletted surface?");
117 return NS_ERROR_INVALID_ARG
;
119 if (mNext
.InputSize() == aConfig
.mInputSize
) {
120 NS_WARNING("Created a downscaler, but not downscaling?");
121 return NS_ERROR_INVALID_ARG
;
123 if (mNext
.InputSize().width
> aConfig
.mInputSize
.width
) {
124 NS_WARNING("Created a downscaler, but width is larger");
125 return NS_ERROR_INVALID_ARG
;
127 if (mNext
.InputSize().height
> aConfig
.mInputSize
.height
) {
128 NS_WARNING("Created a downscaler, but height is larger");
129 return NS_ERROR_INVALID_ARG
;
131 if (aConfig
.mInputSize
.width
<= 0 || aConfig
.mInputSize
.height
<= 0) {
132 NS_WARNING("Invalid input size for DownscalingFilter");
133 return NS_ERROR_INVALID_ARG
;
136 mInputSize
= aConfig
.mInputSize
;
137 gfx::IntSize outputSize
= mNext
.InputSize();
138 mScale
= gfxSize(double(mInputSize
.width
) / outputSize
.width
,
139 double(mInputSize
.height
) / outputSize
.height
);
140 mHasAlpha
= aConfig
.mFormat
== gfx::SurfaceFormat::B8G8R8A8
;
144 auto resizeMethod
= gfx::ConvolutionFilter::ResizeMethod::LANCZOS3
;
145 if (!mXFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.width
,
147 !mYFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.height
,
148 outputSize
.height
)) {
149 NS_WARNING("Failed to compute filters for image downscaling");
150 return NS_ERROR_OUT_OF_MEMORY
;
153 // Allocate the buffer, which contains scanlines of the input image.
154 mRowBuffer
.reset(new (fallible
)
155 uint8_t[PaddedWidthInBytes(mInputSize
.width
)]);
156 if (MOZ_UNLIKELY(!mRowBuffer
)) {
157 return NS_ERROR_OUT_OF_MEMORY
;
160 // Clear the buffer to avoid writing uninitialized memory to the output.
161 memset(mRowBuffer
.get(), 0, PaddedWidthInBytes(mInputSize
.width
));
163 // Allocate the window, which contains horizontally downscaled scanlines.
164 // (We can store scanlines which are already downscaled because our
165 // downscaling filter is separable.)
166 mWindowCapacity
= mYFilter
.MaxFilter();
167 mWindow
.reset(new (fallible
) uint8_t*[mWindowCapacity
]);
168 if (MOZ_UNLIKELY(!mWindow
)) {
169 return NS_ERROR_OUT_OF_MEMORY
;
172 // Allocate the "window" of recent rows that we keep in memory as input for
173 // the downscaling code. We intentionally iterate through the entire array
174 // even if an allocation fails, to ensure that all the pointers in it are
175 // either valid or nullptr. That in turn ensures that ReleaseWindow() can
176 // clean up correctly.
177 bool anyAllocationFailed
= false;
178 const size_t windowRowSizeInBytes
= PaddedWidthInBytes(outputSize
.width
);
179 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
180 mWindow
[i
] = new (fallible
) uint8_t[windowRowSizeInBytes
];
181 anyAllocationFailed
= anyAllocationFailed
|| mWindow
[i
] == nullptr;
184 if (MOZ_UNLIKELY(anyAllocationFailed
)) {
185 return NS_ERROR_OUT_OF_MEMORY
;
188 ConfigureFilter(mInputSize
, sizeof(uint32_t));
192 Maybe
<SurfaceInvalidRect
> TakeInvalidRect() override
{
193 Maybe
<SurfaceInvalidRect
> invalidRect
= mNext
.TakeInvalidRect();
196 // Compute the input space invalid rect by scaling.
197 invalidRect
->mInputSpaceRect
.ScaleRoundOut(mScale
.width
, mScale
.height
);
204 uint8_t* DoResetToFirstRow() override
{
205 mNext
.ResetToFirstRow();
211 return GetRowPointer();
214 uint8_t* DoAdvanceRow() override
{
215 if (mInputRow
>= mInputSize
.height
) {
216 NS_WARNING("Advancing DownscalingFilter past the end of the input");
220 if (mOutputRow
>= mNext
.InputSize().height
) {
221 NS_WARNING("Advancing DownscalingFilter past the end of the output");
225 int32_t filterOffset
= 0;
226 int32_t filterLength
= 0;
227 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
229 int32_t inputRowToRead
= filterOffset
+ mRowsInWindow
;
230 MOZ_ASSERT(mInputRow
<= inputRowToRead
, "Reading past end of input");
231 if (mInputRow
== inputRowToRead
) {
232 MOZ_RELEASE_ASSERT(mRowsInWindow
< mWindowCapacity
,
233 "Need more rows than capacity!");
234 mXFilter
.ConvolveHorizontally(mRowBuffer
.get(), mWindow
[mRowsInWindow
++],
238 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
239 "Writing past end of output");
241 while (mRowsInWindow
>= filterLength
) {
244 if (mOutputRow
== mNext
.InputSize().height
) {
245 break; // We're done.
248 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
,
254 return mInputRow
< mInputSize
.height
? GetRowPointer() : nullptr;
258 uint8_t* GetRowPointer() const { return mRowBuffer
.get(); }
260 static size_t PaddedWidthInBytes(size_t aLogicalWidth
) {
261 // Convert from width in BGRA/BGRX pixels to width in bytes, padding
262 // to handle overreads by the SIMD code inside Skia.
263 return gfx::ConvolutionFilter::PadBytesForSIMD(aLogicalWidth
*
267 void DownscaleInputRow() {
268 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
269 "Writing past end of output");
271 int32_t filterOffset
= 0;
272 int32_t filterLength
= 0;
273 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
275 mNext
.template WriteUnsafeComputedRow
<uint32_t>([&](uint32_t* aRow
,
277 mYFilter
.ConvolveVertically(mWindow
.get(),
278 reinterpret_cast<uint8_t*>(aRow
), mOutputRow
,
279 mXFilter
.NumValues(), mHasAlpha
);
284 if (mOutputRow
== mNext
.InputSize().height
) {
285 return; // We're done.
288 int32_t newFilterOffset
= 0;
289 int32_t newFilterLength
= 0;
290 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &newFilterOffset
,
293 int diff
= newFilterOffset
- filterOffset
;
294 MOZ_ASSERT(diff
>= 0, "Moving backwards in the filter?");
296 // Shift the buffer. We're just moving pointers here, so this is cheap.
297 mRowsInWindow
-= diff
;
298 mRowsInWindow
= std::min(std::max(mRowsInWindow
, 0), mWindowCapacity
);
300 // If we already have enough rows to satisfy the filter, there is no need
301 // to swap as we won't be writing more before the next convolution.
302 if (filterLength
> mRowsInWindow
) {
303 for (int32_t i
= 0; i
< mRowsInWindow
; ++i
) {
304 std::swap(mWindow
[i
], mWindow
[filterLength
- mRowsInWindow
+ i
]);
309 void ReleaseWindow() {
314 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
322 Next mNext
; /// The next SurfaceFilter in the chain.
324 gfx::IntSize mInputSize
; /// The size of the input image.
325 gfxSize mScale
; /// The scale factors in each dimension.
326 /// Computed from @mInputSize and
327 /// the next filter's input size.
329 UniquePtr
<uint8_t[]> mRowBuffer
; /// The buffer into which input is written.
330 UniquePtr
<uint8_t*[]> mWindow
; /// The last few rows which were written.
332 gfx::ConvolutionFilter mXFilter
; /// The Lanczos filter in X.
333 gfx::ConvolutionFilter mYFilter
; /// The Lanczos filter in Y.
335 int32_t mWindowCapacity
; /// How many rows the window contains.
337 int32_t mRowsInWindow
; /// How many rows we've buffered in the window.
338 int32_t mInputRow
; /// The current row we're reading. (0-indexed)
339 int32_t mOutputRow
; /// The current row we're writing. (0-indexed)
341 bool mHasAlpha
; /// If true, the image has transparency.
347 } // namespace mozilla
349 #endif // mozilla_image_DownscalingFilter_h