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