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/playback/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_bitmap_rect
,
18 const gfx::Rect
& canvas_playback_rect
,
19 const gfx::Rect
& source_rect
,
21 SkColor background_color
,
22 bool clear_canvas_with_debug_color
,
23 bool requires_clear
) {
24 bool partial_update
= canvas_bitmap_rect
!= canvas_playback_rect
;
28 if (clear_canvas_with_debug_color
) {
29 // Any non-painted areas in the content bounds will be left in this color.
30 if (!partial_update
) {
31 canvas
->clear(DebugColors::NonPaintedFillColor());
34 canvas
->clipRect(gfx::RectToSkRect(
35 canvas_playback_rect
- canvas_bitmap_rect
.OffsetFromOrigin()));
36 canvas
->drawColor(DebugColors::NonPaintedFillColor());
41 // If this raster source has opaque contents, it is guaranteeing that it will
42 // draw an opaque rect the size of the layer. If it is not, then we must
43 // clear this canvas ourselves.
45 TRACE_EVENT_INSTANT0("cc", "SkCanvas::clear", TRACE_EVENT_SCOPE_THREAD
);
46 // Clearing is about ~4x faster than drawing a rect even if the content
47 // isn't covering a majority of the canvas.
48 if (!partial_update
) {
49 canvas
->clear(SK_ColorTRANSPARENT
);
52 canvas
->clipRect(gfx::RectToSkRect(
53 canvas_playback_rect
- canvas_bitmap_rect
.OffsetFromOrigin()));
54 canvas
->drawColor(SK_ColorTRANSPARENT
, SkXfermode::kClear_Mode
);
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 recording won't cover it) and outside the last texel
61 // (due to linear filtering when using this texture).
62 gfx::Rect content_rect
=
63 gfx::ToEnclosingRect(gfx::ScaleRect(source_rect
, contents_scale
));
65 // The final texel of content may only be partially covered by a
66 // rasterization; this rect represents the content rect that is fully
67 // covered by content.
68 gfx::Rect deflated_content_rect
= content_rect
;
69 deflated_content_rect
.Inset(0, 0, 1, 1);
70 deflated_content_rect
.Intersect(canvas_playback_rect
);
71 if (!deflated_content_rect
.Contains(canvas_playback_rect
)) {
72 if (clear_canvas_with_debug_color
) {
73 // Any non-painted areas outside of the content bounds are left in
74 // this color. If this is seen then it means that cc neglected to
75 // rerasterize a tile that used to intersect with the content rect
76 // after the content bounds grew.
78 canvas
->translate(-canvas_bitmap_rect
.x(), -canvas_bitmap_rect
.y());
79 canvas
->clipRect(gfx::RectToSkRect(content_rect
),
80 SkRegion::kDifference_Op
);
81 canvas
->drawColor(DebugColors::MissingResizeInvalidations(),
82 SkXfermode::kSrc_Mode
);
86 // Drawing at most 2 x 2 x (canvas width + canvas height) texels is 2-3X
87 // faster than clearing, so special case this.
89 canvas
->translate(-canvas_bitmap_rect
.x(), -canvas_bitmap_rect
.y());
90 gfx::Rect inflated_content_rect
= content_rect
;
91 // Only clear edges that will be inside the canvas_playback_rect, else we
92 // clear things that are still valid from a previous raster.
93 inflated_content_rect
.Inset(0, 0, -1, -1);
94 inflated_content_rect
.Intersect(canvas_playback_rect
);
95 canvas
->clipRect(gfx::RectToSkRect(inflated_content_rect
),
96 SkRegion::kReplace_Op
);
97 canvas
->clipRect(gfx::RectToSkRect(deflated_content_rect
),
98 SkRegion::kDifference_Op
);
99 canvas
->drawColor(background_color
, SkXfermode::kSrc_Mode
);