Chromium Code Reviews| 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 #if SK_SUPPORT_GPU | 8 #if SK_SUPPORT_GPU |
| 9 #include "GrLayerHoister.h" | 9 #include "GrLayerHoister.h" |
| 10 #include "GrRecordReplaceDraw.h" | 10 #include "GrRecordReplaceDraw.h" |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
| 14 #include "SkMultiPictureDraw.h" | 14 #include "SkMultiPictureDraw.h" |
| 15 #include "SkPicture.h" | 15 #include "SkPicture.h" |
| 16 | 16 |
| 17 void SkMultiPictureDraw::DrawData::init(SkCanvas* canvas, const SkPicture* pictu re, | |
| 18 const SkMatrix* matrix, const SkPaint* p aint) { | |
| 19 fPicture = SkRef(picture); | |
| 20 fCanvas = SkRef(canvas); | |
| 21 if (matrix) { | |
| 22 fMatrix = *matrix; | |
| 23 } else { | |
| 24 fMatrix.setIdentity(); | |
| 25 } | |
| 26 if (paint) { | |
| 27 fPaint = SkNEW_ARGS(SkPaint, (*paint)); | |
| 28 } else { | |
| 29 fPaint = NULL; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void SkMultiPictureDraw::DrawData::Reset(SkTDArray<DrawData>& data) { | |
| 34 for (int i = 0; i < data.count(); ++i) { | |
| 35 data[i].fPicture->unref(); | |
| 36 data[i].fCanvas->unref(); | |
| 37 SkDELETE(data[i].fPaint); | |
| 38 } | |
| 39 data.rewind(); | |
| 40 } | |
| 41 | |
| 17 SkMultiPictureDraw::SkMultiPictureDraw(int reserve) { | 42 SkMultiPictureDraw::SkMultiPictureDraw(int reserve) { |
| 18 if (reserve > 0) { | 43 if (reserve > 0) { |
| 19 fDrawData.setReserve(reserve); | 44 fGPUDrawData.setReserve(reserve); |
| 45 fThreadSafeDrawData.setReserve(reserve); | |
| 20 } | 46 } |
| 21 } | 47 } |
| 22 | 48 |
| 23 void SkMultiPictureDraw::reset() { | 49 void SkMultiPictureDraw::reset() { |
| 24 for (int i = 0; i < fDrawData.count(); ++i) { | 50 fGPUDrawData.reset(); |
| 25 fDrawData[i].picture->unref(); | 51 fThreadSafeDrawData.reset(); |
| 26 fDrawData[i].canvas->unref(); | |
| 27 SkDELETE(fDrawData[i].paint); | |
| 28 } | |
| 29 | |
| 30 fDrawData.rewind(); | |
| 31 } | 52 } |
| 32 | 53 |
| 33 void SkMultiPictureDraw::add(SkCanvas* canvas, | 54 void SkMultiPictureDraw::add(SkCanvas* canvas, |
| 34 const SkPicture* picture, | 55 const SkPicture* picture, |
| 35 const SkMatrix* matrix, | 56 const SkMatrix* matrix, |
| 36 const SkPaint* paint) { | 57 const SkPaint* paint) { |
| 37 if (NULL == canvas || NULL == picture) { | 58 if (NULL == canvas || NULL == picture) { |
| 38 SkDEBUGFAIL("parameters to SkMultiPictureDraw::add should be non-NULL"); | 59 SkDEBUGFAIL("parameters to SkMultiPictureDraw::add should be non-NULL"); |
| 39 return; | 60 return; |
| 40 } | 61 } |
| 41 | 62 |
| 42 DrawData* data = fDrawData.append(); | 63 SkTDArray<DrawData>& array = canvas->getGrContext() ? fGPUDrawData : fThread SafeDrawData; |
| 43 | 64 array.append()->init(canvas, picture, matrix, paint); |
| 44 data->picture = SkRef(picture); | |
| 45 data->canvas = SkRef(canvas); | |
| 46 if (matrix) { | |
| 47 data->matrix = *matrix; | |
| 48 } else { | |
| 49 data->matrix.setIdentity(); | |
| 50 } | |
| 51 if (paint) { | |
| 52 data->paint = SkNEW_ARGS(SkPaint, (*paint)); | |
| 53 } else { | |
| 54 data->paint = NULL; | |
| 55 } | |
| 56 } | 65 } |
| 57 | 66 |
| 58 #undef SK_IGNORE_GPU_LAYER_HOISTING | 67 #undef SK_IGNORE_GPU_LAYER_HOISTING |
| 59 #define SK_IGNORE_GPU_LAYER_HOISTING 1 | 68 #define SK_IGNORE_GPU_LAYER_HOISTING 1 |
| 60 | 69 |
| 70 #include "SkTaskGroup.h" | |
| 71 | |
| 72 struct PictureDrawRunnable : public SkRunnable { | |
| 73 SkCanvas* fCanvas; | |
| 74 const SkPicture* fPicture; | |
| 75 const SkMatrix* fMatrix; | |
| 76 const SkPaint* fPaint; | |
| 77 | |
| 78 PictureDrawRunnable* init(SkCanvas* canvas, const SkPicture* picture, const SkMatrix* matrix, | |
| 79 const SkPaint* paint) { | |
| 80 // no need to ref/copy these, as the caller ensures they all survive our task. | |
| 81 fCanvas = canvas; | |
| 82 fPicture = picture; | |
| 83 fMatrix = matrix; | |
| 84 fPaint = paint; | |
| 85 return this; | |
| 86 } | |
| 87 | |
| 88 virtual void run() SK_OVERRIDE { | |
| 89 fCanvas->drawPicture(fPicture, fMatrix, fPaint); | |
| 90 } | |
| 91 }; | |
| 92 | |
| 93 class AutoMPDReset : SkNoncopyable { | |
| 94 SkMultiPictureDraw* fMPD; | |
| 95 public: | |
| 96 AutoMPDReset(SkMultiPictureDraw* mpd) : fMPD(mpd) {} | |
| 97 ~AutoMPDReset() { fMPD->reset(); } | |
| 98 }; | |
| 99 | |
| 61 void SkMultiPictureDraw::draw() { | 100 void SkMultiPictureDraw::draw() { |
| 101 AutoMPDReset mpdreset(this); | |
| 102 // we place the taskgroup after the MPDReset, to ensure that we don't delete the DrawData | |
| 103 // objects until after we're finished the tasks (which have pointers to the data). | |
|
mtklein
2014/10/29 14:29:03
If we're worried about scope timing, can't hurt to
reed1
2014/10/29 17:05:11
deliberately want to not force the wait until the
| |
| 104 SkTaskGroup group; | |
| 105 | |
| 106 // Queue up the non-gpu tasks first, so they can execute in parallel while w e then | |
| 107 // handle the gpu tasks. | |
| 108 if (fThreadSafeDrawData.count() > 0) { | |
|
mtklein
2014/10/29 14:29:03
My instinct is to drop these extra checks (this an
reed1
2014/10/29 17:05:11
Done.
| |
| 109 const int count = fThreadSafeDrawData.count(); | |
| 110 SkAutoSTArray<32, PictureDrawRunnable> pdr(count); | |
| 111 for (int i = 0; i < count; ++i) { | |
| 112 const DrawData& data = fThreadSafeDrawData[i]; | |
| 113 group.add(pdr[i].init(data.fCanvas, data.fPicture, &data.fMatrix, da ta.fPaint)); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 // Enter the GPU-only section | |
| 118 | |
| 119 const int count = fGPUDrawData.count(); | |
| 120 if (0 == count) { | |
| 121 return; | |
| 122 } | |
| 62 | 123 |
| 63 #ifndef SK_IGNORE_GPU_LAYER_HOISTING | 124 #ifndef SK_IGNORE_GPU_LAYER_HOISTING |
| 64 GrContext* context = NULL; | 125 GrContext* context = fGPUDrawData[0].fCanvas->getGrContext(); |
| 126 SkASSERT(context); | |
| 65 | 127 |
| 66 // Start by collecting all the layers that are going to be atlased and rende r | 128 // Start by collecting all the layers that are going to be atlased and rende r |
| 67 // them (if necessary). Hoisting the free floating layers is deferred until | 129 // them (if necessary). Hoisting the free floating layers is deferred until |
| 68 // drawing the canvas that requires them. | 130 // drawing the canvas that requires them. |
| 69 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled; | 131 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled; |
| 70 | 132 |
| 71 for (int i = 0; i < fDrawData.count(); ++i) { | 133 for (int i = 0; i < count; ++i) { |
| 72 if (fDrawData[i].canvas->getGrContext() && | 134 const DrawData& data = fGPUDrawData[i]; |
| 73 !fDrawData[i].paint && fDrawData[i].matrix.isIdentity()) { | 135 // we only expect 1 context for all the canvases |
| 74 SkASSERT(NULL == context || context == fDrawData[i].canvas->getGrCon text()); | 136 SkASSERT(data.canvas->getGrContext() == context); |
| 75 context = fDrawData[i].canvas->getGrContext(); | |
| 76 | 137 |
| 138 if (!data.fPaint && data.fMatrix.isIdentity()) { | |
| 77 // TODO: this path always tries to optimize pictures. Should we | 139 // TODO: this path always tries to optimize pictures. Should we |
| 78 // switch to this API approach (vs. SkCanvas::EXPERIMENTAL_optimize) ? | 140 // switch to this API approach (vs. SkCanvas::EXPERIMENTAL_optimize) ? |
| 79 fDrawData[i].canvas->EXPERIMENTAL_optimize(fDrawData[i].picture); | 141 data.fCanvas->EXPERIMENTAL_optimize(data.fPicture); |
| 80 | 142 |
| 81 SkRect clipBounds; | 143 SkRect clipBounds; |
| 82 if (!fDrawData[i].canvas->getClipBounds(&clipBounds)) { | 144 if (!data.fCanvas->getClipBounds(&clipBounds)) { |
| 83 continue; | 145 continue; |
| 84 } | 146 } |
| 85 | 147 |
| 86 // TODO: sorting the cacheable layers from smallest to largest | 148 // TODO: sorting the cacheable layers from smallest to largest |
| 87 // would improve the packing and reduce the number of swaps | 149 // would improve the packing and reduce the number of swaps |
| 88 // TODO: another optimization would be to make a first pass to | 150 // TODO: another optimization would be to make a first pass to |
| 89 // lock any required layer that is already in the atlas | 151 // lock any required layer that is already in the atlas |
| 90 GrLayerHoister::FindLayersToAtlas(context, fDrawData[i].picture, | 152 GrLayerHoister::FindLayersToAtlas(context, data.fPicture, |
| 91 clipBounds, | 153 clipBounds, |
| 92 &atlasedNeedRendering, &atlasedRec ycled); | 154 &atlasedNeedRendering, &atlasedRec ycled); |
| 93 } | 155 } |
| 94 } | 156 } |
| 95 | 157 |
| 96 if (NULL != context) { | 158 GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering); |
| 97 GrLayerHoister::DrawLayersToAtlas(context, atlasedNeedRendering); | |
| 98 } | |
| 99 | 159 |
| 100 SkTDArray<GrHoistedLayer> needRendering, recycled; | 160 SkTDArray<GrHoistedLayer> needRendering, recycled; |
| 101 #endif | 161 #endif |
| 102 | 162 |
| 103 for (int i = 0; i < fDrawData.count(); ++i) { | 163 for (int i = 0; i < count; ++i) { |
| 164 const DrawData& data = fGPUDrawData[i]; | |
| 165 SkCanvas* canvas = data.fCanvas; | |
| 166 const SkPicture* picture = data.fPicture; | |
| 167 | |
| 104 #ifndef SK_IGNORE_GPU_LAYER_HOISTING | 168 #ifndef SK_IGNORE_GPU_LAYER_HOISTING |
| 105 if (fDrawData[i].canvas->getGrContext() && | 169 if (!data.fPaint && data.fMatrix.isIdentity()) { |
| 106 !fDrawData[i].paint && fDrawData[i].matrix.isIdentity()) { | |
| 107 | 170 |
| 108 SkRect clipBounds; | 171 SkRect clipBounds; |
| 109 if (!fDrawData[i].canvas->getClipBounds(&clipBounds)) { | 172 if (!canvas->getClipBounds(&clipBounds)) { |
| 110 continue; | 173 continue; |
| 111 } | 174 } |
| 112 | 175 |
| 113 // Find the layers required by this canvas. It will return atlased | 176 // Find the layers required by this canvas. It will return atlased |
| 114 // layers in the 'recycled' list since they have already been drawn. | 177 // layers in the 'recycled' list since they have already been drawn. |
| 115 GrLayerHoister::FindLayersToHoist(context, fDrawData[i].picture, | 178 GrLayerHoister::FindLayersToHoist(context, picture, |
| 116 clipBounds, &needRendering, &recyc led); | 179 clipBounds, &needRendering, &recyc led); |
| 117 | 180 |
| 118 GrLayerHoister::DrawLayers(context, needRendering); | 181 GrLayerHoister::DrawLayers(context, needRendering); |
| 119 | 182 |
| 120 GrReplacements replacements; | 183 GrReplacements replacements; |
| 121 | 184 |
| 122 GrLayerHoister::ConvertLayersToReplacements(needRendering, &replacem ents); | 185 GrLayerHoister::ConvertLayersToReplacements(needRendering, &replacem ents); |
| 123 GrLayerHoister::ConvertLayersToReplacements(recycled, &replacements) ; | 186 GrLayerHoister::ConvertLayersToReplacements(recycled, &replacements) ; |
| 124 | 187 |
| 125 const SkMatrix initialMatrix = fDrawData[i].canvas->getTotalMatrix() ; | 188 const SkMatrix initialMatrix = canvas->getTotalMatrix(); |
| 126 | 189 |
| 127 // Render the entire picture using new layers | 190 // Render the entire picture using new layers |
| 128 GrRecordReplaceDraw(fDrawData[i].picture, fDrawData[i].canvas, | 191 GrRecordReplaceDraw(picture, canvas, &replacements, initialMatrix, N ULL); |
| 129 &replacements, initialMatrix, NULL); | |
| 130 | 192 |
| 131 GrLayerHoister::UnlockLayers(context, needRendering); | 193 GrLayerHoister::UnlockLayers(context, needRendering); |
| 132 GrLayerHoister::UnlockLayers(context, recycled); | 194 GrLayerHoister::UnlockLayers(context, recycled); |
| 133 | 195 |
| 134 needRendering.rewind(); | 196 needRendering.rewind(); |
| 135 recycled.rewind(); | 197 recycled.rewind(); |
| 136 } else | 198 } else |
| 137 #endif | 199 #endif |
| 138 { | 200 { |
| 139 fDrawData[i].canvas->drawPicture(fDrawData[i].picture, | 201 canvas->drawPicture(picture, &data.fMatrix, data.fPaint); |
| 140 &fDrawData[i].matrix, | |
| 141 fDrawData[i].paint); | |
| 142 } | 202 } |
| 143 } | 203 } |
| 144 | 204 |
| 145 #ifndef SK_IGNORE_GPU_LAYER_HOISTING | 205 #ifndef SK_IGNORE_GPU_LAYER_HOISTING |
| 146 if (NULL != context) { | 206 GrLayerHoister::UnlockLayers(context, atlasedNeedRendering); |
| 147 GrLayerHoister::UnlockLayers(context, atlasedNeedRendering); | 207 GrLayerHoister::UnlockLayers(context, atlasedRecycled); |
| 148 GrLayerHoister::UnlockLayers(context, atlasedRecycled); | |
| 149 } | |
| 150 #endif | 208 #endif |
| 151 | |
| 152 this->reset(); | |
| 153 } | 209 } |
| 154 | 210 |
| OLD | NEW |