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 | |
| 113 | GrAccelData::SaveLayerInfo dst; |
| 114 | |
| 115 | // TODO: need to store an SkRect in GrAccelData::SaveLayerInfo? |
| 116 | SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(src.fOffset.fX), |
| 117 | SkIntToScalar(src.fOffset.fY), |
| 118 | SkIntToScalar(src.fSize.width()), |
| 119 | SkIntToScalar(src.fSize.height())); |
| 120 | SkIRect newClip(fCurrentClipBounds); |
| 121 | newClip.intersect(this->adjustAndMap(srcRect, dp.paint)); |
| 122 | |
| 123 | dst.fValid = true; |
| 124 | dst.fPictureID = dp.picture->uniqueID(); |
| 125 | dst.fSize = SkISize::Make(newClip.width(), newClip.height()); |
| 126 | dst.fOffset = SkIPoint::Make(newClip.fLeft, newClip.fTop); |
| 127 | dst.fOriginXform = *fCTM; |
| 128 | dst.fOriginXform.postConcat(src.fOriginXform); |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 129 | |
| 130 | if (NULL == src.fPaint) { |
| 131 | dst.fPaint = NULL; |
| 132 | } else { |
| 133 | dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint)); |
| 134 | } |
| 135 | |
| 136 | dst.fSaveLayerOpID = src.fSaveLayerOpID; |
| 137 | dst.fRestoreOpID = src.fRestoreOpID; |
| 138 | dst.fHasNestedLayers = src.fHasNestedLayers; |
| 139 | dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested; |
| 140 | |
| 141 | fAccelData->addSaveLayerInfo(dst); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void pushSaveBlock() { |
| 146 | fSaveStack.push(SaveInfo(fCurrentOp, false, NULL, SkIRect::MakeEmpty())); |
| 147 | } |
| 148 | |
| 149 | // Inform all the saveLayers already on the stack that they now have a |
| 150 | // nested saveLayer inside them |
| 151 | void updateStackForSaveLayer() { |
| 152 | for (int index = fSaveStack.count() - 1; index >= 0; --index) { |
| 153 | if (fSaveStack[index].fHasNestedSaveLayer) { |
| 154 | break; |
| 155 | } |
| 156 | fSaveStack[index].fHasNestedSaveLayer = true; |
| 157 | if (fSaveStack[index].fIsSaveLayer) { |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void pushSaveLayerBlock(const SkPaint* paint) { |
| 164 | this->updateStackForSaveLayer(); |
| 165 | |
| 166 | fSaveStack.push(SaveInfo(fCurrentOp, true, paint, fCurrentClipBounds)); |
| 167 | ++fSaveLayersInStack; |
| 168 | } |
| 169 | |
| 170 | void popSaveBlock() { |
| 171 | if (fSaveStack.count() <= 0) { |
| 172 | SkASSERT(false); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | SaveInfo si; |
| 177 | fSaveStack.pop(&si); |
| 178 | |
| 179 | if (!si.fIsSaveLayer) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | --fSaveLayersInStack; |
| 184 | |
| 185 | GrAccelData::SaveLayerInfo slInfo; |
| 186 | |
| 187 | slInfo.fValid = true; |
| 188 | slInfo.fPictureID = fPictureID; |
| 189 | slInfo.fSize = SkISize::Make(si.fBounds.width(), si.fBounds.height()); |
| 190 | slInfo.fOffset = SkIPoint::Make(si.fBounds.fLeft, si.fBounds.fTop); |
| 191 | slInfo.fOriginXform = *fCTM; |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 192 | |
| 193 | if (NULL == si.fPaint) { |
| 194 | slInfo.fPaint = NULL; |
| 195 | } else { |
| 196 | slInfo.fPaint = SkNEW_ARGS(SkPaint, (*si.fPaint)); |
| 197 | } |
| 198 | |
| 199 | slInfo.fSaveLayerOpID = si.fStartIndex; |
| 200 | slInfo.fRestoreOpID = fCurrentOp; |
| 201 | slInfo.fHasNestedLayers = si.fHasNestedSaveLayer; |
| 202 | slInfo.fIsNested = fSaveLayersInStack > 0; |
| 203 | |
| 204 | fAccelData->addSaveLayerInfo(slInfo); |
| 205 | } |
| 206 | |
| 207 | // Returns true if rect was meaningfully adjusted for the effects of paint, |
| 208 | // false if the paint could affect the rect in unknown ways. |
| 209 | static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
| 210 | if (paint) { |
| 211 | if (paint->canComputeFastBounds()) { |
| 212 | *rect = paint->computeFastBounds(*rect, rect); |
| 213 | return true; |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | // Adjust rect for all paints that may affect its geometry, then map it to device space. |
| 221 | SkIRect adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 222 | // Inverted rectangles really confuse our BBHs. |
| 223 | rect.sort(); |
| 224 | |
| 225 | // Adjust the rect for its own paint. |
| 226 | if (!AdjustForPaint(paint, &rect)) { |
| 227 | // The paint could do anything to our bounds. The only safe answer is the current clip. |
| 228 | return fCurrentClipBounds; |
| 229 | } |
| 230 | |
| 231 | // Adjust rect for all the paints from the SaveLayers we're inside. |
| 232 | for (int i = fSaveStack.count() - 1; i >= 0; i--) { |
| 233 | if (!AdjustForPaint(fSaveStack[i].fPaint, &rect)) { |
| 234 | // Same deal as above. |
| 235 | return fCurrentClipBounds; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Map the rect back to device space. |
| 240 | fCTM->mapRect(&rect); |
| 241 | SkIRect devRect; |
| 242 | rect.roundOut(&devRect); |
| 243 | |
| 244 | // Nothing can draw outside the current clip. |
| 245 | // (Only bounded ops call into this method, so oddballs like Clear don't matter here.) |
| 246 | devRect.intersect(fCurrentClipBounds); |
| 247 | return devRect; |
| 248 | } |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 251 | |
| 252 | // 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] | 253 | // EXPERIMENTAL_optimize method. |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 254 | const GrAccelData* GPUOptimize(const SkPicture* pict) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 255 | if (NULL == pict || pict->cullRect().isEmpty()) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 256 | return NULL; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 257 | } |
| 258 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 259 | SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 260 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 261 | const GrAccelData* existing = |
| 262 | static_cast<const GrAccelData*>(pict->EXPERIMENTAL_getAccelData(key)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 263 | if (existing) { |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 264 | return existing; |
| 265 | } |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 266 | |
robertphillips | d628330 | 2014-08-27 11:53:28 -0700 | [diff] [blame] | 267 | SkAutoTUnref<GrAccelData> data(SkNEW_ARGS(GrAccelData, (key))); |
| 268 | |
| 269 | pict->EXPERIMENTAL_addAccelData(data); |
| 270 | |
| 271 | CollectLayers collector(pict, data); |
| 272 | |
| 273 | return data; |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 274 | } |