OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 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/content_layer_updater.h" | 5 #include "cc/resources/content_layer_updater.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "cc/debug/rendering_stats_instrumentation.h" | 9 #include "cc/debug/rendering_stats_instrumentation.h" |
10 #include "cc/resources/layer_painter.h" | 10 #include "cc/resources/layer_painter.h" |
11 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
12 #include "third_party/skia/include/core/SkPaint.h" | 12 #include "third_party/skia/include/core/SkPaint.h" |
13 #include "third_party/skia/include/core/SkRect.h" | 13 #include "third_party/skia/include/core/SkRect.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 14 #include "third_party/skia/include/core/SkScalar.h" |
15 #include "ui/gfx/rect_conversions.h" | 15 #include "ui/gfx/rect_conversions.h" |
16 #include "ui/gfx/rect_f.h" | 16 #include "ui/gfx/rect_f.h" |
| 17 #include "ui/gfx/skia_util.h" |
17 | 18 |
18 namespace cc { | 19 namespace cc { |
19 | 20 |
20 ContentLayerUpdater::ContentLayerUpdater( | 21 ContentLayerUpdater::ContentLayerUpdater( |
21 scoped_ptr<LayerPainter> painter, | 22 scoped_ptr<LayerPainter> painter, |
22 RenderingStatsInstrumentation* stats_instrumentation, | 23 RenderingStatsInstrumentation* stats_instrumentation, |
23 int layer_id) | 24 int layer_id) |
24 : rendering_stats_instrumentation_(stats_instrumentation), | 25 : rendering_stats_instrumentation_(stats_instrumentation), |
25 layer_id_(layer_id), | 26 layer_id_(layer_id), |
26 layer_is_opaque_(false), | 27 layer_is_opaque_(false), |
27 layer_fills_bounds_completely_(false), | 28 layer_fills_bounds_completely_(false), |
28 painter_(painter.Pass()) {} | 29 painter_(painter.Pass()), |
| 30 background_color_(SK_ColorTRANSPARENT) { |
| 31 } |
29 | 32 |
30 ContentLayerUpdater::~ContentLayerUpdater() {} | 33 ContentLayerUpdater::~ContentLayerUpdater() {} |
31 | 34 |
32 void ContentLayerUpdater::set_rendering_stats_instrumentation( | 35 void ContentLayerUpdater::set_rendering_stats_instrumentation( |
33 RenderingStatsInstrumentation* rsi) { | 36 RenderingStatsInstrumentation* rsi) { |
34 rendering_stats_instrumentation_ = rsi; | 37 rendering_stats_instrumentation_ = rsi; |
35 } | 38 } |
36 | 39 |
37 void ContentLayerUpdater::PaintContents(SkCanvas* canvas, | 40 void ContentLayerUpdater::PaintContents(SkCanvas* canvas, |
38 const gfx::Rect& content_rect, | 41 const gfx::Size& layer_content_size, |
| 42 const gfx::Rect& paint_rect, |
39 float contents_width_scale, | 43 float contents_width_scale, |
40 float contents_height_scale) { | 44 float contents_height_scale) { |
41 TRACE_EVENT0("cc", "ContentLayerUpdater::PaintContents"); | 45 TRACE_EVENT0("cc", "ContentLayerUpdater::PaintContents"); |
42 if (!canvas) | 46 if (!canvas) |
43 return; | 47 return; |
44 canvas->save(); | 48 canvas->save(); |
45 canvas->translate(SkFloatToScalar(-content_rect.x()), | 49 canvas->translate(SkIntToScalar(-paint_rect.x()), |
46 SkFloatToScalar(-content_rect.y())); | 50 SkIntToScalar(-paint_rect.y())); |
47 | 51 |
48 // The |canvas| backing should be sized to hold the |content_rect|. | 52 // The |canvas| backing should be sized to hold the |paint_rect|. |
49 DCHECK_EQ(content_rect.width(), canvas->getBaseLayerSize().width()); | 53 DCHECK_EQ(paint_rect.width(), canvas->getBaseLayerSize().width()); |
50 DCHECK_EQ(content_rect.height(), canvas->getBaseLayerSize().height()); | 54 DCHECK_EQ(paint_rect.height(), canvas->getBaseLayerSize().height()); |
51 | 55 |
52 gfx::Rect layer_rect = content_rect; | 56 const bool is_scaled = |
53 if (contents_width_scale != 1.f || contents_height_scale != 1.f) { | 57 contents_width_scale != 1.f || contents_height_scale != 1.f; |
| 58 |
| 59 if (is_scaled && (layer_is_opaque_ || layer_fills_bounds_completely_)) { |
| 60 // Even if completely covered, for rasterizations that touch the edge of the |
| 61 // layer, we also need to raster the background color underneath the last |
| 62 // texel (since the paint won't cover it). |
| 63 // |
| 64 // The final texel of content may only be partially covered by a |
| 65 // rasterization; this rect represents the content rect that is fully |
| 66 // covered by content. |
| 67 const gfx::Rect layer_content_rect = gfx::Rect(layer_content_size); |
| 68 gfx::Rect deflated_layer_content_rect = layer_content_rect; |
| 69 deflated_layer_content_rect.Inset(0, 0, 1, 1); |
| 70 |
| 71 if (!layer_content_rect.Contains(deflated_layer_content_rect)) { |
| 72 // Drawing at most 1 x 1 x (canvas width + canvas height) texels is 2-3X |
| 73 // faster than clearing, so special case this. |
| 74 DCHECK_LE(paint_rect.right(), layer_content_rect.right()); |
| 75 DCHECK_LE(paint_rect.bottom(), layer_content_rect.bottom()); |
| 76 canvas->save(); |
| 77 canvas->clipRect(gfx::RectToSkRect(layer_content_rect), |
| 78 SkRegion::kReplace_Op); |
| 79 canvas->clipRect(gfx::RectToSkRect(deflated_layer_content_rect), |
| 80 SkRegion::kDifference_Op); |
| 81 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode); |
| 82 canvas->restore(); |
| 83 } |
| 84 } |
| 85 |
| 86 gfx::Rect layer_rect; |
| 87 if (is_scaled) { |
54 canvas->scale(SkFloatToScalar(contents_width_scale), | 88 canvas->scale(SkFloatToScalar(contents_width_scale), |
55 SkFloatToScalar(contents_height_scale)); | 89 SkFloatToScalar(contents_height_scale)); |
56 | 90 |
| 91 // NOTE: this may go beyond the bounds of the layer, but that shouldn't |
| 92 // cause problems (anything beyond the layer is clipped out). |
57 layer_rect = gfx::ScaleToEnclosingRect( | 93 layer_rect = gfx::ScaleToEnclosingRect( |
58 content_rect, 1.f / contents_width_scale, 1.f / contents_height_scale); | 94 paint_rect, 1.f / contents_width_scale, 1.f / contents_height_scale); |
| 95 } else { |
| 96 layer_rect = paint_rect; |
59 } | 97 } |
60 | 98 |
61 SkRect layer_sk_rect = SkRect::MakeXYWH( | 99 SkRect layer_sk_rect = SkRect::MakeXYWH( |
62 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()); | 100 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height()); |
63 | 101 |
64 canvas->clipRect(layer_sk_rect); | 102 canvas->clipRect(layer_sk_rect); |
65 | 103 |
66 // If the layer has opaque contents or will fill the bounds completely there | 104 // If the layer has opaque contents or will fill the bounds completely there |
67 // is no need to clear the canvas before painting. | 105 // is no need to clear the canvas before painting. |
68 if (!layer_is_opaque_ && !layer_fills_bounds_completely_) { | 106 if (!layer_is_opaque_ && !layer_fills_bounds_completely_) { |
69 TRACE_EVENT0("cc", "Clear"); | 107 TRACE_EVENT0("cc", "Clear"); |
70 canvas->drawColor(SK_ColorTRANSPARENT, SkXfermode::kSrc_Mode); | 108 canvas->drawColor(SK_ColorTRANSPARENT, SkXfermode::kSrc_Mode); |
71 } | 109 } |
72 | 110 |
73 painter_->Paint(canvas, layer_rect); | 111 painter_->Paint(canvas, layer_rect); |
74 canvas->restore(); | 112 canvas->restore(); |
75 | 113 |
76 content_rect_ = content_rect; | 114 paint_rect_ = paint_rect; |
77 } | 115 } |
78 | 116 |
79 void ContentLayerUpdater::SetOpaque(bool opaque) { | 117 void ContentLayerUpdater::SetOpaque(bool opaque) { |
80 layer_is_opaque_ = opaque; | 118 layer_is_opaque_ = opaque; |
81 } | 119 } |
82 | 120 |
83 void ContentLayerUpdater::SetFillsBoundsCompletely(bool fills_bounds) { | 121 void ContentLayerUpdater::SetFillsBoundsCompletely(bool fills_bounds) { |
84 layer_fills_bounds_completely_ = fills_bounds; | 122 layer_fills_bounds_completely_ = fills_bounds; |
85 } | 123 } |
86 | 124 |
| 125 void ContentLayerUpdater::SetBackgroundColor(SkColor background_color) { |
| 126 background_color_ = background_color; |
| 127 } |
| 128 |
87 } // namespace cc | 129 } // namespace cc |
OLD | NEW |