summaryrefslogtreecommitdiffstats
path: root/image/SurfacePipeFactory.h
blob: 004ce349a68c049976f5c4797a59d33a57782398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_image_SurfacePipeFactory_h
#define mozilla_image_SurfacePipeFactory_h

#include "SurfacePipe.h"
#include "SurfaceFilters.h"

namespace mozilla {
namespace image {

namespace detail {

/**
 * FilterPipeline is a helper template for SurfacePipeFactory that determines
 * the full type of the sequence of SurfaceFilters that a sequence of
 * configuration structs corresponds to. To make this work, all configuration
 * structs must include a typedef 'Filter' that identifies the SurfaceFilter
 * they configure.
 */
template <typename... Configs>
struct FilterPipeline;

template <typename Config, typename... Configs>
struct FilterPipeline<Config, Configs...> {
  typedef typename Config::template Filter<
      typename FilterPipeline<Configs...>::Type>
      Type;
};

template <typename Config>
struct FilterPipeline<Config> {
  typedef typename Config::Filter Type;
};

}  // namespace detail

/**
 * Flags for SurfacePipeFactory, used in conjunction with the factory functions
 * in SurfacePipeFactory to enable or disable various SurfacePipe
 * functionality.
 */
enum class SurfacePipeFlags {
  DEINTERLACE = 1 << 0,  // If set, deinterlace the image.

  ADAM7_INTERPOLATE =
      1 << 1,  // If set, the caller is deinterlacing the
               // image using ADAM7, and we may want to
               // interpolate it for better intermediate results.

  FLIP_VERTICALLY = 1 << 2,  // If set, flip the image vertically.

  PROGRESSIVE_DISPLAY = 1 << 3,  // If set, we expect the image to be displayed
                                 // progressively. This enables features that
                                 // result in a better user experience for
                                 // progressive display but which may be more
                                 // computationally expensive.

