Bug 1551001 [wpt PR 16740] - Don't mark disconnected tree-scopes for style update...
[gecko.git] / image / SurfacePipe.h
blob224680f29c0e33c2cc4d795d3d3461327c64b37a
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;
130 /// @return a pointer to the buffer for the current row.
131 uint8_t* CurrentRowPointer() const { return mRowPointer; }
133 /// @return true if we've finished writing to the surface.
134 bool IsSurfaceFinished() const { return mRowPointer == nullptr; }
136 /// @return the input size this filter expects.
137 gfx::IntSize InputSize() const { return mInputSize; }
140 * Write pixels to the surface one at a time by repeatedly calling a lambda
141 * that yields pixels. WritePixels() is completely memory safe.
143 * Writing continues until every pixel in the surface has been written to
144 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
145 * which WritePixels() will return to the caller.
147 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
148 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
149 * size passed to ConfigureFilter().
151 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
152 * which means we can remove the PixelType template parameter from this
153 * method.
155 * @param aFunc A lambda that functions as a generator, yielding the next
156 * pixel in the surface each time it's called. The lambda must
157 * return a NextPixel<PixelType> value.
159 * @return A WriteState value indicating the lambda generator's state.
160 * WritePixels() itself will return WriteState::FINISHED if writing
161 * has finished, regardless of the lambda's internal state.
163 template <typename PixelType, typename Func>
164 WriteState WritePixels(Func aFunc) {
165 Maybe<WriteState> result;
166 while (
167 !(result = DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc)))) {
170 return *result;
174 * Write pixels to the surface by calling a lambda which may write as many
175 * pixels as there is remaining to complete the row. It is not completely
176 * memory safe as it trusts the underlying decoder not to overrun the given
177 * buffer, however it is an acceptable tradeoff for performance.
179 * Writing continues until every pixel in the surface has been written to
180 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
181 * which WritePixelBlocks() will return to the caller.
183 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
184 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
185 * size passed to ConfigureFilter().
187 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
188 * which means we can remove the PixelType template parameter from this
189 * method.
191 * @param aFunc A lambda that functions as a generator, yielding at most the
192 * maximum number of pixels requested. The lambda must accept a
193 * pointer argument to the first pixel to write, a maximum
194 * number of pixels to write as part of the block, and return a
195 * NextPixel<PixelType> value.
197 * @return A WriteState value indicating the lambda generator's state.
198 * WritePixelBlocks() itself will return WriteState::FINISHED if
199 * writing has finished, regardless of the lambda's internal state.
201 template <typename PixelType, typename Func>
202 WriteState WritePixelBlocks(Func aFunc) {
203 Maybe<WriteState> result;
204 while (!(result = DoWritePixelBlockToRow<PixelType>(
205 std::forward<Func>(aFunc)))) {
208 return *result;
212 * A variant of WritePixels() that writes a single row of pixels to the
213 * surface one at a time by repeatedly calling a lambda that yields pixels.
214 * WritePixelsToRow() is completely memory safe.
216 * Writing continues until every pixel in the row has been written to. If the
217 * surface is complete at that pointer, WriteState::FINISHED is returned;
218 * otherwise, WritePixelsToRow() returns WriteState::NEED_MORE_DATA. The
219 * lambda can terminate writing early by returning a WriteState itself, which
220 * WritePixelsToRow() will return to the caller.
222 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
223 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
224 * size passed to ConfigureFilter().
226 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
227 * which means we can remove the PixelType template parameter from this
228 * method.
230 * @param aFunc A lambda that functions as a generator, yielding the next
231 * pixel in the surface each time it's called. The lambda must
232 * return a NextPixel<PixelType> value.
234 * @return A WriteState value indicating the lambda generator's state.
235 * WritePixels() itself will return WriteState::FINISHED if writing
236 * the entire surface has finished, or WriteState::NEED_MORE_DATA if
237 * writing the row has finished, regardless of the lambda's internal
238 * state.
240 template <typename PixelType, typename Func>
241 WriteState WritePixelsToRow(Func aFunc) {
242 return DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc))
243 .valueOr(WriteState::NEED_MORE_DATA);
247 * Write a row to the surface by copying from a buffer. This is bounds checked
248 * and memory safe with respect to the surface, but care must still be taken
249 * by the caller not to overread the source buffer. This variant of
250 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
251 * pixels.
253 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
254 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
255 * size passed to ConfigureFilter().
257 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
258 * which means we can remove the PixelType template parameter from this
259 * method.
261 * @param aSource A buffer to copy from. This buffer must be
262 * |mInputSize.width| pixels wide, which means
263 * |mInputSize.width * sizeof(PixelType)| bytes. May not be
264 * null.
266 * @return WriteState::FINISHED if the entire surface has been written to.
267 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
268 * value is passed, returns WriteState::FAILURE.
270 template <typename PixelType>
271 WriteState WriteBuffer(const PixelType* aSource) {
272 return WriteBuffer(aSource, 0, mInputSize.width);
276 * Write a row to the surface by copying from a buffer. This is bounds checked
277 * and memory safe with respect to the surface, but care must still be taken
278 * by the caller not to overread the source buffer. This variant of
279 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
280 * to the row starting at @aStartColumn. Any pixels in columns before
281 * @aStartColumn or after the pixels copied from the buffer are cleared.
283 * Bounds checking failures produce warnings in debug builds because although
284 * the bounds checking maintains safety, this kind of failure could indicate a
285 * bug in the calling code.
287 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
288 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
289 * size passed to ConfigureFilter().
291 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
292 * which means we can remove the PixelType template parameter from this
293 * method.
295 * @param aSource A buffer to copy from. This buffer must be @aLength pixels
296 * wide, which means |aLength * sizeof(PixelType)| bytes. May
297 * not be null.
298 * @param aStartColumn The column to start writing to in the row. Columns
299 * before this are cleared.
300 * @param aLength The number of bytes, at most, which may be copied from
301 * @aSource. Fewer bytes may be copied in practice due to
302 * bounds checking.
304 * @return WriteState::FINISHED if the entire surface has been written to.
305 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
306 * value is passed, returns WriteState::FAILURE.
308 template <typename PixelType>
309 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
310 const size_t aLength) {
311 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
312 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
313 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
315 if (IsSurfaceFinished()) {
316 return WriteState::FINISHED; // Already done.
319 if (MOZ_UNLIKELY(!aSource)) {
320 NS_WARNING("Passed a null pointer to WriteBuffer");
321 return WriteState::FAILURE;
324 PixelType* dest = reinterpret_cast<PixelType*>(mRowPointer);
326 // Clear the area before |aStartColumn|.
327 const size_t prefixLength =
328 std::min<size_t>(mInputSize.width, aStartColumn);
329 if (MOZ_UNLIKELY(prefixLength != aStartColumn)) {
330 NS_WARNING("Provided starting column is out-of-bounds in WriteBuffer");
333 memset(dest, 0, mInputSize.width * sizeof(PixelType));
334 dest += prefixLength;
336 // Write |aLength| pixels from |aSource| into the row, with bounds checking.
337 const size_t bufferLength =
338 std::min<size_t>(mInputSize.width - prefixLength, aLength);
339 if (MOZ_UNLIKELY(bufferLength != aLength)) {
340 NS_WARNING("Provided buffer length is out-of-bounds in WriteBuffer");
343 memcpy(dest, aSource, bufferLength * sizeof(PixelType));
344 dest += bufferLength;
346 // Clear the rest of the row.
347 const size_t suffixLength =
348 mInputSize.width - (prefixLength + bufferLength);
349 memset(dest, 0, suffixLength * sizeof(PixelType));
351 AdvanceRow();
353 return IsSurfaceFinished() ? WriteState::FINISHED
354 : WriteState::NEED_MORE_DATA;
358 * Write an empty row to the surface. If some pixels have already been written
359 * to this row, they'll be discarded.
361 * @return WriteState::FINISHED if the entire surface has been written to.
362 * Otherwise, returns WriteState::NEED_MORE_DATA.
364 WriteState WriteEmptyRow() {
365 if (IsSurfaceFinished()) {
366 return WriteState::FINISHED; // Already done.
369 memset(mRowPointer, 0, mInputSize.width * mPixelSize);
370 AdvanceRow();
372 return IsSurfaceFinished() ? WriteState::FINISHED
373 : WriteState::NEED_MORE_DATA;
377 * Write a row to the surface by calling a lambda that uses a pointer to
378 * directly write to the row. This is unsafe because SurfaceFilter can't
379 * provide any bounds checking; that's up to the lambda itself. For this
380 * reason, the other Write*() methods should be preferred whenever it's
381 * possible to use them; WriteUnsafeComputedRow() should be used only when
382 * it's absolutely necessary to avoid extra copies or other performance
383 * penalties.
385 * This method should never be exposed to SurfacePipe consumers; it's strictly
386 * for use in SurfaceFilters. If external code needs this method, it should
387 * probably be turned into a SurfaceFilter.
389 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
390 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
391 * size passed to ConfigureFilter().
393 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
394 * which means we can remove the PixelType template parameter from this
395 * method.
397 * @param aFunc A lambda that writes directly to the row.
399 * @return WriteState::FINISHED if the entire surface has been written to.
400 * Otherwise, returns WriteState::NEED_MORE_DATA.
402 template <typename PixelType, typename Func>
403 WriteState WriteUnsafeComputedRow(Func aFunc) {
404 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
405 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
406 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
408 if (IsSurfaceFinished()) {
409 return WriteState::FINISHED; // Already done.
412 // Call the provided lambda with a pointer to the buffer for the current
413 // row. This is unsafe because we can't do any bounds checking; the lambda
414 // itself has to be responsible for that.
415 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
416 aFunc(rowPtr, mInputSize.width);
417 AdvanceRow();
419 return IsSurfaceFinished() ? WriteState::FINISHED
420 : WriteState::NEED_MORE_DATA;
423 //////////////////////////////////////////////////////////////////////////////
424 // Methods Subclasses Should Override
425 //////////////////////////////////////////////////////////////////////////////
428 * @return a SurfaceInvalidRect representing the region of the surface that
429 * has been written to since the last time TakeInvalidRect() was
430 * called, or Nothing() if the region is empty (i.e. nothing has been
431 * written).
433 virtual Maybe<SurfaceInvalidRect> TakeInvalidRect() = 0;
435 protected:
437 * Called by ResetToFirstRow() to actually perform the reset. It's legal to
438 * throw away any previously written data at this point, as all rows must be
439 * written to on every pass.
441 virtual uint8_t* DoResetToFirstRow() = 0;
444 * Called by AdvanceRow() to actually advance this filter to the next row.
446 * @return a pointer to the buffer for the next row, or nullptr to indicate
447 * that we've finished the entire surface.
449 virtual uint8_t* DoAdvanceRow() = 0;
451 //////////////////////////////////////////////////////////////////////////////
452 // Methods For Internal Use By Subclasses
453 //////////////////////////////////////////////////////////////////////////////
456 * Called by subclasses' Configure() methods to initialize the configuration
457 * of this filter. After the filter is configured, calls ResetToFirstRow().
459 * @param aInputSize The input size of this filter, in pixels. The previous
460 * filter in the chain will expect to write into rows
461 * |aInputSize.width| pixels wide.
462 * @param aPixelSize How large, in bytes, each pixel in the surface is. This
463 * should be either 1 for paletted images or 4 for BGRA/BGRX
464 * images.
466 void ConfigureFilter(gfx::IntSize aInputSize, uint8_t aPixelSize) {
467 mInputSize = aInputSize;
468 mPixelSize = aPixelSize;
470 ResetToFirstRow();
473 private:
475 * An internal method used to implement WritePixelBlocks. This method writes
476 * up to the number of pixels necessary to complete the row and returns Some()
477 * if we either finished the entire surface or the lambda returned a
478 * WriteState indicating that we should return to the caller. If the row was
479 * successfully written without either of those things happening, it returns
480 * Nothing(), allowing WritePixelBlocks() to iterate to fill as many rows as
481 * possible.
483 template <typename PixelType, typename Func>
484 Maybe<WriteState> DoWritePixelBlockToRow(Func aFunc) {
485 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
486 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
487 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
489 if (IsSurfaceFinished()) {
490 return Some(WriteState::FINISHED); // We're already done.
493 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
494 int32_t remainder = mInputSize.width - mCol;
495 int32_t written;
496 Maybe<WriteState> result;
497 Tie(written, result) = aFunc(&rowPtr[mCol], remainder);
498 if (written == remainder) {
499 MOZ_ASSERT(result.isNothing());
500 mCol = mInputSize.width;
501 AdvanceRow(); // We've finished the row.
502 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
505 MOZ_ASSERT(written >= 0 && written < remainder);
506 MOZ_ASSERT(result.isSome());
508 mCol += written;
509 if (*result == WriteState::FINISHED) {
510 ZeroOutRestOfSurface<PixelType>();
513 return result;
517 * An internal method used to implement both WritePixels() and
518 * WritePixelsToRow(). Those methods differ only in their behavior after a row
519 * is successfully written - WritePixels() continues to write another row,
520 * while WritePixelsToRow() returns to the caller. This method writes a single
521 * row and returns Some() if we either finished the entire surface or the
522 * lambda returned a WriteState indicating that we should return to the
523 * caller. If the row was successfully written without either of those things
524 * happening, it returns Nothing(), allowing WritePixels() and
525 * WritePixelsToRow() to implement their respective behaviors.
527 template <typename PixelType, typename Func>
528 Maybe<WriteState> DoWritePixelsToRow(Func aFunc) {
529 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
530 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
531 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
533 if (IsSurfaceFinished()) {
534 return Some(WriteState::FINISHED); // We're already done.
537 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
539 for (; mCol < mInputSize.width; ++mCol) {
540 NextPixel<PixelType> result = aFunc();
541 if (result.template is<PixelType>()) {
542 rowPtr[mCol] = result.template as<PixelType>();
543 continue;
546 switch (result.template as<WriteState>()) {
547 case WriteState::NEED_MORE_DATA:
548 return Some(WriteState::NEED_MORE_DATA);
550 case WriteState::FINISHED:
551 ZeroOutRestOfSurface<PixelType>();
552 return Some(WriteState::FINISHED);
554 case WriteState::FAILURE:
555 // Note that we don't need to record this anywhere, because this
556 // indicates an error in aFunc, and there's nothing wrong with our
557 // machinery. The caller can recover as needed and continue writing to
558 // the row.
559 return Some(WriteState::FAILURE);
563 AdvanceRow(); // We've finished the row.
565 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
568 template <typename PixelType>
569 void ZeroOutRestOfSurface() {
570 WritePixels<PixelType>([] { return AsVariant(PixelType(0)); });
573 gfx::IntSize mInputSize; /// The size of the input this filter expects.
574 uint8_t* mRowPointer; /// Pointer to the current row or null if finished.
575 int32_t mCol; /// The current column we're writing to. (0-indexed)
576 uint8_t mPixelSize; /// How large each pixel in the surface is, in bytes.
580 * SurfacePipe is the public API that decoders should use to interact with a
581 * SurfaceFilter pipeline.
583 class SurfacePipe {
584 public:
585 SurfacePipe() {}
587 SurfacePipe(SurfacePipe&& aOther) : mHead(std::move(aOther.mHead)) {}
589 ~SurfacePipe() {}
591 SurfacePipe& operator=(SurfacePipe&& aOther) {
592 MOZ_ASSERT(this != &aOther);
593 mHead = std::move(aOther.mHead);
594 return *this;
597 /// Begins a new pass, seeking to the first row of the surface.
598 void ResetToFirstRow() {
599 MOZ_ASSERT(mHead, "Use before configured!");
600 mHead->ResetToFirstRow();
604 * Write pixels to the surface one at a time by repeatedly calling a lambda
605 * that yields pixels. WritePixels() is completely memory safe.
607 * @see SurfaceFilter::WritePixels() for the canonical documentation.
609 template <typename PixelType, typename Func>
610 WriteState WritePixels(Func aFunc) {
611 MOZ_ASSERT(mHead, "Use before configured!");
612 return mHead->WritePixels<PixelType>(std::forward<Func>(aFunc));
616 * A variant of WritePixels() that writes up to a single row of pixels to the
617 * surface in blocks by repeatedly calling a lambda that yields up to the
618 * requested number of pixels.
620 * @see SurfaceFilter::WritePixelBlocks() for the canonical documentation.
622 template <typename PixelType, typename Func>
623 WriteState WritePixelBlocks(Func aFunc) {
624 MOZ_ASSERT(mHead, "Use before configured!");
625 return mHead->WritePixelBlocks<PixelType>(std::forward<Func>(aFunc));
629 * A variant of WritePixels() that writes a single row of pixels to the
630 * surface one at a time by repeatedly calling a lambda that yields pixels.
631 * WritePixelsToRow() is completely memory safe.
633 * @see SurfaceFilter::WritePixelsToRow() for the canonical documentation.
635 template <typename PixelType, typename Func>
636 WriteState WritePixelsToRow(Func aFunc) {
637 MOZ_ASSERT(mHead, "Use before configured!");
638 return mHead->WritePixelsToRow<PixelType>(std::forward<Func>(aFunc));
642 * Write a row to the surface by copying from a buffer. This is bounds checked
643 * and memory safe with respect to the surface, but care must still be taken
644 * by the caller not to overread the source buffer. This variant of
645 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
646 * pixels.
648 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
650 template <typename PixelType>
651 WriteState WriteBuffer(const PixelType* aSource) {
652 MOZ_ASSERT(mHead, "Use before configured!");
653 return mHead->WriteBuffer<PixelType>(aSource);
657 * Write a row to the surface by copying from a buffer. This is bounds checked
658 * and memory safe with respect to the surface, but care must still be taken
659 * by the caller not to overread the source buffer. This variant of
660 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
661 * to the row starting at @aStartColumn. Any pixels in columns before
662 * @aStartColumn or after the pixels copied from the buffer are cleared.
664 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
666 template <typename PixelType>
667 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
668 const size_t aLength) {
669 MOZ_ASSERT(mHead, "Use before configured!");
670 return mHead->WriteBuffer<PixelType>(aSource, aStartColumn, aLength);
674 * Write an empty row to the surface. If some pixels have already been written
675 * to this row, they'll be discarded.
677 * @see SurfaceFilter::WriteEmptyRow() for the canonical documentation.
679 WriteState WriteEmptyRow() {
680 MOZ_ASSERT(mHead, "Use before configured!");
681 return mHead->WriteEmptyRow();
684 /// @return true if we've finished writing to the surface.
685 bool IsSurfaceFinished() const { return mHead->IsSurfaceFinished(); }
687 /// @see SurfaceFilter::TakeInvalidRect() for the canonical documentation.
688 Maybe<SurfaceInvalidRect> TakeInvalidRect() const {
689 MOZ_ASSERT(mHead, "Use before configured!");
690 return mHead->TakeInvalidRect();
693 private:
694 friend class SurfacePipeFactory;
695 friend class TestSurfacePipeFactory;
697 explicit SurfacePipe(UniquePtr<SurfaceFilter>&& aHead)
698 : mHead(std::move(aHead)) {}
700 SurfacePipe(const SurfacePipe&) = delete;
701 SurfacePipe& operator=(const SurfacePipe&) = delete;
703 UniquePtr<SurfaceFilter> mHead; /// The first filter in the chain.
707 * AbstractSurfaceSink contains shared implementation for both SurfaceSink and
708 * PalettedSurfaceSink.
710 class AbstractSurfaceSink : public SurfaceFilter {
711 public:
712 AbstractSurfaceSink()
713 : mImageData(nullptr),
714 mImageDataLength(0),
715 mRow(0),
716 mFlipVertically(false) {}
718 Maybe<SurfaceInvalidRect> TakeInvalidRect() final;
720 protected:
721 uint8_t* DoResetToFirstRow() final;
722 uint8_t* DoAdvanceRow() final;
723 virtual uint8_t* GetRowPointer() const = 0;
725 gfx::IntRect
726 mInvalidRect; /// The region of the surface that has been written
727 /// to since the last call to TakeInvalidRect().
728 uint8_t* mImageData; /// A pointer to the beginning of the surface data.
729 uint32_t mImageDataLength; /// The length of the surface data.
730 uint32_t mRow; /// The row to which we're writing. (0-indexed)
731 bool mFlipVertically; /// If true, write the rows from top to bottom.
734 class SurfaceSink;
736 /// A configuration struct for SurfaceSink.
737 struct SurfaceConfig {
738 using Filter = SurfaceSink;
739 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
740 gfx::IntSize mOutputSize; /// The size of the surface.
741 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
742 bool mFlipVertically; /// If true, write the rows from bottom to top.
743 Maybe<AnimationParams> mAnimParams; /// Given for animated images.
747 * A sink for surfaces. It handles the allocation of the surface and protects
748 * against buffer overflow. This sink should be used for images.
750 * Sinks must always be at the end of the SurfaceFilter chain.
752 class SurfaceSink final : public AbstractSurfaceSink {
753 public:
754 nsresult Configure(const SurfaceConfig& aConfig);
756 protected:
757 uint8_t* GetRowPointer() const override;
760 } // namespace image
761 } // namespace mozilla
763 #endif // mozilla_image_SurfacePipe_h