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