summaryrefslogtreecommitdiffstats
path: root/gfx/2d/HelpersD2D.h
blob: 291dff9f533677ca45bb63230b5867c5278a0a55 (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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/* -*- 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_GFX_HELPERSD2D_H_
#define MOZILLA_GFX_HELPERSD2D_H_

#include <d2d1_1.h>

#include <vector>

#include <dwrite.h>
#include <versionhelpers.h>
#include "2D.h"
#include "Logging.h"
#include "ImageScaling.h"

#include "ScaledFontDWrite.h"

#undef min
#undef max

namespace mozilla {
namespace gfx {

RefPtr<ID2D1Factory1> D2DFactory();

static inline D2D1_POINT_2F D2DPoint(const Point& aPoint) {
  return D2D1::Point2F(aPoint.x, aPoint.y);
}

static inline D2D1_SIZE_U D2DIntSize(const IntSize& aSize) {
  return D2D1::SizeU(aSize.width, aSize.height);
}

template <typename T>
static inline D2D1_RECT_F D2DRect(const T& aRect) {
  return D2D1::RectF(aRect.X(), aRect.Y(), aRect.XMost(), aRect.YMost());
}

static inline D2D1_ROUNDED_RECT D2DRoundedRect(const RoundedRect& aRect) {
  return D2D1::RoundedRect(D2DRect(aRect.rect),
                           aRect.corners.BottomLeft().width,
                           aRect.corners.BottomLeft().height);
}

static inline D2D1_EXTEND_MODE D2DExtend(ExtendMode aExtendMode, Axis aAxis) {
  D2D1_EXTEND_MODE extend;
  switch (aExtendMode) {
    case ExtendMode::REPEAT:
      extend = D2D1_EXTEND_MODE_WRAP;
      break;
    case ExtendMode::REPEAT_X: {
      extend = aAxis == Axis::X_AXIS ? D2D1_EXTEND_MODE_WRAP
                                     : D2D1_EXTEND_MODE_CLAMP;
      break;
    }
    case ExtendMode::REPEAT_Y: {
      extend = aAxis == Axis::Y_AXIS ? D2D1_EXTEND_MODE_WRAP
                                     : D2D1_EXTEND_MODE_CLAMP;
      break;
    }
    case ExtendMode::REFLECT:
      extend = D2D1_EXTEND_MODE_MIRROR;
      break;
    default:
      extend = D2D1_EXTEND_MODE_CLAMP;
  }

  return extend;
}

static inline D2D1_BITMAP_INTERPOLATION_MODE D2DFilter(
    const SamplingFilter aSamplingFilter) {
  switch (aSamplingFilter) {
    case SamplingFilter::POINT:
      return D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
    default:
      return D2D1_BITMAP_INTERPOLATION_MODE_LINEAR;
  }
}

static inline D2D1_INTERPOLATION_MODE D2DInterpolationMode(
    const SamplingFilter aSamplingFilter) {
  switch (aSamplingFilter) {
    case SamplingFilter::POINT:
      return D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
    default:
      return D2D1_INTERPOLATION_MODE_LINEAR;
  }
}

static inline D2D1_MATRIX_5X4_F D2DMatrix5x4(const Matrix5x4& aMatrix) {
  return D2D1::Matrix5x4F(aMatrix._11, aMatrix._12, aMatrix._13, aMatrix._14,
                          aMatrix._21, aMatrix._22, aMatrix._23, aMatrix._24,
                          aMatrix._31, aMatrix._32, aMatrix._33, aMatrix._34,
                          aMatrix._41, aMatrix._42, aMatrix._43, aMatrix._44,
                          aMatrix._51, aMatrix._52, aMatrix._53, aMatrix._54);
}

static inline D2D1_VECTOR_3F D2DVector3D(const Point3D& aPoint) {
  return D2D1::Vector3F(aPoint.x, aPoint.y, aPoint.z);
}

static inline D2D1_ANTIALIAS_MODE D2DAAMode(AntialiasMode aMode) {
  switch (aMode) {
    case AntialiasMode::NONE:
      return D2D1_ANTIALIAS_MODE_ALIASED;
    default:
      return D2D1_ANTIALIAS_MODE_PER_PRIMITIVE;
  }
}

static inline D2D1_MATRIX_3X2_F D2DMatrix(const Matrix& aTransform) {
  return D2D1::Matrix3x2F(aTransform._11, aTransform._12, aTransform._21,
                          aTransform._22, aTransform._31, aTransform._32);
}

static inline D2D1_COLOR_F D2DColor(const DeviceColor& aColor) {
  return D2D1::ColorF(aColor.r, aColor.g, aColor.b, aColor.a);
}

static inline IntSize ToIntSize(const D2D1_SIZE_U& aSize) {
  return IntSize(aSize.width, aSize.height);
}

static inline SurfaceFormat ToPixelFormat(const DXGI_FORMAT& aFormat) {
  switch (aFormat) {
    case DXGI_FORMAT_A8_UNORM:
    case DXGI_FORMAT_R8_UNORM:
      return SurfaceFormat::A8;
    default:
      return SurfaceFormat::B8G8R8A8;
  }
}

static inline SurfaceFormat ToPixelFormat(const D2D1_PIXEL_FORMAT& aFormat) {
  switch (aFormat.format) {
    case DXGI_FORMAT_A8_UNORM:
    case DXGI_FORMAT_R8_UNORM:
      return SurfaceFormat::A8;
    case DXGI_FORMAT_B8G8R8A8_UNORM:
      if (aFormat.alphaMode == D2D1_ALPHA_MODE_IGNORE) {
        return SurfaceFormat::B8G8R8X8;
      } else {
        return SurfaceFormat::B8G8R8A8;
      }
    default:
      return SurfaceFormat::B8G8R8A8;
  }
}

static inline Rect ToRect(const D2D1_RECT_F& aRect) {
  return Rect(aRect.left, aRect.top, aRect.right - aRect.left,
              aRect.bottom - aRect.top);
}

static inline Matrix ToMatrix(const D2D1_MATRIX_3X2_F& aTransform) {
  return Matrix(aTransform._11, aTransform._12, aTransform._21, aTransform._22,
                aTransform._31, aTransform._32);
}

static inline Point ToPoint(const D2D1_POINT_2F& aPoint) {
  return Point(aPoint.x, aPoint.y);
}

static inline DXGI_FORMAT DXGIFormat(SurfaceFormat aFormat) {
  switch (aFormat) {
    case SurfaceFormat::B8G8R8A8:
      return DXGI_FORMAT_B8G8R8A8_UNORM;
    case SurfaceFormat::B8G8R8X8:
      return DXGI_FORMAT_B8G8R8A8_UNORM;
    case SurfaceFormat::A8:
      return DXGI_FORMAT_A8_UNORM;
    default:
      return DXGI_FORMAT_UNKNOWN;
  }
}

static inline D2D1_ALPHA_MODE D2DAlphaModeForFormat(SurfaceFormat aFormat) {
  switch (aFormat) {
    case SurfaceFormat::B8G8R8X8:
      return D2D1_ALPHA_MODE_IGNORE;
    default:
      return D2D1_ALPHA_MODE_PREMULTIPLIED;
  }
}

static inline D2D1_PIXEL_FORMAT D2DPixelFormat(SurfaceFormat aFormat) {
  return D2D1::PixelFormat(DXGIFormat(aFormat), D2DAlphaModeForFormat(aFormat));
}

static inline bool D2DSupportsCompositeMode(CompositionOp aOp) {
  switch (aOp) {
    case CompositionOp::OP_OVER:
    case CompositionOp::OP_ADD:
    case CompositionOp::OP_ATOP:
    case CompositionOp::OP_OUT:
    case CompositionOp::OP_IN:
    case CompositionOp::OP_SOURCE:
    case CompositionOp::OP_DEST_IN:
    case CompositionOp::OP_DEST_OUT:
    case CompositionOp::OP_DEST_OVER:
    case CompositionOp::OP_DEST_ATOP:
    case CompositionOp::OP_XOR:
      return true;
    default:
      return false;
  }
}

static inline D2D1_COMPOSITE_MODE D2DCompositionMode(CompositionOp aOp) {
  switch (aOp) {
    case CompositionOp::OP_OVER:
      return D2D1_COMPOSITE_MODE_SOURCE_OVER;
    case CompositionOp::OP_ADD:
      return D2D1_COMPOSITE_MODE_PLUS;
    case CompositionOp::OP_ATOP:
      return D2D1_COMPOSITE_MODE_SOURCE_ATOP;
    case CompositionOp::OP_OUT:
      return D2D1_COMPOSITE_MODE_SOURCE_OUT;
    case CompositionOp::OP_IN:
      return D2D1_COMPOSITE_MODE_SOURCE_IN;
    case CompositionOp::OP_SOURCE:
      return D2D1_COMPOSITE_MODE_SOURCE_COPY;
    case CompositionOp::OP_DEST_IN:
      return D2D1_COMPOSITE_MODE_DESTINATION_IN;
    case CompositionOp::OP_DEST_OUT:
      return D2D1_COMPOSITE_MODE_DESTINATION_OUT;
    case CompositionOp::OP_DEST_OVER:
      return D2D1_COMPOSITE_MODE_DESTINATION_OVER;
    case CompositionOp::OP_DEST_ATOP:
      return D2D1_COMPOSITE_MODE_DESTINATION_ATOP;
    case CompositionOp::OP_XOR:
      return D2D1_COMPOSITE_MODE_XOR;
    default:
      return D2D1_COMPOSITE_MODE_SOURCE_OVER;
  }
}

static inline D2D1_BLEND_MODE D2DBlendMode(CompositionOp aOp) {
  switch (aOp) {
    case CompositionOp::OP_MULTIPLY:
      return D2D1_BLEND_MODE_MULTIPLY;
    case CompositionOp::OP_SCREEN:
      return D2D1_BLEND_MODE_SCREEN;
    case CompositionOp::OP_OVERLAY:
      return D2D1_BLEND_MODE_OVERLAY;
    case CompositionOp::OP_DARKEN:
      return D2D1_BLEND_MODE_DARKEN;
    case CompositionOp::OP_LIGHTEN:
      return D2D1_BLEND_MODE_LIGHTEN;
    case CompositionOp::OP_COLOR_DODGE:
      return D2D1_BLEND_MODE_COLOR_DODGE;
    case CompositionOp::OP_COLOR_BURN:
      return D2D1_BLEND_MODE_COLOR_BURN;
    case CompositionOp::OP_HARD_LIGHT:
      return D2D1_BLEND_MODE_HARD_LIGHT;
    case CompositionOp::OP_SOFT_LIGHT:
      return D2D1_BLEND_MODE_SOFT_LIGHT;
    case CompositionOp::OP_DIFFERENCE:
      return D2D1_BLEND_MODE_DIFFERENCE;
    case CompositionOp::OP_EXCLUSION:
      return D2D1_BLEND_MODE_EXCLUSION;
    case CompositionOp::OP_HUE:
      return D2D1_BLEND_MODE_HUE;
    case CompositionOp::OP_SATURATION:
      return D2D1_BLEND_MODE_SATURATION;
    case CompositionOp::OP_COLOR:
      return D2D1_BLEND_MODE_COLOR;
    case CompositionOp::OP_LUMINOSITY:
      return D2D1_BLEND_MODE_LUMINOSITY;
    default:
      return D2D1_BLEND_MODE_MULTIPLY;
  }
}

static inline bool D2DSupportsPrimitiveBlendMode(CompositionOp aOp) {
  switch (aOp) {
    case CompositionOp::OP_OVER:
      //  case CompositionOp::OP_SOURCE:
      return true;
      //  case CompositionOp::OP_DARKEN:
    case CompositionOp::OP_ADD:
      return IsWindows8Point1OrGreater();
    default:
      return false;
  }
}

static inline D2D1_PRIMITIVE_BLEND D2DPrimitiveBlendMode(CompositionOp aOp) {
  switch (aOp) {
    case CompositionOp::OP_OVER:
      return D2D1_PRIMITIVE_BLEND_SOURCE_OVER;
    // D2D1_PRIMITIVE_BLEND_COPY should leave pixels out of the source's
    // bounds unchanged, but doesn't- breaking unbounded ops.
    // D2D1_PRIMITIVE_BLEND_MIN doesn't quite work like darken either, as it
    // accounts for the source alpha.
    //
    // case CompositionOp::OP_SOURCE:
    //   return D2D1_PRIMITIVE_BLEND_COPY;
    // case CompositionOp::OP_DARKEN:
    //   return D2D1_PRIMITIVE_BLEND_MIN;
    case CompositionOp::OP_ADD:
      return D2D1_PRIMITIVE_BLEND_ADD;
    default:
      return D2D1_PRIMITIVE_BLEND_SOURCE_OVER;
  }
}

static inline bool IsPatternSupportedByD2D(const Pattern& aPattern) {
  if (aPattern.GetType() == PatternType::CONIC_GRADIENT) {
    return false;
  }

  if (aPattern.GetType() != PatternType::RADIAL_GRADIENT) {
    return true;
  }

  const RadialGradientPattern* pat =
      static_cast<const RadialGradientPattern*>(&aPattern);

  if (pat->mRadius1 != 0) {
    return false;
  }

  Point diff = pat->mCenter2 - pat->mCenter1;

  if (sqrt(diff.x * diff.x + diff.y * diff.y) >= pat->mRadius2) {
    // Inner point lies outside the circle.
    return false;
  }

  return true;
}

/**
 * This structure is used to pass rectangles to our shader constant. We can use
 * this for passing rectangular areas to SetVertexShaderConstant. In the format
 * of a 4 component float(x,y,width,height). Our vertex shader can then use
 * this to construct rectangular positions from the 0,0-1,1 quad that we source
 * it with.
 */
struct ShaderConstantRectD3D10 {
  float mX, mY, mWidth, mHeight;
  ShaderConstantRectD3D10(float aX, float aY, float aWidth, float aHeight)
      : mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight) {}

  // For easy passing to SetVertexShaderConstantF.
  operator float*() { return &mX; }
};

