Line data Source code
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 : /**
8 : * Downscaler is a high-quality, streaming image downscaler based upon Skia's
9 : * scaling implementation.
10 : */
11 :
12 : #ifndef mozilla_image_Downscaler_h
13 : #define mozilla_image_Downscaler_h
14 :
15 : #include "mozilla/Maybe.h"
16 : #include "mozilla/UniquePtr.h"
17 : #include "gfxPoint.h"
18 : #include "nsRect.h"
19 : #include "mozilla/gfx/ConvolutionFilter.h"
20 :
21 : namespace mozilla {
22 : namespace image {
23 :
24 : /**
25 : * DownscalerInvalidRect wraps two invalidation rects: one in terms of the
26 : * original image size, and one in terms of the target size.
27 : */
28 0 : struct DownscalerInvalidRect
29 : {
30 : nsIntRect mOriginalSizeRect;
31 : nsIntRect mTargetSizeRect;
32 : };
33 :
34 : #ifdef MOZ_ENABLE_SKIA
35 :
36 : /**
37 : * Downscaler is a high-quality, streaming image downscaler based upon Skia's
38 : * scaling implementation.
39 : *
40 : * Decoders can construct a Downscaler once they know their target size, then
41 : * call BeginFrame() for each frame they decode. They should write a decoded row
42 : * into the buffer returned by RowBuffer(), and then call CommitRow() to signal
43 : * that they have finished.
44 : *
45 :
46 : * Because invalidations need to be computed in terms of the scaled version of
47 : * the image, Downscaler also tracks them. Decoders can call HasInvalidation()
48 : * and TakeInvalidRect() instead of tracking invalidations themselves.
49 : */
50 : class Downscaler
51 : {
52 : public:
53 : /// Constructs a new Downscaler which to scale to size @aTargetSize.
54 : explicit Downscaler(const nsIntSize& aTargetSize);
55 : ~Downscaler();
56 :
57 : const nsIntSize& OriginalSize() const { return mOriginalSize; }
58 0 : const nsIntSize& TargetSize() const { return mTargetSize; }
59 : const nsIntSize FrameSize() const { return nsIntSize(mFrameRect.width, mFrameRect.height); }
60 : const gfxSize& Scale() const { return mScale; }
61 :
62 : /**
63 : * Begins a new frame and reinitializes the Downscaler.
64 : *
65 : * @param aOriginalSize The original size of this frame, before scaling.
66 : * @param aFrameRect The region of the original image which has data.
67 : * Every pixel outside @aFrameRect is considered blank and
68 : * has zero alpha.
69 : * @param aOutputBuffer The buffer to which the Downscaler should write its
70 : * output; this is the same buffer where the Decoder
71 : * would write its output when not downscaling during
72 : * decode.
73 : * @param aHasAlpha Whether or not this frame has an alpha channel.
74 : * Performance is a little better if it doesn't have one.
75 : * @param aFlipVertically If true, output rows will be written to the output
76 : * buffer in reverse order vertically, which matches
77 : * the way they are stored in some image formats.
78 : */
79 : nsresult BeginFrame(const nsIntSize& aOriginalSize,
80 : const Maybe<nsIntRect>& aFrameRect,
81 : uint8_t* aOutputBuffer,
82 : bool aHasAlpha,
83 : bool aFlipVertically = false);
84 :
85 : bool IsFrameComplete() const { return mCurrentInLine >= mOriginalSize.height; }
86 :
87 : /// Retrieves the buffer into which the Decoder should write each row.
88 0 : uint8_t* RowBuffer()
89 : {
90 0 : return mRowBuffer.get() + mFrameRect.x * sizeof(uint32_t);
91 : }
92 :
93 : /// Clears the current row buffer.
94 0 : void ClearRow() { ClearRestOfRow(0); }
95 :
96 : /// Clears the current row buffer starting at @aStartingAtCol.
97 : void ClearRestOfRow(uint32_t aStartingAtCol);
98 :
99 : /// Signals that the decoder has finished writing a row into the row buffer.
100 : void CommitRow();
101 :
102 : /// Returns true if there is a non-empty invalid rect available.
103 : bool HasInvalidation() const;
104 :
105 : /// Takes the Downscaler's current invalid rect and resets it.
106 : DownscalerInvalidRect TakeInvalidRect();
107 :
108 : /**
109 : * Resets the Downscaler's position in the image, for a new progressive pass
110 : * over the same frame. Because the same data structures can be reused, this
111 : * is more efficient than calling BeginFrame.
112 : */
113 : void ResetForNextProgressivePass();
114 :
115 : private:
116 : void DownscaleInputLine();
117 : void ReleaseWindow();
118 : void SkipToRow(int32_t aRow);
119 :
120 : nsIntSize mOriginalSize;
121 : nsIntSize mTargetSize;
122 : nsIntRect mFrameRect;
123 : gfxSize mScale;
124 :
125 : uint8_t* mOutputBuffer;
126 :
127 : UniquePtr<uint8_t[]> mRowBuffer;
128 : UniquePtr<uint8_t*[]> mWindow;
129 :
130 : gfx::ConvolutionFilter mXFilter;
131 : gfx::ConvolutionFilter mYFilter;
132 :
133 : int32_t mWindowCapacity;
134 :
135 : int32_t mLinesInBuffer;
136 : int32_t mPrevInvalidatedLine;
137 : int32_t mCurrentOutLine;
138 : int32_t mCurrentInLine;
139 :
140 : bool mHasAlpha : 1;
141 : bool mFlipVertically : 1;
142 : };
143 :
144 : #else
145 :
146 : /**
147 : * Downscaler requires Skia to work, so we provide a dummy implementation if
148 : * Skia is disabled that asserts if constructed.
149 : */
150 :
151 : class Downscaler
152 : {
153 : public:
154 : explicit Downscaler(const nsIntSize&) : mScale(1.0, 1.0)
155 : {
156 : MOZ_RELEASE_ASSERT(false, "Skia is not enabled");
157 : }
158 :
159 : const nsIntSize& OriginalSize() const { return mSize; }
160 : const nsIntSize& TargetSize() const { return mSize; }
161 : const gfxSize& Scale() const { return mScale; }
162 :
163 : nsresult BeginFrame(const nsIntSize&, const Maybe<nsIntRect>&, uint8_t*, bool, bool = false)
164 : {
165 : return NS_ERROR_FAILURE;
166 : }
167 :
168 : bool IsFrameComplete() const { return false; }
169 : uint8_t* RowBuffer() { return nullptr; }
170 : void ClearRow() { }
171 : void ClearRestOfRow(uint32_t) { }
172 : void CommitRow() { }
173 : bool HasInvalidation() const { return false; }
174 : DownscalerInvalidRect TakeInvalidRect() { return DownscalerInvalidRect(); }
175 : void ResetForNextProgressivePass() { }
176 : const nsIntSize FrameSize() const { return nsIntSize(0, 0); }
177 : private:
178 : nsIntSize mSize;
179 : gfxSize mScale;
180 : };
181 :
182 : #endif // MOZ_ENABLE_SKIA
183 :
184 :
185 :
186 : } // namespace image
187 : } // namespace mozilla
188 :
189 : #endif // mozilla_image_Downscaler_h
|