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* mainPicture, |
18 const SkRect& query, | 18 const SkRect& query, |
19 SkTDArray<GrCachedLayer*>* atlased, | 19 SkTDArray<HoistingInfo>* atlased, |
20 SkTDArray<GrCachedLayer*>* nonAtlased, | 20 SkTDArray<HoistingInfo>* 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* mainData = mainPicture->EXPERIMENTAL_getAccelDat a(key); | |
27 if (NULL == mainData) { | |
28 return false; | |
29 } | |
30 | |
31 const GrAccelData *mainGPUData = static_cast<const GrAccelData*>(mainData); | |
32 if (0 == mainGPUData->numSaveLayers()) { | |
bsalomon
2014/09/23 14:57:35
does numSaveLayers() count the nested layers?
robertphillips
2014/09/24 14:49:03
Yes all the layers (both nested and appearing in s
| |
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(mainGPUData->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 < mainGPUData->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 = mainGPUData->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() + mainGPUData->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 < mainGPUData->numSaveLayers(); ++i) { |
68 if (pullForward[i]) { | 80 if (pullForward[i]) { |
69 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | 81 const GrAccelData::SaveLayerInfo& info = mainGPUData->saveLayerInfo( i); |
82 const SkPicture* pict = info.fPicture ? info.fPicture : mainPicture; | |
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 HoistingInfo* 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->layer = layer; | |
114 info->picture = 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 ingInfo>& 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].layer->start(); |
116 layerInfo->fStop = layers[i]->stop(); | 134 layerInfo->fStop = layers[i].layer->stop(); |
117 layerInfo->fPos = layers[i]->offset();; | 135 layerInfo->fPos = layers[i].layer->offset();; |
118 | 136 |
119 SkBitmap bm; | 137 SkBitmap bm; |
120 wrap_texture(layers[i]->texture(), | 138 wrap_texture(layers[i].layer->texture(), |
121 !layers[i]->isAtlased() ? layers[i]->rect().width() | 139 !layers[i].layer->isAtlased() ? layers[i].layer->rect().wid th() |
122 : layers[i]->texture()->width(), | 140 : layers[i].layer->texture()- >width(), |
123 !layers[i]->isAtlased() ? layers[i]->rect().height() | 141 !layers[i].layer->isAtlased() ? layers[i].layer->rect().hei ght() |
124 : layers[i]->texture()->height(), | 142 : layers[i].layer->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].layer->paint() |
147 ? SkNEW_ARGS(SkPaint, (*layers[i].layer->paint() )) | |
148 : NULL; | |
129 | 149 |
130 layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i]->rect().fLeft, | 150 layerInfo->fSrcRect = SkIRect::MakeXYWH(layers[i].layer->rect().fLeft, |
131 layers[i]->rect().fTop, | 151 layers[i].layer->rect().fTop, |
132 layers[i]->rect().width(), | 152 layers[i].layer->rect().width(), |
133 layers[i]->rect().height()); | 153 layers[i].layer->rect().height() ); |
134 } | 154 } |
135 } | 155 } |
136 | 156 |
137 void GrLayerHoister::DrawLayers(const SkPicture* picture, | 157 void GrLayerHoister::DrawLayers(const SkTDArray<HoistingInfo>& atlased, |
138 const SkTDArray<GrCachedLayer*>& atlased, | 158 const SkTDArray<HoistingInfo>& 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()->asRenderT arget(), | 164 atlased[0].layer->texture()->asR enderTarget(), |
146 SkSurface::kStandard_TextRenderM ode)); | 165 SkSurface::kStandard_TextRenderM ode)); |
147 | 166 |
148 SkCanvas* atlasCanvas = surface->getCanvas(); | 167 SkCanvas* atlasCanvas = surface->getCanvas(); |
149 | 168 |
150 SkPaint paint; | 169 SkPaint paint; |
151 paint.setColor(SK_ColorTRANSPARENT); | 170 paint.setColor(SK_ColorTRANSPARENT); |
152 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); | 171 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); |
153 | 172 |
154 for (int i = 0; i < atlased.count(); ++i) { | 173 for (int i = 0; i < atlased.count(); ++i) { |
155 GrCachedLayer* layer = atlased[i]; | 174 GrCachedLayer* layer = atlased[i].layer; |
175 const SkPicture* pict = atlased[i].picture; | |
156 | 176 |
157 atlasCanvas->save(); | 177 atlasCanvas->save(); |
158 | 178 |
159 // Add a rect clip to make sure the rendering doesn't | 179 // Add a rect clip to make sure the rendering doesn't |
160 // extend beyond the boundaries of the atlased sub-rect | 180 // extend beyond the boundaries of the atlased sub-rect |
161 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | 181 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
162 SkIntToScalar(layer->rect().fTop), | 182 SkIntToScalar(layer->rect().fTop), |
163 SkIntToScalar(layer->rect().width()) , | 183 SkIntToScalar(layer->rect().width()) , |
164 SkIntToScalar(layer->rect().height() )); | 184 SkIntToScalar(layer->rect().height() )); |
165 atlasCanvas->clipRect(bound); | 185 atlasCanvas->clipRect(bound); |
166 | 186 |
167 // Since 'clear' doesn't respect the clip we need to draw a rect | 187 // Since 'clear' doesn't respect the clip we need to draw a rect |
168 // TODO: ensure none of the atlased layers contain a clear call! | 188 // TODO: ensure none of the atlased layers contain a clear call! |
169 atlasCanvas->drawRect(bound, paint); | 189 atlasCanvas->drawRect(bound, paint); |
170 | 190 |
171 // info.fCTM maps the layer's top/left to the origin. | 191 // info.fCTM maps the layer's top/left to the origin. |
172 // Since this layer is atlased, the top/left corner needs | 192 // Since this layer is atlased, the top/left corner needs |
173 // to be offset to the correct location in the backing texture. | 193 // to be offset to the correct location in the backing texture. |
174 SkMatrix initialCTM; | 194 SkMatrix initialCTM; |
175 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), | 195 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), |
176 SkIntToScalar(-layer->offset().fY)); | 196 SkIntToScalar(-layer->offset().fY)); |
177 initialCTM.postTranslate(bound.fLeft, bound.fTop); | 197 initialCTM.postTranslate(bound.fLeft, bound.fTop); |
178 | 198 |
179 atlasCanvas->translate(SkIntToScalar(-layer->offset().fX), | 199 atlasCanvas->translate(SkIntToScalar(-layer->offset().fX), |
180 SkIntToScalar(-layer->offset().fY)); | 200 SkIntToScalar(-layer->offset().fY)); |
181 atlasCanvas->translate(bound.fLeft, bound.fTop); | 201 atlasCanvas->translate(bound.fLeft, bound.fTop); |
182 atlasCanvas->concat(layer->ctm()); | 202 atlasCanvas->concat(layer->ctm()); |
183 | 203 |
184 SkRecordPartialDraw(*picture->fRecord.get(), atlasCanvas, bound, | 204 SkRecordPartialDraw(*pict->fRecord.get(), atlasCanvas, bound, |
185 layer->start()+1, layer->stop(), initialCTM); | 205 layer->start()+1, layer->stop(), initialCTM); |
186 | 206 |
187 atlasCanvas->restore(); | 207 atlasCanvas->restore(); |
188 } | 208 } |
189 | 209 |
190 atlasCanvas->flush(); | 210 atlasCanvas->flush(); |
191 } | 211 } |
192 | 212 |
193 // Render the non-atlased layers that require it | 213 // Render the non-atlased layers that require it |
194 for (int i = 0; i < nonAtlased.count(); ++i) { | 214 for (int i = 0; i < nonAtlased.count(); ++i) { |
195 GrCachedLayer* layer = nonAtlased[i]; | 215 GrCachedLayer* layer = nonAtlased[i].layer; |
216 const SkPicture* pict = nonAtlased[i].picture; | |
196 | 217 |
197 // Each non-atlased layer has its own GrTexture | 218 // Each non-atlased layer has its own GrTexture |
198 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( | 219 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect( |
199 layer->texture()->asRenderTarget (), | 220 layer->texture()->asRenderTarget (), |
200 SkSurface::kStandard_TextRenderM ode)); | 221 SkSurface::kStandard_TextRenderM ode)); |
201 | 222 |
202 SkCanvas* layerCanvas = surface->getCanvas(); | 223 SkCanvas* layerCanvas = surface->getCanvas(); |
203 | 224 |
204 // Add a rect clip to make sure the rendering doesn't | 225 // Add a rect clip to make sure the rendering doesn't |
205 // extend beyond the boundaries of the atlased sub-rect | 226 // extend beyond the boundaries of the atlased sub-rect |
206 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), | 227 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft), |
207 SkIntToScalar(layer->rect().fTop), | 228 SkIntToScalar(layer->rect().fTop), |
208 SkIntToScalar(layer->rect().width()), | 229 SkIntToScalar(layer->rect().width()), |
209 SkIntToScalar(layer->rect().height())); | 230 SkIntToScalar(layer->rect().height())); |
210 | 231 |
211 layerCanvas->clipRect(bound); // TODO: still useful? | 232 layerCanvas->clipRect(bound); // TODO: still useful? |
212 | 233 |
213 layerCanvas->clear(SK_ColorTRANSPARENT); | 234 layerCanvas->clear(SK_ColorTRANSPARENT); |
214 | 235 |
215 SkMatrix initialCTM; | 236 SkMatrix initialCTM; |
216 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), | 237 initialCTM.setTranslate(SkIntToScalar(-layer->offset().fX), |
217 SkIntToScalar(-layer->offset().fY)); | 238 SkIntToScalar(-layer->offset().fY)); |
218 | 239 |
219 layerCanvas->translate(SkIntToScalar(-layer->offset().fX), | 240 layerCanvas->translate(SkIntToScalar(-layer->offset().fX), |
220 SkIntToScalar(-layer->offset().fY)); | 241 SkIntToScalar(-layer->offset().fY)); |
221 layerCanvas->concat(layer->ctm()); | 242 layerCanvas->concat(layer->ctm()); |
222 | 243 |
223 SkRecordPartialDraw(*picture->fRecord.get(), layerCanvas, bound, | 244 SkRecordPartialDraw(*pict->fRecord.get(), layerCanvas, bound, |
224 layer->start()+1, layer->stop(), initialCTM); | 245 layer->start()+1, layer->stop(), initialCTM); |
225 | 246 |
226 layerCanvas->flush(); | 247 layerCanvas->flush(); |
227 } | 248 } |
228 | 249 |
229 convert_layers_to_replacements(atlased, replacements); | 250 convert_layers_to_replacements(atlased, replacements); |
230 convert_layers_to_replacements(nonAtlased, replacements); | 251 convert_layers_to_replacements(nonAtlased, replacements); |
231 } | 252 } |
232 | 253 |
233 void GrLayerHoister::UnlockLayers(GrLayerCache* layerCache, const SkPicture* pic ture) { | 254 static void unlock_layer_in_cache(GrLayerCache* layerCache, |
234 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); | 255 const SkPicture* picture, |
256 GrCachedLayer* layer) { | |
257 layerCache->unlock(layer); | |
235 | 258 |
236 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key); | 259 #if DISABLE_CACHING |
237 SkASSERT(data); | 260 // This code completely clears out the atlas. It is required when |
261 // caching is disabled so the atlas doesn't fill up and force more | |
262 // free floating layers | |
263 layerCache->purge(picture->uniqueID()); | |
264 #endif | |
265 } | |
238 | 266 |
239 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); | 267 void GrLayerHoister::UnlockLayers(GrLayerCache* layerCache, |
240 SkASSERT(0 != gpuData->numSaveLayers()); | 268 const SkTDArray<HoistingInfo>& atlased, |
269 const SkTDArray<HoistingInfo>& nonAtlased) { | |
241 | 270 |
242 // unlock the layers | 271 for (int i = 0; i < atlased.count(); ++i) { |
243 for (int i = 0; i < gpuData->numSaveLayers(); ++i) { | 272 unlock_layer_in_cache(layerCache, atlased[i].picture, atlased[i].layer); |
244 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i); | 273 } |
245 | 274 |
246 GrCachedLayer* layer = layerCache->findLayer(picture->uniqueID(), | 275 for (int i = 0; i < nonAtlased.count(); ++i) { |
247 info.fSaveLayerOpID, | 276 unlock_layer_in_cache(layerCache, nonAtlased[i].picture, nonAtlased[i].l ayer); |
248 info.fRestoreOpID, | |
249 info.fOffset, | |
250 info.fOriginXform); | |
251 layerCache->unlock(layer); | |
252 } | 277 } |
253 | 278 |
254 #if DISABLE_CACHING | 279 #if DISABLE_CACHING |
255 // This code completely clears out the atlas. It is required when | 280 // This code completely clears out the atlas. It is required when |
256 // caching is disabled so the atlas doesn't fill up and force more | 281 // caching is disabled so the atlas doesn't fill up and force more |
257 // free floating layers | 282 // free floating layers |
258 layerCache->purge(picture->uniqueID()); | |
259 | |
260 layerCache->purgeAll(); | 283 layerCache->purgeAll(); |
261 #endif | 284 #endif |
262 } | 285 } |
263 | 286 |
OLD | NEW |