static inline DWRITE_MATRIX DWriteMatrixFromMatrix(Matrix& aMatrix) {
  DWRITE_MATRIX mat;
  mat.m11 = aMatrix._11;
  mat.m12 = aMatrix._12;
  mat.m21 = aMatrix._21;
  mat.m22 = aMatrix._22;
  mat.dx = aMatrix._31;
  mat.dy = aMatrix._32;
  return mat;
}

class AutoDWriteGlyphRun : public DWRITE_GLYPH_RUN {
  static const unsigned kNumAutoGlyphs = 256;

 public:
  AutoDWriteGlyphRun() { glyphCount = 0; }

  ~AutoDWriteGlyphRun() {
    if (glyphCount > kNumAutoGlyphs) {
      delete[] glyphIndices;
      delete[] glyphAdvances;
      delete[] glyphOffsets;
    }
  }

  void allocate(unsigned aNumGlyphs) {
    glyphCount = aNumGlyphs;
    if (aNumGlyphs <= kNumAutoGlyphs) {
      glyphIndices = &mAutoIndices[0];
      glyphAdvances = &mAutoAdvances[0];
      glyphOffsets = &mAutoOffsets[0];
    } else {
      glyphIndices = new UINT16[aNumGlyphs];
      glyphAdvances = new FLOAT[aNumGlyphs];
      glyphOffsets = new DWRITE_GLYPH_OFFSET[aNumGlyphs];
    }
  }

