OLD | NEW |
(Empty) | |
| 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 |
| 8 // Some shared code used by both SkBigPicture and SkMiniPicture. |
| 9 // SkTextHunter -- SkRecord visitor that returns true when the op draws text
. |
| 10 // SkBitmapHunter -- SkRecord visitor that returns true when the op draws a bi
tmap. |
| 11 // SkPathCounter -- SkRecord visitor that counts paths that draw slowly on th
e GPU. |
| 12 |
| 13 #include "SkPathEffect.h" |
| 14 #include "SkRecords.h" |
| 15 #include "SkTLogic.h" |
| 16 |
| 17 struct SkTextHunter { |
| 18 // Most ops never have text. Some always do. Subpictures know themeselves. |
| 19 template <typename T> bool operator()(const T&) { return false; } |
| 20 bool operator()(const SkRecords::DrawPosText&) { return true; } |
| 21 bool operator()(const SkRecords::DrawPosTextH&) { return true; } |
| 22 bool operator()(const SkRecords::DrawText&) { return true; } |
| 23 bool operator()(const SkRecords::DrawTextBlob&) { return true; } |
| 24 bool operator()(const SkRecords::DrawTextOnPath&) { return true; } |
| 25 bool operator()(const SkRecords::DrawPicture& op) { return op.picture->hasTe
xt(); } |
| 26 }; |
| 27 |
| 28 |
| 29 struct SkBitmapHunter { |
| 30 // Helpers. These create HasMember_bitmap and HasMember_paint. |
| 31 SK_CREATE_MEMBER_DETECTOR(bitmap); |
| 32 SK_CREATE_MEMBER_DETECTOR(paint); |
| 33 |
| 34 // Some ops have a paint, some have an optional paint. Either way, get back
a pointer. |
| 35 static const SkPaint* AsPtr(const SkPaint& p) { return &p; } |
| 36 static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return
p; } |
| 37 |
| 38 // Main entry for visitor: |
| 39 // If the op is a DrawPicture, recurse. |
| 40 // If the op has a bitmap directly, return true. |
| 41 // If the op has a paint and the paint has a bitmap, return true. |
| 42 // Otherwise, return false. |
| 43 bool operator()(const SkRecords::DrawPicture& op) { return op.picture->willP
layBackBitmaps(); } |
| 44 |
| 45 template <typename T> |
| 46 bool operator()(const T& r) { return CheckBitmap(r); } |
| 47 |
| 48 // If the op has a bitmap, of course we're going to play back bitmaps. |
| 49 template <typename T> |
| 50 static SK_WHEN(HasMember_bitmap<T>, bool) CheckBitmap(const T&) { return tru
e; } |
| 51 |
| 52 // If not, look for one in its paint (if it has a paint). |
| 53 template <typename T> |
| 54 static SK_WHEN(!HasMember_bitmap<T>, bool) CheckBitmap(const T& r) { return
CheckPaint(r); } |
| 55 |
| 56 // If we have a paint, dig down into the effects looking for a bitmap. |
| 57 template <typename T> |
| 58 static SK_WHEN(HasMember_paint<T>, bool) CheckPaint(const T& r) { |
| 59 const SkPaint* paint = AsPtr(r.paint); |
| 60 if (paint) { |
| 61 const SkShader* shader = paint->getShader(); |
| 62 if (shader && |
| 63 shader->asABitmap(nullptr, nullptr, nullptr) == SkShader::kDefau
lt_BitmapType) { |
| 64 return true; |
| 65 } |
| 66 } |
| 67 return false; |
| 68 } |
| 69 |
| 70 // If we don't have a paint, that non-paint has no bitmap. |
| 71 template <typename T> |
| 72 static SK_WHEN(!HasMember_paint<T>, bool) CheckPaint(const T&) { return fals
e; } |
| 73 }; |
| 74 |
| 75 // TODO: might be nicer to have operator() return an int (the number of slow pat
hs) ? |
| 76 struct SkPathCounter { |
| 77 SK_CREATE_MEMBER_DETECTOR(paint); |
| 78 |
| 79 // Some ops have a paint, some have an optional paint. Either way, get back
a pointer. |
| 80 static const SkPaint* AsPtr(const SkPaint& p) { return &p; } |
| 81 static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return
p; } |
| 82 |
| 83 SkPathCounter() : fNumSlowPathsAndDashEffects(0) {} |
| 84 |
| 85 // Recurse into nested pictures. |
| 86 void operator()(const SkRecords::DrawPicture& op) { |
| 87 fNumSlowPathsAndDashEffects += op.picture->numSlowPaths(); |
| 88 } |
| 89 |
| 90 void checkPaint(const SkPaint* paint) { |
| 91 if (paint && paint->getPathEffect()) { |
| 92 // Initially assume it's slow. |
| 93 fNumSlowPathsAndDashEffects++; |
| 94 } |
| 95 } |
| 96 |
| 97 void operator()(const SkRecords::DrawPoints& op) { |
| 98 this->checkPaint(&op.paint); |
| 99 const SkPathEffect* effect = op.paint.getPathEffect(); |
| 100 if (effect) { |
| 101 SkPathEffect::DashInfo info; |
| 102 SkPathEffect::DashType dashType = effect->asADash(&info); |
| 103 if (2 == op.count && SkPaint::kRound_Cap != op.paint.getStrokeCap()
&& |
| 104 SkPathEffect::kDash_DashType == dashType && 2 == info.fCount) { |
| 105 fNumSlowPathsAndDashEffects--; |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 void operator()(const SkRecords::DrawPath& op) { |
| 111 this->checkPaint(&op.paint); |
| 112 if (op.paint.isAntiAlias() && !op.path.isConvex()) { |
| 113 SkPaint::Style paintStyle = op.paint.getStyle(); |
| 114 const SkRect& pathBounds = op.path.getBounds(); |
| 115 if (SkPaint::kStroke_Style == paintStyle && |
| 116 0 == op.paint.getStrokeWidth()) { |
| 117 // AA hairline concave path is not slow. |
| 118 } else if (SkPaint::kFill_Style == paintStyle && pathBounds.width()
< 64.f && |
| 119 pathBounds.height() < 64.f && !op.path.isVolatile()) { |
| 120 // AADF eligible concave path is not slow. |
| 121 } else { |
| 122 fNumSlowPathsAndDashEffects++; |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 template <typename T> |
| 128 SK_WHEN(HasMember_paint<T>, void) operator()(const T& op) { |
| 129 this->checkPaint(AsPtr(op.paint)); |
| 130 } |
| 131 |
| 132 template <typename T> |
| 133 SK_WHEN(!HasMember_paint<T>, void) operator()(const T& op) { /* do nothing *
/ } |
| 134 |
| 135 int fNumSlowPathsAndDashEffects; |
| 136 }; |
OLD | NEW |