OLD | NEW |
(Empty) | |
| 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" |
| 10 #include "SkPictureRangePlayback.h" |
| 11 #include "SkCanvas.h" |
| 12 #include "SkSurface.h" |
| 13 |
| 14 // Return true if any layers are suitable for hoisting |
| 15 bool GrLayerHoister::FindLayersToHoist(const GrAccelData *gpuData, |
| 16 const SkPicture::OperationList* ops, |
| 17 const SkRect& query, |
| 18 bool pullForward[]) { |
| 19 bool anyHoisted = false; |
| 20 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
| 21 pullForward[i] = false; |
| 22 } |
| 23 |
| 24 // Layer hoisting pre-renders the entire layer since it will be cached and p
otentially |
| 25 // reused with different clips (e.g., in different tiles). Because of this t
he |
| 26 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerM
axSize |
| 27 // is used to limit which clips are pre-rendered. |
| 28 static const int kSaveLayerMaxSize = 256; |
| 29 |
| 30 if (NULL != ops) { |
| 31 // In this case the picture has been generated with a BBH so we use |
| 32 // the BBH to limit the pre-rendering to just the layers needed to cover |
| 33 // the region being drawn |
| 34 for (int i = 0; i < ops->numOps(); ++i) { |
| 35 uint32_t offset = ops->offset(i); |
| 36 |
| 37 // For now we're saving all the layers in the GrAccelData so they |
| 38 // can be nested. Additionally, the nested layers appear before |
| 39 // their parent in the list. |
| 40 for (int j = 0; j < gpuData->numSaveLayers(); ++j) { |
| 41 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(
j); |
| 42 |
| 43 if (pullForward[j]) { |
| 44 continue; // already pulling forward |
| 45 } |
| 46 |
| 47 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID)
{ |
| 48 continue; // the op isn't in this range |
| 49 } |
| 50 |
| 51 // TODO: once this code is more stable unsuitable layers can |
| 52 // just be omitted during the optimization stage |
| 53 if (!info.fValid || |
| 54 kSaveLayerMaxSize < info.fSize.fWidth || |
| 55 kSaveLayerMaxSize < info.fSize.fHeight || |
| 56 info.fIsNested) { |
| 57 continue; // this layer is unsuitable |
| 58 } |
| 59 |
| 60 pullForward[j] = true; |
| 61 anyHoisted = true; |
| 62 } |
| 63 } |
| 64 } else { |
| 65 // In this case there is no BBH associated with the picture. Pre-render |
| 66 // all the layers that intersect the drawn region |
| 67 for (int j = 0; j < gpuData->numSaveLayers(); ++j) { |
| 68 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j); |
| 69 |
| 70 SkRect layerRect = SkRect::MakeXYWH(SkIntToScalar(info.fOffset.fX), |
| 71 SkIntToScalar(info.fOffset.fY), |
| 72 SkIntToScalar(info.fSize.fWidth)
, |
| 73 SkIntToScalar(info.fSize.fHeight
)); |
| 74 |
| 75 if (!SkRect::Intersects(query, layerRect)) { |
| 76 continue; |
| 77 } |
| 78 |
| 79 // TODO: once this code is more stable unsuitable layers can |
| 80 // just be omitted during the optimization stage |
| 81 if (!info.fValid || |
| 82 kSaveLayerMaxSize < info.fSize.fWidth || |
| 83 kSaveLayerMaxSize < info.fSize.fHeight || |
| 84 info.fIsNested) { |
| 85 continue; |
| 86 } |
| 87 |
| 88 pullForward[j] = true; |
| 89 anyHoisted = true; |
| 90 } |
| 91 } |
| 92 |
| 93 return anyHoisted; |
| 94 } |
| 95 |
| 96 void GrLayerHoister::DrawLayers(const SkPicture* picture, |
| 97 const SkTDArray<GrCachedLayer*>& atlased, |
| 98 const SkTDArray<GrCachedLayer*>& nonAtlased) { |
| 99 // Render the atlased layers that require it |
| 100 if (atlased.count() > 0) { |
| 101 // All the atlased layers are rendered into the same GrTexture |
| 102 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
| 103 atlased[0]->texture()->asRenderT
arget(), |
| 104 SkSurface::kStandard_TextRenderM
ode, |
| 105 SkSurface::kDontClear_RenderTarg
etFlag)); |
| 106 |
| 107 SkCanvas* atlasCanvas = surface->getCanvas(); |
| 108 |
| 109 SkPaint paint; |
| 110 paint.setColor(SK_ColorTRANSPARENT); |
| 111 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); |
| 112 |
| 113 for (int i = 0; i < atlased.count(); ++i) { |
| 114 GrCachedLayer* layer = atlased[i]; |
| 115 |
| 116 atlasCanvas->save(); |
| 117 |
| 118 // Add a rect clip to make sure the rendering doesn't |
| 119 // extend beyond the boundaries of the atlased sub-rect |
| 120 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
| 121 SkIntToScalar(layer->rect().fTop), |
| 122 SkIntToScalar(layer->rect().width())
, |
| 123 SkIntToScalar(layer->rect().height()
)); |
| 124 atlasCanvas->clipRect(bound); |
| 125 |
| 126 // Since 'clear' doesn't respect the clip we need to draw a rect |
| 127 // TODO: ensure none of the atlased layers contain a clear call! |
| 128 atlasCanvas->drawRect(bound, paint); |
| 129 |
| 130 // info.fCTM maps the layer's top/left to the origin. |
| 131 // Since this layer is atlased, the top/left corner needs |
| 132 // to be offset to the correct location in the backing texture. |
| 133 atlasCanvas->translate(bound.fLeft, bound.fTop); |
| 134 atlasCanvas->concat(layer->ctm()); |
| 135 |
| 136 SkPictureRangePlayback rangePlayback(picture, |
| 137 layer->start(), |
| 138 layer->stop()); |
| 139 rangePlayback.draw(atlasCanvas, NULL); |
| 140 |
| 141 atlasCanvas->restore(); |
| 142 } |
| 143 |
| 144 atlasCanvas->flush(); |
| 145 } |
| 146 |
| 147 // Render the non-atlased layers that require it |
| 148 for (int i = 0; i < nonAtlased.count(); ++i) { |
| 149 GrCachedLayer* layer = nonAtlased[i]; |
| 150 |
| 151 // Each non-atlased layer has its own GrTexture |
| 152 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
| 153 layer->texture()->asRenderTarget(), |
| 154 SkSurface::kStandard_TextRenderMode, |
| 155 SkSurface::kDontClear_RenderTargetFlag)); |
| 156 |
| 157 SkCanvas* layerCanvas = surface->getCanvas(); |
| 158 |
| 159 // Add a rect clip to make sure the rendering doesn't |
| 160 // extend beyond the boundaries of the atlased sub-rect |
| 161 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
| 162 SkIntToScalar(layer->rect().fTop), |
| 163 SkIntToScalar(layer->rect().width()), |
| 164 SkIntToScalar(layer->rect().height())); |
| 165 |
| 166 layerCanvas->clipRect(bound); // TODO: still useful? |
| 167 |
| 168 layerCanvas->clear(SK_ColorTRANSPARENT); |
| 169 |
| 170 layerCanvas->concat(layer->ctm()); |
| 171 |
| 172 SkPictureRangePlayback rangePlayback(picture, |
| 173 layer->start(), |
| 174 layer->stop()); |
| 175 rangePlayback.draw(layerCanvas, NULL); |
| 176 |
| 177 layerCanvas->flush(); |
| 178 } |
| 179 } |
| 180 |
| 181 void GrLayerHoister::UnlockLayers(GrLayerCache* layerCache, const SkPicture* pic
ture) { |
| 182 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
| 183 |
| 184 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); |
| 185 SkASSERT(NULL != data); |
| 186 |
| 187 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); |
| 188 SkASSERT(0 != gpuData->numSaveLayers()); |
| 189 |
| 190 // unlock the layers |
| 191 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { |
| 192 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); |
| 193 |
| 194 GrCachedLayer* layer = layerCache->findLayer(picture->uniqueID(), |
| 195 info.fSaveLayerOpID, |
| 196 info.fRestoreOpID, |
| 197 info.fOriginXform); |
| 198 layerCache->unlock(layer); |
| 199 } |
| 200 |
| 201 #if DISABLE_CACHING |
| 202 // This code completely clears out the atlas. It is required when |
| 203 // caching is disabled so the atlas doesn't fill up and force more |
| 204 // free floating layers |
| 205 layerCache->purge(picture->uniqueID()); |
| 206 |
| 207 layerCache->purgeAll(); |
| 208 #endif |
| 209 } |
| 210 |
OLD | NEW |