 private:
  DWRITE_GLYPH_OFFSET mAutoOffsets[kNumAutoGlyphs];
  FLOAT mAutoAdvances[kNumAutoGlyphs];
  UINT16 mAutoIndices[kNumAutoGlyphs];
};

static inline void DWriteGlyphRunFromGlyphs(const GlyphBuffer& aGlyphs,
                                            ScaledFontDWrite* aFont,
                                            AutoDWriteGlyphRun* run) {
  run->allocate(aGlyphs.mNumGlyphs);

  FLOAT* advances = const_cast<FLOAT*>(run->glyphAdvances);
  UINT16* indices = const_cast<UINT16*>(run->glyphIndices);
  DWRITE_GLYPH_OFFSET* offsets =
      const_cast<DWRITE_GLYPH_OFFSET*>(run->glyphOffsets);

  memset(advances, 0, sizeof(FLOAT) * aGlyphs.mNumGlyphs);
  for (unsigned int i = 0; i < aGlyphs.mNumGlyphs; i++) {
    indices[i] = aGlyphs.mGlyphs[i].mIndex;
    offsets[i].advanceOffset = aGlyphs.mGlyphs[i].mPosition.x;
    offsets[i].ascenderOffset = -aGlyphs.mGlyphs[i].mPosition.y;
  }

  run->bidiLevel = 0;
  run->fontFace = aFont->mFontFace;
  run->fontEmSize = aFont->GetSize();
  run->glyphCount = aGlyphs.mNumGlyphs;
  run->isSideways = FALSE;
}

