Title: [207140] trunk/Source/WebCore
Revision
207140
Author
[email protected]
Date
2016-10-11 08:57:39 -0700 (Tue, 11 Oct 2016)

Log Message

[Win][Direct2D] Add initial Pattern handling implementation
https://bugs.webkit.org/show_bug.cgi?id=163270

Reviewed by Simon Fraser.

Provide an implemenation of Patterns for Direct2D.

No new tests. Covered by fast/borders/border-image-01.html and others.

* platform/graphics/GraphicsContext.h:
* platform/graphics/Pattern.h: Add D2D definitions.
* platform/graphics/win/GraphicsContextDirect2D.cpp:
(WebCore::GraphicsContext::solidStrokeBrush): Use a more generic return type.
(WebCore::GraphicsContext::solidFillBrush): Ditto.
(WebCore::GraphicsContext::patternStrokeBrush): Added.
(WebCore::GraphicsContext::patternFillBrush): Added.
(WebCore::GraphicsContext::drawPattern): Provide D2D implementation.
(WebCore::GraphicsContext::applyStrokePattern): Ditto.
(WebCore::GraphicsContext::applyFillPattern): Ditto.
(WebCore::GraphicsContext::drawPath): Use stroke pattern if available.
(WebCore::GraphicsContext::fillPath): Use fill pattern if available.
(WebCore::GraphicsContext::strokePath): Use stroke pattern if available.
(WebCore::GraphicsContext::fillRect): Use fill pattern if available.
* platform/graphics/win/GraphicsContextPlatformPrivateDirect2D.h:
* platform/graphics/win/PatternDirect2D.cpp: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (207139 => 207140)


--- trunk/Source/WebCore/ChangeLog	2016-10-11 15:53:58 UTC (rev 207139)
+++ trunk/Source/WebCore/ChangeLog	2016-10-11 15:57:39 UTC (rev 207140)
@@ -1,3 +1,31 @@
+2016-10-10  Brent Fulgham  <[email protected]>
+
+        [Win][Direct2D] Add initial Pattern handling implementation
+        https://bugs.webkit.org/show_bug.cgi?id=163270
+
+        Reviewed by Simon Fraser.
+
+        Provide an implemenation of Patterns for Direct2D.
+
+        No new tests. Covered by fast/borders/border-image-01.html and others.
+
+        * platform/graphics/GraphicsContext.h:
+        * platform/graphics/Pattern.h: Add D2D definitions.
+        * platform/graphics/win/GraphicsContextDirect2D.cpp:
+        (WebCore::GraphicsContext::solidStrokeBrush): Use a more generic return type.
+        (WebCore::GraphicsContext::solidFillBrush): Ditto.
+        (WebCore::GraphicsContext::patternStrokeBrush): Added.
+        (WebCore::GraphicsContext::patternFillBrush): Added.
+        (WebCore::GraphicsContext::drawPattern): Provide D2D implementation.
+        (WebCore::GraphicsContext::applyStrokePattern): Ditto.
+        (WebCore::GraphicsContext::applyFillPattern): Ditto.
+        (WebCore::GraphicsContext::drawPath): Use stroke pattern if available.
+        (WebCore::GraphicsContext::fillPath): Use fill pattern if available.
+        (WebCore::GraphicsContext::strokePath): Use stroke pattern if available.
+        (WebCore::GraphicsContext::fillRect): Use fill pattern if available.
+        * platform/graphics/win/GraphicsContextPlatformPrivateDirect2D.h:
+        * platform/graphics/win/PatternDirect2D.cpp: Added.
+
 2016-10-11  Per Arne Vollan  <[email protected]>
 
         [Win] Compile fix.

Modified: trunk/Source/WebCore/platform/graphics/GraphicsContext.h (207139 => 207140)


--- trunk/Source/WebCore/platform/graphics/GraphicsContext.h	2016-10-11 15:53:58 UTC (rev 207139)
+++ trunk/Source/WebCore/platform/graphics/GraphicsContext.h	2016-10-11 15:57:39 UTC (rev 207140)
@@ -555,8 +555,10 @@
     WEBCORE_EXPORT bool didBeginDraw() const;
     D2D1_COLOR_F colorWithGlobalAlpha(const Color&) const;
 
-    ID2D1SolidColorBrush* solidStrokeBrush();
-    ID2D1SolidColorBrush* solidFillBrush();
+    ID2D1Brush* solidStrokeBrush() const;
+    ID2D1Brush* solidFillBrush() const;
+    ID2D1Brush* patternStrokeBrush() const;
+    ID2D1Brush* patternFillBrush() const;
 #endif
 #else // PLATFORM(WIN)
     bool shouldIncludeChildWindows() const { return false; }

Modified: trunk/Source/WebCore/platform/graphics/Pattern.h (207139 => 207140)


--- trunk/Source/WebCore/platform/graphics/Pattern.h	2016-10-11 15:53:58 UTC (rev 207139)
+++ trunk/Source/WebCore/platform/graphics/Pattern.h	2016-10-11 15:57:39 UTC (rev 207140)
@@ -38,7 +38,9 @@
 typedef struct CGPattern* CGPatternRef;
 typedef CGPatternRef PlatformPatternPtr;
 #elif USE(DIRECT2D)
-typedef void* PlatformPatternPtr;
+interface ID2D1BitmapBrush;
+interface ID2D1RenderTarget;
+typedef ID2D1BitmapBrush* PlatformPatternPtr;
 #elif USE(CAIRO)
 #include <cairo.h>
 typedef cairo_pattern_t* PlatformPatternPtr;
@@ -60,8 +62,12 @@
 
     void platformDestroy();
 
-    // Pattern space is an abstract space that maps to the default user space by the transformation 'userSpaceTransformation' 
+    // Pattern space is an abstract space that maps to the default user space by the transformation 'userSpaceTransformation'
+#if !USE(DIRECT2D)
     PlatformPatternPtr createPlatformPattern(const AffineTransform& userSpaceTransformation) const;
+#else
+    PlatformPatternPtr createPlatformPattern(ID2D1RenderTarget*, float alpha, const AffineTransform& userSpaceTransformation) const;
+#endif
     void setPatternSpaceTransform(const AffineTransform& patternSpaceTransformation);
     const AffineTransform& getPatternSpaceTransform() { return m_patternSpaceTransformation; };
     void setPlatformPatternSpaceTransform();

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);
     });
 }
 

Modified: trunk/Source/WebCore/platform/graphics/win/GraphicsContextPlatformPrivateDirect2D.h (207139 => 207140)


--- trunk/Source/WebCore/platform/graphics/win/GraphicsContextPlatformPrivateDirect2D.h	2016-10-11 15:53:58 UTC (rev 207139)
+++ trunk/Source/WebCore/platform/graphics/win/GraphicsContextPlatformPrivateDirect2D.h	2016-10-11 15:57:39 UTC (rev 207140)
@@ -84,6 +84,8 @@
 
     COMPtr<ID2D1SolidColorBrush> m_solidStrokeBrush;
     COMPtr<ID2D1SolidColorBrush> m_solidFillBrush;
+    COMPtr<ID2D1BitmapBrush> m_patternStrokeBrush;
+    COMPtr<ID2D1BitmapBrush> m_patternFillBrush;
 
 private:
     void recomputeStrokeStyle();

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
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to