[FTW] Refactor Direct2D code to follow Cairo's model to support modern WebKit
[WebKit-https.git] / Source / WebCore / platform / graphics / displaylists / DisplayListRecorder.cpp
index 366cff7..e797a7f 100644 (file)
 #include "DisplayListItems.h"
 #include "GraphicsContext.h"
 #include "Logging.h"
-#include "TextStream.h"
 #include <wtf/MathExtras.h>
+#include <wtf/text/TextStream.h>
 
 namespace WebCore {
 namespace DisplayList {
 
-Recorder::Recorder(GraphicsContext& context, DisplayList& displayList, const FloatRect& initialClip, const AffineTransform& baseCTM)
-    : m_graphicsContext(context)
+Recorder::Recorder(GraphicsContext& context, DisplayList& displayList, const GraphicsContextState& state, const FloatRect& initialClip, const AffineTransform& baseCTM)
+    : GraphicsContextImpl(context, initialClip, baseCTM)
     , m_displayList(displayList)
 {
     LOG_WITH_STREAM(DisplayLists, stream << "\nRecording with clip " << initialClip);
-
-    // FIXME: Hook up recorder in the GraphicsContext.
-    // m_graphicsContext.setDisplayListRecorder(this);
-    m_stateStack.append(ContextState(baseCTM, initialClip));
+    m_stateStack.append(ContextState(state, baseCTM, initialClip));
 }
 
 Recorder::~Recorder()
@@ -102,41 +99,44 @@ void Recorder::setMiterLimit(float miterLimit)
     appendItem(SetMiterLimit::create(miterLimit));
 }
 
-void Recorder::drawGlyphs(const Font& font, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& startPoint, FontSmoothingMode smoothingMode)
+void Recorder::drawGlyphs(const Font& font, const GlyphBuffer& glyphBuffer, unsigned from, unsigned numGlyphs, const FloatPoint& startPoint, FontSmoothingMode smoothingMode)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawGlyphs::create(font, glyphBuffer.glyphs(from), glyphBuffer.advances(from), numGlyphs, FloatPoint(), toFloatSize(startPoint), smoothingMode)));
     updateItemExtent(newItem);
 }
 
-void Recorder::drawImage(Image& image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
+ImageDrawResult Recorder::drawImage(Image& image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawImage::create(image, destination, source, imagePaintingOptions)));
     updateItemExtent(newItem);
+    return ImageDrawResult::DidRecord;
 }
 
-void Recorder::drawTiledImage(Image& image, const FloatRect& destination, const FloatPoint& source, const FloatSize& tileSize, const FloatSize& spacing, const ImagePaintingOptions& imagePaintingOptions)
+ImageDrawResult Recorder::drawTiledImage(Image& image, const FloatRect& destination, const FloatPoint& source, const FloatSize& tileSize, const FloatSize& spacing, const ImagePaintingOptions& imagePaintingOptions)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawTiledImage::create(image, destination, source, tileSize, spacing, imagePaintingOptions)));
     updateItemExtent(newItem);
+    return ImageDrawResult::DidRecord;
 }
 
-#if USE(CG) || USE(CAIRO)
-void Recorder::drawNativeImage(PassNativeImagePtr imagePtr, const FloatSize& imageSize, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, ImageOrientation orientation)
+ImageDrawResult Recorder::drawTiledImage(Image& image, const FloatRect& destination, const FloatRect& source, const FloatSize& tileScaleFactor, Image::TileRule hRule, Image::TileRule vRule, const ImagePaintingOptions& imagePaintingOptions)
 {
-    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawNativeImage::create(imagePtr, imageSize, destRect, srcRect, op, blendMode, orientation)));
+    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawTiledScaledImage::create(image, destination, source, tileScaleFactor, hRule, vRule, imagePaintingOptions)));
     updateItemExtent(newItem);
+    return ImageDrawResult::DidRecord;
 }
-#endif
 
-void Recorder::drawTiledImage(Image& image, const FloatRect& destination, const FloatRect& source, const FloatSize& tileScaleFactor, Image::TileRule hRule, Image::TileRule vRule, const ImagePaintingOptions& imagePaintingOptions)
+#if USE(CG) || USE(CAIRO) || USE(DIRECT2D)
+void Recorder::drawNativeImage(const NativeImagePtr& image, const FloatSize& imageSize, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, ImageOrientation orientation)
 {
-    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawTiledScaledImage::create(image, destination, source, tileScaleFactor, hRule, vRule, imagePaintingOptions)));
+    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawNativeImage::create(image, imageSize, destRect, srcRect, op, blendMode, orientation)));
     updateItemExtent(newItem);
 }
+#endif
 
-void Recorder::drawPattern(Image& image, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, const FloatRect& destRect, BlendMode blendMode)
+void Recorder::drawPattern(Image& image, const FloatRect& destRect, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, BlendMode blendMode)
 {
-    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawPattern::create(image, tileRect, patternTransform, phase, spacing, op, destRect, blendMode)));
+    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawPattern::create(image, destRect, tileRect, patternTransform, phase, spacing, op, blendMode)));
     updateItemExtent(newItem);
 }
 
@@ -150,10 +150,10 @@ void Recorder::restore()
 {
     if (!m_stateStack.size())
         return;
-    
+
     bool stateUsedForDrawing = currentState().wasUsedForDrawing;
     size_t saveIndex = currentState().saveItemIndex;
-    
+
     m_stateStack.removeLast();
     // Have to avoid eliding nested Save/Restore when a descendant state contains drawing items.
     currentState().wasUsedForDrawing |= stateUsedForDrawing;
@@ -165,7 +165,7 @@ void Recorder::restore()
     }
 
     appendItem(Restore::create());
