OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkRecordDraw_DEFINED | 8 #ifndef SkRecordDraw_DEFINED |
9 #define SkRecordDraw_DEFINED | 9 #define SkRecordDraw_DEFINED |
10 | 10 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 p.setColor(c.color); | 64 p.setColor(c.color); |
65 DrawRect drawRect(p, fClearRect); | 65 DrawRect drawRect(p, fClearRect); |
66 this->INHERITED::operator()(drawRect); | 66 this->INHERITED::operator()(drawRect); |
67 } | 67 } |
68 | 68 |
69 private: | 69 private: |
70 const SkRect fClearRect; | 70 const SkRect fClearRect; |
71 typedef Draw INHERITED; | 71 typedef Draw INHERITED; |
72 }; | 72 }; |
73 | 73 |
| 74 template <typename VISITOR> class VisitMaster : SkNoncopyable { |
| 75 public: |
| 76 VisitMaster(const SkRecord& record, SkBBoxHierarchy* bbh) { |
| 77 VISITOR visitor(record, bbh); |
| 78 |
| 79 for (unsigned curOp = 0; curOp < record.count(); curOp++) { |
| 80 visitor.setCurrentOp(curOp); |
| 81 record.visit<void>(curOp, visitor); |
| 82 } |
| 83 |
| 84 visitor.cleanUp(); |
| 85 } |
| 86 }; |
| 87 |
| 88 // This is an SkRecord visitor that fills an SkBBoxHierarchy. |
| 89 // |
| 90 // The interesting part here is how to calculate bounds for ops which don't |
| 91 // have intrinsic bounds. What is the bounds of a Save or a Translate? |
| 92 // |
| 93 // We answer this by thinking about a particular definition of bounds: if I |
| 94 // don't execute this op, pixels in this rectangle might draw incorrectly. So |
| 95 // the bounds of a Save, a Translate, a Restore, etc. are the union of the |
| 96 // bounds of Draw* ops that they might have an effect on. For any given |
| 97 // Save/Restore block, the bounds of the Save, the Restore, and any other |
| 98 // non-drawing ("control") ops inside are exactly the union of the bounds of |
| 99 // the drawing ops inside that block. |
| 100 // |
| 101 // To implement this, we keep a stack of active Save blocks. As we consume ops |
| 102 // inside the Save/Restore block, drawing ops are unioned with the bounds of |
| 103 // the block, and control ops are stashed away for later. When we finish the |
| 104 // block with a Restore, our bounds are complete, and we go back and fill them |
| 105 // in for all the control ops we stashed away. |
| 106 class FillBounds : SkNoncopyable { |
| 107 public: |
| 108 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh); |
| 109 |
| 110 void setCurrentOp(unsigned currentOp) { fCurrentOp = currentOp; } |
| 111 void cleanUp(); |
| 112 |
| 113 template <typename T> void operator()(const T& op) { |
| 114 this->updateCTM(op); |
| 115 this->updateClipBounds(op); |
| 116 this->trackBounds(op); |
| 117 } |
| 118 |
| 119 protected: |
| 120 // In FillBounds, SkRect are in local coordinates, Bounds are translated bac
k to identity space. |
| 121 typedef SkRect Bounds; |
| 122 |
| 123 unsigned currentOp() const { return fCurrentOp; } |
| 124 const SkMatrix& ctm() const { return *fCTM; } |
| 125 const Bounds& currentClipBounds() const { return fCurrentClipBounds; } |
| 126 |
| 127 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const; |
| 128 const Bounds& getBounds(unsigned index) const { return fBounds[index]; } |
| 129 |
| 130 private: |
| 131 struct SaveBounds { |
| 132 int controlOps; // Number of control ops in this Save block, incl
uding the Save. |
| 133 Bounds bounds; // Bounds of everything in the block. |
| 134 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all op
s in this block. |
| 135 }; |
| 136 |
| 137 template <typename T> void updateCTM(const T&); |
| 138 template <typename T> void updateClipBounds(const T&); |
| 139 template <typename T> void trackBounds(const T&); |
| 140 template <typename T> Bounds bounds(const T&) const; |
| 141 |
| 142 void updateClipBoundsForClipOp(const SkIRect& devBounds); |
| 143 |
| 144 void pushSaveBlock(const SkPaint* paint); |
| 145 Bounds popSaveBlock(); |
| 146 void pushControl(); |
| 147 void popControl(const Bounds& bounds); |
| 148 void updateSaveBounds(const Bounds& bounds); |
| 149 |
| 150 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const; |
| 151 |
| 152 const unsigned int fNumRecords; |
| 153 |
| 154 // The BBH being filled in |
| 155 SkBBoxHierarchy* fBBH; |
| 156 |
| 157 // Conservative identity-space bounds for each op in the SkRecord. |
| 158 SkAutoTMalloc<Bounds> fBounds; |
| 159 |
| 160 // We walk fCurrentOp through the SkRecord, as we go using updateCTM() |
| 161 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative |
| 162 // identity-space bounds of the current clip (fCurrentClipBounds). |
| 163 unsigned fCurrentOp; |
| 164 const SkMatrix* fCTM; |
| 165 Bounds fCurrentClipBounds; |
| 166 |
| 167 // Used to track the bounds of Save/Restore blocks and the control ops insid
e them. |
| 168 SkTDArray<SaveBounds> fSaveStack; |
| 169 SkTDArray<unsigned> fControlIndices; |
| 170 }; |
| 171 |
| 172 |
74 } // namespace SkRecords | 173 } // namespace SkRecords |
75 | 174 |
76 #endif//SkRecordDraw_DEFINED | 175 #endif//SkRecordDraw_DEFINED |
OLD | NEW |