Chromium Code Reviews| 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" | 13 #include "cc/debug/traced_picture.h" |
| 14 #include "cc/debug/traced_value.h" | 14 #include "cc/debug/traced_value.h" |
| 15 #include "cc/resources/largest_display_item.h" | |
| 15 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkDrawPictureCallback.h" | 17 #include "third_party/skia/include/core/SkDrawPictureCallback.h" |
| 17 #include "third_party/skia/include/core/SkPictureRecorder.h" | 18 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 18 #include "third_party/skia/include/utils/SkPictureUtils.h" | 19 #include "third_party/skia/include/utils/SkPictureUtils.h" |
| 19 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
| 20 | 21 |
| 21 namespace cc { | 22 namespace cc { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 bool PictureTracingEnabled() { | 26 bool PictureTracingEnabled() { |
| 26 bool tracing_enabled; | 27 bool tracing_enabled; |
| 27 TRACE_EVENT_CATEGORY_GROUP_ENABLED( | 28 TRACE_EVENT_CATEGORY_GROUP_ENABLED( |
| 28 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 29 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
| 29 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 30 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
| 30 &tracing_enabled); | 31 &tracing_enabled); |
| 31 return tracing_enabled; | 32 return tracing_enabled; |
| 32 } | 33 } |
| 33 | 34 |
| 35 const int kDefaultNumDisplayItemsToReserve = 100; | |
| 36 | |
| 34 } // namespace | 37 } // namespace |
| 35 | 38 |
| 36 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, | 39 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, |
| 37 bool use_cached_picture, | 40 bool use_cached_picture, |
| 38 bool retain_individual_display_items) | 41 bool retain_individual_display_items) |
| 39 : recorder_(new SkPictureRecorder()), | 42 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve), |
| 43 recorder_(new SkPictureRecorder()), | |
| 40 use_cached_picture_(use_cached_picture), | 44 use_cached_picture_(use_cached_picture), |
| 41 retain_individual_display_items_(retain_individual_display_items), | 45 retain_individual_display_items_(retain_individual_display_items), |
| 42 layer_rect_(layer_rect), | 46 layer_rect_(layer_rect), |
| 43 is_suitable_for_gpu_rasterization_(true), | 47 is_suitable_for_gpu_rasterization_(true), |
| 44 approximate_op_count_(0), | 48 approximate_op_count_(0), |
| 45 picture_memory_usage_(0) { | 49 picture_memory_usage_(0) { |
| 50 #if DCHECK_IS_ON() | |
| 51 needs_process_ = false; | |
| 52 #endif | |
| 46 if (use_cached_picture_) { | 53 if (use_cached_picture_) { |
| 47 SkRTreeFactory factory; | 54 SkRTreeFactory factory; |
| 48 recorder_.reset(new SkPictureRecorder()); | 55 recorder_.reset(new SkPictureRecorder()); |
| 49 canvas_ = skia::SharePtr(recorder_->beginRecording( | 56 canvas_ = skia::SharePtr(recorder_->beginRecording( |
| 50 layer_rect_.width(), layer_rect_.height(), &factory)); | 57 layer_rect_.width(), layer_rect_.height(), &factory)); |
| 51 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); | 58 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); |
| 52 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); | 59 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); |
| 53 } | 60 } |
| 54 } | 61 } |
| 55 | 62 |
| 56 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, bool use_cached_picture) | 63 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, bool use_cached_picture) |
| 57 : DisplayItemList(layer_rect, | 64 : DisplayItemList(layer_rect, |
| 58 use_cached_picture, | 65 use_cached_picture, |
| 59 !use_cached_picture || PictureTracingEnabled()) { | 66 !use_cached_picture || PictureTracingEnabled()) { |
| 60 } | 67 } |
| 61 | 68 |
| 62 scoped_refptr<DisplayItemList> DisplayItemList::Create( | 69 scoped_refptr<DisplayItemList> DisplayItemList::Create( |
| 63 gfx::Rect layer_rect, | 70 gfx::Rect layer_rect, |
| 64 bool use_cached_picture) { | 71 bool use_cached_picture) { |
| 65 return make_scoped_refptr( | 72 return make_scoped_refptr( |
| 66 new DisplayItemList(layer_rect, use_cached_picture)); | 73 new DisplayItemList(layer_rect, use_cached_picture)); |
| 67 } | 74 } |
| 68 | 75 |
| 69 DisplayItemList::~DisplayItemList() { | 76 DisplayItemList::~DisplayItemList() { |
| 70 } | 77 } |
| 71 | 78 |
| 72 void DisplayItemList::Raster(SkCanvas* canvas, | 79 void DisplayItemList::Raster(SkCanvas* canvas, |
| 73 SkDrawPictureCallback* callback, | 80 SkDrawPictureCallback* callback, |
| 74 float contents_scale) const { | 81 float contents_scale) const { |
| 82 DCHECK(ProcessAppendedItemsCalled()); | |
| 75 if (!use_cached_picture_) { | 83 if (!use_cached_picture_) { |
| 76 canvas->save(); | 84 canvas->save(); |
| 77 canvas->scale(contents_scale, contents_scale); | 85 canvas->scale(contents_scale, contents_scale); |
| 78 for (size_t i = 0; i < items_.size(); ++i) { | 86 for (auto* item : items_) |
| 79 items_[i]->Raster(canvas, callback); | 87 item->Raster(canvas, callback); |
| 80 } | |
| 81 canvas->restore(); | 88 canvas->restore(); |
| 82 } else { | 89 } else { |
| 83 DCHECK(picture_); | 90 DCHECK(picture_); |
| 84 | 91 |
| 85 canvas->save(); | 92 canvas->save(); |
| 86 canvas->scale(contents_scale, contents_scale); | 93 canvas->scale(contents_scale, contents_scale); |
| 87 canvas->translate(layer_rect_.x(), layer_rect_.y()); | 94 canvas->translate(layer_rect_.x(), layer_rect_.y()); |
| 88 if (callback) { | 95 if (callback) { |
| 89 // If we have a callback, we need to call |draw()|, |drawPicture()| | 96 // If we have a callback, we need to call |draw()|, |drawPicture()| |
| 90 // doesn't take a callback. This is used by |AnalysisCanvas| to early | 97 // doesn't take a callback. This is used by |AnalysisCanvas| to early |
| 91 // out. | 98 // out. |
| 92 picture_->playback(canvas, callback); | 99 picture_->playback(canvas, callback); |
| 93 } else { | 100 } else { |
| 94 // Prefer to call |drawPicture()| on the canvas since it could place the | 101 // Prefer to call |drawPicture()| on the canvas since it could place the |
| 95 // entire picture on the canvas instead of parsing the skia operations. | 102 // entire picture on the canvas instead of parsing the skia operations. |
| 96 canvas->drawPicture(picture_.get()); | 103 canvas->drawPicture(picture_.get()); |
| 97 } | 104 } |
| 98 canvas->restore(); | 105 canvas->restore(); |
| 99 } | 106 } |
| 100 } | 107 } |
| 101 | 108 |
| 109 void DisplayItemList::ProcessAppendedItems() { | |
| 110 #if DCHECK_IS_ON() | |
| 111 needs_process_ = false; | |
| 112 #endif | |
| 113 for (DisplayItem* item : items_) { | |
|
ajuma
2015/05/06 15:49:48
With this post-processing approach, the ListContai
| |
| 114 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); | |
| 115 approximate_op_count_ += item->ApproximateOpCount(); | |
| 116 | |
| 117 if (use_cached_picture_) { | |
| 118 DCHECK(canvas_); | |
| 119 item->Raster(canvas_.get(), NULL); | |
| 120 } | |
| 121 | |
| 122 if (retain_individual_display_items_) { | |
| 123 // Warning: this double-counts SkPicture data if use_cached_picture_ is | |
| 124 // also true. | |
| 125 picture_memory_usage_ += item->PictureMemoryUsage(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 if (!retain_individual_display_items_) | |
| 130 items_.clear(); | |
| 131 } | |
| 132 | |
| 102 void DisplayItemList::CreateAndCacheSkPicture() { | 133 void DisplayItemList::CreateAndCacheSkPicture() { |
| 134 DCHECK(ProcessAppendedItemsCalled()); | |
| 103 // Convert to an SkPicture for faster rasterization. | 135 // Convert to an SkPicture for faster rasterization. |
| 104 DCHECK(use_cached_picture_); | 136 DCHECK(use_cached_picture_); |
| 105 DCHECK(!picture_); | 137 DCHECK(!picture_); |
| 106 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 138 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
| 107 DCHECK(picture_); | 139 DCHECK(picture_); |
| 108 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get()); | 140 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
| 109 recorder_.reset(); | 141 recorder_.reset(); |
| 110 canvas_.clear(); | 142 canvas_.clear(); |
| 111 } | 143 } |
| 112 | 144 |
| 113 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { | |
| 114 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); | |
| 115 approximate_op_count_ += item->ApproximateOpCount(); | |
| 116 | |
| 117 if (use_cached_picture_) { | |
| 118 DCHECK(canvas_); | |
| 119 item->Raster(canvas_.get(), NULL); | |
| 120 } | |
| 121 | |
| 122 if (retain_individual_display_items_) { | |
| 123 // Warning: this double-counts SkPicture data if use_cached_picture_ is also | |
| 124 // true. | |
| 125 picture_memory_usage_ += item->PictureMemoryUsage(); | |
| 126 items_.push_back(item.Pass()); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 145 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
| 146 DCHECK(ProcessAppendedItemsCalled()); | |
| 131 // This is more permissive than Picture's implementation, since none of the | 147 // This is more permissive than Picture's implementation, since none of the |
| 132 // items might individually trigger a veto even though they collectively have | 148 // items might individually trigger a veto even though they collectively have |
| 133 // enough "bad" operations that a corresponding Picture would get vetoed. | 149 // enough "bad" operations that a corresponding Picture would get vetoed. |
| 134 return is_suitable_for_gpu_rasterization_; | 150 return is_suitable_for_gpu_rasterization_; |
| 135 } | 151 } |
| 136 | 152 |
| 137 int DisplayItemList::ApproximateOpCount() const { | 153 int DisplayItemList::ApproximateOpCount() const { |
| 154 DCHECK(ProcessAppendedItemsCalled()); | |
| 138 return approximate_op_count_; | 155 return approximate_op_count_; |
| 139 } | 156 } |
| 140 | 157 |
| 141 size_t DisplayItemList::PictureMemoryUsage() const { | 158 size_t DisplayItemList::PictureMemoryUsage() const { |
| 159 DCHECK(ProcessAppendedItemsCalled()); | |
| 142 // We double-count in this case. Produce zero to avoid being misleading. | 160 // We double-count in this case. Produce zero to avoid being misleading. |
| 143 if (use_cached_picture_ && retain_individual_display_items_) | 161 if (use_cached_picture_ && retain_individual_display_items_) |
| 144 return 0; | 162 return 0; |
| 145 | 163 |
| 146 DCHECK_IMPLIES(use_cached_picture_, picture_); | 164 DCHECK_IMPLIES(use_cached_picture_, picture_); |
| 147 return picture_memory_usage_; | 165 return picture_memory_usage_; |
| 148 } | 166 } |
| 149 | 167 |
| 150 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 168 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
| 151 DisplayItemList::AsValue() const { | 169 DisplayItemList::AsValue() const { |
| 170 DCHECK(ProcessAppendedItemsCalled()); | |
| 152 scoped_refptr<base::trace_event::TracedValue> state = | 171 scoped_refptr<base::trace_event::TracedValue> state = |
| 153 new base::trace_event::TracedValue(); | 172 new base::trace_event::TracedValue(); |
| 154 | 173 |
| 155 state->SetInteger("length", items_.size()); | 174 state->SetInteger("length", items_.size()); |
| 156 state->BeginArray("params.items"); | 175 state->BeginArray("params.items"); |
| 157 for (const DisplayItem* item : items_) { | 176 for (const DisplayItem* item : items_) { |
| 158 item->AsValueInto(state.get()); | 177 item->AsValueInto(state.get()); |
| 159 } | 178 } |
| 160 state->EndArray(); | 179 state->EndArray(); |
| 161 state->SetValue("params.layer_rect", | 180 state->SetValue("params.layer_rect", |
| 162 MathUtil::AsValue(layer_rect_).release()); | 181 MathUtil::AsValue(layer_rect_).release()); |
| 163 | 182 |
| 164 SkPictureRecorder recorder; | 183 SkPictureRecorder recorder; |
| 165 SkCanvas* canvas = | 184 SkCanvas* canvas = |
| 166 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); | 185 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); |
| 167 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); | 186 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); |
| 168 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | 187 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); |
| 169 Raster(canvas, NULL, 1.f); | 188 Raster(canvas, NULL, 1.f); |
| 170 skia::RefPtr<SkPicture> picture = | 189 skia::RefPtr<SkPicture> picture = |
| 171 skia::AdoptRef(recorder.endRecordingAsPicture()); | 190 skia::AdoptRef(recorder.endRecordingAsPicture()); |
| 172 | 191 |
| 173 std::string b64_picture; | 192 std::string b64_picture; |
| 174 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); | 193 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); |
| 175 state->SetString("skp64", b64_picture); | 194 state->SetString("skp64", b64_picture); |
| 176 | 195 |
| 177 return state; | 196 return state; |
| 178 } | 197 } |
| 179 | 198 |
| 180 void DisplayItemList::EmitTraceSnapshot() const { | 199 void DisplayItemList::EmitTraceSnapshot() const { |
| 200 DCHECK(ProcessAppendedItemsCalled()); | |
| 181 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 201 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| 182 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 202 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
| 183 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 203 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
| 184 "cc::DisplayItemList", this, AsValue()); | 204 "cc::DisplayItemList", this, AsValue()); |
| 185 } | 205 } |
| 186 | 206 |
| 187 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { | 207 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { |
| 208 DCHECK(ProcessAppendedItemsCalled()); | |
| 188 // This should be only called once, and only after CreateAndCacheSkPicture. | 209 // This should be only called once, and only after CreateAndCacheSkPicture. |
| 189 DCHECK(picture_); | 210 DCHECK(picture_); |
| 190 DCHECK(!pixel_refs_); | 211 DCHECK(!pixel_refs_); |
| 191 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); | 212 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); |
| 192 if (!picture_->willPlayBackBitmaps()) | 213 if (!picture_->willPlayBackBitmaps()) |
| 193 return; | 214 return; |
| 194 | 215 |
| 195 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); | 216 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); |
| 196 } | 217 } |
| 197 } // namespace cc | 218 } // namespace cc |
| OLD | NEW |