-    
+
     if (saveIndex) {
         Save& saveItem = downcast<Save>(m_displayList.itemAt(saveIndex));
         saveItem.setRestoreIndex(m_displayList.itemCount() - 1);
@@ -196,6 +196,17 @@ void Recorder::concatCTM(const AffineTransform& transform)
     appendItem(ConcatenateCTM::create(transform));
 }
 
+void Recorder::setCTM(const AffineTransform&)
+{
+    WTFLogAlways("GraphicsContext::setCTM() is not compatible with DisplayList::Recorder.");
+}
+
+AffineTransform Recorder::getCTM(GraphicsContext::IncludeDeviceScale)
+{
+    WTFLogAlways("GraphicsContext::getCTM() is not yet compatible with DisplayList::Recorder.");
+    return { };
+}
+
 void Recorder::beginTransparencyLayer(float opacity)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(BeginTransparencyLayer::create(opacity)));
@@ -219,15 +230,15 @@ void Recorder::drawLine(const FloatPoint& point1, const FloatPoint& point2)
     updateItemExtent(newItem);
 }
 
-void Recorder::drawLinesForText(const FloatPoint& point, const DashArray& widths, bool printing, bool doubleLines, float strokeThickness)
+void Recorder::drawLinesForText(const FloatPoint& point, float thickness, const DashArray& widths, bool printing, bool doubleLines)
 {
-    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawLinesForText::create(FloatPoint(), toFloatSize(point), widths, printing, doubleLines, strokeThickness)));
+    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawLinesForText::create(FloatPoint(), toFloatSize(point), thickness, widths, printing, doubleLines)));
     updateItemExtent(newItem);
 }
 
-void Recorder::drawLineForDocumentMarker(const FloatPoint& point, float width, GraphicsContext::DocumentMarkerLineStyle style)
+void Recorder::drawDotsForDocumentMarker(const FloatRect& rect, DocumentMarkerLineStyle style)
 {
-    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawLineForDocumentMarker::create(point, width, style)));
+    DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawDotsForDocumentMarker::create(rect, style)));
     updateItemExtent(newItem);
 }
 
@@ -243,13 +254,13 @@ void Recorder::drawPath(const Path& path)
     updateItemExtent(newItem);
 }
 
-void Recorder::drawFocusRing(const Path& path, int width, int offset, const Color& color)
+void Recorder::drawFocusRing(const Path& path, float width, float offset, const Color& color)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawFocusRingPath::create(path, width, offset, color)));
     updateItemExtent(newItem);
 }
 
-void Recorder::drawFocusRing(const Vector<IntRect>& rects, int width, int offset, const Color& color)
+void Recorder::drawFocusRing(const Vector<FloatRect>& rects, float width, float offset, const Color& color)
 {
     DrawingItem& newItem = downcast<DrawingItem>(appendItem(DrawFocusRingRects::create(rects, width, offset, color)));
     updateItemExtent(newItem);
@@ -361,6 +372,17 @@ void Recorder::clipPath(const Path& path, WindRule windRule)
     appendItem(ClipPath::create(path, windRule));
 }
 
+IntRect Recorder::clipBounds()
+{
+    WTFLogAlways("Getting the clip bounds not yet supported with DisplayList::Recorder.");
+    return IntRect(-2048, -2048, 4096, 4096);
+}
+
+void Recorder::clipToImageBuffer(ImageBuffer&, const FloatRect&)
+{
+    WTFLogAlways("GraphicsContext::clipToImageBuffer is not compatible with DisplayList::Recorder.");
+}
+
 void Recorder::applyDeviceScaleFactor(float deviceScaleFactor)
 {
     // FIXME: this changes the baseCTM, which will invalidate all of our cached extents.
@@ -368,6 +390,12 @@ void Recorder::applyDeviceScaleFactor(float deviceScaleFactor)
     appendItem(ApplyDeviceScaleFactor::create(deviceScaleFactor));
 }
 
+FloatRect Recorder::roundToDevicePixels(const FloatRect& rect, GraphicsContext::RoundingMode)
+{
+    WTFLogAlways("GraphicsContext::roundToDevicePixels() is not yet compatible with DisplayList::Recorder.");
+    return rect;
+}
+
 Item& Recorder::appendItem(Ref<Item>&& item)
 {
     willAppendItem(item.get());
@@ -376,7 +404,7 @@ Item& Recorder::appendItem(Ref<Item>&& item)
 
 void Recorder::updateItemExtent(DrawingItem& item) const
 {
-    if (Optional<FloatRect> rect = item.localBounds(m_graphicsContext))
+    if (Optional<FloatRect> rect = item.localBounds(graphicsContext()))
         item.setExtent(extentFromLocalBounds(rect.value()));
 }
 
@@ -398,7 +426,7 @@ FloatRect Recorder::extentFromLocalBounds(const FloatRect& rect) const
     FloatSize shadowOffset;
     float shadowRadius;
     Color shadowColor;
-    if (m_graphicsContext.getShadow(shadowOffset, shadowRadius, shadowColor)) {
+    if (graphicsContext().getShadow(shadowOffset, shadowRadius, shadowColor)) {
         FloatRect shadowExtent= bounds;
         shadowExtent.move(shadowOffset);
         shadowExtent.inflate(shadowPaintingExtent(shadowRadius));