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 #include "GrRecordReplaceDraw.h" | 8 #include "GrRecordReplaceDraw.h" |
9 #include "SkCanvasPriv.h" | 9 #include "SkCanvasPriv.h" |
10 #include "SkImage.h" | 10 #include "SkImage.h" |
11 #include "SkRecordDraw.h" | 11 #include "SkRecordDraw.h" |
12 #include "SkRecords.h" | 12 #include "SkRecords.h" |
13 | 13 |
14 GrReplacements::ReplacementInfo* GrReplacements::newReplacement(uint32_t picture
ID, | 14 GrReplacements::ReplacementInfo* GrReplacements::newReplacement(uint32_t picture
ID, |
15 unsigned start, | 15 const SkMatrix&
initialMat, |
16 const SkMatrix&
ctm) { | 16 const int* key,
int keySize) { |
17 ReplacementInfo* replacement = SkNEW_ARGS(ReplacementInfo, (pictureID, start
, ctm)); | 17 ReplacementInfo* replacement = SkNEW_ARGS(ReplacementInfo, (pictureID, initi
alMat, |
| 18 key, keySize)); |
18 fReplacementHash.add(replacement); | 19 fReplacementHash.add(replacement); |
19 return replacement; | 20 return replacement; |
20 } | 21 } |
21 | 22 |
22 void GrReplacements::freeAll() { | 23 void GrReplacements::freeAll() { |
23 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key>::Iter iter(&fReplaceme
ntHash); | 24 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key>::Iter iter(&fReplaceme
ntHash); |
24 | 25 |
25 for (; !iter.done(); ++iter) { | 26 for (; !iter.done(); ++iter) { |
26 ReplacementInfo* replacement = &(*iter); | 27 ReplacementInfo* replacement = &(*iter); |
27 SkDELETE(replacement); | 28 SkDELETE(replacement); |
28 } | 29 } |
29 | 30 |
30 fReplacementHash.reset(); | 31 fReplacementHash.reset(); |
31 } | 32 } |
32 | 33 |
33 const GrReplacements::ReplacementInfo* GrReplacements::lookupByStart(uint32_t pi
ctureID, | 34 const GrReplacements::ReplacementInfo* GrReplacements::lookup(uint32_t pictureID
, |
34 unsigned st
art, | 35 const SkMatrix& in
itialMat, |
35 const SkMat
rix& ctm) const { | 36 const int* key, |
36 return fReplacementHash.find(ReplacementInfo::Key(pictureID, start, ctm)); | 37 int keySize) const
{ |
| 38 return fReplacementHash.find(ReplacementInfo::Key(pictureID, initialMat, key
, keySize)); |
37 } | 39 } |
38 | 40 |
39 static inline void draw_replacement_bitmap(const GrReplacements::ReplacementInfo
* ri, | 41 static inline void draw_replacement_bitmap(const GrReplacements::ReplacementInfo
* ri, |
40 SkCanvas* canvas) { | 42 SkCanvas* canvas) { |
41 SkRect src = SkRect::Make(ri->fSrcRect); | 43 SkRect src = SkRect::Make(ri->fSrcRect); |
42 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(ri->fPos.fX), | 44 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(ri->fPos.fX), |
43 SkIntToScalar(ri->fPos.fY), | 45 SkIntToScalar(ri->fPos.fY), |
44 SkIntToScalar(ri->fSrcRect.width()), | 46 SkIntToScalar(ri->fSrcRect.width()), |
45 SkIntToScalar(ri->fSrcRect.height())); | 47 SkIntToScalar(ri->fSrcRect.height())); |
46 | 48 |
47 canvas->save(); | 49 canvas->save(); |
48 canvas->setMatrix(SkMatrix::I()); | 50 canvas->setMatrix(SkMatrix::I()); |
49 canvas->drawImageRect(ri->fImage, &src, dst, ri->fPaint); | 51 canvas->drawImageRect(ri->fImage, &src, dst, ri->fPaint); |
50 canvas->restore(); | 52 canvas->restore(); |
51 } | 53 } |
52 | 54 |
53 // Used by GrRecordReplaceDraw. It intercepts nested drawPicture calls and | 55 // Used by GrRecordReplaceDraw. It intercepts nested drawPicture calls and |
54 // also draws them with replaced layers. | 56 // also draws them with replaced layers. |
55 class ReplaceDraw : public SkRecords::Draw { | 57 class ReplaceDraw : public SkRecords::Draw { |
56 public: | 58 public: |
57 ReplaceDraw(SkCanvas* canvas, | 59 ReplaceDraw(SkCanvas* canvas, |
58 SkPicture const* const drawablePicts[], int drawableCount, | 60 SkPicture const* const drawablePicts[], int drawableCount, |
| 61 const SkPicture* topLevelPicture, |
59 const SkPicture* picture, | 62 const SkPicture* picture, |
60 const GrReplacements* replacements, | 63 const GrReplacements* replacements, |
61 const SkMatrix& initialMatrix, | 64 const SkMatrix& initialMatrix, |
62 SkDrawPictureCallback* callback) | 65 SkDrawPictureCallback* callback, |
| 66 const int* opIndices, int numIndices) |
63 : INHERITED(canvas, drawablePicts, NULL, drawableCount) | 67 : INHERITED(canvas, drawablePicts, NULL, drawableCount) |
64 , fCanvas(canvas) | 68 , fCanvas(canvas) |
| 69 , fTopLevelPicture(topLevelPicture) |
65 , fPicture(picture) | 70 , fPicture(picture) |
66 , fReplacements(replacements) | 71 , fReplacements(replacements) |
67 , fInitialMatrix(initialMatrix) | 72 , fInitialMatrix(initialMatrix) |
68 , fCallback(callback) | 73 , fCallback(callback) |
69 , fIndex(0) | 74 , fIndex(0) |
70 , fNumReplaced(0) { | 75 , fNumReplaced(0) { |
| 76 fOpIndexStack.append(numIndices, opIndices); |
71 } | 77 } |
72 | 78 |
73 int draw() { | 79 int draw() { |
74 const SkBBoxHierarchy* bbh = fPicture->fBBH.get(); | 80 const SkBBoxHierarchy* bbh = fPicture->fBBH.get(); |
75 const SkRecord* record = fPicture->fRecord.get(); | 81 const SkRecord* record = fPicture->fRecord.get(); |
76 if (NULL == record) { | 82 if (NULL == record) { |
77 return 0; | 83 return 0; |
78 } | 84 } |
79 | 85 |
80 fNumReplaced = 0; | 86 fNumReplaced = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
111 } | 117 } |
112 | 118 |
113 return fNumReplaced; | 119 return fNumReplaced; |
114 } | 120 } |
115 | 121 |
116 // Same as Draw for all ops except DrawPicture and SaveLayer. | 122 // Same as Draw for all ops except DrawPicture and SaveLayer. |
117 template <typename T> void operator()(const T& r) { | 123 template <typename T> void operator()(const T& r) { |
118 this->INHERITED::operator()(r); | 124 this->INHERITED::operator()(r); |
119 } | 125 } |
120 void operator()(const SkRecords::DrawPicture& dp) { | 126 void operator()(const SkRecords::DrawPicture& dp) { |
| 127 |
| 128 int drawPictureOffset; |
| 129 if (fOps.count()) { |
| 130 drawPictureOffset = fOps[fIndex]; |
| 131 } else { |
| 132 drawPictureOffset = fIndex; |
| 133 } |
| 134 |
| 135 fOpIndexStack.push(drawPictureOffset); |
| 136 |
121 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); | 137 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); |
122 | 138 |
123 // Draw sub-pictures with the same replacement list but a different pict
ure | 139 // Draw sub-pictures with the same replacement list but a different pict
ure |
124 ReplaceDraw draw(fCanvas, this->drawablePicts(), this->drawableCount(), | 140 ReplaceDraw draw(fCanvas, this->drawablePicts(), this->drawableCount(), |
125 dp.picture, fReplacements, fInitialMatrix, fCallback); | 141 fTopLevelPicture, dp.picture, fReplacements, fInitialMa
trix, fCallback, |
| 142 fOpIndexStack.begin(), fOpIndexStack.count()); |
126 | 143 |
127 fNumReplaced += draw.draw(); | 144 fNumReplaced += draw.draw(); |
| 145 |
| 146 fOpIndexStack.pop(); |
128 } | 147 } |
129 void operator()(const SkRecords::SaveLayer& sl) { | 148 void operator()(const SkRecords::SaveLayer& sl) { |
130 | 149 |
131 // For a saveLayer command, check if it can be replaced by a drawBitmap | 150 // For a saveLayer command, check if it can be replaced by a drawBitmap |
132 // call and, if so, draw it and then update the current op index accordi
ngly. | 151 // call and, if so, draw it and then update the current op index accordi
ngly. |
133 unsigned startOffset; | 152 unsigned startOffset; |
134 if (fOps.count()) { | 153 if (fOps.count()) { |
135 startOffset = fOps[fIndex]; | 154 startOffset = fOps[fIndex]; |
136 } else { | 155 } else { |
137 startOffset = fIndex; | 156 startOffset = fIndex; |
138 } | 157 } |
139 | 158 |
140 const SkMatrix& ctm = fCanvas->getTotalMatrix(); | 159 fOpIndexStack.push(startOffset); |
141 const GrReplacements::ReplacementInfo* ri = fReplacements->lookupByStart
( | 160 |
142 fPicture->uniqueID()
, | 161 const GrReplacements::ReplacementInfo* ri = fReplacements->lookup( |
143 startOffset, | 162 fTopLevelPic
ture->uniqueID(), |
144 ctm); | 163 fInitialMatr
ix, |
| 164 fOpIndexStac
k.begin(), |
| 165 fOpIndexStac
k.count()); |
145 | 166 |
146 if (ri) { | 167 if (ri) { |
147 fNumReplaced++; | 168 fNumReplaced++; |
148 draw_replacement_bitmap(ri, fCanvas); | 169 draw_replacement_bitmap(ri, fCanvas); |
149 | 170 |
150 if (fPicture->fBBH.get()) { | 171 if (fPicture->fBBH.get()) { |
151 while (fOps[fIndex] < ri->fStop) { | 172 while (fOps[fIndex] < ri->fStop) { |
152 ++fIndex; | 173 ++fIndex; |
153 } | 174 } |
154 SkASSERT(fOps[fIndex] == ri->fStop); | 175 SkASSERT(fOps[fIndex] == ri->fStop); |
155 } else { | 176 } else { |
156 fIndex = ri->fStop; | 177 fIndex = ri->fStop; |
157 } | 178 } |
| 179 fOpIndexStack.pop(); |
158 return; | 180 return; |
159 } | 181 } |
160 | 182 |
161 // This is a fail for layer hoisting | 183 // This is a fail for layer hoisting |
162 this->INHERITED::operator()(sl); | 184 this->INHERITED::operator()(sl); |
| 185 |
| 186 fOpIndexStack.pop(); |
163 } | 187 } |
164 | 188 |
165 private: | 189 private: |
166 SkCanvas* fCanvas; | 190 SkCanvas* fCanvas; |
| 191 const SkPicture* fTopLevelPicture; |
167 const SkPicture* fPicture; | 192 const SkPicture* fPicture; |
168 const GrReplacements* fReplacements; | 193 const GrReplacements* fReplacements; |
169 const SkMatrix fInitialMatrix; | 194 const SkMatrix fInitialMatrix; |
170 SkDrawPictureCallback* fCallback; | 195 SkDrawPictureCallback* fCallback; |
171 | 196 |
172 SkTDArray<unsigned> fOps; | 197 SkTDArray<unsigned> fOps; |
173 int fIndex; | 198 int fIndex; |
174 int fNumReplaced; | 199 int fNumReplaced; |
175 | 200 |
| 201 // The op code indices of all the enclosing drawPicture and saveLayer calls |
| 202 SkTDArray<int> fOpIndexStack; |
| 203 |
176 typedef Draw INHERITED; | 204 typedef Draw INHERITED; |
177 }; | 205 }; |
178 | 206 |
179 int GrRecordReplaceDraw(const SkPicture* picture, | 207 int GrRecordReplaceDraw(const SkPicture* picture, |
180 SkCanvas* canvas, | 208 SkCanvas* canvas, |
181 const GrReplacements* replacements, | 209 const GrReplacements* replacements, |
182 const SkMatrix& initialMatrix, | 210 const SkMatrix& initialMatrix, |
183 SkDrawPictureCallback* callback) { | 211 SkDrawPictureCallback* callback) { |
184 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); | 212 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
185 | 213 |
186 // TODO: drawablePicts? | 214 // TODO: drawablePicts? |
187 ReplaceDraw draw(canvas, NULL, 0, picture, replacements, initialMatrix, call
back); | 215 ReplaceDraw draw(canvas, NULL, 0, |
| 216 picture, picture, |
| 217 replacements, initialMatrix, callback, NULL, 0); |
188 | 218 |
189 return draw.draw(); | 219 return draw.draw(); |
190 } | 220 } |
OLD | NEW |