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" |
17 #include "SkRecorder.h" | 18 #include "SkRecorder.h" |
18 #include "SkTypes.h" | 19 #include "SkTypes.h" |
19 | 20 |
20 SkPictureRecorder::SkPictureRecorder() { | 21 SkPictureRecorder::SkPictureRecorder() { |
21 fActivelyRecording = false; | 22 fActivelyRecording = false; |
22 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder
)); | 23 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder
)); |
23 } | 24 } |
24 | 25 |
25 SkPictureRecorder::~SkPictureRecorder() {} | 26 SkPictureRecorder::~SkPictureRecorder() {} |
26 | 27 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 int drawableCount = 0; | 111 int drawableCount = 0; |
111 SkDrawable* const* drawables = nullptr; | 112 SkDrawable* const* drawables = nullptr; |
112 SkDrawableList* drawableList = fRecorder->getDrawableList(); | 113 SkDrawableList* drawableList = fRecorder->getDrawableList(); |
113 if (drawableList) { | 114 if (drawableList) { |
114 drawableCount = drawableList->count(); | 115 drawableCount = drawableList->count(); |
115 drawables = drawableList->begin(); | 116 drawables = drawableList->begin(); |
116 } | 117 } |
117 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b
bh*/, nullptr/*callback*/); | 118 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b
bh*/, nullptr/*callback*/); |
118 } | 119 } |
119 | 120 |
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 | |
180 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable() { | 121 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable() { |
181 fActivelyRecording = false; | 122 fActivelyRecording = false; |
182 fRecorder->flushMiniRecorder(); | 123 fRecorder->flushMiniRecorder(); |
183 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. | 124 fRecorder->restoreToCount(1); // If we were missing any restores, add them
now. |
184 | 125 |
185 // TODO: delay as much of this work until just before first playback? | 126 // TODO: delay as much of this work until just before first playback? |
186 SkRecordOptimize(fRecord); | 127 SkRecordOptimize(fRecord); |
187 | 128 |
188 if (fBBH.get()) { | 129 if (fBBH.get()) { |
189 SkAutoTMalloc<SkRect> bounds(fRecord->count()); | 130 SkAutoTMalloc<SkRect> bounds(fRecord->count()); |
190 SkRecordFillBounds(fCullRect, *fRecord, bounds); | 131 SkRecordFillBounds(fCullRect, *fRecord, bounds); |
191 fBBH->insert(bounds, fRecord->count()); | 132 fBBH->insert(bounds, fRecord->count()); |
192 } | 133 } |
193 | 134 |
194 sk_sp<SkDrawable> drawable = | 135 sk_sp<SkDrawable> drawable = |
195 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawab
leList(), fCullRect, | 136 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawab
leList(), fCullRect, |
196 SkToBool(fFlags & kComputeSaveLayerInfo_Recor
dFlag)); | 137 SkToBool(fFlags & kComputeSaveLayerInfo_Recor
dFlag)); |
197 | 138 |
198 // release our refs now, so only the drawable will be the owner. | 139 // release our refs now, so only the drawable will be the owner. |
199 fRecord.reset(nullptr); | 140 fRecord.reset(nullptr); |
200 fBBH.reset(nullptr); | 141 fBBH.reset(nullptr); |
201 | 142 |
202 return drawable; | 143 return drawable; |
203 } | 144 } |
OLD | NEW |