mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 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 | */ |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 7 | #ifndef SkPictureCommon_DEFINED |
| 8 | #define SkPictureCommon_DEFINED |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 9 | |
| 10 | // Some shared code used by both SkBigPicture and SkMiniPicture. |
| 11 | // SkTextHunter -- SkRecord visitor that returns true when the op draws text. |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 12 | // SkBitmapHunter -- SkRecord visitor that returns true when the op draws a bitmap or image. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 13 | // SkPathCounter -- SkRecord visitor that counts paths that draw slowly on the GPU. |
| 14 | |
| 15 | #include "SkPathEffect.h" |
| 16 | #include "SkRecords.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 17 | #include "SkShader.h" |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 18 | #include "SkTLogic.h" |
| 19 | |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 20 | // N.B. This name is slightly historical: hunting season is now open for SkImages too. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 21 | struct SkBitmapHunter { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 22 | // 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. |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 28 | // If the op has a bitmap or image directly, return true. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 29 | // 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(); } |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 32 | bool operator()(const SkRecords::DrawDrawable&) { /*TODO*/ return false; } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 33 | |
| 34 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 35 | bool operator()(const T& op) { return CheckBitmap(op); } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 36 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 37 | // If the op is tagged as having an image, return true. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 38 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 39 | static SK_WHEN(T::kTags & SkRecords::kHasImage_Tag, bool) CheckBitmap(const T&) { |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 40 | return true; |
| 41 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 42 | |
| 43 | // If not, look for one in its paint (if it has a paint). |
| 44 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 45 | static SK_WHEN(!(T::kTags & SkRecords::kHasImage_Tag), bool) CheckBitmap(const T& op) { |
| 46 | return CheckPaint(op); |
mtklein | a16af21 | 2015-08-26 08:14:52 -0700 | [diff] [blame] | 47 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 48 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 49 | // Most draws-type ops have paints. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 50 | template <typename T> |
mtklein | 1bb5fec | 2016-08-01 13:17:47 -0700 | [diff] [blame] | 51 | static SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, bool) CheckPaint(const T& op) { |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 52 | return PaintHasBitmap(AsPtr(op.paint)); |
| 53 | } |
| 54 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 55 | template <typename T> |
mtklein | 1bb5fec | 2016-08-01 13:17:47 -0700 | [diff] [blame] | 56 | static SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), bool) CheckPaint(const T&) { |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | static bool PaintHasBitmap(const SkPaint* paint) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 62 | if (paint) { |
| 63 | const SkShader* shader = paint->getShader(); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 64 | if (shader && shader->isAImage()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | return false; |
| 69 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // TODO: might be nicer to have operator() return an int (the number of slow paths) ? |
| 73 | struct SkPathCounter { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 74 | // 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 | |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 122 | void operator()(const SkRecords::ClipPath& op) { |
| 123 | // TODO: does the SkRegion op matter? |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 124 | if (op.opAA.aa() && !op.path.isConvex()) { |
fmalita | b5fc58e | 2016-05-25 11:31:04 -0700 | [diff] [blame] | 125 | fNumSlowPathsAndDashEffects++; |
| 126 | } |
| 127 | } |
| 128 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 129 | void operator()(const SkRecords::SaveLayer& op) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 130 | this->checkPaint(AsPtr(op.paint)); |
| 131 | } |
| 132 | |
| 133 | template <typename T> |
Mike Reed | 74564b4 | 2017-05-15 13:27:02 -0400 | [diff] [blame] | 134 | SK_WHEN(T::kTags & SkRecords::kHasPaint_Tag, void) operator()(const T& op) { |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 135 | this->checkPaint(AsPtr(op.paint)); |
| 136 | } |
| 137 | |
| 138 | template <typename T> |
Mike Reed | 74564b4 | 2017-05-15 13:27:02 -0400 | [diff] [blame] | 139 | SK_WHEN(!(T::kTags & SkRecords::kHasPaint_Tag), void) |
| 140 | operator()(const T& op) { /* do nothing */ } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 141 | |
| 142 | int fNumSlowPathsAndDashEffects; |
| 143 | }; |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 144 | #endif // SkPictureCommon_DEFINED |