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