Backed out changeset 7ac06c7aba78 (bug 1851717) for causing talos failures. a=backout
[gecko.git] / image / SurfacePipe.h
blob7dd20276e5899d407874d3144d396908775565be
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/. */
7 /**
8 * A SurfacePipe is a pipeline that consists of a series of SurfaceFilters
9 * terminating in a SurfaceSink. Each SurfaceFilter transforms the image data in
10 * some way before the SurfaceSink ultimately writes it to the surface. This
11 * design allows for each transformation to be tested independently, for the
12 * transformations to be combined as needed to meet the needs of different
13 * situations, and for all image decoders to share the same code for these
14 * transformations.
16 * Writing to the SurfacePipe is done using lambdas that act as generator
17 * functions. Because the SurfacePipe machinery controls where the writes take
18 * place, a bug in an image decoder cannot cause a buffer overflow of the
19 * underlying surface.
22 #ifndef mozilla_image_SurfacePipe_h
23 #define mozilla_image_SurfacePipe_h
25 #include <stdint.h>
27 #include <utility>
29 #include "AnimationParams.h"
30 #include "mozilla/Likely.h"
31 #include "mozilla/Maybe.h"
33 #include "mozilla/UniquePtr.h"
34 #include "mozilla/Unused.h"
35 #include "mozilla/Variant.h"
36 #include "mozilla/gfx/2D.h"
37 #include "mozilla/gfx/Swizzle.h"
38 #include "nsDebug.h"
39 #include "Orientation.h"
41 namespace mozilla {
42 namespace image {
44 class Decoder;
46 /**
47 * An invalid rect for a surface. Results are given both in the space of the
48 * input image (i.e., before any SurfaceFilters are applied) and in the space
49 * of the output surface (after all SurfaceFilters).
51 struct SurfaceInvalidRect {
52 OrientedIntRect
53 mInputSpaceRect; /// The invalid rect in pre-SurfacePipe space.
54 OrientedIntRect
55 mOutputSpaceRect; /// The invalid rect in post-SurfacePipe space.
58 /**
59 * An enum used to allow the lambdas passed to WritePixels() to communicate
60 * their state to the caller.
62 enum class WriteState : uint8_t {
63 NEED_MORE_DATA, /// The lambda ran out of data.
65 FINISHED, /// The lambda is done writing to the surface; future writes
66 /// will fail.
68 FAILURE /// The lambda encountered an error. The caller may recover
69 /// if possible and continue to write. (This never indicates
70 /// an error in the SurfacePipe machinery itself; it's only
71 /// generated by the lambdas.)
74 /**
75 * A template alias used to make the return value of WritePixels() lambdas
76 * (which may return either a pixel value or a WriteState) easier to specify.
78 template <typename PixelType>
79 using NextPixel = Variant<PixelType, WriteState>;
81 /**
82 * SurfaceFilter is the abstract superclass of SurfacePipe pipeline stages. It
83 * implements the the code that actually writes to the surface - WritePixels()
84 * and the other Write*() methods - which are non-virtual for efficiency.
86 * SurfaceFilter's API is nonpublic; only SurfacePipe and other SurfaceFilters
87 * should use it. Non-SurfacePipe code should use the methods on SurfacePipe.
89 * To implement a SurfaceFilter, it's necessary to subclass SurfaceFilter and
90 * implement, at a minimum, the pure virtual methods. It's also necessary to
91 * define a Config struct with a Filter typedef member that identifies the
92 * matching SurfaceFilter class, and a Configure() template method. See an
93 * existing SurfaceFilter subclass, such as RemoveFrameRectFilter, for an
94 * example of how the Configure() method must be implemented. It takes a list of
95 * Config structs, passes the tail of the list to the next filter in the chain's
96 * Configure() method, and then uses the head of the list to configure itself. A
97 * SurfaceFilter's Configure() method must also call
98 * SurfaceFilter::ConfigureFilter() to provide the Write*() methods with the
99 * information they need to do their jobs.
101 class SurfaceFilter {
102 public:
103 SurfaceFilter() : mRowPointer(nullptr), mCol(0), mPixelSize(0) {}
105 virtual ~SurfaceFilter() {}
108 * Reset this surface to the first row. It's legal for this filter to throw
109 * away any previously written data at this point, as all rows must be written
110 * to on every pass.
112 * @return a pointer to the buffer for the first row.
114 uint8_t* ResetToFirstRow() {
115 mCol = 0;
116 mRowPointer = DoResetToFirstRow();
117 return mRowPointer;
121 * Called by WritePixels() to advance this filter to the next row.
123 * @return a pointer to the buffer for the next row, or nullptr to indicate
124 * that we've finished the entire surface.
126 uint8_t* AdvanceRow() {
127 mCol = 0;
128 mRowPointer = DoAdvanceRow();
129 return mRowPointer;
133 * Called by WriteBuffer() to advance this filter to the next row, if the
134 * supplied row is a full row.
136 * @return a pointer to the buffer for the next row, or nullptr to indicate
137 * that we've finished the entire surface.
139 uint8_t* AdvanceRow(const uint8_t* aInputRow) {
140 mCol = 0;
141 mRowPointer = DoAdvanceRowFromBuffer(aInputRow);
142 return mRowPointer;
145 /// @return a pointer to the buffer for the current row.
146 uint8_t* CurrentRowPointer() const { return mRowPointer; }
148 /// @return true if we've finished writing to the surface.
149 bool IsSurfaceFinished() const { return mRowPointer == nullptr; }
151 /// @return the input size this filter expects.
152 gfx::IntSize InputSize() const { return mInputSize; }
155 * Write pixels to the surface one at a time by repeatedly calling a lambda
156 * that yields pixels. WritePixels() is completely memory safe.
158 * Writing continues until every pixel in the surface has been written to
159 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
160 * which WritePixels() will return to the caller.
162 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
163 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
164 * size passed to ConfigureFilter().
166 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
167 * which means we can remove the PixelType template parameter from this
168 * method.
170 * @param aFunc A lambda that functions as a generator, yielding the next
171 * pixel in the surface each time it's called. The lambda must
172 * return a NextPixel<PixelType> value.
174 * @return A WriteState value indicating the lambda generator's state.
175 * WritePixels() itself will return WriteState::FINISHED if writing
176 * has finished, regardless of the lambda's internal state.
178 template <typename PixelType, typename Func>
179 WriteState WritePixels(Func aFunc) {
180 Maybe<WriteState> result;
181 while (
182 !(result = DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc)))) {
185 return *result;
189 * Write pixels to the surface by calling a lambda which may write as many
190 * pixels as there is remaining to complete the row. It is not completely
191 * memory safe as it trusts the underlying decoder not to overrun the given
192 * buffer, however it is an acceptable tradeoff for performance.
194 * Writing continues until every pixel in the surface has been written to
195 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
196 * which WritePixelBlocks() will return to the caller.
198 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
199 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
200 * size passed to ConfigureFilter().
202 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
203 * which means we can remove the PixelType template parameter from this
204 * method.
206 * @param aFunc A lambda that functions as a generator, yielding at most the
207 * maximum number of pixels requested. The lambda must accept a
208 * pointer argument to the first pixel to write, a maximum
209 * number of pixels to write as part of the block, and return a
210 * NextPixel<PixelType> value.
212 * @return A WriteState value indicating the lambda generator's state.
213 * WritePixelBlocks() itself will return WriteState::FINISHED if
214 * writing has finished, regardless of the lambda's internal state.
216 template <typename PixelType, typename Func>
217 WriteState WritePixelBlocks(Func aFunc) {
218 Maybe<WriteState> result;
219 while (!(result = DoWritePixelBlockToRow<PixelType>(
220 std::forward<Func>(aFunc)))) {
223 return *result;
227 * A variant of WritePixels() that writes a single row of pixels to the
228 * surface one at a time by repeatedly calling a lambda that yields pixels.
229 * WritePixelsToRow() is completely memory safe.
231 * Writing continues until every pixel in the row has been written to. If the
232 * surface is complete at that pointer, WriteState::FINISHED is returned;
233 * otherwise, WritePixelsToRow() returns WriteState::NEED_MORE_DATA. The
234 * lambda can terminate writing early by returning a WriteState itself, which
235 * WritePixelsToRow() will return to the caller.
237 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
238 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
239 * size passed to ConfigureFilter().
241 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
242 * which means we can remove the PixelType template parameter from this
243 * method.
245 * @param aFunc A lambda that functions as a generator, yielding the next
246 * pixel in the surface each time it's called. The lambda must
247 * return a NextPixel<PixelType> value.
249 * @return A WriteState value indicating the lambda generator's state.
250 * WritePixels() itself will return WriteState::FINISHED if writing
251 * the entire surface has finished, or WriteState::NEED_MORE_DATA if
252 * writing the row has finished, regardless of the lambda's internal
253 * state.
255 template <typename PixelType, typename Func>
256 WriteState WritePixelsToRow(Func aFunc) {
257 return DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc))
258 .valueOr(WriteState::NEED_MORE_DATA);
262 * Write a row to the surface by copying from a buffer. This is bounds checked
263 * and memory safe with respect to the surface, but care must still be taken
264 * by the caller not to overread the source buffer. This variant of
265 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
266 * pixels.
268 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
269 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
270 * size passed to ConfigureFilter().
272 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
273 * which means we can remove the PixelType template parameter from this
274 * method.
276 * @param aSource A buffer to copy from. This buffer must be
277 * |mInputSize.width| pixels wide, which means
278 * |mInputSize.width * sizeof(PixelType)| bytes. May not be
279 * null.
281 * @return WriteState::FINISHED if the entire surface has been written to.
282 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
283 * value is passed, returns WriteState::FAILURE.
285 template <typename PixelType>
286 WriteState WriteBuffer(const PixelType* aSource) {
287 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
288 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
289 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
291 if (IsSurfaceFinished()) {
292 return WriteState::FINISHED; // Already done.
295 if (MOZ_UNLIKELY(!aSource)) {
296 NS_WARNING("Passed a null pointer to WriteBuffer");
297 return WriteState::FAILURE;
300 AdvanceRow(reinterpret_cast<const uint8_t*>(aSource));
301 return IsSurfaceFinished() ? WriteState::FINISHED
302 : WriteState::NEED_MORE_DATA;
306 * Write a row to the surface by copying from a buffer. This is bounds checked
307 * and memory safe with respect to the surface, but care must still be taken
308 * by the caller not to overread the source buffer. This variant of
309 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
310 * to the row starting at @aStartColumn. Any pixels in columns before
311 * @aStartColumn or after the pixels copied from the buffer are cleared.
313 * Bounds checking failures produce warnings in debug builds because although
314 * the bounds checking maintains safety, this kind of failure could indicate a
315 * bug in the calling code.
317 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
318 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
319 * size passed to ConfigureFilter().
321 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
322 * which means we can remove the PixelType template parameter from this
323 * method.
325 * @param aSource A buffer to copy from. This buffer must be @aLength pixels
326 * wide, which means |aLength * sizeof(PixelType)| bytes. May
327 * not be null.
328 * @param aStartColumn The column to start writing to in the row. Columns
329 * before this are cleared.
330 * @param aLength The number of bytes, at most, which may be copied from
331 * @aSource. Fewer bytes may be copied in practice due to
332 * bounds checking.
334 * @return WriteState::FINISHED if the entire surface has been written to.
335 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
336 * value is passed, returns WriteState::FAILURE.
338 template <typename PixelType>
339 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
340 const size_t aLength) {
341 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
342 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
343 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
345 if (IsSurfaceFinished()) {
346 return WriteState::FINISHED; // Already done.
349 if (MOZ_UNLIKELY(!aSource)) {
350 NS_WARNING("Passed a null pointer to WriteBuffer");
351 return WriteState::FAILURE;
354 PixelType* dest = reinterpret_cast<PixelType*>(mRowPointer);
356 // Clear the area before |aStartColumn|.
357 const size_t prefixLength =
358 std::min<size_t>(mInputSize.width, aStartColumn);
359 if (MOZ_UNLIKELY(prefixLength != aStartColumn)) {
360 NS_WARNING("Provided starting column is out-of-bounds in WriteBuffer");
363 memset(dest, 0, mInputSize.width * sizeof(PixelType));
364 dest += prefixLength;
366 // Write |aLength| pixels from |aSource| into the row, with bounds checking.
367 const size_t bufferLength =
368 std::min<size_t>(mInputSize.width - prefixLength, aLength);
369 if (MOZ_UNLIKELY(bufferLength != aLength)) {
370 NS_WARNING("Provided buffer length is out-of-bounds in WriteBuffer");
373 memcpy(dest, aSource, bufferLength * sizeof(PixelType));
374 dest += bufferLength;
376 // Clear the rest of the row.
377 const size_t suffixLength =
378 mInputSize.width - (prefixLength + bufferLength);
379 memset(dest, 0, suffixLength * sizeof(PixelType));
381 AdvanceRow();
383 return IsSurfaceFinished() ? WriteState::FINISHED
384 : WriteState::NEED_MORE_DATA;
388 * Write an empty row to the surface. If some pixels have already been written
389 * to this row, they'll be discarded.
391 * @return WriteState::FINISHED if the entire surface has been written to.
392 * Otherwise, returns WriteState::NEED_MORE_DATA.
394 WriteState WriteEmptyRow() {
395 if (IsSurfaceFinished()) {
396 return WriteState::FINISHED; // Already done.
399 memset(mRowPointer, 0, mInputSize.width * mPixelSize);
400 AdvanceRow();
402 return IsSurfaceFinished() ? WriteState::FINISHED
403 : WriteState::NEED_MORE_DATA;
407 * Write a row to the surface by calling a lambda that uses a pointer to
408 * directly write to the row. This is unsafe because SurfaceFilter can't
409 * provide any bounds checking; that's up to the lambda itself. For this
410 * reason, the other Write*() methods should be preferred whenever it's
411 * possible to use them; WriteUnsafeComputedRow() should be used only when
412 * it's absolutely necessary to avoid extra copies or other performance
413 * penalties.
415 * This method should never be exposed to SurfacePipe consumers; it's strictly
416 * for use in SurfaceFilters. If external code needs this method, it should
417 * probably be turned into a SurfaceFilter.
419 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
420 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
421 * size passed to ConfigureFilter().
423 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
424 * which means we can remove the PixelType template parameter from this
425 * method.
427 * @param aFunc A lambda that writes directly to the row.
429 * @return WriteState::FINISHED if the entire surface has been written to.
430 * Otherwise, returns WriteState::NEED_MORE_DATA.
432 template <typename PixelType, typename Func>
433 WriteState WriteUnsafeComputedRow(Func aFunc) {
434 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
435 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
436 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
438 if (IsSurfaceFinished()) {
439 return WriteState::FINISHED; // Already done.
442 // Call the provided lambda with a pointer to the buffer for the current
443 // row. This is unsafe because we can't do any bounds checking; the lambda
444 // itself has to be responsible for that.
445 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
446 aFunc(rowPtr, mInputSize.width);
447 AdvanceRow();
449 return IsSurfaceFinished() ? WriteState::FINISHED
450 : WriteState::NEED_MORE_DATA;
453 //////////////////////////////////////////////////////////////////////////////
454 // Methods Subclasses Should Override
455 //////////////////////////////////////////////////////////////////////////////
458 * @return a SurfaceInvalidRect representing the region of the surface that
459 * has been written to since the last time TakeInvalidRect() was
460 * called, or Nothing() if the region is empty (i.e. nothing has been
461 * written).
463 virtual Maybe<SurfaceInvalidRect> TakeInvalidRect() = 0;
465 protected:
467 * Called by ResetToFirstRow() to actually perform the reset. It's legal to
468 * throw away any previously written data at this point, as all rows must be
469 * written to on every pass.
471 virtual uint8_t* DoResetToFirstRow() = 0;
474 * Called by AdvanceRow() to actually advance this filter to the next row.
476 * @param aInputRow The input row supplied by the decoder.
478 * @return a pointer to the buffer for the next row, or nullptr to indicate
479 * that we've finished the entire surface.
481 virtual uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) = 0;
484 * Called by AdvanceRow() to actually advance this filter to the next row.
486 * @return a pointer to the buffer for the next row, or nullptr to indicate
487 * that we've finished the entire surface.
489 virtual uint8_t* DoAdvanceRow() = 0;
491 //////////////////////////////////////////////////////////////////////////////
492 // Methods For Internal Use By Subclasses
493 //////////////////////////////////////////////////////////////////////////////
496 * Called by subclasses' Configure() methods to initialize the configuration
497 * of this filter. After the filter is configured, calls ResetToFirstRow().
499 * @param aInputSize The input size of this filter, in pixels. The previous
500 * filter in the chain will expect to write into rows
501 * |aInputSize.width| pixels wide.
502 * @param aPixelSize How large, in bytes, each pixel in the surface is. This
503 * should be either 1 for paletted images or 4 for BGRA/BGRX
504 * images.
506 void ConfigureFilter(gfx::IntSize aInputSize, uint8_t aPixelSize) {
507 mInputSize = aInputSize;
508 mPixelSize = aPixelSize;
510 ResetToFirstRow();
514 * Called by subclasses' DoAdvanceRowFromBuffer() methods to copy a decoder
515 * supplied row buffer into its internal row pointer. Ideally filters at the
516 * top of the filter pipeline are able to consume the decoder row buffer
517 * directly without the extra copy prior to performing its transformation.
519 * @param aInputRow The input row supplied by the decoder.
521 void CopyInputRow(const uint8_t* aInputRow) {
522 MOZ_ASSERT(aInputRow);
523 MOZ_ASSERT(mCol == 0);
524 memcpy(mRowPointer, aInputRow, mPixelSize * mInputSize.width);
527 private:
529 * An internal method used to implement WritePixelBlocks. This method writes
530 * up to the number of pixels necessary to complete the row and returns Some()
531 * if we either finished the entire surface or the lambda returned a
532 * WriteState indicating that we should return to the caller. If the row was
533 * successfully written without either of those things happening, it returns
534 * Nothing(), allowing WritePixelBlocks() to iterate to fill as many rows as
535 * possible.
537 template <typename PixelType, typename Func>
538 Maybe<WriteState> DoWritePixelBlockToRow(Func aFunc) {
539 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
540 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
541 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
543 if (IsSurfaceFinished()) {
544 return Some(WriteState::FINISHED); // We're already done.
547 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
548 int32_t remainder = mInputSize.width - mCol;
549 auto [written, result] = aFunc(&rowPtr[mCol], remainder);
550 if (written == remainder) {
551 MOZ_ASSERT(result.isNothing());
552 mCol = mInputSize.width;
553 AdvanceRow(); // We've finished the row.
554 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
557 MOZ_ASSERT(written >= 0 && written < remainder);
558 MOZ_ASSERT(result.isSome());
560 mCol += written;
561 if (*result == WriteState::FINISHED) {
562 ZeroOutRestOfSurface<PixelType>();
565 return result;
569 * An internal method used to implement both WritePixels() and
570 * WritePixelsToRow(). Those methods differ only in their behavior after a row
571 * is successfully written - WritePixels() continues to write another row,
572 * while WritePixelsToRow() returns to the caller. This method writes a single
573 * row and returns Some() if we either finished the entire surface or the
574 * lambda returned a WriteState indicating that we should return to the
575 * caller. If the row was successfully written without either of those things
576 * happening, it returns Nothing(), allowing WritePixels() and
577 * WritePixelsToRow() to implement their respective behaviors.
579 template <typename PixelType, typename Func>
580 Maybe<WriteState> DoWritePixelsToRow(Func aFunc) {
581 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
582 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
583 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
585 if (IsSurfaceFinished()) {
586 return Some(WriteState::FINISHED); // We're already done.
589 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
591 for (; mCol < mInputSize.width; ++mCol) {
592 NextPixel<PixelType> result = aFunc();
593 if (result.template is<PixelType>()) {
594 rowPtr[mCol] = result.template as<PixelType>();
595 continue;
598 switch (result.template as<WriteState>()) {
599 case WriteState::NEED_MORE_DATA:
600 return Some(WriteState::NEED_MORE_DATA);
602 case WriteState::FINISHED:
603 ZeroOutRestOfSurface<PixelType>();
604 return Some(WriteState::FINISHED);
606 case WriteState::FAILURE:
607 // Note that we don't need to record this anywhere, because this
608 // indicates an error in aFunc, and there's nothing wrong with our
609 // machinery. The caller can recover as needed and continue writing to
610 // the row.
611 return Some(WriteState::FAILURE);
615 AdvanceRow(); // We've finished the row.
617 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
620 template <typename PixelType>
621 void ZeroOutRestOfSurface() {
622 WritePixels<PixelType>([] { return AsVariant(PixelType(0)); });
625 gfx::IntSize mInputSize; /// The size of the input this filter expects.
626 uint8_t* mRowPointer; /// Pointer to the current row or null if finished.
627 int32_t mCol; /// The current column we're writing to. (0-indexed)
628 uint8_t mPixelSize; /// How large each pixel in the surface is, in bytes.
632 * SurfacePipe is the public API that decoders should use to interact with a
633 * SurfaceFilter pipeline.
635 class SurfacePipe {
636 public:
637 SurfacePipe() {}
639 SurfacePipe(SurfacePipe&& aOther) : mHead(std::move(aOther.mHead)) {}
641 ~SurfacePipe() {}
643 SurfacePipe& operator=(SurfacePipe&& aOther) {
644 MOZ_ASSERT(this != &aOther);
645 mHead = std::move(aOther.mHead);
646 return *this;
649 /// Begins a new pass, seeking to the first row of the surface.
650 void ResetToFirstRow() {
651 MOZ_ASSERT(mHead, "Use before configured!");
652 mHead->ResetToFirstRow();
656 * Write pixels to the surface one at a time by repeatedly calling a lambda
657 * that yields pixels. WritePixels() is completely memory safe.
659 * @see SurfaceFilter::WritePixels() for the canonical documentation.
661 template <typename PixelType, typename Func>
662 WriteState WritePixels(Func aFunc) {
663 MOZ_ASSERT(mHead, "Use before configured!");
664 return mHead->WritePixels<PixelType>(std::forward<Func>(aFunc));
668 * A variant of WritePixels() that writes up to a single row of pixels to the
669 * surface in blocks by repeatedly calling a lambda that yields up to the
670 * requested number of pixels.
672 * @see SurfaceFilter::WritePixelBlocks() for the canonical documentation.
674 template <typename PixelType, typename Func>
675 WriteState WritePixelBlocks(Func aFunc) {
676 MOZ_ASSERT(mHead, "Use before configured!");
677 return mHead->WritePixelBlocks<PixelType>(std::forward<Func>(aFunc));
681 * A variant of WritePixels() that writes a single row of pixels to the
682 * surface one at a time by repeatedly calling a lambda that yields pixels.
683 * WritePixelsToRow() is completely memory safe.
685 * @see SurfaceFilter::WritePixelsToRow() for the canonical documentation.
687 template <typename PixelType, typename Func>
688 WriteState WritePixelsToRow(Func aFunc) {
689 MOZ_ASSERT(mHead, "Use before configured!");
690 return mHead->WritePixelsToRow<PixelType>(std::forward<Func>(aFunc));
694 * Write a row to the surface by copying from a buffer. This is bounds checked
695 * and memory safe with respect to the surface, but care must still be taken
696 * by the caller not to overread the source buffer. This variant of
697 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
698 * pixels.
700 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
702 template <typename PixelType>
703 WriteState WriteBuffer(const PixelType* aSource) {
704 MOZ_ASSERT(mHead, "Use before configured!");
705 return mHead->WriteBuffer<PixelType>(aSource);
709 * Write a row to the surface by copying from a buffer. This is bounds checked
710 * and memory safe with respect to the surface, but care must still be taken
711 * by the caller not to overread the source buffer. This variant of
712 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
713 * to the row starting at @aStartColumn. Any pixels in columns before
714 * @aStartColumn or after the pixels copied from the buffer are cleared.
716 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
718 template <typename PixelType>
719 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
720 const size_t aLength) {
721 MOZ_ASSERT(mHead, "Use before configured!");
722 return mHead->WriteBuffer<PixelType>(aSource, aStartColumn, aLength);
726 * Write an empty row to the surface. If some pixels have already been written
727 * to this row, they'll be discarded.
729 * @see SurfaceFilter::WriteEmptyRow() for the canonical documentation.
731 WriteState WriteEmptyRow() {
732 MOZ_ASSERT(mHead, "Use before configured!");
733 return mHead->WriteEmptyRow();
736 /// @return true if we've finished writing to the surface.
737 bool IsSurfaceFinished() const { return mHead->IsSurfaceFinished(); }
739 /// @see SurfaceFilter::TakeInvalidRect() for the canonical documentation.
740 Maybe<SurfaceInvalidRect> TakeInvalidRect() const {
741 MOZ_ASSERT(mHead, "Use before configured!");
742 return mHead->TakeInvalidRect();
745 private:
746 friend class SurfacePipeFactory;
747 friend class TestSurfacePipeFactory;
749 explicit SurfacePipe(UniquePtr<SurfaceFilter>&& aHead)
750 : mHead(std::move(aHead)) {}
752 SurfacePipe(const SurfacePipe&) = delete;
753 SurfacePipe& operator=(const SurfacePipe&) = delete;
755 UniquePtr<SurfaceFilter> mHead; /// The first filter in the chain.
759 * AbstractSurfaceSink contains shared implementation for both SurfaceSink and
760 * ReorientSurfaceSink.
762 class AbstractSurfaceSink : public SurfaceFilter {
763 public:
764 AbstractSurfaceSink()
765 : mImageData(nullptr),
766 mImageDataLength(0),
767 mRow(0),
768 mFlipVertically(false) {}
770 Maybe<SurfaceInvalidRect> TakeInvalidRect() final;
772 protected:
773 uint8_t* DoResetToFirstRow() final;
774 virtual uint8_t* GetRowPointer() const = 0;
776 OrientedIntRect
777 mInvalidRect; /// The region of the surface that has been written
778 /// to since the last call to TakeInvalidRect().
779 uint8_t* mImageData; /// A pointer to the beginning of the surface data.
780 uint32_t mImageDataLength; /// The length of the surface data.
781 uint32_t mRow; /// The row to which we're writing. (0-indexed)
782 bool mFlipVertically; /// If true, write the rows from top to bottom.
785 class SurfaceSink;
787 /// A configuration struct for SurfaceSink.
788 struct SurfaceConfig {
789 using Filter = SurfaceSink;
790 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
791 gfx::IntSize mOutputSize; /// The size of the surface.
792 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
793 bool mFlipVertically; /// If true, write the rows from bottom to top.
794 Maybe<AnimationParams> mAnimParams; /// Given for animated images.
798 * A sink for surfaces. It handles the allocation of the surface and protects
799 * against buffer overflow. This sink should be used for most images.
801 * Sinks must always be at the end of the SurfaceFilter chain.
803 class SurfaceSink final : public AbstractSurfaceSink {
804 public:
805 nsresult Configure(const SurfaceConfig& aConfig);
807 protected:
808 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
809 uint8_t* DoAdvanceRow() final;
810 uint8_t* GetRowPointer() const final;
813 class ReorientSurfaceSink;
815 /// A configuration struct for ReorientSurfaceSink.
816 struct ReorientSurfaceConfig {
817 using Filter = ReorientSurfaceSink;
818 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
819 OrientedIntSize mOutputSize; /// The size of the surface.
820 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
821 Orientation mOrientation; /// The desired orientation of the surface data.
825 * A sink for surfaces. It handles the allocation of the surface and protects
826 * against buffer overflow. This sink should be used for images which have a
827 * non-identity orientation which we want to apply during decoding.
829 * Sinks must always be at the end of the SurfaceFilter chain.
831 class ReorientSurfaceSink final : public AbstractSurfaceSink {
832 public:
833 nsresult Configure(const ReorientSurfaceConfig& aConfig);
835 protected:
836 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
837 uint8_t* DoAdvanceRow() final;
838 uint8_t* GetRowPointer() const final;
840 UniquePtr<uint8_t[]> mBuffer;
841 gfx::ReorientRowFn mReorientFn;
842 gfx::IntSize mSurfaceSize;
845 } // namespace image
846 } // namespace mozilla
848 #endif // mozilla_image_SurfacePipe_h