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