<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[206815] trunk/Source/WebCore</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/206815">206815</a></dd>
<dt>Author</dt> <dd>bfulgham@apple.com</dd>
<dt>Date</dt> <dd>2016-10-05 10:18:31 -0700 (Wed, 05 Oct 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>[Win][Direct2D] Add initial D2D Path and Gradient implementation
https://bugs.webkit.org/show_bug.cgi?id=162923
Reviewed by Alex Christensen.
This patch lands a set of new files that implements initial Path
and Gradient features on Windows using Direct2D.
No new tests until complete backend lands.
* platform/graphics/Gradient.h:
* platform/graphics/Path.cpp:
* platform/graphics/Path.h:
* platform/graphics/win/GradientDirect2D.cpp: Added.
* platform/graphics/win/PathDirect2D.cpp: Added.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsGradienth">trunk/Source/WebCore/platform/graphics/Gradient.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsPathcpp">trunk/Source/WebCore/platform/graphics/Path.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsPathh">trunk/Source/WebCore/platform/graphics/Path.h</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreplatformgraphicswinGradientDirect2Dcpp">trunk/Source/WebCore/platform/graphics/win/GradientDirect2D.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicswinPathDirect2Dcpp">trunk/Source/WebCore/platform/graphics/win/PathDirect2D.cpp</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (206814 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-10-05 16:46:19 UTC (rev 206814)
+++ trunk/Source/WebCore/ChangeLog        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2016-10-05 Brent Fulgham <bfulgham@apple.com>
+
+ [Win][Direct2D] Add initial D2D Path and Gradient implementation
+ https://bugs.webkit.org/show_bug.cgi?id=162923
+
+ Reviewed by Alex Christensen.
+
+ This patch lands a set of new files that implements initial Path
+ and Gradient features on Windows using Direct2D.
+
+ No new tests until complete backend lands.
+
+ * platform/graphics/Gradient.h:
+ * platform/graphics/Path.cpp:
+ * platform/graphics/Path.h:
+ * platform/graphics/win/GradientDirect2D.cpp: Added.
+ * platform/graphics/win/PathDirect2D.cpp: Added.
+
</ins><span class="cx"> 2016-10-05 Romain Bellessort <romain.bellessort@crf.canon.fr>
</span><span class="cx">
</span><span class="cx"> [Readable Streams API] Align function names with spec
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsGradienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/Gradient.h (206814 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/Gradient.h        2016-10-05 16:46:19 UTC (rev 206814)
+++ trunk/Source/WebCore/platform/graphics/Gradient.h        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -41,6 +41,10 @@
</span><span class="cx"> typedef struct CGGradient* CGGradientRef;
</span><span class="cx"> typedef CGGradientRef PlatformGradient;
</span><span class="cx">
</span><ins>+#elif USE(DIRECT2D)
+interface ID2D1Brush;
+interface ID2D1RenderTarget;
+typedef ID2D1Brush* PlatformGradient;
</ins><span class="cx"> #elif USE(CAIRO)
</span><span class="cx"> typedef struct _cairo_pattern cairo_pattern_t;
</span><span class="cx"> typedef cairo_pattern_t* PlatformGradient;
</span><span class="lines">@@ -159,6 +163,8 @@
</span><span class="cx"> #if USE(CG)
</span><span class="cx"> void paint(CGContextRef);
</span><span class="cx"> void paint(GraphicsContext*);
</span><ins>+#elif USE(DIRECT2D)
+ PlatformGradient createPlatformGradientIfNecessary(ID2D1RenderTarget*);
</ins><span class="cx"> #elif USE(CAIRO)
</span><span class="cx"> PlatformGradient platformGradient(float globalAlpha);
</span><span class="cx"> #endif
</span><span class="lines">@@ -167,11 +173,15 @@
</span><span class="cx"> WEBCORE_EXPORT Gradient(const FloatPoint& p0, const FloatPoint& p1);
</span><span class="cx"> Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
</span><span class="cx">
</span><del>- void platformInit() { m_gradient = 0; }
</del><ins>+ void platformInit() { m_gradient = nullptr; }
</ins><span class="cx"> void platformDestroy();
</span><span class="cx">
</span><span class="cx"> void sortStopsIfNecessary();
</span><span class="cx">
</span><ins>+#if USE(DIRECT2D)
+ void generateGradient(ID2D1RenderTarget*);
+#endif
+
</ins><span class="cx"> // Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
</span><span class="cx"> bool m_radial;
</span><span class="cx"> FloatPoint m_p0;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsPathcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/Path.cpp (206814 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/Path.cpp        2016-10-05 16:46:19 UTC (rev 206814)
+++ trunk/Source/WebCore/platform/graphics/Path.cpp        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -158,7 +158,7 @@
</span><span class="cx"> closeSubpath();
</span><span class="cx"> }
</span><span class="cx">
</span><del>-#if !USE(CG)
</del><ins>+#if !USE(CG) && !USE(DIRECT2D)
</ins><span class="cx"> Path Path::polygonPathFromPoints(const Vector<FloatPoint>& points)
</span><span class="cx"> {
</span><span class="cx"> Path path;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsPathh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/Path.h (206814 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/Path.h        2016-10-05 16:46:19 UTC (rev 206814)
+++ trunk/Source/WebCore/platform/graphics/Path.h        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -1,6 +1,6 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
- * 2006 Rob Buis <buis@kde.org>
</del><ins>+ * Copyright (C) 2003, 2006, 2009, 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2006 Rob Buis <buis@kde.org>
</ins><span class="cx"> * Copyright (C) 2007-2008 Torch Mobile, Inc.
</span><span class="cx"> *
</span><span class="cx"> * Redistribution and use in source and binary forms, with or without
</span><span class="lines">@@ -41,6 +41,16 @@
</span><span class="cx"> #include <CoreGraphics/CGPath.h>
</span><span class="cx"> typedef struct CGPath PlatformPath;
</span><span class="cx">
</span><ins>+#elif USE(DIRECT2D)
+#include "COMPtr.h"
+
+interface ID2D1Geometry;
+interface ID2D1GeometryGroup;
+interface ID2D1PathGeometry;
+interface ID2D1GeometrySink;
+
+typedef ID2D1GeometryGroup PlatformPath;
+
</ins><span class="cx"> #elif USE(CAIRO)
</span><span class="cx">
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -171,16 +181,29 @@
</span><span class="cx">
</span><span class="cx"> void addBeziersForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
</span><span class="cx">
</span><del>-#if USE(CG)
</del><ins>+#if USE(CG) || USE(DIRECT2D)
</ins><span class="cx"> void platformAddPathForRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
</span><span class="cx"> #endif
</span><span class="cx">
</span><ins>+#if USE(DIRECT2D)
+ ID2D1GeometrySink* activePath() const { return m_activePath.get(); }
+ void appendGeometry(ID2D1Geometry*);
+ void createGeometryWithFillMode(WindRule, COMPtr<ID2D1GeometryGroup>&) const;
+ void drawDidComplete() const;
+
+ HRESULT initializePathState();
+#endif
+
</ins><span class="cx"> #ifndef NDEBUG
</span><span class="cx"> void dump() const;
</span><span class="cx"> #endif
</span><span class="cx">
</span><span class="cx"> private:
</span><del>- PlatformPathPtr m_path;
</del><ins>+ PlatformPathPtr m_path { nullptr };
+#if USE(DIRECT2D)
+ COMPtr<ID2D1PathGeometry> m_activePathGeometry;
+ COMPtr<ID2D1GeometrySink> m_activePath;
+#endif
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> TextStream& operator<<(TextStream&, const Path&);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicswinGradientDirect2Dcpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/graphics/win/GradientDirect2D.cpp (0 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/win/GradientDirect2D.cpp         (rev 0)
+++ trunk/Source/WebCore/platform/graphics/win/GradientDirect2D.cpp        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -0,0 +1,133 @@
</span><ins>+/*
+ * 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 "Gradient.h"
+
+#include "FloatPoint.h"
+#include "GraphicsContext.h"
+#include <d2d1.h>
+#include <wtf/RetainPtr.h>
+
+#define GRADIENT_DRAWING 3
+
+namespace WebCore {
+
+void Gradient::platformDestroy()
+{
+ if (m_gradient)
+ m_gradient->Release();
+ m_gradient = nullptr;
+}
+
+ID2D1Brush* Gradient::platformGradient()
+{
+ ASSERT(m_gradient);
+ return m_gradient;
+}
+
+ID2D1Brush* Gradient::createPlatformGradientIfNecessary(ID2D1RenderTarget* context)
+{
+ generateGradient(context);
+ return m_gradient;
+}
+
+void Gradient::generateGradient(ID2D1RenderTarget* renderTarget)
+{
+ sortStopsIfNecessary();
+
+ Vector<D2D1_GRADIENT_STOP> gradientStops;
+ for (auto stop : m_stops)
+ gradientStops.append(D2D1::GradientStop(stop.stop, D2D1::ColorF(stop.red, stop.green, stop.blue, stop.alpha)));
+
+ COMPtr<ID2D1GradientStopCollection> gradientStopCollection;
+ HRESULT hr = renderTarget->CreateGradientStopCollection(gradientStops.data(), gradientStops.size(), &gradientStopCollection);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+
+ if (m_radial) {
+ ID2D1RadialGradientBrush* radialGradient = nullptr;
+ hr = renderTarget->CreateRadialGradientBrush(
+ D2D1::RadialGradientBrushProperties(p0(), p1(), startRadius(), endRadius()),
+ D2D1::BrushProperties(), gradientStopCollection.get(),
+ &radialGradient);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ m_gradient = radialGradient;
+ } else {
+ ID2D1LinearGradientBrush* linearGradient = nullptr;
+ hr = renderTarget->CreateLinearGradientBrush(
+ D2D1::LinearGradientBrushProperties(p0(), p1()),
+ D2D1::BrushProperties(), gradientStopCollection.get(),
+ &linearGradient);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ m_gradient = linearGradient;
+ }
+
+ hash();
+}
+
+void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
+{
+ auto d2dContext = context->platformContext();
+
+ bool needScaling = aspectRatio() != 1;
+ if (needScaling) {
+ context->save();
+ // Scale from the center of the gradient. We only ever scale non-deprecated gradients,
+ // for which m_p0 == m_p1.
+ ASSERT(m_p0 == m_p1);
+
+ D2D1_MATRIX_3X2_F ctm;
+ d2dContext->GetTransform(&ctm);
+
+ AffineTransform transform(ctm);
+ transform.translate(m_p0);
+ transform.scaleNonUniform(1.0, 1.0 / aspectRatio());
+ transform.translate(-m_p0);
+
+ d2dContext->SetTransform(ctm);
+ }
+
+ if (!m_cachedHash)
+ generateGradient(d2dContext);
+
+ if (!context->didBeginDraw())
+ d2dContext->BeginDraw();
+
+ d2dContext->SetTags(GRADIENT_DRAWING, __LINE__);
+
+ const D2D1_RECT_F d2dRect = rect;
+ d2dContext->FillRectangle(&d2dRect, m_gradient);
+
+ if (!context->didBeginDraw()) {
+ D2D1_TAG first, second;
+ HRESULT hr = d2dContext->EndDraw(&first, &second);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ }
+
+ if (needScaling)
+ context->restore();
+}
+
+}
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicswinPathDirect2Dcpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/platform/graphics/win/PathDirect2D.cpp (0 => 206815)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/win/PathDirect2D.cpp         (rev 0)
+++ trunk/Source/WebCore/platform/graphics/win/PathDirect2D.cpp        2016-10-05 17:18:31 UTC (rev 206815)
</span><span class="lines">@@ -0,0 +1,517 @@
</span><ins>+/*
+ * 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 "Path.h"
+
+#if USE(DIRECT2D)
+
+#include "AffineTransform.h"
+#include "COMPtr.h"
+#include "FloatRect.h"
+#include "GraphicsContext.h"
+#include "IntRect.h"
+#include "NotImplemented.h"
+#include "StrokeStyleApplier.h"
+#include <d2d1.h>
+#include <wtf/MathExtras.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+
+Path Path::polygonPathFromPoints(const Vector<FloatPoint>& points)
+{
+ Path path;
+ if (points.size() < 2)
+ return path;
+
+ Vector<D2D1_POINT_2F, 32> d2dPoints;
+ d2dPoints.reserveInitialCapacity(points.size() - 1);
+ for (auto point : points)
+ d2dPoints.uncheckedAppend(D2D1::Point2F(point.x(), point.y()));
+
+ path.moveTo(points.first());
+
+ ASSERT(path.activePath());
+
+ path.activePath()->AddLines(d2dPoints.data(), d2dPoints.size());
+ path.closeSubpath();
+
+ return path;
+}
+
+Path::Path()
+{
+}
+
+Path::~Path()
+{
+}
+
+PlatformPathPtr Path::ensurePlatformPath()
+{
+ if (!m_path) {
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(D2D1_FILL_MODE_WINDING, nullptr, 0, &m_path);
+ ASSERT(SUCCEEDED(hr));
+ if (FAILED(hr))
+ return nullptr;
+ }
+
+ return m_path;
+}
+
+void Path::appendGeometry(ID2D1Geometry* geometry)
+{
+ unsigned geometryCount = m_path ? m_path->GetSourceGeometryCount() : 0;
+ Vector<ID2D1Geometry*> geometries(geometryCount, nullptr);
+
+ if (geometryCount)
+ m_path->GetSourceGeometries(geometries.data(), geometryCount);
+
+ geometries.append(geometry);
+
+ auto fillMode = m_path ? m_path->GetFillMode() : D2D1_FILL_MODE_WINDING;
+
+ COMPtr<ID2D1GeometryGroup> protectedPath = m_path;
+ m_path = nullptr;
+
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(fillMode, geometries.data(), geometries.size(), &m_path);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+}
+
+void Path::createGeometryWithFillMode(WindRule webkitFillMode, COMPtr<ID2D1GeometryGroup>& path) const
+{
+ RELEASE_ASSERT(m_path);
+
+ auto fillMode = (webkitFillMode == RULE_EVENODD) ? D2D1_FILL_MODE_ALTERNATE : D2D1_FILL_MODE_WINDING;
+
+ if (fillMode == m_path->GetFillMode()) {
+ path = m_path;
+ return;
+ }
+
+ unsigned geometryCount = m_path->GetSourceGeometryCount();
+
+ Vector<ID2D1Geometry*> geometries(geometryCount, nullptr);
+ ASSERT(geometryCount);
+ m_path->GetSourceGeometries(geometries.data(), geometryCount);
+
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(fillMode, geometries.data(), geometries.size(), &path);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+}
+
+Path::Path(const Path& other)
+{
+ if (other.platformPath() && other.activePath()) {
+ auto otherPath = other.platformPath();
+
+ unsigned geometryCount = otherPath->GetSourceGeometryCount();
+
+ Vector<ID2D1Geometry*> geometries(geometryCount, nullptr);
+ ASSERT(geometryCount);
+ otherPath->GetSourceGeometries(geometries.data(), geometryCount);
+
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(other.m_path->GetFillMode(), geometries.data(), geometryCount, &m_path);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ }
+}
+
+Path& Path::operator=(const Path& other)
+{
+ m_path = other.m_path;
+ if (m_path)
+ m_path->AddRef();
+
+ m_activePath = other.m_activePath;
+ m_activePathGeometry = other.m_activePathGeometry;
+
+ return *this;
+}
+
+HRESULT Path::initializePathState()
+{
+ if (m_path)
+ m_path->Release();
+ m_path = nullptr;
+ m_activePath = nullptr;
+ m_activePathGeometry = nullptr;
+
+ GraphicsContext::systemFactory()->CreatePathGeometry(&m_activePathGeometry);
+
+ Vector<ID2D1Geometry*> geometries;
+ geometries.append(m_activePathGeometry.get());
+
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(D2D1_FILL_MODE_WINDING, geometries.data(), geometries.size(), &m_path);
+ if (FAILED(hr))
+ return hr;
+
+ return m_activePathGeometry->Open(&m_activePath);
+}
+
+void Path::drawDidComplete() const
+{
+ FloatPoint currentPoint = this->currentPoint();
+
+ // To maintain proper semantics with CG, we need to clear our Direct2D
+ // path objects when a draw has finished.
+ HRESULT hr = const_cast<Path*>(this)->initializePathState();
+
+ if (!SUCCEEDED(hr))
+ return;
+
+ m_activePath->SetFillMode(D2D1_FILL_MODE_WINDING);
+
+ m_activePath->BeginFigure(currentPoint, D2D1_FIGURE_BEGIN_FILLED);
+}
+
+bool Path::contains(const FloatPoint& point, WindRule rule) const
+{
+ if (isNull())
+ return false;
+
+ if (!fastBoundingRect().contains(point))
+ return false;
+
+ BOOL contains;
+ if (!SUCCEEDED(m_path->FillContainsPoint(D2D1::Point2F(point.x(), point.y()), nullptr, &contains)))
+ return false;
+
+ return contains;
+}
+
+bool Path::strokeContains(StrokeStyleApplier* applier, const FloatPoint& point) const
+{
+ if (isNull())
+ return false;
+
+ UNUSED_PARAM(applier);
+ UNUSED_PARAM(point);
+ notImplemented();
+ return false;
+}
+
+void Path::translate(const FloatSize& size)
+{
+ transform(AffineTransform(1, 0, 0, 1, size.width(), size.height()));
+}
+
+void Path::transform(const AffineTransform& transform)
+{
+ if (transform.isIdentity() || isEmpty())
+ return;
+
+ bool pathIsActive = false;
+ if (m_activePath) {
+ m_activePath->Close();
+ m_activePath = nullptr;
+ m_activePathGeometry = nullptr;
+ pathIsActive = true;
+ }
+
+ const D2D1_MATRIX_3X2_F& d2dTransform = static_cast<const D2D1_MATRIX_3X2_F>(transform);
+ COMPtr<ID2D1TransformedGeometry> transformedPath;
+ if (!SUCCEEDED(GraphicsContext::systemFactory()->CreateTransformedGeometry(m_path, d2dTransform, &transformedPath)))
+ return;
+
+ Vector<ID2D1Geometry*> geometries;
+ geometries.append(transformedPath.get());
+
+ if (pathIsActive) {
+ GraphicsContext::systemFactory()->CreatePathGeometry(&m_activePathGeometry);
+ m_activePathGeometry->Open(&m_activePath);
+ geometries.append(m_activePathGeometry.get());
+ }
+
+ auto fillMode = m_path->GetFillMode();
+
+ m_path->Release();
+ m_path = nullptr;
+
+ HRESULT hr = GraphicsContext::systemFactory()->CreateGeometryGroup(fillMode, geometries.data(), geometries.size(), &m_path);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+}
+
+FloatRect Path::boundingRect() const
+{
+ if (isNull())
+ return FloatRect();
+
+ D2D1_RECT_F bounds = { };
+ if (!SUCCEEDED(m_path->GetBounds(nullptr, &bounds)))
+ return FloatRect();
+
+ return FloatRect(bounds.left, bounds.bottom, bounds.right - bounds.left, bounds.top - bounds.bottom);
+}
+
+FloatRect Path::fastBoundingRect() const
+{
+ if (isNull())
+ return FloatRect();
+
+ D2D1_RECT_F bounds = { };
+ if (!SUCCEEDED(m_path->GetBounds(nullptr, &bounds)))
+ return FloatRect();
+
+ return FloatRect(bounds.left, bounds.bottom, bounds.right - bounds.left, bounds.top - bounds.bottom);
+}
+
+FloatRect Path::strokeBoundingRect(StrokeStyleApplier* applier) const
+{
+ if (isNull())
+ return FloatRect();
+
+ UNUSED_PARAM(applier);
+ notImplemented();
+ return FloatRect();
+}
+
+void Path::moveTo(const FloatPoint& point)
+{
+ if (!m_activePath) {
+ GraphicsContext::systemFactory()->CreatePathGeometry(&m_activePathGeometry);
+
+ appendGeometry(m_activePathGeometry.get());
+
+ if (!SUCCEEDED(m_activePathGeometry->Open(&m_activePath)))
+ return;
+
+ m_activePath->SetFillMode(D2D1_FILL_MODE_WINDING);
+ }
+
+ m_activePath->BeginFigure(point, D2D1_FIGURE_BEGIN_FILLED);
+}
+
+void Path::addLineTo(const FloatPoint& point)
+{
+ ASSERT(m_activePath.get());
+
+ m_activePath->AddLine(point);
+}
+
+void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
+{
+ ASSERT(m_activePath.get());
+
+ m_activePath->AddQuadraticBezier(D2D1::QuadraticBezierSegment(cp, p));
+}
+
+void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
+{
+ ASSERT(m_activePath.get());
+
+ FloatPoint beforePoint = currentPoint();
+
+ m_activePath->AddBezier(D2D1::BezierSegment(cp1, cp2, p));
+}
+
+void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
+{
+ UNUSED_PARAM(p1);
+ UNUSED_PARAM(p2);
+ UNUSED_PARAM(radius);
+ notImplemented();
+}
+
+static equalRadiusWidths(const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
+{
+ return topLeftRadius.width() == topRightRadius.width()
+ && topRightRadius.width() == bottomLeftRadius.width()
+ && bottomLeftRadius.width() == bottomRightRadius.width();
+}
+
+static equalRadiusHeights(const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
+{
+ return topLeftRadius.height() == bottomLeftRadius.height()
+ && bottomLeftRadius.height() == topRightRadius.height()
+ && topRightRadius.height() == bottomRightRadius.height();
+}
+
+void Path::platformAddPathForRoundedRect(const FloatRect& rect, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
+{
+ bool equalWidths = equalRadiusWidths(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
+ bool equalHeights = equalRadiusHeights(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
+
+ if (equalWidths && equalHeights) {
+ // Ensure that CG can render the rounded rect.
+ float radiusWidth = topLeftRadius.width();
+ float radiusHeight = topLeftRadius.height();
+ auto rectToDraw = D2D1::RectF(rect.x(), rect.y(), rect.maxX(), rect.maxY());
+
+ COMPtr<ID2D1RoundedRectangleGeometry> roundRect;
+ HRESULT hr = GraphicsContext::systemFactory()->CreateRoundedRectangleGeometry(D2D1::RoundedRect(rectToDraw, radiusWidth, radiusHeight), &roundRect);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ appendGeometry(roundRect.get());
+ return;
+ }
+
+ addBeziersForRoundedRect(rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
+}
+
+void Path::closeSubpath()
+{
+ if (isNull())
+ return;
+
+ ASSERT(m_activePath.get());
+ m_activePath->EndFigure(D2D1_FIGURE_END_OPEN);
+}
+
+static void drawArcSection(ID2D1GeometrySink* sink, FloatPoint center, float radius, float startAngle, float endAngle, bool clockwise)
+{
+ // Direct2D wants us to specify the end point of the arc, not the center. It will be drawn from
+ // whatever the current point in the 'm_activePath' is.
+ FloatPoint p = center;
+ float endX = radius * std::cos(endAngle);
+ float endY = radius * std::sin(endAngle);
+ p.move(endX, endY);
+
+ float arcDeg = rad2deg(endAngle - startAngle);
+ D2D1_SWEEP_DIRECTION direction = clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE;
+ sink->AddArc(D2D1::ArcSegment(p, D2D1::SizeF(radius, radius), arcDeg, direction, D2D1_ARC_SIZE_SMALL));
+}
+
+void Path::addArc(const FloatPoint& center, float radius, float startAngle, float endAngle, bool clockwise)
+{
+ if (!m_activePath) {
+ float startX = radius * std::cos(startAngle);
+ float startY = radius * std::sin(startAngle);
+
+ FloatPoint start = center;
+ start.move(startX, startY);
+ moveTo(start);
+ }
+
+ // Direct2D has problems drawing large arcs. It gets confused if drawing a complete (or
+ // nearly complete) circle in the counter-clockwise direction. So, draw any arcs larger
+ // than 180 degrees in two pieces.
+ float fullSweep = endAngle - startAngle;
+ float negate = fullSweep < 0 ? -1.0f : 1.0f;
+ float maxSweep = negate * std::min(std::abs(fullSweep), piFloat);
+ float firstArcEnd = startAngle + maxSweep;
+ drawArcSection(m_activePath.get(), center, radius, startAngle, firstArcEnd, clockwise);
+
+ if (areEssentiallyEqual(firstArcEnd, endAngle))
+ return;
+
+ drawArcSection(m_activePath.get(), center, radius, firstArcEnd, endAngle, clockwise);
+}
+
+void Path::addRect(const FloatRect& r)
+{
+ COMPtr<ID2D1RectangleGeometry> rectangle;
+ HRESULT hr = GraphicsContext::systemFactory()->CreateRectangleGeometry(static_cast<D2D1_RECT_F>(r), &rectangle);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ appendGeometry(rectangle.get());
+}
+
+void Path::addEllipse(FloatPoint p, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
+{
+ AffineTransform transform;
+ transform.translate(p.x(), p.y()).rotate(rad2deg(rotation)).scale(radiusX, radiusY);
+
+ notImplemented();
+}
+
+void Path::addEllipse(const FloatRect& r)
+{
+ COMPtr<ID2D1EllipseGeometry> ellipse;
+ HRESULT hr = GraphicsContext::systemFactory()->CreateEllipseGeometry(D2D1::Ellipse(D2D1::Point2F(r.center().x(), r.center().y()), r.width(), r.height()), &ellipse);
+ RELEASE_ASSERT(SUCCEEDED(hr));
+ appendGeometry(ellipse.get());
+}
+
+void Path::addPath(const Path& path, const AffineTransform& transform)
+{
+ if (!path.platformPath())
+ return;
+
+ if (!transform.isInvertible())
+ return;
+
+ notImplemented();
+}
+
+
+void Path::clear()
+{
+ if (isNull())
+ return;
+
+ if (m_path)
+ m_path->Release();
+
+ m_path = nullptr;
+ m_activePath = nullptr;
+ m_activePathGeometry = nullptr;
+}
+
+bool Path::isEmpty() const
+{
+ if (isNull())
+ return true;
+
+ if (!m_path->GetSourceGeometryCount())
+ return true;
+
+ return false;
+}
+
+bool Path::hasCurrentPoint() const
+{
+ return !isEmpty();
+}
+
+FloatPoint Path::currentPoint() const
+{
+ if (isNull())
+ return FloatPoint();
+
+ float length;
+ HRESULT hr = m_path->ComputeLength(nullptr, &length);
+ if (!SUCCEEDED(hr))
+ return FloatPoint();
+
+ D2D1_POINT_2F point = { };
+ D2D1_POINT_2F tangent = { };
+ hr = m_path->ComputePointAtLength(length, nullptr, &point, &tangent);
+ if (!SUCCEEDED(hr))
+ return FloatPoint();
+
+ return point;
+}
+
+void Path::apply(const PathApplierFunction& function) const
+{
+ if (isNull())
+ return;
+
+ notImplemented();
+}
+
+}
+
+#endif // USE(DIRECT2D)
</ins></span></pre>
</div>
</div>
</body>
</html>