OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/playback/drawing_display_item.h" | 5 #include "cc/playback/drawing_display_item.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/trace_event/trace_event_argument.h" | 10 #include "base/trace_event/trace_event_argument.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "cc/debug/picture_debug_util.h" | 12 #include "cc/debug/picture_debug_util.h" |
13 #include "cc/proto/display_item.pb.h" | 13 #include "cc/proto/display_item.pb.h" |
14 #include "third_party/skia/include/core/SkCanvas.h" | 14 #include "third_party/skia/include/core/SkCanvas.h" |
15 #include "third_party/skia/include/core/SkData.h" | 15 #include "third_party/skia/include/core/SkData.h" |
16 #include "third_party/skia/include/core/SkMatrix.h" | 16 #include "third_party/skia/include/core/SkMatrix.h" |
17 #include "third_party/skia/include/core/SkPicture.h" | 17 #include "third_party/skia/include/core/SkPicture.h" |
18 #include "third_party/skia/include/core/SkStream.h" | 18 #include "third_party/skia/include/core/SkStream.h" |
19 #include "third_party/skia/include/utils/SkPictureUtils.h" | 19 #include "third_party/skia/include/utils/SkPictureUtils.h" |
20 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
21 | 21 |
22 namespace cc { | 22 namespace cc { |
23 | 23 |
24 DrawingDisplayItem::DrawingDisplayItem() {} | 24 DrawingDisplayItem::DrawingDisplayItem() { |
25 | |
26 DrawingDisplayItem::DrawingDisplayItem(skia::RefPtr<SkPicture> picture) { | |
27 SetNew(std::move(picture)); | |
28 } | |
29 | |
30 DrawingDisplayItem::DrawingDisplayItem(const proto::DisplayItem& proto) { | |
31 DCHECK_EQ(proto::DisplayItem::Type_Drawing, proto.type()); | |
32 | |
33 skia::RefPtr<SkPicture> picture; | |
34 const proto::DrawingDisplayItem& details = proto.drawing_item(); | |
35 if (details.has_picture()) { | |
36 SkMemoryStream stream(details.picture().data(), details.picture().size()); | |
37 | |
38 // TODO(dtrainor, nyquist): Add an image decoder. | |
39 picture = skia::AdoptRef(SkPicture::CreateFromStream(&stream, nullptr)); | |
40 } | |
41 | |
42 SetNew(std::move(picture)); | |
43 } | |
44 | |
45 DrawingDisplayItem::DrawingDisplayItem(const DrawingDisplayItem& item) { | |
46 item.CloneTo(this); | |
47 } | 25 } |
48 | 26 |
49 DrawingDisplayItem::~DrawingDisplayItem() { | 27 DrawingDisplayItem::~DrawingDisplayItem() { |
50 } | 28 } |
51 | 29 |
52 void DrawingDisplayItem::SetNew(skia::RefPtr<SkPicture> picture) { | 30 void DrawingDisplayItem::SetNew(skia::RefPtr<SkPicture> picture) { |
53 picture_ = std::move(picture); | 31 picture_ = std::move(picture); |
| 32 DisplayItem::SetNew(picture_->suitableForGpuRasterization(NULL), |
| 33 picture_->approximateOpCount(), |
| 34 SkPictureUtils::ApproximateBytesUsed(picture_.get())); |
54 } | 35 } |
55 | 36 |
56 void DrawingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { | 37 void DrawingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { |
57 proto->set_type(proto::DisplayItem::Type_Drawing); | 38 proto->set_type(proto::DisplayItem::Type_Drawing); |
58 | 39 |
59 proto::DrawingDisplayItem* details = proto->mutable_drawing_item(); | 40 proto::DrawingDisplayItem* details = proto->mutable_drawing_item(); |
60 | 41 |
61 // Just use skia's serialize() method for now. | 42 // Just use skia's serialize() method for now. |
62 if (picture_) { | 43 if (picture_) { |
63 SkDynamicMemoryWStream stream; | 44 SkDynamicMemoryWStream stream; |
64 | 45 |
65 // TODO(dtrainor, nyquist): Add an SkPixelSerializer to not serialize images | 46 // TODO(dtrainor, nyquist): Add an SkPixelSerializer to not serialize images |
66 // more than once (crbug.com/548434). | 47 // more than once (crbug.com/548434). |
67 picture_->serialize(&stream, nullptr); | 48 picture_->serialize(&stream, nullptr); |
68 if (stream.bytesWritten() > 0) { | 49 if (stream.bytesWritten() > 0) { |
69 SkAutoDataUnref data(stream.copyToData()); | 50 SkAutoDataUnref data(stream.copyToData()); |
70 details->set_picture(data->data(), data->size()); | 51 details->set_picture(data->data(), data->size()); |
71 } | 52 } |
72 } | 53 } |
73 } | 54 } |
74 | 55 |
| 56 void DrawingDisplayItem::FromProtobuf(const proto::DisplayItem& proto) { |
| 57 DCHECK_EQ(proto::DisplayItem::Type_Drawing, proto.type()); |
| 58 |
| 59 skia::RefPtr<SkPicture> picture; |
| 60 const proto::DrawingDisplayItem& details = proto.drawing_item(); |
| 61 if (details.has_picture()) { |
| 62 SkMemoryStream stream(details.picture().data(), details.picture().size()); |
| 63 |
| 64 // TODO(dtrainor, nyquist): Add an image decoder. |
| 65 picture = skia::AdoptRef(SkPicture::CreateFromStream(&stream, nullptr)); |
| 66 } |
| 67 |
| 68 SetNew(std::move(picture)); |
| 69 } |
| 70 |
75 void DrawingDisplayItem::Raster(SkCanvas* canvas, | 71 void DrawingDisplayItem::Raster(SkCanvas* canvas, |
76 const gfx::Rect& canvas_target_playback_rect, | 72 const gfx::Rect& canvas_target_playback_rect, |
77 SkPicture::AbortCallback* callback) const { | 73 SkPicture::AbortCallback* callback) const { |
78 // The canvas_playback_rect can be empty to signify no culling is desired. | 74 // The canvas_playback_rect can be empty to signify no culling is desired. |
79 if (!canvas_target_playback_rect.IsEmpty()) { | 75 if (!canvas_target_playback_rect.IsEmpty()) { |
80 const SkMatrix& matrix = canvas->getTotalMatrix(); | 76 const SkMatrix& matrix = canvas->getTotalMatrix(); |
81 const SkRect& cull_rect = picture_->cullRect(); | 77 const SkRect& cull_rect = picture_->cullRect(); |
82 SkRect target_rect; | 78 SkRect target_rect; |
83 matrix.mapRect(&target_rect, cull_rect); | 79 matrix.mapRect(&target_rect, cull_rect); |
84 if (!target_rect.intersect(gfx::RectToSkRect(canvas_target_playback_rect))) | 80 if (!target_rect.intersect(gfx::RectToSkRect(canvas_target_playback_rect))) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 std::string b64_picture; | 112 std::string b64_picture; |
117 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture); | 113 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture); |
118 array->SetString("skp64", b64_picture); | 114 array->SetString("skp64", b64_picture); |
119 array->EndDictionary(); | 115 array->EndDictionary(); |
120 } | 116 } |
121 | 117 |
122 void DrawingDisplayItem::CloneTo(DrawingDisplayItem* item) const { | 118 void DrawingDisplayItem::CloneTo(DrawingDisplayItem* item) const { |
123 item->SetNew(picture_); | 119 item->SetNew(picture_); |
124 } | 120 } |
125 | 121 |
126 size_t DrawingDisplayItem::ExternalMemoryUsage() const { | |
127 return SkPictureUtils::ApproximateBytesUsed(picture_.get()); | |
128 } | |
129 | |
130 int DrawingDisplayItem::ApproximateOpCount() const { | |
131 return picture_->approximateOpCount(); | |
132 } | |
133 | |
134 bool DrawingDisplayItem::IsSuitableForGpuRasterization() const { | |
135 return picture_->suitableForGpuRasterization(NULL); | |
136 } | |
137 | |
138 } // namespace cc | 122 } // namespace cc |
OLD | NEW |