robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [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 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 8 | #include "SkBigPicture.h" |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 9 | #include "SkCanvas.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 10 | #include "SkImageFilterCache.h" |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 11 | #include "SkLayerInfo.h" |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 12 | #include "SkRecordDraw.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 14 | #include "SkSurface.h" |
| 15 | |
robertphillips | 325474d | 2016-05-04 11:26:46 -0700 | [diff] [blame] | 16 | #include "GrLayerHoister.h" |
| 17 | |
| 18 | #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU |
| 19 | |
| 20 | #include "GrContext.h" |
| 21 | #include "GrLayerCache.h" |
| 22 | #include "GrRecordReplaceDraw.h" |
| 23 | |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 24 | // Create the layer information for the hoisted layer and secure the |
| 25 | // required texture/render target resources. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 26 | static void prepare_for_hoisting(GrLayerCache* layerCache, |
robertphillips | 232f6b0 | 2014-10-09 16:43:42 -0700 | [diff] [blame] | 27 | const SkPicture* topLevelPicture, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 28 | const SkMatrix& initialMat, |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 29 | const SkLayerInfo::BlockInfo& info, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 30 | const SkIRect& srcIR, |
| 31 | const SkIRect& dstIR, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 32 | SkTDArray<GrHoistedLayer>* needRendering, |
| 33 | SkTDArray<GrHoistedLayer>* recycled, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 34 | bool attemptToAtlas, |
| 35 | int numSamples) { |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 36 | const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; |
| 37 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 38 | GrCachedLayer* layer = layerCache->findLayerOrCreate(topLevelPicture->uniqueID(), |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 39 | SkToInt(info.fSaveLayerOpID), |
| 40 | SkToInt(info.fRestoreOpID), |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 41 | srcIR, |
| 42 | dstIR, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 43 | initialMat, |
| 44 | info.fKey, |
| 45 | info.fKeySize, |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 46 | info.fPaint); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 47 | GrSurfaceDesc desc; |
| 48 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 49 | desc.fWidth = srcIR.width(); |
| 50 | desc.fHeight = srcIR.height(); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 51 | desc.fConfig = kSkia8888_GrPixelConfig; |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 52 | desc.fSampleCnt = numSamples; |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 53 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 54 | bool locked, needsRendering; |
| 55 | if (attemptToAtlas) { |
| 56 | locked = layerCache->tryToAtlas(layer, desc, &needsRendering); |
| 57 | } else { |
| 58 | locked = layerCache->lock(layer, desc, &needsRendering); |
| 59 | } |
| 60 | if (!locked) { |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 61 | // GPU resources could not be secured for the hoisting of this layer |
| 62 | return; |
| 63 | } |
| 64 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 65 | if (attemptToAtlas) { |
| 66 | SkASSERT(layer->isAtlased()); |
| 67 | } |
| 68 | |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 69 | GrHoistedLayer* hl; |
| 70 | |
| 71 | if (needsRendering) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 72 | if (!attemptToAtlas) { |
| 73 | SkASSERT(!layer->isAtlased()); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 74 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 75 | hl = needRendering->append(); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 76 | } else { |
| 77 | hl = recycled->append(); |
| 78 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 79 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 80 | layerCache->addUse(layer); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 81 | hl->fLayer = layer; |
| 82 | hl->fPicture = pict; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 83 | hl->fLocalMat = info.fLocalMat; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 84 | hl->fInitialMat = initialMat; |
| 85 | hl->fPreMat = initialMat; |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 86 | hl->fPreMat.preConcat(info.fPreMat); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 87 | } |
| 88 | |
robertphillips | 57f192d | 2015-01-08 10:15:25 -0800 | [diff] [blame] | 89 | // Compute the source rect and return false if it is empty. |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 90 | static bool compute_source_rect(const SkLayerInfo::BlockInfo& info, const SkMatrix& initialMat, |
| 91 | const SkIRect& dstIR, SkIRect* srcIR) { |
| 92 | SkIRect clipBounds = dstIR; |
| 93 | |
| 94 | SkMatrix totMat = initialMat; |
| 95 | totMat.preConcat(info.fPreMat); |
| 96 | totMat.preConcat(info.fLocalMat); |
| 97 | |
| 98 | if (info.fPaint && info.fPaint->getImageFilter()) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 99 | clipBounds = info.fPaint->getImageFilter()->filterBounds(clipBounds, totMat); |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (!info.fSrcBounds.isEmpty()) { |
| 103 | SkRect r; |
| 104 | |
| 105 | totMat.mapRect(&r, info.fSrcBounds); |
| 106 | r.roundOut(srcIR); |
| 107 | |
| 108 | if (!srcIR->intersect(clipBounds)) { |
| 109 | return false; |
| 110 | } |
| 111 | } else { |
| 112 | *srcIR = clipBounds; |
| 113 | } |
| 114 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 118 | // Atlased layers must be small enough to fit in the atlas, not have a |
| 119 | // paint with an image filter and be neither nested nor nesting. |
| 120 | // TODO: allow leaf nested layers to appear in the atlas. |
| 121 | void GrLayerHoister::FindLayersToAtlas(GrContext* context, |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 122 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 123 | const SkMatrix& initialMat, |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 124 | const SkRect& query, |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 125 | SkTDArray<GrHoistedLayer>* atlased, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 126 | SkTDArray<GrHoistedLayer>* recycled, |
| 127 | int numSamples) { |
| 128 | if (0 != numSamples) { |
| 129 | // MSAA layers are currently never atlased |
| 130 | return; |
| 131 | } |
| 132 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 133 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 134 | layerCache->processDeletedPictures(); |
| 135 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 136 | const SkBigPicture::AccelData* topLevelData = nullptr; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 137 | if (const SkBigPicture* bp = topLevelPicture->asSkBigPicture()) { |
| 138 | topLevelData = bp->accelData(); |
| 139 | } |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 140 | if (!topLevelData) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 141 | return; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 142 | } |
| 143 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 144 | const SkLayerInfo *topLevelGPUData = static_cast<const SkLayerInfo*>(topLevelData); |
| 145 | if (0 == topLevelGPUData->numBlocks()) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 146 | return; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 147 | } |
| 148 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 149 | atlased->setReserve(atlased->count() + topLevelGPUData->numBlocks()); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 150 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 151 | for (int i = 0; i < topLevelGPUData->numBlocks(); ++i) { |
| 152 | const SkLayerInfo::BlockInfo& info = topLevelGPUData->block(i); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 153 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 154 | // TODO: ignore perspective projected layers here? |
| 155 | bool disallowAtlasing = info.fHasNestedLayers || info.fIsNested || |
| 156 | (info.fPaint && info.fPaint->getImageFilter()); |
| 157 | |
| 158 | if (disallowAtlasing) { |
| 159 | continue; |
| 160 | } |
| 161 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 162 | SkRect layerRect; |
| 163 | initialMat.mapRect(&layerRect, info.fBounds); |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 164 | if (!layerRect.intersect(query)) { |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 165 | continue; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 166 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 167 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 168 | const SkIRect dstIR = layerRect.roundOut(); |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 169 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 170 | SkIRect srcIR; |
| 171 | |
robertphillips | 57f192d | 2015-01-08 10:15:25 -0800 | [diff] [blame] | 172 | if (!compute_source_rect(info, initialMat, dstIR, &srcIR) || |
| 173 | !GrLayerCache::PlausiblyAtlasable(srcIR.width(), srcIR.height())) { |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 174 | continue; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 175 | } |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 176 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 177 | prepare_for_hoisting(layerCache, topLevelPicture, initialMat, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 178 | info, srcIR, dstIR, atlased, recycled, true, 0); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 179 | } |
| 180 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void GrLayerHoister::FindLayersToHoist(GrContext* context, |
| 184 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 185 | const SkMatrix& initialMat, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 186 | const SkRect& query, |
| 187 | SkTDArray<GrHoistedLayer>* needRendering, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 188 | SkTDArray<GrHoistedLayer>* recycled, |
| 189 | int numSamples) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 190 | GrLayerCache* layerCache = context->getLayerCache(); |
| 191 | |
| 192 | layerCache->processDeletedPictures(); |
| 193 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | const SkBigPicture::AccelData* topLevelData = nullptr; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 195 | if (const SkBigPicture* bp = topLevelPicture->asSkBigPicture()) { |
| 196 | topLevelData = bp->accelData(); |
| 197 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 198 | if (!topLevelData) { |
| 199 | return; |
| 200 | } |
| 201 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 202 | const SkLayerInfo *topLevelGPUData = static_cast<const SkLayerInfo*>(topLevelData); |
| 203 | if (0 == topLevelGPUData->numBlocks()) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Find and prepare for hoisting all the layers that intersect the query rect |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 208 | for (int i = 0; i < topLevelGPUData->numBlocks(); ++i) { |
| 209 | const SkLayerInfo::BlockInfo& info = topLevelGPUData->block(i); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 210 | if (info.fIsNested) { |
| 211 | // Parent layers are currently hoisted while nested layers are not. |
| 212 | continue; |
| 213 | } |
| 214 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 215 | SkRect layerRect; |
| 216 | initialMat.mapRect(&layerRect, info.fBounds); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 217 | if (!layerRect.intersect(query)) { |
| 218 | continue; |
| 219 | } |
| 220 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 221 | const SkIRect dstIR = layerRect.roundOut(); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 222 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 223 | SkIRect srcIR; |
| 224 | if (!compute_source_rect(info, initialMat, dstIR, &srcIR)) { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | prepare_for_hoisting(layerCache, topLevelPicture, initialMat, info, srcIR, dstIR, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 229 | needRendering, recycled, false, numSamples); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 230 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 231 | } |
| 232 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 233 | void GrLayerHoister::DrawLayersToAtlas(GrContext* context, |
| 234 | const SkTDArray<GrHoistedLayer>& atlased) { |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 235 | if (atlased.count() > 0) { |
| 236 | // All the atlased layers are rendered into the same GrTexture |
robertphillips | a86a233 | 2014-12-16 14:25:08 -0800 | [diff] [blame] | 237 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
robertphillips | b451134 | 2016-05-03 09:37:08 -0700 | [diff] [blame] | 238 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTargetDirect( |
robertphillips | a86a233 | 2014-12-16 14:25:08 -0800 | [diff] [blame] | 239 | atlased[0].fLayer->texture()->asRenderTarget(), &props)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 240 | |
| 241 | SkCanvas* atlasCanvas = surface->getCanvas(); |
| 242 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 243 | for (int i = 0; i < atlased.count(); ++i) { |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 244 | const GrCachedLayer* layer = atlased[i].fLayer; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 245 | const SkBigPicture* pict = atlased[i].fPicture->asSkBigPicture(); |
| 246 | if (!pict) { |
| 247 | // TODO: can we assume / assert this? |
| 248 | continue; |
| 249 | } |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 250 | const SkIPoint offset = SkIPoint::Make(layer->srcIR().fLeft, layer->srcIR().fTop); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 251 | SkDEBUGCODE(const SkPaint* layerPaint = layer->paint();) |
| 252 | |
| 253 | SkASSERT(!layerPaint || !layerPaint->getImageFilter()); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 254 | SkASSERT(!layer->filter()); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 255 | |
| 256 | atlasCanvas->save(); |
| 257 | |
| 258 | // Add a rect clip to make sure the rendering doesn't |
| 259 | // extend beyond the boundaries of the atlased sub-rect |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 260 | const SkRect bound = SkRect::Make(layer->rect()); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 261 | atlasCanvas->clipRect(bound); |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 262 | atlasCanvas->clear(0); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 263 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 264 | // '-offset' maps the layer's top/left to the origin. |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 265 | // Since this layer is atlased, the top/left corner needs |
| 266 | // to be offset to the correct location in the backing texture. |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 267 | SkMatrix initialCTM; |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 268 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
| 269 | initialCTM.preTranslate(bound.fLeft, bound.fTop); |
| 270 | initialCTM.preConcat(atlased[i].fPreMat); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 271 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 272 | atlasCanvas->setMatrix(initialCTM); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 273 | atlasCanvas->concat(atlased[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 274 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 275 | pict->partialPlayback(atlasCanvas, layer->start() + 1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 276 | atlasCanvas->restore(); |
| 277 | } |
| 278 | |
| 279 | atlasCanvas->flush(); |
| 280 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 281 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 282 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 283 | void GrLayerHoister::FilterLayer(GrContext* context, |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 284 | const SkSurfaceProps* props, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 285 | const GrHoistedLayer& info) { |
| 286 | GrCachedLayer* layer = info.fLayer; |
| 287 | |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 288 | SkASSERT(layer->filter()); |
| 289 | |
| 290 | static const int kDefaultCacheSize = 32 * 1024 * 1024; |
| 291 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 292 | const SkIPoint filterOffset = SkIPoint::Make(layer->srcIR().fLeft, layer->srcIR().fTop); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 293 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 294 | SkMatrix totMat(info.fPreMat); |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 295 | totMat.preConcat(info.fLocalMat); |
| 296 | totMat.postTranslate(-SkIntToScalar(filterOffset.fX), -SkIntToScalar(filterOffset.fY)); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 297 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 298 | SkASSERT(0 == layer->rect().fLeft && 0 == layer->rect().fTop); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 299 | const SkIRect& clipBounds = layer->rect(); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 300 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 301 | // This cache is transient, and is freed (along with all its contained |
| 302 | // textures) when it goes out of scope. |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 303 | SkAutoTUnref<SkImageFilterCache> cache(SkImageFilterCache::Create(kDefaultCacheSize)); |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 304 | SkImageFilter::Context filterContext(totMat, clipBounds, cache); |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 305 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 306 | // TODO: should the layer hoister store stand alone layers as SkSpecialImages internally? |
robertphillips | b451134 | 2016-05-03 09:37:08 -0700 | [diff] [blame] | 307 | SkASSERT(layer->rect().width() == layer->texture()->width() && |
| 308 | layer->rect().height() == layer->texture()->height()); |
| 309 | const SkIRect subset = SkIRect::MakeWH(layer->rect().width(), layer->rect().height()); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 310 | sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromGpu(subset, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 311 | kNeedNewImageUniqueID_SpecialImage, |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 312 | sk_ref_sp(layer->texture()), |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 313 | props)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 314 | |
| 315 | SkIPoint offset = SkIPoint::Make(0, 0); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 316 | sk_sp<SkSpecialImage> result(layer->filter()->filterImage(img.get(), |
| 317 | filterContext, |
| 318 | &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 319 | if (!result) { |
robertphillips | 95145a9 | 2015-01-14 08:08:21 -0800 | [diff] [blame] | 320 | // Filtering failed. Press on with the unfiltered version. |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 321 | return; |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 322 | } |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 323 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 324 | SkASSERT(result->isTextureBacked()); |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 325 | sk_sp<GrTexture> texture(result->asTextureRef(context)); |
| 326 | layer->setTexture(texture.get(), result->subset(), false); |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 327 | layer->setOffset(offset); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 328 | } |
| 329 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 330 | void GrLayerHoister::DrawLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers) { |
| 331 | for (int i = 0; i < layers.count(); ++i) { |
| 332 | GrCachedLayer* layer = layers[i].fLayer; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 333 | const SkBigPicture* pict = layers[i].fPicture->asSkBigPicture(); |
| 334 | if (!pict) { |
| 335 | // TODO: can we assume / assert this? |
| 336 | continue; |
| 337 | } |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 338 | const SkIPoint offset = SkIPoint::Make(layer->srcIR().fLeft, layer->srcIR().fTop); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 339 | |
| 340 | // Each non-atlased layer has its own GrTexture |
robertphillips | a86a233 | 2014-12-16 14:25:08 -0800 | [diff] [blame] | 341 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 342 | auto surface(SkSurface::MakeRenderTargetDirect( |
robertphillips | a86a233 | 2014-12-16 14:25:08 -0800 | [diff] [blame] | 343 | layer->texture()->asRenderTarget(), &props)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 344 | |
| 345 | SkCanvas* layerCanvas = surface->getCanvas(); |
| 346 | |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 347 | SkASSERT(0 == layer->rect().fLeft && 0 == layer->rect().fTop); |
| 348 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 349 | // Add a rect clip to make sure the rendering doesn't |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 350 | // extend beyond the boundaries of the layer |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 351 | const SkRect bound = SkRect::Make(layer->rect()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 352 | layerCanvas->clipRect(bound); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 353 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 354 | |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 355 | SkMatrix initialCTM; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 356 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 357 | initialCTM.preConcat(layers[i].fPreMat); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 358 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 359 | layerCanvas->setMatrix(initialCTM); |
| 360 | layerCanvas->concat(layers[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 361 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 362 | pict->partialPlayback(layerCanvas, layer->start()+1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 363 | layerCanvas->flush(); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 364 | |
| 365 | if (layer->filter()) { |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 366 | FilterLayer(context, &surface->props(), layers[i]); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 367 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 371 | void GrLayerHoister::UnlockLayers(GrContext* context, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 372 | const SkTDArray<GrHoistedLayer>& layers) { |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 373 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 374 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 375 | for (int i = 0; i < layers.count(); ++i) { |
| 376 | layerCache->removeUse(layers[i].fLayer); |
robertphillips | b5a9715 | 2014-09-30 11:33:02 -0700 | [diff] [blame] | 377 | } |
| 378 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 379 | SkDEBUGCODE(layerCache->validate();) |
| 380 | } |
| 381 | |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 382 | void GrLayerHoister::Begin(GrContext* context) { |
robertphillips | cf1d198 | 2015-11-06 05:59:14 -0800 | [diff] [blame] | 383 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | 42597bc | 2015-11-06 04:14:55 -0800 | [diff] [blame] | 384 | |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 385 | layerCache->begin(); |
| 386 | } |
| 387 | |
| 388 | void GrLayerHoister::End(GrContext* context) { |
| 389 | GrLayerCache* layerCache = context->getLayerCache(); |
| 390 | |
| 391 | #if !GR_CACHE_HOISTED_LAYERS |
| 392 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 393 | // This code completely clears out the atlas. It is required when |
| 394 | // caching is disabled so the atlas doesn't fill up and force more |
| 395 | // free floating layers |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 396 | layerCache->purgeAll(); |
| 397 | #endif |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 398 | |
| 399 | layerCache->end(); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 400 | } |
robertphillips | 325474d | 2016-05-04 11:26:46 -0700 | [diff] [blame] | 401 | |
| 402 | #endif |