| 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 int sta
rt, | 15 unsigned int sta
rt, |
| 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, start
, key, keySize)); |
| 18 fReplacementHash.add(replacement); | 18 fReplacementHash.add(replacement); |
| 19 return replacement; | 19 return replacement; |
| 20 } | 20 } |
| 21 | 21 |
| 22 void GrReplacements::freeAll() { | 22 void GrReplacements::freeAll() { |
| 23 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key>::Iter iter(&fReplaceme
ntHash); | 23 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key>::Iter iter(&fReplaceme
ntHash); |
| 24 | 24 |
| 25 for (; !iter.done(); ++iter) { | 25 for (; !iter.done(); ++iter) { |
| 26 ReplacementInfo* replacement = &(*iter); | 26 ReplacementInfo* replacement = &(*iter); |
| 27 SkDELETE(replacement); | 27 SkDELETE(replacement); |
| 28 } | 28 } |
| 29 | 29 |
| 30 fReplacementHash.reset(); | 30 fReplacementHash.reset(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 const GrReplacements::ReplacementInfo* GrReplacements::lookupByStart(uint32_t pi
ctureID, | 33 const GrReplacements::ReplacementInfo* GrReplacements::lookupByStart(uint32_t pi
ctureID, int start, |
| 34 size_t star
t, | 34 const int*
key, int keySize) const { |
| 35 const SkMat
rix& ctm) const { | 35 return fReplacementHash.find(ReplacementInfo::Key(pictureID, start, key, key
Size)); |
| 36 return fReplacementHash.find(ReplacementInfo::Key(pictureID, start, ctm)); | |
| 37 } | 36 } |
| 38 | 37 |
| 39 static inline void draw_replacement_bitmap(const GrReplacements::ReplacementInfo
* ri, | 38 static inline void draw_replacement_bitmap(const GrReplacements::ReplacementInfo
* ri, |
| 40 SkCanvas* canvas, | 39 SkCanvas* canvas, |
| 41 const SkMatrix& initialMatrix) { | 40 const SkMatrix& initialMatrix) { |
| 42 SkRect src = SkRect::Make(ri->fSrcRect); | 41 SkRect src = SkRect::Make(ri->fSrcRect); |
| 43 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(ri->fPos.fX), | 42 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(ri->fPos.fX), |
| 44 SkIntToScalar(ri->fPos.fY), | 43 SkIntToScalar(ri->fPos.fY), |
| 45 SkIntToScalar(ri->fSrcRect.width()), | 44 SkIntToScalar(ri->fSrcRect.width()), |
| 46 SkIntToScalar(ri->fSrcRect.height())); | 45 SkIntToScalar(ri->fSrcRect.height())); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 111 } |
| 113 | 112 |
| 114 return fNumReplaced; | 113 return fNumReplaced; |
| 115 } | 114 } |
| 116 | 115 |
| 117 // Same as Draw for all ops except DrawPicture and SaveLayer. | 116 // Same as Draw for all ops except DrawPicture and SaveLayer. |
| 118 template <typename T> void operator()(const T& r) { | 117 template <typename T> void operator()(const T& r) { |
| 119 this->INHERITED::operator()(r); | 118 this->INHERITED::operator()(r); |
| 120 } | 119 } |
| 121 void operator()(const SkRecords::DrawPicture& dp) { | 120 void operator()(const SkRecords::DrawPicture& dp) { |
| 121 |
| 122 int curOffset; |
| 123 if (fOps.count()) { |
| 124 curOffset = fOps[fIndex]; |
| 125 } else { |
| 126 curOffset = fIndex; |
| 127 } |
| 128 |
| 129 fSaveLayerOpStack.push(curOffset); |
| 130 |
| 122 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); | 131 SkAutoCanvasMatrixPaint acmp(fCanvas, dp.matrix, dp.paint, dp.picture->c
ullRect()); |
| 123 | 132 |
| 124 // Draw sub-pictures with the same replacement list but a different pict
ure | 133 // Draw sub-pictures with the same replacement list but a different pict
ure |
| 125 ReplaceDraw draw(fCanvas, this->drawablePicts(), this->drawableCount(), | 134 ReplaceDraw draw(fCanvas, this->drawablePicts(), this->drawableCount(), |
| 126 dp.picture, fReplacements, fInitialMatrix, fCallback); | 135 dp.picture, fReplacements, fInitialMatrix, fCallback); |
| 127 | 136 |
| 128 fNumReplaced += draw.draw(); | 137 fNumReplaced += draw.draw(); |
| 138 |
| 139 fSaveLayerOpStack.pop(); |
| 129 } | 140 } |
| 130 void operator()(const SkRecords::SaveLayer& sl) { | 141 void operator()(const SkRecords::SaveLayer& sl) { |
| 131 | 142 |
| 132 // For a saveLayer command, check if it can be replaced by a drawBitmap | 143 // For a saveLayer command, check if it can be replaced by a drawBitmap |
| 133 // call and, if so, draw it and then update the current op index accordi
ngly. | 144 // call and, if so, draw it and then update the current op index accordi
ngly. |
| 134 size_t startOffset; | 145 size_t startOffset; |
| 135 if (fOps.count()) { | 146 if (fOps.count()) { |
| 136 startOffset = fOps[fIndex]; | 147 startOffset = fOps[fIndex]; |
| 137 } else { | 148 } else { |
| 138 startOffset = fIndex; | 149 startOffset = fIndex; |
| 139 } | 150 } |
| 140 | 151 |
| 141 const SkMatrix& ctm = fCanvas->getTotalMatrix(); | 152 fSaveLayerOpStack.push(startOffset); |
| 153 |
| 142 const GrReplacements::ReplacementInfo* ri = fReplacements->lookupByStart
( | 154 const GrReplacements::ReplacementInfo* ri = fReplacements->lookupByStart
( |
| 143 fPicture->uniqueID()
, | 155 fPicture->uniqueID()
, startOffset, |
| 144 startOffset, | 156 fSaveLayerOpStack.be
gin(), fSaveLayerOpStack.count()); |
| 145 ctm); | |
| 146 | 157 |
| 147 if (ri) { | 158 if (ri) { |
| 148 fNumReplaced++; | 159 fNumReplaced++; |
| 149 draw_replacement_bitmap(ri, fCanvas, fInitialMatrix); | 160 draw_replacement_bitmap(ri, fCanvas, fInitialMatrix); |
| 150 | 161 |
| 151 if (fPicture->fBBH.get()) { | 162 if (fPicture->fBBH.get()) { |
| 152 while (fOps[fIndex] < ri->fStop) { | 163 while (fOps[fIndex] < ri->fStop) { |
| 153 ++fIndex; | 164 ++fIndex; |
| 154 } | 165 } |
| 155 SkASSERT(fOps[fIndex] == ri->fStop); | 166 SkASSERT(fOps[fIndex] == ri->fStop); |
| 156 } else { | 167 } else { |
| 157 fIndex = ri->fStop; | 168 fIndex = ri->fStop; |
| 158 } | 169 } |
| 170 fSaveLayerOpStack.pop(); |
| 159 return; | 171 return; |
| 160 } | 172 } |
| 161 | 173 |
| 162 // This is a fail for layer hoisting | 174 // This is a fail for layer hoisting |
| 163 this->INHERITED::operator()(sl); | 175 this->INHERITED::operator()(sl); |
| 176 |
| 177 fSaveLayerOpStack.pop(); |
| 164 } | 178 } |
| 165 | 179 |
| 166 private: | 180 private: |
| 167 SkCanvas* fCanvas; | 181 SkCanvas* fCanvas; |
| 168 const SkPicture* fPicture; | 182 const SkPicture* fPicture; |
| 169 const GrReplacements* fReplacements; | 183 const GrReplacements* fReplacements; |
| 170 const SkMatrix fInitialMatrix; | 184 const SkMatrix fInitialMatrix; |
| 171 SkDrawPictureCallback* fCallback; | 185 SkDrawPictureCallback* fCallback; |
| 172 | 186 |
| 173 SkTDArray<unsigned> fOps; | 187 SkTDArray<unsigned> fOps; |
| 174 int fIndex; | 188 int fIndex; |
| 175 int fNumReplaced; | 189 int fNumReplaced; |
| 176 | 190 |
| 191 SkTDArray<int> fSaveLayerOpStack; |
| 192 |
| 177 typedef Draw INHERITED; | 193 typedef Draw INHERITED; |
| 178 }; | 194 }; |
| 179 | 195 |
| 180 int GrRecordReplaceDraw(const SkPicture* picture, | 196 int GrRecordReplaceDraw(const SkPicture* picture, |
| 181 SkCanvas* canvas, | 197 SkCanvas* canvas, |
| 182 const GrReplacements* replacements, | 198 const GrReplacements* replacements, |
| 183 const SkMatrix& initialMatrix, | 199 const SkMatrix& initialMatrix, |
| 184 SkDrawPictureCallback* callback) { | 200 SkDrawPictureCallback* callback) { |
| 185 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); | 201 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
| 186 | 202 |
| 187 // TODO: drawablePicts? | 203 // TODO: drawablePicts? |
| 188 ReplaceDraw draw(canvas, NULL, 0, picture, replacements, initialMatrix, call
back); | 204 ReplaceDraw draw(canvas, NULL, 0, picture, replacements, initialMatrix, call
back); |
| 189 | 205 |
| 190 return draw.draw(); | 206 return draw.draw(); |
| 191 } | 207 } |
| OLD | NEW |