Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / graphics / GraphicsContext.h
blob2006b821ac43093176431c3a8999bd41ce09eccf
1 /*
2 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008-2009 Torch Mobile, Inc.
4 * Copyright (C) 2013 Google Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef GraphicsContext_h
29 #define GraphicsContext_h
31 #include "platform/PlatformExport.h"
32 #include "platform/fonts/Font.h"
33 #include "platform/geometry/FloatRect.h"
34 #include "platform/geometry/FloatRoundedRect.h"
35 #include "platform/graphics/DashArray.h"
36 #include "platform/graphics/DrawLooperBuilder.h"
37 #include "platform/graphics/ImageOrientation.h"
38 #include "platform/graphics/GraphicsContextState.h"
39 #include "platform/graphics/skia/SkiaUtils.h"
40 #include "third_party/skia/include/core/SkMetaData.h"
41 #include "third_party/skia/include/core/SkPictureRecorder.h"
42 #include "third_party/skia/include/core/SkRegion.h"
43 #include "wtf/FastAllocBase.h"
44 #include "wtf/Forward.h"
45 #include "wtf/Noncopyable.h"
46 #include "wtf/PassOwnPtr.h"
48 class SkBitmap;
49 class SkImage;
50 class SkPaint;
51 class SkPath;
52 class SkPicture;
53 class SkRRect;
54 class SkTextBlob;
55 struct SkImageInfo;
56 struct SkRect;
58 namespace blink {
60 class DisplayItemList;
61 class ImageBuffer;
62 class KURL;
64 class PLATFORM_EXPORT GraphicsContext {
65 WTF_MAKE_NONCOPYABLE(GraphicsContext); WTF_MAKE_FAST_ALLOCATED(GraphicsContext);
66 public:
67 enum DisabledMode {
68 NothingDisabled = 0, // Run as normal.
69 FullyDisabled = 1 // Do absolutely minimal work to remove the cost of the context from performance tests.
72 explicit GraphicsContext(DisplayItemList*, DisabledMode = NothingDisabled, SkMetaData* = 0);
74 ~GraphicsContext();
76 SkCanvas* canvas() { return m_canvas; }
77 const SkCanvas* canvas() const { return m_canvas; }
79 DisplayItemList* displayItemList() { return m_displayItemList; }
81 bool contextDisabled() const { return m_disabledState; }
83 // ---------- State management methods -----------------
84 void save();
85 void restore();
87 #if ENABLE(ASSERT)
88 unsigned saveCount() const;
89 #endif
91 float strokeThickness() const { return immutableState()->strokeData().thickness(); }
92 void setStrokeThickness(float thickness) { mutableState()->setStrokeThickness(thickness); }
94 StrokeStyle strokeStyle() const { return immutableState()->strokeData().style(); }
95 void setStrokeStyle(StrokeStyle style) { mutableState()->setStrokeStyle(style); }
97 Color strokeColor() const { return immutableState()->strokeColor(); }
98 void setStrokeColor(const Color& color) { mutableState()->setStrokeColor(color); }
100 Gradient* strokeGradient() const { return immutableState()->strokeGradient(); }
101 void setStrokeGradient(PassRefPtr<Gradient>, float alpha = 1);
103 void setLineCap(LineCap cap) { mutableState()->setLineCap(cap); }
104 void setLineDash(const DashArray& dashes, float dashOffset) { mutableState()->setLineDash(dashes, dashOffset); }
105 void setLineJoin(LineJoin join) { mutableState()->setLineJoin(join); }
106 void setMiterLimit(float limit) { mutableState()->setMiterLimit(limit); }
108 Color fillColor() const { return immutableState()->fillColor(); }
109 void setFillColor(const Color& color) { mutableState()->setFillColor(color); }
111 void setFillGradient(PassRefPtr<Gradient>, float alpha = 1);
113 void setShouldAntialias(bool antialias) { mutableState()->setShouldAntialias(antialias); }
114 bool shouldAntialias() const { return immutableState()->shouldAntialias(); }
116 void setTextDrawingMode(TextDrawingModeFlags mode) { mutableState()->setTextDrawingMode(mode); }
117 TextDrawingModeFlags textDrawingMode() const { return immutableState()->textDrawingMode(); }
119 void setImageInterpolationQuality(InterpolationQuality quality) { mutableState()->setInterpolationQuality(quality); }
120 InterpolationQuality imageInterpolationQuality() const { return immutableState()->interpolationQuality(); }
122 // Specify the device scale factor which may change the way document markers
123 // and fonts are rendered.
124 void setDeviceScaleFactor(float factor) { m_deviceScaleFactor = factor; }
125 float deviceScaleFactor() const { return m_deviceScaleFactor; }
127 // Returns if the context is a printing context instead of a display
128 // context. Bitmap shouldn't be resampled when printing to keep the best
129 // possible quality.
130 bool printing() const { return m_printing; }
131 void setPrinting(bool printing) { m_printing = printing; }
133 SkColorFilter* colorFilter() const;
134 void setColorFilter(ColorFilter);
135 // ---------- End state management methods -----------------
137 // These draw methods will do both stroking and filling.
138 // FIXME: ...except drawRect(), which fills properly but always strokes
139 // using a 1-pixel stroke inset from the rect borders (of the correct
140 // stroke color).
141 void drawRect(const IntRect&);
142 void drawLine(const IntPoint&, const IntPoint&);
144 void fillPolygon(size_t numPoints, const FloatPoint*, const Color&, bool shouldAntialias);
146 void fillPath(const Path&);
147 void strokePath(const Path&);
149 void fillEllipse(const FloatRect&);
150 void strokeEllipse(const FloatRect&);
152 void fillRect(const FloatRect&);
153 void fillRect(const FloatRect&, const Color&, SkXfermode::Mode = SkXfermode::kSrcOver_Mode);
154 void fillRoundedRect(const FloatRoundedRect&, const Color&);
155 void fillDRRect(const FloatRoundedRect&, const FloatRoundedRect&, const Color&);
157 void clearRect(const FloatRect&);
159 void strokeRect(const FloatRect&);
160 void strokeRect(const FloatRect&, float lineWidth);
162 void drawPicture(const SkPicture*);
163 void compositePicture(SkPicture*, const FloatRect& dest, const FloatRect& src, SkXfermode::Mode);
165 void drawImage(Image*, const IntRect&, SkXfermode::Mode = SkXfermode::kSrcOver_Mode, RespectImageOrientationEnum = DoNotRespectImageOrientation);
166 void drawImage(Image*, const FloatRect& destRect, const FloatRect& srcRect, SkXfermode::Mode = SkXfermode::kSrcOver_Mode, RespectImageOrientationEnum = DoNotRespectImageOrientation);
168 void drawTiledImage(Image*, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
169 SkXfermode::Mode = SkXfermode::kSrcOver_Mode, const IntSize& repeatSpacing = IntSize());
170 void drawTiledImage(Image*, const IntRect& destRect, const IntRect& srcRect,
171 const FloatSize& tileScaleFactor, Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
172 SkXfermode::Mode = SkXfermode::kSrcOver_Mode);
174 // These methods write to the canvas.
175 // Also drawLine(const IntPoint& point1, const IntPoint& point2) and fillRoundedRect
176 void drawOval(const SkRect&, const SkPaint&);
177 void drawPath(const SkPath&, const SkPaint&);
178 void drawRect(const SkRect&, const SkPaint&);
180 void clip(const IntRect& rect) { clipRect(rect); }
181 void clip(const FloatRect& rect) { clipRect(rect); }
182 void clipRoundedRect(const FloatRoundedRect&, SkRegion::Op = SkRegion::kIntersect_Op);
183 void clipOut(const IntRect& rect) { clipRect(rect, NotAntiAliased, SkRegion::kDifference_Op); }
184 void clipOut(const FloatRect& rect) { clipRect(rect, NotAntiAliased, SkRegion::kDifference_Op); }
185 void clipOut(const Path&);
186 void clipOutRoundedRect(const FloatRoundedRect&);
187 void clipPath(const SkPath&, AntiAliasingMode = NotAntiAliased, SkRegion::Op = SkRegion::kIntersect_Op);
188 void clipPolygon(size_t numPoints, const FloatPoint*, bool antialias);
189 void clipRect(const SkRect&, AntiAliasingMode = NotAntiAliased, SkRegion::Op = SkRegion::kIntersect_Op);
191 void drawText(const Font&, const TextRunPaintInfo&, const FloatPoint&);
192 void drawText(const Font&, const TextRunPaintInfo&, const FloatPoint&, const SkPaint&);
193 void drawEmphasisMarks(const Font&, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&);
194 void drawBidiText(const Font&, const TextRunPaintInfo&, const FloatPoint&, Font::CustomFontNotReadyAction = Font::DoNotPaintIfFontNotReady);
195 void drawHighlightForText(const Font&, const TextRun&, const FloatPoint&, int h, const Color& backgroundColor, int from = 0, int to = -1);
197 void drawLineForText(const FloatPoint&, float width, bool printing);
198 enum DocumentMarkerLineStyle {
199 DocumentMarkerSpellingLineStyle,
200 DocumentMarkerGrammarLineStyle
202 void drawLineForDocumentMarker(const FloatPoint&, float width, DocumentMarkerLineStyle);
204 // beginLayer()/endLayer() behaves like save()/restore() for CTM and clip states.
205 // Apply SkXfermode::Mode when the layer is composited on the backdrop (i.e. endLayer()).
206 void beginLayer(float opacity = 1.0f, SkXfermode::Mode = SkXfermode::kSrcOver_Mode,
207 const FloatRect* = 0, ColorFilter = ColorFilterNone, SkImageFilter* = 0);
208 void endLayer();
210 // Instead of being dispatched to the active canvas, draw commands following beginRecording()
211 // are stored in a display list that can be replayed at a later time. Pass in the bounding
212 // rectangle for the content in the list.
213 void beginRecording(const FloatRect&);
214 PassRefPtr<const SkPicture> endRecording();
216 void setShadow(const FloatSize& offset, float blur, const Color&,
217 DrawLooperBuilder::ShadowTransformMode = DrawLooperBuilder::ShadowRespectsTransforms,
218 DrawLooperBuilder::ShadowAlphaMode = DrawLooperBuilder::ShadowRespectsAlpha, ShadowMode = DrawShadowAndForeground);
220 // It is assumed that this draw looper is used only for shadows
221 // (i.e. a draw looper is set if and only if there is a shadow).
222 // The builder passed into this method will be destroyed.
223 void setDrawLooper(PassOwnPtr<DrawLooperBuilder>);
224 void clearDrawLooper();
226 void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
227 void drawFocusRing(const Path&, int width, int offset, const Color&);
229 enum Edge {
230 NoEdge = 0,
231 TopEdge = 1 << 1,
232 RightEdge = 1 << 2,
233 BottomEdge = 1 << 3,
234 LeftEdge = 1 << 4
236 typedef unsigned Edges;
237 void drawInnerShadow(const FloatRoundedRect&, const Color& shadowColor, const IntSize shadowOffset, int shadowBlur, int shadowSpread, Edges clippedEdges = NoEdge);
239 const SkPaint& fillPaint() const { return immutableState()->fillPaint(); }
241 // ---------- Transformation methods -----------------
242 void concatCTM(const AffineTransform&);
244 void scale(float x, float y);
245 void rotate(float angleInRadians);
246 void translate(float x, float y);
247 // ---------- End transformation methods -----------------
249 SkFilterQuality computeFilterQuality(Image*, const FloatRect& dest, const FloatRect& src) const;
251 // URL drawing
252 void setURLForRect(const KURL&, const IntRect&);
253 void setURLFragmentForRect(const String& name, const IntRect&);
255 static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle);
257 static int focusRingOutsetExtent(int offset, int width)
259 // Unlike normal outlines (whole width is outside of the offset), focus rings are drawn with the
260 // center of the path aligned with the offset, so only half of the width is outside of the offset.
261 return focusRingOffset(offset) + (focusRingWidth(width) + 1) / 2;
264 #if OS(MACOSX)
265 static int focusRingWidth(int width) { return width; }
266 #else
267 static int focusRingWidth(int width) { return 1; }
268 #endif
270 #if ENABLE(ASSERT)
271 void setInDrawingRecorder(bool);
272 #endif
274 static PassRefPtr<SkColorFilter> WebCoreColorFilterToSkiaColorFilter(ColorFilter);
276 private:
277 const GraphicsContextState* immutableState() const { return m_paintState; }
279 GraphicsContextState* mutableState()
281 realizePaintSave();
282 return m_paintState;
285 template<typename DrawTextFunc>
286 void drawTextPasses(const DrawTextFunc&);
288 static void setPathFromPoints(SkPath*, size_t, const FloatPoint*);
290 #if OS(MACOSX)
291 static inline int focusRingOffset(int offset) { return offset + 2; }
292 #else
293 static inline int focusRingOffset(int offset) { return 0; }
294 static SkPMColor lineColors(int);
295 static SkPMColor antiColors1(int);
296 static SkPMColor antiColors2(int);
297 static void draw1xMarker(SkBitmap*, int);
298 static void draw2xMarker(SkBitmap*, int);
299 #endif
301 void saveLayer(const SkRect* bounds, const SkPaint*);
302 void restoreLayer();
304 // Helpers for drawing a focus ring (drawFocusRing)
305 void drawFocusRingPath(const SkPath&, const Color&, int width);
306 void drawFocusRingRect(const SkRect&, const Color&, int width);
308 // SkCanvas wrappers.
309 void clipRRect(const SkRRect&, AntiAliasingMode = NotAntiAliased, SkRegion::Op = SkRegion::kIntersect_Op);
310 void concat(const SkMatrix&);
311 void drawRRect(const SkRRect&, const SkPaint&);
313 // Apply deferred paint state saves
314 void realizePaintSave()
316 if (contextDisabled())
317 return;
319 if (m_paintState->saveCount()) {
320 m_paintState->decrementSaveCount();
321 ++m_paintStateIndex;
322 if (m_paintStateStack.size() == m_paintStateIndex) {
323 m_paintStateStack.append(GraphicsContextState::createAndCopy(*m_paintState));
324 m_paintState = m_paintStateStack[m_paintStateIndex].get();
325 } else {
326 GraphicsContextState* priorPaintState = m_paintState;
327 m_paintState = m_paintStateStack[m_paintStateIndex].get();
328 m_paintState->copy(*priorPaintState);
333 void fillRectWithRoundedHole(const FloatRect&, const FloatRoundedRect& roundedHoleRect, const Color&);
335 bool isRecording() const;
337 const SkMetaData& metaData() const { return m_metaData; }
339 // null indicates painting is contextDisabled. Never delete this object.
340 SkCanvas* m_canvas;
342 // This stores the canvas object used to construct the GraphicsContext, if any. It is only
343 // used when Slimming Paint is active.
344 SkCanvas* m_originalCanvas;
346 // This being null indicates not to paint into a DisplayItemList, and instead directly into the canvas.
347 DisplayItemList* m_displayItemList;
349 // Paint states stack. Enables local drawing state change with save()/restore() calls.
350 // This state controls the appearance of drawn content.
351 // We do not delete from this stack to avoid memory churn.
352 Vector<OwnPtr<GraphicsContextState>> m_paintStateStack;
353 // Current index on the stack. May not be the last thing on the stack.
354 unsigned m_paintStateIndex;
355 // Raw pointer to the current state.
356 GraphicsContextState* m_paintState;
358 SkPictureRecorder m_pictureRecorder;
360 SkMetaData m_metaData;
362 #if ENABLE(ASSERT)
363 unsigned m_layerCount;
364 bool m_disableDestructionChecks;
365 bool m_inDrawingRecorder;
366 #endif
368 const DisabledMode m_disabledState;
370 float m_deviceScaleFactor;
372 unsigned m_printing : 1;
373 unsigned m_hasMetaData : 1;
376 } // namespace blink
378 #endif // GraphicsContext_h