static inline already_AddRefed<ID2D1Geometry> ConvertRectToGeometry(
    const D2D1_RECT_F& aRect) {
  RefPtr<ID2D1RectangleGeometry> rectGeom;
  D2DFactory()->CreateRectangleGeometry(&aRect, getter_AddRefs(rectGeom));
  return rectGeom.forget();
}

static inline already_AddRefed<ID2D1Geometry> GetTransformedGeometry(
    ID2D1Geometry* aGeometry, const D2D1_MATRIX_3X2_F& aTransform) {
  RefPtr<ID2D1PathGeometry> tmpGeometry;
  D2DFactory()->CreatePathGeometry(getter_AddRefs(tmpGeometry));
  RefPtr<ID2D1GeometrySink> currentSink;
  tmpGeometry->Open(getter_AddRefs(currentSink));
  aGeometry->Simplify(D2D1_GEOMETRY_SIMPLIFICATION_OPTION_CUBICS_AND_LINES,
                      aTransform, currentSink);
  currentSink->Close();
  return tmpGeometry.forget();
}

static inline already_AddRefed<ID2D1Geometry> IntersectGeometry(
    ID2D1Geometry* aGeometryA, ID2D1Geometry* aGeometryB) {
  RefPtr<ID2D1PathGeometry> pathGeom;
  D2DFactory()->CreatePathGeometry(getter_AddRefs(pathGeom));
  RefPtr<ID2D1GeometrySink> sink;
  pathGeom->Open(getter_AddRefs(sink));
  aGeometryA->CombineWithGeometry(aGeometryB, D2D1_COMBINE_MODE_INTERSECT,
                                  nullptr, sink);
  sink->Close();

  return pathGeom.forget();
}

