File: GraphicsContext.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (471 lines) | stat: -rw-r--r-- 17,222 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 * Copyright (C) 2008-2009 Torch Mobile, Inc.
 * Copyright (C) 2013 Google 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 COMPUTER, 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
 * 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.
 */

#ifndef GraphicsContext_h
#define GraphicsContext_h

#include "platform/PlatformExport.h"
#include "platform/fonts/Font.h"
#include "platform/graphics/ColorBehavior.h"
#include "platform/graphics/DashArray.h"
#include "platform/graphics/DrawLooperBuilder.h"
#include "platform/graphics/GraphicsContextState.h"
#include "platform/graphics/ImageOrientation.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "third_party/skia/include/core/SkClipOp.h"
#include "third_party/skia/include/core/SkMetaData.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "wtf/Allocator.h"
#include "wtf/Forward.h"
#include "wtf/Noncopyable.h"
#include <memory>

class SkBitmap;
class SkPaint;
class SkPath;
class SkPicture;
class SkRRect;
struct SkRect;

namespace blink {

class FloatRect;
class FloatRoundedRect;
class KURL;
class PaintController;
class Path;

class PLATFORM_EXPORT GraphicsContext {
  WTF_MAKE_NONCOPYABLE(GraphicsContext);
  USING_FAST_MALLOC(GraphicsContext);

 public:
  enum DisabledMode {
    NothingDisabled = 0,  // Run as normal.
    FullyDisabled = 1     // Do absolutely minimal work to remove the cost of
                          // the context from performance tests.
  };

  explicit GraphicsContext(
      PaintController&,
      DisabledMode = NothingDisabled,
      SkMetaData* = 0,
      ColorBehavior = ColorBehavior::transformToGlobalTarget());

  ~GraphicsContext();

  SkCanvas* canvas() { return m_canvas; }
  const SkCanvas* canvas() const { return m_canvas; }

  PaintController& getPaintController() { return m_paintController; }
  const ColorBehavior& getColorBehavior() const { return m_colorBehavior; }

  bool contextDisabled() const { return m_disabledState; }

  // ---------- State management methods -----------------
  void save();
  void restore();

#if DCHECK_IS_ON()
  unsigned saveCount() const;
#endif

  float strokeThickness() const {
    return immutableState()->getStrokeData().thickness();
  }
  void setStrokeThickness(float thickness) {
    mutableState()->setStrokeThickness(thickness);
  }

  StrokeStyle getStrokeStyle() const {
    return immutableState()->getStrokeData().style();
  }
  void setStrokeStyle(StrokeStyle style) {
    mutableState()->setStrokeStyle(style);
  }

  Color strokeColor() const { return immutableState()->strokeColor(); }
  void setStrokeColor(const Color& color) {
    mutableState()->setStrokeColor(color);
  }

  void setLineCap(LineCap cap) { mutableState()->setLineCap(cap); }
  void setLineDash(const DashArray& dashes, float dashOffset) {
    mutableState()->setLineDash(dashes, dashOffset);
  }
  void setLineJoin(LineJoin join) { mutableState()->setLineJoin(join); }
  void setMiterLimit(float limit) { mutableState()->setMiterLimit(limit); }

  Color fillColor() const { return immutableState()->fillColor(); }
  void setFillColor(const Color& color) { mutableState()->setFillColor(color); }

  void setShouldAntialias(bool antialias) {
    mutableState()->setShouldAntialias(antialias);
  }
  bool shouldAntialias() const { return immutableState()->shouldAntialias(); }

  void setTextDrawingMode(TextDrawingModeFlags mode) {
    mutableState()->setTextDrawingMode(mode);
  }
  TextDrawingModeFlags textDrawingMode() const {
    return immutableState()->textDrawingMode();
  }

  void setImageInterpolationQuality(InterpolationQuality quality) {
    mutableState()->setInterpolationQuality(quality);
  }
  InterpolationQuality imageInterpolationQuality() const {
    return immutableState()->getInterpolationQuality();
  }

