1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "DisplayList.h"
29#include "GraphicsContextImpl.h"
30#include "Image.h" // For Image::TileRule.
31#include "TextFlags.h"
32#include <wtf/Noncopyable.h>
33
34namespace WebCore {
35
36class FloatPoint;
37class FloatRect;
38class GlyphBuffer;
39class FloatPoint;
40class Font;
41class Image;
42
43struct GraphicsContextState;
44struct ImagePaintingOptions;
45
46namespace DisplayList {
47
48class DrawingItem;
49
50class Recorder : public GraphicsContextImpl {
51 WTF_MAKE_FAST_ALLOCATED;
52 WTF_MAKE_NONCOPYABLE(Recorder);
53public:
54 Recorder(GraphicsContext&, DisplayList&, const GraphicsContextState&, const FloatRect& initialClip, const AffineTransform&);
55 virtual ~Recorder();
56
57 size_t itemCount() const { return m_displayList.itemCount(); }
58
59private:
60 bool hasPlatformContext() const override { return false; }
61 PlatformGraphicsContext* platformContext() const override { return nullptr; }
62
63 void updateState(const GraphicsContextState&, GraphicsContextState::StateChangeFlags) override;
64 void clearShadow() override;
65
66 void setLineCap(LineCap) override;
67 void setLineDash(const DashArray&, float dashOffset) override;
68 void setLineJoin(LineJoin) override;
69 void setMiterLimit(float) override;
70
71 void fillRect(const FloatRect&) override;
72 void fillRect(const FloatRect&, const Color&) override;
73 void fillRect(const FloatRect&, Gradient&) override;
74 void fillRect(const FloatRect&, const Color&, CompositeOperator, BlendMode) override;
75 void fillRoundedRect(const FloatRoundedRect&, const Color&, BlendMode) override;
76 void fillRectWithRoundedHole(const FloatRect&, const FloatRoundedRect& roundedHoleRect, const Color&) override;
77 void fillPath(const Path&) override;
78 void fillEllipse(const FloatRect&) override;
79 void strokeRect(const FloatRect&, float lineWidth) override;
80 void strokePath(const Path&) override;
81 void strokeEllipse(const FloatRect&) override;
82 void clearRect(const FloatRect&) override;
83
84#if USE(CG)
85 void applyStrokePattern() override;
86 void applyFillPattern() override;
87#endif
88
89 void drawGlyphs(const Font&, const GlyphBuffer&, unsigned from, unsigned numGlyphs, const FloatPoint& anchorPoint, FontSmoothingMode) override;
90
91 ImageDrawResult drawImage(Image&, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions&) override;
92 ImageDrawResult drawTiledImage(Image&, const FloatRect& destination, const FloatPoint& source, const FloatSize& tileSize, const FloatSize& spacing, const ImagePaintingOptions&) override;
93 ImageDrawResult drawTiledImage(Image&, const FloatRect& destination, const FloatRect& source, const FloatSize& tileScaleFactor, Image::TileRule hRule, Image::TileRule vRule, const ImagePaintingOptions&) override;
94#if USE(CG) || USE(CAIRO)
95 void drawNativeImage(const NativeImagePtr&, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator, BlendMode, ImageOrientation) override;
96#endif
97 void drawPattern(Image&, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform&, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, BlendMode = BlendMode::Normal) override;
98
99 void drawRect(const FloatRect&, float borderThickness) override;
100 void drawLine(const FloatPoint&, const FloatPoint&) override;
101 void drawLinesForText(const FloatPoint&, float thickness, const DashArray& widths, bool printing, bool doubleLines) override;
102 void drawDotsForDocumentMarker(const FloatRect&, DocumentMarkerLineStyle) override;
103 void drawEllipse(const FloatRect&) override;
104 void drawPath(const Path&) override;
105
106 void drawFocusRing(const Path&, float width, float offset, const Color&) override;
107 void drawFocusRing(const Vector<FloatRect>&, float width, float offset, const Color&) override;
108
109 void save() override;
110 void restore() override;
111
112 void translate(float x, float y) override;
113 void rotate(float angleInRadians) override;
114 void scale(const FloatSize&) override;
115 void concatCTM(const AffineTransform&) override;
116 void setCTM(const AffineTransform&) override;
117 AffineTransform getCTM(GraphicsContext::IncludeDeviceScale) override;
118
119 void beginTransparencyLayer(float opacity) override;
120 void endTransparencyLayer() override;
121
122 void clip(const FloatRect&) override;
123 void clipOut(const FloatRect&) override;
124 void clipOut(const Path&) override;
125 void clipPath(const Path&, WindRule) override;
126 IntRect clipBounds() override;
127 void clipToImageBuffer(ImageBuffer&, const FloatRect&) override;
128
129 void applyDeviceScaleFactor(float) override;
130
131 FloatRect roundToDevicePixels(const FloatRect&, GraphicsContext::RoundingMode) override;
132
133 Item& appendItem(Ref<Item>&&);
134 void willAppendItem(const Item&);
135
136 FloatRect extentFromLocalBounds(const FloatRect&) const;
137 void updateItemExtent(DrawingItem&) const;
138
139 const AffineTransform& ctm() const;
140 const FloatRect& clipBounds() const;
141
142 struct ContextState {
143 AffineTransform ctm;
144 FloatRect clipBounds;
145 GraphicsContextStateChange stateChange;
146 GraphicsContextState lastDrawingState;
147 bool wasUsedForDrawing { false };
148 size_t saveItemIndex { 0 };
149
150 ContextState(const GraphicsContextState& state, const AffineTransform& transform, const FloatRect& clip)
151 : ctm(transform)
152 , clipBounds(clip)
153 , lastDrawingState(state)
154 {
155 }
156
157 ContextState cloneForSave(size_t saveIndex) const
158 {
159 ContextState state(lastDrawingState, ctm, clipBounds);
160 state.stateChange = stateChange;
161 state.saveItemIndex = saveIndex;
162 return state;
163 }
164
165 void translate(float x, float y);
166 void rotate(float angleInRadians);
167 void scale(const FloatSize&);
168 void concatCTM(const AffineTransform&);
169 };
170
171 const ContextState& currentState() const;
172 ContextState& currentState();
173
174 DisplayList& m_displayList;
175
176 Vector<ContextState, 32> m_stateStack;
177};
178
179}
180}
181
182