static inline already_AddRefed<ID2D1StrokeStyle> CreateStrokeStyleForOptions(
    const StrokeOptions& aStrokeOptions) {
  RefPtr<ID2D1StrokeStyle> style;

  D2D1_CAP_STYLE capStyle;
  D2D1_LINE_JOIN joinStyle;

  switch (aStrokeOptions.mLineCap) {
    case CapStyle::BUTT:
      capStyle = D2D1_CAP_STYLE_FLAT;
      break;
    case CapStyle::ROUND:
      capStyle = D2D1_CAP_STYLE_ROUND;
      break;
    case CapStyle::SQUARE:
      capStyle = D2D1_CAP_STYLE_SQUARE;
      break;
  }

  switch (aStrokeOptions.mLineJoin) {
    case JoinStyle::MITER:
      joinStyle = D2D1_LINE_JOIN_MITER;
      break;
    case JoinStyle::MITER_OR_BEVEL:
      joinStyle = D2D1_LINE_JOIN_MITER_OR_BEVEL;
      break;
    case JoinStyle::ROUND:
      joinStyle = D2D1_LINE_JOIN_ROUND;
      break;
    case JoinStyle::BEVEL:
      joinStyle = D2D1_LINE_JOIN_BEVEL;
      break;
  }

  HRESULT hr;
  // We need to check mDashLength in addition to mDashPattern here since if
  // mDashPattern is set but mDashLength is zero then the stroke will fail to
  // paint.
  if (aStrokeOptions.mDashLength > 0 && aStrokeOptions.mDashPattern) {
    typedef std::vector<Float> FloatVector;
    // D2D "helpfully" multiplies the dash pattern by the line width.
    // That's not what cairo does, or is what <canvas>'s dash wants.
    // So fix the multiplication in advance.
    Float lineWidth = aStrokeOptions.mLineWidth;
    FloatVector dash(aStrokeOptions.mDashPattern,
                     aStrokeOptions.mDashPattern + aStrokeOptions.mDashLength);
    for (FloatVector::iterator it = dash.begin(); it != dash.end(); ++it) {
      *it /= lineWidth;
    }

    hr = D2DFactory()->CreateStrokeStyle(
        D2D1::StrokeStyleProperties(
            capStyle, capStyle, capStyle, joinStyle, aStrokeOptions.mMiterLimit,
            D2D1_DASH_STYLE_CUSTOM, aStrokeOptions.mDashOffset / lineWidth),
        &dash[0],  // data() is not C++98, although it's in recent gcc
                   // and VC10's STL
        dash.size(), getter_AddRefs(style));
  } else {
    hr = D2DFactory()->CreateStrokeStyle(
        D2D1::StrokeStyleProperties(capStyle, capStyle, capStyle, joinStyle,
                                    aStrokeOptions.mMiterLimit),
        nullptr, 0, getter_AddRefs(style));
  }

  if (FAILED(hr)) {
    gfxWarning() << "Failed to create Direct2D stroke style.";
  }

  return style.forget();
}

