1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef MOZILLA_GFX_HELPERSD2D_H_
7 #define MOZILLA_GFX_HELPERSD2D_H_
8 
9 #include <d2d1_1.h>
10 
11 #include <vector>
12 
13 #include <dwrite.h>
14 #include <versionhelpers.h>
15 #include "2D.h"
16 #include "Logging.h"
17 #include "Tools.h"
18 #include "ImageScaling.h"
19 
20 #include "ScaledFontDWrite.h"
21 
22 #undef min
23 #undef max
24 
25 namespace mozilla {
26 namespace gfx {
27 
28 ID2D1Factory1* D2DFactory1();
D2DFactory()29 static ID2D1Factory* D2DFactory() { return D2DFactory1(); }
30 
D2DPoint(const Point & aPoint)31 static inline D2D1_POINT_2F D2DPoint(const Point &aPoint)
32 {
33   return D2D1::Point2F(aPoint.x, aPoint.y);
34 }
35 
D2DIntSize(const IntSize & aSize)36 static inline D2D1_SIZE_U D2DIntSize(const IntSize &aSize)
37 {
38   return D2D1::SizeU(aSize.width, aSize.height);
39 }
40 
41 template <typename T>
D2DRect(const T & aRect)42 static inline D2D1_RECT_F D2DRect(const T &aRect)
43 {
44   return D2D1::RectF(aRect.x, aRect.y, aRect.XMost(), aRect.YMost());
45 }
46 
D2DExtend(ExtendMode aExtendMode,Axis aAxis)47 static inline D2D1_EXTEND_MODE D2DExtend(ExtendMode aExtendMode, Axis aAxis)
48 {
49   D2D1_EXTEND_MODE extend;
50   switch (aExtendMode) {
51   case ExtendMode::REPEAT:
52     extend = D2D1_EXTEND_MODE_WRAP;
53     break;
54   case ExtendMode::REPEAT_X:
55   {
56     extend = aAxis == Axis::X_AXIS
57              ? D2D1_EXTEND_MODE_WRAP
58              : D2D1_EXTEND_MODE_CLAMP;
59     break;
60   }
61   case ExtendMode::REPEAT_Y:
62   {
63     extend = aAxis == Axis::Y_AXIS
64              ? D2D1_EXTEND_MODE_WRAP
65              : D2D1_EXTEND_MODE_CLAMP;
66     break;
67   }
68   case ExtendMode::REFLECT:
69     extend = D2D1_EXTEND_MODE_MIRROR;
70     break;
71   default:
72     extend = D2D1_EXTEND_MODE_CLAMP;
73   }
74 
75   return extend;
76 }
77 
D2DFilter(const SamplingFilter aSamplingFilter)78 static inline D2D1_BITMAP_INTERPOLATION_MODE D2DFilter(const SamplingFilter aSamplingFilter)
79 {
80   switch (aSamplingFilter) {
81   case SamplingFilter::POINT:
82     return D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
83   default:
84     return D2D1_BITMAP_INTERPOLATION_MODE_LINEAR;
85   }
86 }
87 
D2DInterpolationMode(const SamplingFilter aSamplingFilter)88 static inline D2D1_INTERPOLATION_MODE D2DInterpolationMode(const SamplingFilter aSamplingFilter)
89 {
90   switch (aSamplingFilter) {
91   case SamplingFilter::POINT:
92     return D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
93   default:
94     return D2D1_INTERPOLATION_MODE_LINEAR;
95   }
96 }
97 
D2DMatrix5x4(const Matrix5x4 & aMatrix)98 static inline D2D1_MATRIX_5X4_F D2DMatrix5x4(const Matrix5x4 &aMatrix)
99 {
100   return D2D1::Matrix5x4F(aMatrix._11, aMatrix._12, aMatrix._13, aMatrix._14,
101                           aMatrix._21, aMatrix._22, aMatrix._23, aMatrix._24,
102                           aMatrix._31, aMatrix._32, aMatrix._33, aMatrix._34,
103                           aMatrix._41, aMatrix._42, aMatrix._43, aMatrix._44,
104                           aMatrix._51, aMatrix._52, aMatrix._53, aMatrix._54);
105 }
106 
D2DVector3D(const Point3D & aPoint)107 static inline D2D1_VECTOR_3F D2DVector3D(const Point3D &aPoint)
108 {
109   return D2D1::Vector3F(aPoint.x, aPoint.y, aPoint.z);
110 }
111 
D2DAAMode(AntialiasMode aMode)112 static inline D2D1_ANTIALIAS_MODE D2DAAMode(AntialiasMode aMode)
113 {
114   switch (aMode) {
115   case AntialiasMode::NONE:
116     return D2D1_ANTIALIAS_MODE_ALIASED;
117   default:
118     return D2D1_ANTIALIAS_MODE_PER_PRIMITIVE;
119   }
120 }
121 
D2DMatrix(const Matrix & aTransform)122 static inline D2D1_MATRIX_3X2_F D2DMatrix(const Matrix &aTransform)
123 {
124   return D2D1::Matrix3x2F(aTransform._11, aTransform._12,
125                           aTransform._21, aTransform._22,
126                           aTransform._31, aTransform._32);
127 }
128 
D2DColor(const Color & aColor)129 static inline D2D1_COLOR_F D2DColor(const Color &aColor)
130 {
131   return D2D1::ColorF(aColor.r, aColor.g, aColor.b, aColor.a);
132 }
133 
ToIntSize(const D2D1_SIZE_U & aSize)134 static inline IntSize ToIntSize(const D2D1_SIZE_U &aSize)
135 {
136   return IntSize(aSize.width, aSize.height);
137 }
138 
ToPixelFormat(const D2D1_PIXEL_FORMAT & aFormat)139 static inline SurfaceFormat ToPixelFormat(const D2D1_PIXEL_FORMAT &aFormat)
140 {
141   switch(aFormat.format) {
142   case DXGI_FORMAT_A8_UNORM:
143   case DXGI_FORMAT_R8_UNORM:
144     return SurfaceFormat::A8;
145   case DXGI_FORMAT_B8G8R8A8_UNORM:
146     if (aFormat.alphaMode == D2D1_ALPHA_MODE_IGNORE) {
147       return SurfaceFormat::B8G8R8X8;
148     } else {
149       return SurfaceFormat::B8G8R8A8;
150     }
151   default:
152     return SurfaceFormat::B8G8R8A8;
153   }
154 }
155 
ToRect(const D2D1_RECT_F & aRect)156 static inline Rect ToRect(const D2D1_RECT_F &aRect)
157 {
158   return Rect(aRect.left, aRect.top, aRect.right - aRect.left, aRect.bottom - aRect.top);
159 }
160 
ToMatrix(const D2D1_MATRIX_3X2_F & aTransform)161 static inline Matrix ToMatrix(const D2D1_MATRIX_3X2_F &aTransform)
162 {
163   return Matrix(aTransform._11, aTransform._12,
164                 aTransform._21, aTransform._22,
165                 aTransform._31, aTransform._32);
166 }
167 
ToPoint(const D2D1_POINT_2F & aPoint)168 static inline Point ToPoint(const D2D1_POINT_2F &aPoint)
169 {
170   return Point(aPoint.x, aPoint.y);
171 }
172 
DXGIFormat(SurfaceFormat aFormat)173 static inline DXGI_FORMAT DXGIFormat(SurfaceFormat aFormat)
174 {
175   switch (aFormat) {
176   case SurfaceFormat::B8G8R8A8:
177     return DXGI_FORMAT_B8G8R8A8_UNORM;
178   case SurfaceFormat::B8G8R8X8:
179     return DXGI_FORMAT_B8G8R8A8_UNORM;
180   case SurfaceFormat::A8:
181     return DXGI_FORMAT_A8_UNORM;
182   default:
183     return DXGI_FORMAT_UNKNOWN;
184   }
185 }
186 
D2DAlphaModeForFormat(SurfaceFormat aFormat)187 static inline D2D1_ALPHA_MODE D2DAlphaModeForFormat(SurfaceFormat aFormat)
188 {
189   switch (aFormat) {
190   case SurfaceFormat::B8G8R8X8:
191     return D2D1_ALPHA_MODE_IGNORE;
192   default:
193     return D2D1_ALPHA_MODE_PREMULTIPLIED;
194   }
195 }
196 
D2DPixelFormat(SurfaceFormat aFormat)197 static inline D2D1_PIXEL_FORMAT D2DPixelFormat(SurfaceFormat aFormat)
198 {
199   return D2D1::PixelFormat(DXGIFormat(aFormat), D2DAlphaModeForFormat(aFormat));
200 }
201 
D2DSupportsCompositeMode(CompositionOp aOp)202 static inline bool D2DSupportsCompositeMode(CompositionOp aOp)
203 {
204   switch(aOp) {
205   case CompositionOp::OP_OVER:
206   case CompositionOp::OP_ADD:
207   case CompositionOp::OP_ATOP:
208   case CompositionOp::OP_OUT:
209   case CompositionOp::OP_IN:
210   case CompositionOp::OP_SOURCE:
211   case CompositionOp::OP_DEST_IN:
212   case CompositionOp::OP_DEST_OUT:
213   case CompositionOp::OP_DEST_OVER:
214   case CompositionOp::OP_DEST_ATOP:
215   case CompositionOp::OP_XOR:
216     return true;
217   default:
218     return false;
219   }
220 }
221 
D2DCompositionMode(CompositionOp aOp)222 static inline D2D1_COMPOSITE_MODE D2DCompositionMode(CompositionOp aOp)
223 {
224   switch(aOp) {
225   case CompositionOp::OP_OVER:
226     return D2D1_COMPOSITE_MODE_SOURCE_OVER;
227   case CompositionOp::OP_ADD:
228     return D2D1_COMPOSITE_MODE_PLUS;
229   case CompositionOp::OP_ATOP:
230     return D2D1_COMPOSITE_MODE_SOURCE_ATOP;
231   case CompositionOp::OP_OUT:
232     return D2D1_COMPOSITE_MODE_SOURCE_OUT;
233   case CompositionOp::OP_IN:
234     return D2D1_COMPOSITE_MODE_SOURCE_IN;
235   case CompositionOp::OP_SOURCE:
236     return D2D1_COMPOSITE_MODE_SOURCE_COPY;
237   case CompositionOp::OP_DEST_IN:
238     return D2D1_COMPOSITE_MODE_DESTINATION_IN;
239   case CompositionOp::OP_DEST_OUT:
240     return D2D1_COMPOSITE_MODE_DESTINATION_OUT;
241   case CompositionOp::OP_DEST_OVER:
242     return D2D1_COMPOSITE_MODE_DESTINATION_OVER;
243   case CompositionOp::OP_DEST_ATOP:
244     return D2D1_COMPOSITE_MODE_DESTINATION_ATOP;
245   case CompositionOp::OP_XOR:
246     return D2D1_COMPOSITE_MODE_XOR;
247   default:
248     return D2D1_COMPOSITE_MODE_SOURCE_OVER;
249   }
250 }
251 
D2DBlendMode(CompositionOp aOp)252 static inline D2D1_BLEND_MODE D2DBlendMode(CompositionOp aOp)
253 {
254   switch (aOp) {
255   case CompositionOp::OP_MULTIPLY:
256     return D2D1_BLEND_MODE_MULTIPLY;
257   case CompositionOp::OP_SCREEN:
258     return D2D1_BLEND_MODE_SCREEN;
259   case CompositionOp::OP_OVERLAY:
260     return D2D1_BLEND_MODE_OVERLAY;
261   case CompositionOp::OP_DARKEN:
262     return D2D1_BLEND_MODE_DARKEN;
263   case CompositionOp::OP_LIGHTEN:
264     return D2D1_BLEND_MODE_LIGHTEN;
265   case CompositionOp::OP_COLOR_DODGE:
266     return D2D1_BLEND_MODE_COLOR_DODGE;
267   case CompositionOp::OP_COLOR_BURN:
268     return D2D1_BLEND_MODE_COLOR_BURN;
269   case CompositionOp::OP_HARD_LIGHT:
270     return D2D1_BLEND_MODE_HARD_LIGHT;
271   case CompositionOp::OP_SOFT_LIGHT:
272     return D2D1_BLEND_MODE_SOFT_LIGHT;
273   case CompositionOp::OP_DIFFERENCE:
274     return D2D1_BLEND_MODE_DIFFERENCE;
275   case CompositionOp::OP_EXCLUSION:
276     return D2D1_BLEND_MODE_EXCLUSION;
277   case CompositionOp::OP_HUE:
278     return D2D1_BLEND_MODE_HUE;
279   case CompositionOp::OP_SATURATION:
280     return D2D1_BLEND_MODE_SATURATION;
281   case CompositionOp::OP_COLOR:
282     return D2D1_BLEND_MODE_COLOR;
283   case CompositionOp::OP_LUMINOSITY:
284     return D2D1_BLEND_MODE_LUMINOSITY;
285   default:
286     return D2D1_BLEND_MODE_MULTIPLY;
287   }
288 }
289 
D2DSupportsPrimitiveBlendMode(CompositionOp aOp)290 static inline bool D2DSupportsPrimitiveBlendMode(CompositionOp aOp)
291 {
292   switch (aOp) {
293     case CompositionOp::OP_OVER:
294 //  case CompositionOp::OP_SOURCE:
295       return true;
296 //  case CompositionOp::OP_DARKEN:
297     case CompositionOp::OP_ADD:
298       return IsWindows8Point1OrGreater();
299     default:
300       return false;
301   }
302 }
303 
D2DPrimitiveBlendMode(CompositionOp aOp)304 static inline D2D1_PRIMITIVE_BLEND D2DPrimitiveBlendMode(CompositionOp aOp)
305 {
306   switch (aOp) {
307     case CompositionOp::OP_OVER:
308       return D2D1_PRIMITIVE_BLEND_SOURCE_OVER;
309     // D2D1_PRIMITIVE_BLEND_COPY should leave pixels out of the source's
310     // bounds unchanged, but doesn't- breaking unbounded ops.
311     // D2D1_PRIMITIVE_BLEND_MIN doesn't quite work like darken either, as it
312     // accounts for the source alpha.
313     //
314     // case CompositionOp::OP_SOURCE:
315     //   return D2D1_PRIMITIVE_BLEND_COPY;
316     // case CompositionOp::OP_DARKEN:
317     //   return D2D1_PRIMITIVE_BLEND_MIN;
318     case CompositionOp::OP_ADD:
319       return D2D1_PRIMITIVE_BLEND_ADD;
320     default:
321       return D2D1_PRIMITIVE_BLEND_SOURCE_OVER;
322   }
323 }
324 
IsPatternSupportedByD2D(const Pattern & aPattern)325 static inline bool IsPatternSupportedByD2D(const Pattern &aPattern)
326 {
327   if (aPattern.GetType() != PatternType::RADIAL_GRADIENT) {
328     return true;
329   }
330 
331   const RadialGradientPattern *pat =
332     static_cast<const RadialGradientPattern*>(&aPattern);
333 
334   if (pat->mRadius1 != 0) {
335     return false;
336   }
337 
338   Point diff = pat->mCenter2 - pat->mCenter1;
339 
340   if (sqrt(diff.x * diff.x + diff.y * diff.y) >= pat->mRadius2) {
341     // Inner point lies outside the circle.
342     return false;
343   }
344 
345   return true;
346 }
347 
348 /**
349  * This structure is used to pass rectangles to our shader constant. We can use
350  * this for passing rectangular areas to SetVertexShaderConstant. In the format
351  * of a 4 component float(x,y,width,height). Our vertex shader can then use
352  * this to construct rectangular positions from the 0,0-1,1 quad that we source
353  * it with.
354  */
355 struct ShaderConstantRectD3D10
356 {
357   float mX, mY, mWidth, mHeight;
ShaderConstantRectD3D10ShaderConstantRectD3D10358   ShaderConstantRectD3D10(float aX, float aY, float aWidth, float aHeight)
359     : mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight)
360   { }
361 
362   // For easy passing to SetVertexShaderConstantF.
363   operator float* () { return &mX; }
364 };
365 
366 static inline DWRITE_MATRIX
DWriteMatrixFromMatrix(Matrix & aMatrix)367 DWriteMatrixFromMatrix(Matrix &aMatrix)
368 {
369   DWRITE_MATRIX mat;
370   mat.m11 = aMatrix._11;
371   mat.m12 = aMatrix._12;
372   mat.m21 = aMatrix._21;
373   mat.m22 = aMatrix._22;
374   mat.dx = aMatrix._31;
375   mat.dy = aMatrix._32;
376   return mat;
377 }
378 
379 class AutoDWriteGlyphRun : public DWRITE_GLYPH_RUN
380 {
381     static const unsigned kNumAutoGlyphs = 256;
382 
383 public:
AutoDWriteGlyphRun()384     AutoDWriteGlyphRun() {
385         glyphCount = 0;
386     }
387 
~AutoDWriteGlyphRun()388     ~AutoDWriteGlyphRun() {
389         if (glyphCount > kNumAutoGlyphs) {
390             delete[] glyphIndices;
391             delete[] glyphAdvances;
392             delete[] glyphOffsets;
393         }
394     }
395 
allocate(unsigned aNumGlyphs)396     void allocate(unsigned aNumGlyphs) {
397         glyphCount = aNumGlyphs;
398         if (aNumGlyphs <= kNumAutoGlyphs) {
399             glyphIndices = &mAutoIndices[0];
400             glyphAdvances = &mAutoAdvances[0];
401             glyphOffsets = &mAutoOffsets[0];
402         } else {
403             glyphIndices = new UINT16[aNumGlyphs];
404             glyphAdvances = new FLOAT[aNumGlyphs];
405             glyphOffsets = new DWRITE_GLYPH_OFFSET[aNumGlyphs];
406         }
407     }
408 
409 private:
410     DWRITE_GLYPH_OFFSET mAutoOffsets[kNumAutoGlyphs];
411     FLOAT               mAutoAdvances[kNumAutoGlyphs];
412     UINT16              mAutoIndices[kNumAutoGlyphs];
413 };
414 
415 static inline void
DWriteGlyphRunFromGlyphs(const GlyphBuffer & aGlyphs,ScaledFontDWrite * aFont,AutoDWriteGlyphRun * run)416 DWriteGlyphRunFromGlyphs(const GlyphBuffer &aGlyphs, ScaledFontDWrite *aFont, AutoDWriteGlyphRun *run)
417 {
418   run->allocate(aGlyphs.mNumGlyphs);
419 
420   FLOAT *advances = const_cast<FLOAT*>(run->glyphAdvances);
421   UINT16 *indices = const_cast<UINT16*>(run->glyphIndices);
422   DWRITE_GLYPH_OFFSET *offsets = const_cast<DWRITE_GLYPH_OFFSET*>(run->glyphOffsets);
423 
424   memset(advances, 0, sizeof(FLOAT) * aGlyphs.mNumGlyphs);
425   for (unsigned int i = 0; i < aGlyphs.mNumGlyphs; i++) {
426     indices[i] = aGlyphs.mGlyphs[i].mIndex;
427     offsets[i].advanceOffset = aGlyphs.mGlyphs[i].mPosition.x;
428     offsets[i].ascenderOffset = -aGlyphs.mGlyphs[i].mPosition.y;
429   }
430 
431   run->bidiLevel = 0;
432   run->fontFace = aFont->mFontFace;
433   run->fontEmSize = aFont->GetSize();
434   run->glyphCount = aGlyphs.mNumGlyphs;
435   run->isSideways = FALSE;
436 }
437 
438 static inline already_AddRefed<ID2D1Geometry>
ConvertRectToGeometry(const D2D1_RECT_F & aRect)439 ConvertRectToGeometry(const D2D1_RECT_F& aRect)
440 {
441   RefPtr<ID2D1RectangleGeometry> rectGeom;
442   D2DFactory()->CreateRectangleGeometry(&aRect, getter_AddRefs(rectGeom));
443   return rectGeom.forget();
444 }
445 
446 static inline already_AddRefed<ID2D1Geometry>
GetTransformedGeometry(ID2D1Geometry * aGeometry,const D2D1_MATRIX_3X2_F & aTransform)447 GetTransformedGeometry(ID2D1Geometry *aGeometry, const D2D1_MATRIX_3X2_F &aTransform)
448 {
449   RefPtr<ID2D1PathGeometry> tmpGeometry;
450   D2DFactory()->CreatePathGeometry(getter_AddRefs(tmpGeometry));
451   RefPtr<ID2D1GeometrySink> currentSink;
452   tmpGeometry->Open(getter_AddRefs(currentSink));
453   aGeometry->Simplify(D2D1_GEOMETRY_SIMPLIFICATION_OPTION_CUBICS_AND_LINES,
454                       aTransform, currentSink);
455   currentSink->Close();
456   return tmpGeometry.forget();
457 }
458 
459 static inline already_AddRefed<ID2D1Geometry>
IntersectGeometry(ID2D1Geometry * aGeometryA,ID2D1Geometry * aGeometryB)460 IntersectGeometry(ID2D1Geometry *aGeometryA, ID2D1Geometry *aGeometryB)
461 {
462   RefPtr<ID2D1PathGeometry> pathGeom;
463   D2DFactory()->CreatePathGeometry(getter_AddRefs(pathGeom));
464   RefPtr<ID2D1GeometrySink> sink;
465   pathGeom->Open(getter_AddRefs(sink));
466   aGeometryA->CombineWithGeometry(aGeometryB, D2D1_COMBINE_MODE_INTERSECT, nullptr, sink);
467   sink->Close();
468 
469   return pathGeom.forget();
470 }
471 
472 static inline already_AddRefed<ID2D1StrokeStyle>
CreateStrokeStyleForOptions(const StrokeOptions & aStrokeOptions)473 CreateStrokeStyleForOptions(const StrokeOptions &aStrokeOptions)
474 {
475   RefPtr<ID2D1StrokeStyle> style;
476 
477   D2D1_CAP_STYLE capStyle;
478   D2D1_LINE_JOIN joinStyle;
479 
480   switch (aStrokeOptions.mLineCap) {
481   case CapStyle::BUTT:
482     capStyle = D2D1_CAP_STYLE_FLAT;
483     break;
484   case CapStyle::ROUND:
485     capStyle = D2D1_CAP_STYLE_ROUND;
486     break;
487   case CapStyle::SQUARE:
488     capStyle = D2D1_CAP_STYLE_SQUARE;
489     break;
490   }
491 
492   switch (aStrokeOptions.mLineJoin) {
493   case JoinStyle::MITER:
494     joinStyle = D2D1_LINE_JOIN_MITER;
495     break;
496   case JoinStyle::MITER_OR_BEVEL:
497     joinStyle = D2D1_LINE_JOIN_MITER_OR_BEVEL;
498     break;
499   case JoinStyle::ROUND:
500     joinStyle = D2D1_LINE_JOIN_ROUND;
501     break;
502   case JoinStyle::BEVEL:
503     joinStyle = D2D1_LINE_JOIN_BEVEL;
504     break;
505   }
506 
507 
508   HRESULT hr;
509   // We need to check mDashLength in addition to mDashPattern here since if
510   // mDashPattern is set but mDashLength is zero then the stroke will fail to
511   // paint.
512   if (aStrokeOptions.mDashLength > 0 && aStrokeOptions.mDashPattern) {
513     typedef std::vector<Float> FloatVector;
514     // D2D "helpfully" multiplies the dash pattern by the line width.
515     // That's not what cairo does, or is what <canvas>'s dash wants.
516     // So fix the multiplication in advance.
517     Float lineWidth = aStrokeOptions.mLineWidth;
518     FloatVector dash(aStrokeOptions.mDashPattern,
519                      aStrokeOptions.mDashPattern + aStrokeOptions.mDashLength);
520     for (FloatVector::iterator it = dash.begin(); it != dash.end(); ++it) {
521       *it /= lineWidth;
522     }
523 
524     hr = D2DFactory()->CreateStrokeStyle(
525       D2D1::StrokeStyleProperties(capStyle, capStyle,
526                                   capStyle, joinStyle,
527                                   aStrokeOptions.mMiterLimit,
528                                   D2D1_DASH_STYLE_CUSTOM,
529                                   aStrokeOptions.mDashOffset / lineWidth),
530       &dash[0], // data() is not C++98, although it's in recent gcc
531                 // and VC10's STL
532       dash.size(),
533       getter_AddRefs(style));
534   } else {
535     hr = D2DFactory()->CreateStrokeStyle(
536       D2D1::StrokeStyleProperties(capStyle, capStyle,
537                                   capStyle, joinStyle,
538                                   aStrokeOptions.mMiterLimit),
539       nullptr, 0, getter_AddRefs(style));
540   }
541 
542   if (FAILED(hr)) {
543     gfxWarning() << "Failed to create Direct2D stroke style.";
544   }
545 
546   return style.forget();
547 }
548 
549 // This creates a (partially) uploaded bitmap for a DataSourceSurface. It
550 // uploads the minimum requirement and possibly downscales. It adjusts the
551 // input Matrix to compensate.
552 static inline already_AddRefed<ID2D1Bitmap>
553 CreatePartialBitmapForSurface(DataSourceSurface *aSurface, const Matrix &aDestinationTransform,
554                               const IntSize &aDestinationSize, ExtendMode aExtendMode,
555                               Matrix &aSourceTransform, ID2D1RenderTarget *aRT,
556                               const IntRect* aSourceRect = nullptr)
557 {
558   RefPtr<ID2D1Bitmap> bitmap;
559 
560   // This is where things get complicated. The source surface was
561   // created for a surface that was too large to fit in a texture.
562   // We'll need to figure out if we can work with a partial upload
563   // or downsample in software.
564 
565   Matrix transform = aDestinationTransform;
566   Matrix invTransform = transform = aSourceTransform * transform;
567   if (!invTransform.Invert()) {
568     // Singular transform, nothing to be drawn.
569     return nullptr;
570   }
571 
572   Rect rect(0, 0, Float(aDestinationSize.width), Float(aDestinationSize.height));
573 
574   // Calculate the rectangle of the source mapped to our surface.
575   rect = invTransform.TransformBounds(rect);
576   rect.RoundOut();
577 
578   IntSize size = aSurface->GetSize();
579 
580   Rect uploadRect(0, 0, Float(size.width), Float(size.height));
581   if (aSourceRect) {
582     uploadRect = Rect(aSourceRect->x, aSourceRect->y, aSourceRect->width, aSourceRect->height);
583   }
584 
585   // Limit the uploadRect as much as possible without supporting discontiguous uploads
586   //
587   //                               region we will paint from
588   //   uploadRect
589   //   .---------------.              .---------------.         resulting uploadRect
590   //   |               |rect          |               |
591   //   |          .---------.         .----.     .----.          .---------------.
592   //   |          |         |  ---->  |    |     |    |   ---->  |               |
593   //   |          '---------'         '----'     '----'          '---------------'
594   //   '---------------'              '---------------'
595   //
596   //
597 
598   if (uploadRect.Contains(rect)) {
599     // Extend mode is irrelevant, the displayed rect is completely contained
600     // by the source bitmap.
601     uploadRect = rect;
602   } else if (aExtendMode == ExtendMode::CLAMP && uploadRect.Intersects(rect)) {
603     // Calculate the rectangle on the source bitmap that touches our
604     // surface, and upload that, for ExtendMode::CLAMP we can actually guarantee
605     // correct behaviour in this case.
606     uploadRect = uploadRect.Intersect(rect);
607 
608     // We now proceed to check if we can limit at least one dimension of the
609     // upload rect safely without looking at extend mode.
610   } else if (rect.x >= 0 && rect.XMost() < size.width) {
611     uploadRect.x = rect.x;
612     uploadRect.width = rect.width;
613   } else if (rect.y >= 0 && rect.YMost() < size.height) {
614     uploadRect.y = rect.y;
615     uploadRect.height = rect.height;
616   }
617 
618   if (uploadRect.IsEmpty()) {
619     // Nothing to be drawn.
620     return nullptr;
621   }
622 
623   if (uploadRect.width <= aRT->GetMaximumBitmapSize() &&
624       uploadRect.height <= aRT->GetMaximumBitmapSize()) {
625     {
626       // Scope to auto-Unmap() |mapping|.
627       DataSourceSurface::ScopedMap mapping(aSurface, DataSourceSurface::READ);
628       if (MOZ2D_WARN_IF(!mapping.IsMapped())) {
629         return nullptr;
630       }
631 
632       // A partial upload will suffice.
633       aRT->CreateBitmap(D2D1::SizeU(uint32_t(uploadRect.width), uint32_t(uploadRect.height)),
634                         mapping.GetData() + int(uploadRect.x) * 4 + int(uploadRect.y) * mapping.GetStride(),
635                         mapping.GetStride(),
636                         D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
637                         getter_AddRefs(bitmap));
638     }
639 
640     aSourceTransform.PreTranslate(uploadRect.x, uploadRect.y);
641 
642     return bitmap.forget();
643   } else {
644     int Bpp = BytesPerPixel(aSurface->GetFormat());
645 
646     if (Bpp != 4) {
647       // This shouldn't actually happen in practice!
648       MOZ_ASSERT(false);
649       return nullptr;
650     }
651 
652     {
653       // Scope to auto-Unmap() |mapping|.
654       DataSourceSurface::ScopedMap mapping(aSurface, DataSourceSurface::READ);
655       if (MOZ2D_WARN_IF(!mapping.IsMapped())) {
656         return nullptr;
657       }
658       ImageHalfScaler scaler(mapping.GetData(), mapping.GetStride(), size);
659 
660       // Calculate the maximum width/height of the image post transform.
661       Point topRight = transform.TransformPoint(Point(Float(size.width), 0));
662       Point topLeft = transform.TransformPoint(Point(0, 0));
663       Point bottomRight = transform.TransformPoint(Point(Float(size.width), Float(size.height)));
664       Point bottomLeft = transform.TransformPoint(Point(0, Float(size.height)));
665 
666       IntSize scaleSize;
667 
668       scaleSize.width = int32_t(std::max(Distance(topRight, topLeft),
669                                          Distance(bottomRight, bottomLeft)));
670       scaleSize.height = int32_t(std::max(Distance(topRight, bottomRight),
671                                           Distance(topLeft, bottomLeft)));
672 
673       if (unsigned(scaleSize.width) > aRT->GetMaximumBitmapSize()) {
674         // Ok, in this case we'd really want a downscale of a part of the bitmap,
675         // perhaps we can do this later but for simplicity let's do something
676         // different here and assume it's good enough, this should be rare!
677         scaleSize.width = 4095;
678       }
679       if (unsigned(scaleSize.height) > aRT->GetMaximumBitmapSize()) {
680         scaleSize.height = 4095;
681       }
682 
683       scaler.ScaleForSize(scaleSize);
684 
685       IntSize newSize = scaler.GetSize();
686 
687       if (newSize.IsEmpty()) {
688         return nullptr;
689       }
690 
691       aRT->CreateBitmap(D2D1::SizeU(newSize.width, newSize.height),
692                         scaler.GetScaledData(), scaler.GetStride(),
693                         D2D1::BitmapProperties(D2DPixelFormat(aSurface->GetFormat())),
694                         getter_AddRefs(bitmap));
695 
696       aSourceTransform.PreScale(Float(size.width) / newSize.width,
697                                 Float(size.height) / newSize.height);
698     }
699     return bitmap.forget();
700   }
701 }
702 
AddRectToSink(ID2D1GeometrySink * aSink,const D2D1_RECT_F & aRect)703 static inline void AddRectToSink(ID2D1GeometrySink* aSink, const D2D1_RECT_F& aRect)
704 {
705   aSink->BeginFigure(D2D1::Point2F(aRect.left, aRect.top), D2D1_FIGURE_BEGIN_FILLED);
706   aSink->AddLine(D2D1::Point2F(aRect.right, aRect.top));
707   aSink->AddLine(D2D1::Point2F(aRect.right, aRect.bottom));
708   aSink->AddLine(D2D1::Point2F(aRect.left, aRect.bottom));
709   aSink->EndFigure(D2D1_FIGURE_END_CLOSED);
710 }
711 
712 class DCCommandSink : public ID2D1CommandSink
713 {
714 public:
DCCommandSink(ID2D1DeviceContext * aCtx)715   DCCommandSink(ID2D1DeviceContext* aCtx) : mCtx(aCtx)
716   {
717   }
718 
QueryInterface(const IID & aIID,void ** aPtr)719   HRESULT STDMETHODCALLTYPE QueryInterface(const IID &aIID, void **aPtr)
720   {
721     if (!aPtr) {
722       return E_POINTER;
723     }
724 
725     if (aIID == IID_IUnknown) {
726       *aPtr = static_cast<IUnknown*>(this);
727       return S_OK;
728     } else if (aIID == IID_ID2D1CommandSink) {
729       *aPtr = static_cast<ID2D1CommandSink*>(this);
730       return S_OK;
731     }
732 
733     return E_NOINTERFACE;
734   }
735 
AddRef()736   ULONG STDMETHODCALLTYPE AddRef()
737   {
738     return 1;
739   }
740 
Release()741   ULONG STDMETHODCALLTYPE Release()
742   {
743     return 1;
744   }
745 
BeginDraw()746   STDMETHODIMP BeginDraw()
747   {
748     // We don't want to do anything here!
749     return S_OK;
750   }
EndDraw()751   STDMETHODIMP EndDraw()
752   {
753     // We don't want to do anything here!
754     return S_OK;
755   }
756 
SetAntialiasMode(D2D1_ANTIALIAS_MODE antialiasMode)757   STDMETHODIMP SetAntialiasMode(
758     D2D1_ANTIALIAS_MODE antialiasMode
759     )
760   {
761     mCtx->SetAntialiasMode(antialiasMode);
762     return S_OK;
763   }
764 
SetTags(D2D1_TAG tag1,D2D1_TAG tag2)765   STDMETHODIMP SetTags(D2D1_TAG tag1, D2D1_TAG tag2)
766   {
767     mCtx->SetTags(tag1, tag2);
768     return S_OK;
769   }
770 
SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode)771   STDMETHODIMP SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode)
772   {
773     mCtx->SetTextAntialiasMode(textAntialiasMode);
774     return S_OK;
775   }
776 
SetTextRenderingParams(_In_opt_ IDWriteRenderingParams * textRenderingParams)777   STDMETHODIMP SetTextRenderingParams(_In_opt_ IDWriteRenderingParams *textRenderingParams)
778   {
779     mCtx->SetTextRenderingParams(textRenderingParams);
780     return S_OK;
781   }
782 
SetTransform(_In_ CONST D2D1_MATRIX_3X2_F * transform)783   STDMETHODIMP  SetTransform(_In_ CONST D2D1_MATRIX_3X2_F *transform)
784   {
785     mCtx->SetTransform(transform);
786     return S_OK;
787   }
788 
SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND primitiveBlend)789   STDMETHODIMP SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND primitiveBlend)
790   {
791     mCtx->SetPrimitiveBlend(primitiveBlend);
792     return S_OK;
793   }
794 
SetUnitMode(D2D1_UNIT_MODE unitMode)795   STDMETHODIMP SetUnitMode(D2D1_UNIT_MODE unitMode)
796   {
797     mCtx->SetUnitMode(unitMode);
798     return S_OK;
799   }
800 
Clear(_In_opt_ CONST D2D1_COLOR_F * color)801   STDMETHODIMP Clear(_In_opt_ CONST D2D1_COLOR_F *color)
802   {
803     mCtx->Clear(color);
804     return S_OK;
805   }
806 
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)807   STDMETHODIMP DrawGlyphRun(
808       D2D1_POINT_2F baselineOrigin,
809       _In_ CONST DWRITE_GLYPH_RUN *glyphRun,
810       _In_opt_ CONST DWRITE_GLYPH_RUN_DESCRIPTION *glyphRunDescription,
811       _In_ ID2D1Brush *foregroundBrush,
812       DWRITE_MEASURING_MODE measuringMode
813     )
814   {
815     mCtx->DrawGlyphRun(baselineOrigin, glyphRun, glyphRunDescription,
816                        foregroundBrush, measuringMode);
817     return S_OK;
818   }
819 
DrawLine(D2D1_POINT_2F point0,D2D1_POINT_2F point1,_In_ ID2D1Brush * brush,FLOAT strokeWidth,_In_opt_ ID2D1StrokeStyle * strokeStyle)820   STDMETHODIMP DrawLine(
821       D2D1_POINT_2F point0,
822       D2D1_POINT_2F point1,
823       _In_ ID2D1Brush *brush,
824       FLOAT strokeWidth,
825       _In_opt_ ID2D1StrokeStyle *strokeStyle
826     )
827   {
828     mCtx->DrawLine(point0, point1, brush, strokeWidth, strokeStyle);
829     return S_OK;
830   }
831 
DrawGeometry(_In_ ID2D1Geometry * geometry,_In_ ID2D1Brush * brush,FLOAT strokeWidth,_In_opt_ ID2D1StrokeStyle * strokeStyle)832   STDMETHODIMP DrawGeometry(
833       _In_ ID2D1Geometry *geometry,
834       _In_ ID2D1Brush *brush,
835       FLOAT strokeWidth,
836       _In_opt_ ID2D1StrokeStyle *strokeStyle
837     )
838   {
839     mCtx->DrawGeometry(geometry, brush, strokeWidth, strokeStyle);
840     return S_OK;
841   }
842 
DrawRectangle(_In_ CONST D2D1_RECT_F * rect,_In_ ID2D1Brush * brush,FLOAT strokeWidth,_In_opt_ ID2D1StrokeStyle * strokeStyle)843   STDMETHODIMP DrawRectangle(
844       _In_ CONST D2D1_RECT_F *rect,
845       _In_ ID2D1Brush *brush,
846       FLOAT strokeWidth,
847       _In_opt_ ID2D1StrokeStyle *strokeStyle
848     )
849   {
850     mCtx->DrawRectangle(rect, brush, strokeWidth, strokeStyle);
851     return S_OK;
852   }
853 
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)854   STDMETHODIMP DrawBitmap(
855       _In_ ID2D1Bitmap *bitmap,
856       _In_opt_ CONST D2D1_RECT_F *destinationRectangle,
857       FLOAT opacity,
858       D2D1_INTERPOLATION_MODE interpolationMode,
859       _In_opt_ CONST D2D1_RECT_F *sourceRectangle,
860       _In_opt_ CONST D2D1_MATRIX_4X4_F *perspectiveTransform
861     )
862   {
863     mCtx->DrawBitmap(bitmap, destinationRectangle, opacity,
864                      interpolationMode, sourceRectangle,
865                      perspectiveTransform);
866     return S_OK;
867   }
868 
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)869   STDMETHODIMP DrawImage(
870       _In_ ID2D1Image *image,
871       _In_opt_ CONST D2D1_POINT_2F *targetOffset,
872       _In_opt_ CONST D2D1_RECT_F *imageRectangle,
873       D2D1_INTERPOLATION_MODE interpolationMode,
874       D2D1_COMPOSITE_MODE compositeMode
875     )
876   {
877     mCtx->DrawImage(image, targetOffset, imageRectangle,
878                     interpolationMode, compositeMode);
879     return S_OK;
880   }
881 
DrawGdiMetafile(_In_ ID2D1GdiMetafile * gdiMetafile,_In_opt_ CONST D2D1_POINT_2F * targetOffset)882   STDMETHODIMP DrawGdiMetafile(
883       _In_ ID2D1GdiMetafile *gdiMetafile,
884       _In_opt_ CONST D2D1_POINT_2F *targetOffset
885     )
886   {
887     mCtx->DrawGdiMetafile(gdiMetafile, targetOffset);
888     return S_OK;
889   }
890 
FillMesh(_In_ ID2D1Mesh * mesh,_In_ ID2D1Brush * brush)891   STDMETHODIMP FillMesh(
892       _In_ ID2D1Mesh *mesh,
893       _In_ ID2D1Brush *brush
894     )
895   {
896     mCtx->FillMesh(mesh, brush);
897     return S_OK;
898   }
899 
FillOpacityMask(_In_ ID2D1Bitmap * opacityMask,_In_ ID2D1Brush * brush,_In_opt_ CONST D2D1_RECT_F * destinationRectangle,_In_opt_ CONST D2D1_RECT_F * sourceRectangle)900   STDMETHODIMP FillOpacityMask(
901       _In_ ID2D1Bitmap *opacityMask,
902       _In_ ID2D1Brush *brush,
903       _In_opt_ CONST D2D1_RECT_F *destinationRectangle,
904       _In_opt_ CONST D2D1_RECT_F *sourceRectangle
905     )
906   {
907     mCtx->FillOpacityMask(opacityMask, brush, destinationRectangle,
908                           sourceRectangle);
909     return S_OK;
910   }
911 
FillGeometry(_In_ ID2D1Geometry * geometry,_In_ ID2D1Brush * brush,_In_opt_ ID2D1Brush * opacityBrush)912   STDMETHODIMP FillGeometry(
913       _In_ ID2D1Geometry *geometry,
914       _In_ ID2D1Brush *brush,
915       _In_opt_ ID2D1Brush *opacityBrush
916     )
917   {
918     mCtx->FillGeometry(geometry, brush, opacityBrush);
919     return S_OK;
920   }
921 
FillRectangle(_In_ CONST D2D1_RECT_F * rect,_In_ ID2D1Brush * brush)922   STDMETHODIMP FillRectangle(
923       _In_ CONST D2D1_RECT_F *rect,
924       _In_ ID2D1Brush *brush
925     )
926   {
927     mCtx->FillRectangle(rect, brush);
928     return S_OK;
929   }
930 
PushAxisAlignedClip(_In_ CONST D2D1_RECT_F * clipRect,D2D1_ANTIALIAS_MODE antialiasMode)931   STDMETHODIMP PushAxisAlignedClip(
932       _In_ CONST D2D1_RECT_F *clipRect,
933       D2D1_ANTIALIAS_MODE antialiasMode
934     )
935   {
936     mCtx->PushAxisAlignedClip(clipRect, antialiasMode);
937     return S_OK;
938   }
939 
PushLayer(_In_ CONST D2D1_LAYER_PARAMETERS1 * layerParameters1,_In_opt_ ID2D1Layer * layer)940   STDMETHODIMP PushLayer(
941       _In_ CONST D2D1_LAYER_PARAMETERS1 *layerParameters1,
942       _In_opt_ ID2D1Layer *layer
943     )
944   {
945     mCtx->PushLayer(layerParameters1, layer);
946     return S_OK;
947   }
948 
PopAxisAlignedClip()949   STDMETHODIMP PopAxisAlignedClip()
950   {
951     mCtx->PopAxisAlignedClip();
952     return S_OK;
953   }
954 
PopLayer()955   STDMETHODIMP PopLayer()
956   {
957     mCtx->PopLayer();
958     return S_OK;
959   }
960 
961   ID2D1DeviceContext* mCtx;
962 };
963 
964 }
965 }
966 
967 #endif /* MOZILLA_GFX_HELPERSD2D_H_ */
968