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 | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 13 | #include "SkGrPixelRef.h" |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 14 | #include "SkLayerInfo.h" |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 15 | #include "SkRecordDraw.h" |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
| 17 | |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 18 | // Create the layer information for the hoisted layer and secure the |
| 19 | // required texture/render target resources. |
| 20 | static void prepare_for_hoisting(GrLayerCache* layerCache, |
robertphillips | 232f6b0 | 2014-10-09 16:43:42 -0700 | [diff] [blame] | 21 | const SkPicture* topLevelPicture, |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame^] | 22 | const SkMatrix& matrix, |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 23 | const SkLayerInfo::BlockInfo& info, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 24 | const SkIRect& layerRect, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 25 | SkTDArray<GrHoistedLayer>* needRendering, |
| 26 | SkTDArray<GrHoistedLayer>* recycled, |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 27 | bool attemptToAtlas, |
| 28 | int numSamples) { |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 29 | const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPicture; |
| 30 | |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame^] | 31 | SkMatrix combined = matrix; |
| 32 | combined.preConcat(info.fPreMat); |
| 33 | combined.preConcat(info.fLocalMat); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 34 | |
robertphillips | cbe80ca | 2014-10-09 15:36:06 -0700 | [diff] [blame] | 35 | GrCachedLayer* layer = layerCache->findLayerOrCreate(pict->uniqueID(), |
| 36 | info.fSaveLayerOpID, |
| 37 | info.fRestoreOpID, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 38 | layerRect, |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 39 | combined, |
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 | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 77 | hl->fOffset = SkIPoint::Make(layerRect.fLeft, layerRect.fTop); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 78 | hl->fLocalMat = info.fLocalMat; |
robertphillips | 30d7841 | 2014-11-24 09:49:17 -0800 | [diff] [blame^] | 79 | hl->fPreMat = matrix; |
| 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 | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 189 | static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) { |
| 190 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 191 | result->setInfo(info); |
| 192 | result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| 193 | } |
| 194 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 195 | void GrLayerHoister::ConvertLayersToReplacements(const SkTDArray<GrHoistedLayer>& layers, |
| 196 | GrReplacements* replacements) { |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 197 | // TODO: just replace GrReplacements::ReplacementInfo with GrCachedLayer? |
| 198 | for (int i = 0; i < layers.count(); ++i) { |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 199 | GrCachedLayer* layer = layers[i].fLayer; |
robertphillips | 68cd2aa | 2014-10-01 09:24:06 -0700 | [diff] [blame] | 200 | const SkPicture* picture = layers[i].fPicture; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 201 | |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 202 | SkMatrix combined = SkMatrix::Concat(layers[i].fPreMat, layers[i].fLocalMat); |
| 203 | |
robertphillips | 68cd2aa | 2014-10-01 09:24:06 -0700 | [diff] [blame] | 204 | GrReplacements::ReplacementInfo* layerInfo = |
| 205 | replacements->newReplacement(picture->uniqueID(), |
| 206 | layer->start(), |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 207 | combined); |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 208 | layerInfo->fStop = layer->stop(); |
| 209 | layerInfo->fPos = layers[i].fOffset; |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 210 | |
| 211 | SkBitmap bm; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 212 | wrap_texture(layers[i].fLayer->texture(), |
| 213 | !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().width() |
| 214 | : layers[i].fLayer->texture()->width(), |
| 215 | !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().height() |
| 216 | : layers[i].fLayer->texture()->height(), |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 217 | &bm); |
| 218 | layerInfo->fImage = SkImage::NewTexture(bm); |
| 219 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 220 | layerInfo->fPaint = layers[i].fLayer->paint() |
| 221 | ? SkNEW_ARGS(SkPaint, (*layers[i].fLayer->paint())) |
| 222 | : NULL; |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 223 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 224 | layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i].fLayer->rect().fLeft, |
| 225 | layers[i].fLayer->rect().fTop, |
| 226 | layers[i].fLayer->rect().width(), |
| 227 | layers[i].fLayer->rect().height()); |
robertphillips | 1c4c528 | 2014-09-18 12:03:15 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 231 | void GrLayerHoister::DrawLayersToAtlas(GrContext* context, |
| 232 | const SkTDArray<GrHoistedLayer>& atlased) { |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 233 | if (atlased.count() > 0) { |
| 234 | // All the atlased layers are rendered into the same GrTexture |
| 235 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 236 | atlased[0].fLayer->texture()->asRenderTarget(), NULL)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 237 | |
| 238 | SkCanvas* atlasCanvas = surface->getCanvas(); |
| 239 | |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 240 | SkPaint clearPaint; |
| 241 | clearPaint.setColor(SK_ColorTRANSPARENT); |
| 242 | clearPaint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 243 | |
| 244 | for (int i = 0; i < atlased.count(); ++i) { |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 245 | const GrCachedLayer* layer = atlased[i].fLayer; |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 246 | const SkPicture* pict = atlased[i].fPicture; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 247 | const SkIPoint offset = atlased[i].fOffset; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 248 | SkDEBUGCODE(const SkPaint* layerPaint = layer->paint();) |
| 249 | |
| 250 | SkASSERT(!layerPaint || !layerPaint->getImageFilter()); |
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 |
| 256 | SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
| 257 | SkIntToScalar(layer->rect().fTop), |
| 258 | SkIntToScalar(layer->rect().width()), |
| 259 | SkIntToScalar(layer->rect().height())); |
| 260 | atlasCanvas->clipRect(bound); |
| 261 | |
| 262 | // Since 'clear' doesn't respect the clip we need to draw a rect |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 263 | atlasCanvas->drawRect(bound, clearPaint); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 264 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 265 | // '-offset' maps the layer's top/left to the origin. |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 266 | // Since this layer is atlased, the top/left corner needs |
| 267 | // to be offset to the correct location in the backing texture. |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 268 | SkMatrix initialCTM; |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 269 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
| 270 | initialCTM.preTranslate(bound.fLeft, bound.fTop); |
| 271 | initialCTM.preConcat(atlased[i].fPreMat); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 272 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 273 | atlasCanvas->setMatrix(initialCTM); |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 274 | atlasCanvas->concat(atlased[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 275 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 276 | SkRecordPartialDraw(*pict->fRecord.get(), atlasCanvas, |
| 277 | pict->drawablePicts(), pict->drawableCount(), |
| 278 | bound, layer->start() + 1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 279 | |
| 280 | atlasCanvas->restore(); |
| 281 | } |
| 282 | |
| 283 | atlasCanvas->flush(); |
| 284 | } |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 285 | } |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 286 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 287 | void GrLayerHoister::DrawLayers(GrContext* context, const SkTDArray<GrHoistedLayer>& layers) { |
| 288 | for (int i = 0; i < layers.count(); ++i) { |
| 289 | GrCachedLayer* layer = layers[i].fLayer; |
| 290 | const SkPicture* pict = layers[i].fPicture; |
| 291 | const SkIPoint& offset = layers[i].fOffset; |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 292 | |
| 293 | // Each non-atlased layer has its own GrTexture |
| 294 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 295 | layer->texture()->asRenderTarget(), NULL)); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 296 | |
| 297 | SkCanvas* layerCanvas = surface->getCanvas(); |
| 298 | |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 299 | SkASSERT(0 == layer->rect().fLeft && 0 == layer->rect().fTop); |
| 300 | |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 301 | // Add a rect clip to make sure the rendering doesn't |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 302 | // extend beyond the boundaries of the layer |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 303 | SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
| 304 | SkIntToScalar(layer->rect().fTop), |
| 305 | SkIntToScalar(layer->rect().width()), |
| 306 | SkIntToScalar(layer->rect().height())); |
| 307 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 308 | layerCanvas->clipRect(bound); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 309 | |
| 310 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 311 | |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 312 | SkMatrix initialCTM; |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 313 | initialCTM.setTranslate(SkIntToScalar(-offset.fX), SkIntToScalar(-offset.fY)); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 314 | initialCTM.preConcat(layers[i].fPreMat); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 315 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 316 | layerCanvas->setMatrix(initialCTM); |
| 317 | layerCanvas->concat(layers[i].fLocalMat); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 318 | |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 319 | SkRecordPartialDraw(*pict->fRecord.get(), layerCanvas, |
| 320 | pict->drawablePicts(), pict->drawableCount(), |
| 321 | bound, layer->start()+1, layer->stop(), initialCTM); |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 322 | |
| 323 | layerCanvas->flush(); |
| 324 | } |
| 325 | } |
| 326 | |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 327 | void GrLayerHoister::UnlockLayers(GrContext* context, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 328 | const SkTDArray<GrHoistedLayer>& layers) { |
robertphillips | d61ef01 | 2014-10-08 05:17:02 -0700 | [diff] [blame] | 329 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 330 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 331 | for (int i = 0; i < layers.count(); ++i) { |
| 332 | layerCache->removeUse(layers[i].fLayer); |
robertphillips | b5a9715 | 2014-09-30 11:33:02 -0700 | [diff] [blame] | 333 | } |
| 334 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 335 | SkDEBUGCODE(layerCache->validate();) |
| 336 | } |
| 337 | |
| 338 | void GrLayerHoister::PurgeCache(GrContext* context) { |
| 339 | #if !GR_CACHE_HOISTED_LAYERS |
| 340 | GrLayerCache* layerCache = context->getLayerCache(); |
| 341 | |
robertphillips | 30d2cc6 | 2014-09-24 08:52:18 -0700 | [diff] [blame] | 342 | // This code completely clears out the atlas. It is required when |
| 343 | // caching is disabled so the atlas doesn't fill up and force more |
| 344 | // free floating layers |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 345 | layerCache->purgeAll(); |
| 346 | #endif |
robertphillips | 98d709b | 2014-09-02 10:20:50 -0700 | [diff] [blame] | 347 | } |