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 "base/time/time.h"
9 #include "cc/debug/rendering_stats_instrumentation.h"
10 #include "cc/resources/layer_painter.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "third_party/skia/include/core/SkPaint.h"
13 #include "third_party/skia/include/core/SkRect.h"
14 #include "third_party/skia/include/core/SkScalar.h"
15 #include "ui/gfx/rect_conversions.h"
16 #include "ui/gfx/rect_f.h"
20 ContentLayerUpdater::ContentLayerUpdater(
21 scoped_ptr<LayerPainter> painter,
22 RenderingStatsInstrumentation* stats_instrumentation,
24 : rendering_stats_instrumentation_(stats_instrumentation),
26 layer_is_opaque_(false),
27 layer_fills_bounds_completely_(false),
28 painter_(painter.Pass()) {}
30 ContentLayerUpdater::~ContentLayerUpdater() {}
32 void ContentLayerUpdater::set_rendering_stats_instrumentation(
33 RenderingStatsInstrumentation* rsi) {
34 rendering_stats_instrumentation_ = rsi;
37 void ContentLayerUpdater::PaintContents(SkCanvas* canvas,
38 const gfx::Point& origin,
39 float contents_width_scale,
40 float contents_height_scale,
41 gfx::Rect* resulting_opaque_rect) {
42 TRACE_EVENT0("cc", "ContentLayerUpdater::PaintContents");
44 canvas->translate(SkFloatToScalar(-origin.x()),
45 SkFloatToScalar(-origin.y()));
47 SkISize size = canvas->getDeviceSize();
48 gfx::Rect content_rect(origin, gfx::Size(size.width(), size.height()));
50 gfx::Rect layer_rect = content_rect;
52 if (contents_width_scale != 1.f || contents_height_scale != 1.f) {
53 canvas->scale(SkFloatToScalar(contents_width_scale),
54 SkFloatToScalar(contents_height_scale));
56 layer_rect = gfx::ScaleToEnclosingRect(
57 content_rect, 1.f / contents_width_scale, 1.f / contents_height_scale);
60 SkRect layer_sk_rect = SkRect::MakeXYWH(
61 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height());
63 canvas->clipRect(layer_sk_rect);
65 // If the layer has opaque contents or will fill the bounds completely there
66 // is no need to clear the canvas before painting.
67 if (!layer_is_opaque_ && !layer_fills_bounds_completely_) {
68 TRACE_EVENT0("cc", "Clear");
69 canvas->drawColor(SK_ColorTRANSPARENT, SkXfermode::kSrc_Mode);
72 gfx::RectF opaque_layer_rect;
73 painter_->Paint(canvas, layer_rect, &opaque_layer_rect);
76 gfx::Rect opaque_content_rect = gfx::ToEnclosedRect(gfx::ScaleRect(
77 opaque_layer_rect, contents_width_scale, contents_height_scale));
78 *resulting_opaque_rect = opaque_content_rect;
80 content_rect_ = content_rect;
83 void ContentLayerUpdater::SetOpaque(bool opaque) {
84 layer_is_opaque_ = opaque;
87 void ContentLayerUpdater::SetFillsBoundsCompletely(bool fills_bounds) {
88 layer_fills_bounds_completely_ = fills_bounds;