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
3 // found in the LICENSE file.
5 #include "cc/resources/content_layer_updater.h"
7 #include "base/debug/trace_event.h"
8 #include "cc/debug/rendering_stats_instrumentation.h"
9 #include "cc/resources/layer_painter.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkRect.h"
12 #include "third_party/skia/include/core/SkScalar.h"
13 #include "ui/gfx/geometry/rect_conversions.h"
14 #include "ui/gfx/geometry/rect_f.h"
15 #include "ui/gfx/skia_util.h"
19 ContentLayerUpdater::ContentLayerUpdater(
20 scoped_ptr<LayerPainter> painter,
21 RenderingStatsInstrumentation* stats_instrumentation,
23 : rendering_stats_instrumentation_(stats_instrumentation),
25 layer_is_opaque_(false),
26 layer_fills_bounds_completely_(false),
27 painter_(painter.Pass()),
28 background_color_(SK_ColorTRANSPARENT) {
31 ContentLayerUpdater::~ContentLayerUpdater() {}
33 void ContentLayerUpdater::set_rendering_stats_instrumentation(
34 RenderingStatsInstrumentation* rsi) {
35 rendering_stats_instrumentation_ = rsi;
38 void ContentLayerUpdater::PaintContents(SkCanvas* canvas,
39 const gfx::Size& layer_content_size,
40 const gfx::Rect& paint_rect,
41 float contents_width_scale,
42 float contents_height_scale) {
43 TRACE_EVENT0("cc", "ContentLayerUpdater::PaintContents");
47 canvas->translate(SkIntToScalar(-paint_rect.x()),
48 SkIntToScalar(-paint_rect.y()));
50 // The |canvas| backing should be sized to hold the |paint_rect|.
51 DCHECK_EQ(paint_rect.width(), canvas->getBaseLayerSize().width());
52 DCHECK_EQ(paint_rect.height(), canvas->getBaseLayerSize().height());
54 const bool is_scaled =
55 contents_width_scale != 1.f || contents_height_scale != 1.f;
57 if (is_scaled && (layer_is_opaque_ || layer_fills_bounds_completely_)) {
58 // Even if completely covered, for rasterizations that touch the edge of the
59 // layer, we also need to raster the background color underneath the last
60 // texel (since the paint won't cover it).
62 // The final texel of content may only be partially covered by a
63 // rasterization; this rect represents the content rect that is fully
64 // covered by content.
65 const gfx::Rect layer_content_rect = gfx::Rect(layer_content_size);
66 gfx::Rect deflated_layer_content_rect = layer_content_rect;
67 deflated_layer_content_rect.Inset(0, 0, 1, 1);
69 if (!layer_content_rect.Contains(deflated_layer_content_rect)) {
70 // Drawing at most 1 x 1 x (canvas width + canvas height) texels is 2-3X
71 // faster than clearing, so special case this.
72 DCHECK_LE(paint_rect.right(), layer_content_rect.right());
73 DCHECK_LE(paint_rect.bottom(), layer_content_rect.bottom());
75 canvas->clipRect(gfx::RectToSkRect(layer_content_rect),
76 SkRegion::kReplace_Op);
77 canvas->clipRect(gfx::RectToSkRect(deflated_layer_content_rect),
78 SkRegion::kDifference_Op);
79 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode);
86 canvas->scale(SkFloatToScalar(contents_width_scale),
87 SkFloatToScalar(contents_height_scale));
89 // NOTE: this may go beyond the bounds of the layer, but that shouldn't
90 // cause problems (anything beyond the layer is clipped out).
91 layer_rect = gfx::ScaleToEnclosingRect(
92 paint_rect, 1.f / contents_width_scale, 1.f / contents_height_scale);
94 layer_rect = paint_rect;
97 SkRect layer_sk_rect = SkRect::MakeXYWH(
98 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height());
100 canvas->clipRect(layer_sk_rect);
102 // If the layer has opaque contents or will fill the bounds completely there
103 // is no need to clear the canvas before painting.
104 if (!layer_is_opaque_ && !layer_fills_bounds_completely_) {
105 TRACE_EVENT0("cc", "Clear");
106 canvas->drawColor(SK_ColorTRANSPARENT, SkXfermode::kSrc_Mode);
109 painter_->Paint(canvas, layer_rect);
112 paint_rect_ = paint_rect;
115 void ContentLayerUpdater::SetOpaque(bool opaque) {
116 layer_is_opaque_ = opaque;
119 void ContentLayerUpdater::SetFillsBoundsCompletely(bool fills_bounds) {
120 layer_fills_bounds_completely_ = fills_bounds;
123 void ContentLayerUpdater::SetBackgroundColor(SkColor background_color) {
124 background_color_ = background_color;