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::ImageData::ImageData(const SkImage
* img
,
17 const gfx::Point
& point
,
19 : image(skia::SharePtr(img
)), point(point
), paint(paint
) {}
21 FakeContentLayerClient::ImageData::ImageData(const SkImage
* img
,
22 const gfx::Transform
& transform
,
24 : image(skia::SharePtr(img
)), transform(transform
), paint(paint
) {}
26 FakeContentLayerClient::ImageData::~ImageData() {}
28 FakeContentLayerClient::FakeContentLayerClient()
29 : fill_with_nonsolid_color_(false),
30 last_canvas_(nullptr),
31 last_painting_control_(PAINTING_BEHAVIOR_NORMAL
),
32 reported_memory_usage_(0) {}
34 FakeContentLayerClient::~FakeContentLayerClient() {
37 void FakeContentLayerClient::PaintContents(
39 const gfx::Rect
& paint_rect
,
40 PaintingControlSetting painting_control
) {
41 last_canvas_
= canvas
;
42 last_painting_control_
= painting_control
;
44 canvas
->clipRect(gfx::RectToSkRect(paint_rect
));
45 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
46 it
!= draw_rects_
.end(); ++it
) {
47 const gfx::RectF
& draw_rect
= it
->first
;
48 const SkPaint
& paint
= it
->second
;
49 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
52 for (ImageVector::const_iterator it
= draw_images_
.begin();
53 it
!= draw_images_
.end(); ++it
) {
54 canvas
->drawImage(it
->image
.get(), it
->point
.x(), it
->point
.y(),
58 if (fill_with_nonsolid_color_
) {
59 gfx::Rect draw_rect
= paint_rect
;
61 while (!draw_rect
.IsEmpty()) {
63 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
64 canvas
->drawIRect(gfx::RectToSkIRect(draw_rect
), paint
);
65 draw_rect
.Inset(1, 1);
70 scoped_refptr
<DisplayItemList
>
71 FakeContentLayerClient::PaintContentsToDisplayList(
72 const gfx::Rect
& clip
,
73 PaintingControlSetting painting_control
) {
74 // Cached picture is used because unit tests expect to be able to
75 // use GatherPixelRefs.
76 const bool use_cached_picture
= true;
77 scoped_refptr
<DisplayItemList
> display_list
=
78 DisplayItemList::Create(clip
, use_cached_picture
);
79 SkPictureRecorder recorder
;
80 skia::RefPtr
<SkCanvas
> canvas
;
81 skia::RefPtr
<SkPicture
> picture
;
82 auto* item
= display_list
->CreateAndAppendItem
<ClipDisplayItem
>();
83 item
->SetNew(clip
, std::vector
<SkRRect
>());
85 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
86 it
!= draw_rects_
.end(); ++it
) {
87 const gfx::RectF
& draw_rect
= it
->first
;
88 const SkPaint
& paint
= it
->second
;
90 skia::SharePtr(recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
91 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
92 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
93 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
94 item
->SetNew(picture
.Pass());
97 for (ImageVector::const_iterator it
= draw_images_
.begin();
98 it
!= draw_images_
.end(); ++it
) {
99 if (!it
->transform
.IsIdentity()) {
100 auto* item
= display_list
->CreateAndAppendItem
<TransformDisplayItem
>();
101 item
->SetNew(it
->transform
);
103 canvas
= skia::SharePtr(
104 recorder
.beginRecording(it
->image
->width(), it
->image
->height()));
105 canvas
->drawImage(it
->image
.get(), it
->point
.x(), it
->point
.y(),
107 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
108 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
109 item
->SetNew(picture
.Pass());
110 if (!it
->transform
.IsIdentity()) {
111 display_list
->CreateAndAppendItem
<EndTransformDisplayItem
>();
115 if (fill_with_nonsolid_color_
) {
116 gfx::Rect draw_rect
= clip
;
118 while (!draw_rect
.IsEmpty()) {
120 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
122 skia::SharePtr(recorder
.beginRecording(gfx::RectToSkRect(draw_rect
)));
123 canvas
->drawIRect(gfx::RectToSkIRect(draw_rect
), paint
);
124 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
125 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
126 item
->SetNew(picture
.Pass());
127 draw_rect
.Inset(1, 1);
131 display_list
->CreateAndAppendItem
<EndClipDisplayItem
>();
133 display_list
->Finalize();
137 bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }
139 size_t FakeContentLayerClient::GetApproximateUnsharedMemoryUsage() const {
140 return reported_memory_usage_
;