Bug 1601637 [wpt PR 20633] - Add tests for Animation::setPlaybackRate, a=testonly
[gecko.git] / image / SurfacePipe.h
blob7bcadf050b39b3be1b0c61f6decf06e8b5236285
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 "nsDebug.h"
29 #include "mozilla/Likely.h"
30 #include "mozilla/Maybe.h"
31 #include "mozilla/Move.h"
32 #include "mozilla/Tuple.h"
33 #include "mozilla/UniquePtr.h"
34 #include "mozilla/Unused.h"
35 #include "mozilla/Variant.h"
36 #include "mozilla/gfx/2D.h"
38 #include "AnimationParams.h"
40 namespace mozilla {
41 namespace image {
43 class Decoder;
45 /**
46 * An invalid rect for a surface. Results are given both in the space of the
47 * input image (i.e., before any SurfaceFilters are applied) and in the space
48 * of the output surface (after all SurfaceFilters).
50 struct SurfaceInvalidRect {
51 gfx::IntRect mInputSpaceRect; /// The invalid rect in pre-SurfacePipe space.
52 gfx::IntRect
53 mOutputSpaceRect; /// The invalid rect in post-SurfacePipe space.
56 /**
57 * An enum used to allow the lambdas passed to WritePixels() to communicate
58 * their state to the caller.
60 enum class WriteState : uint8_t {
61 NEED_MORE_DATA, /// The lambda ran out of data.
63 FINISHED, /// The lambda is done writing to the surface; future writes
64 /// will fail.
66 FAILURE /// The lambda encountered an error. The caller may recover
67 /// if possible and continue to write. (This never indicates
68 /// an error in the SurfacePipe machinery itself; it's only
69 /// generated by the lambdas.)
72 /**
73 * A template alias used to make the return value of WritePixels() lambdas
74 * (which may return either a pixel value or a WriteState) easier to specify.
76 template <typename PixelType>
77 using NextPixel = Variant<PixelType, WriteState>;
79 /**
80 * SurfaceFilter is the abstract superclass of SurfacePipe pipeline stages. It
81 * implements the the code that actually writes to the surface - WritePixels()
82 * and the other Write*() methods - which are non-virtual for efficiency.
84 * SurfaceFilter's API is nonpublic; only SurfacePipe and other SurfaceFilters
85 * should use it. Non-SurfacePipe code should use the methods on SurfacePipe.
87 * To implement a SurfaceFilter, it's necessary to subclass SurfaceFilter and
88 * implement, at a minimum, the pure virtual methods. It's also necessary to
89 * define a Config struct with a Filter typedef member that identifies the
90 * matching SurfaceFilter class, and a Configure() template method. See an
91 * existing SurfaceFilter subclass, such as RemoveFrameRectFilter, for an
92 * example of how the Configure() method must be implemented. It takes a list of
93 * Config structs, passes the tail of the list to the next filter in the chain's
94 * Configure() method, and then uses the head of the list to configure itself. A
95 * SurfaceFilter's Configure() method must also call
96 * SurfaceFilter::ConfigureFilter() to provide the Write*() methods with the
97 * information they need to do their jobs.
99 class SurfaceFilter {
100 public:
101 SurfaceFilter() : mRowPointer(nullptr), mCol(0), mPixelSize(0) {}
103 virtual ~SurfaceFilter() {}
106 * Reset this surface to the first row. It's legal for this filter to throw
107 * away any previously written data at this point, as all rows must be written
108 * to on every pass.
110 * @return a pointer to the buffer for the first row.
112 uint8_t* ResetToFirstRow() {
113 mCol = 0;
114 mRowPointer = DoResetToFirstRow();
115 return mRowPointer;
119 * Called by WritePixels() to advance this filter to the next row.
121 * @return a pointer to the buffer for the next row, or nullptr to indicate
122 * that we've finished the entire surface.
124 uint8_t* AdvanceRow() {
125 mCol = 0;
126 mRowPointer = DoAdvanceRow();
127 return mRowPointer;
131 * Called by WriteBuffer() to advance this filter to the next row, if the
132 * supplied row is a full row.
134 * @return a pointer to the buffer for the next row, or nullptr to indicate
135 * that we've finished the entire surface.
137 uint8_t* AdvanceRow(const uint8_t* aInputRow) {
138 mCol = 0;
139 mRowPointer = DoAdvanceRowFromBuffer(aInputRow);
140 return mRowPointer;
143 /// @return a pointer to the buffer for the current row.
144 uint8_t* CurrentRowPointer() const { return mRowPointer; }
146 /// @return true if we've finished writing to the surface.
147 bool IsSurfaceFinished() const { return mRowPointer == nullptr; }
149 /// @return the input size this filter expects.
150 gfx::IntSize InputSize() const { return mInputSize; }
153 * Write pixels to the surface one at a time by repeatedly calling a lambda
154 * that yields pixels. WritePixels() is completely memory safe.
156 * Writing continues until every pixel in the surface has been written to
157 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
158 * which WritePixels() will return to the caller.
160 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
161 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
162 * size passed to ConfigureFilter().
164 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
165 * which means we can remove the PixelType template parameter from this
166 * method.
168 * @param aFunc A lambda that functions as a generator, yielding the next
169 * pixel in the surface each time it's called. The lambda must
170 * return a NextPixel<PixelType> value.
172 * @return A WriteState value indicating the lambda generator's state.
173 * WritePixels() itself will return WriteState::FINISHED if writing
174 * has finished, regardless of the lambda's internal state.
176 template <typename PixelType, typename Func>
177 WriteState WritePixels(Func aFunc) {
178 Maybe<WriteState> result;
179 while (
180 !(result = DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc)))) {
183 return *result;
187 * Write pixels to the surface by calling a lambda which may write as many
188 * pixels as there is remaining to complete the row. It is not completely
189 * memory safe as it trusts the underlying decoder not to overrun the given
190 * buffer, however it is an acceptable tradeoff for performance.
192 * Writing continues until every pixel in the surface has been written to
193 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
194 * which WritePixelBlocks() will return to the caller.
196 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
197 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
198 * size passed to ConfigureFilter().
200 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
201 * which means we can remove the PixelType template parameter from this
202 * method.
204 * @param aFunc A lambda that functions as a generator, yielding at most the
205 * maximum number of pixels requested. The lambda must accept a
206 * pointer argument to the first pixel to write, a maximum
207 * number of pixels to write as part of the block, and return a
208 * NextPixel<PixelType> value.
210 * @return A WriteState value indicating the lambda generator's state.
211 * WritePixelBlocks() itself will return WriteState::FINISHED if
212 * writing has finished, regardless of the lambda's internal state.
214 template <typename PixelType, typename Func>
215 WriteState WritePixelBlocks(Func aFunc) {
216 Maybe<WriteState> result;
217 while (!(result = DoWritePixelBlockToRow<PixelType>(
218 std::forward<Func>(aFunc)))) {
221 return *result;
225 * A variant of WritePixels() that writes a single row of pixels to the
226 * surface one at a time by repeatedly calling a lambda that yields pixels.
227 * WritePixelsToRow() is completely memory safe.
229 * Writing continues until every pixel in the row has been written to. If the
230 * surface is complete at that pointer, WriteState::FINISHED is returned;
231 * otherwise, WritePixelsToRow() returns WriteState::NEED_MORE_DATA. The
232 * lambda can terminate writing early by returning a WriteState itself, which
233 * WritePixelsToRow() will return to the caller.
235 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
236 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
237 * size passed to ConfigureFilter().
239 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
240 * which means we can remove the PixelType template parameter from this
241 * method.
243 * @param aFunc A lambda that functions as a generator, yielding the next
244 * pixel in the surface each time it's called. The lambda must
245 * return a NextPixel<PixelType> value.
247 * @return A WriteState value indicating the lambda generator's state.
248 * WritePixels() itself will return WriteState::FINISHED if writing
249 * the entire surface has finished, or WriteState::NEED_MORE_DATA if
250 * writing the row has finished, regardless of the lambda's internal
251 * state.
253 template <typename PixelType, typename Func>
254 WriteState WritePixelsToRow(Func aFunc) {
255 return DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc))
256 .valueOr(WriteState::NEED_MORE_DATA);
260 * Write a row to the surface by copying from a buffer. This is bounds checked
261 * and memory safe with respect to the surface, but care must still be taken
262 * by the caller not to overread the source buffer. This variant of
263 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
264 * pixels.
266 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
267 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
268 * size passed to ConfigureFilter().
270 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
271 * which means we can remove the PixelType template parameter from this
272 * method.
274 * @param aSource A buffer to copy from. This buffer must be
275 * |mInputSize.width| pixels wide, which means
276 * |mInputSize.width * sizeof(PixelType)| bytes. May not be
277 * null.
279 * @return WriteState::FINISHED if the entire surface has been written to.
280 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
281 * value is passed, returns WriteState::FAILURE.
283 template <typename PixelType>
284 WriteState WriteBuffer(const PixelType* aSource) {
285 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
286 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
287 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
289 if (IsSurfaceFinished()) {
290 return WriteState::FINISHED; // Already done.
293 if (MOZ_UNLIKELY(!aSource)) {
294 NS_WARNING("Passed a null pointer to WriteBuffer");
295 return WriteState::FAILURE;
298 AdvanceRow(reinterpret_cast<const uint8_t*>(aSource));
299 return IsSurfaceFinished() ? WriteState::FINISHED
300 : WriteState::NEED_MORE_DATA;
304 * Write a row to the surface by copying from a buffer. This is bounds checked
305 * and memory safe with respect to the surface, but care must still be taken
306 * by the caller not to overread the source buffer. This variant of
307 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
308 * to the row starting at @aStartColumn. Any pixels in columns before
309 * @aStartColumn or after the pixels copied from the buffer are cleared.
311 * Bounds checking failures produce warnings in debug builds because although
312 * the bounds checking maintains safety, this kind of failure could indicate a
313 * bug in the calling code.
315 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
316 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
317 * size passed to ConfigureFilter().
319 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
320 * which means we can remove the PixelType template parameter from this
321 * method.
323 * @param aSource A buffer to copy from. This buffer must be @aLength pixels
324 * wide, which means |aLength * sizeof(PixelType)| bytes. May
325 * not be null.
326 * @param aStartColumn The column to start writing to in the row. Columns
327 * before this are cleared.
328 * @param aLength The number of bytes, at most, which may be copied from
329 * @aSource. Fewer bytes may be copied in practice due to
330 * bounds checking.
332 * @return WriteState::FINISHED if the entire surface has been written to.
333 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
334 * value is passed, returns WriteState::FAILURE.
336 template <typename PixelType>
337 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
338 const size_t aLength) {
339 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
340 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
341 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
343 if (IsSurfaceFinished()) {
344 return WriteState::FINISHED; // Already done.
347 if (MOZ_UNLIKELY(!aSource)) {
348 NS_WARNING("Passed a null pointer to WriteBuffer");
349 return WriteState::FAILURE;
352 PixelType* dest = reinterpret_cast<PixelType*>(mRowPointer);
354 // Clear the area before |aStartColumn|.
355 const size_t prefixLength =
356 std::min<size_t>(mInputSize.width, aStartColumn);
357 if (MOZ_UNLIKELY(prefixLength != aStartColumn)) {
358 NS_WARNING("Provided starting column is out-of-bounds in WriteBuffer");
361 memset(dest, 0, mInputSize.width * sizeof(PixelType));
362 dest += prefixLength;
364 // Write |aLength| pixels from |aSource| into the row, with bounds checking.
365 const size_t bufferLength =
366 std::min<size_t>(mInputSize.width - prefixLength, aLength);
367 if (MOZ_UNLIKELY(bufferLength != aLength)) {
368 NS_WARNING("Provided buffer length is out-of-bounds in WriteBuffer");
371 memcpy(dest, aSource, bufferLength * sizeof(PixelType));
372 dest += bufferLength;
374 // Clear the rest of the row.
375 const size_t suffixLength =
376 mInputSize.width - (prefixLength + bufferLength);
377 memset(dest, 0, suffixLength * sizeof(PixelType));
379 AdvanceRow();
381 return IsSurfaceFinished() ? WriteState::FINISHED
382 : WriteState::NEED_MORE_DATA;
386 * Write an empty row to the surface. If some pixels have already been written
387 * to this row, they'll be discarded.
389 * @return WriteState::FINISHED if the entire surface has been written to.
390 * Otherwise, returns WriteState::NEED_MORE_DATA.
392 WriteState WriteEmptyRow() {
393 if (IsSurfaceFinished()) {
394 return WriteState::FINISHED; // Already done.
397 memset(mRowPointer, 0, mInputSize.width * mPixelSize);
398 AdvanceRow();
400 return IsSurfaceFinished() ? WriteState::FINISHED
401 : WriteState::NEED_MORE_DATA;
405 * Write a row to the surface by calling a lambda that uses a pointer to
406 * directly write to the row. This is unsafe because SurfaceFilter can't
407 * provide any bounds checking; that's up to the lambda itself. For this
408 * reason, the other Write*() methods should be preferred whenever it's
409 * possible to use them; WriteUnsafeComputedRow() should be used only when
410 * it's absolutely necessary to avoid extra copies or other performance
411 * penalties.
413 * This method should never be exposed to SurfacePipe consumers; it's strictly
414 * for use in SurfaceFilters. If external code needs this method, it should
415 * probably be turned into a SurfaceFilter.
417 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
418 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
419 * size passed to ConfigureFilter().
421 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
422 * which means we can remove the PixelType template parameter from this
423 * method.
425 * @param aFunc A lambda that writes directly to the row.
427 * @return WriteState::FINISHED if the entire surface has been written to.
428 * Otherwise, returns WriteState::NEED_MORE_DATA.
430 template <typename PixelType, typename Func>
431 WriteState WriteUnsafeComputedRow(Func aFunc) {
432 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
433 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
434 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
436 if (IsSurfaceFinished()) {
437 return WriteState::FINISHED; // Already done.
440 // Call the provided lambda with a pointer to the buffer for the current
441 // row. This is unsafe because we can't do any bounds checking; the lambda
442 // itself has to be responsible for that.
443 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
444 aFunc(rowPtr, mInputSize.width);
445 AdvanceRow();
447 return IsSurfaceFinished() ? WriteState::FINISHED
448 : WriteState::NEED_MORE_DATA;
451 //////////////////////////////////////////////////////////////////////////////
452 // Methods Subclasses Should Override
453 //////////////////////////////////////////////////////////////////////////////
456 * @return a SurfaceInvalidRect representing the region of the surface that
457 * has been written to since the last time TakeInvalidRect() was
458 * called, or Nothing() if the region is empty (i.e. nothing has been
459 * written).
461 virtual Maybe<SurfaceInvalidRect> TakeInvalidRect() = 0;
463 protected:
465 * Called by ResetToFirstRow() to actually perform the reset. It's legal to
466 * throw away any previously written data at this point, as all rows must be
467 * written to on every pass.
469 virtual uint8_t* DoResetToFirstRow() = 0;
472 * Called by AdvanceRow() to actually advance this filter to the next row.
474 * @param aInputRow The input row supplied by the decoder.
476 * @return a pointer to the buffer for the next row, or nullptr to indicate
477 * that we've finished the entire surface.
479 virtual uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) = 0;
482 * Called by AdvanceRow() to actually advance this filter to the next row.
484 * @return a pointer to the buffer for the next row, or nullptr to indicate
485 * that we've finished the entire surface.
487 virtual uint8_t* DoAdvanceRow() = 0;
489 //////////////////////////////////////////////////////////////////////////////
490 // Methods For Internal Use By Subclasses
491 //////////////////////////////////////////////////////////////////////////////
494 * Called by subclasses' Configure() methods to initialize the configuration
495 * of this filter. After the filter is configured, calls ResetToFirstRow().
497 * @param aInputSize The input size of this filter, in pixels. The previous
498 * filter in the chain will expect to write into rows
499 * |aInputSize.width| pixels wide.
500 * @param aPixelSize How large, in bytes, each pixel in the surface is. This
501 * should be either 1 for paletted images or 4 for BGRA/BGRX
502 * images.
504 void ConfigureFilter(gfx::IntSize aInputSize, uint8_t aPixelSize) {
505 mInputSize = aInputSize;
506 mPixelSize = aPixelSize;
508 ResetToFirstRow();
512 * Called by subclasses' DoAdvanceRowFromBuffer() methods to copy a decoder
513 * supplied row buffer into its internal row pointer. Ideally filters at the
514 * top of the filter pipeline are able to consume the decoder row buffer
515 * directly without the extra copy prior to performing its transformation.
517 * @param aInputRow The input row supplied by the decoder.
519 void CopyInputRow(const uint8_t* aInputRow) {
520 MOZ_ASSERT(aInputRow);
521 MOZ_ASSERT(mCol == 0);
522 memcpy(mRowPointer, aInputRow, mPixelSize * mInputSize.width);
525 private:
527 * An internal method used to implement WritePixelBlocks. This method writes
528 * up to the number of pixels necessary to complete the row and returns Some()
529 * if we either finished the entire surface or the lambda returned a
530 * WriteState indicating that we should return to the caller. If the row was
531 * successfully written without either of those things happening, it returns
532 * Nothing(), allowing WritePixelBlocks() to iterate to fill as many rows as
533 * possible.
535 template <typename PixelType, typename Func>
536 Maybe<WriteState> DoWritePixelBlockToRow(Func aFunc) {
537 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
538 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
539 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
541 if (IsSurfaceFinished()) {
542 return Some(WriteState::FINISHED); // We're already done.
545 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
546 int32_t remainder = mInputSize.width - mCol;
547 int32_t written;
548 Maybe<WriteState> result;
549 Tie(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 * PalettedSurfaceSink.
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 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
775 uint8_t* DoAdvanceRow() final;
776 virtual uint8_t* GetRowPointer() const = 0;
778 gfx::IntRect
779 mInvalidRect; /// The region of the surface that has been written
780 /// to since the last call to TakeInvalidRect().
781 uint8_t* mImageData; /// A pointer to the beginning of the surface data.
782 uint32_t mImageDataLength; /// The length of the surface data.
783 uint32_t mRow; /// The row to which we're writing. (0-indexed)
784 bool mFlipVertically; /// If true, write the rows from top to bottom.
787 class SurfaceSink;
789 /// A configuration struct for SurfaceSink.
790 struct SurfaceConfig {
791 using Filter = SurfaceSink;
792 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
793 gfx::IntSize mOutputSize; /// The size of the surface.
794 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
795 bool mFlipVertically; /// If true, write the rows from bottom to top.
796 Maybe<AnimationParams> mAnimParams; /// Given for animated images.
800 * A sink for surfaces. It handles the allocation of the surface and protects
801 * against buffer overflow. This sink should be used for images.
803 * Sinks must always be at the end of the SurfaceFilter chain.
805 class SurfaceSink final : public AbstractSurfaceSink {
806 public:
807 nsresult Configure(const SurfaceConfig& aConfig);
809 protected:
810 uint8_t* GetRowPointer() const override;
813 } // namespace image
814 } // namespace mozilla
816 #endif // mozilla_image_SurfacePipe_h