| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/resources/display_list_raster_source.h" | |
| 6 | |
| 7 #include "base/trace_event/trace_event.h" | |
| 8 #include "cc/base/region.h" | |
| 9 #include "cc/debug/debug_colors.h" | |
| 10 #include "cc/resources/display_item_list.h" | |
| 11 #include "cc/resources/raster_source_helper.h" | |
| 12 #include "skia/ext/analysis_canvas.h" | |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | |
| 14 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 15 #include "ui/gfx/geometry/rect_conversions.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 #ifdef NDEBUG | |
| 20 const bool kDefaultClearCanvasSetting = false; | |
| 21 #else | |
| 22 const bool kDefaultClearCanvasSetting = true; | |
| 23 #endif | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace cc { | |
| 28 | |
| 29 scoped_refptr<DisplayListRasterSource> | |
| 30 DisplayListRasterSource::CreateFromDisplayListRecordingSource( | |
| 31 const DisplayListRecordingSource* other, | |
| 32 bool can_use_lcd_text) { | |
| 33 return make_scoped_refptr( | |
| 34 new DisplayListRasterSource(other, can_use_lcd_text)); | |
| 35 } | |
| 36 | |
| 37 DisplayListRasterSource::DisplayListRasterSource() | |
| 38 : background_color_(SK_ColorTRANSPARENT), | |
| 39 requires_clear_(true), | |
| 40 can_use_lcd_text_(true), | |
| 41 is_solid_color_(false), | |
| 42 solid_color_(SK_ColorTRANSPARENT), | |
| 43 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting), | |
| 44 slow_down_raster_scale_factor_for_debug_(0), | |
| 45 should_attempt_to_use_distance_field_text_(false) { | |
| 46 } | |
| 47 | |
| 48 DisplayListRasterSource::DisplayListRasterSource( | |
| 49 const DisplayListRecordingSource* other, | |
| 50 bool can_use_lcd_text) | |
| 51 : display_list_(other->display_list_), | |
| 52 background_color_(other->background_color_), | |
| 53 requires_clear_(other->requires_clear_), | |
| 54 can_use_lcd_text_(can_use_lcd_text), | |
| 55 is_solid_color_(other->is_solid_color_), | |
| 56 solid_color_(other->solid_color_), | |
| 57 recorded_viewport_(other->recorded_viewport_), | |
| 58 size_(other->size_), | |
| 59 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting), | |
| 60 slow_down_raster_scale_factor_for_debug_( | |
| 61 other->slow_down_raster_scale_factor_for_debug_), | |
| 62 should_attempt_to_use_distance_field_text_(false) { | |
| 63 } | |
| 64 | |
| 65 DisplayListRasterSource::DisplayListRasterSource( | |
| 66 const DisplayListRasterSource* other, | |
| 67 bool can_use_lcd_text) | |
| 68 : display_list_(other->display_list_), | |
| 69 background_color_(other->background_color_), | |
| 70 requires_clear_(other->requires_clear_), | |
| 71 can_use_lcd_text_(can_use_lcd_text), | |
| 72 is_solid_color_(other->is_solid_color_), | |
| 73 solid_color_(other->solid_color_), | |
| 74 recorded_viewport_(other->recorded_viewport_), | |
| 75 size_(other->size_), | |
| 76 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting), | |
| 77 slow_down_raster_scale_factor_for_debug_( | |
| 78 other->slow_down_raster_scale_factor_for_debug_), | |
| 79 should_attempt_to_use_distance_field_text_( | |
| 80 other->should_attempt_to_use_distance_field_text_) { | |
| 81 } | |
| 82 | |
| 83 DisplayListRasterSource::~DisplayListRasterSource() { | |
| 84 } | |
| 85 | |
| 86 void DisplayListRasterSource::PlaybackToSharedCanvas( | |
| 87 SkCanvas* canvas, | |
| 88 const gfx::Rect& canvas_rect, | |
| 89 float contents_scale) const { | |
| 90 RasterCommon(canvas, NULL, canvas_rect, contents_scale); | |
| 91 } | |
| 92 | |
| 93 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas, | |
| 94 const gfx::Rect& canvas_rect, | |
| 95 float contents_scale) const { | |
| 96 RasterCommon(canvas, canvas, canvas_rect, contents_scale); | |
| 97 } | |
| 98 | |
| 99 void DisplayListRasterSource::PlaybackToCanvas(SkCanvas* canvas, | |
| 100 const gfx::Rect& canvas_rect, | |
| 101 float contents_scale) const { | |
| 102 RasterSourceHelper::PrepareForPlaybackToCanvas( | |
| 103 canvas, canvas_rect, gfx::Rect(size_), contents_scale, background_color_, | |
| 104 clear_canvas_with_debug_color_, requires_clear_); | |
| 105 | |
| 106 RasterCommon(canvas, NULL, canvas_rect, contents_scale); | |
| 107 } | |
| 108 | |
| 109 void DisplayListRasterSource::RasterCommon(SkCanvas* canvas, | |
| 110 SkDrawPictureCallback* callback, | |
| 111 const gfx::Rect& canvas_rect, | |
| 112 float contents_scale) const { | |
| 113 canvas->translate(-canvas_rect.x(), -canvas_rect.y()); | |
| 114 gfx::Rect content_rect = | |
| 115 gfx::ToEnclosingRect(gfx::ScaleRect(gfx::Rect(size_), contents_scale)); | |
| 116 content_rect.Intersect(canvas_rect); | |
| 117 | |
| 118 canvas->clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op); | |
| 119 | |
| 120 DCHECK(display_list_.get()); | |
| 121 display_list_->Raster(canvas, callback, contents_scale); | |
| 122 } | |
| 123 | |
| 124 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() { | |
| 125 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture"); | |
| 126 | |
| 127 gfx::Rect display_list_rect(size_); | |
| 128 SkPictureRecorder recorder; | |
| 129 SkCanvas* canvas = recorder.beginRecording(display_list_rect.width(), | |
| 130 display_list_rect.height()); | |
| 131 if (!display_list_rect.IsEmpty()) | |
| 132 PlaybackToCanvas(canvas, display_list_rect, 1.0); | |
| 133 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); | |
| 134 | |
| 135 return picture; | |
| 136 } | |
| 137 | |
| 138 size_t DisplayListRasterSource::GetPictureMemoryUsage() const { | |
| 139 return display_list_->PictureMemoryUsage(); | |
| 140 } | |
| 141 | |
| 142 void DisplayListRasterSource::PerformSolidColorAnalysis( | |
| 143 const gfx::Rect& content_rect, | |
| 144 float contents_scale, | |
| 145 RasterSource::SolidColorAnalysis* analysis) const { | |
| 146 DCHECK(analysis); | |
| 147 TRACE_EVENT0("cc", "DisplayListRasterSource::PerformSolidColorAnalysis"); | |
| 148 | |
| 149 gfx::Rect layer_rect = | |
| 150 gfx::ScaleToEnclosingRect(content_rect, 1.0f / contents_scale); | |
| 151 | |
| 152 layer_rect.Intersect(gfx::Rect(size_)); | |
| 153 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height()); | |
| 154 RasterForAnalysis(&canvas, layer_rect, 1.0f); | |
| 155 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color); | |
| 156 } | |
| 157 | |
| 158 void DisplayListRasterSource::GatherPixelRefs( | |
| 159 const gfx::Rect& content_rect, | |
| 160 float contents_scale, | |
| 161 std::vector<SkPixelRef*>* pixel_refs) const { | |
| 162 // TODO(ajuma): Implement this. | |
| 163 } | |
| 164 | |
| 165 bool DisplayListRasterSource::CoversRect(const gfx::Rect& content_rect, | |
| 166 float contents_scale) const { | |
| 167 if (size_.IsEmpty()) | |
| 168 return false; | |
| 169 gfx::Rect layer_rect = | |
| 170 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale); | |
| 171 layer_rect.Intersect(gfx::Rect(size_)); | |
| 172 | |
| 173 return recorded_viewport_.Contains(layer_rect); | |
| 174 } | |
| 175 | |
| 176 gfx::Size DisplayListRasterSource::GetSize() const { | |
| 177 return size_; | |
| 178 } | |
| 179 | |
| 180 bool DisplayListRasterSource::IsSolidColor() const { | |
| 181 return is_solid_color_; | |
| 182 } | |
| 183 | |
| 184 SkColor DisplayListRasterSource::GetSolidColor() const { | |
| 185 DCHECK(IsSolidColor()); | |
| 186 return solid_color_; | |
| 187 } | |
| 188 | |
| 189 bool DisplayListRasterSource::HasRecordings() const { | |
| 190 return !!display_list_.get(); | |
| 191 } | |
| 192 | |
| 193 void DisplayListRasterSource::SetShouldAttemptToUseDistanceFieldText() { | |
| 194 should_attempt_to_use_distance_field_text_ = true; | |
| 195 } | |
| 196 | |
| 197 bool DisplayListRasterSource::ShouldAttemptToUseDistanceFieldText() const { | |
| 198 return should_attempt_to_use_distance_field_text_; | |
| 199 } | |
| 200 | |
| 201 void DisplayListRasterSource::AsValueInto( | |
| 202 base::trace_event::TracedValue* array) const { | |
| 203 if (display_list_.get()) | |
| 204 TracedValue::AppendIDRef(display_list_.get(), array); | |
| 205 } | |
| 206 | |
| 207 void DisplayListRasterSource::DidBeginTracing() { | |
| 208 if (display_list_.get()) | |
| 209 display_list_->EmitTraceSnapshot(); | |
| 210 } | |
| 211 | |
| 212 bool DisplayListRasterSource::CanUseLCDText() const { | |
| 213 return can_use_lcd_text_; | |
| 214 } | |
| 215 | |
| 216 scoped_refptr<RasterSource> DisplayListRasterSource::CreateCloneWithoutLCDText() | |
| 217 const { | |
| 218 bool can_use_lcd_text = false; | |
| 219 return scoped_refptr<RasterSource>( | |
| 220 new DisplayListRasterSource(this, can_use_lcd_text)); | |
| 221 } | |
| 222 | |
| 223 } // namespace cc | |
| OLD | NEW |