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