Bug 1790985 - Add support for dumping inlined functions when scraping Linux system...
[gecko.git] / image / SurfacePipe.h
blob0c3533c9f940d6e5557d6728f0b389f33d561a96
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"
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"
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 int32_t written;
550 Maybe<WriteState> result;
551 Tie(written, result) = aFunc(&rowPtr[mCol], remainder);
552 if (written == remainder) {
553 MOZ_ASSERT(result.isNothing());
554 mCol = mInputSize.width;
555 AdvanceRow(); // We've finished the row.
556 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
559 MOZ_ASSERT(written >= 0 && written < remainder);
560 MOZ_ASSERT(result.isSome());
562 mCol += written;
563 if (*result == WriteState::FINISHED) {
564 ZeroOutRestOfSurface<PixelType>();
567 return result;
571 * An internal method used to implement both WritePixels() and
572 * WritePixelsToRow(). Those methods differ only in their behavior after a row
573 * is successfully written - WritePixels() continues to write another row,
574 * while WritePixelsToRow() returns to the caller. This method writes a single
575 * row and returns Some() if we either finished the entire surface or the
576 * lambda returned a WriteState indicating that we should return to the
577 * caller. If the row was successfully written without either of those things
578 * happening, it returns Nothing(), allowing WritePixels() and
579 * WritePixelsToRow() to implement their respective behaviors.
581 template <typename PixelType, typename Func>
582 Maybe<WriteState> DoWritePixelsToRow(Func aFunc) {
583 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
584 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
585 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
587 if (IsSurfaceFinished()) {
588 return Some(WriteState::FINISHED); // We're already done.
591 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
593 for (; mCol < mInputSize.width; ++mCol) {
594 NextPixel<PixelType> result = aFunc();
595 if (result.template is<PixelType>()) {
596 rowPtr[mCol] = result.template as<PixelType>();
597 continue;
600 switch (result.template as<WriteState>()) {
601 case WriteState::NEED_MORE_DATA:
602 return Some(WriteState::NEED_MORE_DATA);
604 case WriteState::FINISHED:
605 ZeroOutRestOfSurface<PixelType>();
606 return Some(WriteState::FINISHED);
608 case WriteState::FAILURE:
609 // Note that we don't need to record this anywhere, because this
610 // indicates an error in aFunc, and there's nothing wrong with our
611 // machinery. The caller can recover as needed and continue writing to
612 // the row.
613 return Some(WriteState::FAILURE);
617 AdvanceRow(); // We've finished the row.
619 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
622 template <typename PixelType>
623 void ZeroOutRestOfSurface() {
624 WritePixels<PixelType>([] { return AsVariant(PixelType(0)); });
627 gfx::IntSize mInputSize; /// The size of the input this filter expects.
628 uint8_t* mRowPointer; /// Pointer to the current row or null if finished.
629 int32_t mCol; /// The current column we're writing to. (0-indexed)
630 uint8_t mPixelSize; /// How large each pixel in the surface is, in bytes.
634 * SurfacePipe is the public API that decoders should use to interact with a
635 * SurfaceFilter pipeline.
637 class SurfacePipe {
638 public:
639 SurfacePipe() {}
641 SurfacePipe(SurfacePipe&& aOther) : mHead(std::move(aOther.mHead)) {}
643 ~SurfacePipe() {}
645 SurfacePipe& operator=(SurfacePipe&& aOther) {
646 MOZ_ASSERT(this != &aOther);
647 mHead = std::move(aOther.mHead);
648 return *this;
651 /// Begins a new pass, seeking to the first row of the surface.
652 void ResetToFirstRow() {
653 MOZ_ASSERT(mHead, "Use before configured!");
654 mHead->ResetToFirstRow();
658 * Write pixels to the surface one at a time by repeatedly calling a lambda
659 * that yields pixels. WritePixels() is completely memory safe.
661 * @see SurfaceFilter::WritePixels() for the canonical documentation.
663 template <typename PixelType, typename Func>
664 WriteState WritePixels(Func aFunc) {
665 MOZ_ASSERT(mHead, "Use before configured!");
666 return mHead->WritePixels<PixelType>(std::forward<Func>(aFunc));
670 * A variant of WritePixels() that writes up to a single row of pixels to the
671 * surface in blocks by repeatedly calling a lambda that yields up to the
672 * requested number of pixels.
674 * @see SurfaceFilter::WritePixelBlocks() for the canonical documentation.
676 template <typename PixelType, typename Func>
677 WriteState WritePixelBlocks(Func aFunc) {
678 MOZ_ASSERT(mHead, "Use before configured!");
679 return mHead->WritePixelBlocks<PixelType>(std::forward<Func>(aFunc));
683 * A variant of WritePixels() that writes a single row of pixels to the
684 * surface one at a time by repeatedly calling a lambda that yields pixels.
685 * WritePixelsToRow() is completely memory safe.
687 * @see SurfaceFilter::WritePixelsToRow() for the canonical documentation.
689 template <typename PixelType, typename Func>
690 WriteState WritePixelsToRow(Func aFunc) {
691 MOZ_ASSERT(mHead, "Use before configured!");
692 return mHead->WritePixelsToRow<PixelType>(std::forward<Func>(aFunc));
696 * Write a row to the surface by copying from a buffer. This is bounds checked
697 * and memory safe with respect to the surface, but care must still be taken
698 * by the caller not to overread the source buffer. This variant of
699 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
700 * pixels.
702 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
704 template <typename PixelType>
705 WriteState WriteBuffer(const PixelType* aSource) {
706 MOZ_ASSERT(mHead, "Use before configured!");
707 return mHead->WriteBuffer<PixelType>(aSource);
711 * Write a row to the surface by copying from a buffer. This is bounds checked
712 * and memory safe with respect to the surface, but care must still be taken
713 * by the caller not to overread the source buffer. This variant of
714 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
715 * to the row starting at @aStartColumn. Any pixels in columns before
716 * @aStartColumn or after the pixels copied from the buffer are cleared.
718 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
720 template <typename PixelType>
721 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
722 const size_t aLength) {
723 MOZ_ASSERT(mHead, "Use before configured!");
724 return mHead->WriteBuffer<PixelType>(aSource, aStartColumn, aLength);
728 * Write an empty row to the surface. If some pixels have already been written
729 * to this row, they'll be discarded.
731 * @see SurfaceFilter::WriteEmptyRow() for the canonical documentation.
733 WriteState WriteEmptyRow() {
734 MOZ_ASSERT(mHead, "Use before configured!");
735 return mHead->WriteEmptyRow();
738 /// @return true if we've finished writing to the surface.
739 bool IsSurfaceFinished() const { return mHead->IsSurfaceFinished(); }
741 /// @see SurfaceFilter::TakeInvalidRect() for the canonical documentation.
742 Maybe<SurfaceInvalidRect> TakeInvalidRect() const {
743 MOZ_ASSERT(mHead, "Use before configured!");
744 return mHead->TakeInvalidRect();
747 private:
748 friend class SurfacePipeFactory;
749 friend class TestSurfacePipeFactory;
751 explicit SurfacePipe(UniquePtr<SurfaceFilter>&& aHead)
752 : mHead(std::move(aHead)) {}
754 SurfacePipe(const SurfacePipe&) = delete;
755 SurfacePipe& operator=(const SurfacePipe&) = delete;
757 UniquePtr<SurfaceFilter> mHead; /// The first filter in the chain.
761 * AbstractSurfaceSink contains shared implementation for both SurfaceSink and
762 * ReorientSurfaceSink.
764 class AbstractSurfaceSink : public SurfaceFilter {
765 public:
766 AbstractSurfaceSink()
767 : mImageData(nullptr),
768 mImageDataLength(0),
769 mRow(0),
770 mFlipVertically(false) {}
772 Maybe<SurfaceInvalidRect> TakeInvalidRect() final;
774 protected:
775 uint8_t* DoResetToFirstRow() final;
776 virtual uint8_t* GetRowPointer() const = 0;
778 OrientedIntRect
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 most 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* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
811 uint8_t* DoAdvanceRow() final;
812 uint8_t* GetRowPointer() const final;
815 class ReorientSurfaceSink;
817 /// A configuration struct for ReorientSurfaceSink.
818 struct ReorientSurfaceConfig {
819 using Filter = ReorientSurfaceSink;
820 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
821 OrientedIntSize mOutputSize; /// The size of the surface.
822 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
823 Orientation mOrientation; /// The desired orientation of the surface data.
827 * A sink for surfaces. It handles the allocation of the surface and protects
828 * against buffer overflow. This sink should be used for images which have a
829 * non-identity orientation which we want to apply during decoding.
831 * Sinks must always be at the end of the SurfaceFilter chain.
833 class ReorientSurfaceSink final : public AbstractSurfaceSink {
834 public:
835 nsresult Configure(const ReorientSurfaceConfig& aConfig);
837 protected:
838 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
839 uint8_t* DoAdvanceRow() final;
840 uint8_t* GetRowPointer() const final;
842 UniquePtr<uint8_t[]> mBuffer;
843 gfx::ReorientRowFn mReorientFn;
844 gfx::IntSize mSurfaceSize;
847 } // namespace image
848 } // namespace mozilla
850 #endif // mozilla_image_SurfacePipe_h