diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp')
-rw-r--r-- | Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp | 708 |
1 files changed, 401 insertions, 307 deletions
diff --git a/Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp b/Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp index 0fa750396..ac4b4a089 100644 --- a/Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp +++ b/Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2006 Apple Inc. All rights reserved. * Copyright (C) 2007 Alp Toker <alp@atoker.com> * Copyright (C) 2008, 2009 Dirk Schulze <krit@webkit.org> * Copyright (C) 2008 Nuanti Ltd. @@ -17,10 +17,10 @@ * 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 COMPUTER, INC. ``AS IS'' AND ANY + * 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 COMPUTER, INC. OR + * 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 @@ -37,30 +37,29 @@ #include "AffineTransform.h" #include "CairoUtilities.h" +#include "DisplayListRecorder.h" #include "DrawErrorUnderline.h" #include "FloatConversion.h" #include "FloatRect.h" +#include "FloatRoundedRect.h" #include "Font.h" #include "GraphicsContextPlatformPrivateCairo.h" +#include "ImageBuffer.h" #include "IntRect.h" #include "NotImplemented.h" -#include "OwnPtrCairo.h" #include "Path.h" #include "Pattern.h" #include "PlatformContextCairo.h" #include "PlatformPathCairo.h" #include "RefPtrCairo.h" #include "ShadowBlur.h" -#include "SimpleFontData.h" #include "TransformationMatrix.h" #include <cairo.h> #include <math.h> #include <stdio.h> #include <wtf/MathExtras.h> -#if PLATFORM(GTK) -#include <gdk/gdk.h> -#elif PLATFORM(WIN) +#if PLATFORM(WIN) #include <cairo-win32.h> #endif @@ -71,36 +70,31 @@ namespace WebCore { // A helper which quickly fills a rectangle with a simple color fill. static inline void fillRectWithColor(cairo_t* cr, const FloatRect& rect, const Color& color) { - if (!color.alpha() && cairo_get_operator(cr) == CAIRO_OPERATOR_OVER) + if (!color.isVisible() && cairo_get_operator(cr) == CAIRO_OPERATOR_OVER) return; + setSourceRGBAFromColor(cr, color); cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); cairo_fill(cr); } -static void addConvexPolygonToContext(cairo_t* context, size_t numPoints, const FloatPoint* points) -{ - cairo_move_to(context, points[0].x(), points[0].y()); - for (size_t i = 1; i < numPoints; i++) - cairo_line_to(context, points[i].x(), points[i].y()); - cairo_close_path(context); -} - enum PathDrawingStyle { Fill = 1, Stroke = 2, FillAndStroke = Fill + Stroke }; -static inline void drawPathShadow(GraphicsContext* context, PathDrawingStyle drawingStyle) +static inline void drawPathShadow(GraphicsContext& context, PathDrawingStyle drawingStyle) { - ShadowBlur& shadow = context->platformContext()->shadowBlur(); + ShadowBlur& shadow = context.platformContext()->shadowBlur(); if (shadow.type() == ShadowBlur::NoShadow) return; // Calculate the extents of the rendered solid paths. - cairo_t* cairoContext = context->platformContext()->cr(); - OwnPtr<cairo_path_t> path = adoptPtr(cairo_copy_path(cairoContext)); + cairo_t* cairoContext = context.platformContext()->cr(); + std::unique_ptr<cairo_path_t, void(*)(cairo_path_t*)> path(cairo_copy_path(cairoContext), [](cairo_path_t* path) { + cairo_path_destroy(path); + }); FloatRect solidFigureExtents; double x0 = 0; @@ -130,14 +124,14 @@ static inline void drawPathShadow(GraphicsContext* context, PathDrawingStyle dra if (drawingStyle & Fill) { cairo_save(cairoShadowContext); cairo_append_path(cairoShadowContext, path.get()); - shadowContext->platformContext()->prepareForFilling(context->state(), PlatformContextCairo::NoAdjustment); + shadowContext->platformContext()->prepareForFilling(context.state(), PlatformContextCairo::NoAdjustment); cairo_fill(cairoShadowContext); cairo_restore(cairoShadowContext); } if (drawingStyle & Stroke) { cairo_append_path(cairoShadowContext, path.get()); - shadowContext->platformContext()->prepareForStroking(context->state(), PlatformContextCairo::DoNotPreserveAlpha); + shadowContext->platformContext()->prepareForStroking(context.state(), PlatformContextCairo::DoNotPreserveAlpha); cairo_stroke(cairoShadowContext); } @@ -150,44 +144,45 @@ static inline void drawPathShadow(GraphicsContext* context, PathDrawingStyle dra cairo_append_path(cairoContext, path.get()); } -static inline void fillCurrentCairoPath(GraphicsContext* context) +static inline void fillCurrentCairoPath(GraphicsContext& context) { - cairo_t* cr = context->platformContext()->cr(); + cairo_t* cr = context.platformContext()->cr(); cairo_save(cr); - context->platformContext()->prepareForFilling(context->state(), PlatformContextCairo::AdjustPatternForGlobalAlpha); + context.platformContext()->prepareForFilling(context.state(), PlatformContextCairo::AdjustPatternForGlobalAlpha); cairo_fill(cr); cairo_restore(cr); } -static inline void shadowAndFillCurrentCairoPath(GraphicsContext* context) +static inline void shadowAndFillCurrentCairoPath(GraphicsContext& context) { drawPathShadow(context, Fill); fillCurrentCairoPath(context); } -static inline void shadowAndStrokeCurrentCairoPath(GraphicsContext* context) +static inline void shadowAndStrokeCurrentCairoPath(GraphicsContext& context) { drawPathShadow(context, Stroke); - context->platformContext()->prepareForStroking(context->state()); - cairo_stroke(context->platformContext()->cr()); + context.platformContext()->prepareForStroking(context.state()); + cairo_stroke(context.platformContext()->cr()); } GraphicsContext::GraphicsContext(cairo_t* cr) - : m_updatingControlTints(false), - m_transparencyCount(0) { + if (!cr) + return; + m_data = new GraphicsContextPlatformPrivateToplevel(new PlatformContextCairo(cr)); } void GraphicsContext::platformInit(PlatformContextCairo* platformContext) { + if (!platformContext) + return; + m_data = new GraphicsContextPlatformPrivate(platformContext); - if (platformContext) - m_data->syncContext(platformContext->cr()); - else - setPaintingDisabled(true); + m_data->syncContext(platformContext->cr()); } void GraphicsContext::platformDestroy() @@ -200,6 +195,11 @@ AffineTransform GraphicsContext::getCTM(IncludeDeviceScale) const if (paintingDisabled()) return AffineTransform(); + if (isRecording()) { + WTFLogAlways("GraphicsContext::getCTM() is not yet compatible with recording contexts."); + return AffineTransform(); + } + cairo_t* cr = platformContext()->cr(); cairo_matrix_t m; cairo_get_matrix(cr, &m); @@ -213,28 +213,34 @@ PlatformContextCairo* GraphicsContext::platformContext() const void GraphicsContext::savePlatformState() { + ASSERT(!isRecording()); platformContext()->save(); m_data->save(); } void GraphicsContext::restorePlatformState() { + ASSERT(!isRecording()); platformContext()->restore(); m_data->restore(); platformContext()->shadowBlur().setShadowValues(FloatSize(m_state.shadowBlur, m_state.shadowBlur), m_state.shadowOffset, m_state.shadowColor, - m_state.shadowColorSpace, m_state.shadowsIgnoreTransforms); } // Draws a filled rectangle with a stroked border. -void GraphicsContext::drawRect(const IntRect& rect) +void GraphicsContext::drawRect(const FloatRect& rect, float borderThickness) { if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->drawRect(rect, borderThickness); + return; + } + ASSERT(!rect.isEmpty()); cairo_t* cr = platformContext()->cr(); @@ -247,108 +253,132 @@ void GraphicsContext::drawRect(const IntRect& rect) FloatRect r(rect); r.inflate(-.5f); cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height()); - cairo_set_line_width(cr, 1.0); + cairo_set_line_width(cr, 1.0); // borderThickness? cairo_stroke(cr); } cairo_restore(cr); } -static double calculateStrokePatternOffset(int distance, int patternWidth) +void GraphicsContext::drawNativeImage(const NativeImagePtr& image, const FloatSize& imageSize, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, ImageOrientation orientation) { - // Example: 80 pixels with a width of 30 pixels. Remainder is 20. - // The maximum pixels of line we could paint will be 50 pixels. - int remainder = distance % patternWidth; - int numSegments = (distance - remainder) / patternWidth; + if (paintingDisabled()) + return; - // Special case 1px dotted borders for speed. - if (patternWidth == 1) - return 1; + if (isRecording()) { + m_displayListRecorder->drawNativeImage(image, imageSize, destRect, srcRect, op, blendMode, orientation); + return; + } - bool evenNumberOfSegments = !(numSegments % 2); - if (remainder) - evenNumberOfSegments = !evenNumberOfSegments; + platformContext()->save(); - if (evenNumberOfSegments) { - if (remainder) - return (patternWidth - remainder) + (remainder / 2); - return patternWidth / 2; + // Set the compositing operation. + if (op == CompositeSourceOver && blendMode == BlendModeNormal) + setCompositeOperation(CompositeCopy); + else + setCompositeOperation(op, blendMode); + + FloatRect dst = destRect; + + if (orientation != DefaultImageOrientation) { + // ImageOrientation expects the origin to be at (0, 0). + translate(dst.x(), dst.y()); + dst.setLocation(FloatPoint()); + concatCTM(orientation.transformFromDefault(dst.size())); + if (orientation.usesWidthAsHeight()) { + // The destination rectangle will have its width and height already reversed for the orientation of + // the image, as it was needed for page layout, so we need to reverse it back here. + dst = FloatRect(dst.x(), dst.y(), dst.height(), dst.width()); + } } - // Odd number of segments. - if (remainder) - return (patternWidth - remainder) / 2.f; - return 0; + platformContext()->drawSurfaceToContext(image.get(), dst, srcRect, *this); + platformContext()->restore(); } -static void drawLineOnCairoContext(GraphicsContext* graphicsContext, cairo_t* context, const FloatPoint& point1, const FloatPoint& point2) +// This is only used to draw borders, so we should not draw shadows. +void GraphicsContext::drawLine(const FloatPoint& point1, const FloatPoint& point2) { - StrokeStyle style = graphicsContext->strokeStyle(); - if (style == NoStroke) + if (paintingDisabled()) return; - const Color& strokeColor = graphicsContext->strokeColor(); - int strokeThickness = floorf(graphicsContext->strokeThickness()); - if (graphicsContext->strokeThickness() < 1) - strokeThickness = 1; + if (strokeStyle() == NoStroke) + return; - int patternWidth = 0; - if (style == DottedStroke) - patternWidth = strokeThickness; - else if (style == DashedStroke) - patternWidth = 3 * strokeThickness; + if (isRecording()) { + m_displayListRecorder->drawLine(point1, point2); + return; + } - bool isVerticalLine = point1.x() == point2.x(); - FloatPoint point1OnPixelBoundaries = point1; - FloatPoint point2OnPixelBoundaries = point2; - GraphicsContext::adjustLineToPixelBoundaries(point1OnPixelBoundaries, point2OnPixelBoundaries, strokeThickness, style); + const Color& strokeColor = this->strokeColor(); + float thickness = strokeThickness(); + bool isVerticalLine = (point1.x() + thickness == point2.x()); + float strokeWidth = isVerticalLine ? point2.y() - point1.y() : point2.x() - point1.x(); + if (!thickness || !strokeWidth) + return; - if (patternWidth) { - // Do a rect fill of our endpoints. This ensures we always have the - // appearance of being a border. We then draw the actual dotted/dashed line. - FloatRect firstRect(point1OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness)); - FloatRect secondRect(point2OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness)); + cairo_t* cairoContext = platformContext()->cr(); + StrokeStyle strokeStyle = this->strokeStyle(); + float cornerWidth = 0; + bool drawsDashedLine = strokeStyle == DottedStroke || strokeStyle == DashedStroke; + + if (drawsDashedLine) { + cairo_save(cairoContext); + // Figure out end points to ensure we always paint corners. + cornerWidth = dashedLineCornerWidthForStrokeWidth(strokeWidth); if (isVerticalLine) { - firstRect.move(-strokeThickness / 2, -strokeThickness); - secondRect.move(-strokeThickness / 2, 0); + fillRectWithColor(cairoContext, FloatRect(point1.x(), point1.y(), thickness, cornerWidth), strokeColor); + fillRectWithColor(cairoContext, FloatRect(point1.x(), point2.y() - cornerWidth, thickness, cornerWidth), strokeColor); } else { - firstRect.move(-strokeThickness, -strokeThickness / 2); - secondRect.move(0, -strokeThickness / 2); + fillRectWithColor(cairoContext, FloatRect(point1.x(), point1.y(), cornerWidth, thickness), strokeColor); + fillRectWithColor(cairoContext, FloatRect(point2.x() - cornerWidth, point1.y(), cornerWidth, thickness), strokeColor); + } + strokeWidth -= 2 * cornerWidth; + float patternWidth = dashedLinePatternWidthForStrokeWidth(strokeWidth); + // Check if corner drawing sufficiently covers the line. + if (strokeWidth <= patternWidth + 1) { + cairo_restore(cairoContext); + return; } - fillRectWithColor(context, firstRect, strokeColor); - fillRectWithColor(context, secondRect, strokeColor); - int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2 * strokeThickness; - double patternOffset = calculateStrokePatternOffset(distance, patternWidth); - double patternWidthAsDouble = patternWidth; - cairo_set_dash(context, &patternWidthAsDouble, 1, patternOffset); + float patternOffset = dashedLinePatternOffsetForPatternAndStrokeWidth(patternWidth, strokeWidth); + const double dashedLine[2] = { static_cast<double>(patternWidth), static_cast<double>(patternWidth) }; + cairo_set_dash(cairoContext, dashedLine, 2, patternOffset); + } else { + setSourceRGBAFromColor(cairoContext, strokeColor); + if (thickness < 1) + cairo_set_line_width(cairoContext, 1); } - setSourceRGBAFromColor(context, strokeColor); - cairo_set_line_width(context, strokeThickness); - cairo_move_to(context, point1OnPixelBoundaries.x(), point1OnPixelBoundaries.y()); - cairo_line_to(context, point2OnPixelBoundaries.x(), point2OnPixelBoundaries.y()); - cairo_stroke(context); -} -// This is only used to draw borders, so we should not draw shadows. -void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2) -{ - if (paintingDisabled()) - return; + auto centeredPoints = centerLineAndCutOffCorners(isVerticalLine, cornerWidth, point1, point2); + auto p1 = centeredPoints[0]; + auto p2 = centeredPoints[1]; - cairo_t* cairoContext = platformContext()->cr(); - cairo_save(cairoContext); - drawLineOnCairoContext(this, cairoContext, point1, point2); - cairo_restore(cairoContext); + if (shouldAntialias()) + cairo_set_antialias(cairoContext, CAIRO_ANTIALIAS_NONE); + + cairo_new_path(cairoContext); + cairo_move_to(cairoContext, p1.x(), p1.y()); + cairo_line_to(cairoContext, p2.x(), p2.y()); + cairo_stroke(cairoContext); + if (drawsDashedLine) + cairo_restore(cairoContext); + if (shouldAntialias()) + cairo_set_antialias(cairoContext, CAIRO_ANTIALIAS_DEFAULT); } // This method is only used to draw the little circles used in lists. -void GraphicsContext::drawEllipse(const IntRect& rect) +void GraphicsContext::drawEllipse(const FloatRect& rect) { if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->drawEllipse(rect); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_save(cr); float yRadius = .5 * rect.height(); @@ -358,7 +388,7 @@ void GraphicsContext::drawEllipse(const IntRect& rect) cairo_arc(cr, 0., 0., 1., 0., 2 * piFloat); cairo_restore(cr); - if (fillColor().alpha()) { + if (fillColor().isVisible()) { setSourceRGBAFromColor(cr, fillColor()); cairo_fill_preserve(cr); } @@ -371,67 +401,19 @@ void GraphicsContext::drawEllipse(const IntRect& rect) cairo_new_path(cr); } -void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias) +void GraphicsContext::fillPath(const Path& path) { - if (paintingDisabled()) + if (paintingDisabled() || path.isEmpty()) return; - if (npoints <= 1) + if (isRecording()) { + m_displayListRecorder->fillPath(path); return; - - cairo_t* cr = platformContext()->cr(); - - cairo_save(cr); - cairo_set_antialias(cr, shouldAntialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE); - addConvexPolygonToContext(cr, npoints, points); - - if (fillColor().alpha()) { - setSourceRGBAFromColor(cr, fillColor()); - cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); - cairo_fill_preserve(cr); } - if (strokeStyle() != NoStroke) { - setSourceRGBAFromColor(cr, strokeColor()); - cairo_set_line_width(cr, strokeThickness()); - cairo_stroke(cr); - } else - cairo_new_path(cr); - - cairo_restore(cr); -} - -void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialiased) -{ - if (paintingDisabled()) - return; - - if (numPoints <= 1) - return; - - cairo_t* cr = platformContext()->cr(); - - cairo_new_path(cr); - cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr); - cairo_antialias_t savedAntialiasRule = cairo_get_antialias(cr); - - cairo_set_antialias(cr, antialiased ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE); - cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING); - addConvexPolygonToContext(cr, numPoints, points); - cairo_clip(cr); - - cairo_set_antialias(cr, savedAntialiasRule); - cairo_set_fill_rule(cr, savedFillRule); -} - -void GraphicsContext::fillPath(const Path& path) -{ - if (paintingDisabled() || path.isEmpty()) - return; - cairo_t* cr = platformContext()->cr(); setPathOnCairoContext(cr, path.platformPath()->context()); - shadowAndFillCurrentCairoPath(this); + shadowAndFillCurrentCairoPath(*this); } void GraphicsContext::strokePath(const Path& path) @@ -439,9 +421,14 @@ void GraphicsContext::strokePath(const Path& path) if (paintingDisabled() || path.isEmpty()) return; + if (isRecording()) { + m_displayListRecorder->strokePath(path); + return; + } + cairo_t* cr = platformContext()->cr(); setPathOnCairoContext(cr, path.platformPath()->context()); - shadowAndStrokeCurrentCairoPath(this); + shadowAndStrokeCurrentCairoPath(*this); } void GraphicsContext::fillRect(const FloatRect& rect) @@ -449,18 +436,28 @@ void GraphicsContext::fillRect(const FloatRect& rect) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->fillRect(rect); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); - shadowAndFillCurrentCairoPath(this); + shadowAndFillCurrentCairoPath(*this); } -void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace) +void GraphicsContext::fillRect(const FloatRect& rect, const Color& color) { if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->fillRect(rect, color); + return; + } + if (hasShadow()) - platformContext()->shadowBlur().drawRectShadow(this, rect, RoundedRect::Radii()); + platformContext()->shadowBlur().drawRectShadow(*this, FloatRoundedRect(rect)); fillRectWithColor(platformContext()->cr(), rect, color); } @@ -470,6 +467,11 @@ void GraphicsContext::clip(const FloatRect& rect) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->clip(rect); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr); @@ -492,15 +494,44 @@ void GraphicsContext::clipPath(const Path& path, WindRule clipRule) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->clipPath(path, clipRule); + return; + } + cairo_t* cr = platformContext()->cr(); if (!path.isNull()) setPathOnCairoContext(cr, path.platformPath()->context()); + + cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr); cairo_set_fill_rule(cr, clipRule == RULE_EVENODD ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); cairo_clip(cr); + cairo_set_fill_rule(cr, savedFillRule); + + m_data->clip(path); +} + +void GraphicsContext::clipToImageBuffer(ImageBuffer& buffer, const FloatRect& destRect) +{ + if (paintingDisabled()) + return; + + RefPtr<Image> image = buffer.copyImage(DontCopyBackingStore); + RefPtr<cairo_surface_t> surface = image->nativeImageForCurrentFrame(); + if (surface) + platformContext()->pushImageMask(surface.get(), destRect); } IntRect GraphicsContext::clipBounds() const { + if (paintingDisabled()) + return IntRect(); + + if (isRecording()) { + WTFLogAlways("Getting the clip bounds not yet supported with display lists"); + return IntRect(-2048, -2048, 4096, 4096); // FIXME: display lists. + } + double x1, x2, y1, y2; cairo_clip_extents(platformContext()->cr(), &x1, &y1, &x2, &y2); return enclosingIntRect(FloatRect(x1, y1, x2 - x1, y2 - y1)); @@ -510,11 +541,13 @@ static inline void adjustFocusRingColor(Color& color) { #if !PLATFORM(GTK) // Force the alpha to 50%. This matches what the Mac does with outline rings. - color.setRGB(makeRGBA(color.red(), color.green(), color.blue(), 127)); + color = Color(makeRGBA(color.red(), color.green(), color.blue(), 127)); +#else + UNUSED_PARAM(color); #endif } -static inline void adjustFocusRingLineWidth(int& width) +static inline void adjustFocusRingLineWidth(float& width) { #if PLATFORM(GTK) width = 2; @@ -532,8 +565,11 @@ static inline StrokeStyle focusRingStrokeStyle() #endif } -void GraphicsContext::drawFocusRing(const Path& path, int width, int /* offset */, const Color& color) +void GraphicsContext::drawFocusRing(const Path& path, float width, float /* offset */, const Color& color) { + if (paintingDisabled()) + return; + // FIXME: We should draw paths that describe a rectangle with rounded corners // so as to be consistent with how we draw rectangular focus rings. Color ringColor = color; @@ -542,109 +578,91 @@ void GraphicsContext::drawFocusRing(const Path& path, int width, int /* offset * cairo_t* cr = platformContext()->cr(); cairo_save(cr); + cairo_push_group(cr); appendWebCorePathToCairoContext(cr, path); setSourceRGBAFromColor(cr, ringColor); cairo_set_line_width(cr, width); setPlatformStrokeStyle(focusRingStrokeStyle()); - cairo_stroke(cr); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + cairo_stroke_preserve(cr); + + cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); + cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING); + cairo_fill(cr); + + cairo_pop_group_to_source(cr); + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + cairo_paint(cr); cairo_restore(cr); } -void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int /* offset */, const Color& color) +void GraphicsContext::drawFocusRing(const Vector<FloatRect>& rects, float width, float /* offset */, const Color& color) { if (paintingDisabled()) return; - unsigned rectCount = rects.size(); - - cairo_t* cr = platformContext()->cr(); - cairo_save(cr); - cairo_push_group(cr); - cairo_new_path(cr); - + Path path; #if PLATFORM(GTK) -#ifdef GTK_API_VERSION_2 - GdkRegion* reg = gdk_region_new(); -#else - cairo_region_t* reg = cairo_region_create(); -#endif - - for (unsigned i = 0; i < rectCount; i++) { -#ifdef GTK_API_VERSION_2 - GdkRectangle rect = rects[i]; - gdk_region_union_with_rect(reg, &rect); -#else - cairo_rectangle_int_t rect = rects[i]; - cairo_region_union_rectangle(reg, &rect); -#endif - } - gdk_cairo_region(cr, reg); -#ifdef GTK_API_VERSION_2 - gdk_region_destroy(reg); -#else - cairo_region_destroy(reg); -#endif + for (const auto& rect : rects) + path.addRect(rect); #else + unsigned rectCount = rects.size(); int radius = (width - 1) / 2; - Path path; + Path subPath; for (unsigned i = 0; i < rectCount; ++i) { if (i > 0) - path.clear(); - path.addRoundedRect(rects[i], FloatSize(radius, radius)); - appendWebCorePathToCairoContext(cr, path); + subPath.clear(); + subPath.addRoundedRect(rects[i], FloatSize(radius, radius)); + path.addPath(subPath, AffineTransform()); } #endif - Color ringColor = color; - adjustFocusRingColor(ringColor); - adjustFocusRingLineWidth(width); - setSourceRGBAFromColor(cr, ringColor); - cairo_set_line_width(cr, width); - setPlatformStrokeStyle(focusRingStrokeStyle()); - - cairo_set_operator(cr, CAIRO_OPERATOR_OVER); - cairo_stroke_preserve(cr); - - cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); - cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING); - cairo_fill(cr); - - cairo_pop_group_to_source(cr); - cairo_set_operator(cr, CAIRO_OPERATOR_OVER); - cairo_paint(cr); - cairo_restore(cr); + drawFocusRing(path, width, 0, color); } -FloatRect GraphicsContext::computeLineBoundsForText(const FloatPoint& origin, float width, bool) +void GraphicsContext::drawLineForText(const FloatPoint& origin, float width, bool printing, bool doubleUnderlines, StrokeStyle) { - return FloatRect(origin, FloatSize(width, strokeThickness())); + DashArray widths; + widths.append(width); + widths.append(0); + drawLinesForText(origin, widths, printing, doubleUnderlines); } -void GraphicsContext::drawLineForText(const FloatPoint& origin, float width, bool printing) +void GraphicsContext::drawLinesForText(const FloatPoint& point, const DashArray& widths, bool printing, bool doubleUnderlines, StrokeStyle) { if (paintingDisabled()) return; - cairo_t* cairoContext = platformContext()->cr(); - cairo_save(cairoContext); - - // This bumping of <1 stroke thicknesses matches the one in drawLineOnCairoContext. - FloatPoint endPoint(origin + IntSize(width, 0)); - FloatRect lineExtents = computeLineBoundsForText(origin, width, printing); + if (widths.size() <= 0) + return; - ShadowBlur& shadow = platformContext()->shadowBlur(); - if (GraphicsContext* shadowContext = shadow.beginShadowLayer(this, lineExtents)) { - drawLineOnCairoContext(this, shadowContext->platformContext()->cr(), origin, endPoint); - shadow.endShadowLayer(this); + if (isRecording()) { + m_displayListRecorder->drawLinesForText(point, widths, printing, doubleUnderlines, strokeThickness()); + return; } - drawLineOnCairoContext(this, cairoContext, origin, endPoint); - cairo_restore(cairoContext); -} + Color localStrokeColor(strokeColor()); -void GraphicsContext::drawLinesForText(const FloatPoint& point, const DashArray& widths, bool printing) -{ + FloatRect bounds = computeLineBoundsAndAntialiasingModeForText(point, widths.last(), printing, localStrokeColor); + + Vector<FloatRect, 4> dashBounds; + ASSERT(!(widths.size() % 2)); + dashBounds.reserveInitialCapacity(dashBounds.size() / 2); for (size_t i = 0; i < widths.size(); i += 2) - drawLineForText(FloatPoint(point.x() + widths[i], point.y()), widths[i+1] - widths[i], printing); + dashBounds.append(FloatRect(FloatPoint(bounds.x() + widths[i], bounds.y()), FloatSize(widths[i+1] - widths[i], bounds.height()))); + + if (doubleUnderlines) { + // The space between double underlines is equal to the height of the underline + for (size_t i = 0; i < widths.size(); i += 2) + dashBounds.append(FloatRect(FloatPoint(bounds.x() + widths[i], bounds.y() + 2 * bounds.height()), FloatSize(widths[i+1] - widths[i], bounds.height()))); + } + + cairo_t* cr = platformContext()->cr(); + cairo_save(cr); + + for (auto& dash : dashBounds) + fillRectWithColor(cr, dash, localStrokeColor); + + cairo_restore(cr); } void GraphicsContext::updateDocumentMarkerResources() @@ -679,6 +697,14 @@ void GraphicsContext::drawLineForDocumentMarker(const FloatPoint& origin, float FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect, RoundingMode) { + if (paintingDisabled()) + return frect; + + if (isRecording()) { + WTFLogAlways("GraphicsContext::roundToDevicePixels() is not yet compatible with recording contexts."); + return frect; + } + FloatRect result; double x = frect.x(); double y = frect.y(); @@ -701,7 +727,7 @@ FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect, RoundingM width = 1; else width = round(width); - if (height > -1 && width < 0) + if (height > -1 && height < 0) height = -1; else if (height > 0 && height < 1) height = 1; @@ -719,18 +745,23 @@ void GraphicsContext::translate(float x, float y) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->translate(x, y); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_translate(cr, x, y); m_data->translate(x, y); } -void GraphicsContext::setPlatformFillColor(const Color&, ColorSpace) +void GraphicsContext::setPlatformFillColor(const Color&) { // Cairo contexts can't hold separate fill and stroke colors // so we set them just before we actually fill or stroke } -void GraphicsContext::setPlatformStrokeColor(const Color&, ColorSpace) +void GraphicsContext::setPlatformStrokeColor(const Color&) { // Cairo contexts can't hold separate fill and stroke colors // so we set them just before we actually fill or stroke @@ -741,17 +772,21 @@ void GraphicsContext::setPlatformStrokeThickness(float strokeThickness) if (paintingDisabled()) return; + ASSERT(!isRecording()); + cairo_set_line_width(platformContext()->cr(), strokeThickness); } void GraphicsContext::setPlatformStrokeStyle(StrokeStyle strokeStyle) { - static double dashPattern[] = {5.0, 5.0}; - static double dotPattern[] = {1.0, 1.0}; + static const double dashPattern[] = { 5.0, 5.0 }; + static const double dotPattern[] = { 1.0, 1.0 }; if (paintingDisabled()) return; + ASSERT(!isRecording()); + switch (strokeStyle) { case NoStroke: // FIXME: is it the right way to emulate NoStroke? @@ -771,7 +806,7 @@ void GraphicsContext::setPlatformStrokeStyle(StrokeStyle strokeStyle) } } -void GraphicsContext::setURLForRect(const URL&, const IntRect&) +void GraphicsContext::setURLForRect(const URL&, const FloatRect&) { notImplemented(); } @@ -781,6 +816,11 @@ void GraphicsContext::concatCTM(const AffineTransform& transform) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->concatCTM(transform); + return; + } + cairo_t* cr = platformContext()->cr(); const cairo_matrix_t matrix = cairo_matrix_t(transform); cairo_transform(cr, &matrix); @@ -792,13 +832,18 @@ void GraphicsContext::setCTM(const AffineTransform& transform) if (paintingDisabled()) return; + if (isRecording()) { + WTFLogAlways("GraphicsContext::setCTM() is not compatible with recording contexts."); + return; + } + cairo_t* cr = platformContext()->cr(); const cairo_matrix_t matrix = cairo_matrix_t(transform); cairo_set_matrix(cr, &matrix); m_data->setCTM(transform); } -void GraphicsContext::setPlatformShadow(FloatSize const& size, float, Color const&, ColorSpace) +void GraphicsContext::setPlatformShadow(FloatSize const& size, float, Color const&) { if (paintingDisabled()) return; @@ -813,7 +858,6 @@ void GraphicsContext::setPlatformShadow(FloatSize const& size, float, Color cons platformContext()->shadowBlur().setShadowValues(FloatSize(m_state.shadowBlur, m_state.shadowBlur), m_state.shadowOffset, m_state.shadowColor, - m_state.shadowColorSpace, m_state.shadowsIgnoreTransforms); } @@ -830,6 +874,8 @@ void GraphicsContext::beginPlatformTransparencyLayer(float opacity) if (paintingDisabled()) return; + ASSERT(!isRecording()); + cairo_t* cr = platformContext()->cr(); cairo_push_group(cr); m_data->layers.append(opacity); @@ -840,6 +886,8 @@ void GraphicsContext::endPlatformTransparencyLayer() if (paintingDisabled()) return; + ASSERT(!isRecording()); + cairo_t* cr = platformContext()->cr(); cairo_pop_group_to_source(cr); @@ -857,6 +905,11 @@ void GraphicsContext::clearRect(const FloatRect& rect) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->clearRect(rect); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_save(cr); @@ -871,11 +924,16 @@ void GraphicsContext::strokeRect(const FloatRect& rect, float width) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->strokeRect(rect, width); + return; + } + cairo_t* cr = platformContext()->cr(); cairo_save(cr); cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); cairo_set_line_width(cr, width); - shadowAndStrokeCurrentCairoPath(this); + shadowAndStrokeCurrentCairoPath(*this); cairo_restore(cr); } @@ -884,6 +942,11 @@ void GraphicsContext::setLineCap(LineCap lineCap) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->setLineCap(lineCap); + return; + } + cairo_line_cap_t cairoCap = CAIRO_LINE_CAP_BUTT; switch (lineCap) { case ButtCap: @@ -899,9 +962,29 @@ void GraphicsContext::setLineCap(LineCap lineCap) cairo_set_line_cap(platformContext()->cr(), cairoCap); } +static inline bool isDashArrayAllZero(const DashArray& dashes) +{ + for (auto& dash : dashes) { + if (dash) + return false; + } + return true; +} + void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset) { - cairo_set_dash(platformContext()->cr(), dashes.data(), dashes.size(), dashOffset); + if (paintingDisabled()) + return; + + if (isRecording()) { + m_displayListRecorder->setLineDash(dashes, dashOffset); + return; + } + + if (isDashArrayAllZero(dashes)) + cairo_set_dash(platformContext()->cr(), 0, 0, 0); + else + cairo_set_dash(platformContext()->cr(), dashes.data(), dashes.size(), dashOffset); } void GraphicsContext::setLineJoin(LineJoin lineJoin) @@ -909,6 +992,11 @@ void GraphicsContext::setLineJoin(LineJoin lineJoin) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->setLineJoin(lineJoin); + return; + } + cairo_line_join_t cairoJoin = CAIRO_LINE_JOIN_MITER; switch (lineJoin) { case MiterJoin: @@ -929,10 +1017,16 @@ void GraphicsContext::setMiterLimit(float miter) if (paintingDisabled()) return; + if (isRecording()) { + // Maybe this should be part of the state. + m_displayListRecorder->setMiterLimit(miter); + return; + } + cairo_set_miter_limit(platformContext()->cr(), miter); } -void GraphicsContext::setAlpha(float alpha) +void GraphicsContext::setPlatformAlpha(float alpha) { platformContext()->setGlobalAlpha(alpha); } @@ -942,39 +1036,12 @@ void GraphicsContext::setPlatformCompositeOperation(CompositeOperator op, BlendM if (paintingDisabled()) return; - cairo_operator_t cairo_op; - if (blendOp == BlendModeNormal) - cairo_op = toCairoOperator(op); - else - cairo_op = toCairoOperator(blendOp); - - cairo_set_operator(platformContext()->cr(), cairo_op); -} - -void GraphicsContext::clip(const Path& path, WindRule windRule) -{ - if (paintingDisabled()) - return; - - cairo_t* cr = platformContext()->cr(); - OwnPtr<cairo_path_t> pathCopy; - if (!path.isNull()) { - pathCopy = adoptPtr(cairo_copy_path(path.platformPath()->context())); - cairo_append_path(cr, pathCopy.get()); - } - cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr); - if (windRule == RULE_NONZERO) - cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING); - else - cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); - cairo_clip(cr); - cairo_set_fill_rule(cr, savedFillRule); - m_data->clip(path); + cairo_set_operator(platformContext()->cr(), toCairoOperator(op, blendOp)); } void GraphicsContext::canvasClip(const Path& path, WindRule windRule) { - clip(path, windRule); + clipPath(path, windRule); } void GraphicsContext::clipOut(const Path& path) @@ -982,6 +1049,11 @@ void GraphicsContext::clipOut(const Path& path) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->clipOut(path); + return; + } + cairo_t* cr = platformContext()->cr(); double x1, y1, x2, y2; cairo_clip_extents(cr, &x1, &y1, &x2, &y2); @@ -999,6 +1071,11 @@ void GraphicsContext::rotate(float radians) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->rotate(radians); + return; + } + cairo_rotate(platformContext()->cr(), radians); m_data->rotate(radians); } @@ -1008,15 +1085,25 @@ void GraphicsContext::scale(const FloatSize& size) if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->scale(size); + return; + } + cairo_scale(platformContext()->cr(), size.width(), size.height()); m_data->scale(size); } -void GraphicsContext::clipOut(const IntRect& r) +void GraphicsContext::clipOut(const FloatRect& r) { if (paintingDisabled()) return; + if (isRecording()) { + m_displayListRecorder->clipOut(r); + return; + } + cairo_t* cr = platformContext()->cr(); double x1, y1, x2, y2; cairo_clip_extents(cr, &x1, &y1, &x2, &y2); @@ -1028,31 +1115,38 @@ void GraphicsContext::clipOut(const IntRect& r) cairo_set_fill_rule(cr, savedFillRule); } -void GraphicsContext::fillRoundedRect(const IntRect& r, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace) +void GraphicsContext::platformFillRoundedRect(const FloatRoundedRect& rect, const Color& color) { if (paintingDisabled()) return; + ASSERT(!isRecording()); + if (hasShadow()) - platformContext()->shadowBlur().drawRectShadow(this, r, RoundedRect::Radii(topLeft, topRight, bottomLeft, bottomRight)); + platformContext()->shadowBlur().drawRectShadow(*this, rect); cairo_t* cr = platformContext()->cr(); cairo_save(cr); Path path; - path.addRoundedRect(r, topLeft, topRight, bottomLeft, bottomRight); + path.addRoundedRect(rect); appendWebCorePathToCairoContext(cr, path); setSourceRGBAFromColor(cr, color); cairo_fill(cr); cairo_restore(cr); } -void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const RoundedRect& roundedHoleRect, const Color& color, ColorSpace) +void GraphicsContext::fillRectWithRoundedHole(const FloatRect& rect, const FloatRoundedRect& roundedHoleRect, const Color& color) { if (paintingDisabled() || !color.isValid()) return; + if (isRecording()) { + m_displayListRecorder->fillRectWithRoundedHole(rect, roundedHoleRect, color); + return; + } + if (this->mustUseShadowBlur()) - platformContext()->shadowBlur().drawInsetShadow(this, rect, roundedHoleRect.rect(), roundedHoleRect.radii()); + platformContext()->shadowBlur().drawInsetShadow(*this, rect, roundedHoleRect); Path path; path.addRect(rect); @@ -1064,57 +1158,57 @@ void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const Rounded cairo_t* cr = platformContext()->cr(); cairo_save(cr); setPathOnCairoContext(platformContext()->cr(), path.platformPath()->context()); - fillCurrentCairoPath(this); + fillCurrentCairoPath(*this); cairo_restore(cr); } -#if PLATFORM(GTK) -void GraphicsContext::setGdkExposeEvent(GdkEventExpose* expose) +void GraphicsContext::drawPattern(Image& image, const FloatRect& destRect, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, BlendMode blendMode) { - m_data->expose = expose; -} + if (paintingDisabled()) + return; -GdkEventExpose* GraphicsContext::gdkExposeEvent() const -{ - return m_data->expose; -} + if (isRecording()) { + m_displayListRecorder->drawPattern(image, destRect, tileRect, patternTransform, phase, spacing, op, blendMode); + return; + } -GdkWindow* GraphicsContext::gdkWindow() const -{ - if (!m_data->expose) - return 0; + RefPtr<cairo_surface_t> surface = image.nativeImageForCurrentFrame(); + if (!surface) // If it's too early we won't have an image yet. + return; - return m_data->expose->window; + cairo_t* cr = platformContext()->cr(); + drawPatternToCairoContext(cr, surface.get(), IntSize(image.size()), tileRect, patternTransform, phase, toCairoOperator(op, blendMode), destRect); } -#endif void GraphicsContext::setPlatformShouldAntialias(bool enable) { if (paintingDisabled()) return; + ASSERT(!isRecording()); + // When true, use the default Cairo backend antialias mode (usually this // enables standard 'grayscale' antialiasing); false to explicitly disable - // antialiasing. This is the same strategy as used in drawConvexPolygon(). + // antialiasing. cairo_set_antialias(platformContext()->cr(), enable ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE); } -void GraphicsContext::setImageInterpolationQuality(InterpolationQuality quality) +void GraphicsContext::setPlatformImageInterpolationQuality(InterpolationQuality quality) { - platformContext()->setImageInterpolationQuality(quality); -} + ASSERT(!isRecording()); -InterpolationQuality GraphicsContext::imageInterpolationQuality() const -{ - return platformContext()->imageInterpolationQuality(); + platformContext()->setImageInterpolationQuality(quality); } bool GraphicsContext::isAcceleratedContext() const { + if (isRecording()) + return false; + return cairo_surface_get_type(cairo_get_target(platformContext()->cr())) == CAIRO_SURFACE_TYPE_GL; } -#if ENABLE(3D_RENDERING) && USE(TEXTURE_MAPPER) +#if ENABLE(3D_TRANSFORMS) && USE(TEXTURE_MAPPER) TransformationMatrix GraphicsContext::get3DTransform() const { // FIXME: Can we approximate the transformation better than this? @@ -1130,7 +1224,7 @@ void GraphicsContext::set3DTransform(const TransformationMatrix& transform) { setCTM(transform.toAffineTransform()); } -#endif +#endif // ENABLE(3D_TRANSFORMS) && USE(TEXTURE_MAPPER) } // namespace WebCore |