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"
27 #ifdef MOZ_ENABLE_SKIA
28 # include "mozilla/gfx/ConvolutionFilter.h"
31 #include "SurfacePipe.h"
36 //////////////////////////////////////////////////////////////////////////////
38 //////////////////////////////////////////////////////////////////////////////
40 template <typename Next
>
41 class DownscalingFilter
;
44 * A configuration struct for DownscalingConfig.
46 struct DownscalingConfig
{
47 template <typename Next
>
48 using Filter
= DownscalingFilter
<Next
>;
49 gfx::IntSize mInputSize
; /// The size of the input image. We'll downscale
50 /// from this size to the input size of the next
51 /// SurfaceFilter in the chain.
52 gfx::SurfaceFormat mFormat
; /// The pixel format - BGRA or BGRX. (BGRX has
53 /// slightly better performance.)
56 #ifndef MOZ_ENABLE_SKIA
59 * DownscalingFilter requires Skia. This is a fallback implementation for
60 * non-Skia builds that fails when Configure() is called (which will prevent
61 * SurfacePipeFactory from returning an instance of it) and crashes if a caller
62 * manually constructs an instance and attempts to actually use it. Callers
63 * should avoid this by ensuring that they do not request downscaling in
66 template <typename Next
>
67 class DownscalingFilter final
: public SurfaceFilter
{
69 Maybe
<SurfaceInvalidRect
> TakeInvalidRect() override
{ return Nothing(); }
71 template <typename
... Rest
>
72 nsresult
Configure(const DownscalingConfig
& aConfig
, const Rest
&... aRest
) {
73 return NS_ERROR_FAILURE
;
77 uint8_t* DoResetToFirstRow() override
{
81 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow
) override
{
85 uint8_t* DoAdvanceRow() override
{
94 * DownscalingFilter performs Lanczos downscaling, taking image input data at
95 * one size and outputting it rescaled to a different size.
97 * The 'Next' template parameter specifies the next filter in the chain.
99 template <typename Next
>
100 class DownscalingFilter final
: public SurfaceFilter
{
103 : mWindowCapacity(0),
109 ~DownscalingFilter() { ReleaseWindow(); }
111 template <typename
... Rest
>
112 nsresult
Configure(const DownscalingConfig
& aConfig
, const Rest
&... aRest
) {
113 nsresult rv
= mNext
.Configure(aRest
...);
118 if (mNext
.InputSize() == aConfig
.mInputSize
) {
119 NS_WARNING("Created a downscaler, but not downscaling?");
120 return NS_ERROR_INVALID_ARG
;
122 if (mNext
.InputSize().width
> aConfig
.mInputSize
.width
) {
123 NS_WARNING("Created a downscaler, but width is larger");
124 return NS_ERROR_INVALID_ARG
;
126 if (mNext
.InputSize().height
> aConfig
.mInputSize
.height
) {
127 NS_WARNING("Created a downscaler, but height is larger");
128 return NS_ERROR_INVALID_ARG
;
130 if (aConfig
.mInputSize
.width
<= 0 || aConfig
.mInputSize
.height
<= 0) {
131 NS_WARNING("Invalid input size for DownscalingFilter");
132 return NS_ERROR_INVALID_ARG
;
135 mInputSize
= aConfig
.mInputSize
;
136 gfx::IntSize outputSize
= mNext
.InputSize();
137 mScale
= gfxSize(double(mInputSize
.width
) / outputSize
.width
,
138 double(mInputSize
.height
) / outputSize
.height
);
139 mHasAlpha
= aConfig
.mFormat
== gfx::SurfaceFormat::OS_RGBA
;
143 auto resizeMethod
= gfx::ConvolutionFilter::ResizeMethod::LANCZOS3
;
144 if (!mXFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.width
,
146 !mYFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.height
,
147 outputSize
.height
)) {
148 NS_WARNING("Failed to compute filters for image downscaling");
149 return NS_ERROR_OUT_OF_MEMORY
;
152 // Allocate the buffer, which contains scanlines of the input image.
153 mRowBuffer
.reset(new (fallible
)
154 uint8_t[PaddedWidthInBytes(mInputSize
.width
)]);
155 if (MOZ_UNLIKELY(!mRowBuffer
)) {
156 return NS_ERROR_OUT_OF_MEMORY
;
159 // Clear the buffer to avoid writing uninitialized memory to the output.
160 memset(mRowBuffer
.get(), 0, PaddedWidthInBytes(mInputSize
.width
));
162 // Allocate the window, which contains horizontally downscaled scanlines.
163 // (We can store scanlines which are already downscaled because our
164 // downscaling filter is separable.)
165 mWindowCapacity
= mYFilter
.MaxFilter();
166 mWindow
.reset(new (fallible
) uint8_t*[mWindowCapacity
]);
167 if (MOZ_UNLIKELY(!mWindow
)) {
168 return NS_ERROR_OUT_OF_MEMORY
;
171 // Allocate the "window" of recent rows that we keep in memory as input for
172 // the downscaling code. We intentionally iterate through the entire array
173 // even if an allocation fails, to ensure that all the pointers in it are
174 // either valid or nullptr. That in turn ensures that ReleaseWindow() can
175 // clean up correctly.
176 bool anyAllocationFailed
= false;
177 const size_t windowRowSizeInBytes
= PaddedWidthInBytes(outputSize
.width
);
178 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
179 mWindow
[i
] = new (fallible
) uint8_t[windowRowSizeInBytes
];
180 anyAllocationFailed
= anyAllocationFailed
|| mWindow
[i
] == nullptr;
183 if (MOZ_UNLIKELY(anyAllocationFailed
)) {
184 return NS_ERROR_OUT_OF_MEMORY
;
187 ConfigureFilter(mInputSize
, sizeof(uint32_t));
191 Maybe
<SurfaceInvalidRect
> TakeInvalidRect() override
{
192 Maybe
<SurfaceInvalidRect
> invalidRect
= mNext
.TakeInvalidRect();
195 // Compute the input space invalid rect by scaling.
196 invalidRect
->mInputSpaceRect
.ScaleRoundOut(mScale
.width
, mScale
.height
);
203 uint8_t* DoResetToFirstRow() override
{
204 mNext
.ResetToFirstRow();
210 return GetRowPointer();
213 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow
) override
{
214 if (mInputRow
>= mInputSize
.height
) {
215 NS_WARNING("Advancing DownscalingFilter past the end of the input");
219 if (mOutputRow
>= mNext
.InputSize().height
) {
220 NS_WARNING("Advancing DownscalingFilter past the end of the output");
224 int32_t filterOffset
= 0;
225 int32_t filterLength
= 0;
226 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
228 int32_t inputRowToRead
= filterOffset
+ mRowsInWindow
;
229 MOZ_ASSERT(mInputRow
<= inputRowToRead
, "Reading past end of input");
230 if (mInputRow
== inputRowToRead
) {
231 MOZ_RELEASE_ASSERT(mRowsInWindow
< mWindowCapacity
,
232 "Need more rows than capacity!");
233 mXFilter
.ConvolveHorizontally(aInputRow
, mWindow
[mRowsInWindow
++],
237 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
238 "Writing past end of output");
240 while (mRowsInWindow
>= filterLength
) {
243 if (mOutputRow
== mNext
.InputSize().height
) {
244 break; // We're done.
247 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
,
253 return mInputRow
< mInputSize
.height
? GetRowPointer() : nullptr;
256 uint8_t* DoAdvanceRow() override
{
257 return DoAdvanceRowFromBuffer(mRowBuffer
.get());
261 uint8_t* GetRowPointer() const { return mRowBuffer
.get(); }
263 static size_t PaddedWidthInBytes(size_t aLogicalWidth
) {
264 // Convert from width in BGRA/BGRX pixels to width in bytes, padding
265 // to handle overreads by the SIMD code inside Skia.
266 return gfx::ConvolutionFilter::PadBytesForSIMD(aLogicalWidth
*
270 void DownscaleInputRow() {
271 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
272 "Writing past end of output");
274 int32_t filterOffset
= 0;
275 int32_t filterLength
= 0;
276 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
278 mNext
.template WriteUnsafeComputedRow
<uint32_t>([&](uint32_t* aRow
,
280 mYFilter
.ConvolveVertically(mWindow
.get(),
281 reinterpret_cast<uint8_t*>(aRow
), mOutputRow
,
282 mXFilter
.NumValues(), mHasAlpha
);
287 if (mOutputRow
== mNext
.InputSize().height
) {
288 return; // We're done.
291 int32_t newFilterOffset
= 0;
292 int32_t newFilterLength
= 0;
293 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &newFilterOffset
,
296 int diff
= newFilterOffset
- filterOffset
;
297 MOZ_ASSERT(diff
>= 0, "Moving backwards in the filter?");
299 // Shift the buffer. We're just moving pointers here, so this is cheap.
300 mRowsInWindow
-= diff
;
301 mRowsInWindow
= std::min(std::max(mRowsInWindow
, 0), mWindowCapacity
);
303 // If we already have enough rows to satisfy the filter, there is no need
304 // to swap as we won't be writing more before the next convolution.
305 if (filterLength
> mRowsInWindow
) {
306 for (int32_t i
= 0; i
< mRowsInWindow
; ++i
) {
307 std::swap(mWindow
[i
], mWindow
[filterLength
- mRowsInWindow
+ i
]);
312 void ReleaseWindow() {
317 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
325 Next mNext
; /// The next SurfaceFilter in the chain.
327 gfx::IntSize mInputSize
; /// The size of the input image.
328 gfxSize mScale
; /// The scale factors in each dimension.
329 /// Computed from @mInputSize and
330 /// the next filter's input size.
332 UniquePtr
<uint8_t[]> mRowBuffer
; /// The buffer into which input is written.
333 UniquePtr
<uint8_t*[]> mWindow
; /// The last few rows which were written.
335 gfx::ConvolutionFilter mXFilter
; /// The Lanczos filter in X.
336 gfx::ConvolutionFilter mYFilter
; /// The Lanczos filter in Y.
338 int32_t mWindowCapacity
; /// How many rows the window contains.
340 int32_t mRowsInWindow
; /// How many rows we've buffered in the window.
341 int32_t mInputRow
; /// The current row we're reading. (0-indexed)
342 int32_t mOutputRow
; /// The current row we're writing. (0-indexed)
344 bool mHasAlpha
; /// If true, the image has transparency.
350 } // namespace mozilla
352 #endif // mozilla_image_DownscalingFilter_h