// This creates a (partially) uploaded bitmap for a DataSourceSurface. It
// uploads the minimum requirement and possibly downscales. It adjusts the
// input Matrix to compensate.
static inline already_AddRefed<ID2D1Bitmap> CreatePartialBitmapForSurface(
    DataSourceSurface* aSurface, const Matrix& aDestinationTransform,
    const IntSize& aDestinationSize, ExtendMode aExtendMode,
    Matrix& aSourceTransform, ID2D1RenderTarget* aRT,
    const IntRect* aSourceRect = nullptr) {
  RefPtr<ID2D1Bitmap> bitmap;

  // This is where things get complicated. The source surface was
  // created for a surface that was too large to fit in a texture.
  // We'll need to figure out if we can work with a partial upload
  // or downsample in software.

  Matrix transform = aDestinationTransform;
  Matrix invTransform = transform = aSourceTransform * transform;
  if (!invTransform.Invert()) {
    // Singular transform, nothing to be drawn.
    return nullptr;
  }

  Rect rect(0, 0, Float(aDestinationSize.width),
            Float(aDestinationSize.height));

  // Calculate the rectangle of the source mapped to our surface.
  rect = invTransform.TransformBounds(rect);
  rect.RoundOut();

  IntSize size = aSurface->GetSize();

  Rect uploadRect(0, 0, Float(size.width), Float(size.height));
  if (aSourceRect) {
    uploadRect = Rect(aSourceRect->X(), aSourceRect->Y(), aSourceRect->Width(),
                      aSourceRect->Height());
  }

  // Limit the uploadRect as much as possible without supporting discontiguous
  // uploads
  //
  // clang-format off
  //                               region we will paint from
  //   uploadRect
  //   .---------------.              .---------------.         resulting uploadRect
  //   |               |rect          |               |
  //   |          .---------.         .----.     .----.          .---------------.
  //   |          |         |  ---->  |    |     |    |   ---->  |               |
  //   |          '---------'         '----'     '----'          '---------------'
  //   '---------------'              '---------------'
  // clang-format on
  //
  //

  int Bpp = BytesPerPixel(aSurface->GetFormat());

  if (uploadRect.Contains(rect)) {
    // Extend mode is irrelevant, the displayed rect is completely contained
    // by the source bitmap.
    uploadRect = rect;
  } else if (aExtendMode == ExtendMode::CLAMP && uploadRect.Intersects(rect)) {
    // Calculate the rectangle on the source bitmap that touches our
    // surface, and upload that, for ExtendMode::CLAMP we can actually guarantee
    // correct behaviour in this case.
    uploadRect = uploadRect.Intersect(rect);

    // We now proceed to check if we can limit at least one dimension of the
    // upload rect safely without looking at extend mode.
  } else if (rect.X() >= 0 && rect.XMost() < size.width) {
    uploadRect.MoveToX(rect.X());
    uploadRect.SetWidth(rect.Width());
  } else if (rect.Y() >= 0 && rect.YMost() < size.height) {
    uploadRect.MoveToY(rect.Y());
    uploadRect.SetHeight(rect.Height());
  }

  if (uploadRect.IsEmpty()) {
    // Nothing to be drawn.
    return nullptr;
  }

  if (uploadRect.Width() <= aRT->GetMaximumBitmapSize() &&
      uploadRect.Height() <= aRT->GetMaximumBitmapSize()) {
    {
      // Scope to auto-Unmap() |mapping|.
      DataSourceSurface::ScopedMap mapping(aSurface, DataSourceSurface::READ);
      if (MOZ2D_WARN_IF(!mapping.IsMapped())) {
        return nullptr;
      }

      // A partial upload will suffice.
      aRT->CreateBitmap(
          D2D1::SizeU(uint32_t(uploadRect.Width()),
                      uint32_t(uploadRect.Height())),
          mapping.GetData() + int(uploadRect.X()) * Bpp +
              int(uploadRect.Y()) * mapping.GetStride(),
          mapping.GetStride(),
          D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
          getter_AddRefs(bitmap));
    }

    aSourceTransform.PreTranslate(uploadRect.X(), uploadRect.Y());

    return bitmap.forget();
  } else {
    if (Bpp != 4) {
      // This shouldn't actually happen in practice!
      MOZ_ASSERT(false);
      return nullptr;
    }

    {
      // Scope to auto-Unmap() |mapping|.
      DataSourceSurface::ScopedMap mapping(aSurface, DataSourceSurface::READ);
      if (MOZ2D_WARN_IF(!mapping.IsMapped())) {
        return nullptr;
      }
      ImageHalfScaler scaler(mapping.GetData(), mapping.GetStride(), size);

      // Calculate the maximum width/height of the image post transform.
      Point topRight = transform.TransformPoint(Point(Float(size.width), 0));
      Point topLeft = transform.TransformPoint(Point(0, 0));
      Point bottomRight = transform.TransformPoint(
          Point(Float(size.width), Float(size.height)));
      Point bottomLeft = transform.TransformPoint(Point(0, Float(size.height)));

      IntSize scaleSize;

      scaleSize.width = int32_t(std::max(Distance(topRight, topLeft),
                                         Distance(bottomRight, bottomLeft)));
      scaleSize.height = int32_t(std::max(Distance(topRight, bottomRight),
                                          Distance(topLeft, bottomLeft)));

      if (unsigned(scaleSize.width) > aRT->GetMaximumBitmapSize()) {
        // Ok, in this case we'd really want a downscale of a part of the
        // bitmap, perhaps we can do this later but for simplicity let's do
        // something different here and assume it's good enough, this should be
        // rare!
        scaleSize.width = 4095;
      }
      if (unsigned(scaleSize.height) > aRT->GetMaximumBitmapSize()) {
        scaleSize.height = 4095;
      }

      scaler.ScaleForSize(scaleSize);

      IntSize newSize = scaler.GetSize();

      if (newSize.IsEmpty()) {
        return nullptr;
      }

      aRT->CreateBitmap(
          D2D1::SizeU(newSize.width, newSize.height), scaler.GetScaledData(),
          scaler.GetStride(),
          D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
          getter_AddRefs(bitmap));

      aSourceTransform.PreScale(Float(size.width) / newSize.width,
                                Float(size.height) / newSize.height);
    }
    return bitmap.forget();
  }
}

static inline void AddRectToSink(ID2D1GeometrySink* aSink,
                                 const D2D1_RECT_F& aRect) {
  aSink->BeginFigure(D2D1::Point2F(aRect.left, aRect.top),
                     D2D1_FIGURE_BEGIN_FILLED);
  aSink->AddLine(D2D1::Point2F(aRect.right, aRect.top));
  aSink->AddLine(D2D1::Point2F(aRect.right, aRect.bottom));
  aSink->AddLine(D2D1::Point2F(aRect.left, aRect.bottom));
  aSink->EndFigure(D2D1_FIGURE_END_CLOSED);
}

class DCCommandSink : public ID2D1CommandSink {
 public:
  explicit DCCommandSink(ID2D1DeviceContext* aCtx) : mCtx(aCtx) {}

  HRESULT STDMETHODCALLTYPE QueryInterface(const IID& aIID, void** aPtr) {
    if (!aPtr) {
      return E_POINTER;
    }

    if (aIID == IID_IUnknown) {
      *aPtr = static_cast<IUnknown*>(this);
      return S_OK;
    } else if (aIID == IID_ID2D1CommandSink) {
      *aPtr = static_cast<ID2D1CommandSink*>(this);
      return S_OK;
    }

    return E_NOINTERFACE;
  }

  ULONG STDMETHODCALLTYPE AddRef() { return 1; }

  ULONG STDMETHODCALLTYPE Release() { return 1; }

  STDMETHODIMP BeginDraw() {
    // We don't want to do anything here!
    return S_OK;
  }
  STDMETHODIMP EndDraw() {
    // We don't want to do anything here!
    return S_OK;
  }

