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 | |
| 12 | #include "SkCanvas.h" |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame^] | 13 | #include "SkGpuDevice.h" |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 14 | #include "SkGrPixelRef.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. |
| 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 | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 26 | const SkIRect& layerRect, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 27 | SkTDArray<GrHoistedLayer>* needRendering, |
| 28 | SkTDArray<GrHoistedLayer>* recycled, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 29 | bool attemptToAtlas, |
| 30 | int numSamples) { |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 31 | const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; |
| 32 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 33 | GrCachedLayer* layer = layerCache->findLayerOrCreate(topLevelPicture->uniqueID(), |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 34 | info.fSaveLayerOpID, |
| 35 | info.fRestoreOpID, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 36 | layerRect, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 37 | initialMat, |
| 38 | info.fKey, |
| 39 | info.fKeySize, |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 40 | info.fPaint); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 41 | GrSurfaceDesc desc; |
| 42 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 43 | desc.fWidth = layerRect.width(); |
| 44 | desc.fHeight = layerRect.height(); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 45 | desc.fConfig = kSkia8888_GrPixelConfig; |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 46 | desc.fSampleCnt = numSamples; |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 47 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 48 | bool locked, needsRendering; |
| 49 | if (attemptToAtlas) { |
| 50 | locked = layerCache->tryToAtlas(layer, desc, &needsRendering); |
| 51 | } else { |
| 52 | locked = layerCache->lock(layer, desc, &needsRendering); |
| 53 | } |
| 54 | if (!locked) { |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 55 | // GPU resources could not be secured for the hoisting of this layer |
| 56 | return; |
| 57 | } |
| 58 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 59 | if (attemptToAtlas) { |
| 60 | SkASSERT(layer->isAtlased()); |
| 61 | } |
| 62 | |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 63 | GrHoistedLayer* hl; |
| 64 | |
| 65 | if (needsRendering) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 66 | if (!attemptToAtlas) { |
| 67 | SkASSERT(!layer->isAtlased()); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 68 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 69 | hl = needRendering->append(); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 70 | } else { |
| 71 | hl = recycled->append(); |
| 72 | } |
| 73 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 74 | layerCache->addUse(layer); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 75 | hl->fLayer = layer; |
| 76 | hl->fPicture = pict; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 77 | hl->fLocalMat = info.fLocalMat; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 78 | hl->fInitialMat = initialMat; |
| 79 | hl->fPreMat = initialMat; |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 80 | hl->fPreMat.preConcat(info.fPreMat); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 81 | } |
| 82 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 83 | // Atlased layers must be small enough to fit in the atlas, not have a |
| 84 | // paint with an image filter and be neither nested nor nesting. |
| 85 | // TODO: allow leaf nested layers to appear in the atlas. |
| 86 | void GrLayerHoister::FindLayersToAtlas(GrContext* context, |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 87 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 88 | const SkMatrix& initialMat, |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 89 | const SkRect& query, |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 90 | SkTDArray<GrHoistedLayer>* atlased, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 91 | SkTDArray<GrHoistedLayer>* recycled, |
| 92 | int numSamples) { |
| 93 | if (0 != numSamples) { |
| 94 | // MSAA layers are currently never atlased |
| 95 | return; |
| 96 | } |
| 97 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 98 | GrLayerCache* layerCache = context->getLayerCache(); |
| 99 | |
| 100 | layerCache->processDeletedPictures(); |
| 101 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 102 | SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey(); |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 103 | |
| 104 | const SkPicture::AccelData* topLevelData = topLevelPicture->EXPERIMENTAL_getAccelData(key); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 105 | if (!topLevelData) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 106 | return; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 109 | const SkLayerInfo *topLevelGPUData = static_cast<const SkLayerInfo*>(topLevelData); |
| 110 | if (0 == topLevelGPUData->numBlocks()) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 111 | return; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 114 | atlased->setReserve(atlased->count() + topLevelGPUData->numBlocks()); |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 115 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 116 | for (int i = 0; i < topLevelGPUData->numBlocks(); ++i) { |
| 117 | const SkLayerInfo::BlockInfo& info = topLevelGPUData->block(i); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 118 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 119 | // TODO: ignore perspective projected layers here? |
| 120 | bool disallowAtlasing = info.fHasNestedLayers || info.fIsNested || |
| 121 | (info.fPaint && info.fPaint->getImageFilter()); |
| 122 | |
| 123 | if (disallowAtlasing) { |
| 124 | continue; |
| 125 | } |
| 126 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 127 | SkRect layerRect; |
| 128 | initialMat.mapRect(&layerRect, info.fBounds); |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 129 | if (!layerRect.intersect(query)) { |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 130 | continue; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 131 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 132 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 133 | const SkIRect ir = layerRect.roundOut(); |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 134 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 135 | if (!GrLayerCache::PlausiblyAtlasable(ir.width(), ir.height())) { |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 136 | continue; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 137 | } |
robertphillips | 2ed8a75 | 2014-09-03 13:46:02 -0700 | [diff] [blame] | 138 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 139 | prepare_for_hoisting(layerCache, topLevelPicture, initialMat, |
| 140 | info, ir, atlased, recycled, true, 0); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void GrLayerHoister::FindLayersToHoist(GrContext* context, |
| 146 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 147 | const SkMatrix& initialMat, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 148 | const SkRect& query, |
| 149 | SkTDArray<GrHoistedLayer>* needRendering, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 150 | SkTDArray<GrHoistedLayer>* recycled, |
| 151 | int numSamples) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 152 | GrLayerCache* layerCache = context->getLayerCache(); |
| 153 | |
| 154 | layerCache->processDeletedPictures(); |
| 155 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 156 | SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey(); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 157 | |
| 158 | const SkPicture::AccelData* topLevelData = topLevelPicture->EXPERIMENTAL_getAccelData(key); |
| 159 | if (!topLevelData) { |
| 160 | return; |
| 161 | } |
| 162 | |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 163 | const SkLayerInfo *topLevelGPUData = static_cast<const SkLayerInfo*>(topLevelData); |
| 164 | if (0 == topLevelGPUData->numBlocks()) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Find and prepare for hoisting all the layers that intersect the query rect |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 169 | for (int i = 0; i < topLevelGPUData->numBlocks(); ++i) { |
| 170 | const SkLayerInfo::BlockInfo& info = topLevelGPUData->block(i); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 171 | if (info.fIsNested) { |
| 172 | // Parent layers are currently hoisted while nested layers are not. |
| 173 | continue; |
| 174 | } |
| 175 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 176 | SkRect layerRect; |
| 177 | initialMat.mapRect(&layerRect, info.fBounds); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 178 | if (!layerRect.intersect(query)) { |
| 179 | continue; |
| 180 | } |
| 181 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 182 | const SkIRect ir = layerRect.roundOut(); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 183 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame] | 184 | prepare_for_hoisting(layerCache, topLevelPicture, initialMat, info, ir, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 185 | needRendering, recycled, false, numSamples); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 186 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 187 | } |
| 188 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 189 | void GrLayerHoister::DrawLayersToAtlas(GrContext* context, |
| 190 | const SkTDArray<GrHoistedLayer>& atlased) { |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 191 | if (atlased.count() > 0) { |
| 192 | // All the atlased layers are rendered into the same GrTexture |
| 193 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 194 | atlased[0].fLayer->texture()->asRenderTarget(), NULL)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 195 | |
| 196 | SkCanvas* atlasCanvas = surface->getCanvas(); |
| 197 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 198 | for (int i = 0; i < atlased.count(); ++i) { |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 199 | const GrCachedLayer* layer = atlased[i].fLayer; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 200 | const SkPicture* pict = atlased[i].fPicture; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 201 | const SkIPoint offset = SkIPoint::Make(layer->bound().fLeft, layer->bound().fTop); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 202 | SkDEBUGCODE(const SkPaint* layerPaint = layer->paint();) |
| 203 | |
| 204 | SkASSERT(!layerPaint || !layerPaint->getImageFilter()); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame^] | 205 | SkASSERT(!layer->filter()); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 206 | |
| 207 | atlasCanvas->save(); |
| 208 | |
| 209 | // Add a rect clip to make sure the rendering doesn't |
| 210 | // extend beyond the boundaries of the atlased sub-rect |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 211 | const SkRect bound = SkRect::Make(layer->rect()); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 212 | atlasCanvas->clipRect(bound); |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 213 | atlasCanvas->clear(0); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 214 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 215 | // '-offset' maps the layer's top/left to the origin. |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 216 | // Since this layer is atlased, the top/left corner needs |
| 217 | // to be offset to the correct location in the backing texture. |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 218 | SkMatrix initialCTM; |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 219 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
| 220 | initialCTM.preTranslate(bound.fLeft, bound.fTop); |
| 221 | initialCTM.preConcat(atlased[i].fPreMat); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 222 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 223 | atlasCanvas->setMatrix(initialCTM); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 224 | atlasCanvas->concat(atlased[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 225 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 226 | SkRecordPartialDraw(*pict->fRecord.get(), atlasCanvas, |
| 227 | pict->drawablePicts(), pict->drawableCount(), |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 228 | layer->start() + 1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 229 | |
| 230 | atlasCanvas->restore(); |
| 231 | } |
| 232 | |
| 233 | atlasCanvas->flush(); |
| 234 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 235 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 236 | |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame^] | 237 | void GrLayerHoister::FilterLayer(GrContext* context, SkGpuDevice* device, GrCachedLayer* layer) { |
| 238 | SkASSERT(layer->filter()); |
| 239 | |
| 240 | static const int kDefaultCacheSize = 32 * 1024 * 1024; |
| 241 | |
| 242 | if (layer->filter()->canFilterImageGPU()) { |
| 243 | SkBitmap filteredBitmap; |
| 244 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 245 | |
| 246 | SkASSERT(0 == layer->rect().fLeft && 0 == layer->rect().fTop); |
| 247 | SkIRect clipBounds = layer->rect(); |
| 248 | |
| 249 | // This cache is transient, and is freed (along with all its contained |
| 250 | // textures) when it goes out of scope. |
| 251 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kDefaultCacheSize)); |
| 252 | SkImageFilter::Context filterContext(SkMatrix::I(), clipBounds, cache); |
| 253 | |
| 254 | if (!device->filterTexture(context, layer->texture(), layer->filter(), |
| 255 | filterContext, &filteredBitmap, &offset)) { |
| 256 | // Filtering failed. Press on with the unfiltered version |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // TODO: need to fix up offset |
| 261 | SkASSERT(0 == offset.fX && 0 == offset.fY); |
| 262 | |
| 263 | SkIRect newRect = SkIRect::MakeWH(filteredBitmap.width(), filteredBitmap.height()); |
| 264 | layer->setTexture(filteredBitmap.getTexture(), newRect); |
| 265 | } |
| 266 | } |
| 267 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 268 | void GrLayerHoister::DrawLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers) { |
| 269 | for (int i = 0; i < layers.count(); ++i) { |
| 270 | GrCachedLayer* layer = layers[i].fLayer; |
| 271 | const SkPicture* pict = layers[i].fPicture; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 272 | const SkIPoint offset = SkIPoint::Make(layer->bound().fLeft, layer->bound().fTop); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 273 | |
| 274 | // Each non-atlased layer has its own GrTexture |
| 275 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 276 | layer->texture()->asRenderTarget(), NULL)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 277 | |
| 278 | SkCanvas* layerCanvas = surface->getCanvas(); |
| 279 | |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 280 | SkASSERT(0 == layer->rect().fLeft && 0 == layer->rect().fTop); |
| 281 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 282 | // Add a rect clip to make sure the rendering doesn't |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 283 | // extend beyond the boundaries of the layer |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 284 | const SkRect bound = SkRect::Make(layer->rect()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 285 | layerCanvas->clipRect(bound); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 286 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 287 | |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 288 | SkMatrix initialCTM; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 289 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 290 | initialCTM.preConcat(layers[i].fPreMat); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 291 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 292 | layerCanvas->setMatrix(initialCTM); |
| 293 | layerCanvas->concat(layers[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 294 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 295 | SkRecordPartialDraw(*pict->fRecord.get(), layerCanvas, |
| 296 | pict->drawablePicts(), pict->drawableCount(), |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 297 | layer->start()+1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 298 | |
| 299 | layerCanvas->flush(); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame^] | 300 | |
| 301 | if (layer->filter()) { |
| 302 | SkSurface_Gpu* gpuSurf = static_cast<SkSurface_Gpu*>(surface.get()); |
| 303 | |
| 304 | FilterLayer(context, gpuSurf->getDevice(), layer); |
| 305 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 309 | void GrLayerHoister::UnlockLayers(GrContext* context, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 310 | const SkTDArray<GrHoistedLayer>& layers) { |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 311 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 312 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 313 | for (int i = 0; i < layers.count(); ++i) { |
| 314 | layerCache->removeUse(layers[i].fLayer); |
robertphillips | b5a9715 | 2014-09-30 11:33:02 -0700 | [diff] [blame] | 315 | } |
| 316 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 317 | SkDEBUGCODE(layerCache->validate();) |
| 318 | } |
| 319 | |
| 320 | void GrLayerHoister::PurgeCache(GrContext* context) { |
| 321 | #if !GR_CACHE_HOISTED_LAYERS |
| 322 | GrLayerCache* layerCache = context->getLayerCache(); |
| 323 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 324 | // This code completely clears out the atlas. It is required when |
| 325 | // caching is disabled so the atlas doesn't fill up and force more |
| 326 | // free floating layers |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 327 | layerCache->purgeAll(); |
| 328 | #endif |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 329 | } |