blob: 43805fd49f70e94e62f314060a528a3ce8b5d7cd [file] [log] [blame]
mtklein9db912c2015-05-19 11:11:26 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Hal Canary03a7f5f2017-02-10 09:06:38 -05007#ifndef SkPictureCommon_DEFINED
8#define SkPictureCommon_DEFINED
mtklein9db912c2015-05-19 11:11:26 -07009
10// Some shared code used by both SkBigPicture and SkMiniPicture.
11// SkTextHunter -- SkRecord visitor that returns true when the op draws text.
mtkleina16af212015-08-26 08:14:52 -070012// SkBitmapHunter -- SkRecord visitor that returns true when the op draws a bitmap or image.
mtklein9db912c2015-05-19 11:11:26 -070013// SkPathCounter -- SkRecord visitor that counts paths that draw slowly on the GPU.
14
15#include "SkPathEffect.h"
16#include "SkRecords.h"
Florin Malitaab244f02017-05-03 19:16:58 +000017#include "SkShader.h"
mtklein9db912c2015-05-19 11:11:26 -070018#include "SkTLogic.h"
19
mtkleina16af212015-08-26 08:14:52 -070020// N.B. This name is slightly historical: hunting season is now open for SkImages too.
mtklein9db912c2015-05-19 11:11:26 -070021struct SkBitmapHunter {
mtklein9db912c2015-05-19 11:11:26 -070022 // Some ops have a paint, some have an optional paint. Either way, get back a pointer.
23 static const SkPaint* AsPtr(const SkPaint& p) { return &p; }
24 static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return p; }
25
26 // Main entry for visitor:
27 // If the op is a DrawPicture, recurse.
mtkleina16af212015-08-26 08:14:52 -070028 // If the op has a bitmap or image directly, return true.
mtklein9db912c2015-05-19 11:11:26 -070029 // If the op has a paint and the paint has a bitmap, return true.
30 // Otherwise, return false.
31 bool operator()(const SkRecords::DrawPicture& op) { return op.picture->willPlayBackBitmaps(); }
mtklein449d9b72015-09-28 10:33:02 -070032 bool operator()(const SkRecords::DrawDrawable&) { /*TODO*/ return false; }
mtklein9db912c2015-05-19 11:11:26 -070033
34 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -070035 bool operator()(const T& op) { return CheckBitmap(op); }
mtklein9db912c2015-05-19 11:11:26 -070036
mtklein449d9b72015-09-28 10:33:02 -070037 // If the op is tagged as having an image, return true.
mtklein9db912c2015-05-19 11:11:26 -070038 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -070039 static SK_WHEN(T::kTags & SkRecords::kHasImage_Tag, bool) CheckBitmap(const T&) {
mtkleina16af212015-08-26 08:14:52 -070040 return true;
41 }
mtklein9db912c2015-05-19 11:11:26 -070042
43 // If not, look for one in its paint (if it has a paint).
44 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -070045 static SK_WHEN(!(T::kTags & SkRecords::kHasImage_Tag), bool) CheckBitmap(const T& op) {
46 return CheckPaint(op);
mtkleina16af212015-08-26 08:14:52 -070047 }
mtklein9db912c2015-05-19 11:11:26 -070048
mtklein449d9b72015-09-28 10:33:02 -070049 // Most draws-type ops have paints.
mtklein9db912c2015-05-19 11:11:26 -070050 template <typename T>
mtklein1bb5fec2016-08-01 13:17:47 -070051 static SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, bool) CheckPaint(const T& op) {
mtklein449d9b72015-09-28 10:33:02 -070052 return PaintHasBitmap(AsPtr(op.paint));
53 }
54
mtklein449d9b72015-09-28 10:33:02 -070055 template <typename T>
mtklein1bb5fec2016-08-01 13:17:47 -070056 static SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), bool) CheckPaint(const T&) {
mtklein449d9b72015-09-28 10:33:02 -070057 return false;
58 }
59
60private:
61 static bool PaintHasBitmap(const SkPaint* paint) {
mtklein9db912c2015-05-19 11:11:26 -070062 if (paint) {
63 const SkShader* shader = paint->getShader();
Mike Reed627778d2016-09-28 17:13:38 -040064 if (shader && shader->isAImage()) {
mtklein9db912c2015-05-19 11:11:26 -070065 return true;
66 }
67 }
68 return false;
69 }
mtklein9db912c2015-05-19 11:11:26 -070070};
71
72// TODO: might be nicer to have operator() return an int (the number of slow paths) ?
73struct SkPathCounter {
mtklein9db912c2015-05-19 11:11:26 -070074 // Some ops have a paint, some have an optional paint. Either way, get back a pointer.
75 static const SkPaint* AsPtr(const SkPaint& p) { return &p; }
76 static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return p; }
77
78 SkPathCounter() : fNumSlowPathsAndDashEffects(0) {}
79
80 // Recurse into nested pictures.
81 void operator()(const SkRecords::DrawPicture& op) {
82 fNumSlowPathsAndDashEffects += op.picture->numSlowPaths();
83 }
84
85 void checkPaint(const SkPaint* paint) {
86 if (paint && paint->getPathEffect()) {
87 // Initially assume it's slow.
88 fNumSlowPathsAndDashEffects++;
89 }
90 }
91
92 void operator()(const SkRecords::DrawPoints& op) {
93 this->checkPaint(&op.paint);
94 const SkPathEffect* effect = op.paint.getPathEffect();
95 if (effect) {
96 SkPathEffect::DashInfo info;
97 SkPathEffect::DashType dashType = effect->asADash(&info);
98 if (2 == op.count && SkPaint::kRound_Cap != op.paint.getStrokeCap() &&
99 SkPathEffect::kDash_DashType == dashType && 2 == info.fCount) {
100 fNumSlowPathsAndDashEffects--;
101 }
102 }
103 }
104
105 void operator()(const SkRecords::DrawPath& op) {
106 this->checkPaint(&op.paint);
107 if (op.paint.isAntiAlias() && !op.path.isConvex()) {
108 SkPaint::Style paintStyle = op.paint.getStyle();
109 const SkRect& pathBounds = op.path.getBounds();
110 if (SkPaint::kStroke_Style == paintStyle &&
111 0 == op.paint.getStrokeWidth()) {
112 // AA hairline concave path is not slow.
113 } else if (SkPaint::kFill_Style == paintStyle && pathBounds.width() < 64.f &&
114 pathBounds.height() < 64.f && !op.path.isVolatile()) {
115 // AADF eligible concave path is not slow.
116 } else {
117 fNumSlowPathsAndDashEffects++;
118 }
119 }
120 }
121
fmalitab5fc58e2016-05-25 11:31:04 -0700122 void operator()(const SkRecords::ClipPath& op) {
123 // TODO: does the SkRegion op matter?
Mike Reedebfce6d2016-12-12 10:02:12 -0500124 if (op.opAA.aa() && !op.path.isConvex()) {
fmalitab5fc58e2016-05-25 11:31:04 -0700125 fNumSlowPathsAndDashEffects++;
126 }
127 }
128
mtklein449d9b72015-09-28 10:33:02 -0700129 void operator()(const SkRecords::SaveLayer& op) {
mtklein9db912c2015-05-19 11:11:26 -0700130 this->checkPaint(AsPtr(op.paint));
131 }
132
133 template <typename T>
Mike Reed74564b42017-05-15 13:27:02 -0400134 SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, void) operator()(const T& op) {
mtklein449d9b72015-09-28 10:33:02 -0700135 this->checkPaint(AsPtr(op.paint));
136 }
137
138 template <typename T>
Mike Reed74564b42017-05-15 13:27:02 -0400139 SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), void)
140 operator()(const T& op) { /* do nothing */ }
mtklein9db912c2015-05-19 11:11:26 -0700141
142 int fNumSlowPathsAndDashEffects;
143};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500144#endif // SkPictureCommon_DEFINED