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 // Need to include something before #if SK_SUPPORT_GPU so that the Android
9 // framework build, which gets its defines from SkTypes rather than a makefile,
10 // has the definition before checking it.
11 #include "SkCanvas.h"
12 #include "SkCanvasPriv.h"
13 #include "SkMultiPictureDraw.h"
14 #include "SkPicture.h"
15 #include "SkTaskGroup.h"
16
17 #if SK_SUPPORT_GPU
18 #include "GrContext.h"
19 #include "GrLayerHoister.h"
20 #include "GrRecordReplaceDraw.h"
21 #include "GrRenderTarget.h"
22 #endif
23
draw()24 void SkMultiPictureDraw::DrawData::draw() {
25 fCanvas->drawPicture(fPicture, &fMatrix, fPaint);
26 }
27
init(SkCanvas * canvas,const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)28 void SkMultiPictureDraw::DrawData::init(SkCanvas* canvas, const SkPicture* picture,
29 const SkMatrix* matrix, const SkPaint* paint) {
30 fPicture = SkRef(picture);
31 fCanvas = SkRef(canvas);
32 if (matrix) {
33 fMatrix = *matrix;
34 } else {
35 fMatrix.setIdentity();
36 }
37 if (paint) {
38 fPaint = new SkPaint(*paint);
39 } else {
40 fPaint = nullptr;
41 }
42 }
43
Reset(SkTDArray<DrawData> & data)44 void SkMultiPictureDraw::DrawData::Reset(SkTDArray<DrawData>& data) {
45 for (int i = 0; i < data.count(); ++i) {
46 data[i].fPicture->unref();
47 data[i].fCanvas->unref();
48 delete data[i].fPaint;
49 }
50 data.rewind();
51 }
52
53 //////////////////////////////////////////////////////////////////////////////////////
54
SkMultiPictureDraw(int reserve)55 SkMultiPictureDraw::SkMultiPictureDraw(int reserve) {
56 if (reserve > 0) {
57 fGPUDrawData.setReserve(reserve);
58 fThreadSafeDrawData.setReserve(reserve);
59 }
60 }
61
reset()62 void SkMultiPictureDraw::reset() {
63 DrawData::Reset(fGPUDrawData);
64 DrawData::Reset(fThreadSafeDrawData);
65 }
66
add(SkCanvas * canvas,const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)67 void SkMultiPictureDraw::add(SkCanvas* canvas,
68 const SkPicture* picture,
69 const SkMatrix* matrix,
70 const SkPaint* paint) {
71 if (nullptr == canvas || nullptr == picture) {
72 SkDEBUGFAIL("parameters to SkMultiPictureDraw::add should be non-nullptr");
73 return;
74 }
75
76 SkTDArray<DrawData>& array = canvas->getGrContext() ? fGPUDrawData : fThreadSafeDrawData;
77 array.append()->init(canvas, picture, matrix, paint);
78 }
79
80 class AutoMPDReset : SkNoncopyable {
81 SkMultiPictureDraw* fMPD;
82 public:
AutoMPDReset(SkMultiPictureDraw * mpd)83 AutoMPDReset(SkMultiPictureDraw* mpd) : fMPD(mpd) {}
~AutoMPDReset()84 ~AutoMPDReset() { fMPD->reset(); }
85 };
86
87 //#define FORCE_SINGLE_THREAD_DRAWING_FOR_TESTING
88
draw(bool flush)89 void SkMultiPictureDraw::draw(bool flush) {
90 AutoMPDReset mpdreset(this);
91
92 #ifdef FORCE_SINGLE_THREAD_DRAWING_FOR_TESTING
93 for (int i = 0; i < fThreadSafeDrawData.count(); ++i) {
94 fThreadSafeDrawData[i].draw();
95 }
96 #else
97 SkTaskGroup().batch(fThreadSafeDrawData.count(), [&](int i) {
98 fThreadSafeDrawData[i].draw();
99 });
100 #endif
101
102 // N.B. we could get going on any GPU work from this main thread while the CPU work runs.
103 // But in practice, we've either got GPU work or CPU work, not both.
104
105 const int count = fGPUDrawData.count();
106 if (0 == count) {
107 return;
108 }
109
110 #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
111 GrContext* context = fGPUDrawData[0].fCanvas->getGrContext();
112 SkASSERT(context);
113
114 // Start by collecting all the layers that are going to be atlased and render
115 // them (if necessary). Hoisting the free floating layers is deferred until
116 // drawing the canvas that requires them.
117 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
118
119 GrLayerHoister::Begin(context);
120
121 for (int i = 0; i < count; ++i) {
122 const DrawData& data = fGPUDrawData[i];
123 // we only expect 1 context for all the canvases
124 SkASSERT(data.fCanvas->getGrContext() == context);
125
126 if (!data.fPaint) {
127 SkRect clipBounds;
128 if (!data.fCanvas->getClipBounds(&clipBounds)) {
129 continue;
130 }
131
132 SkMatrix initialMatrix = data.fCanvas->getTotalMatrix();
133 initialMatrix.preConcat(data.fMatrix);
134
135 GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget();
136 SkASSERT(rt);
137
138 // TODO: sorting the cacheable layers from smallest to largest
139 // would improve the packing and reduce the number of swaps
140 // TODO: another optimization would be to make a first pass to
141 // lock any required layer that is already in the atlas
142 GrLayerHoister::FindLayersToAtlas(context, data.fPicture, initialMatrix,
143 clipBounds,
144 &atlasedNeedRendering, &atlasedRecycled,
145 rt->numColorSamples());
146 }
147 }
148
149 GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering);
150
151 SkTDArray<GrHoistedLayer> needRendering, recycled;
152 #endif
153
154 for (int i = 0; i < count; ++i) {
155 const DrawData& data = fGPUDrawData[i];
156 SkCanvas* canvas = data.fCanvas;
157 const SkPicture* picture = data.fPicture;
158
159 #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
160 if (!data.fPaint) {
161
162 SkRect clipBounds;
163 if (!canvas->getClipBounds(&clipBounds)) {
164 continue;
165 }
166
167 SkAutoCanvasMatrixPaint acmp(canvas, &data.fMatrix, data.fPaint, picture->cullRect());
168
169 const SkMatrix initialMatrix = canvas->getTotalMatrix();
170
171 GrRenderTarget* rt = data.fCanvas->internal_private_accessTopLayerRenderTarget();
172 SkASSERT(rt);
173
174 // Find the layers required by this canvas. It will return atlased
175 // layers in the 'recycled' list since they have already been drawn.
176 GrLayerHoister::FindLayersToHoist(context, picture, initialMatrix,
177 clipBounds, &needRendering, &recycled,
178 rt->numColorSamples());
179
180 GrLayerHoister::DrawLayers(context, needRendering);
181
182 // Render the entire picture using new layers
183 GrRecordReplaceDraw(picture, canvas, context->getLayerCache(),
184 initialMatrix, nullptr);
185
186 GrLayerHoister::UnlockLayers(context, needRendering);
187 GrLayerHoister::UnlockLayers(context, recycled);
188
189 needRendering.rewind();
190 recycled.rewind();
191 } else
192 #endif
193 {
194 canvas->drawPicture(picture, &data.fMatrix, data.fPaint);
195 }
196 if (flush) {
197 canvas->flush();
198 }
199 }
200
201 #if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
202 GrLayerHoister::UnlockLayers(context, atlasedNeedRendering);
203 GrLayerHoister::UnlockLayers(context, atlasedRecycled);
204 GrLayerHoister::End(context);
205 #endif
206 }
207
208