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