| 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" | 8 #include "GrContext.h" |
| 9 #include "GrLayerCache.h" | 9 #include "GrLayerCache.h" |
| 10 #include "GrRecordReplaceDraw.h" | 10 #include "GrRecordReplaceDraw.h" |
| 11 #include "SkBigPicture.h" |
| 11 #include "SkCanvasPriv.h" | 12 #include "SkCanvasPriv.h" |
| 12 #include "SkGrPixelRef.h" | 13 #include "SkGrPixelRef.h" |
| 13 #include "SkImage.h" | 14 #include "SkImage.h" |
| 14 #include "SkRecordDraw.h" | 15 #include "SkRecordDraw.h" |
| 15 #include "SkRecords.h" | 16 #include "SkRecords.h" |
| 16 | 17 |
| 17 static inline void wrap_texture(GrTexture* texture, int width, int height, SkBit
map* result) { | 18 static inline void wrap_texture(GrTexture* texture, int width, int height, SkBit
map* result) { |
| 18 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 19 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 19 result->setInfo(info); | 20 result->setInfo(info); |
| 20 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); | 21 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 38 const SkRect src = SkRect::Make(layer->rect()); | 39 const SkRect src = SkRect::Make(layer->rect()); |
| 39 const SkRect dst = SkRect::Make(layer->srcIR()); | 40 const SkRect dst = SkRect::Make(layer->srcIR()); |
| 40 | 41 |
| 41 SkASSERT(layer->offset().isZero()); | 42 SkASSERT(layer->offset().isZero()); |
| 42 | 43 |
| 43 canvas->save(); | 44 canvas->save(); |
| 44 canvas->setMatrix(SkMatrix::I()); | 45 canvas->setMatrix(SkMatrix::I()); |
| 45 canvas->drawBitmapRectToRect(bm, &src, dst, layer->paint()); | 46 canvas->drawBitmapRectToRect(bm, &src, dst, layer->paint()); |
| 46 canvas->restore(); | 47 canvas->restore(); |
| 47 } else { | 48 } else { |
| 48 canvas->drawSprite(bm, | 49 canvas->drawSprite(bm, |
| 49 layer->srcIR().fLeft + layer->offset().fX, | 50 layer->srcIR().fLeft + layer->offset().fX, |
| 50 layer->srcIR().fTop + layer->offset().fY, | 51 layer->srcIR().fTop + layer->offset().fY, |
| 51 layer->paint()); | 52 layer->paint()); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Used by GrRecordReplaceDraw. It intercepts nested drawPicture calls and | 56 // Used by GrRecordReplaceDraw. It intercepts nested drawPicture calls and |
| 56 // also draws them with replaced layers. | 57 // also draws them with replaced layers. |
| 57 class ReplaceDraw : public SkRecords::Draw { | 58 class ReplaceDraw : public SkRecords::Draw { |
| 58 public: | 59 public: |
| 59 ReplaceDraw(SkCanvas* canvas, GrLayerCache* layerCache, | 60 ReplaceDraw(SkCanvas* canvas, GrLayerCache* layerCache, |
| 60 SkPicture const* const drawablePicts[], int drawableCount, | 61 SkPicture const* const drawablePicts[], int drawableCount, |
| 61 const SkPicture* topLevelPicture, | 62 const SkPicture* topLevelPicture, |
| 62 const SkPicture* picture, | 63 const SkBigPicture* picture, |
| 63 const SkMatrix& initialMatrix, | 64 const SkMatrix& initialMatrix, |
| 64 SkPicture::AbortCallback* callback, | 65 SkPicture::AbortCallback* callback, |
| 65 const unsigned* opIndices, int numIndices) | 66 const unsigned* opIndices, int numIndices) |
| 66 : INHERITED(canvas, drawablePicts, NULL, drawableCount) | 67 : INHERITED(canvas, drawablePicts, NULL, drawableCount) |
| 67 , fCanvas(canvas) | 68 , fCanvas(canvas) |
| 68 , fLayerCache(layerCache) | 69 , fLayerCache(layerCache) |
| 69 , fTopLevelPicture(topLevelPicture) | 70 , fTopLevelPicture(topLevelPicture) |
| 70 , fPicture(picture) | 71 , fPicture(picture) |
| 71 , fInitialMatrix(initialMatrix) | 72 , fInitialMatrix(initialMatrix) |
| 72 , fCallback(callback) | 73 , fCallback(callback) |
| 73 , fIndex(0) | 74 , fIndex(0) |
| 74 , fNumReplaced(0) { | 75 , fNumReplaced(0) { |
| 75 fOpIndexStack.append(numIndices, opIndices); | 76 fOpIndexStack.append(numIndices, opIndices); |
| 76 } | 77 } |
| 77 | 78 |
| 78 int draw() { | 79 int draw() { |
| 79 const SkBBoxHierarchy* bbh = fPicture->fBBH.get(); | 80 const SkBBoxHierarchy* bbh = fPicture->bbh(); |
| 80 const SkRecord* record = fPicture->fRecord.get(); | 81 const SkRecord* record = fPicture->record(); |
| 81 if (NULL == record) { | 82 if (NULL == record) { |
| 82 return 0; | 83 return 0; |
| 83 } | 84 } |
| 84 | 85 |
| 85 fNumReplaced = 0; | 86 fNumReplaced = 0; |
| 86 | 87 |
| 87 fOps.rewind(); | 88 fOps.rewind(); |
| 88 | 89 |
| 89 if (bbh) { | 90 if (bbh) { |
| 90 // Draw only ops that affect pixels in the canvas's current clip. | 91 // Draw only ops that affect pixels in the canvas's current clip. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 if (fOps.count()) { | 129 if (fOps.count()) { |
| 129 drawPictureOffset = fOps[fIndex]; | 130 drawPictureOffset = fOps[fIndex]; |
| 130 } else { | 131 } else { |
| 131 drawPictureOffset = fIndex; | 132 drawPictureOffset = fIndex; |
| 132 } | 133 } |
| 133 | 134 |
| 134 fOpIndexStack.push(drawPictureOffset); | 135 fOpIndexStack.push(drawPictureOffset); |
| 135 | 136 |
| 136 SkAutoCanvasMatrixPaint acmp(fCanvas, &dp.matrix, dp.paint, dp.picture->
cullRect()); | 137 SkAutoCanvasMatrixPaint acmp(fCanvas, &dp.matrix, dp.paint, dp.picture->
cullRect()); |
| 137 | 138 |
| 138 // Draw sub-pictures with the same replacement list but a different pict
ure | 139 if (const SkBigPicture* bp = dp.picture->asSkBigPicture()) { |
| 139 ReplaceDraw draw(fCanvas, fLayerCache, | 140 // Draw sub-pictures with the same replacement list but a different
picture |
| 140 this->drawablePicts(), this->drawableCount(), | 141 ReplaceDraw draw(fCanvas, fLayerCache, |
| 141 fTopLevelPicture, dp.picture, fInitialMatrix, fCallback
, | 142 this->drawablePicts(), this->drawableCount(), |
| 142 fOpIndexStack.begin(), fOpIndexStack.count()); | 143 fTopLevelPicture, bp, fInitialMatrix, fCallback, |
| 143 | 144 fOpIndexStack.begin(), fOpIndexStack.count()); |
| 144 fNumReplaced += draw.draw(); | 145 fNumReplaced += draw.draw(); |
| 146 } else { |
| 147 // TODO: can we assume / assert this doesn't happen? |
| 148 dp.picture->playback(fCanvas, fCallback); |
| 149 } |
| 145 | 150 |
| 146 fOpIndexStack.pop(); | 151 fOpIndexStack.pop(); |
| 147 } | 152 } |
| 148 void operator()(const SkRecords::SaveLayer& sl) { | 153 void operator()(const SkRecords::SaveLayer& sl) { |
| 149 | 154 |
| 150 // For a saveLayer command, check if it can be replaced by a drawBitmap | 155 // 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. | 156 // call and, if so, draw it and then update the current op index accordi
ngly. |
| 152 unsigned startOffset; | 157 unsigned startOffset; |
| 153 if (fOps.count()) { | 158 if (fOps.count()) { |
| 154 startOffset = fOps[fIndex]; | 159 startOffset = fOps[fIndex]; |
| 155 } else { | 160 } else { |
| 156 startOffset = fIndex; | 161 startOffset = fIndex; |
| 157 } | 162 } |
| 158 | 163 |
| 159 fOpIndexStack.push(startOffset); | 164 fOpIndexStack.push(startOffset); |
| 160 | 165 |
| 161 GrCachedLayer* layer = fLayerCache->findLayer(fTopLevelPicture->uniqueID
(), | 166 GrCachedLayer* layer = fLayerCache->findLayer(fTopLevelPicture->uniqueID
(), |
| 162 fInitialMatrix, | 167 fInitialMatrix, |
| 163 fOpIndexStack.begin(), | 168 fOpIndexStack.begin(), |
| 164 fOpIndexStack.count()); | 169 fOpIndexStack.count()); |
| 165 | 170 |
| 166 if (layer) { | 171 if (layer) { |
| 167 fNumReplaced++; | 172 fNumReplaced++; |
| 168 | 173 |
| 169 draw_replacement_bitmap(layer, fCanvas); | 174 draw_replacement_bitmap(layer, fCanvas); |
| 170 | 175 |
| 171 if (fPicture->fBBH.get()) { | 176 if (fPicture->bbh()) { |
| 172 while (fOps[fIndex] < layer->stop()) { | 177 while (fOps[fIndex] < layer->stop()) { |
| 173 ++fIndex; | 178 ++fIndex; |
| 174 } | 179 } |
| 175 SkASSERT(fOps[fIndex] == layer->stop()); | 180 SkASSERT(fOps[fIndex] == layer->stop()); |
| 176 } else { | 181 } else { |
| 177 fIndex = layer->stop(); | 182 fIndex = layer->stop(); |
| 178 } | 183 } |
| 179 fOpIndexStack.pop(); | 184 fOpIndexStack.pop(); |
| 180 return; | 185 return; |
| 181 } | 186 } |
| 182 | 187 |
| 183 // This is a fail for layer hoisting | 188 // This is a fail for layer hoisting |
| 184 this->INHERITED::operator()(sl); | 189 this->INHERITED::operator()(sl); |
| 185 | 190 |
| 186 fOpIndexStack.pop(); | 191 fOpIndexStack.pop(); |
| 187 } | 192 } |
| 188 | 193 |
| 189 private: | 194 private: |
| 190 SkCanvas* fCanvas; | 195 SkCanvas* fCanvas; |
| 191 GrLayerCache* fLayerCache; | 196 GrLayerCache* fLayerCache; |
| 192 const SkPicture* fTopLevelPicture; | 197 const SkPicture* fTopLevelPicture; |
| 193 const SkPicture* fPicture; | 198 const SkBigPicture* fPicture; |
| 194 const SkMatrix fInitialMatrix; | 199 const SkMatrix fInitialMatrix; |
| 195 SkPicture::AbortCallback* fCallback; | 200 SkPicture::AbortCallback* fCallback; |
| 196 | 201 |
| 197 SkTDArray<unsigned> fOps; | 202 SkTDArray<unsigned> fOps; |
| 198 int fIndex; | 203 int fIndex; |
| 199 int fNumReplaced; | 204 int fNumReplaced; |
| 200 | 205 |
| 201 // The op code indices of all the enclosing drawPicture and saveLayer calls | 206 // The op code indices of all the enclosing drawPicture and saveLayer calls |
| 202 SkTDArray<unsigned> fOpIndexStack; | 207 SkTDArray<unsigned> fOpIndexStack; |
| 203 | 208 |
| 204 typedef Draw INHERITED; | 209 typedef Draw INHERITED; |
| 205 }; | 210 }; |
| 206 | 211 |
| 207 int GrRecordReplaceDraw(const SkPicture* picture, | 212 int GrRecordReplaceDraw(const SkPicture* picture, |
| 208 SkCanvas* canvas, | 213 SkCanvas* canvas, |
| 209 GrLayerCache* layerCache, | 214 GrLayerCache* layerCache, |
| 210 const SkMatrix& initialMatrix, | 215 const SkMatrix& initialMatrix, |
| 211 SkPicture::AbortCallback* callback) { | 216 SkPicture::AbortCallback* callback) { |
| 212 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); | 217 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
| 213 | 218 |
| 214 // TODO: drawablePicts? | 219 if (const SkBigPicture* bp = picture->asSkBigPicture()) { |
| 215 ReplaceDraw draw(canvas, layerCache, NULL, 0, | 220 // TODO: drawablePicts? |
| 216 picture, picture, | 221 ReplaceDraw draw(canvas, layerCache, NULL, 0, |
| 217 initialMatrix, callback, NULL, 0); | 222 bp, bp, |
| 218 return draw.draw(); | 223 initialMatrix, callback, NULL, 0); |
| 224 return draw.draw(); |
| 225 } else { |
| 226 // TODO: can we assume / assert this doesn't happen? |
| 227 picture->playback(canvas, callback); |
| 228 return 0; |
| 229 } |
| 219 } | 230 } |
| OLD | NEW |