  // Specify the device scale factor which may change the way document markers
  // and fonts are rendered.
  void setDeviceScaleFactor(float factor) { m_deviceScaleFactor = factor; }
  float deviceScaleFactor() const { return m_deviceScaleFactor; }

  // Returns if the context is a printing context instead of a display
  // context. Bitmap shouldn't be resampled when printing to keep the best
  // possible quality.
  bool printing() const { return m_printing; }
  void setPrinting(bool printing) { m_printing = printing; }

  SkColorFilter* getColorFilter() const;
  void setColorFilter(ColorFilter);
  // ---------- End state management methods -----------------

  // These draw methods will do both stroking and filling.
  // FIXME: ...except drawRect(), which fills properly but always strokes
  // using a 1-pixel stroke inset from the rect borders (of the correct
  // stroke color).
  void drawRect(const IntRect&);
  void drawLine(const IntPoint&, const IntPoint&);

  void fillPath(const Path&);
  void strokePath(const Path&);

  void fillEllipse(const FloatRect&);
  void strokeEllipse(const FloatRect&);

  void fillRect(const FloatRect&);
  void fillRect(const FloatRect&,
                const Color&,
                SkBlendMode = SkBlendMode::kSrcOver);
  void fillRoundedRect(const FloatRoundedRect&, const Color&);
  void fillDRRect(const FloatRoundedRect&,
                  const FloatRoundedRect&,
                  const Color&);

  void strokeRect(const FloatRect&, float lineWidth);

  void drawPicture(const SkPicture*);
  void compositePicture(sk_sp<SkPicture>,
                        const FloatRect& dest,
                        const FloatRect& src,
                        SkBlendMode);

  void drawImage(Image*,
                 const FloatRect& destRect,
                 const FloatRect* srcRect = nullptr,
                 SkBlendMode = SkBlendMode::kSrcOver,
                 RespectImageOrientationEnum = DoNotRespectImageOrientation);
  void drawImageRRect(
      Image*,
      const FloatRoundedRect& dest,
      const FloatRect& srcRect,
      SkBlendMode = SkBlendMode::kSrcOver,
      RespectImageOrientationEnum = DoNotRespectImageOrientation);
  void drawTiledImage(Image*,
                      const FloatRect& destRect,
                      const FloatPoint& srcPoint,
                      const FloatSize& tileSize,
                      SkBlendMode = SkBlendMode::kSrcOver,
                      const FloatSize& repeatSpacing = FloatSize());
  void drawTiledImage(Image*,
                      const FloatRect& destRect,
                      const FloatRect& srcRect,
                      const FloatSize& tileScaleFactor,
                      Image::TileRule hRule = Image::StretchTile,
                      Image::TileRule vRule = Image::StretchTile,
                      SkBlendMode = SkBlendMode::kSrcOver);

  // These methods write to the canvas.
  // Also drawLine(const IntPoint& point1, const IntPoint& point2) and
  // fillRoundedRect().
  void drawOval(const SkRect&, const SkPaint&);
  void drawPath(const SkPath&, const SkPaint&);
  void drawRect(const SkRect&, const SkPaint&);
  void drawRRect(const SkRRect&, const SkPaint&);

  void clip(const IntRect& rect) { clipRect(rect); }
  void clip(const FloatRect& rect) { clipRect(rect); }
  void clipRoundedRect(const FloatRoundedRect&,
                       SkClipOp = SkClipOp::kIntersect,
                       AntiAliasingMode = AntiAliased);
  void clipOut(const IntRect& rect) {
    clipRect(rect, NotAntiAliased, SkClipOp::kDifference);
  }
  void clipOut(const FloatRect& rect) {
    clipRect(rect, NotAntiAliased, SkClipOp::kDifference);
  }
  void clipOut(const Path&);
  void clipOutRoundedRect(const FloatRoundedRect&);
  void clipPath(const SkPath&,
                AntiAliasingMode = NotAntiAliased,
                SkClipOp = SkClipOp::kIntersect);
  void clipRect(const SkRect&,
                AntiAliasingMode = NotAntiAliased,
                SkClipOp = SkClipOp::kIntersect);

