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 "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" | 16 #include "third_party/skia/include/core/SkDrawPictureCallback.h" |
| 17 #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" | 18 #include "third_party/skia/include/utils/SkPictureUtils.h" |
| 19 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
| 20 | 20 |
| 21 namespace cc { | 21 namespace cc { |
| 22 | 22 |
| 23 DisplayItemList::DisplayItemList() | 23 DisplayItemList::DisplayItemList(gfx::Rect layer_rect) |
| 24 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) { | 24 : recorder_(new SkPictureRecorder()), |
| 25 retain_individual_display_items_(false), | |
| 26 layer_rect_(layer_rect), | |
| 27 is_suitable_for_gpu_rasterization_(true), | |
| 28 approximate_op_count_(0) { | |
| 29 SkRTreeFactory factory; | |
| 30 recorder_.reset(new SkPictureRecorder()); | |
| 31 canvas_ = skia::SharePtr(recorder_->beginRecording( | |
| 32 layer_rect_.width(), layer_rect_.height(), &factory)); | |
| 33 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); | |
| 34 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); | |
| 35 | |
| 36 bool tracing_enabled; | |
| 37 TRACE_EVENT_CATEGORY_GROUP_ENABLED( | |
| 38 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | |
| 39 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | |
| 40 &tracing_enabled); | |
|
ajuma
2015/04/14 18:02:32
If tracing is enabled after the DisplayItemList is
| |
| 41 if (tracing_enabled) | |
| 42 retain_individual_display_items_ = true; | |
| 25 } | 43 } |
| 26 | 44 |
| 27 scoped_refptr<DisplayItemList> DisplayItemList::Create() { | 45 scoped_refptr<DisplayItemList> DisplayItemList::Create(gfx::Rect layer_rect) { |
| 28 return make_scoped_refptr(new DisplayItemList()); | 46 return make_scoped_refptr(new DisplayItemList(layer_rect)); |
| 29 } | 47 } |
| 30 | 48 |
| 31 DisplayItemList::~DisplayItemList() { | 49 DisplayItemList::~DisplayItemList() { |
| 32 } | 50 } |
| 33 | 51 |
| 34 void DisplayItemList::Raster(SkCanvas* canvas, | 52 void DisplayItemList::Raster(SkCanvas* canvas, |
| 35 SkDrawPictureCallback* callback, | 53 SkDrawPictureCallback* callback, |
| 36 float contents_scale) const { | 54 float contents_scale) const { |
| 37 if (!picture_) { | 55 DCHECK(picture_); |
| 38 canvas->save(); | 56 |
| 39 canvas->scale(contents_scale, contents_scale); | 57 canvas->save(); |
| 40 for (size_t i = 0; i < items_.size(); ++i) { | 58 canvas->scale(contents_scale, contents_scale); |
| 41 items_[i]->Raster(canvas, callback); | 59 canvas->translate(layer_rect_.x(), layer_rect_.y()); |
| 42 } | 60 if (callback) { |
| 43 canvas->restore(); | 61 // If we have a callback, we need to call |draw()|, |drawPicture()| |
| 62 // doesn't take a callback. This is used by |AnalysisCanvas| to early | |
| 63 // out. | |
| 64 picture_->playback(canvas, callback); | |
| 44 } else { | 65 } else { |
| 45 DCHECK(picture_); | 66 // Prefer to call |drawPicture()| on the canvas since it could place the |
| 46 | 67 // entire picture on the canvas instead of parsing the skia operations. |
| 47 canvas->save(); | 68 canvas->drawPicture(picture_.get()); |
| 48 canvas->scale(contents_scale, contents_scale); | |
| 49 canvas->translate(layer_rect_.x(), layer_rect_.y()); | |
| 50 if (callback) { | |
| 51 // If we have a callback, we need to call |draw()|, |drawPicture()| | |
| 52 // doesn't take a callback. This is used by |AnalysisCanvas| to early | |
| 53 // out. | |
| 54 picture_->playback(canvas, callback); | |
| 55 } else { | |
| 56 // Prefer to call |drawPicture()| on the canvas since it could place the | |
| 57 // entire picture on the canvas instead of parsing the skia operations. | |
| 58 canvas->drawPicture(picture_.get()); | |
| 59 } | |
| 60 canvas->restore(); | |
| 61 } | 69 } |
| 70 canvas->restore(); | |
| 62 } | 71 } |
| 63 | 72 |
| 64 void DisplayItemList::CreateAndCacheSkPicture() { | 73 void DisplayItemList::CreateAndCacheSkPicture() { |
| 65 // Convert to an SkPicture for faster rasterization. Code is identical to | 74 // Convert to an SkPicture for faster rasterization. |
| 66 // that in Picture::Record. | 75 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
| 67 SkRTreeFactory factory; | |
| 68 SkPictureRecorder recorder; | |
| 69 skia::RefPtr<SkCanvas> canvas; | |
| 70 canvas = skia::SharePtr(recorder.beginRecording( | |
| 71 layer_rect_.width(), layer_rect_.height(), &factory)); | |
| 72 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); | |
| 73 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | |
| 74 for (size_t i = 0; i < items_.size(); ++i) | |
| 75 items_[i]->Raster(canvas.get(), NULL); | |
| 76 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
| 77 DCHECK(picture_); | 76 DCHECK(picture_); |
| 77 recorder_.reset(); | |
| 78 canvas_.clear(); | |
| 78 } | 79 } |
| 79 | 80 |
| 80 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { | 81 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { |
| 82 DCHECK(canvas_); | |
| 83 | |
| 81 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); | 84 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); |
| 82 approximate_op_count_ += item->ApproximateOpCount(); | 85 approximate_op_count_ += item->ApproximateOpCount(); |
| 83 items_.push_back(item.Pass()); | 86 |
| 87 item->Raster(canvas_.get(), NULL); | |
| 88 | |
| 89 if (retain_individual_display_items_) | |
| 90 items_.push_back(item.Pass()); | |
| 84 } | 91 } |
| 85 | 92 |
| 86 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 93 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
| 87 // This is more permissive than Picture's implementation, since none of the | 94 // This is more permissive than Picture's implementation, since none of the |
| 88 // items might individually trigger a veto even though they collectively have | 95 // items might individually trigger a veto even though they collectively have |
| 89 // enough "bad" operations that a corresponding Picture would get vetoed. | 96 // enough "bad" operations that a corresponding Picture would get vetoed. |
| 90 return is_suitable_for_gpu_rasterization_; | 97 return is_suitable_for_gpu_rasterization_; |
| 91 } | 98 } |
| 92 | 99 |
| 93 int DisplayItemList::ApproximateOpCount() const { | 100 int DisplayItemList::ApproximateOpCount() const { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 114 | 121 |
| 115 state->SetInteger("length", items_.size()); | 122 state->SetInteger("length", items_.size()); |
| 116 state->BeginArray("params.items"); | 123 state->BeginArray("params.items"); |
| 117 for (const DisplayItem* item : items_) { | 124 for (const DisplayItem* item : items_) { |
| 118 item->AsValueInto(state.get()); | 125 item->AsValueInto(state.get()); |
| 119 } | 126 } |
| 120 state->EndArray(); | 127 state->EndArray(); |
| 121 state->SetValue("params.layer_rect", | 128 state->SetValue("params.layer_rect", |
| 122 MathUtil::AsValue(layer_rect_).release()); | 129 MathUtil::AsValue(layer_rect_).release()); |
| 123 | 130 |
| 124 SkPictureRecorder recorder; | |
| 125 SkCanvas* canvas = | |
| 126 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); | |
| 127 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); | |
| 128 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | |
| 129 for (size_t i = 0; i < items_.size(); ++i) | |
| 130 items_[i]->RasterForTracing(canvas); | |
| 131 skia::RefPtr<SkPicture> picture = | |
| 132 skia::AdoptRef(recorder.endRecordingAsPicture()); | |
| 133 | |
| 134 std::string b64_picture; | 131 std::string b64_picture; |
| 135 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); | 132 DCHECK(picture_); |
| 133 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture); | |
| 136 state->SetString("skp64", b64_picture); | 134 state->SetString("skp64", b64_picture); |
| 137 | 135 |
| 138 return state; | 136 return state; |
| 139 } | 137 } |
| 140 | 138 |
| 141 void DisplayItemList::EmitTraceSnapshot() const { | 139 void DisplayItemList::EmitTraceSnapshot() const { |
| 142 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 140 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| 143 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 141 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
| 144 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 142 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
| 145 "cc::DisplayItemList", this, AsValue()); | 143 "cc::DisplayItemList", this, AsValue()); |
| 146 } | 144 } |
| 147 | 145 |
| 148 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { | 146 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { |
| 149 // This should be only called once, and only after CreateAndCacheSkPicture. | 147 // This should be only called once, and only after CreateAndCacheSkPicture. |
| 150 DCHECK(picture_); | 148 DCHECK(picture_); |
| 151 DCHECK(!pixel_refs_); | 149 DCHECK(!pixel_refs_); |
| 152 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); | 150 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); |
| 153 if (!picture_->willPlayBackBitmaps()) | 151 if (!picture_->willPlayBackBitmaps()) |
| 154 return; | 152 return; |
| 155 | 153 |
| 156 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); | 154 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); |
| 157 } | 155 } |
| 158 } // namespace cc | 156 } // namespace cc |
| OLD | NEW |