OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrLayerCache.h" | 8 #include "GrLayerCache.h" |
9 #include "GrLayerHoister.h" | 9 #include "GrLayerHoister.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkRecordDraw.h" | 11 #include "SkRecordDraw.h" |
12 #include "GrRecordReplaceDraw.h" | 12 #include "GrRecordReplaceDraw.h" |
13 #include "SkGrPixelRef.h" | 13 #include "SkGrPixelRef.h" |
14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
15 | 15 |
16 // Return true if any layers are suitable for hoisting | 16 // Return true if any layers are suitable for hoisting |
17 bool GrLayerHoister::FindLayersToHoist(const GrAccelData *gpuData, | 17 bool GrLayerHoister::FindLayersToHoist(const SkPicture* topLevelPicture, |
18 const SkRect& query, | 18 const SkRect& query, |
19 SkTDArray<GrCachedLayer*>* atlased, | 19 SkTDArray<HoistedLayer>* atlased, |
20 SkTDArray<GrCachedLayer*>* nonAtlased, | 20 SkTDArray<HoistedLayer>* nonAtlased, |
21 GrLayerCache* layerCache) { | 21 GrLayerCache* layerCache) { |
22 bool anyHoisted = false; | 22 bool anyHoisted = false; |
23 | 23 |
| 24 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
| 25 |
| 26 const SkPicture::AccelData* topLevelData = topLevelPicture->EXPERIMENTAL_get
AccelData(key); |
| 27 if (NULL == topLevelData) { |
| 28 return false; |
| 29 } |
| 30 |
| 31 const GrAccelData *topLevelGPUData = static_cast<const GrAccelData*>(topLeve
lData); |
| 32 if (0 == topLevelGPUData->numSaveLayers()) { |
| 33 return false; |
| 34 } |
| 35 |
24 // Layer hoisting pre-renders the entire layer since it will be cached and p
otentially | 36 // 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 | 37 // 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 | 38 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerM
axSize |
27 // is used to limit which clips are pre-rendered. | 39 // is used to limit which clips are pre-rendered. |
28 static const int kSaveLayerMaxSize = 256; | 40 static const int kSaveLayerMaxSize = 256; |
29 | 41 |
30 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers()); | 42 SkAutoTArray<bool> pullForward(topLevelGPUData->numSaveLayers()); |
31 | 43 |
32 // Pre-render all the layers that intersect the query rect | 44 // Pre-render all the layers that intersect the query rect |
33 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | 45 for (int i = 0; i < topLevelGPUData->numSaveLayers(); ++i) { |
34 pullForward[i] = false; | 46 pullForward[i] = false; |
35 | 47 |
36 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | 48 const GrAccelData::SaveLayerInfo& info = topLevelGPUData->saveLayerInfo(
i); |
37 | 49 |
38 SkRect layerRect = SkRect::MakeXYWH(SkIntToScalar(info.fOffset.fX), | 50 SkRect layerRect = SkRect::MakeXYWH(SkIntToScalar(info.fOffset.fX), |
39 SkIntToScalar(info.fOffset.fY), | 51 SkIntToScalar(info.fOffset.fY), |
40 SkIntToScalar(info.fSize.fWidth), | 52 SkIntToScalar(info.fSize.fWidth), |
41 SkIntToScalar(info.fSize.fHeight)); | 53 SkIntToScalar(info.fSize.fHeight)); |
42 | 54 |
43 if (!SkRect::Intersects(query, layerRect)) { | 55 if (!SkRect::Intersects(query, layerRect)) { |
44 continue; | 56 continue; |
45 } | 57 } |
46 | 58 |
47 // TODO: once this code is more stable unsuitable layers can | 59 // TODO: once this code is more stable unsuitable layers can |
48 // just be omitted during the optimization stage | 60 // just be omitted during the optimization stage |
49 if (!info.fValid || | 61 if (!info.fValid || |
50 kSaveLayerMaxSize < info.fSize.fWidth || | 62 kSaveLayerMaxSize < info.fSize.fWidth || |
51 kSaveLayerMaxSize < info.fSize.fHeight || | 63 kSaveLayerMaxSize < info.fSize.fHeight || |
52 info.fIsNested) { | 64 info.fIsNested) { |
53 continue; | 65 continue; |
54 } | 66 } |
55 | 67 |
56 pullForward[i] = true; | 68 pullForward[i] = true; |
57 anyHoisted = true; | 69 anyHoisted = true; |
58 } | 70 } |
59 | 71 |
60 if (!anyHoisted) { | 72 if (!anyHoisted) { |
61 return false; | 73 return false; |
62 } | 74 } |
63 | 75 |
64 atlased->setReserve(atlased->reserved() + gpuData->numSaveLayers()); | 76 atlased->setReserve(atlased->reserved() + topLevelGPUData->numSaveLayers()); |
65 | 77 |
66 // Generate the layer and/or ensure it is locked | 78 // Generate the layer and/or ensure it is locked |
67 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | 79 for (int i = 0; i < topLevelGPUData->numSaveLayers(); ++i) { |
68 if (pullForward[i]) { | 80 if (pullForward[i]) { |
69 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | 81 const GrAccelData::SaveLayerInfo& info = topLevelGPUData->saveLayerI
nfo(i); |
| 82 const SkPicture* pict = info.fPicture ? info.fPicture : topLevelPict
ure; |
70 | 83 |
71 GrCachedLayer* layer = layerCache->findLayerOrCreate(info.fPictureID
, | 84 GrCachedLayer* layer = layerCache->findLayerOrCreate(pict->uniqueID(
), |
72 info.fSaveLayer
OpID, | 85 info.fSaveLayer
OpID, |
73 info.fRestoreOp
ID, | 86 info.fRestoreOp
ID, |
74 info.fOffset, | 87 info.fOffset, |
75 info.fOriginXfo
rm, | 88 info.fOriginXfo
rm, |
76 info.fPaint); | 89 info.fPaint); |
77 | 90 |
78 GrTextureDesc desc; | 91 GrTextureDesc desc; |
79 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 92 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
80 desc.fWidth = info.fSize.fWidth; | 93 desc.fWidth = info.fSize.fWidth; |
81 desc.fHeight = info.fSize.fHeight; | 94 desc.fHeight = info.fSize.fHeight; |
82 desc.fConfig = kSkia8888_GrPixelConfig; | 95 desc.fConfig = kSkia8888_GrPixelConfig; |
83 // TODO: need to deal with sample count | 96 // TODO: need to deal with sample count |
84 | 97 |
85 bool needsRendering = layerCache->lock(layer, desc, | 98 bool needsRendering = layerCache->lock(layer, desc, |
86 info.fHasNestedLayers || info
.fIsNested); | 99 info.fHasNestedLayers || info
.fIsNested); |
87 if (NULL == layer->texture()) { | 100 if (NULL == layer->texture()) { |
88 continue; | 101 continue; |
89 } | 102 } |
90 | 103 |
91 if (needsRendering) { | 104 if (needsRendering) { |
| 105 HoistedLayer* info; |
| 106 |
92 if (layer->isAtlased()) { | 107 if (layer->isAtlased()) { |
93 *atlased->append() = layer; | 108 info = atlased->append(); |
94 } else { | 109 } else { |
95 *nonAtlased->append() = layer; | 110 info = nonAtlased->append(); |
96 } | 111 } |
| 112 |
| 113 info->fLayer = layer; |
| 114 info->fPicture = pict; |
97 } | 115 } |
98 } | 116 } |
99 } | 117 } |
100 | 118 |
101 return anyHoisted; | 119 return anyHoisted; |
102 } | 120 } |
103 | 121 |
104 static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* re
sult) { | 122 static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* re
sult) { |
105 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 123 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
106 result->setInfo(info); | 124 result->setInfo(info); |
107 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); | 125 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
108 } | 126 } |
109 | 127 |
110 static void convert_layers_to_replacements(const SkTDArray<GrCachedLayer*>& laye
rs, | 128 static void convert_layers_to_replacements(const SkTDArray<GrLayerHoister::Hoist
edLayer>& layers, |
111 GrReplacements* replacements) { | 129 GrReplacements* replacements) { |
112 // TODO: just replace GrReplacements::ReplacementInfo with GrCachedLayer? | 130 // TODO: just replace GrReplacements::ReplacementInfo with GrCachedLayer? |
113 for (int i = 0; i < layers.count(); ++i) { | 131 for (int i = 0; i < layers.count(); ++i) { |
114 GrReplacements::ReplacementInfo* layerInfo = replacements->push(); | 132 GrReplacements::ReplacementInfo* layerInfo = replacements->push(); |
115 layerInfo->fStart = layers[i]->start(); | 133 layerInfo->fStart = layers[i].fLayer->start(); |
116 layerInfo->fStop = layers[i]->stop(); | 134 layerInfo->fStop = layers[i].fLayer->stop(); |
117 layerInfo->fPos = layers[i]->offset();; | 135 layerInfo->fPos = layers[i].fLayer->offset();; |
118 | 136 |
119 SkBitmap bm; | 137 SkBitmap bm; |
120 wrap_texture(layers[i]->texture(), | 138 wrap_texture(layers[i].fLayer->texture(), |
121 !layers[i]->isAtlased() ? layers[i]->rect().width() | 139 !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().w
idth() |
122 : layers[i]->texture()->width(), | 140 : layers[i].fLayer->texture(
)->width(), |
123 !layers[i]->isAtlased() ? layers[i]->rect().height() | 141 !layers[i].fLayer->isAtlased() ? layers[i].fLayer->rect().h
eight() |
124 : layers[i]->texture()->height(), | 142 : layers[i].fLayer->texture(
)->height(), |
125 &bm); | 143 &bm); |
126 layerInfo->fImage = SkImage::NewTexture(bm); | 144 layerInfo->fImage = SkImage::NewTexture(bm); |
127 | 145 |
128 layerInfo->fPaint = layers[i]->paint() ? SkNEW_ARGS(SkPaint, (*layers[i]
->paint())) : NULL; | 146 layerInfo->fPaint = layers[i].fLayer->paint() |
| 147 ? SkNEW_ARGS(SkPaint, (*layers[i].fLayer->paint(
))) |
| 148 : NULL; |
129 | 149 |
130 layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i]->rect().fLeft, | 150 layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i].fLayer->rect().fLeft, |
131 layers[i]->rect().fTop, | 151 layers[i].fLayer->rect().fTop, |
132 layers[i]->rect().width(), | 152 layers[i].fLayer->rect().width()
, |
133 layers[i]->rect().height()); | 153 layers[i].fLayer->rect().height(
)); |
134 } | 154 } |
135 } | 155 } |
136 | 156 |
137 void GrLayerHoister::DrawLayers(const SkPicture* picture, | 157 void GrLayerHoister::DrawLayers(const SkTDArray<HoistedLayer>& atlased, |
138 const SkTDArray<GrCachedLayer*>& atlased, | 158 const SkTDArray<HoistedLayer>& nonAtlased, |
139 const SkTDArray<GrCachedLayer*>& nonAtlased, | |
140 GrReplacements* replacements) { | 159 GrReplacements* replacements) { |
141 // Render the atlased layers that require it | 160 // Render the atlased layers that require it |
142 if (atlased.count() > 0) { | 161 if (atlased.count() > 0) { |
143 // All the atlased layers are rendered into the same GrTexture | 162 // All the atlased layers are rendered into the same GrTexture |
144 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( | 163 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
145 atlased[0]->texture()->asRenderTarget(),
NULL)); | 164 atlased[0].fLayer->texture()->asRenderTa
rget(), NULL)); |
146 | 165 |
147 SkCanvas* atlasCanvas = surface->getCanvas(); | 166 SkCanvas* atlasCanvas = surface->getCanvas(); |
148 | 167 |
149 SkPaint paint; | 168 SkPaint paint; |
150 paint.setColor(SK_ColorTRANSPARENT); | 169 paint.setColor(SK_ColorTRANSPARENT); |
151 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); | 170 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); |
152 | 171 |
153 for (int i = 0; i < atlased.count(); ++i) { | 172 for (int i = 0; i < atlased.count(); ++i) { |
154 GrCachedLayer* layer = atlased[i]; | 173 GrCachedLayer* layer = atlased[i].fLayer; |
| 174 const SkPicture* pict = atlased[i].fPicture; |
155 | 175 |
156 atlasCanvas->save(); | 176 atlasCanvas->save(); |
157 | 177 |
158 // Add a rect clip to make sure the rendering doesn't | 178 // Add a rect clip to make sure the rendering doesn't |
159 // extend beyond the boundaries of the atlased sub-rect | 179 // extend beyond the boundaries of the atlased sub-rect |
160 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | 180 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
161 SkIntToScalar(layer->rect().fTop), | 181 SkIntToScalar(layer->rect().fTop), |
162 SkIntToScalar(layer->rect().width())
, | 182 SkIntToScalar(layer->rect().width())
, |
163 SkIntToScalar(layer->rect().height()
)); | 183 SkIntToScalar(layer->rect().height()
)); |
164 atlasCanvas->clipRect(bound); | 184 atlasCanvas->clipRect(bound); |
165 | 185 |
166 // Since 'clear' doesn't respect the clip we need to draw a rect | 186 // Since 'clear' doesn't respect the clip we need to draw a rect |
167 // TODO: ensure none of the atlased layers contain a clear call! | 187 // TODO: ensure none of the atlased layers contain a clear call! |
168 atlasCanvas->drawRect(bound, paint); | 188 atlasCanvas->drawRect(bound, paint); |
169 | 189 |
170 // info.fCTM maps the layer's top/left to the origin. | 190 // info.fCTM maps the layer's top/left to the origin. |
171 // Since this layer is atlased, the top/left corner needs | 191 // Since this layer is atlased, the top/left corner needs |
172 // to be offset to the correct location in the backing texture. | 192 // to be offset to the correct location in the backing texture. |
173 SkMatrix initialCTM; | 193 SkMatrix initialCTM; |
174 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), | 194 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), |
175 SkIntToScalar(-layer->offset().fY)); | 195 SkIntToScalar(-layer->offset().fY)); |
176 initialCTM.postTranslate(bound.fLeft, bound.fTop); | 196 initialCTM.postTranslate(bound.fLeft, bound.fTop); |
177 | 197 |
178 atlasCanvas->translate(SkIntToScalar(-layer->offset().fX), | 198 atlasCanvas->translate(SkIntToScalar(-layer->offset().fX), |
179 SkIntToScalar(-layer->offset().fY)); | 199 SkIntToScalar(-layer->offset().fY)); |
180 atlasCanvas->translate(bound.fLeft, bound.fTop); | 200 atlasCanvas->translate(bound.fLeft, bound.fTop); |
181 atlasCanvas->concat(layer->ctm()); | 201 atlasCanvas->concat(layer->ctm()); |
182 | 202 |
183 SkRecordPartialDraw(*picture->fRecord.get(), atlasCanvas, bound, | 203 SkRecordPartialDraw(*pict->fRecord.get(), atlasCanvas, bound, |
184 layer->start()+1, layer->stop(), initialCTM); | 204 layer->start()+1, layer->stop(), initialCTM); |
185 | 205 |
186 atlasCanvas->restore(); | 206 atlasCanvas->restore(); |
187 } | 207 } |
188 | 208 |
189 atlasCanvas->flush(); | 209 atlasCanvas->flush(); |
190 } | 210 } |
191 | 211 |
192 // Render the non-atlased layers that require it | 212 // Render the non-atlased layers that require it |
193 for (int i = 0; i < nonAtlased.count(); ++i) { | 213 for (int i = 0; i < nonAtlased.count(); ++i) { |
194 GrCachedLayer* layer = nonAtlased[i]; | 214 GrCachedLayer* layer = nonAtlased[i].fLayer; |
| 215 const SkPicture* pict = nonAtlased[i].fPicture; |
195 | 216 |
196 // Each non-atlased layer has its own GrTexture | 217 // Each non-atlased layer has its own GrTexture |
197 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( | 218 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
198 layer->texture()->asRenderTarget(), NULL
)); | 219 layer->texture()->asRenderTarget(), NULL
)); |
199 | 220 |
200 SkCanvas* layerCanvas = surface->getCanvas(); | 221 SkCanvas* layerCanvas = surface->getCanvas(); |
201 | 222 |
202 // Add a rect clip to make sure the rendering doesn't | 223 // Add a rect clip to make sure the rendering doesn't |
203 // extend beyond the boundaries of the atlased sub-rect | 224 // extend beyond the boundaries of the atlased sub-rect |
204 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | 225 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
205 SkIntToScalar(layer->rect().fTop), | 226 SkIntToScalar(layer->rect().fTop), |
206 SkIntToScalar(layer->rect().width()), | 227 SkIntToScalar(layer->rect().width()), |
207 SkIntToScalar(layer->rect().height())); | 228 SkIntToScalar(layer->rect().height())); |
208 | 229 |
209 layerCanvas->clipRect(bound); // TODO: still useful? | 230 layerCanvas->clipRect(bound); // TODO: still useful? |
210 | 231 |
211 layerCanvas->clear(SK_ColorTRANSPARENT); | 232 layerCanvas->clear(SK_ColorTRANSPARENT); |
212 | 233 |
213 SkMatrix initialCTM; | 234 SkMatrix initialCTM; |
214 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), | 235 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), |
215 SkIntToScalar(-layer->offset().fY)); | 236 SkIntToScalar(-layer->offset().fY)); |
216 | 237 |
217 layerCanvas->translate(SkIntToScalar(-layer->offset().fX), | 238 layerCanvas->translate(SkIntToScalar(-layer->offset().fX), |
218 SkIntToScalar(-layer->offset().fY)); | 239 SkIntToScalar(-layer->offset().fY)); |
219 layerCanvas->concat(layer->ctm()); | 240 layerCanvas->concat(layer->ctm()); |
220 | 241 |
221 SkRecordPartialDraw(*picture->fRecord.get(), layerCanvas, bound, | 242 SkRecordPartialDraw(*pict->fRecord.get(), layerCanvas, bound, |
222 layer->start()+1, layer->stop(), initialCTM); | 243 layer->start()+1, layer->stop(), initialCTM); |
223 | 244 |
224 layerCanvas->flush(); | 245 layerCanvas->flush(); |
225 } | 246 } |
226 | 247 |
227 convert_layers_to_replacements(atlased, replacements); | 248 convert_layers_to_replacements(atlased, replacements); |
228 convert_layers_to_replacements(nonAtlased, replacements); | 249 convert_layers_to_replacements(nonAtlased, replacements); |
229 } | 250 } |
230 | 251 |
231 void GrLayerHoister::UnlockLayers(GrLayerCache* layerCache, const SkPicture* pic
ture) { | 252 static void unlock_layer_in_cache(GrLayerCache* layerCache, |
232 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); | 253 const SkPicture* picture, |
| 254 GrCachedLayer* layer) { |
| 255 layerCache->unlock(layer); |
233 | 256 |
234 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); | 257 #if DISABLE_CACHING |
235 SkASSERT(data); | 258 // This code completely clears out the atlas. It is required when |
| 259 // caching is disabled so the atlas doesn't fill up and force more |
| 260 // free floating layers |
| 261 layerCache->purge(picture->uniqueID()); |
| 262 #endif |
| 263 } |
236 | 264 |
237 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); | 265 void GrLayerHoister::UnlockLayers(GrLayerCache* layerCache, |
238 SkASSERT(0 != gpuData->numSaveLayers()); | 266 const SkTDArray<HoistedLayer>& atlased, |
| 267 const SkTDArray<HoistedLayer>& nonAtlased) { |
239 | 268 |
240 // unlock the layers | 269 for (int i = 0; i < atlased.count(); ++i) { |
241 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | 270 unlock_layer_in_cache(layerCache, atlased[i].fPicture, atlased[i].fLayer
); |
242 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | 271 } |
243 | 272 |
244 GrCachedLayer* layer = layerCache->findLayer(picture->uniqueID(), | 273 for (int i = 0; i < nonAtlased.count(); ++i) { |
245 info.fSaveLayerOpID, | 274 unlock_layer_in_cache(layerCache, nonAtlased[i].fPicture, nonAtlased[i].
fLayer); |
246 info.fRestoreOpID, | |
247 info.fOffset, | |
248 info.fOriginXform); | |
249 layerCache->unlock(layer); | |
250 } | 275 } |
251 | 276 |
252 #if DISABLE_CACHING | 277 #if DISABLE_CACHING |
253 // This code completely clears out the atlas. It is required when | 278 // This code completely clears out the atlas. It is required when |
254 // caching is disabled so the atlas doesn't fill up and force more | 279 // caching is disabled so the atlas doesn't fill up and force more |
255 // free floating layers | 280 // free floating layers |
256 layerCache->purge(picture->uniqueID()); | |
257 | |
258 layerCache->purgeAll(); | 281 layerCache->purgeAll(); |
259 #endif | 282 #endif |
260 } | 283 } |
261 | 284 |
OLD | NEW |