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 "include/core/SkMatrix.h"
9 #include "include/core/SkPictureRecorder.h"
10 #include "src/core/SkPictureData.h"
11 #include "src/core/SkPicturePlayback.h"
12 #include "src/core/SkPictureRecord.h"
13 #include "src/core/SkRecordDraw.h"
14 #include "src/core/SkRecordedDrawable.h"
15
onApproximateBytesUsed()16 size_t SkRecordedDrawable::onApproximateBytesUsed() {
17 size_t drawablesSize = 0;
18 if (fDrawableList) {
19 for (auto&& drawable : *fDrawableList) {
20 drawablesSize += drawable->approximateBytesUsed();
21 }
22 }
23 return sizeof(*this) +
24 (fRecord ? fRecord->bytesUsed() : 0) +
25 (fBBH ? fBBH->bytesUsed() : 0) +
26 drawablesSize;
27 }
28
onDraw(SkCanvas * canvas)29 void SkRecordedDrawable::onDraw(SkCanvas* canvas) {
30 SkDrawable* const* drawables = nullptr;
31 int drawableCount = 0;
32 if (fDrawableList) {
33 drawables = fDrawableList->begin();
34 drawableCount = fDrawableList->count();
35 }
36 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, fBBH.get(), nullptr);
37 }
38
onNewPictureSnapshot()39 SkPicture* SkRecordedDrawable::onNewPictureSnapshot() {
40 // TODO: should we plumb-down the BBHFactory and recordFlags from our host
41 // PictureRecorder?
42 std::unique_ptr<SkBigPicture::SnapshotArray> pictList{
43 fDrawableList ? fDrawableList->newDrawableSnapshot() : nullptr
44 };
45
46 size_t subPictureBytes = 0;
47 for (int i = 0; pictList && i < pictList->count(); i++) {
48 subPictureBytes += pictList->begin()[i]->approximateBytesUsed();
49 }
50 return new SkBigPicture(fBounds, fRecord, std::move(pictList), fBBH, subPictureBytes);
51 }
52
flatten(SkWriteBuffer & buffer) const53 void SkRecordedDrawable::flatten(SkWriteBuffer& buffer) const {
54 // Write the bounds.
55 buffer.writeRect(fBounds);
56
57 // Create an SkPictureRecord to record the draw commands.
58 SkPictInfo info;
59 SkPictureRecord pictureRecord(SkISize::Make(fBounds.width(), fBounds.height()), 0);
60
61 // If the query contains the whole picture, don't bother with the bounding box hierarchy.
62 SkBBoxHierarchy* bbh;
63 if (pictureRecord.getLocalClipBounds().contains(fBounds)) {
64 bbh = nullptr;
65 } else {
66 bbh = fBBH.get();
67 }
68
69 // Record the draw commands.
70 pictureRecord.beginRecording();
71 SkRecordDraw(*fRecord, &pictureRecord, nullptr, fDrawableList->begin(), fDrawableList->count(),
72 bbh, nullptr);
73 pictureRecord.endRecording();
74
75 // Flatten the recorded commands and drawables.
76 SkPictureData pictureData(pictureRecord, info);
77 pictureData.flatten(buffer);
78 }
79
CreateProc(SkReadBuffer & buffer)80 sk_sp<SkFlattenable> SkRecordedDrawable::CreateProc(SkReadBuffer& buffer) {
81 // Read the bounds.
82 SkRect bounds;
83 buffer.readRect(&bounds);
84
85 // Unflatten into a SkPictureData.
86 SkPictInfo info;
87 info.setVersion(buffer.getVersion());
88 info.fCullRect = bounds;
89 std::unique_ptr<SkPictureData> pictureData(SkPictureData::CreateFromBuffer(buffer, info));
90 if (!pictureData) {
91 return nullptr;
92 }
93
94 // Create a drawable.
95 SkPicturePlayback playback(pictureData.get());
96 SkPictureRecorder recorder;
97 playback.draw(recorder.beginRecording(bounds), nullptr, &buffer);
98 return recorder.finishRecordingAsDrawable();
99 }
100