| 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::ProcessAppendedItemsOnTheFly() { |
| 110 if (retain_individual_display_items_) |
| 111 return; |
| 112 if (items_.size() >= kDefaultNumDisplayItemsToReserve) { |
| 113 ProcessAppendedItems(); |
| 114 // This function exists to keep the |items_| from growing indefinitely if |
| 115 // we're not going to store them anyway. So the items better be deleted |
| 116 // after |items_| grows too large and we process it. |
| 117 DCHECK(items_.empty()); |
| 118 } |
| 119 } |
| 120 |
| 121 void DisplayItemList::ProcessAppendedItems() { |
| 122 #if DCHECK_IS_ON() |
| 123 needs_process_ = false; |
| 124 #endif |
| 125 for (DisplayItem* item : items_) { |
| 126 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); |
| 127 approximate_op_count_ += item->ApproximateOpCount(); |
| 128 |
| 129 if (use_cached_picture_) { |
| 130 DCHECK(canvas_); |
| 131 item->Raster(canvas_.get(), NULL); |
| 132 } |
| 133 |
| 134 if (retain_individual_display_items_) { |
| 135 // Warning: this double-counts SkPicture data if use_cached_picture_ is |
| 136 // also true. |
| 137 picture_memory_usage_ += item->PictureMemoryUsage(); |
| 138 } |
| 139 } |
| 140 |
| 141 if (!retain_individual_display_items_) |
| 142 items_.clear(); |
| 143 } |
| 144 |
| 102 void DisplayItemList::CreateAndCacheSkPicture() { | 145 void DisplayItemList::CreateAndCacheSkPicture() { |
| 146 DCHECK(ProcessAppendedItemsCalled()); |
| 103 // Convert to an SkPicture for faster rasterization. | 147 // Convert to an SkPicture for faster rasterization. |
| 104 DCHECK(use_cached_picture_); | 148 DCHECK(use_cached_picture_); |
| 105 DCHECK(!picture_); | 149 DCHECK(!picture_); |
| 106 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 150 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
| 107 DCHECK(picture_); | 151 DCHECK(picture_); |
| 108 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get()); | 152 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
| 109 recorder_.reset(); | 153 recorder_.reset(); |
| 110 canvas_.clear(); | 154 canvas_.clear(); |
| 111 } | 155 } |
| 112 | 156 |
| 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 { | 157 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
| 158 DCHECK(ProcessAppendedItemsCalled()); |
| 131 // This is more permissive than Picture's implementation, since none of the | 159 // This is more permissive than Picture's implementation, since none of the |
| 132 // items might individually trigger a veto even though they collectively have | 160 // items might individually trigger a veto even though they collectively have |
| 133 // enough "bad" operations that a corresponding Picture would get vetoed. | 161 // enough "bad" operations that a corresponding Picture would get vetoed. |
| 134 return is_suitable_for_gpu_rasterization_; | 162 return is_suitable_for_gpu_rasterization_; |
| 135 } | 163 } |
| 136 | 164 |
| 137 int DisplayItemList::ApproximateOpCount() const { | 165 int DisplayItemList::ApproximateOpCount() const { |
| 166 DCHECK(ProcessAppendedItemsCalled()); |
| 138 return approximate_op_count_; | 167 return approximate_op_count_; |
| 139 } | 168 } |
| 140 | 169 |
| 141 size_t DisplayItemList::PictureMemoryUsage() const { | 170 size_t DisplayItemList::PictureMemoryUsage() const { |
| 171 DCHECK(ProcessAppendedItemsCalled()); |
| 142 // We double-count in this case. Produce zero to avoid being misleading. | 172 // We double-count in this case. Produce zero to avoid being misleading. |
| 143 if (use_cached_picture_ && retain_individual_display_items_) | 173 if (use_cached_picture_ && retain_individual_display_items_) |
| 144 return 0; | 174 return 0; |
| 145 | 175 |
| 146 DCHECK_IMPLIES(use_cached_picture_, picture_); | 176 DCHECK_IMPLIES(use_cached_picture_, picture_); |
| 147 return picture_memory_usage_; | 177 return picture_memory_usage_; |
| 148 } | 178 } |
| 149 | 179 |
| 150 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 180 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
| 151 DisplayItemList::AsValue() const { | 181 DisplayItemList::AsValue() const { |
| 182 DCHECK(ProcessAppendedItemsCalled()); |
| 152 scoped_refptr<base::trace_event::TracedValue> state = | 183 scoped_refptr<base::trace_event::TracedValue> state = |
| 153 new base::trace_event::TracedValue(); | 184 new base::trace_event::TracedValue(); |
| 154 | 185 |
| 155 state->SetInteger("length", items_.size()); | 186 state->SetInteger("length", items_.size()); |
| 156 state->BeginArray("params.items"); | 187 state->BeginArray("params.items"); |
| 157 for (const DisplayItem* item : items_) { | 188 for (const DisplayItem* item : items_) { |
| 158 item->AsValueInto(state.get()); | 189 item->AsValueInto(state.get()); |
| 159 } | 190 } |
| 160 state->EndArray(); | 191 state->EndArray(); |
| 161 state->SetValue("params.layer_rect", | 192 state->SetValue("params.layer_rect", |
| 162 MathUtil::AsValue(layer_rect_).release()); | 193 MathUtil::AsValue(layer_rect_).release()); |
| 163 | 194 |
| 164 SkPictureRecorder recorder; | 195 SkPictureRecorder recorder; |
| 165 SkCanvas* canvas = | 196 SkCanvas* canvas = |
| 166 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); | 197 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); |
| 167 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); | 198 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); |
| 168 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | 199 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); |
| 169 Raster(canvas, NULL, 1.f); | 200 Raster(canvas, NULL, 1.f); |
| 170 skia::RefPtr<SkPicture> picture = | 201 skia::RefPtr<SkPicture> picture = |
| 171 skia::AdoptRef(recorder.endRecordingAsPicture()); | 202 skia::AdoptRef(recorder.endRecordingAsPicture()); |
| 172 | 203 |
| 173 std::string b64_picture; | 204 std::string b64_picture; |
| 174 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); | 205 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); |
| 175 state->SetString("skp64", b64_picture); | 206 state->SetString("skp64", b64_picture); |
| 176 | 207 |
| 177 return state; | 208 return state; |
| 178 } | 209 } |
| 179 | 210 |
| 180 void DisplayItemList::EmitTraceSnapshot() const { | 211 void DisplayItemList::EmitTraceSnapshot() const { |
| 212 DCHECK(ProcessAppendedItemsCalled()); |
| 181 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 213 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| 182 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 214 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
| 183 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 215 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
| 184 "cc::DisplayItemList", this, AsValue()); | 216 "cc::DisplayItemList", this, AsValue()); |
| 185 } | 217 } |
| 186 | 218 |
| 187 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { | 219 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { |
| 220 DCHECK(ProcessAppendedItemsCalled()); |
| 188 // This should be only called once, and only after CreateAndCacheSkPicture. | 221 // This should be only called once, and only after CreateAndCacheSkPicture. |
| 189 DCHECK(picture_); | 222 DCHECK(picture_); |
| 190 DCHECK(!pixel_refs_); | 223 DCHECK(!pixel_refs_); |
| 191 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); | 224 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); |
| 192 if (!picture_->willPlayBackBitmaps()) | 225 if (!picture_->willPlayBackBitmaps()) |
| 193 return; | 226 return; |
| 194 | 227 |
| 195 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); | 228 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); |
| 196 } | 229 } |
| 197 } // namespace cc | 230 } // namespace cc |
| OLD | NEW |