File: InterceptingCanvas.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 (380 lines) | stat: -rw-r--r-- 13,812 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
/*
 * 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:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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 THE COPYRIGHT
 * OWNER 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 InterceptingCanvas_h
#define InterceptingCanvas_h

#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "wtf/Allocator.h"
#include "wtf/Assertions.h"
#include "wtf/Noncopyable.h"

namespace blink {

class InterceptingCanvasBase : public SkCanvas {
  WTF_MAKE_NONCOPYABLE(InterceptingCanvasBase);

 public:
  template <typename DerivedCanvas>
  class CanvasInterceptorBase {
    STACK_ALLOCATED();
    WTF_MAKE_NONCOPYABLE(CanvasInterceptorBase);

   protected:
    CanvasInterceptorBase(InterceptingCanvasBase* canvas) : m_canvas(canvas) {
      ++m_canvas->m_callNestingDepth;
    }

    ~CanvasInterceptorBase() {
      ASSERT(m_canvas->m_callNestingDepth > 0);
      if (!--m_canvas->m_callNestingDepth)
        m_canvas->m_callCount++;
    }

    DerivedCanvas* canvas() { return static_cast<DerivedCanvas*>(m_canvas); }
    bool topLevelCall() const { return m_canvas->callNestingDepth() == 1; }
    InterceptingCanvasBase* m_canvas;
  };

  void resetStepCount() { m_callCount = 0; }

 protected:
  explicit InterceptingCanvasBase(SkBitmap bitmap)
      : SkCanvas(bitmap), m_callNestingDepth(0), m_callCount(0) {}
  InterceptingCanvasBase(int width, int height)
      : SkCanvas(width, height), m_callNestingDepth(0), m_callCount(0) {}

  void unrollDrawPicture(const SkPicture*,
                         const SkMatrix*,
                         const SkPaint*,
                         SkPicture::AbortCallback*);

  void onDrawPaint(const SkPaint&) override = 0;
  void onDrawPoints(PointMode,
                    size_t count,
                    const SkPoint pts[],
                    const SkPaint&) override = 0;
  void onDrawRect(const SkRect&, const SkPaint&) override = 0;
  void onDrawOval(const SkRect&, const SkPaint&) override = 0;
  void onDrawRRect(const SkRRect&, const SkPaint&) override = 0;
  void onDrawPath(const SkPath&, const SkPaint&) override = 0;
  void onDrawBitmap(const SkBitmap&,
                    SkScalar left,
                    SkScalar top,
                    const SkPaint*) override = 0;
  void onDrawBitmapRect(const SkBitmap&,
                        const SkRect* src,
                        const SkRect& dst,
                        const SkPaint*,
                        SrcRectConstraint) override = 0;
  void onDrawBitmapNine(const SkBitmap&,
                        const SkIRect& center,
                        const SkRect& dst,
                        const SkPaint*) override = 0;
  void onDrawImage(const SkImage*,
                   SkScalar,
                   SkScalar,
                   const SkPaint*) override = 0;
  void onDrawImageRect(const SkImage*,
                       const SkRect* src,
                       const SkRect& dst,
                       const SkPaint*,
                       SrcRectConstraint) override = 0;
  void onDrawVertices(VertexMode vmode,
                      int vertexCount,
                      const SkPoint vertices[],
                      const SkPoint texs[],
                      const SkColor colors[],
                      SkBlendMode bmode,
                      const uint16_t indices[],
                      int indexCount,
                      const SkPaint&) override = 0;

  void onDrawDRRect(const SkRRect& outer,
                    const SkRRect& inner,
                    const SkPaint&) override = 0;
  void onDrawText(const void* text,
                  size_t byteLength,
                  SkScalar x,
                  SkScalar y,
                  const SkPaint&) override = 0;
  void onDrawPosText(const void* text,
                     size_t byteLength,
                     const SkPoint pos[],
                     const SkPaint&) override = 0;
  void onDrawPosTextH(const void* text,
                      size_t byteLength,
                      const SkScalar xpos[],
                      SkScalar constY,
                      const SkPaint&) override = 0;
  void onDrawTextOnPath(const void* text,
                        size_t byteLength,
                        const SkPath&,
                        const SkMatrix*,
                        const SkPaint&) override = 0;
  void onDrawTextBlob(const SkTextBlob*,
                      SkScalar x,
                      SkScalar y,
                      const SkPaint&) override = 0;
  void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override = 0;
  void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override = 0;
  void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override = 0;
  void onClipRegion(const SkRegion&, SkClipOp) override = 0;
  void onDrawPicture(const SkPicture*,
                     const SkMatrix*,
                     const SkPaint*) override = 0;
  void didSetMatrix(const SkMatrix&) override = 0;
  void didConcat(const SkMatrix&) override = 0;
  void willSave() override = 0;
  SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override = 0;
  void willRestore() override = 0;

  unsigned callNestingDepth() const { return m_callNestingDepth; }
  unsigned callCount() const { return m_callCount; }

 private:
  unsigned m_callNestingDepth;
  unsigned m_callCount;
};

template <typename DerivedCanvas>
class CanvasInterceptor {};

template <typename DerivedCanvas,
          typename Interceptor = CanvasInterceptor<DerivedCanvas>>
class InterceptingCanvas : public InterceptingCanvasBase {
 protected:
  explicit InterceptingCanvas(SkBitmap bitmap)
      : InterceptingCanvasBase(bitmap) {}
  InterceptingCanvas(int width, int height)
      : InterceptingCanvasBase(width, height) {}

  void onDrawPaint(const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawPaint(paint);
  }

  void onDrawPoints(PointMode mode,
                    size_t count,
                    const SkPoint pts[],
                    const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawPoints(mode, count, pts, paint);
  }

  void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawRect(rect, paint);
  }

  void onDrawOval(const SkRect& rect, const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawOval(rect, paint);
  }

  void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawRRect(rrect, paint);
  }

  void onDrawPath(const SkPath& path, const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawPath(path, paint);
  }

  void onDrawBitmap(const SkBitmap& bitmap,
                    SkScalar left,
                    SkScalar top,
                    const SkPaint* paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawBitmap(bitmap, left, top, paint);
  }

  void onDrawBitmapRect(const SkBitmap& bitmap,
                        const SkRect* src,
                        const SkRect& dst,
                        const SkPaint* paint,
                        SrcRectConstraint constraint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawBitmapRect(bitmap, src, dst, paint, constraint);
  }

  void onDrawBitmapNine(const SkBitmap& bitmap,
                        const SkIRect& center,
                        const SkRect& dst,
                        const SkPaint* paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawBitmapNine(bitmap, center, dst, paint);
  }

  void onDrawImage(const SkImage* image,
                   SkScalar x,
                   SkScalar y,
                   const SkPaint* paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawImage(image, x, y, paint);
  }

  void onDrawImageRect(const SkImage* image,
                       const SkRect* src,
                       const SkRect& dst,
                       const SkPaint* paint,
                       SrcRectConstraint constraint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawImageRect(image, src, dst, paint, constraint);
  }

  void onDrawVertices(VertexMode vmode,
                      int vertexCount,
                      const SkPoint vertices[],
                      const SkPoint texs[],
                      const SkColor colors[],
                      SkBlendMode bmode,
                      const uint16_t indices[],
                      int indexCount,
                      const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawVertices(vmode, vertexCount, vertices, texs, colors,
                                   bmode, indices, indexCount, paint);
  }

  void onDrawDRRect(const SkRRect& outer,
                    const SkRRect& inner,
                    const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawDRRect(outer, inner, paint);
  }

  void onDrawText(const void* text,
                  size_t byteLength,
                  SkScalar x,
                  SkScalar y,
                  const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawText(text, byteLength, x, y, paint);
  }

  void onDrawPosText(const void* text,
                     size_t byteLength,
                     const SkPoint pos[],
                     const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawPosText(text, byteLength, pos, paint);
  }

  void onDrawPosTextH(const void* text,
                      size_t byteLength,
                      const SkScalar xpos[],
                      SkScalar constY,
                      const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawPosTextH(text, byteLength, xpos, constY, paint);
  }

  void onDrawTextOnPath(const void* text,
                        size_t byteLength,
                        const SkPath& path,
                        const SkMatrix* matrix,
                        const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawTextOnPath(text, byteLength, path, matrix, paint);
  }

  void onDrawTextBlob(const SkTextBlob* blob,
                      SkScalar x,
                      SkScalar y,
                      const SkPaint& paint) override {
    Interceptor interceptor(this);
    this->SkCanvas::onDrawTextBlob(blob, x, y, paint);
  }

  void onClipRect(const SkRect& rect,
                  SkClipOp op,
                  ClipEdgeStyle edgeStyle) override {
    Interceptor interceptor(this);
    this->SkCanvas::onClipRect(rect, op, edgeStyle);
  }

  void onClipRRect(const SkRRect& rrect,
                   SkClipOp op,
                   ClipEdgeStyle edgeStyle) override {
    Interceptor interceptor(this);
    this->SkCanvas::onClipRRect(rrect, op, edgeStyle);
  }

  void onClipPath(const SkPath& path,
                  SkClipOp op,
                  ClipEdgeStyle edgeStyle) override {
    Interceptor interceptor(this);
    this->SkCanvas::onClipPath(path, op, edgeStyle);
  }

  void onClipRegion(const SkRegion& region, SkClipOp op) override {
    Interceptor interceptor(this);
    this->SkCanvas::onClipRegion(region, op);
  }

  void onDrawPicture(const SkPicture* picture,
                     const SkMatrix* matrix,
                     const SkPaint* paint) override {
    this->unrollDrawPicture(picture, matrix, paint, nullptr);
  }

  void didSetMatrix(const SkMatrix& matrix) override {
    Interceptor interceptor(this);
    this->SkCanvas::didSetMatrix(matrix);
  }

  void didConcat(const SkMatrix& matrix) override {
    Interceptor interceptor(this);
    this->SkCanvas::didConcat(matrix);
  }

  void willSave() override {
    Interceptor interceptor(this);
    this->SkCanvas::willSave();
  }

  SkCanvas::SaveLayerStrategy getSaveLayerStrategy(
      const SaveLayerRec& rec) override {
    Interceptor interceptor(this);
    return this->SkCanvas::getSaveLayerStrategy(rec);
  }

  void willRestore() override {
    Interceptor interceptor(this);
    this->SkCanvas::willRestore();
  }
};

}  // namespace blink

#endif  // InterceptingCanvas_h