  void drawText(const Font&, const TextRunPaintInfo&, const FloatPoint&);
  void drawText(const Font&,
                const TextRunPaintInfo&,
                const FloatPoint&,
                const SkPaint&);
  void drawEmphasisMarks(const Font&,
                         const TextRunPaintInfo&,
                         const AtomicString& mark,
                         const FloatPoint&);
  void drawBidiText(
      const Font&,
      const TextRunPaintInfo&,
      const FloatPoint&,
      Font::CustomFontNotReadyAction = Font::DoNotPaintIfFontNotReady);
  void drawHighlightForText(const Font&,
                            const TextRun&,
                            const FloatPoint&,
                            int h,
                            const Color& backgroundColor,
                            int from = 0,
                            int to = -1);

  void drawLineForText(const FloatPoint&, float width);
  enum DocumentMarkerLineStyle {
    DocumentMarkerSpellingLineStyle,
    DocumentMarkerGrammarLineStyle
  };
  void drawLineForDocumentMarker(const FloatPoint&,
                                 float width,
                                 DocumentMarkerLineStyle);

  // beginLayer()/endLayer() behave like save()/restore() for CTM and clip
  // states. Apply SkBlendMode when the layer is composited on the backdrop
  // (i.e. endLayer()).
  void beginLayer(float opacity = 1.0f,
                  SkBlendMode = SkBlendMode::kSrcOver,
                  const FloatRect* = 0,
                  ColorFilter = ColorFilterNone,
                  sk_sp<SkImageFilter> = nullptr);
  void endLayer();

  // Instead of being dispatched to the active canvas, draw commands following
  // beginRecording() are stored in a display list that can be replayed at a
  // later time. Pass in the bounding rectangle for the content in the list.
  void beginRecording(const FloatRect&);

  // Returns a picture with any recorded draw commands since the prerequisite
  // call to beginRecording().  The picture is guaranteed to be non-null (but
  // not necessarily non-empty), even when the context is disabled.
  sk_sp<SkPicture> endRecording();

  void setShadow(const FloatSize& offset,
                 float blur,
                 const Color&,
                 DrawLooperBuilder::ShadowTransformMode =
                     DrawLooperBuilder::ShadowRespectsTransforms,
                 DrawLooperBuilder::ShadowAlphaMode =
                     DrawLooperBuilder::ShadowRespectsAlpha,
                 ShadowMode = DrawShadowAndForeground);

  void setDrawLooper(sk_sp<SkDrawLooper>);

  void drawFocusRing(const Vector<IntRect>&,
                     float width,
                     int offset,
                     const Color&);
  void drawFocusRing(const Path&, float width, int offset, const Color&);

  enum Edge {
    NoEdge = 0,
    TopEdge = 1 << 1,
    RightEdge = 1 << 2,
    BottomEdge = 1 << 3,
    LeftEdge = 1 << 4
  };
  typedef unsigned Edges;
  void drawInnerShadow(const FloatRoundedRect&,
                       const Color& shadowColor,
                       const FloatSize& shadowOffset,
                       float shadowBlur,
                       float shadowSpread,
                       Edges clippedEdges = NoEdge);

  const SkPaint& fillPaint() const { return immutableState()->fillPaint(); }
  const SkPaint& strokePaint() const { return immutableState()->strokePaint(); }

  // ---------- Transformation methods -----------------
  void concatCTM(const AffineTransform&);

  void scale(float x, float y);
  void rotate(float angleInRadians);
  void translate(float x, float y);
  // ---------- End transformation methods -----------------

  SkFilterQuality computeFilterQuality(Image*,
                                       const FloatRect& dest,
                                       const FloatRect& src) const;

  // Sets target URL of a clickable area.
  void setURLForRect(const KURL&, const IntRect&);

  // Sets the destination of a clickable area of a URL fragment (in a URL
  // pointing to the same web page). When the area is clicked, the page should
  // be scrolled to the location set by setURLDestinationLocation() for the
  // destination whose name is |name|.
  void setURLFragmentForRect(const String& name, const IntRect&);

