2015-01-20 14:06:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downscaler is a high-quality, streaming image downscaler based upon Skia's
|
|
|
|
* scaling implementation.
|
|
|
|
*/
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#ifndef mozilla_image_Downscaler_h
|
|
|
|
#define mozilla_image_Downscaler_h
|
2015-01-20 14:06:37 +03:00
|
|
|
|
2015-09-26 11:36:19 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2015-01-20 14:06:37 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-06-30 20:24:27 +03:00
|
|
|
#include "gfxPoint.h"
|
2015-01-20 14:06:37 +03:00
|
|
|
#include "nsRect.h"
|
2017-08-12 07:31:21 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA
|
2017-06-22 20:57:17 +03:00
|
|
|
# include "mozilla/gfx/ConvolutionFilter.h"
|
2017-08-12 07:31:21 +03:00
|
|
|
#endif
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2015-05-06 01:35:34 +03:00
|
|
|
/**
|
|
|
|
* DownscalerInvalidRect wraps two invalidation rects: one in terms of the
|
|
|
|
* original image size, and one in terms of the target size.
|
|
|
|
*/
|
|
|
|
struct DownscalerInvalidRect {
|
|
|
|
nsIntRect mOriginalSizeRect;
|
|
|
|
nsIntRect mTargetSizeRect;
|
|
|
|
};
|
|
|
|
|
2015-05-07 21:36:00 +03:00
|
|
|
#ifdef MOZ_ENABLE_SKIA
|
|
|
|
|
2015-01-20 14:06:37 +03:00
|
|
|
/**
|
|
|
|
* Downscaler is a high-quality, streaming image downscaler based upon Skia's
|
|
|
|
* scaling implementation.
|
|
|
|
*
|
|
|
|
* Decoders can construct a Downscaler once they know their target size, then
|
|
|
|
* call BeginFrame() for each frame they decode. They should write a decoded row
|
|
|
|
* into the buffer returned by RowBuffer(), and then call CommitRow() to signal
|
|
|
|
* that they have finished.
|
|
|
|
*
|
|
|
|
|
|
|
|
* Because invalidations need to be computed in terms of the scaled version of
|
|
|
|
* the image, Downscaler also tracks them. Decoders can call HasInvalidation()
|
|
|
|
* and TakeInvalidRect() instead of tracking invalidations themselves.
|
|
|
|
*/
|
|
|
|
class Downscaler {
|
|
|
|
public:
|
|
|
|
/// Constructs a new Downscaler which to scale to size @aTargetSize.
|
|
|
|
explicit Downscaler(const nsIntSize& aTargetSize);
|
|
|
|
~Downscaler();
|
|
|
|
|
|
|
|
const nsIntSize& OriginalSize() const { return mOriginalSize; }
|
|
|
|
const nsIntSize& TargetSize() const { return mTargetSize; }
|
2017-08-14 15:29:56 +03:00
|
|
|
const nsIntSize FrameSize() const {
|
|
|
|
return nsIntSize(mFrameRect.Width(), mFrameRect.Height());
|
|
|
|
}
|
2015-05-06 01:35:34 +03:00
|
|
|
const gfxSize& Scale() const { return mScale; }
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a new frame and reinitializes the Downscaler.
|
|
|
|
*
|
|
|
|
* @param aOriginalSize The original size of this frame, before scaling.
|
2015-09-26 11:36:19 +03:00
|
|
|
* @param aFrameRect The region of the original image which has data.
|
|
|
|
* Every pixel outside @aFrameRect is considered blank and
|
|
|
|
* has zero alpha.
|
2015-01-20 14:06:37 +03:00
|
|
|
* @param aOutputBuffer The buffer to which the Downscaler should write its
|
|
|
|
* output; this is the same buffer where the Decoder
|
|
|
|
* would write its output when not downscaling during
|
|
|
|
* decode.
|
|
|
|
* @param aHasAlpha Whether or not this frame has an alpha channel.
|
|
|
|
* Performance is a little better if it doesn't have one.
|
2015-09-09 08:07:04 +03:00
|
|
|
* @param aFlipVertically If true, output rows will be written to the output
|
|
|
|
* buffer in reverse order vertically, which matches
|
|
|
|
* the way they are stored in some image formats.
|
2015-01-20 14:06:37 +03:00
|
|
|
*/
|
|
|
|
nsresult BeginFrame(const nsIntSize& aOriginalSize,
|
2015-09-26 11:36:19 +03:00
|
|
|
const Maybe<nsIntRect>& aFrameRect,
|
2015-01-20 14:06:37 +03:00
|
|
|
uint8_t* aOutputBuffer, bool aHasAlpha,
|
2015-09-09 08:07:04 +03:00
|
|
|
bool aFlipVertically = false);
|
2015-01-20 14:06:37 +03:00
|
|
|
|
2015-10-25 23:14:14 +03:00
|
|
|
bool IsFrameComplete() const {
|
|
|
|
return mCurrentInLine >= mOriginalSize.height;
|
|
|
|
}
|
|
|
|
|
2015-01-20 14:06:37 +03:00
|
|
|
/// Retrieves the buffer into which the Decoder should write each row.
|
2015-09-26 11:36:19 +03:00
|
|
|
uint8_t* RowBuffer() {
|
2017-12-21 00:46:28 +03:00
|
|
|
return mRowBuffer.get() + mFrameRect.X() * sizeof(uint32_t);
|
2015-09-26 11:36:19 +03:00
|
|
|
}
|
2015-01-20 14:06:37 +03:00
|
|
|
|
2016-02-16 10:04:57 +03:00
|
|
|
/// Clears the current row buffer.
|
|
|
|
void ClearRow() { ClearRestOfRow(0); }
|
|
|
|
|
|
|
|
/// Clears the current row buffer starting at @aStartingAtCol.
|
|
|
|
void ClearRestOfRow(uint32_t aStartingAtCol);
|
2015-09-09 08:07:06 +03:00
|
|
|
|
2015-01-20 14:06:37 +03:00
|
|
|
/// Signals that the decoder has finished writing a row into the row buffer.
|
|
|
|
void CommitRow();
|
|
|
|
|
|
|
|
/// Returns true if there is a non-empty invalid rect available.
|
|
|
|
bool HasInvalidation() const;
|
|
|
|
|
|
|
|
/// Takes the Downscaler's current invalid rect and resets it.
|
2015-05-06 01:35:34 +03:00
|
|
|
DownscalerInvalidRect TakeInvalidRect();
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the Downscaler's position in the image, for a new progressive pass
|
|
|
|
* over the same frame. Because the same data structures can be reused, this
|
|
|
|
* is more efficient than calling BeginFrame.
|
|
|
|
*/
|
|
|
|
void ResetForNextProgressivePass();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DownscaleInputLine();
|
|
|
|
void ReleaseWindow();
|
2015-09-26 11:36:19 +03:00
|
|
|
void SkipToRow(int32_t aRow);
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
nsIntSize mOriginalSize;
|
|
|
|
nsIntSize mTargetSize;
|
2015-09-26 11:36:19 +03:00
|
|
|
nsIntRect mFrameRect;
|
2015-05-06 01:35:34 +03:00
|
|
|
gfxSize mScale;
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
uint8_t* mOutputBuffer;
|
|
|
|
|
|
|
|
UniquePtr<uint8_t[]> mRowBuffer;
|
|
|
|
UniquePtr<uint8_t*[]> mWindow;
|
|
|
|
|
2017-06-22 20:57:17 +03:00
|
|
|
gfx::ConvolutionFilter mXFilter;
|
|
|
|
gfx::ConvolutionFilter mYFilter;
|
2015-01-20 14:06:37 +03:00
|
|
|
|
|
|
|
int32_t mWindowCapacity;
|
|
|
|
|
|
|
|
int32_t mLinesInBuffer;
|
|
|
|
int32_t mPrevInvalidatedLine;
|
|
|
|
int32_t mCurrentOutLine;
|
|
|
|
int32_t mCurrentInLine;
|
|
|
|
|
2015-09-09 08:07:04 +03:00
|
|
|
bool mHasAlpha : 1;
|
|
|
|
bool mFlipVertically : 1;
|
2015-01-20 14:06:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downscaler requires Skia to work, so we provide a dummy implementation if
|
|
|
|
* Skia is disabled that asserts if constructed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Downscaler {
|
|
|
|
public:
|
2016-12-09 03:20:12 +03:00
|
|
|
explicit Downscaler(const nsIntSize&) : mScale(1.0, 1.0) {
|
2015-01-25 04:50:21 +03:00
|
|
|
MOZ_RELEASE_ASSERT(false, "Skia is not enabled");
|
2015-01-20 14:06:37 +03:00
|
|
|
}
|
|
|
|
|
2016-12-09 03:20:12 +03:00
|
|
|
const nsIntSize& OriginalSize() const { return mSize; }
|
|
|
|
const nsIntSize& TargetSize() const { return mSize; }
|
|
|
|
const gfxSize& Scale() const { return mScale; }
|
2015-01-20 14:06:37 +03:00
|
|
|
|
2015-09-30 20:58:00 +03:00
|
|
|
nsresult BeginFrame(const nsIntSize&, const Maybe<nsIntRect>&, uint8_t*, bool,
|
|
|
|
bool = false) {
|
2015-01-20 14:06:37 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:14:14 +03:00
|
|
|
bool IsFrameComplete() const { return false; }
|
2015-01-25 04:50:21 +03:00
|
|
|
uint8_t* RowBuffer() { return nullptr; }
|
2016-02-16 10:04:57 +03:00
|
|
|
void ClearRow() {}
|
|
|
|
void ClearRestOfRow(uint32_t) {}
|
2015-01-20 14:06:37 +03:00
|
|
|
void CommitRow() {}
|
|
|
|
bool HasInvalidation() const { return false; }
|
2015-05-07 21:36:00 +03:00
|
|
|
DownscalerInvalidRect TakeInvalidRect() { return DownscalerInvalidRect(); }
|
2015-01-20 14:06:37 +03:00
|
|
|
void ResetForNextProgressivePass() {}
|
2015-12-30 22:46:54 +03:00
|
|
|
const nsIntSize FrameSize() const { return nsIntSize(0, 0); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-09 03:20:12 +03:00
|
|
|
private:
|
|
|
|
nsIntSize mSize;
|
|
|
|
gfxSize mScale;
|
2015-01-20 14:06:37 +03:00
|
|
|
};
|
|
|
|
|
2015-05-07 21:36:00 +03:00
|
|
|
#endif // MOZ_ENABLE_SKIA
|
2015-09-18 01:03:31 +03:00
|
|
|
|
2015-01-20 14:06:37 +03:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_Downscaler_h
|