1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SKIA_EXT_ANALYSIS_CANVAS_H_
6#define SKIA_EXT_ANALYSIS_CANVAS_H_
7
8#include "base/compiler_specific.h"
9#include "third_party/skia/include/core/SkCanvas.h"
10#include "third_party/skia/include/core/SkPicture.h"
11
12namespace skia {
13
14// Does not render anything, but gathers statistics about a region
15// (specified as a clip rectangle) of an SkPicture as the picture is
16// played back through it.
17// To use: play a picture into the canvas, and then check result.
18class SK_API AnalysisCanvas : public SkCanvas, public SkDrawPictureCallback {
19 public:
20  AnalysisCanvas(int width, int height);
21  virtual ~AnalysisCanvas();
22
23  // Returns true when a SkColor can be used to represent result.
24  bool GetColorIfSolid(SkColor* color) const;
25
26  void SetForceNotSolid(bool flag);
27  void SetForceNotTransparent(bool flag);
28
29  // SkDrawPictureCallback override.
30  virtual bool abortDrawing() OVERRIDE;
31
32  // SkCanvas overrides.
33  virtual void clear(SkColor) OVERRIDE;
34  virtual void drawPaint(const SkPaint& paint) OVERRIDE;
35  virtual void drawPoints(PointMode,
36                          size_t count,
37                          const SkPoint pts[],
38                          const SkPaint&) OVERRIDE;
39  virtual void drawOval(const SkRect&, const SkPaint&) OVERRIDE;
40  virtual void drawRect(const SkRect&, const SkPaint&) OVERRIDE;
41  virtual void drawRRect(const SkRRect&, const SkPaint&) OVERRIDE;
42  virtual void drawPath(const SkPath& path, const SkPaint&) OVERRIDE;
43  virtual void drawBitmap(const SkBitmap&,
44                          SkScalar left,
45                          SkScalar top,
46                          const SkPaint* paint = NULL) OVERRIDE;
47  virtual void drawBitmapRectToRect(const SkBitmap&,
48                                    const SkRect* src,
49                                    const SkRect& dst,
50                                    const SkPaint* paint,
51                                    DrawBitmapRectFlags flags) OVERRIDE;
52  virtual void drawBitmapMatrix(const SkBitmap&,
53                                const SkMatrix&,
54                                const SkPaint* paint = NULL) OVERRIDE;
55  virtual void drawBitmapNine(const SkBitmap& bitmap,
56                              const SkIRect& center,
57                              const SkRect& dst,
58                              const SkPaint* paint = NULL) OVERRIDE;
59  virtual void drawSprite(const SkBitmap&, int left, int top,
60                          const SkPaint* paint = NULL) OVERRIDE;
61  virtual void drawVertices(VertexMode,
62                            int vertexCount,
63                            const SkPoint vertices[],
64                            const SkPoint texs[],
65                            const SkColor colors[],
66                            SkXfermode*,
67                            const uint16_t indices[],
68                            int indexCount,
69                            const SkPaint&) OVERRIDE;
70
71 protected:
72  virtual void willSave() OVERRIDE;
73  virtual SaveLayerStrategy willSaveLayer(const SkRect*,
74                                          const SkPaint*,
75                                          SaveFlags) OVERRIDE;
76  virtual void willRestore() OVERRIDE;
77
78  virtual void onClipRect(const SkRect& rect,
79                          SkRegion::Op op,
80                          ClipEdgeStyle edge_style) OVERRIDE;
81  virtual void onClipRRect(const SkRRect& rrect,
82                           SkRegion::Op op,
83                           ClipEdgeStyle edge_style) OVERRIDE;
84  virtual void onClipPath(const SkPath& path,
85                          SkRegion::Op op,
86                          ClipEdgeStyle edge_style) OVERRIDE;
87  virtual void onClipRegion(const SkRegion& deviceRgn,
88                            SkRegion::Op op) OVERRIDE;
89
90  virtual void onDrawText(const void* text,
91                          size_t byteLength,
92                          SkScalar x,
93                          SkScalar y,
94                          const SkPaint&) OVERRIDE;
95  virtual void onDrawPosText(const void* text,
96                             size_t byteLength,
97                             const SkPoint pos[],
98                             const SkPaint&) OVERRIDE;
99  virtual void onDrawPosTextH(const void* text,
100                              size_t byteLength,
101                              const SkScalar xpos[],
102                              SkScalar constY,
103                              const SkPaint&) OVERRIDE;
104  virtual void onDrawTextOnPath(const void* text,
105                                size_t byteLength,
106                                const SkPath& path,
107                                const SkMatrix* matrix,
108                                const SkPaint&) OVERRIDE;
109  virtual void onDrawTextBlob(const SkTextBlob* blob,
110                              SkScalar x,
111                              SkScalar y,
112                              const SkPaint& paint) OVERRIDE;
113  virtual void onDrawDRRect(const SkRRect& outer,
114                            const SkRRect& inner,
115                            const SkPaint&) OVERRIDE;
116
117  void OnComplexClip();
118
119 private:
120  typedef SkCanvas INHERITED;
121
122  int saved_stack_size_;
123  int force_not_solid_stack_level_;
124  int force_not_transparent_stack_level_;
125
126  bool is_forced_not_solid_;
127  bool is_forced_not_transparent_;
128  bool is_solid_color_;
129  SkColor color_;
130  bool is_transparent_;
131  int draw_op_count_;
132};
133
134}  // namespace skia
135
136#endif  // SKIA_EXT_ANALYSIS_CANVAS_H_
137
138