Bug 1688649 [wpt PR 27310] - Don't preload firefox in some cases, a=testonly
[gecko.git] / image / SurfacePipe.h
blobd216087f36b65fe2ad11f208e67235db68361bb5
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 "nsDebug.h"
39 namespace mozilla {
40 namespace image {
42 class Decoder;
44 /**
45 * An invalid rect for a surface. Results are given both in the space of the
46 * input image (i.e., before any SurfaceFilters are applied) and in the space
47 * of the output surface (after all SurfaceFilters).
49 struct SurfaceInvalidRect {
50 gfx::IntRect mInputSpaceRect; /// The invalid rect in pre-SurfacePipe space.
51 gfx::IntRect
52 mOutputSpaceRect; /// The invalid rect in post-SurfacePipe space.
55 /**
56 * An enum used to allow the lambdas passed to WritePixels() to communicate
57 * their state to the caller.
59 enum class WriteState : uint8_t {
60 NEED_MORE_DATA, /// The lambda ran out of data.
62 FINISHED, /// The lambda is done writing to the surface; future writes
63 /// will fail.
65 FAILURE /// The lambda encountered an error. The caller may recover
66 /// if possible and continue to write. (This never indicates
67 /// an error in the SurfacePipe machinery itself; it's only
68 /// generated by the lambdas.)
71 /**
72 * A template alias used to make the return value of WritePixels() lambdas
73 * (which may return either a pixel value or a WriteState) easier to specify.
75 template <typename PixelType>
76 using NextPixel = Variant<PixelType, WriteState>;
78 /**
79 * SurfaceFilter is the abstract superclass of SurfacePipe pipeline stages. It
80 * implements the the code that actually writes to the surface - WritePixels()
81 * and the other Write*() methods - which are non-virtual for efficiency.
83 * SurfaceFilter's API is nonpublic; only SurfacePipe and other SurfaceFilters
84 * should use it. Non-SurfacePipe code should use the methods on SurfacePipe.
86 * To implement a SurfaceFilter, it's necessary to subclass SurfaceFilter and
87 * implement, at a minimum, the pure virtual methods. It's also necessary to
88 * define a Config struct with a Filter typedef member that identifies the
89 * matching SurfaceFilter class, and a Configure() template method. See an
90 * existing SurfaceFilter subclass, such as RemoveFrameRectFilter, for an
91 * example of how the Configure() method must be implemented. It takes a list of
92 * Config structs, passes the tail of the list to the next filter in the chain's
93 * Configure() method, and then uses the head of the list to configure itself. A
94 * SurfaceFilter's Configure() method must also call
95 * SurfaceFilter::ConfigureFilter() to provide the Write*() methods with the
96 * information they need to do their jobs.
98 class SurfaceFilter {
99 public:
100 SurfaceFilter() : mRowPointer(nullptr), mCol(0), mPixelSize(0) {}
102 virtual ~SurfaceFilter() {}
105 * Reset this surface to the first row. It's legal for this filter to throw
106 * away any previously written data at this point, as all rows must be written
107 * to on every pass.
109 * @return a pointer to the buffer for the first row.
111 uint8_t* ResetToFirstRow() {
112 mCol = 0;
113 mRowPointer = DoResetToFirstRow();
114 return mRowPointer;
118 * Called by WritePixels() to advance this filter to the next row.
120 * @return a pointer to the buffer for the next row, or nullptr to indicate
121 * that we've finished the entire surface.
123 uint8_t* AdvanceRow() {
124 mCol = 0;
125 mRowPointer = DoAdvanceRow();
126 return mRowPointer;
130 * Called by WriteBuffer() to advance this filter to the next row, if the
131 * supplied row is a full row.
133 * @return a pointer to the buffer for the next row, or nullptr to indicate
134 * that we've finished the entire surface.
136 uint8_t* AdvanceRow(const uint8_t* aInputRow) {
137 mCol = 0;
138 mRowPointer = DoAdvanceRowFromBuffer(aInputRow);
139 return mRowPointer;
142 /// @return a pointer to the buffer for the current row.
143 uint8_t* CurrentRowPointer() const { return mRowPointer; }
145 /// @return true if we've finished writing to the surface.
146 bool IsSurfaceFinished() const { return mRowPointer == nullptr; }
148 /// @return the input size this filter expects.
149 gfx::IntSize InputSize() const { return mInputSize; }
152 * Write pixels to the surface one at a time by repeatedly calling a lambda
153 * that yields pixels. WritePixels() is completely memory safe.
155 * Writing continues until every pixel in the surface has been written to
156 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
157 * which WritePixels() will return to the caller.
159 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
160 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
161 * size passed to ConfigureFilter().
163 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
164 * which means we can remove the PixelType template parameter from this
165 * method.
167 * @param aFunc A lambda that functions as a generator, yielding the next
168 * pixel in the surface each time it's called. The lambda must
169 * return a NextPixel<PixelType> value.
171 * @return A WriteState value indicating the lambda generator's state.
172 * WritePixels() itself will return WriteState::FINISHED if writing
173 * has finished, regardless of the lambda's internal state.
175 template <typename PixelType, typename Func>
176 WriteState WritePixels(Func aFunc) {
177 Maybe<WriteState> result;
178 while (
179 !(result = DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc)))) {
182 return *result;
186 * Write pixels to the surface by calling a lambda which may write as many
187 * pixels as there is remaining to complete the row. It is not completely
188 * memory safe as it trusts the underlying decoder not to overrun the given
189 * buffer, however it is an acceptable tradeoff for performance.
191 * Writing continues until every pixel in the surface has been written to
192 * (i.e., IsSurfaceFinished() returns true) or the lambda returns a WriteState
193 * which WritePixelBlocks() will return to the caller.
195 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
196 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
197 * size passed to ConfigureFilter().
199 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
200 * which means we can remove the PixelType template parameter from this
201 * method.
203 * @param aFunc A lambda that functions as a generator, yielding at most the
204 * maximum number of pixels requested. The lambda must accept a
205 * pointer argument to the first pixel to write, a maximum
206 * number of pixels to write as part of the block, and return a
207 * NextPixel<PixelType> value.
209 * @return A WriteState value indicating the lambda generator's state.
210 * WritePixelBlocks() itself will return WriteState::FINISHED if
211 * writing has finished, regardless of the lambda's internal state.
213 template <typename PixelType, typename Func>
214 WriteState WritePixelBlocks(Func aFunc) {
215 Maybe<WriteState> result;
216 while (!(result = DoWritePixelBlockToRow<PixelType>(
217 std::forward<Func>(aFunc)))) {
220 return *result;
224 * A variant of WritePixels() that writes a single row of pixels to the
225 * surface one at a time by repeatedly calling a lambda that yields pixels.
226 * WritePixelsToRow() is completely memory safe.
228 * Writing continues until every pixel in the row has been written to. If the
229 * surface is complete at that pointer, WriteState::FINISHED is returned;
230 * otherwise, WritePixelsToRow() returns WriteState::NEED_MORE_DATA. The
231 * lambda can terminate writing early by returning a WriteState itself, which
232 * WritePixelsToRow() will return to the caller.
234 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
235 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
236 * size passed to ConfigureFilter().
238 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
239 * which means we can remove the PixelType template parameter from this
240 * method.
242 * @param aFunc A lambda that functions as a generator, yielding the next
243 * pixel in the surface each time it's called. The lambda must
244 * return a NextPixel<PixelType> value.
246 * @return A WriteState value indicating the lambda generator's state.
247 * WritePixels() itself will return WriteState::FINISHED if writing
248 * the entire surface has finished, or WriteState::NEED_MORE_DATA if
249 * writing the row has finished, regardless of the lambda's internal
250 * state.
252 template <typename PixelType, typename Func>
253 WriteState WritePixelsToRow(Func aFunc) {
254 return DoWritePixelsToRow<PixelType>(std::forward<Func>(aFunc))
255 .valueOr(WriteState::NEED_MORE_DATA);
259 * Write a row to the surface by copying from a buffer. This is bounds checked
260 * and memory safe with respect to the surface, but care must still be taken
261 * by the caller not to overread the source buffer. This variant of
262 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
263 * pixels.
265 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
266 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
267 * size passed to ConfigureFilter().
269 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
270 * which means we can remove the PixelType template parameter from this
271 * method.
273 * @param aSource A buffer to copy from. This buffer must be
274 * |mInputSize.width| pixels wide, which means
275 * |mInputSize.width * sizeof(PixelType)| bytes. May not be
276 * null.
278 * @return WriteState::FINISHED if the entire surface has been written to.
279 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
280 * value is passed, returns WriteState::FAILURE.
282 template <typename PixelType>
283 WriteState WriteBuffer(const PixelType* aSource) {
284 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
285 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
286 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
288 if (IsSurfaceFinished()) {
289 return WriteState::FINISHED; // Already done.
292 if (MOZ_UNLIKELY(!aSource)) {
293 NS_WARNING("Passed a null pointer to WriteBuffer");
294 return WriteState::FAILURE;
297 AdvanceRow(reinterpret_cast<const uint8_t*>(aSource));
298 return IsSurfaceFinished() ? WriteState::FINISHED
299 : WriteState::NEED_MORE_DATA;
303 * Write a row to the surface by copying from a buffer. This is bounds checked
304 * and memory safe with respect to the surface, but care must still be taken
305 * by the caller not to overread the source buffer. This variant of
306 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
307 * to the row starting at @aStartColumn. Any pixels in columns before
308 * @aStartColumn or after the pixels copied from the buffer are cleared.
310 * Bounds checking failures produce warnings in debug builds because although
311 * the bounds checking maintains safety, this kind of failure could indicate a
312 * bug in the calling code.
314 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
315 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
316 * size passed to ConfigureFilter().
318 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
319 * which means we can remove the PixelType template parameter from this
320 * method.
322 * @param aSource A buffer to copy from. This buffer must be @aLength pixels
323 * wide, which means |aLength * sizeof(PixelType)| bytes. May
324 * not be null.
325 * @param aStartColumn The column to start writing to in the row. Columns
326 * before this are cleared.
327 * @param aLength The number of bytes, at most, which may be copied from
328 * @aSource. Fewer bytes may be copied in practice due to
329 * bounds checking.
331 * @return WriteState::FINISHED if the entire surface has been written to.
332 * Otherwise, returns WriteState::NEED_MORE_DATA. If a null |aSource|
333 * value is passed, returns WriteState::FAILURE.
335 template <typename PixelType>
336 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
337 const size_t aLength) {
338 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
339 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
340 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
342 if (IsSurfaceFinished()) {
343 return WriteState::FINISHED; // Already done.
346 if (MOZ_UNLIKELY(!aSource)) {
347 NS_WARNING("Passed a null pointer to WriteBuffer");
348 return WriteState::FAILURE;
351 PixelType* dest = reinterpret_cast<PixelType*>(mRowPointer);
353 // Clear the area before |aStartColumn|.
354 const size_t prefixLength =
355 std::min<size_t>(mInputSize.width, aStartColumn);
356 if (MOZ_UNLIKELY(prefixLength != aStartColumn)) {
357 NS_WARNING("Provided starting column is out-of-bounds in WriteBuffer");
360 memset(dest, 0, mInputSize.width * sizeof(PixelType));
361 dest += prefixLength;
363 // Write |aLength| pixels from |aSource| into the row, with bounds checking.
364 const size_t bufferLength =
365 std::min<size_t>(mInputSize.width - prefixLength, aLength);
366 if (MOZ_UNLIKELY(bufferLength != aLength)) {
367 NS_WARNING("Provided buffer length is out-of-bounds in WriteBuffer");
370 memcpy(dest, aSource, bufferLength * sizeof(PixelType));
371 dest += bufferLength;
373 // Clear the rest of the row.
374 const size_t suffixLength =
375 mInputSize.width - (prefixLength + bufferLength);
376 memset(dest, 0, suffixLength * sizeof(PixelType));
378 AdvanceRow();
380 return IsSurfaceFinished() ? WriteState::FINISHED
381 : WriteState::NEED_MORE_DATA;
385 * Write an empty row to the surface. If some pixels have already been written
386 * to this row, they'll be discarded.
388 * @return WriteState::FINISHED if the entire surface has been written to.
389 * Otherwise, returns WriteState::NEED_MORE_DATA.
391 WriteState WriteEmptyRow() {
392 if (IsSurfaceFinished()) {
393 return WriteState::FINISHED; // Already done.
396 memset(mRowPointer, 0, mInputSize.width * mPixelSize);
397 AdvanceRow();
399 return IsSurfaceFinished() ? WriteState::FINISHED
400 : WriteState::NEED_MORE_DATA;
404 * Write a row to the surface by calling a lambda that uses a pointer to
405 * directly write to the row. This is unsafe because SurfaceFilter can't
406 * provide any bounds checking; that's up to the lambda itself. For this
407 * reason, the other Write*() methods should be preferred whenever it's
408 * possible to use them; WriteUnsafeComputedRow() should be used only when
409 * it's absolutely necessary to avoid extra copies or other performance
410 * penalties.
412 * This method should never be exposed to SurfacePipe consumers; it's strictly
413 * for use in SurfaceFilters. If external code needs this method, it should
414 * probably be turned into a SurfaceFilter.
416 * The template parameter PixelType must be uint8_t (for paletted surfaces) or
417 * uint32_t (for BGRA/BGRX surfaces) and must be in agreement with the pixel
418 * size passed to ConfigureFilter().
420 * XXX(seth): We'll remove all support for paletted surfaces in bug 1247520,
421 * which means we can remove the PixelType template parameter from this
422 * method.
424 * @param aFunc A lambda that writes directly to the row.
426 * @return WriteState::FINISHED if the entire surface has been written to.
427 * Otherwise, returns WriteState::NEED_MORE_DATA.
429 template <typename PixelType, typename Func>
430 WriteState WriteUnsafeComputedRow(Func aFunc) {
431 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
432 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
433 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
435 if (IsSurfaceFinished()) {
436 return WriteState::FINISHED; // Already done.
439 // Call the provided lambda with a pointer to the buffer for the current
440 // row. This is unsafe because we can't do any bounds checking; the lambda
441 // itself has to be responsible for that.
442 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
443 aFunc(rowPtr, mInputSize.width);
444 AdvanceRow();
446 return IsSurfaceFinished() ? WriteState::FINISHED
447 : WriteState::NEED_MORE_DATA;
450 //////////////////////////////////////////////////////////////////////////////
451 // Methods Subclasses Should Override
452 //////////////////////////////////////////////////////////////////////////////
455 * @return a SurfaceInvalidRect representing the region of the surface that
456 * has been written to since the last time TakeInvalidRect() was
457 * called, or Nothing() if the region is empty (i.e. nothing has been
458 * written).
460 virtual Maybe<SurfaceInvalidRect> TakeInvalidRect() = 0;
462 protected:
464 * Called by ResetToFirstRow() to actually perform the reset. It's legal to
465 * throw away any previously written data at this point, as all rows must be
466 * written to on every pass.
468 virtual uint8_t* DoResetToFirstRow() = 0;
471 * Called by AdvanceRow() to actually advance this filter to the next row.
473 * @param aInputRow The input row supplied by the decoder.
475 * @return a pointer to the buffer for the next row, or nullptr to indicate
476 * that we've finished the entire surface.
478 virtual uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) = 0;
481 * Called by AdvanceRow() to actually advance this filter to the next row.
483 * @return a pointer to the buffer for the next row, or nullptr to indicate
484 * that we've finished the entire surface.
486 virtual uint8_t* DoAdvanceRow() = 0;
488 //////////////////////////////////////////////////////////////////////////////
489 // Methods For Internal Use By Subclasses
490 //////////////////////////////////////////////////////////////////////////////
493 * Called by subclasses' Configure() methods to initialize the configuration
494 * of this filter. After the filter is configured, calls ResetToFirstRow().
496 * @param aInputSize The input size of this filter, in pixels. The previous
497 * filter in the chain will expect to write into rows
498 * |aInputSize.width| pixels wide.
499 * @param aPixelSize How large, in bytes, each pixel in the surface is. This
500 * should be either 1 for paletted images or 4 for BGRA/BGRX
501 * images.
503 void ConfigureFilter(gfx::IntSize aInputSize, uint8_t aPixelSize) {
504 mInputSize = aInputSize;
505 mPixelSize = aPixelSize;
507 ResetToFirstRow();
511 * Called by subclasses' DoAdvanceRowFromBuffer() methods to copy a decoder
512 * supplied row buffer into its internal row pointer. Ideally filters at the
513 * top of the filter pipeline are able to consume the decoder row buffer
514 * directly without the extra copy prior to performing its transformation.
516 * @param aInputRow The input row supplied by the decoder.
518 void CopyInputRow(const uint8_t* aInputRow) {
519 MOZ_ASSERT(aInputRow);
520 MOZ_ASSERT(mCol == 0);
521 memcpy(mRowPointer, aInputRow, mPixelSize * mInputSize.width);
524 private:
526 * An internal method used to implement WritePixelBlocks. This method writes
527 * up to the number of pixels necessary to complete the row and returns Some()
528 * if we either finished the entire surface or the lambda returned a
529 * WriteState indicating that we should return to the caller. If the row was
530 * successfully written without either of those things happening, it returns
531 * Nothing(), allowing WritePixelBlocks() to iterate to fill as many rows as
532 * possible.
534 template <typename PixelType, typename Func>
535 Maybe<WriteState> DoWritePixelBlockToRow(Func aFunc) {
536 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
537 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
538 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
540 if (IsSurfaceFinished()) {
541 return Some(WriteState::FINISHED); // We're already done.
544 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
545 int32_t remainder = mInputSize.width - mCol;
546 int32_t written;
547 Maybe<WriteState> result;
548 Tie(written, result) = aFunc(&rowPtr[mCol], remainder);
549 if (written == remainder) {
550 MOZ_ASSERT(result.isNothing());
551 mCol = mInputSize.width;
552 AdvanceRow(); // We've finished the row.
553 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
556 MOZ_ASSERT(written >= 0 && written < remainder);
557 MOZ_ASSERT(result.isSome());
559 mCol += written;
560 if (*result == WriteState::FINISHED) {
561 ZeroOutRestOfSurface<PixelType>();
564 return result;
568 * An internal method used to implement both WritePixels() and
569 * WritePixelsToRow(). Those methods differ only in their behavior after a row
570 * is successfully written - WritePixels() continues to write another row,
571 * while WritePixelsToRow() returns to the caller. This method writes a single
572 * row and returns Some() if we either finished the entire surface or the
573 * lambda returned a WriteState indicating that we should return to the
574 * caller. If the row was successfully written without either of those things
575 * happening, it returns Nothing(), allowing WritePixels() and
576 * WritePixelsToRow() to implement their respective behaviors.
578 template <typename PixelType, typename Func>
579 Maybe<WriteState> DoWritePixelsToRow(Func aFunc) {
580 MOZ_ASSERT(mPixelSize == 1 || mPixelSize == 4);
581 MOZ_ASSERT_IF(mPixelSize == 1, sizeof(PixelType) == sizeof(uint8_t));
582 MOZ_ASSERT_IF(mPixelSize == 4, sizeof(PixelType) == sizeof(uint32_t));
584 if (IsSurfaceFinished()) {
585 return Some(WriteState::FINISHED); // We're already done.
588 PixelType* rowPtr = reinterpret_cast<PixelType*>(mRowPointer);
590 for (; mCol < mInputSize.width; ++mCol) {
591 NextPixel<PixelType> result = aFunc();
592 if (result.template is<PixelType>()) {
593 rowPtr[mCol] = result.template as<PixelType>();
594 continue;
597 switch (result.template as<WriteState>()) {
598 case WriteState::NEED_MORE_DATA:
599 return Some(WriteState::NEED_MORE_DATA);
601 case WriteState::FINISHED:
602 ZeroOutRestOfSurface<PixelType>();
603 return Some(WriteState::FINISHED);
605 case WriteState::FAILURE:
606 // Note that we don't need to record this anywhere, because this
607 // indicates an error in aFunc, and there's nothing wrong with our
608 // machinery. The caller can recover as needed and continue writing to
609 // the row.
610 return Some(WriteState::FAILURE);
614 AdvanceRow(); // We've finished the row.
616 return IsSurfaceFinished() ? Some(WriteState::FINISHED) : Nothing();
619 template <typename PixelType>
620 void ZeroOutRestOfSurface() {
621 WritePixels<PixelType>([] { return AsVariant(PixelType(0)); });
624 gfx::IntSize mInputSize; /// The size of the input this filter expects.
625 uint8_t* mRowPointer; /// Pointer to the current row or null if finished.
626 int32_t mCol; /// The current column we're writing to. (0-indexed)
627 uint8_t mPixelSize; /// How large each pixel in the surface is, in bytes.
631 * SurfacePipe is the public API that decoders should use to interact with a
632 * SurfaceFilter pipeline.
634 class SurfacePipe {
635 public:
636 SurfacePipe() {}
638 SurfacePipe(SurfacePipe&& aOther) : mHead(std::move(aOther.mHead)) {}
640 ~SurfacePipe() {}
642 SurfacePipe& operator=(SurfacePipe&& aOther) {
643 MOZ_ASSERT(this != &aOther);
644 mHead = std::move(aOther.mHead);
645 return *this;
648 /// Begins a new pass, seeking to the first row of the surface.
649 void ResetToFirstRow() {
650 MOZ_ASSERT(mHead, "Use before configured!");
651 mHead->ResetToFirstRow();
655 * Write pixels to the surface one at a time by repeatedly calling a lambda
656 * that yields pixels. WritePixels() is completely memory safe.
658 * @see SurfaceFilter::WritePixels() for the canonical documentation.
660 template <typename PixelType, typename Func>
661 WriteState WritePixels(Func aFunc) {
662 MOZ_ASSERT(mHead, "Use before configured!");
663 return mHead->WritePixels<PixelType>(std::forward<Func>(aFunc));
667 * A variant of WritePixels() that writes up to a single row of pixels to the
668 * surface in blocks by repeatedly calling a lambda that yields up to the
669 * requested number of pixels.
671 * @see SurfaceFilter::WritePixelBlocks() for the canonical documentation.
673 template <typename PixelType, typename Func>
674 WriteState WritePixelBlocks(Func aFunc) {
675 MOZ_ASSERT(mHead, "Use before configured!");
676 return mHead->WritePixelBlocks<PixelType>(std::forward<Func>(aFunc));
680 * A variant of WritePixels() that writes a single row of pixels to the
681 * surface one at a time by repeatedly calling a lambda that yields pixels.
682 * WritePixelsToRow() is completely memory safe.
684 * @see SurfaceFilter::WritePixelsToRow() for the canonical documentation.
686 template <typename PixelType, typename Func>
687 WriteState WritePixelsToRow(Func aFunc) {
688 MOZ_ASSERT(mHead, "Use before configured!");
689 return mHead->WritePixelsToRow<PixelType>(std::forward<Func>(aFunc));
693 * Write a row to the surface by copying from a buffer. This is bounds checked
694 * and memory safe with respect to the surface, but care must still be taken
695 * by the caller not to overread the source buffer. This variant of
696 * WriteBuffer() requires a source buffer which contains |mInputSize.width|
697 * pixels.
699 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
701 template <typename PixelType>
702 WriteState WriteBuffer(const PixelType* aSource) {
703 MOZ_ASSERT(mHead, "Use before configured!");
704 return mHead->WriteBuffer<PixelType>(aSource);
708 * Write a row to the surface by copying from a buffer. This is bounds checked
709 * and memory safe with respect to the surface, but care must still be taken
710 * by the caller not to overread the source buffer. This variant of
711 * WriteBuffer() reads at most @aLength pixels from the buffer and writes them
712 * to the row starting at @aStartColumn. Any pixels in columns before
713 * @aStartColumn or after the pixels copied from the buffer are cleared.
715 * @see SurfaceFilter::WriteBuffer() for the canonical documentation.
717 template <typename PixelType>
718 WriteState WriteBuffer(const PixelType* aSource, const size_t aStartColumn,
719 const size_t aLength) {
720 MOZ_ASSERT(mHead, "Use before configured!");
721 return mHead->WriteBuffer<PixelType>(aSource, aStartColumn, aLength);
725 * Write an empty row to the surface. If some pixels have already been written
726 * to this row, they'll be discarded.
728 * @see SurfaceFilter::WriteEmptyRow() for the canonical documentation.
730 WriteState WriteEmptyRow() {
731 MOZ_ASSERT(mHead, "Use before configured!");
732 return mHead->WriteEmptyRow();
735 /// @return true if we've finished writing to the surface.
736 bool IsSurfaceFinished() const { return mHead->IsSurfaceFinished(); }
738 /// @see SurfaceFilter::TakeInvalidRect() for the canonical documentation.
739 Maybe<SurfaceInvalidRect> TakeInvalidRect() const {
740 MOZ_ASSERT(mHead, "Use before configured!");
741 return mHead->TakeInvalidRect();
744 private:
745 friend class SurfacePipeFactory;
746 friend class TestSurfacePipeFactory;
748 explicit SurfacePipe(UniquePtr<SurfaceFilter>&& aHead)
749 : mHead(std::move(aHead)) {}
751 SurfacePipe(const SurfacePipe&) = delete;
752 SurfacePipe& operator=(const SurfacePipe&) = delete;
754 UniquePtr<SurfaceFilter> mHead; /// The first filter in the chain.
758 * AbstractSurfaceSink contains shared implementation for both SurfaceSink and
759 * PalettedSurfaceSink.
761 class AbstractSurfaceSink : public SurfaceFilter {
762 public:
763 AbstractSurfaceSink()
764 : mImageData(nullptr),
765 mImageDataLength(0),
766 mRow(0),
767 mFlipVertically(false) {}
769 Maybe<SurfaceInvalidRect> TakeInvalidRect() final;
771 protected:
772 uint8_t* DoResetToFirstRow() final;
773 uint8_t* DoAdvanceRowFromBuffer(const uint8_t* aInputRow) final;
774 uint8_t* DoAdvanceRow() final;
775 virtual uint8_t* GetRowPointer() const = 0;
777 gfx::IntRect
778 mInvalidRect; /// The region of the surface that has been written
779 /// to since the last call to TakeInvalidRect().
780 uint8_t* mImageData; /// A pointer to the beginning of the surface data.
781 uint32_t mImageDataLength; /// The length of the surface data.
782 uint32_t mRow; /// The row to which we're writing. (0-indexed)
783 bool mFlipVertically; /// If true, write the rows from top to bottom.
786 class SurfaceSink;
788 /// A configuration struct for SurfaceSink.
789 struct SurfaceConfig {
790 using Filter = SurfaceSink;
791 Decoder* mDecoder; /// Which Decoder to use to allocate the surface.
792 gfx::IntSize mOutputSize; /// The size of the surface.
793 gfx::SurfaceFormat mFormat; /// The surface format (BGRA or BGRX).
794 bool mFlipVertically; /// If true, write the rows from bottom to top.
795 Maybe<AnimationParams> mAnimParams; /// Given for animated images.
799 * A sink for surfaces. It handles the allocation of the surface and protects
800 * against buffer overflow. This sink should be used for images.
802 * Sinks must always be at the end of the SurfaceFilter chain.
804 class SurfaceSink final : public AbstractSurfaceSink {
805 public:
806 nsresult Configure(const SurfaceConfig& aConfig);
808 protected:
809 uint8_t* GetRowPointer() const override;
812 } // namespace image
813 } // namespace mozilla
815 #endif // mozilla_image_SurfacePipe_h