  PREMULTIPLY_ALPHA = 1 << 4,  // If set, we want to premultiply the alpha
                               // channel and the individual color channels.
};
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(SurfacePipeFlags)

class SurfacePipeFactory {
 public:
  /**
   * Creates and initializes a normal (i.e., non-paletted) SurfacePipe.
   *
   * @param aDecoder The decoder whose current frame the SurfacePipe will write
   *                 to.
   * @param aInputSize The original size of the image.
   * @param aOutputSize The size the SurfacePipe should output. Must be the same
   *                    as @aInputSize or smaller. If smaller, the image will be
   *                    downscaled during decoding.
   * @param aFrameRect The portion of the image that actually contains data.
   * @param aFormat The surface format of the image; generally B8G8R8A8 or
   *                B8G8R8X8.
   * @param aAnimParams Extra parameters used by animated images.
   * @param aFlags Flags enabling or disabling various functionality for the
   *               SurfacePipe; see the SurfacePipeFlags documentation for more
   *               information.
   *
   * @return A SurfacePipe if the parameters allowed one to be created
   *         successfully, or Nothing() if the SurfacePipe could not be
   *         initialized.
   */
  static Maybe<SurfacePipe> CreateSurfacePipe(
      Decoder* aDecoder, const OrientedIntSize& aInputSize,
      const OrientedIntSize& aOutputSize, const OrientedIntRect& aFrameRect,
      gfx::SurfaceFormat aInFormat, gfx::SurfaceFormat aOutFormat,
      const Maybe<AnimationParams>& aAnimParams, qcms_transform* aTransform,
      SurfacePipeFlags aFlags) {
    const bool deinterlace = bool(aFlags & SurfacePipeFlags::DEINTERLACE);
    const bool flipVertically =
        bool(aFlags & SurfacePipeFlags::FLIP_VERTICALLY);
    const bool progressiveDisplay =
        bool(aFlags & SurfacePipeFlags::PROGRESSIVE_DISPLAY);
    const bool downscale = aInputSize != aOutputSize;
    const bool removeFrameRect = !aFrameRect.IsEqualEdges(
        OrientedIntRect(OrientedIntPoint(0, 0), aInputSize));
    const bool blendAnimation = aAnimParams.isSome();
    const bool colorManagement = aTransform != nullptr;
    const bool premultiplyAlpha =
        bool(aFlags & SurfacePipeFlags::PREMULTIPLY_ALPHA);

    MOZ_ASSERT(aInFormat == gfx::SurfaceFormat::R8G8B8 ||
               aInFormat == gfx::SurfaceFormat::R8G8B8A8 ||
               aInFormat == gfx::SurfaceFormat::R8G8B8X8 ||
               aInFormat == gfx::SurfaceFormat::OS_RGBA ||
               aInFormat == gfx::SurfaceFormat::OS_RGBX);

    MOZ_ASSERT(aOutFormat == gfx::SurfaceFormat::OS_RGBA ||
               aOutFormat == gfx::SurfaceFormat::OS_RGBX);

    MOZ_ASSERT(aDecoder->GetOrientation().IsIdentity());

    const bool inFormatRgb = aInFormat == gfx::SurfaceFormat::R8G8B8;

    const bool inFormatOpaque = aInFormat == gfx::SurfaceFormat::OS_RGBX ||
                                aInFormat == gfx::SurfaceFormat::R8G8B8X8 ||
                                inFormatRgb;
    const bool outFormatOpaque = aOutFormat == gfx::SurfaceFormat::OS_RGBX;

    const bool inFormatOrder = aInFormat == gfx::SurfaceFormat::R8G8B8A8 ||
                               aInFormat == gfx::SurfaceFormat::R8G8B8X8;
    const bool outFormatOrder = aOutFormat == gfx::SurfaceFormat::R8G8B8A8 ||
                                aOutFormat == gfx::SurfaceFormat::R8G8B8X8;

    // Early swizzles are for unpacking RGB or forcing RGBA/BGRA_U32 to
    // RGBX/BGRX_U32. We should never want to premultiply in either case,
    // because the image's alpha channel will always be opaque. This must be
    // done before downscaling and color management.
    bool unpackOrMaskSwizzle =
        inFormatRgb ||
        (!inFormatOpaque && outFormatOpaque && inFormatOrder == outFormatOrder);

    // Late swizzles are for premultiplying RGBA/BGRA_U32 and/or possible
    // converting between RGBA and BGRA_U32. It must happen after color
    // management, and before downscaling.
    bool swapOrAlphaSwizzle =
        (!inFormatRgb && inFormatOrder != outFormatOrder) || premultiplyAlpha;

    if (unpackOrMaskSwizzle && swapOrAlphaSwizzle) {
      MOZ_ASSERT_UNREACHABLE("Early and late swizzles not supported");
      return Nothing();
    }

    if (!unpackOrMaskSwizzle && !swapOrAlphaSwizzle &&
        aInFormat != aOutFormat) {
      MOZ_ASSERT_UNREACHABLE("Need to swizzle, but not configured to");
      return Nothing();
    }

    // Don't interpolate if we're sure we won't show this surface to the user
    // until it's completely decoded. The final pass of an ADAM7 image doesn't
    // need interpolation, so we only need to interpolate if we'll be displaying
    // the image while it's still being decoded.
    const bool adam7Interpolate =
        bool(aFlags & SurfacePipeFlags::ADAM7_INTERPOLATE) &&
        progressiveDisplay;

    if (deinterlace && adam7Interpolate) {
      MOZ_ASSERT_UNREACHABLE("ADAM7 deinterlacing is handled by libpng");
      return Nothing();
    }

    // Construct configurations for the SurfaceFilters. Note that the order of
    // these filters is significant. We want to deinterlace or interpolate raw
    // input rows, before any other transformations, and we want to remove the
    // frame rect (which may involve adding blank rows or columns to the image)
    // before any downscaling, so that the new rows and columns are taken into
    // account.
    DeinterlacingConfig<uint32_t> deinterlacingConfig{progressiveDisplay};
    ADAM7InterpolatingConfig interpolatingConfig;
    RemoveFrameRectConfig removeFrameRectConfig{aFrameRect.ToUnknownRect()};
    BlendAnimationConfig blendAnimationConfig{aDecoder};
    DownscalingConfig downscalingConfig{aInputSize.ToUnknownSize(), aOutFormat};
    ColorManagementConfig colorManagementConfig{aTransform};
    SwizzleConfig swizzleConfig{aInFormat, aOutFormat, premultiplyAlpha};
    SurfaceConfig surfaceConfig{aDecoder, aOutputSize.ToUnknownSize(),
                                aOutFormat, flipVertically, aAnimParams};

    Maybe<SurfacePipe> pipe;

    if (unpackOrMaskSwizzle) {
      if (colorManagement) {
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, removeFrameRectConfig,
                              downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              colorManagementConfig, blendAnimationConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              colorManagementConfig, blendAnimationConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, colorManagementConfig,
                              blendAnimationConfig, surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              colorManagementConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              colorManagementConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, colorManagementConfig,
                              removeFrameRectConfig, surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              colorManagementConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              colorManagementConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(swizzleConfig, colorManagementConfig, surfaceConfig);
            }
          }
        }
      } else {  // (colorManagement is false)
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              downscalingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              downscalingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, downscalingConfig, surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              blendAnimationConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              blendAnimationConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(swizzleConfig, blendAnimationConfig, surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(swizzleConfig, removeFrameRectConfig, surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe =
                  MakePipe(swizzleConfig, deinterlacingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe =
                  MakePipe(swizzleConfig, interpolatingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, surfaceConfig);
            }
          }
        }
      }
    } else if (swapOrAlphaSwizzle) {
      if (colorManagement) {
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              deinterlacingConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              interpolatingConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              deinterlacingConfig, downscalingConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              interpolatingConfig, downscalingConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              downscalingConfig, surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              deinterlacingConfig, blendAnimationConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              interpolatingConfig, blendAnimationConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              blendAnimationConfig, surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              deinterlacingConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              interpolatingConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              removeFrameRectConfig, surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              deinterlacingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(colorManagementConfig, swizzleConfig,
                              interpolatingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(colorManagementConfig, swizzleConfig, surfaceConfig);
            }
          }
        }
      } else {  // (colorManagement is false)
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              downscalingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              downscalingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, downscalingConfig, surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              blendAnimationConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              blendAnimationConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(swizzleConfig, blendAnimationConfig, surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(swizzleConfig, deinterlacingConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(swizzleConfig, interpolatingConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe =
                  MakePipe(swizzleConfig, removeFrameRectConfig, surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe =
                  MakePipe(swizzleConfig, deinterlacingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe =
                  MakePipe(swizzleConfig, interpolatingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(swizzleConfig, surfaceConfig);
            }
          }
        }
      }
    } else {  // (unpackOrMaskSwizzle and swapOrAlphaSwizzle are false)
      if (colorManagement) {
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, removeFrameRectConfig,
                              downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, removeFrameRectConfig,
                              downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(removeFrameRectConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, downscalingConfig,
                              colorManagementConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(downscalingConfig, colorManagementConfig,
                              surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, colorManagementConfig,
                              blendAnimationConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, colorManagementConfig,
                              blendAnimationConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, blendAnimationConfig,
                              surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, colorManagementConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, colorManagementConfig,
                              removeFrameRectConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, removeFrameRectConfig,
                              surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, colorManagementConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, colorManagementConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(colorManagementConfig, surfaceConfig);
            }
          }
        }
      } else {  // (colorManagement is false)
        if (downscale) {
          MOZ_ASSERT(!blendAnimation);
          if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, removeFrameRectConfig,
                              downscalingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(removeFrameRectConfig, downscalingConfig,
                              surfaceConfig);
            }
          } else {  // (removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, downscalingConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, downscalingConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(downscalingConfig, surfaceConfig);
            }
          }
        } else {  // (downscale is false)
          if (blendAnimation) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, blendAnimationConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, blendAnimationConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(blendAnimationConfig, surfaceConfig);
            }
          } else if (removeFrameRect) {
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, removeFrameRectConfig,
                              surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(removeFrameRectConfig, surfaceConfig);
            }
          } else {  // (blendAnimation and removeFrameRect is false)
            if (deinterlace) {
              pipe = MakePipe(deinterlacingConfig, surfaceConfig);
            } else if (adam7Interpolate) {
              pipe = MakePipe(interpolatingConfig, surfaceConfig);
            } else {  // (deinterlace and adam7Interpolate are false)
              pipe = MakePipe(surfaceConfig);
            }
          }
        }
      }
    }

    return pipe;
  }

  /**
   * Creates and initializes a reorienting SurfacePipe.
   *
   * @param aDecoder The decoder whose current frame the SurfacePipe will write
   *                 to.
   * @param aInputSize The original size of the image.
   * @param aOutputSize The size the SurfacePipe should output. Must be the same
   *                    as @aInputSize or smaller. If smaller, the image will be
   *                    downscaled during decoding.
   * @param aFormat The surface format of the image; generally B8G8R8A8 or
   *                B8G8R8X8.
   * @param aOrientation The orientation of the image.
   *
   * @return A SurfacePipe if the parameters allowed one to be created
   *         successfully, or Nothing() if the SurfacePipe could not be
   *         initialized.
   */
  static Maybe<SurfacePipe> CreateReorientSurfacePipe(
      Decoder* aDecoder, const OrientedIntSize& aInputSize,
      const OrientedIntSize& aOutputSize, gfx::SurfaceFormat aFormat,
      qcms_transform* aTransform, const Orientation& aOrientation) {
    const bool downscale = aInputSize != aOutputSize;
    const bool colorManagement = aTransform != nullptr;

    // Construct configurations for the SurfaceFilters. Note that the order of
    // these filters is significant. We want to deinterlace or interpolate raw
    // input rows, before any other transformations, and we want to remove the
    // frame rect (which may involve adding blank rows or columns to the image)
    // before any downscaling, so that the new rows and columns are taken into
    // account.
    DownscalingConfig downscalingConfig{
        aOrientation.ToUnoriented(aInputSize).ToUnknownSize(), aFormat};
    ColorManagementConfig colorManagementConfig{aTransform};
    SurfaceConfig surfaceConfig{aDecoder, aOutputSize.ToUnknownSize(), aFormat,
                                /* mFlipVertically */ false,
                                /* mAnimParams */ Nothing()};
    ReorientSurfaceConfig reorientSurfaceConfig{aDecoder, aOutputSize, aFormat,
                                                aOrientation};

    Maybe<SurfacePipe> pipe;

    if (aOrientation.IsIdentity()) {
      if (colorManagement) {
        if (downscale) {
          pipe =
              MakePipe(downscalingConfig, colorManagementConfig, surfaceConfig);
        } else {  // (downscale is false)
          pipe = MakePipe(colorManagementConfig, surfaceConfig);
        }
      } else {  // (colorManagement is false)
        if (downscale) {
          pipe = MakePipe(downscalingConfig, surfaceConfig);
        } else {  // (downscale is false)
          pipe = MakePipe(surfaceConfig);
        }
      }
    } else {  // (orientation is not identity)
      if (colorManagement) {
        if (downscale) {
          pipe = MakePipe(downscalingConfig, colorManagementConfig,
                          reorientSurfaceConfig);
        } else {  // (downscale is false)
          pipe = MakePipe(colorManagementConfig, reorientSurfaceConfig);
        }
      } else {  // (colorManagement is false)
        if (downscale) {
          pipe = MakePipe(downscalingConfig, reorientSurfaceConfig);
        } else {  // (downscale is false)
          pipe = MakePipe(reorientSurfaceConfig);
        }
      }
    }

    return pipe;
  }

 private:
  template <typename... Configs>
  static Maybe<SurfacePipe> MakePipe(const Configs&... aConfigs) {
    auto pipe = MakeUnique<typename detail::FilterPipeline<Configs...>::Type>();
    nsresult rv = pipe->Configure(aConfigs...);
    if (NS_FAILED(rv)) {
      return Nothing();
    }

    return Some(SurfacePipe{std::move(pipe)});
  }

  virtual ~SurfacePipeFactory() = 0;
};

}  // namespace image
}  // namespace mozilla

#endif  // mozilla_image_SurfacePipeFactory_h