Bug 662038, part 3: Implement dashing for d2d. r=Bas
authorChris Jones <jones.chris.g@gmail.com>
Wed, 29 Jun 2011 14:34:58 -0700
changeset 72009 b0effc7c7f7ff1e1009208bee597602ff84ffa2c
parent 72008 0f2460453266cbc554ac10fb7f64211d696ecf8f
child 72010 ddce22d83b0d85438140b7fc978f5724f3a70722
push id317
push usercjones@mozilla.com
push dateWed, 29 Jun 2011 21:35:14 +0000
treeherdermozilla-inbound@1382428eb6ce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBas
bugs662038
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 662038, part 3: Implement dashing for d2d. r=Bas
gfx/2d/DrawTargetD2D.cpp
--- a/gfx/2d/DrawTargetD2D.cpp
+++ b/gfx/2d/DrawTargetD2D.cpp
@@ -60,16 +60,18 @@ typedef HRESULT (WINAPI*D3D10CreateEffec
   __in   void *pData,
   __in   SIZE_T DataLength,
   __in   UINT FXFlags,
   __in   ID3D10Device *pDevice,
   __in   ID3D10EffectPool *pEffectPool,
   __out  ID3D10Effect **ppEffect
 );
 
+using namespace std;
+
 namespace mozilla {
 namespace gfx {
 
 struct Vertex {
   float x;
   float y;
 };
 
@@ -660,18 +662,20 @@ DrawTargetD2D::ClearRect(const Rect &aRe
   return;
 }
 
 void
 DrawTargetD2D::CopySurface(SourceSurface *aSurface,
                            const IntRect &aSourceRect,
                            const IntPoint &aDestination)
 {
-  Rect srcRect(aSourceRect.x, aSourceRect.y, aSourceRect.width, aSourceRect.height);
-  Rect dstRect(aDestination.x, aDestination.y, aSourceRect.width, aSourceRect.height);
+  Rect srcRect(Float(aSourceRect.x), Float(aSourceRect.y),
+               Float(aSourceRect.width), Float(aSourceRect.height));
+  Rect dstRect(Float(aDestination.x), Float(aDestination.y),
+               Float(aSourceRect.width), Float(aSourceRect.height));
 
   mRT->SetTransform(D2D1::IdentityMatrix());
   mRT->PushAxisAlignedClip(D2DRect(dstRect), D2D1_ANTIALIAS_MODE_ALIASED);
   mRT->Clear(D2D1::ColorF(0, 0.0f));
   mRT->PopAxisAlignedClip();
 
   RefPtr<ID2D1Bitmap> bitmap;
 
@@ -1627,20 +1631,46 @@ DrawTargetD2D::CreateStrokeStyleForOptio
     joinStyle = D2D1_LINE_JOIN_ROUND;
     break;
   case JOIN_BEVEL:
     joinStyle = D2D1_LINE_JOIN_BEVEL;
     break;
   }
 
 
-  HRESULT hr = factory()->CreateStrokeStyle(D2D1::StrokeStyleProperties(capStyle, capStyle,
-                                                                        capStyle, joinStyle,
-                                                                        aStrokeOptions.mMiterLimit),
-                                            NULL, 0, byRef(style));
+  HRESULT hr;
+  if (aStrokeOptions.mDashPattern) {
+    typedef 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 = factory()->CreateStrokeStyle(
+      D2D1::StrokeStyleProperties(capStyle, capStyle,
+                                  capStyle, joinStyle,
+                                  aStrokeOptions.mMiterLimit,
+                                  D2D1_DASH_STYLE_CUSTOM,
+                                  aStrokeOptions.mDashOffset),
+      &dash[0], // data() is not C++98, although it's in recent gcc
+                // and VC10's STL
+      dash.size(),
+      byRef(style));
+  } else {
+    hr = factory()->CreateStrokeStyle(
+      D2D1::StrokeStyleProperties(capStyle, capStyle,
+                                  capStyle, joinStyle,
+                                  aStrokeOptions.mMiterLimit),
+      NULL, 0, byRef(style));
+  }
 
   if (FAILED(hr)) {
     gfxWarning() << "Failed to create Direct2D stroke style.";
   }
 
   return style;
 }