  STDMETHODIMP SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode) {
    mCtx->SetAntialiasMode(antialiasMode);
    return S_OK;
  }

  STDMETHODIMP SetTags(D2D1_TAG tag1, D2D1_TAG tag2) {
    mCtx->SetTags(tag1, tag2);
    return S_OK;
  }

  STDMETHODIMP SetTextAntialiasMode(
      D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode) {
    mCtx->SetTextAntialiasMode(textAntialiasMode);
    return S_OK;
  }

  STDMETHODIMP SetTextRenderingParams(
      _In_opt_ IDWriteRenderingParams* textRenderingParams) {
    mCtx->SetTextRenderingParams(textRenderingParams);
    return S_OK;
  }

  STDMETHODIMP SetTransform(_In_ CONST D2D1_MATRIX_3X2_F* transform) {
    mCtx->SetTransform(transform);
    return S_OK;
  }

  STDMETHODIMP SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND primitiveBlend) {
    mCtx->SetPrimitiveBlend(primitiveBlend);
    return S_OK;
  }

  STDMETHODIMP SetUnitMode(D2D1_UNIT_MODE unitMode) {
    mCtx->SetUnitMode(unitMode);
    return S_OK;
  }

  STDMETHODIMP Clear(_In_opt_ CONST D2D1_COLOR_F* color) {
    mCtx->Clear(color);
    return S_OK;
  }

  STDMETHODIMP DrawGlyphRun(
      D2D1_POINT_2F baselineOrigin, _In_ CONST DWRITE_GLYPH_RUN* glyphRun,
      _In_opt_ CONST DWRITE_GLYPH_RUN_DESCRIPTION* glyphRunDescription,
      _In_ ID2D1Brush* foregroundBrush, DWRITE_MEASURING_MODE measuringMode) {
    mCtx->DrawGlyphRun(baselineOrigin, glyphRun, glyphRunDescription,
                       foregroundBrush, measuringMode);
    return S_OK;
  }

  STDMETHODIMP DrawLine(D2D1_POINT_2F point0, D2D1_POINT_2F point1,
                        _In_ ID2D1Brush* brush, FLOAT strokeWidth,
                        _In_opt_ ID2D1StrokeStyle* strokeStyle) {
    mCtx->DrawLine(point0, point1, brush, strokeWidth, strokeStyle);
    return S_OK;
  }

  STDMETHODIMP DrawGeometry(_In_ ID2D1Geometry* geometry,
                            _In_ ID2D1Brush* brush, FLOAT strokeWidth,
                            _In_opt_ ID2D1StrokeStyle* strokeStyle) {
    mCtx->DrawGeometry(geometry, brush, strokeWidth, strokeStyle);
    return S_OK;
  }

  STDMETHODIMP DrawRectangle(_In_ CONST D2D1_RECT_F* rect,
                             _In_ ID2D1Brush* brush, FLOAT strokeWidth,
                             _In_opt_ ID2D1StrokeStyle* strokeStyle) {
    mCtx->DrawRectangle(rect, brush, strokeWidth, strokeStyle);
    return S_OK;
  }

  STDMETHODIMP DrawBitmap(
      _In_ ID2D1Bitmap* bitmap,
      _In_opt_ CONST D2D1_RECT_F* destinationRectangle, FLOAT opacity,
      D2D1_INTERPOLATION_MODE interpolationMode,
      _In_opt_ CONST D2D1_RECT_F* sourceRectangle,
      _In_opt_ CONST D2D1_MATRIX_4X4_F* perspectiveTransform) {
    mCtx->DrawBitmap(bitmap, destinationRectangle, opacity, interpolationMode,
                     sourceRectangle, perspectiveTransform);
    return S_OK;
  }

  STDMETHODIMP DrawImage(_In_ ID2D1Image* image,
                         _In_opt_ CONST D2D1_POINT_2F* targetOffset,
                         _In_opt_ CONST D2D1_RECT_F* imageRectangle,
                         D2D1_INTERPOLATION_MODE interpolationMode,
                         D2D1_COMPOSITE_MODE compositeMode) {
    mCtx->DrawImage(image, targetOffset, imageRectangle, interpolationMode,
                    compositeMode);
    return S_OK;
  }

  STDMETHODIMP DrawGdiMetafile(_In_ ID2D1GdiMetafile* gdiMetafile,
                               _In_opt_ CONST D2D1_POINT_2F* targetOffset) {
    mCtx->DrawGdiMetafile(gdiMetafile, targetOffset);
    return S_OK;
  }

  STDMETHODIMP FillMesh(_In_ ID2D1Mesh* mesh, _In_ ID2D1Brush* brush) {
    mCtx->FillMesh(mesh, brush);
    return S_OK;
  }

  STDMETHODIMP FillOpacityMask(_In_ ID2D1Bitmap* opacityMask,
                               _In_ ID2D1Brush* brush,
                               _In_opt_ CONST D2D1_RECT_F* destinationRectangle,
                               _In_opt_ CONST D2D1_RECT_F* sourceRectangle) {
    mCtx->FillOpacityMask(opacityMask, brush, destinationRectangle,
                          sourceRectangle);
    return S_OK;
  }

  STDMETHODIMP FillGeometry(_In_ ID2D1Geometry* geometry,
                            _In_ ID2D1Brush* brush,
                            _In_opt_ ID2D1Brush* opacityBrush) {
    mCtx->FillGeometry(geometry, brush, opacityBrush);
    return S_OK;
  }

  STDMETHODIMP FillRectangle(_In_ CONST D2D1_RECT_F* rect,
                             _In_ ID2D1Brush* brush) {
    mCtx->FillRectangle(rect, brush);
    return S_OK;
  }

  STDMETHODIMP PushAxisAlignedClip(_In_ CONST D2D1_RECT_F* clipRect,
                                   D2D1_ANTIALIAS_MODE antialiasMode) {
    mCtx->PushAxisAlignedClip(clipRect, antialiasMode);
    return S_OK;
  }

  STDMETHODIMP PushLayer(_In_ CONST D2D1_LAYER_PARAMETERS1* layerParameters1,
                         _In_opt_ ID2D1Layer* layer) {
    mCtx->PushLayer(layerParameters1, layer);
    return S_OK;
  }

  STDMETHODIMP PopAxisAlignedClip() {
    mCtx->PopAxisAlignedClip();
    return S_OK;
  }

  STDMETHODIMP PopLayer() {
    mCtx->PopLayer();
    return S_OK;
  }

  ID2D1DeviceContext* mCtx;
};

