• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 #ifndef SkPictureCommon_DEFINED
8 #define SkPictureCommon_DEFINED
9 
10 // Some shared code used by both SkBigPicture and SkMiniPicture.
11 //   SkTextHunter   -- SkRecord visitor that returns true when the op draws text.
12 //   SkBitmapHunter -- SkRecord visitor that returns true when the op draws a bitmap or image.
13 //   SkPathCounter  -- SkRecord visitor that counts paths that draw slowly on the GPU.
14 
15 #include "SkPathEffect.h"
16 #include "SkRecords.h"
17 #include "SkShader.h"
18 #include "SkTLogic.h"
19 
20 // N.B. This name is slightly historical: hunting season is now open for SkImages too.
21 struct SkBitmapHunter {
22     // Some ops have a paint, some have an optional paint.  Either way, get back a pointer.
AsPtrSkBitmapHunter23     static const SkPaint* AsPtr(const SkPaint& p) { return &p; }
AsPtrSkBitmapHunter24     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.
28     // If the op has a bitmap or image directly, return true.
29     // If the op has a paint and the paint has a bitmap, return true.
30     // Otherwise, return false.
operatorSkBitmapHunter31     bool operator()(const SkRecords::DrawPicture& op) { return op.picture->willPlayBackBitmaps(); }
operatorSkBitmapHunter32     bool operator()(const SkRecords::DrawDrawable&) { /*TODO*/ return false; }
33 
34     template <typename T>
operatorSkBitmapHunter35     bool operator()(const T& op) { return CheckBitmap(op); }
36 
37     // If the op is tagged as having an image, return true.
38     template <typename T>
SK_WHENSkBitmapHunter39     static SK_WHEN(T::kTags & SkRecords::kHasImage_Tag, bool) CheckBitmap(const T&) {
40         return true;
41     }
42 
43     // If not, look for one in its paint (if it has a paint).
44     template <typename T>
CheckBitmapSkBitmapHunter45     static SK_WHEN(!(T::kTags & SkRecords::kHasImage_Tag), bool) CheckBitmap(const T& op) {
46         return CheckPaint(op);
47     }
48 
49     // Most draws-type ops have paints.
50     template <typename T>
SK_WHENSkBitmapHunter51     static SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, bool) CheckPaint(const T& op) {
52         return PaintHasBitmap(AsPtr(op.paint));
53     }
54 
55     template <typename T>
CheckPaintSkBitmapHunter56     static SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), bool) CheckPaint(const T&) {
57         return false;
58     }
59 
60 private:
PaintHasBitmapSkBitmapHunter61     static bool PaintHasBitmap(const SkPaint* paint) {
62         if (paint) {
63             const SkShader* shader = paint->getShader();
64             if (shader && shader->isAImage()) {
65                 return true;
66             }
67         }
68         return false;
69     }
70 };
71 
72 // TODO: might be nicer to have operator() return an int (the number of slow paths) ?
73 struct SkPathCounter {
74     // Some ops have a paint, some have an optional paint.  Either way, get back a pointer.
AsPtrSkPathCounter75     static const SkPaint* AsPtr(const SkPaint& p) { return &p; }
AsPtrSkPathCounter76     static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return p; }
77 
SkPathCounterSkPathCounter78     SkPathCounter() : fNumSlowPathsAndDashEffects(0) {}
79 
80     // Recurse into nested pictures.
operatorSkPathCounter81     void operator()(const SkRecords::DrawPicture& op) {
82         fNumSlowPathsAndDashEffects += op.picture->numSlowPaths();
83     }
84 
checkPaintSkPathCounter85     void checkPaint(const SkPaint* paint) {
86         if (paint && paint->getPathEffect()) {
87             // Initially assume it's slow.
88             fNumSlowPathsAndDashEffects++;
89         }
90     }
91 
operatorSkPathCounter92     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 
operatorSkPathCounter105     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 
operatorSkPathCounter122     void operator()(const SkRecords::ClipPath& op) {
123         // TODO: does the SkRegion op matter?
124         if (op.opAA.aa() && !op.path.isConvex()) {
125             fNumSlowPathsAndDashEffects++;
126         }
127     }
128 
operatorSkPathCounter129     void operator()(const SkRecords::SaveLayer& op) {
130         this->checkPaint(AsPtr(op.paint));
131     }
132 
133     template <typename T>
SK_WHENSkPathCounter134     SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, void) operator()(const T& op) {
135         this->checkPaint(AsPtr(op.paint));
136     }
137 
138     template <typename T>
139     SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), void)
operatorSkPathCounter140       operator()(const T& op) { /* do nothing */ }
141 
142     int fNumSlowPathsAndDashEffects;
143 };
144 #endif  // SkPictureCommon_DEFINED
145