| 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 #include "SkBigPicture.h" |
| 8 #include "SkData.h" | 9 #include "SkData.h" |
| 9 #include "SkDrawable.h" | 10 #include "SkDrawable.h" |
| 10 #include "SkLayerInfo.h" | 11 #include "SkLayerInfo.h" |
| 11 #include "SkPictureRecorder.h" | 12 #include "SkPictureRecorder.h" |
| 12 #include "SkPictureUtils.h" | 13 #include "SkPictureUtils.h" |
| 13 #include "SkRecord.h" | 14 #include "SkRecord.h" |
| 14 #include "SkRecordDraw.h" | 15 #include "SkRecordDraw.h" |
| 15 #include "SkRecordOpts.h" | 16 #include "SkRecordOpts.h" |
| 16 #include "SkRecorder.h" | 17 #include "SkRecorder.h" |
| 17 #include "SkTypes.h" | 18 #include "SkTypes.h" |
| 18 | 19 |
| 19 SkPictureRecorder::SkPictureRecorder() { | 20 SkPictureRecorder::SkPictureRecorder() { |
| 20 fActivelyRecording = false; | 21 fActivelyRecording = false; |
| 21 fRecorder.reset(SkNEW_ARGS(SkRecorder, (nullptr, SkRect::MakeWH(0,0)))); | 22 fRecorder.reset(SkNEW_ARGS(SkRecorder, (nullptr, SkRect::MakeWH(0,0), &fMini
Recorder))); |
| 22 } | 23 } |
| 23 | 24 |
| 24 SkPictureRecorder::~SkPictureRecorder() {} | 25 SkPictureRecorder::~SkPictureRecorder() {} |
| 25 | 26 |
| 26 SkCanvas* SkPictureRecorder::beginRecording(const SkRect& cullRect, | 27 SkCanvas* SkPictureRecorder::beginRecording(const SkRect& cullRect, |
| 27 SkBBHFactory* bbhFactory /* = NULL *
/, | 28 SkBBHFactory* bbhFactory /* = NULL *
/, |
| 28 uint32_t recordFlags /* = 0 */) { | 29 uint32_t recordFlags /* = 0 */) { |
| 29 fCullRect = cullRect; | 30 fCullRect = cullRect; |
| 30 fFlags = recordFlags; | 31 fFlags = recordFlags; |
| 31 | 32 |
| 32 if (bbhFactory) { | 33 if (bbhFactory) { |
| 33 fBBH.reset((*bbhFactory)(cullRect)); | 34 fBBH.reset((*bbhFactory)(cullRect)); |
| 34 SkASSERT(fBBH.get()); | 35 SkASSERT(fBBH.get()); |
| 35 } | 36 } |
| 36 | 37 |
| 37 fRecord.reset(SkNEW(SkRecord)); | 38 if (!fRecord) { |
| 38 fRecorder->reset(fRecord.get(), cullRect); | 39 fRecord.reset(SkNEW(SkRecord)); |
| 40 } |
| 41 fRecorder->reset(fRecord.get(), cullRect, &fMiniRecorder); |
| 39 fActivelyRecording = true; | 42 fActivelyRecording = true; |
| 40 return this->getRecordingCanvas(); | 43 return this->getRecordingCanvas(); |
| 41 } | 44 } |
| 42 | 45 |
| 43 SkCanvas* SkPictureRecorder::getRecordingCanvas() { | 46 SkCanvas* SkPictureRecorder::getRecordingCanvas() { |
| 44 return fActivelyRecording ? fRecorder.get() : nullptr; | 47 return fActivelyRecording ? fRecorder.get() : nullptr; |
| 45 } | 48 } |
| 46 | 49 |
| 47 SkPicture* SkPictureRecorder::endRecordingAsPicture() { | 50 SkPicture* SkPictureRecorder::endRecordingAsPicture() { |
| 48 fActivelyRecording = false; | 51 fActivelyRecording = false; |
| 49 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. | 52 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. |
| 53 |
| 54 if (fRecord->count() == 0) { |
| 55 return fMiniRecorder.detachAsPicture(fCullRect); |
| 56 } |
| 57 |
| 50 // TODO: delay as much of this work until just before first playback? | 58 // TODO: delay as much of this work until just before first playback? |
| 51 SkRecordOptimize(fRecord); | 59 SkRecordOptimize(fRecord); |
| 52 | 60 |
| 53 SkAutoTUnref<SkLayerInfo> saveLayerData; | 61 SkAutoTUnref<SkLayerInfo> saveLayerData; |
| 54 | 62 |
| 55 if (fBBH && (fFlags & kComputeSaveLayerInfo_RecordFlag)) { | 63 if (fBBH && (fFlags & kComputeSaveLayerInfo_RecordFlag)) { |
| 56 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey(); | 64 saveLayerData.reset(SkNEW(SkLayerInfo)); |
| 57 | |
| 58 saveLayerData.reset(SkNEW_ARGS(SkLayerInfo, (key))); | |
| 59 } | 65 } |
| 60 | 66 |
| 61 SkDrawableList* drawableList = fRecorder->getDrawableList(); | 67 SkDrawableList* drawableList = fRecorder->getDrawableList(); |
| 62 SkPicture::SnapshotArray* pictList = drawableList ? drawableList->newDrawabl
eSnapshot() : NULL; | 68 SkBigPicture::SnapshotArray* pictList = |
| 69 drawableList ? drawableList->newDrawableSnapshot() : NULL; |
| 63 | 70 |
| 64 if (fBBH.get()) { | 71 if (fBBH.get()) { |
| 65 if (saveLayerData) { | 72 if (saveLayerData) { |
| 66 SkRecordComputeLayers(fCullRect, *fRecord, pictList, fBBH.get(), sav
eLayerData); | 73 SkRecordComputeLayers(fCullRect, *fRecord, pictList, fBBH.get(), sav
eLayerData); |
| 67 } else { | 74 } else { |
| 68 SkRecordFillBounds(fCullRect, *fRecord, fBBH.get()); | 75 SkRecordFillBounds(fCullRect, *fRecord, fBBH.get()); |
| 69 } | 76 } |
| 70 SkRect bbhBound = fBBH->getRootBound(); | 77 SkRect bbhBound = fBBH->getRootBound(); |
| 71 SkASSERT((bbhBound.isEmpty() || fCullRect.contains(bbhBound)) | 78 SkASSERT((bbhBound.isEmpty() || fCullRect.contains(bbhBound)) |
| 72 || (bbhBound.isEmpty() && fCullRect.isEmpty())); | 79 || (bbhBound.isEmpty() && fCullRect.isEmpty())); |
| 73 fCullRect = bbhBound; | 80 fCullRect = bbhBound; |
| 74 } | 81 } |
| 75 | 82 |
| 76 size_t subPictureBytes = fRecorder->approxBytesUsedBySubPictures(); | 83 size_t subPictureBytes = fRecorder->approxBytesUsedBySubPictures(); |
| 77 for (int i = 0; pictList && i < pictList->count(); i++) { | 84 for (int i = 0; pictList && i < pictList->count(); i++) { |
| 78 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->begin(
)[i]); | 85 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->begin(
)[i]); |
| 79 } | 86 } |
| 80 return SkNEW_ARGS(SkPicture, (fCullRect, | 87 return SkNEW_ARGS(SkBigPicture, (fCullRect, |
| 81 fRecord.detach(), | 88 fRecord.detach(), |
| 82 pictList, | 89 pictList, |
| 83 fBBH.detach(), | 90 fBBH.detach(), |
| 84 saveLayerData.detach(), | 91 saveLayerData.detach(), |
| 85 subPictureBytes)); | 92 subPictureBytes)); |
| 86 } | 93 } |
| 87 | 94 |
| 88 void SkPictureRecorder::partialReplay(SkCanvas* canvas) const { | 95 void SkPictureRecorder::partialReplay(SkCanvas* canvas) const { |
| 89 if (NULL == canvas) { | 96 if (NULL == canvas) { |
| 90 return; | 97 return; |
| 91 } | 98 } |
| 92 | 99 |
| 93 int drawableCount = 0; | 100 int drawableCount = 0; |
| 94 SkDrawable* const* drawables = NULL; | 101 SkDrawable* const* drawables = NULL; |
| 95 SkDrawableList* drawableList = fRecorder->getDrawableList(); | 102 SkDrawableList* drawableList = fRecorder->getDrawableList(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 126 SkDrawable* const* drawables = NULL; | 133 SkDrawable* const* drawables = NULL; |
| 127 int drawableCount = 0; | 134 int drawableCount = 0; |
| 128 if (fDrawableList) { | 135 if (fDrawableList) { |
| 129 drawables = fDrawableList->begin(); | 136 drawables = fDrawableList->begin(); |
| 130 drawableCount = fDrawableList->count(); | 137 drawableCount = fDrawableList->count(); |
| 131 } | 138 } |
| 132 SkRecordDraw(*fRecord, canvas, NULL, drawables, drawableCount, fBBH, NUL
L/*callback*/); | 139 SkRecordDraw(*fRecord, canvas, NULL, drawables, drawableCount, fBBH, NUL
L/*callback*/); |
| 133 } | 140 } |
| 134 | 141 |
| 135 SkPicture* onNewPictureSnapshot() override { | 142 SkPicture* onNewPictureSnapshot() override { |
| 136 SkPicture::SnapshotArray* pictList = NULL; | 143 SkBigPicture::SnapshotArray* pictList = NULL; |
| 137 if (fDrawableList) { | 144 if (fDrawableList) { |
| 138 // TODO: should we plumb-down the BBHFactory and recordFlags from ou
r host | 145 // TODO: should we plumb-down the BBHFactory and recordFlags from ou
r host |
| 139 // PictureRecorder? | 146 // PictureRecorder? |
| 140 pictList = fDrawableList->newDrawableSnapshot(); | 147 pictList = fDrawableList->newDrawableSnapshot(); |
| 141 } | 148 } |
| 142 | 149 |
| 143 SkAutoTUnref<SkLayerInfo> saveLayerData; | 150 SkAutoTUnref<SkLayerInfo> saveLayerData; |
| 144 | 151 |
| 145 if (fBBH && fDoSaveLayerInfo) { | 152 if (fBBH && fDoSaveLayerInfo) { |
| 146 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey(); | 153 saveLayerData.reset(SkNEW(SkLayerInfo)); |
| 147 | |
| 148 saveLayerData.reset(SkNEW_ARGS(SkLayerInfo, (key))); | |
| 149 | 154 |
| 150 SkBBoxHierarchy* bbh = NULL; // we've already computed fBBH (rece
ived in constructor) | 155 SkBBoxHierarchy* bbh = NULL; // we've already computed fBBH (rece
ived in constructor) |
| 151 // TODO: update saveLayer info computation to reuse the already comp
uted | 156 // TODO: update saveLayer info computation to reuse the already comp
uted |
| 152 // bounds in 'fBBH' | 157 // bounds in 'fBBH' |
| 153 SkRecordComputeLayers(fBounds, *fRecord, pictList, bbh, saveLayerDat
a); | 158 SkRecordComputeLayers(fBounds, *fRecord, pictList, bbh, saveLayerDat
a); |
| 154 } | 159 } |
| 155 | 160 |
| 156 size_t subPictureBytes = 0; | 161 size_t subPictureBytes = 0; |
| 157 for (int i = 0; pictList && i < pictList->count(); i++) { | 162 for (int i = 0; pictList && i < pictList->count(); i++) { |
| 158 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->be
gin()[i]); | 163 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->be
gin()[i]); |
| 159 } | 164 } |
| 160 // SkPicture will take ownership of a ref on both fRecord and fBBH. | 165 // SkBigPicture will take ownership of a ref on both fRecord and fBBH. |
| 161 // We're not willing to give up our ownership, so we must ref them for S
kPicture. | 166 // We're not willing to give up our ownership, so we must ref them for S
kPicture. |
| 162 return SkNEW_ARGS(SkPicture, (fBounds, | 167 return SkNEW_ARGS(SkBigPicture, (fBounds, |
| 163 SkRef(fRecord.get()), | 168 SkRef(fRecord.get()), |
| 164 pictList, | 169 pictList, |
| 165 SkSafeRef(fBBH.get()), | 170 SkSafeRef(fBBH.get()), |
| 166 saveLayerData.detach(), | 171 saveLayerData.detach(), |
| 167 subPictureBytes)); | 172 subPictureBytes)); |
| 168 } | 173 } |
| 169 }; | 174 }; |
| 170 | 175 |
| 171 SkDrawable* SkPictureRecorder::endRecordingAsDrawable() { | 176 SkDrawable* SkPictureRecorder::endRecordingAsDrawable() { |
| 172 fActivelyRecording = false; | 177 fActivelyRecording = false; |
| 178 fRecorder->flushMiniRecorder(); |
| 173 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. | 179 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. |
| 180 |
| 174 // TODO: delay as much of this work until just before first playback? | 181 // TODO: delay as much of this work until just before first playback? |
| 175 SkRecordOptimize(fRecord); | 182 SkRecordOptimize(fRecord); |
| 176 | 183 |
| 177 if (fBBH.get()) { | 184 if (fBBH.get()) { |
| 178 SkRecordFillBounds(fCullRect, *fRecord, fBBH.get()); | 185 SkRecordFillBounds(fCullRect, *fRecord, fBBH.get()); |
| 179 } | 186 } |
| 180 | 187 |
| 181 SkDrawable* drawable = SkNEW_ARGS(SkRecordedDrawable, | 188 SkDrawable* drawable = SkNEW_ARGS(SkRecordedDrawable, |
| 182 (fRecord, fBBH, fRecorder->detachDrawabl
eList(), | 189 (fRecord, fBBH, fRecorder->detachDrawabl
eList(), |
| 183 fCullRect, | 190 fCullRect, |
| 184 SkToBool(fFlags & kComputeSaveLayerInfo
_RecordFlag))); | 191 SkToBool(fFlags & kComputeSaveLayerInfo
_RecordFlag))); |
| 185 | 192 |
| 186 // release our refs now, so only the drawable will be the owner. | 193 // release our refs now, so only the drawable will be the owner. |
| 187 fRecord.reset(NULL); | 194 fRecord.reset(NULL); |
| 188 fBBH.reset(NULL); | 195 fBBH.reset(NULL); |
| 189 | 196 |
| 190 return drawable; | 197 return drawable; |
| 191 } | 198 } |
| OLD | NEW |