class MOZ_STACK_CLASS AutoRestoreFP final {
 public:
  AutoRestoreFP() {
    // save the current floating point control word
    _controlfp_s(&savedFPSetting, 0, 0);
    UINT unused;
    // set the floating point control word to its default value
    _controlfp_s(&unused, _CW_DEFAULT, MCW_PC);
  }
  ~AutoRestoreFP() {
    UINT unused;
    // restore the saved floating point control word
    _controlfp_s(&unused, savedFPSetting, MCW_PC);
  }

 private:
  UINT savedFPSetting;
};

// Note that overrides of ID2D1SimplifiedGeometrySink methods in this class may
// get called from D2D with nonstandard floating point settings (see comments in
// bug 1134549) - use AutoRestoreFP to reset the floating point control word to
// what we expect
class StreamingGeometrySink : public ID2D1SimplifiedGeometrySink {
 public:
  explicit StreamingGeometrySink(PathSink* aSink) : mSink(aSink) {}

  HRESULT STDMETHODCALLTYPE QueryInterface(const IID& aIID, void** aPtr) {
    if (!aPtr) {
      return E_POINTER;
    }

    if (aIID == IID_IUnknown) {
      *aPtr = static_cast<IUnknown*>(this);
      return S_OK;
    } else if (aIID == IID_ID2D1SimplifiedGeometrySink) {
      *aPtr = static_cast<ID2D1SimplifiedGeometrySink*>(this);
      return S_OK;
    }

    return E_NOINTERFACE;
  }

  ULONG STDMETHODCALLTYPE AddRef() { return 1; }

  ULONG STDMETHODCALLTYPE Release() { return 1; }

  // We ignore SetFillMode, this depends on the destination sink.
  STDMETHOD_(void, SetFillMode)(D2D1_FILL_MODE aMode) { return; }
  STDMETHOD_(void, BeginFigure)
  (D2D1_POINT_2F aPoint, D2D1_FIGURE_BEGIN aBegin) {
    AutoRestoreFP resetFloatingPoint;
    mSink->MoveTo(ToPoint(aPoint));
  }
  STDMETHOD_(void, AddLines)(const D2D1_POINT_2F* aLines, UINT aCount) {
    AutoRestoreFP resetFloatingPoint;
    for (UINT i = 0; i < aCount; i++) {
      mSink->LineTo(ToPoint(aLines[i]));
    }
  }
  STDMETHOD_(void, AddBeziers)
  (const D2D1_BEZIER_SEGMENT* aSegments, UINT aCount) {
    AutoRestoreFP resetFloatingPoint;
    for (UINT i = 0; i < aCount; i++) {
      mSink->BezierTo(ToPoint(aSegments[i].point1),
                      ToPoint(aSegments[i].point2),
                      ToPoint(aSegments[i].point3));
    }
  }
  STDMETHOD(Close)() { /* Should never be called! */
    return S_OK;
  }
  STDMETHOD_(void, SetSegmentFlags)
  (D2D1_PATH_SEGMENT aFlags) { /* Should never be called! */
  }

  STDMETHOD_(void, EndFigure)(D2D1_FIGURE_END aEnd) {
    AutoRestoreFP resetFloatingPoint;
    if (aEnd == D2D1_FIGURE_END_CLOSED) {
      return mSink->Close();
    }
  }

 private:
  PathSink* mSink;
};

}  // namespace gfx
}  // namespace mozilla

#endif /* MOZILLA_GFX_HELPERSD2D_H_ */