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.
5 #ifndef SKIA_EXT_ANALYSIS_CANVAS_H_
6 #define SKIA_EXT_ANALYSIS_CANVAS_H_
11 #include "base/hash_tables.h"
12 #include "skia/ext/lazy_pixel_ref.h"
13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkDevice.h"
20 // Does not render anything, but gathers statistics about a region
21 // (specified as a clip rectangle) of an SkPicture as the picture is
22 // played back through it.
23 // To use: create a SkBitmap with kNo_Config, create an AnalysisDevice
24 // using that bitmap, and create an AnalysisCanvas using the device.
25 // Play a picture into the canvas, and then check isCheap().
26 class SK_API AnalysisCanvas
: public SkCanvas
{
28 typedef std::list
<skia::LazyPixelRef
*> LazyPixelRefList
;
30 AnalysisCanvas(AnalysisDevice
*);
31 virtual ~AnalysisCanvas();
33 // Returns true if the estimated cost of drawing is below an
34 // arbitrary threshold.
36 bool getColorIfSolid(SkColor
* color
) const;
37 bool isTransparent() const;
39 void consumeLazyPixelRefs(LazyPixelRefList
* pixelRefs
);
41 // Returns the estimated cost of drawing, in arbitrary units.
42 int getEstimatedCost() const;
44 virtual bool clipRect(const SkRect
& rect
,
45 SkRegion::Op op
= SkRegion::kIntersect_Op
,
46 bool doAntiAlias
= false) OVERRIDE
;
47 virtual bool clipPath(const SkPath
& path
,
48 SkRegion::Op op
= SkRegion::kIntersect_Op
,
49 bool doAntiAlias
= false) OVERRIDE
;
50 virtual bool clipRRect(const SkRRect
& rrect
,
51 SkRegion::Op op
= SkRegion::kIntersect_Op
,
52 bool doAntiAlias
= false) OVERRIDE
;
54 virtual int saveLayer(const SkRect
* bounds
, const SkPaint
*,
55 SkCanvas::SaveFlags flags
) OVERRIDE
;
56 virtual int save(SaveFlags flags
= kMatrixClip_SaveFlag
) OVERRIDE
;
58 virtual void restore() OVERRIDE
;
61 typedef SkCanvas INHERITED
;
62 static const int kNoLayer
;
65 int forceNotSolidStackLevel_
;
66 int forceNotTransparentStackLevel_
;
69 class SK_API AnalysisDevice
: public SkDevice
{
71 typedef std::list
<skia::LazyPixelRef
*> LazyPixelRefList
;
72 typedef base::hash_set
<uint32_t> IdSet
;
74 AnalysisDevice(const SkBitmap
& bm
);
75 virtual ~AnalysisDevice();
77 int getEstimatedCost() const;
78 bool getColorIfSolid(SkColor
* color
) const;
79 bool isTransparent() const;
81 void consumeLazyPixelRefs(LazyPixelRefList
* pixelRefs
);
83 void setForceNotSolid(bool flag
);
84 void setForceNotTransparent(bool flag
);
87 virtual void clear(SkColor color
) OVERRIDE
;
88 virtual void drawPaint(const SkDraw
&, const SkPaint
& paint
) OVERRIDE
;
89 virtual void drawPoints(const SkDraw
&, SkCanvas::PointMode mode
,
90 size_t count
, const SkPoint
[],
91 const SkPaint
& paint
) OVERRIDE
;
92 virtual void drawRect(const SkDraw
&, const SkRect
& r
,
93 const SkPaint
& paint
) OVERRIDE
;
94 virtual void drawOval(const SkDraw
&, const SkRect
& oval
,
95 const SkPaint
& paint
) OVERRIDE
;
96 virtual void drawPath(const SkDraw
&, const SkPath
& path
,
98 const SkMatrix
* prePathMatrix
= NULL
,
99 bool pathIsMutable
= false) OVERRIDE
;
100 virtual void drawBitmap(const SkDraw
&, const SkBitmap
& bitmap
,
101 const SkIRect
* srcRectOrNull
,
102 const SkMatrix
& matrix
, const SkPaint
& paint
)
104 virtual void drawSprite(const SkDraw
&, const SkBitmap
& bitmap
,
105 int x
, int y
, const SkPaint
& paint
) OVERRIDE
;
106 virtual void drawBitmapRect(const SkDraw
&, const SkBitmap
&,
107 const SkRect
* srcOrNull
, const SkRect
& dst
,
108 const SkPaint
& paint
) OVERRIDE
;
109 virtual void drawText(const SkDraw
&, const void* text
, size_t len
,
110 SkScalar x
, SkScalar y
, const SkPaint
& paint
)
112 virtual void drawPosText(const SkDraw
& draw
, const void* text
, size_t len
,
113 const SkScalar pos
[], SkScalar constY
,
114 int scalarsPerPos
, const SkPaint
& paint
) OVERRIDE
;
115 virtual void drawTextOnPath(const SkDraw
&, const void* text
, size_t len
,
116 const SkPath
& path
, const SkMatrix
* matrix
,
117 const SkPaint
& paint
) OVERRIDE
;
118 #ifdef SK_BUILD_FOR_ANDROID
119 virtual void drawPosTextOnPath(const SkDraw
& draw
, const void* text
,
121 const SkPoint pos
[], const SkPaint
& paint
,
122 const SkPath
& path
, const SkMatrix
* matrix
)
125 virtual void drawVertices(const SkDraw
&, SkCanvas::VertexMode
,
127 const SkPoint verts
[], const SkPoint texs
[],
128 const SkColor colors
[], SkXfermode
* xmode
,
129 const uint16_t indices
[], int indexCount
,
130 const SkPaint
& paint
) OVERRIDE
;
131 virtual void drawDevice(const SkDraw
&, SkDevice
*, int x
, int y
,
132 const SkPaint
&) OVERRIDE
;
138 typedef SkDevice INHERITED
;
140 void addPixelRefIfLazy(SkPixelRef
* pixelRef
);
141 void addBitmap(const SkBitmap
& bitmap
);
142 void addBitmapFromPaint(const SkPaint
& paint
);
144 bool isForcedNotSolid_
;
145 bool isForcedNotTransparent_
;
150 IdSet existingPixelRefIDs_
;
151 LazyPixelRefList lazyPixelRefs_
;
156 #endif // SKIA_EXT_ANALYSIS_CANVAS_H_