Modified: trunk/Source/WebCore/platform/graphics/win/GraphicsContextDirect2D.cpp (207139 => 207140)
--- trunk/Source/WebCore/platform/graphics/win/GraphicsContextDirect2D.cpp 2016-10-11 15:53:58 UTC (rev 207139)
+++ trunk/Source/WebCore/platform/graphics/win/GraphicsContextDirect2D.cpp 2016-10-11 15:57:39 UTC (rev 207140)
@@ -415,16 +415,26 @@
return D2D1::ColorF(color.rgb(), globalAlpha * colorAlpha);
}
-ID2D1SolidColorBrush* GraphicsContext::solidStrokeBrush()
+ID2D1Brush* GraphicsContext::solidStrokeBrush() const
{
return m_data->m_solidStrokeBrush.get();
}
-ID2D1SolidColorBrush* GraphicsContext::solidFillBrush()
+ID2D1Brush* GraphicsContext::solidFillBrush() const
{
return m_data->m_solidFillBrush.get();
}
+ID2D1Brush* GraphicsContext::patternStrokeBrush() const
+{
+ return m_data->m_patternStrokeBrush.get();
+}
+
+ID2D1Brush* GraphicsContext::patternFillBrush() const
+{
+ return m_data->m_patternFillBrush.get();
+}
+
void GraphicsContext::drawPattern(Image& image, const FloatRect& destRect, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, BlendMode blendMode)
{
if (paintingDisabled() || !patternTransform.isInvertible())
@@ -435,7 +445,51 @@
return;
}
- notImplemented();
+ auto context = platformContext();
+ D2DContextStateSaver stateSaver(*m_data);
+
+ m_data->clip(destRect);
+
+ setPlatformCompositeOperation(op, blendMode);
+
+ auto bitmapBrushProperties = D2D1::BitmapBrushProperties();
+ bitmapBrushProperties.extendModeX = D2D1_EXTEND_MODE_WRAP;
+ bitmapBrushProperties.extendModeY = D2D1_EXTEND_MODE_WRAP;
+
+ // Create a brush transformation so we paint using the section of the image we care about.
+ AffineTransform transformation = patternTransform;
+ transformation.translate(destRect.location());
+
+ auto brushProperties = D2D1::BrushProperties();
+ brushProperties.transform = transformation;
+ brushProperties.opacity = 1.0f;
+
+ auto tileImage = image.nativeImageForCurrentFrame();
+
+ // If we only want a subset of the bitmap, we need to create a cropped bitmap image. According to the documentation,
+ // this does not allocate new bitmap memory.
+ if (image.width() > destRect.width() || image.height() > destRect.height()) {
+ float dpiX = 0;
+ float dpiY = 0;
+ tileImage->GetDpi(&dpiX, &dpiY);
+ auto bitmapProperties = D2D1::BitmapProperties(tileImage->GetPixelFormat(), dpiX, dpiY);
+ COMPtr<ID2D1Bitmap> subImage;
+ HRESULT hr = context->CreateBitmap(IntSize(tileRect.size()), bitmapProperties, &subImage);
+ if (SUCCEEDED(hr)) {
+ D2D1_RECT_U finishRect = IntRect(tileRect);
+ hr = subImage->CopyFromBitmap(nullptr, tileImage.get(), &finishRect);
+ if (SUCCEEDED(hr))
+ tileImage = subImage;
+ }
+ }
+
+ COMPtr<ID2D1BitmapBrush> patternBrush;
+ HRESULT hr = context->CreateBitmapBrush(tileImage.get(), &bitmapBrushProperties, &brushProperties, &patternBrush);
+
+ drawWithoutShadow(destRect, [this, destRect, patternBrush](ID2D1RenderTarget* renderTarget) {
+ const D2D1_RECT_F d2dRect = destRect;
+ renderTarget->FillRectangle(&d2dRect, patternBrush.get());
+ });
}
void GraphicsContext::clipToImageBuffer(ImageBuffer& buffer, const FloatRect& destRect)
@@ -700,8 +754,11 @@
if (paintingDisabled())
return;
- // Note: Because of the way Direct2D draws, we may not need this explicit context 'set stroke pattern' logic.
- notImplemented();
+ auto context = platformContext();
+ AffineTransform userToBaseCTM; // FIXME: This isn't really needed on Windows
+
+ const float patternAlpha = 1;
+ m_data->m_patternStrokeBrush = adoptCOM(m_state.strokePattern->createPlatformPattern(context, patternAlpha, userToBaseCTM));
}
void GraphicsContext::applyFillPattern()
@@ -709,8 +766,11 @@
if (paintingDisabled())
return;
- // Note: Because of the way Direct2D draws, we may not need this explicit context 'set fill pattern' logic.
- notImplemented();
+ auto context = platformContext();
+ AffineTransform userToBaseCTM; // FIXME: This isn't really needed on Windows
+
+ const float patternAlpha = 1;
+ m_data->m_patternFillBrush = adoptCOM(m_state.fillPattern->createPlatformPattern(context, patternAlpha, userToBaseCTM));
}
void GraphicsContext::drawPath(const Path& path)
@@ -747,7 +807,8 @@
auto rect = path.fastBoundingRect();
drawWithoutShadow(rect, [this, &path](ID2D1RenderTarget* renderTarget) {
- renderTarget->DrawGeometry(path.platformPath(), solidStrokeBrush(), strokeThickness(), m_data->strokeStyle());
+ auto brush = m_state.strokePattern ? patternStrokeBrush() : solidStrokeBrush();
+ renderTarget->DrawGeometry(path.platformPath(), brush, strokeThickness(), m_data->strokeStyle());
});
}
@@ -870,7 +931,8 @@
FloatRect contextRect(FloatPoint(), context->GetSize());
drawWithoutShadow(contextRect, [this, &pathToFill](ID2D1RenderTarget* renderTarget) {
- renderTarget->FillGeometry(pathToFill.get(), solidFillBrush());
+ auto brush = m_state.fillPattern ? patternFillBrush() : solidFillBrush();
+ renderTarget->FillGeometry(pathToFill.get(), brush);
});
}
@@ -912,7 +974,8 @@
FloatRect contextRect(FloatPoint(), context->GetSize());
drawWithoutShadow(contextRect, [this, &path](ID2D1RenderTarget* renderTarget) {
- renderTarget->DrawGeometry(path.platformPath(), solidStrokeBrush(), strokeThickness(), m_data->strokeStyle());
+ auto brush = m_state.strokePattern ? patternStrokeBrush() : solidStrokeBrush();
+ renderTarget->DrawGeometry(path.platformPath(), brush, strokeThickness(), m_data->strokeStyle());
});
}
@@ -958,7 +1021,8 @@
drawWithoutShadow(rect, [this, rect](ID2D1RenderTarget* renderTarget) {
const D2D1_RECT_F d2dRect = rect;
- renderTarget->FillRectangle(&d2dRect, solidFillBrush());
+ auto brush = m_state.fillPattern ? patternFillBrush() : solidFillBrush();
+ renderTarget->FillRectangle(&d2dRect, brush);
});
}
Added: trunk/Source/WebCore/platform/graphics/win/PatternDirect2D.cpp (0 => 207140)
--- trunk/Source/WebCore/platform/graphics/win/PatternDirect2D.cpp (rev 0)
+++ trunk/Source/WebCore/platform/graphics/win/PatternDirect2D.cpp 2016-10-11 15:57:39 UTC (rev 207140)
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "Pattern.h"
+
+#if USE(DIRECT2D)
+
+#include "AffineTransform.h"
+#include "GraphicsContext.h"
+#include <CoreGraphics/CoreGraphics.h>
+#include <d2d1.h>
+#include <wtf/MainThread.h>
+
+
+namespace WebCore {
+
+ID2D1BitmapBrush* Pattern::createPlatformPattern(ID2D1RenderTarget* renderTarget, float alpha, const AffineTransform& userSpaceTransformation) const
+{
+ RELEASE_ASSERT(renderTarget);
+
+ FloatRect tileRect = tileImage()->rect();
+
+ AffineTransform patternTransform = userSpaceTransformation * m_patternSpaceTransformation;
+ auto bitmapBrushProperties = D2D1::BitmapBrushProperties();
+ bitmapBrushProperties.extendModeX = m_repeatX ? D2D1_EXTEND_MODE_WRAP : D2D1_EXTEND_MODE_CLAMP;
+ bitmapBrushProperties.extendModeY = m_repeatY ? D2D1_EXTEND_MODE_WRAP : D2D1_EXTEND_MODE_CLAMP;
+
+ auto brushProperties = D2D1::BrushProperties();
+ brushProperties.transform = patternTransform;
+ brushProperties.opacity = alpha;
+
+ ID2D1BitmapBrush* patternBrush = nullptr;
+ HRESULT hr = renderTarget->CreateBitmapBrush(tileImage()->nativeImage().get(), &bitmapBrushProperties, &brushProperties, &patternBrush);
+ ASSERT(hr);
+ return patternBrush;
+}
+
+}
+
+#endif