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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 const SkPicture* picture, | 59 const SkPicture* picture, |
60 const GrReplacements* replacements, | 60 const GrReplacements* replacements, |
61 const SkMatrix& initialMatrix, | 61 const SkMatrix& initialMatrix, |
62 SkDrawPictureCallback* callback) | 62 SkDrawPictureCallback* callback) |
63 : INHERITED(canvas) | 63 : INHERITED(canvas) |
64 , fCanvas(canvas) | 64 , fCanvas(canvas) |
65 , fPicture(picture) | 65 , fPicture(picture) |
66 , fReplacements(replacements) | 66 , fReplacements(replacements) |
67 , fInitialMatrix(initialMatrix) | 67 , fInitialMatrix(initialMatrix) |
68 , fCallback(callback) | 68 , fCallback(callback) |
69 , fIndex(0) { | 69 , fIndex(0) |
| 70 , fNumReplaced(0) { |
70 } | 71 } |
71 | 72 |
72 void draw() { | 73 int draw() { |
73 const SkBBoxHierarchy* bbh = fPicture->fBBH.get(); | 74 const SkBBoxHierarchy* bbh = fPicture->fBBH.get(); |
74 const SkRecord* record = fPicture->fRecord.get(); | 75 const SkRecord* record = fPicture->fRecord.get(); |
75 if (NULL == record) { | 76 if (NULL == record) { |
76 return; | 77 return 0; |
77 } | 78 } |
78 | 79 |
| 80 fNumReplaced = 0; |
| 81 |
79 fOps.rewind(); | 82 fOps.rewind(); |
80 | 83 |
81 if (bbh) { | 84 if (bbh) { |
82 // Draw only ops that affect pixels in the canvas's current clip. | 85 // Draw only ops that affect pixels in the canvas's current clip. |
83 // The SkRecord and BBH were recorded in identity space. This canva
s | 86 // The SkRecord and BBH were recorded in identity space. This canva
s |
84 // is not necessarily in that same space. getClipBounds() returns u
s | 87 // is not necessarily in that same space. getClipBounds() returns u
s |
85 // this canvas' clip bounds transformed back into identity space, wh
ich | 88 // this canvas' clip bounds transformed back into identity space, wh
ich |
86 // lets us query the BBH. | 89 // lets us query the BBH. |
87 SkRect query = { 0, 0, 0, 0 }; | 90 SkRect query = { 0, 0, 0, 0 }; |
88 (void)fCanvas->getClipBounds(&query); | 91 (void)fCanvas->getClipBounds(&query); |
89 | 92 |
90 bbh->search(query, &fOps); | 93 bbh->search(query, &fOps); |
91 | 94 |
92 for (fIndex = 0; fIndex < fOps.count(); ++fIndex) { | 95 for (fIndex = 0; fIndex < fOps.count(); ++fIndex) { |
93 if (fCallback && fCallback->abortDrawing()) { | 96 if (fCallback && fCallback->abortDrawing()) { |
94 return; | 97 return fNumReplaced; |
95 } | 98 } |
96 | 99 |
97 record->visit<void>(fOps[fIndex], *this); | 100 record->visit<void>(fOps[fIndex], *this); |
98 } | 101 } |
99 | 102 |
100 } else { | 103 } else { |
101 for (fIndex = 0; fIndex < (int) record->count(); ++fIndex) { | 104 for (fIndex = 0; fIndex < (int) record->count(); ++fIndex) { |
102 if (fCallback && fCallback->abortDrawing()) { | 105 if (fCallback && fCallback->abortDrawing()) { |
103 return; | 106 return fNumReplaced; |
104 } | 107 } |
105 | 108 |
106 record->visit<void>(fIndex, *this); | 109 record->visit<void>(fIndex, *this); |
107 } | 110 } |
108 } | 111 } |
| 112 |
| 113 return fNumReplaced; |
109 } | 114 } |
110 | 115 |
111 // Same as Draw for all ops except DrawPicture and SaveLayer. | 116 // Same as Draw for all ops except DrawPicture and SaveLayer. |
112 template <typename T> void operator()(const T& r) { | 117 template <typename T> void operator()(const T& r) { |
113 this->INHERITED::operator()(r); | 118 this->INHERITED::operator()(r); |
114 } | 119 } |
115 void operator()(const SkRecords::DrawPicture& dp) { | 120 void operator()(const SkRecords::DrawPicture& dp) { |
116 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); | 121 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); |
117 | 122 |
118 // Draw sub-pictures with the same replacement list but a different pict
ure | 123 // Draw sub-pictures with the same replacement list but a different pict
ure |
119 ReplaceDraw draw(fCanvas, dp.picture, fReplacements, fInitialMatrix, fCa
llback); | 124 ReplaceDraw draw(fCanvas, dp.picture, fReplacements, fInitialMatrix, fCa
llback); |
120 | 125 |
121 draw.draw(); | 126 fNumReplaced += draw.draw(); |
122 } | 127 } |
123 void operator()(const SkRecords::SaveLayer& sl) { | 128 void operator()(const SkRecords::SaveLayer& sl) { |
124 | 129 |
125 // For a saveLayer command, check if it can be replaced by a drawBitmap | 130 // For a saveLayer command, check if it can be replaced by a drawBitmap |
126 // call and, if so, draw it and then update the current op index accordi
ngly. | 131 // call and, if so, draw it and then update the current op index accordi
ngly. |
127 size_t startOffset; | 132 size_t startOffset; |
128 if (fOps.count()) { | 133 if (fOps.count()) { |
129 startOffset = fOps[fIndex]; | 134 startOffset = fOps[fIndex]; |
130 } else { | 135 } else { |
131 startOffset = fIndex; | 136 startOffset = fIndex; |
132 } | 137 } |
133 | 138 |
| 139 const SkMatrix& ctm = fCanvas->getTotalMatrix(); |
134 const GrReplacements::ReplacementInfo* ri = fReplacements->lookupByStart
( | 140 const GrReplacements::ReplacementInfo* ri = fReplacements->lookupByStart
( |
135 fPicture->uniqueID()
, | 141 fPicture->uniqueID()
, |
136 startOffset, | 142 startOffset, |
137 fCanvas->getTotalMat
rix()); | 143 ctm); |
138 | 144 |
139 if (ri) { | 145 if (ri) { |
| 146 fNumReplaced++; |
140 draw_replacement_bitmap(ri, fCanvas, fInitialMatrix); | 147 draw_replacement_bitmap(ri, fCanvas, fInitialMatrix); |
141 | 148 |
142 if (fPicture->fBBH.get()) { | 149 if (fPicture->fBBH.get()) { |
143 while (fOps[fIndex] < ri->fStop) { | 150 while (fOps[fIndex] < ri->fStop) { |
144 ++fIndex; | 151 ++fIndex; |
145 } | 152 } |
146 SkASSERT(fOps[fIndex] == ri->fStop); | 153 SkASSERT(fOps[fIndex] == ri->fStop); |
147 } else { | 154 } else { |
148 fIndex = ri->fStop; | 155 fIndex = ri->fStop; |
149 } | 156 } |
150 return; | 157 return; |
151 } | 158 } |
152 | 159 |
153 // This is a fail for layer hoisting | 160 // This is a fail for layer hoisting |
154 this->INHERITED::operator()(sl); | 161 this->INHERITED::operator()(sl); |
155 } | 162 } |
156 | 163 |
157 private: | 164 private: |
158 SkCanvas* fCanvas; | 165 SkCanvas* fCanvas; |
159 const SkPicture* fPicture; | 166 const SkPicture* fPicture; |
160 const GrReplacements* fReplacements; | 167 const GrReplacements* fReplacements; |
161 const SkMatrix fInitialMatrix; | 168 const SkMatrix fInitialMatrix; |
162 SkDrawPictureCallback* fCallback; | 169 SkDrawPictureCallback* fCallback; |
163 | 170 |
164 SkTDArray<unsigned> fOps; | 171 SkTDArray<unsigned> fOps; |
165 int fIndex; | 172 int fIndex; |
| 173 int fNumReplaced; |
166 | 174 |
167 typedef Draw INHERITED; | 175 typedef Draw INHERITED; |
168 }; | 176 }; |
169 | 177 |
170 void GrRecordReplaceDraw(const SkPicture* picture, | 178 int GrRecordReplaceDraw(const SkPicture* picture, |
171 SkCanvas* canvas, | 179 SkCanvas* canvas, |
172 const GrReplacements* replacements, | 180 const GrReplacements* replacements, |
173 const SkMatrix& initialMatrix, | 181 const SkMatrix& initialMatrix, |
174 SkDrawPictureCallback* callback) { | 182 SkDrawPictureCallback* callback) { |
175 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); | 183 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
176 | 184 |
177 ReplaceDraw draw(canvas, picture, replacements, initialMatrix, callback); | 185 ReplaceDraw draw(canvas, picture, replacements, initialMatrix, callback); |
178 | 186 |
179 draw.draw(); | 187 return draw.draw(); |
180 } | 188 } |
OLD | NEW |