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 #include "mozilla/gfx/ConvolutionFilter.h"
29 #include "SurfacePipe.h"
34 //////////////////////////////////////////////////////////////////////////////
36 //////////////////////////////////////////////////////////////////////////////
38 template <typename Next
>
39 class DownscalingFilter
;
42 * A configuration struct for DownscalingConfig.
44 struct DownscalingConfig
{
45 template <typename Next
>
46 using Filter
= DownscalingFilter
<Next
>;
47 gfx::IntSize mInputSize
; /// The size of the input image. We'll downscale
48 /// from this size to the input size of the next
49 /// SurfaceFilter in the chain.
50 gfx::SurfaceFormat mFormat
; /// The pixel format - BGRA or BGRX. (BGRX has
51 /// slightly better performance.)
55 * DownscalingFilter performs Lanczos downscaling, taking image input data at
56 * one size and outputting it rescaled to a different size.
58 * The 'Next' template parameter specifies the next filter in the chain.
60 template <typename Next
>
61 class DownscalingFilter final
: public SurfaceFilter
{
70 ~DownscalingFilter() { ReleaseWindow(); }
72 template <typename
... Rest
>
73 nsresult
Configure(const DownscalingConfig
& aConfig
, const Rest
&... aRest
) {
74 nsresult rv
= mNext
.Configure(aRest
...);
79 if (mNext
.InputSize() == aConfig
.mInputSize
) {
80 NS_WARNING("Created a downscaler, but not downscaling?");
81 return NS_ERROR_INVALID_ARG
;
83 if (mNext
.InputSize().width
> aConfig
.mInputSize
.width
) {
84 NS_WARNING("Created a downscaler, but width is larger");
85 return NS_ERROR_INVALID_ARG
;
87 if (mNext
.InputSize().height
> aConfig
.mInputSize
.height
) {
88 NS_WARNING("Created a downscaler, but height is larger");
89 return NS_ERROR_INVALID_ARG
;
91 if (aConfig
.mInputSize
.width
<= 0 || aConfig
.mInputSize
.height
<= 0) {
92 NS_WARNING("Invalid input size for DownscalingFilter");
93 return NS_ERROR_INVALID_ARG
;
96 mInputSize
= aConfig
.mInputSize
;
97 gfx::IntSize outputSize
= mNext
.InputSize();
99 gfx::MatrixScalesDouble(double(mInputSize
.width
) / outputSize
.width
,
100 double(mInputSize
.height
) / outputSize
.height
);
101 mHasAlpha
= aConfig
.mFormat
== gfx::SurfaceFormat::OS_RGBA
;
105 auto resizeMethod
= gfx::ConvolutionFilter::ResizeMethod::LANCZOS3
;
106 if (!mXFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.width
,
108 !mYFilter
.ComputeResizeFilter(resizeMethod
, mInputSize
.height
,
109 outputSize
.height
)) {
110 NS_WARNING("Failed to compute filters for image downscaling");
111 return NS_ERROR_OUT_OF_MEMORY
;
114 // Allocate the buffer, which contains scanlines of the input image.
115 mRowBuffer
.reset(new (fallible
)
116 uint8_t[PaddedWidthInBytes(mInputSize
.width
)]);
117 if (MOZ_UNLIKELY(!mRowBuffer
)) {
118 return NS_ERROR_OUT_OF_MEMORY
;
121 // Clear the buffer to avoid writing uninitialized memory to the output.
122 memset(mRowBuffer
.get(), 0, PaddedWidthInBytes(mInputSize
.width
));
124 // Allocate the window, which contains horizontally downscaled scanlines.
125 // (We can store scanlines which are already downscaled because our
126 // downscaling filter is separable.)
127 mWindowCapacity
= mYFilter
.MaxFilter();
128 mWindow
.reset(new (fallible
) uint8_t*[mWindowCapacity
]);
129 if (MOZ_UNLIKELY(!mWindow
)) {
130 return NS_ERROR_OUT_OF_MEMORY
;
133 // Allocate the "window" of recent rows that we keep in memory as input for
134 // the downscaling code. We intentionally iterate through the entire array
135 // even if an allocation fails, to ensure that all the pointers in it are
136 // either valid or nullptr. That in turn ensures that ReleaseWindow() can
137 // clean up correctly.
138 bool anyAllocationFailed
= false;
139 const size_t windowRowSizeInBytes
= PaddedWidthInBytes(outputSize
.width
);
140 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
141 mWindow
[i
] = new (fallible
) uint8_t[windowRowSizeInBytes
];
142 anyAllocationFailed
= anyAllocationFailed
|| mWindow
[i
] == nullptr;
145 if (MOZ_UNLIKELY(anyAllocationFailed
)) {
146 return NS_ERROR_OUT_OF_MEMORY
;
149 ConfigureFilter(mInputSize
, sizeof(uint32_t));
153 Maybe
<SurfaceInvalidRect
> TakeInvalidRect() override
{
154 Maybe
<SurfaceInvalidRect
> invalidRect
= mNext
.TakeInvalidRect();
157 // Compute the input space invalid rect by scaling.
158 invalidRect
->mInputSpaceRect
.ScaleRoundOut(mScale
.xScale
, mScale
.yScale
);
165 uint8_t* DoResetToFirstRow() override
{
166 mNext
.ResetToFirstRow();
172 return GetRowPointer();
175 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow
) override
{
176 if (mInputRow
>= mInputSize
.height
) {
177 NS_WARNING("Advancing DownscalingFilter past the end of the input");
181 if (mOutputRow
>= mNext
.InputSize().height
) {
182 NS_WARNING("Advancing DownscalingFilter past the end of the output");
186 int32_t filterOffset
= 0;
187 int32_t filterLength
= 0;
188 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
190 int32_t inputRowToRead
= filterOffset
+ mRowsInWindow
;
191 MOZ_ASSERT(mInputRow
<= inputRowToRead
, "Reading past end of input");
192 if (mInputRow
== inputRowToRead
) {
193 MOZ_RELEASE_ASSERT(mRowsInWindow
< mWindowCapacity
,
194 "Need more rows than capacity!");
195 mXFilter
.ConvolveHorizontally(aInputRow
, mWindow
[mRowsInWindow
++],
199 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
200 "Writing past end of output");
202 while (mRowsInWindow
>= filterLength
) {
205 if (mOutputRow
== mNext
.InputSize().height
) {
206 break; // We're done.
209 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
,
215 return mInputRow
< mInputSize
.height
? GetRowPointer() : nullptr;
218 uint8_t* DoAdvanceRow() override
{
219 return DoAdvanceRowFromBuffer(mRowBuffer
.get());
223 uint8_t* GetRowPointer() const { return mRowBuffer
.get(); }
225 static size_t PaddedWidthInBytes(size_t aLogicalWidth
) {
226 // Convert from width in BGRA/BGRX pixels to width in bytes, padding
227 // to handle overreads by the SIMD code inside Skia.
228 return gfx::ConvolutionFilter::PadBytesForSIMD(aLogicalWidth
*
232 void DownscaleInputRow() {
233 MOZ_ASSERT(mOutputRow
< mNext
.InputSize().height
,
234 "Writing past end of output");
236 int32_t filterOffset
= 0;
237 int32_t filterLength
= 0;
238 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &filterOffset
, &filterLength
);
240 mNext
.template WriteUnsafeComputedRow
<uint32_t>([&](uint32_t* aRow
,
242 mYFilter
.ConvolveVertically(mWindow
.get(),
243 reinterpret_cast<uint8_t*>(aRow
), mOutputRow
,
244 mXFilter
.NumValues(), mHasAlpha
);
249 if (mOutputRow
== mNext
.InputSize().height
) {
250 return; // We're done.
253 int32_t newFilterOffset
= 0;
254 int32_t newFilterLength
= 0;
255 mYFilter
.GetFilterOffsetAndLength(mOutputRow
, &newFilterOffset
,
258 int diff
= newFilterOffset
- filterOffset
;
259 MOZ_ASSERT(diff
>= 0, "Moving backwards in the filter?");
261 // Shift the buffer. We're just moving pointers here, so this is cheap.
262 mRowsInWindow
-= diff
;
263 mRowsInWindow
= std::min(std::max(mRowsInWindow
, 0), mWindowCapacity
);
265 // If we already have enough rows to satisfy the filter, there is no need
266 // to swap as we won't be writing more before the next convolution.
267 if (filterLength
> mRowsInWindow
) {
268 for (int32_t i
= 0; i
< mRowsInWindow
; ++i
) {
269 std::swap(mWindow
[i
], mWindow
[filterLength
- mRowsInWindow
+ i
]);
274 void ReleaseWindow() {
279 for (int32_t i
= 0; i
< mWindowCapacity
; ++i
) {
287 Next mNext
; /// The next SurfaceFilter in the chain.
289 gfx::IntSize mInputSize
; /// The size of the input image.
290 gfx::MatrixScalesDouble mScale
; /// The scale factors in each dimension.
291 /// Computed from @mInputSize and
292 /// the next filter's input size.
294 UniquePtr
<uint8_t[]> mRowBuffer
; /// The buffer into which input is written.
295 UniquePtr
<uint8_t*[]> mWindow
; /// The last few rows which were written.
297 gfx::ConvolutionFilter mXFilter
; /// The Lanczos filter in X.
298 gfx::ConvolutionFilter mYFilter
; /// The Lanczos filter in Y.
300 int32_t mWindowCapacity
; /// How many rows the window contains.
302 int32_t mRowsInWindow
; /// How many rows we've buffered in the window.
303 int32_t mInputRow
; /// The current row we're reading. (0-indexed)
304 int32_t mOutputRow
; /// The current row we're writing. (0-indexed)
306 bool mHasAlpha
; /// If true, the image has transparency.
310 } // namespace mozilla
312 #endif // mozilla_image_DownscalingFilter_h