1 // Copyright 2012 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/test/fake_content_layer_client.h"
7 #include "cc/playback/clip_display_item.h"
8 #include "cc/playback/drawing_display_item.h"
9 #include "cc/playback/transform_display_item.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkPictureRecorder.h"
12 #include "ui/gfx/skia_util.h"
16 FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap
& bitmap
,
17 const gfx::Point
& point
,
19 : bitmap(bitmap
), point(point
), paint(paint
) {
22 FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap
& bitmap
,
23 const gfx::Transform
& transform
,
25 : bitmap(bitmap
), transform(transform
), paint(paint
) {
28 FakeContentLayerClient::BitmapData::~BitmapData() {
31 FakeContentLayerClient::FakeContentLayerClient()
32 : fill_with_nonsolid_color_(false),
33 last_canvas_(nullptr),
34 last_painting_control_(PAINTING_BEHAVIOR_NORMAL
),
35 reported_memory_usage_(0) {}
37 FakeContentLayerClient::~FakeContentLayerClient() {
40 void FakeContentLayerClient::PaintContents(
42 const gfx::Rect
& paint_rect
,
43 PaintingControlSetting painting_control
) {
44 last_canvas_
= canvas
;
45 last_painting_control_
= painting_control
;
47 canvas
->clipRect(gfx::RectToSkRect(paint_rect
));
48 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
49 it
!= draw_rects_
.end(); ++it
) {
50 const gfx::RectF
& draw_rect
= it
->first
;
51 const SkPaint
& paint
= it
->second
;
52 canvas
->drawRectCoords(draw_rect
.x(),
59 for (BitmapVector::const_iterator it
= draw_bitmaps_
.begin();
60 it
!= draw_bitmaps_
.end(); ++it
) {
61 canvas
->drawBitmap(it
->bitmap
, it
->point
.x(), it
->point
.y(), &it
->paint
);
64 if (fill_with_nonsolid_color_
) {
65 gfx::RectF draw_rect
= paint_rect
;
67 while (!draw_rect
.IsEmpty()) {
69 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
70 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
71 draw_rect
.Inset(1, 1);
76 scoped_refptr
<DisplayItemList
>
77 FakeContentLayerClient::PaintContentsToDisplayList(
78 const gfx::Rect
& clip
,
79 PaintingControlSetting painting_control
) {
80 // Cached picture is used because unit tests expect to be able to
81 // use GatherPixelRefs.
82 const bool use_cached_picture
= true;
83 scoped_refptr
<DisplayItemList
> display_list
=
84 DisplayItemList::Create(clip
, use_cached_picture
);
85 SkPictureRecorder recorder
;
86 skia::RefPtr
<SkCanvas
> canvas
;
87 skia::RefPtr
<SkPicture
> picture
;
88 auto* item
= display_list
->CreateAndAppendItem
<ClipDisplayItem
>();
89 item
->SetNew(clip
, std::vector
<SkRRect
>());
91 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
92 it
!= draw_rects_
.end(); ++it
) {
93 const gfx::RectF
& draw_rect
= it
->first
;
94 const SkPaint
& paint
= it
->second
;
96 skia::SharePtr(recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
97 canvas
->drawRectCoords(draw_rect
.x(), draw_rect
.y(), draw_rect
.width(),
98 draw_rect
.height(), paint
);
99 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
100 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
101 item
->SetNew(picture
.Pass());
104 for (BitmapVector::const_iterator it
= draw_bitmaps_
.begin();
105 it
!= draw_bitmaps_
.end(); ++it
) {
106 if (!it
->transform
.IsIdentity()) {
107 auto* item
= display_list
->CreateAndAppendItem
<TransformDisplayItem
>();
108 item
->SetNew(it
->transform
);
110 canvas
= skia::SharePtr(
111 recorder
.beginRecording(it
->bitmap
.width(), it
->bitmap
.height()));
112 canvas
->drawBitmap(it
->bitmap
, it
->point
.x(), it
->point
.y(), &it
->paint
);
113 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
114 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
115 item
->SetNew(picture
.Pass());
116 if (!it
->transform
.IsIdentity()) {
117 display_list
->CreateAndAppendItem
<EndTransformDisplayItem
>();
121 if (fill_with_nonsolid_color_
) {
122 gfx::RectF draw_rect
= clip
;
124 while (!draw_rect
.IsEmpty()) {
126 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
127 canvas
= skia::SharePtr(
128 recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
129 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
130 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
131 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
132 item
->SetNew(picture
.Pass());
133 draw_rect
.Inset(1, 1);
137 display_list
->CreateAndAppendItem
<EndClipDisplayItem
>();
139 display_list
->Finalize();
143 bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }
145 size_t FakeContentLayerClient::GetApproximateUnsharedMemoryUsage() const {
146 return reported_memory_usage_
;