SkMultiPictureDraw.cpp revision ecf987559b1db0cb3fcaccdde2a065e4391d9b60
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#if SK_SUPPORT_GPU
9#include "GrLayerHoister.h"
10#include "GrRecordReplaceDraw.h"
11#endif
12
13#include "SkCanvas.h"
14#include "SkMultiPictureDraw.h"
15#include "SkPicture.h"
16#include "SkTaskGroup.h"
17
18void SkMultiPictureDraw::DrawData::draw() {
19    fCanvas->drawPicture(fPicture, &fMatrix, fPaint);
20}
21
22void SkMultiPictureDraw::DrawData::init(SkCanvas* canvas, const SkPicture* picture,
23                                        const SkMatrix* matrix, const SkPaint* paint) {
24    fPicture = SkRef(picture);
25    fCanvas = SkRef(canvas);
26    if (matrix) {
27        fMatrix = *matrix;
28    } else {
29        fMatrix.setIdentity();
30    }
31    if (paint) {
32        fPaint = SkNEW_ARGS(SkPaint, (*paint));
33    } else {
34        fPaint = NULL;
35    }
36}
37
38void SkMultiPictureDraw::DrawData::Reset(SkTDArray<DrawData>& data) {
39    for (int i = 0; i < data.count(); ++i) {
40        data[i].fPicture->unref();
41        data[i].fCanvas->unref();
42        SkDELETE(data[i].fPaint);
43    }
44    data.rewind();
45}
46
47//////////////////////////////////////////////////////////////////////////////////////
48
49SkMultiPictureDraw::SkMultiPictureDraw(int reserve) {
50    if (reserve > 0) {
51        fGPUDrawData.setReserve(reserve);
52        fThreadSafeDrawData.setReserve(reserve);
53    }
54}
55
56void SkMultiPictureDraw::reset() {
57    DrawData::Reset(fGPUDrawData);
58    DrawData::Reset(fThreadSafeDrawData);
59}
60
61void SkMultiPictureDraw::add(SkCanvas* canvas,
62                             const SkPicture* picture,
63                             const SkMatrix* matrix,
64                             const SkPaint* paint) {
65    if (NULL == canvas || NULL == picture) {
66        SkDEBUGFAIL("parameters to SkMultiPictureDraw::add should be non-NULL");
67        return;
68    }
69
70    SkTDArray<DrawData>& array = canvas->getGrContext() ? fGPUDrawData : fThreadSafeDrawData;
71    array.append()->init(canvas, picture, matrix, paint);
72}
73
74class AutoMPDReset : SkNoncopyable {
75    SkMultiPictureDraw* fMPD;
76public:
77    AutoMPDReset(SkMultiPictureDraw* mpd) : fMPD(mpd) {}
78    ~AutoMPDReset() { fMPD->reset(); }
79};
80
81void SkMultiPictureDraw::draw() {
82    AutoMPDReset mpdreset(this);
83    // we place the taskgroup after the MPDReset, to ensure that we don't delete the DrawData
84    // objects until after we're finished the tasks (which have pointers to the data).
85
86    SkTaskGroup group;
87    group.batch(DrawData::Draw, fThreadSafeDrawData.begin(), fThreadSafeDrawData.count());
88    // we deliberately don't call wait() here, since the destructor will do that, this allows us
89    // to continue processing gpu-data without having to wait on the cpu tasks.
90
91    const int count = fGPUDrawData.count();
92    if (0 == count) {
93        return;
94    }
95
96#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
97    GrContext* context = fGPUDrawData[0].fCanvas->getGrContext();
98    SkASSERT(context);
99
100    // Start by collecting all the layers that are going to be atlased and render
101    // them (if necessary). Hoisting the free floating layers is deferred until
102    // drawing the canvas that requires them.
103    SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
104
105    for (int i = 0; i < count; ++i) {
106        const DrawData& data = fGPUDrawData[i];
107        // we only expect 1 context for all the canvases
108        SkASSERT(data.fCanvas->getGrContext() == context);
109
110        if (!data.fPaint && data.fMatrix.isIdentity()) {
111            // TODO: this path always tries to optimize pictures. Should we
112            // switch to this API approach (vs. SkCanvas::EXPERIMENTAL_optimize)?
113            data.fCanvas->EXPERIMENTAL_optimize(data.fPicture);
114
115            SkRect clipBounds;
116            if (!data.fCanvas->getClipBounds(&clipBounds)) {
117                continue;
118            }
119
120            // TODO: sorting the cacheable layers from smallest to largest
121            // would improve the packing and reduce the number of swaps
122            // TODO: another optimization would be to make a first pass to
123            // lock any required layer that is already in the atlas
124            GrLayerHoister::FindLayersToAtlas(context, data.fPicture,
125                                              clipBounds,
126                                              &atlasedNeedRendering, &atlasedRecycled);
127        }
128    }
129
130    GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering);
131
132    SkTDArray<GrHoistedLayer> needRendering, recycled;
133#endif
134
135    for (int i = 0; i < count; ++i) {
136        const DrawData& data = fGPUDrawData[i];
137        SkCanvas* canvas = data.fCanvas;
138        const SkPicture* picture = data.fPicture;
139
140#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
141        if (!data.fPaint && data.fMatrix.isIdentity()) {
142
143            SkRect clipBounds;
144            if (!canvas->getClipBounds(&clipBounds)) {
145                continue;
146            }
147
148            // Find the layers required by this canvas. It will return atlased
149            // layers in the 'recycled' list since they have already been drawn.
150            GrLayerHoister::FindLayersToHoist(context, picture,
151                                              clipBounds, &needRendering, &recycled);
152
153            GrLayerHoister::DrawLayers(context, needRendering);
154
155            GrReplacements replacements;
156
157            GrLayerHoister::ConvertLayersToReplacements(needRendering, &replacements);
158            GrLayerHoister::ConvertLayersToReplacements(recycled, &replacements);
159
160            const SkMatrix initialMatrix = canvas->getTotalMatrix();
161
162            // Render the entire picture using new layers
163            GrRecordReplaceDraw(picture, canvas, &replacements, initialMatrix, NULL);
164
165            GrLayerHoister::UnlockLayers(context, needRendering);
166            GrLayerHoister::UnlockLayers(context, recycled);
167
168            needRendering.rewind();
169            recycled.rewind();
170        } else
171#endif
172        {
173            canvas->drawPicture(picture, &data.fMatrix, data.fPaint);
174        }
175    }
176
177#if !defined(SK_IGNORE_GPU_LAYER_HOISTING) && SK_SUPPORT_GPU
178    GrLayerHoister::UnlockLayers(context, atlasedNeedRendering);
179    GrLayerHoister::UnlockLayers(context, atlasedRecycled);
180#if !GR_CACHE_HOISTED_LAYERS
181    GrLayerHoister::PurgeCache(context);
182#endif
183#endif
184}
185
186