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.
5 #include "cc/resources/raster_source_helper.h"
7 #include "base/trace_event/trace_event.h"
8 #include "cc/debug/debug_colors.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "ui/gfx/geometry/rect_conversions.h"
11 #include "ui/gfx/skia_util.h"
15 void RasterSourceHelper::PrepareForPlaybackToCanvas(
17 const gfx::Rect
& canvas_rect
,
18 const gfx::Rect
& source_rect
,
20 SkColor background_color
,
21 bool clear_canvas_with_debug_color
,
22 bool requires_clear
) {
24 if (clear_canvas_with_debug_color
) {
25 // Any non-painted areas in the content bounds will be left in this color.
26 canvas
->clear(DebugColors::NonPaintedFillColor());
29 // If this raster source has opaque contents, it is guaranteeing that it will
30 // draw an opaque rect the size of the layer. If it is not, then we must
31 // clear this canvas ourselves.
33 TRACE_EVENT_INSTANT0("cc", "SkCanvas::clear", TRACE_EVENT_SCOPE_THREAD
);
34 // Clearing is about ~4x faster than drawing a rect even if the content
35 // isn't covering a majority of the canvas.
36 canvas
->clear(SK_ColorTRANSPARENT
);
38 // Even if completely covered, for rasterizations that touch the edge of the
39 // layer, we also need to raster the background color underneath the last
40 // texel (since the recording won't cover it) and outside the last texel
41 // (due to linear filtering when using this texture).
42 gfx::Rect content_rect
=
43 gfx::ToEnclosingRect(gfx::ScaleRect(source_rect
, contents_scale
));
45 // The final texel of content may only be partially covered by a
46 // rasterization; this rect represents the content rect that is fully
47 // covered by content.
48 gfx::Rect deflated_content_rect
= content_rect
;
49 deflated_content_rect
.Inset(0, 0, 1, 1);
50 if (!deflated_content_rect
.Contains(canvas_rect
)) {
51 if (clear_canvas_with_debug_color
) {
52 // Any non-painted areas outside of the content bounds are left in
53 // this color. If this is seen then it means that cc neglected to
54 // rerasterize a tile that used to intersect with the content rect
55 // after the content bounds grew.
57 canvas
->translate(-canvas_rect
.x(), -canvas_rect
.y());
58 canvas
->clipRect(gfx::RectToSkRect(content_rect
),
59 SkRegion::kDifference_Op
);
60 canvas
->drawColor(DebugColors::MissingResizeInvalidations(),
61 SkXfermode::kSrc_Mode
);
65 // Drawing at most 2 x 2 x (canvas width + canvas height) texels is 2-3X
66 // faster than clearing, so special case this.
68 canvas
->translate(-canvas_rect
.x(), -canvas_rect
.y());
69 gfx::Rect inflated_content_rect
= content_rect
;
70 inflated_content_rect
.Inset(0, 0, -1, -1);
71 canvas
->clipRect(gfx::RectToSkRect(inflated_content_rect
),
72 SkRegion::kReplace_Op
);
73 canvas
->clipRect(gfx::RectToSkRect(deflated_content_rect
),
74 SkRegion::kDifference_Op
);
75 canvas
->drawColor(background_color
, SkXfermode::kSrc_Mode
);