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/resources/display_item_list.h" | 5 #include "cc/resources/display_item_list.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "base/trace_event/trace_event_argument.h" | 10 #include "base/trace_event/trace_event_argument.h" |
11 #include "cc/base/math_util.h" | 11 #include "cc/base/math_util.h" |
12 #include "cc/debug/picture_debug_util.h" | 12 #include "cc/debug/picture_debug_util.h" |
| 13 #include "cc/debug/traced_picture.h" |
| 14 #include "cc/debug/traced_value.h" |
13 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkDrawPictureCallback.h" |
14 #include "third_party/skia/include/core/SkPictureRecorder.h" | 17 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 18 #include "third_party/skia/include/utils/SkPictureUtils.h" |
15 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
16 | 20 |
17 namespace cc { | 21 namespace cc { |
18 | 22 |
19 DisplayItemList::DisplayItemList() | 23 DisplayItemList::DisplayItemList() |
20 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) { | 24 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) { |
21 } | 25 } |
22 | 26 |
23 scoped_refptr<DisplayItemList> DisplayItemList::Create() { | 27 scoped_refptr<DisplayItemList> DisplayItemList::Create() { |
24 return make_scoped_refptr(new DisplayItemList()); | 28 return make_scoped_refptr(new DisplayItemList()); |
25 } | 29 } |
26 | 30 |
27 DisplayItemList::~DisplayItemList() { | 31 DisplayItemList::~DisplayItemList() { |
28 } | 32 } |
29 | 33 |
30 void DisplayItemList::Raster(SkCanvas* canvas, | 34 void DisplayItemList::Raster(SkCanvas* canvas, |
31 SkDrawPictureCallback* callback, | 35 SkDrawPictureCallback* callback, |
32 float contents_scale) const { | 36 float contents_scale) const { |
33 canvas->save(); | 37 if (!picture_) { |
34 canvas->scale(contents_scale, contents_scale); | 38 canvas->save(); |
35 for (size_t i = 0; i < items_.size(); ++i) { | 39 canvas->scale(contents_scale, contents_scale); |
36 items_[i]->Raster(canvas, callback); | 40 for (size_t i = 0; i < items_.size(); ++i) { |
| 41 items_[i]->Raster(canvas, callback); |
| 42 } |
| 43 canvas->restore(); |
| 44 } else { |
| 45 DCHECK(picture_); |
| 46 |
| 47 canvas->save(); |
| 48 canvas->scale(contents_scale, contents_scale); |
| 49 if (callback) { |
| 50 // If we have a callback, we need to call |draw()|, |drawPicture()| |
| 51 // doesn't take a callback. This is used by |AnalysisCanvas| to early |
| 52 // out. |
| 53 picture_->playback(canvas, callback); |
| 54 } else { |
| 55 // Prefer to call |drawPicture()| on the canvas since it could place the |
| 56 // entire picture on the canvas instead of parsing the skia operations. |
| 57 canvas->drawPicture(picture_.get()); |
| 58 } |
| 59 canvas->restore(); |
37 } | 60 } |
38 canvas->restore(); | 61 } |
| 62 |
| 63 void DisplayItemList::CreateAndCacheSkPicture() { |
| 64 // Convert to an SkPicture for faster rasterization. Code is identical to |
| 65 // that in Picture::Record. |
| 66 SkRTreeFactory factory; |
| 67 SkPictureRecorder recorder; |
| 68 skia::RefPtr<SkCanvas> canvas; |
| 69 canvas = skia::SharePtr(recorder.beginRecording( |
| 70 layer_rect_.width(), layer_rect_.height(), &factory)); |
| 71 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); |
| 72 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); |
| 73 for (size_t i = 0; i < items_.size(); ++i) |
| 74 items_[i]->Raster(canvas.get(), NULL); |
| 75 picture_ = skia::AdoptRef(recorder.endRecording()); |
| 76 DCHECK(picture_); |
39 } | 77 } |
40 | 78 |
41 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { | 79 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { |
42 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); | 80 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); |
43 approximate_op_count_ += item->ApproximateOpCount(); | 81 approximate_op_count_ += item->ApproximateOpCount(); |
44 items_.push_back(item.Pass()); | 82 items_.push_back(item.Pass()); |
45 } | 83 } |
46 | 84 |
47 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 85 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
48 // This is more permissive than Picture's implementation, since none of the | 86 // This is more permissive than Picture's implementation, since none of the |
49 // items might individually trigger a veto even though they collectively have | 87 // items might individually trigger a veto even though they collectively have |
50 // enough "bad" operations that a corresponding Picture would get vetoed. | 88 // enough "bad" operations that a corresponding Picture would get vetoed. |
51 return is_suitable_for_gpu_rasterization_; | 89 return is_suitable_for_gpu_rasterization_; |
52 } | 90 } |
53 | 91 |
54 int DisplayItemList::ApproximateOpCount() const { | 92 int DisplayItemList::ApproximateOpCount() const { |
55 return approximate_op_count_; | 93 return approximate_op_count_; |
56 } | 94 } |
57 | 95 |
58 size_t DisplayItemList::PictureMemoryUsage() const { | 96 size_t DisplayItemList::PictureMemoryUsage() const { |
59 size_t total_size = 0; | 97 size_t total_size = 0; |
60 | 98 |
61 for (const auto& item : items_) { | 99 for (const auto& item : items_) { |
62 total_size += item->PictureMemoryUsage(); | 100 total_size += item->PictureMemoryUsage(); |
63 } | 101 } |
64 | 102 |
| 103 if (picture_) |
| 104 total_size += SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
| 105 |
65 return total_size; | 106 return total_size; |
66 } | 107 } |
67 | 108 |
68 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 109 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
69 DisplayItemList::AsValue() const { | 110 DisplayItemList::AsValue() const { |
70 scoped_refptr<base::trace_event::TracedValue> state = | 111 scoped_refptr<base::trace_event::TracedValue> state = |
71 new base::trace_event::TracedValue(); | 112 new base::trace_event::TracedValue(); |
72 | 113 |
73 state->SetInteger("length", items_.size()); | 114 state->SetInteger("length", items_.size()); |
74 state->BeginArray("params.items"); | 115 state->BeginArray("params.items"); |
(...skipping 21 matching lines...) Expand all Loading... |
96 } | 137 } |
97 | 138 |
98 void DisplayItemList::EmitTraceSnapshot() const { | 139 void DisplayItemList::EmitTraceSnapshot() const { |
99 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 140 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
100 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT( | 141 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT( |
101 "devtools.timeline.picture"), | 142 "devtools.timeline.picture"), |
102 "cc::DisplayItemList", this, AsValue()); | 143 "cc::DisplayItemList", this, AsValue()); |
103 } | 144 } |
104 | 145 |
105 } // namespace cc | 146 } // namespace cc |
OLD | NEW |