summaryrefslogtreecommitdiffstats
path: root/cc/resources/content_layer_updater.cc
blob: 206d76ba2ff82daed1548da7215d5b17dc8d90c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/resources/content_layer_updater.h"

#include "base/debug/trace_event.h"
#include "cc/resources/layer_painter.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/skia_util.h"

namespace cc {

ContentLayerUpdater::ContentLayerUpdater(scoped_ptr<LayerPainter> painter,
                                         int layer_id)
    : layer_id_(layer_id),
      layer_is_opaque_(false),
      layer_fills_bounds_completely_(false),
      painter_(painter.Pass()),
      background_color_(SK_ColorTRANSPARENT) {
}

ContentLayerUpdater::~ContentLayerUpdater() {}

void ContentLayerUpdater::PaintContents(SkCanvas* canvas,
                                        const gfx::Size& layer_content_size,
                                        const gfx::Rect& paint_rect,
                                        float contents_width_scale,
                                        float contents_height_scale) {
  TRACE_EVENT0("cc", "ContentLayerUpdater::PaintContents");
  if (!canvas)
    return;
  canvas->save();
  canvas->translate(SkIntToScalar(-paint_rect.x()),
                    SkIntToScalar(-paint_rect.y()));

  // The |canvas| backing should be sized to hold the |paint_rect|.
  DCHECK_EQ(paint_rect.width(), canvas->getBaseLayerSize().width());
  DCHECK_EQ(paint_rect.height(), canvas->getBaseLayerSize().height());

  const bool is_scaled =
      contents_width_scale != 1.f || contents_height_scale != 1.f;

  if (is_scaled && (layer_is_opaque_ || layer_fills_bounds_completely_)) {
    // Even if completely covered, for rasterizations that touch the edge of the
    // layer, we also need to raster the background color underneath the last
    // texel (since the paint won't cover it).
    //
    // The final texel of content may only be partially covered by a
    // rasterization; this rect represents the content rect that is fully
    // covered by content.
    const gfx::Rect layer_content_rect = gfx::Rect(layer_content_size);
    gfx::Rect deflated_layer_content_rect = layer_content_rect;
    deflated_layer_content_rect.Inset(0, 0, 1, 1);

    if (!layer_content_rect.Contains(deflated_layer_content_rect)) {
      // Drawing at most 1 x 1 x (canvas width + canvas height) texels is 2-3X
      // faster than clearing, so special case this.
      DCHECK_LE(paint_rect.right(), layer_content_rect.right());
      DCHECK_LE(paint_rect.bottom(), layer_content_rect.bottom());
      canvas->save();
      canvas->clipRect(gfx::RectToSkRect(layer_content_rect),
                       SkRegion::kReplace_Op);
      canvas->clipRect(gfx::RectToSkRect(deflated_layer_content_rect),
                       SkRegion::kDifference_Op);
      canvas->drawColor(background_color_, SkXfermode::kSrc_Mode);
      canvas->restore();
    }
  }

  gfx::Rect layer_rect;
  if (is_scaled) {
    canvas->scale(SkFloatToScalar(contents_width_scale),
                  SkFloatToScalar(contents_height_scale));

    // NOTE: this may go beyond the bounds of the layer, but that shouldn't
    // cause problems (anything beyond the layer is clipped out).
    layer_rect = gfx::ScaleToEnclosingRect(
        paint_rect, 1.f / contents_width_scale, 1.f / contents_height_scale);
  } else {
    layer_rect = paint_rect;
  }

  SkRect layer_sk_rect = SkRect::MakeXYWH(
      layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height());

  canvas->clipRect(layer_sk_rect);

  // If the layer has opaque contents or will fill the bounds completely there
  // is no need to clear the canvas before painting.
  if (!layer_is_opaque_ && !layer_fills_bounds_completely_) {
    TRACE_EVENT0("cc", "Clear");
    canvas->drawColor(SK_ColorTRANSPARENT, SkXfermode::kSrc_Mode);
  }

  painter_->Paint(canvas, layer_rect);
  canvas->restore();

  paint_rect_ = paint_rect;
}

void ContentLayerUpdater::SetOpaque(bool opaque) {
  layer_is_opaque_ = opaque;
}

void ContentLayerUpdater::SetFillsBoundsCompletely(bool fills_bounds) {
  layer_fills_bounds_completely_ = fills_bounds;
}

void ContentLayerUpdater::SetBackgroundColor(SkColor background_color) {
  background_color_ = background_color;
}

}  // namespace cc