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 "SkBigPicture.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkDrawable.h" | 10 #include "SkDrawable.h" |
11 #include "SkLayerInfo.h" | 11 #include "SkLayerInfo.h" |
12 #include "SkPictureRecorder.h" | 12 #include "SkPictureRecorder.h" |
13 #include "SkPictureUtils.h" | 13 #include "SkPictureUtils.h" |
14 #include "SkRecord.h" | 14 #include "SkRecord.h" |
15 #include "SkRecordDraw.h" | 15 #include "SkRecordDraw.h" |
16 #include "SkRecordOpts.h" | 16 #include "SkRecordOpts.h" |
17 #include "SkRecordedDrawable.h" | |
18 #include "SkRecorder.h" | 17 #include "SkRecorder.h" |
19 #include "SkTypes.h" | 18 #include "SkTypes.h" |
20 | 19 |
21 SkPictureRecorder::SkPictureRecorder() { | 20 SkPictureRecorder::SkPictureRecorder() { |
22 fActivelyRecording = false; | 21 fActivelyRecording = false; |
23 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder
)); | 22 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder
)); |
24 } | 23 } |
25 | 24 |
26 SkPictureRecorder::~SkPictureRecorder() {} | 25 SkPictureRecorder::~SkPictureRecorder() {} |
27 | 26 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 int drawableCount = 0; | 110 int drawableCount = 0; |
112 SkDrawable* const* drawables = nullptr; | 111 SkDrawable* const* drawables = nullptr; |
113 SkDrawableList* drawableList = fRecorder->getDrawableList(); | 112 SkDrawableList* drawableList = fRecorder->getDrawableList(); |
114 if (drawableList) { | 113 if (drawableList) { |
115 drawableCount = drawableList->count(); | 114 drawableCount = drawableList->count(); |
116 drawables = drawableList->begin(); | 115 drawables = drawableList->begin(); |
117 } | 116 } |
118 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b
bh*/, nullptr/*callback*/); | 117 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b
bh*/, nullptr/*callback*/); |
119 } | 118 } |
120 | 119 |
| 120 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 121 |
| 122 class SkRecordedDrawable : public SkDrawable { |
| 123 SkAutoTUnref<SkRecord> fRecord; |
| 124 SkAutoTUnref<SkBBoxHierarchy> fBBH; |
| 125 SkAutoTDelete<SkDrawableList> fDrawableList; |
| 126 const SkRect fBounds; |
| 127 const bool fDoSaveLayerInfo; |
| 128 |
| 129 public: |
| 130 SkRecordedDrawable(SkRecord* record, SkBBoxHierarchy* bbh, SkDrawableList* d
rawableList, |
| 131 const SkRect& bounds, bool doSaveLayerInfo) |
| 132 : fRecord(SkRef(record)) |
| 133 , fBBH(SkSafeRef(bbh)) |
| 134 , fDrawableList(drawableList) // we take ownership |
| 135 , fBounds(bounds) |
| 136 , fDoSaveLayerInfo(doSaveLayerInfo) |
| 137 {} |
| 138 |
| 139 protected: |
| 140 SkRect onGetBounds() override { return fBounds; } |
| 141 |
| 142 void onDraw(SkCanvas* canvas) override { |
| 143 SkDrawable* const* drawables = nullptr; |
| 144 int drawableCount = 0; |
| 145 if (fDrawableList) { |
| 146 drawables = fDrawableList->begin(); |
| 147 drawableCount = fDrawableList->count(); |
| 148 } |
| 149 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, fBBH,
nullptr/*callback*/); |
| 150 } |
| 151 |
| 152 SkPicture* onNewPictureSnapshot() override { |
| 153 SkBigPicture::SnapshotArray* pictList = nullptr; |
| 154 if (fDrawableList) { |
| 155 // TODO: should we plumb-down the BBHFactory and recordFlags from ou
r host |
| 156 // PictureRecorder? |
| 157 pictList = fDrawableList->newDrawableSnapshot(); |
| 158 } |
| 159 |
| 160 SkAutoTUnref<SkLayerInfo> saveLayerData; |
| 161 if (fBBH && fDoSaveLayerInfo) { |
| 162 // TODO: can we avoid work by not allocating / filling these bounds? |
| 163 SkAutoTMalloc<SkRect> scratchBounds(fRecord->count()); |
| 164 saveLayerData.reset(new SkLayerInfo); |
| 165 |
| 166 SkRecordComputeLayers(fBounds, *fRecord, scratchBounds, pictList, sa
veLayerData); |
| 167 } |
| 168 |
| 169 size_t subPictureBytes = 0; |
| 170 for (int i = 0; pictList && i < pictList->count(); i++) { |
| 171 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->be
gin()[i]); |
| 172 } |
| 173 // SkBigPicture will take ownership of a ref on both fRecord and fBBH. |
| 174 // We're not willing to give up our ownership, so we must ref them for S
kPicture. |
| 175 return new SkBigPicture(fBounds, SkRef(fRecord.get()), pictList, SkSafeR
ef(fBBH.get()), |
| 176 saveLayerData.release(), subPictureBytes); |
| 177 } |
| 178 }; |
| 179 |
121 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable() { | 180 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable() { |
122 fActivelyRecording = false; | 181 fActivelyRecording = false; |
123 fRecorder->flushMiniRecorder(); | 182 fRecorder->flushMiniRecorder(); |
124 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. | 183 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. |
125 | 184 |
126 // TODO: delay as much of this work until just before first playback? | 185 // TODO: delay as much of this work until just before first playback? |
127 SkRecordOptimize(fRecord); | 186 SkRecordOptimize(fRecord); |
128 | 187 |
129 if (fBBH.get()) { | 188 if (fBBH.get()) { |
130 SkAutoTMalloc<SkRect> bounds(fRecord->count()); | 189 SkAutoTMalloc<SkRect> bounds(fRecord->count()); |
131 SkRecordFillBounds(fCullRect, *fRecord, bounds); | 190 SkRecordFillBounds(fCullRect, *fRecord, bounds); |
132 fBBH->insert(bounds, fRecord->count()); | 191 fBBH->insert(bounds, fRecord->count()); |
133 } | 192 } |
134 | 193 |
135 sk_sp<SkDrawable> drawable = | 194 sk_sp<SkDrawable> drawable = |
136 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawab
leList(), fCullRect, | 195 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawab
leList(), fCullRect, |
137 SkToBool(fFlags & kComputeSaveLayerInfo_Recor
dFlag)); | 196 SkToBool(fFlags & kComputeSaveLayerInfo_Recor
dFlag)); |
138 | 197 |
139 // release our refs now, so only the drawable will be the owner. | 198 // release our refs now, so only the drawable will be the owner. |
140 fRecord.reset(nullptr); | 199 fRecord.reset(nullptr); |
141 fBBH.reset(nullptr); | 200 fBBH.reset(nullptr); |
142 | 201 |
143 return drawable; | 202 return drawable; |
144 } | 203 } |
OLD | NEW |