commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrPictureUtils.h" |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 9 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 10 | #include "SkPaintPriv.h" |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 11 | #include "SkRecord.h" |
| 12 | #include "SkRecords.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 13 | |
robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 14 | SkPicture::AccelData::Key GrAccelData::ComputeAccelDataKey() { |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 15 | static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain(); |
| 16 | |
| 17 | return gGPUID; |
| 18 | } |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 19 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 20 | // SkRecord visitor to gather saveLayer/restore information. |
| 21 | class CollectLayers { |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 22 | public: |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 23 | CollectLayers(const SkPicture* pict, GrAccelData* accelData) |
| 24 | : fPictureID(pict->uniqueID()) |
| 25 | , fCTM(&SkMatrix::I()) |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 26 | , fSaveLayersInStack(0) |
| 27 | , fAccelData(accelData) { |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 28 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 29 | pict->cullRect().roundOut(&fCurrentClipBounds); |
| 30 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 31 | if (NULL == pict->fRecord.get()) { |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 32 | return; |
| 33 | } |
| 34 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 35 | for (fCurrentOp = 0; fCurrentOp < pict->fRecord->count(); ++fCurrentOp) { |
| 36 | pict->fRecord->visit<void>(fCurrentOp, *this); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 37 | } |
| 38 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 39 | while (!fSaveStack.isEmpty()) { |
| 40 | this->popSaveBlock(); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 44 | template <typename T> void operator()(const T& op) { |
| 45 | this->updateCTM(op); |
| 46 | this->updateClipBounds(op); |
| 47 | this->trackSaveLayers(op); |
| 48 | } |
| 49 | |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 50 | private: |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 51 | |
| 52 | class SaveInfo { |
| 53 | public: |
| 54 | SaveInfo() { } |
| 55 | SaveInfo(int opIndex, bool isSaveLayer, const SkPaint* paint, const SkIRect& bounds) |
| 56 | : fStartIndex(opIndex) |
| 57 | , fIsSaveLayer(isSaveLayer) |
| 58 | , fHasNestedSaveLayer(false) |
| 59 | , fPaint(paint) |
| 60 | , fBounds(bounds) { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | int fStartIndex; |
| 65 | bool fIsSaveLayer; |
| 66 | bool fHasNestedSaveLayer; |
| 67 | const SkPaint* fPaint; |
| 68 | SkIRect fBounds; |
| 69 | }; |
| 70 | |
| 71 | uint32_t fPictureID; |
| 72 | unsigned int fCurrentOp; |
| 73 | const SkMatrix* fCTM; |
| 74 | SkIRect fCurrentClipBounds; |
| 75 | int fSaveLayersInStack; |
| 76 | SkTDArray<SaveInfo> fSaveStack; |
| 77 | GrAccelData* fAccelData; |
| 78 | |
| 79 | template <typename T> void updateCTM(const T&) { /* most ops don't change the CTM */ } |
| 80 | void updateCTM(const SkRecords::Restore& op) { fCTM = &op.matrix; } |
| 81 | void updateCTM(const SkRecords::SetMatrix& op) { fCTM = &op.matrix; } |
| 82 | |
| 83 | template <typename T> void updateClipBounds(const T&) { /* most ops don't change the clip */ } |
| 84 | // Each of these devBounds fields is the state of the device bounds after the op. |
| 85 | // So Restore's devBounds are those bounds saved by its paired Save or SaveLayer. |
| 86 | void updateClipBounds(const SkRecords::Restore& op) { fCurrentClipBounds = op.devBounds; } |
| 87 | void updateClipBounds(const SkRecords::ClipPath& op) { fCurrentClipBounds = op.devBounds; } |
| 88 | void updateClipBounds(const SkRecords::ClipRRect& op) { fCurrentClipBounds = op.devBounds; } |
| 89 | void updateClipBounds(const SkRecords::ClipRect& op) { fCurrentClipBounds = op.devBounds; } |
| 90 | void updateClipBounds(const SkRecords::ClipRegion& op) { fCurrentClipBounds = op.devBounds; } |
| 91 | void updateClipBounds(const SkRecords::SaveLayer& op) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 92 | if (op.bounds) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 93 | fCurrentClipBounds.intersect(this->adjustAndMap(*op.bounds, op.paint)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | template <typename T> void trackSaveLayers(const T& op) { |
| 98 | /* most ops aren't involved in saveLayers */ |
| 99 | } |
| 100 | void trackSaveLayers(const SkRecords::Save& s) { this->pushSaveBlock(); } |
| 101 | void trackSaveLayers(const SkRecords::SaveLayer& sl) { this->pushSaveLayerBlock(sl.paint); } |
| 102 | void trackSaveLayers(const SkRecords::Restore& r) { this->popSaveBlock(); } |
| 103 | void trackSaveLayers(const SkRecords::DrawPicture& dp) { |
| 104 | // For sub-pictures, we wrap their layer information within the parent |
| 105 | // picture's rendering hierarchy |
| 106 | const GrAccelData* childData = GPUOptimize(dp.picture); |
| 107 | |
| 108 | for (int i = 0; i < childData->numSaveLayers(); ++i) { |
| 109 | const GrAccelData::SaveLayerInfo& src = childData->saveLayerInfo(i); |
| 110 | |
| 111 | this->updateStackForSaveLayer(); |
| 112 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 113 | // TODO: need to store an SkRect in GrAccelData::SaveLayerInfo? |
| 114 | SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(src.fOffset.fX), |
| 115 | SkIntToScalar(src.fOffset.fY), |
| 116 | SkIntToScalar(src.fSize.width()), |
| 117 | SkIntToScalar(src.fSize.height())); |
| 118 | SkIRect newClip(fCurrentClipBounds); |
| 119 | newClip.intersect(this->adjustAndMap(srcRect, dp.paint)); |
| 120 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 121 | GrAccelData::SaveLayerInfo& dst = fAccelData->addSaveLayerInfo(); |
| 122 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 123 | // If src.fPicture is NULL the layer is in dp.picture; otherwise |
| 124 | // it belongs to a sub-picture. |
| 125 | dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPicture*>(dp.picture); |
| 126 | dst.fPicture->ref(); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 127 | dst.fSize = SkISize::Make(newClip.width(), newClip.height()); |
| 128 | dst.fOffset = SkIPoint::Make(newClip.fLeft, newClip.fTop); |
robertphillips | b5a9715 | 2014-09-30 11:33:02 -0700 | [diff] [blame] | 129 | dst.fOriginXform = src.fOriginXform; |
| 130 | dst.fOriginXform.postConcat(*fCTM); |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 131 | if (src.fPaint) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 132 | dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint)); |
| 133 | } |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 134 | dst.fSaveLayerOpID = src.fSaveLayerOpID; |
| 135 | dst.fRestoreOpID = src.fRestoreOpID; |
| 136 | dst.fHasNestedLayers = src.fHasNestedLayers; |
| 137 | dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested; |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | void pushSaveBlock() { |
| 142 | fSaveStack.push(SaveInfo(fCurrentOp, false, NULL, SkIRect::MakeEmpty())); |
| 143 | } |
| 144 | |
| 145 | // Inform all the saveLayers already on the stack that they now have a |
| 146 | // nested saveLayer inside them |
| 147 | void updateStackForSaveLayer() { |
| 148 | for (int index = fSaveStack.count() - 1; index >= 0; --index) { |
| 149 | if (fSaveStack[index].fHasNestedSaveLayer) { |
| 150 | break; |
| 151 | } |
| 152 | fSaveStack[index].fHasNestedSaveLayer = true; |
| 153 | if (fSaveStack[index].fIsSaveLayer) { |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void pushSaveLayerBlock(const SkPaint* paint) { |
| 160 | this->updateStackForSaveLayer(); |
| 161 | |
| 162 | fSaveStack.push(SaveInfo(fCurrentOp, true, paint, fCurrentClipBounds)); |
| 163 | ++fSaveLayersInStack; |
| 164 | } |
| 165 | |
| 166 | void popSaveBlock() { |
| 167 | if (fSaveStack.count() <= 0) { |
| 168 | SkASSERT(false); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | SaveInfo si; |
| 173 | fSaveStack.pop(&si); |
| 174 | |
| 175 | if (!si.fIsSaveLayer) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | --fSaveLayersInStack; |
| 180 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 181 | GrAccelData::SaveLayerInfo& slInfo = fAccelData->addSaveLayerInfo(); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 182 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 183 | SkASSERT(NULL == slInfo.fPicture); // This layer is in the top-most picture |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 184 | slInfo.fSize = SkISize::Make(si.fBounds.width(), si.fBounds.height()); |
| 185 | slInfo.fOffset = SkIPoint::Make(si.fBounds.fLeft, si.fBounds.fTop); |
| 186 | slInfo.fOriginXform = *fCTM; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 187 | if (si.fPaint) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 188 | slInfo.fPaint = SkNEW_ARGS(SkPaint, (*si.fPaint)); |
| 189 | } |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 190 | slInfo.fSaveLayerOpID = si.fStartIndex; |
| 191 | slInfo.fRestoreOpID = fCurrentOp; |
| 192 | slInfo.fHasNestedLayers = si.fHasNestedSaveLayer; |
| 193 | slInfo.fIsNested = fSaveLayersInStack > 0; |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Returns true if rect was meaningfully adjusted for the effects of paint, |
| 197 | // false if the paint could affect the rect in unknown ways. |
| 198 | static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
| 199 | if (paint) { |
| 200 | if (paint->canComputeFastBounds()) { |
| 201 | *rect = paint->computeFastBounds(*rect, rect); |
| 202 | return true; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | // Adjust rect for all paints that may affect its geometry, then map it to device space. |
| 210 | SkIRect adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 211 | // Inverted rectangles really confuse our BBHs. |
| 212 | rect.sort(); |
| 213 | |
| 214 | // Adjust the rect for its own paint. |
| 215 | if (!AdjustForPaint(paint, &rect)) { |
| 216 | // The paint could do anything to our bounds. The only safe answer is the current clip. |
| 217 | return fCurrentClipBounds; |
| 218 | } |
| 219 | |
| 220 | // Adjust rect for all the paints from the SaveLayers we're inside. |
| 221 | for (int i = fSaveStack.count() - 1; i >= 0; i--) { |
| 222 | if (!AdjustForPaint(fSaveStack[i].fPaint, &rect)) { |
| 223 | // Same deal as above. |
| 224 | return fCurrentClipBounds; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Map the rect back to device space. |
| 229 | fCTM->mapRect(&rect); |
| 230 | SkIRect devRect; |
| 231 | rect.roundOut(&devRect); |
| 232 | |
| 233 | // Nothing can draw outside the current clip. |
| 234 | // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
| 235 | devRect.intersect(fCurrentClipBounds); |
| 236 | return devRect; |
| 237 | } |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 240 | |
| 241 | // GPUOptimize is only intended to be called within the context of SkGpuDevice's |
skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 242 | // EXPERIMENTAL_optimize method. |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 243 | const GrAccelData* GPUOptimize(const SkPicture* pict) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 244 | if (NULL == pict || pict->cullRect().isEmpty()) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 245 | return NULL; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 246 | } |
| 247 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 248 | SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 249 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 250 | const GrAccelData* existing = |
| 251 | static_cast<const GrAccelData*>(pict->EXPERIMENTAL_getAccelData(key)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 252 | if (existing) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 253 | return existing; |
| 254 | } |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 255 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 256 | SkAutoTUnref<GrAccelData> data(SkNEW_ARGS(GrAccelData, (key))); |
| 257 | |
| 258 | pict->EXPERIMENTAL_addAccelData(data); |
| 259 | |
| 260 | CollectLayers collector(pict, data); |
| 261 | |
| 262 | return data; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 263 | } |