  // Sets location of a URL destination (a.k.a. anchor) in the page.
  void setURLDestinationLocation(const String& name, const IntPoint&);

  static void adjustLineToPixelBoundaries(FloatPoint& p1,
                                          FloatPoint& p2,
                                          float strokeWidth,
                                          StrokeStyle);

  static int focusRingOutsetExtent(int offset, int width) {
    // Unlike normal outlines (whole width is outside of the offset), focus
    // rings are drawn with the center of the path aligned with the offset, so
    // only half of the width is outside of the offset.
    return focusRingOffset(offset) + (width + 1) / 2;
  }

#if DCHECK_IS_ON()
  void setInDrawingRecorder(bool);
#endif

  static sk_sp<SkColorFilter> WebCoreColorFilterToSkiaColorFilter(ColorFilter);

 private:
  const GraphicsContextState* immutableState() const { return m_paintState; }

  GraphicsContextState* mutableState() {
    realizePaintSave();
    return m_paintState;
  }

  template <typename DrawTextFunc>
  void drawTextPasses(const DrawTextFunc&);

#if OS(MACOSX)
  static inline int focusRingOffset(int offset) { return offset + 2; }
#else
  static inline int focusRingOffset(int offset) { return 0; }
  static SkPMColor lineColors(int);
  static SkPMColor antiColors1(int);
  static SkPMColor antiColors2(int);
  static void draw1xMarker(SkBitmap*, int);
  static void draw2xMarker(SkBitmap*, int);
#endif

  void saveLayer(const SkRect* bounds, const SkPaint*);
  void restoreLayer();

  // Helpers for drawing a focus ring (drawFocusRing)
  void drawFocusRingPath(const SkPath&, const Color&, float width);
  void drawFocusRingRect(const SkRect&, const Color&, float width);

  // SkCanvas wrappers.
  void clipRRect(const SkRRect&,
                 AntiAliasingMode = NotAntiAliased,
                 SkClipOp = SkClipOp::kIntersect);
  void concat(const SkMatrix&);

  // Apply deferred paint state saves
  void realizePaintSave() {
    if (contextDisabled())
      return;

    if (m_paintState->saveCount()) {
      m_paintState->decrementSaveCount();
      ++m_paintStateIndex;
      if (m_paintStateStack.size() == m_paintStateIndex) {
        m_paintStateStack.push_back(
            GraphicsContextState::createAndCopy(*m_paintState));
        m_paintState = m_paintStateStack[m_paintStateIndex].get();
      } else {
        GraphicsContextState* priorPaintState = m_paintState;
        m_paintState = m_paintStateStack[m_paintStateIndex].get();
        m_paintState->copy(*priorPaintState);
      }
    }
  }

  void fillRectWithRoundedHole(const FloatRect&,
                               const FloatRoundedRect& roundedHoleRect,
                               const Color&);

  const SkMetaData& metaData() const { return m_metaData; }

  // null indicates painting is contextDisabled. Never delete this object.
  SkCanvas* m_canvas;

  PaintController& m_paintController;

  // Paint states stack. The state controls the appearance of drawn content, so
  // this stack enables local drawing state changes with save()/restore() calls.
  // We do not delete from this stack to avoid memory churn.
  Vector<std::unique_ptr<GraphicsContextState>> m_paintStateStack;

  // Current index on the stack. May not be the last thing on the stack.
  unsigned m_paintStateIndex;

  // Raw pointer to the current state.
  GraphicsContextState* m_paintState;

  SkPictureRecorder m_pictureRecorder;

  SkMetaData m_metaData;

  const ColorBehavior m_colorBehavior;

#if DCHECK_IS_ON()
  int m_layerCount;
  bool m_disableDestructionChecks;
  bool m_inDrawingRecorder;
#endif

  const DisabledMode m_disabledState;

  float m_deviceScaleFactor;

  unsigned m_printing : 1;
  unsigned m_hasMetaData : 1;
};

}  // namespace blink

#endif  // GraphicsContext_h