Bug 1251431 - Part 1: Allow usage of an A8 source pattern to MaskSurface for D2D 1.1 Moz2D backend. r=jwatt
MozReview-Commit-ID: 3SOuVvklajf
--- a/gfx/2d/DrawTargetD2D1.cpp
+++ b/gfx/2d/DrawTargetD2D1.cpp
@@ -1625,16 +1625,39 @@ DrawTargetD2D1::CreateBrushForPattern(co
D2D1_RECT_F samplingBounds;
Matrix mat = pat->mMatrix;
MOZ_ASSERT(pat->mSurface->IsValid());
RefPtr<ID2D1Image> image = GetImageForSurface(pat->mSurface, mat, pat->mExtendMode, !pat->mSamplingRect.IsEmpty() ? &pat->mSamplingRect : nullptr);
+ if (pat->mSurface->GetFormat() == SurfaceFormat::A8) {
+ // See bug 1251431, at least FillOpacityMask does not appear to allow a source bitmapbrush
+ // with source format A8. This creates a BGRA surface with the same alpha values that
+ // the A8 surface has.
+ RefPtr<ID2D1Bitmap> bitmap;
+ image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
+ if (bitmap) {
+ RefPtr<ID2D1Image> oldTarget;
+ RefPtr<ID2D1Bitmap1> tmpBitmap;
+ mDC->CreateBitmap(D2D1::SizeU(pat->mSurface->GetSize().width, pat->mSurface->GetSize().height), nullptr, 0,
+ &D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET, D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)),
+ getter_AddRefs(tmpBitmap));
+ mDC->GetTarget(getter_AddRefs(oldTarget));
+ mDC->SetTarget(tmpBitmap);
+
+ RefPtr<ID2D1SolidColorBrush> brush;
+ mDC->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), getter_AddRefs(brush));
+ mDC->FillOpacityMask(bitmap, brush);
+ mDC->SetTarget(oldTarget);
+ image = tmpBitmap;
+ }
+ }
+
if (!image) {
return CreateTransparentBlackBrush();
}
if (pat->mSamplingRect.IsEmpty()) {
RefPtr<ID2D1Bitmap> bitmap;
image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
if (bitmap) {