OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 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 #include "SkLayerInfo.h" | |
9 #include "SkMatrix.h" | |
10 #include "SkPictureData.h" | |
11 #include "SkPicturePlayback.h" | |
12 #include "SkPictureRecord.h" | |
13 #include "SkPictureRecorder.h" | |
14 #include "SkPictureUtils.h" | |
15 #include "SkRecordedDrawable.h" | |
16 #include "SkRecordDraw.h" | |
17 | |
18 void SkRecordedDrawable::onDraw(SkCanvas* canvas) { | |
19 SkDrawable* const* drawables = nullptr; | |
20 int drawableCount = 0; | |
21 if (fDrawableList) { | |
22 drawables = fDrawableList->begin(); | |
23 drawableCount = fDrawableList->count(); | |
24 } | |
25 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, fBBH, null
ptr/*callback*/); | |
26 } | |
27 | |
28 SkPicture* SkRecordedDrawable::onNewPictureSnapshot() { | |
29 SkBigPicture::SnapshotArray* pictList = nullptr; | |
30 if (fDrawableList) { | |
31 // TODO: should we plumb-down the BBHFactory and recordFlags from our ho
st | |
32 // PictureRecorder? | |
33 pictList = fDrawableList->newDrawableSnapshot(); | |
34 } | |
35 | |
36 SkAutoTUnref<SkLayerInfo> saveLayerData; | |
37 if (fBBH && fDoSaveLayerInfo) { | |
38 // TODO: can we avoid work by not allocating / filling these bounds? | |
39 SkAutoTMalloc<SkRect> scratchBounds(fRecord->count()); | |
40 saveLayerData.reset(new SkLayerInfo); | |
41 | |
42 SkRecordComputeLayers(fBounds, *fRecord, scratchBounds, pictList, saveLa
yerData); | |
43 } | |
44 | |
45 size_t subPictureBytes = 0; | |
46 for (int i = 0; pictList && i < pictList->count(); i++) { | |
47 subPictureBytes += SkPictureUtils::ApproximateBytesUsed(pictList->begin(
)[i]); | |
48 } | |
49 // SkBigPicture will take ownership of a ref on both fRecord and fBBH. | |
50 // We're not willing to give up our ownership, so we must ref them for SkPic
ture. | |
51 return new SkBigPicture(fBounds, SkRef(fRecord.get()), pictList, SkSafeRef(f
BBH.get()), | |
52 saveLayerData.release(), subPictureBytes); | |
53 } | |
54 | |
55 void SkRecordedDrawable::flatten(SkWriteBuffer& buffer) const { | |
56 // Write the bounds. | |
57 buffer.writeRect(fBounds); | |
58 | |
59 // Create an SkPictureRecord to record the draw commands. | |
60 SkPictInfo info; | |
61 SkPictureRecord pictureRecord(SkISize::Make(fBounds.width(), fBounds.height(
)), 0); | |
62 | |
63 // If the query contains the whole picture, don't bother with the bounding b
ox hierarchy. | |
64 SkRect clipBounds; | |
65 pictureRecord.getClipBounds(&clipBounds); | |
66 SkBBoxHierarchy* bbh; | |
67 if (clipBounds.contains(fBounds)) { | |
68 bbh = nullptr; | |
69 } else { | |
70 bbh = fBBH.get(); | |
71 } | |
72 | |
73 // Record the draw commands. | |
74 pictureRecord.beginRecording(); | |
75 SkRecordDraw(*fRecord, &pictureRecord, nullptr, fDrawableList->begin(), fDra
wableList->count(), | |
76 bbh, nullptr); | |
77 pictureRecord.endRecording(); | |
78 | |
79 // Flatten the recorded commands and drawables. | |
80 SkPictureData pictureData(pictureRecord, info, false); | |
81 pictureData.flatten(buffer); | |
82 } | |
83 | |
84 sk_sp<SkFlattenable> SkRecordedDrawable::CreateProc(SkReadBuffer& buffer) { | |
85 // Read the bounds. | |
86 SkRect bounds; | |
87 buffer.readRect(&bounds); | |
88 | |
89 // Unflatten into a SkPictureData. | |
90 SkPictInfo info; | |
91 info.fCullRect = bounds; | |
92 SkAutoTDelete<SkPictureData> pictureData(SkPictureData::CreateFromBuffer(buf
fer, info)); | |
93 if (!pictureData) { | |
94 return nullptr; | |
95 } | |
96 | |
97 // Create a drawable. | |
98 SkPicturePlayback playback(pictureData); | |
99 SkPictureRecorder recorder; | |
100 playback.draw(recorder.beginRecording(bounds), nullptr, &buffer); | |
101 return recorder.finishRecordingAsDrawable(); | |
102 } | |
OLD | NEW |