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/display_item_list_settings.h"
9 #include "cc/playback/drawing_display_item.h"
10 #include "cc/playback/transform_display_item.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "third_party/skia/include/core/SkPictureRecorder.h"
13 #include "ui/gfx/skia_util.h"
17 FakeContentLayerClient::ImageData::ImageData(const SkImage
* img
,
18 const gfx::Point
& point
,
20 : image(skia::SharePtr(img
)), point(point
), paint(paint
) {}
22 FakeContentLayerClient::ImageData::ImageData(const SkImage
* img
,
23 const gfx::Transform
& transform
,
25 : image(skia::SharePtr(img
)), transform(transform
), paint(paint
) {}
27 FakeContentLayerClient::ImageData::~ImageData() {}
29 FakeContentLayerClient::FakeContentLayerClient()
30 : fill_with_nonsolid_color_(false),
31 last_canvas_(nullptr),
32 last_painting_control_(PAINTING_BEHAVIOR_NORMAL
),
33 reported_memory_usage_(0) {}
35 FakeContentLayerClient::~FakeContentLayerClient() {
38 void FakeContentLayerClient::PaintContents(
40 const gfx::Rect
& paint_rect
,
41 PaintingControlSetting painting_control
) {
42 last_canvas_
= canvas
;
43 last_painting_control_
= painting_control
;
45 canvas
->clipRect(gfx::RectToSkRect(paint_rect
));
46 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
47 it
!= draw_rects_
.end(); ++it
) {
48 const gfx::RectF
& draw_rect
= it
->first
;
49 const SkPaint
& paint
= it
->second
;
50 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
53 for (ImageVector::const_iterator it
= draw_images_
.begin();
54 it
!= draw_images_
.end(); ++it
) {
55 canvas
->drawImage(it
->image
.get(), it
->point
.x(), it
->point
.y(),
59 if (fill_with_nonsolid_color_
) {
60 gfx::Rect draw_rect
= paint_rect
;
62 while (!draw_rect
.IsEmpty()) {
64 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
65 canvas
->drawIRect(gfx::RectToSkIRect(draw_rect
), paint
);
66 draw_rect
.Inset(1, 1);
71 scoped_refptr
<DisplayItemList
>
72 FakeContentLayerClient::PaintContentsToDisplayList(
73 const gfx::Rect
& clip
,
74 PaintingControlSetting painting_control
) {
75 // Cached picture is used because unit tests expect to be able to
76 // use GatherPixelRefs.
77 DisplayItemListSettings settings
;
78 settings
.use_cached_picture
= true;
79 scoped_refptr
<DisplayItemList
> display_list
=
80 DisplayItemList::Create(clip
, settings
);
81 SkPictureRecorder recorder
;
82 skia::RefPtr
<SkCanvas
> canvas
;
83 skia::RefPtr
<SkPicture
> picture
;
84 auto* item
= display_list
->CreateAndAppendItem
<ClipDisplayItem
>();
85 item
->SetNew(clip
, std::vector
<SkRRect
>());
87 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
88 it
!= draw_rects_
.end(); ++it
) {
89 const gfx::RectF
& draw_rect
= it
->first
;
90 const SkPaint
& paint
= it
->second
;
92 skia::SharePtr(recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
93 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
94 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
95 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
96 item
->SetNew(picture
.Pass());
99 for (ImageVector::const_iterator it
= draw_images_
.begin();
100 it
!= draw_images_
.end(); ++it
) {
101 if (!it
->transform
.IsIdentity()) {
102 auto* item
= display_list
->CreateAndAppendItem
<TransformDisplayItem
>();
103 item
->SetNew(it
->transform
);
105 canvas
= skia::SharePtr(
106 recorder
.beginRecording(it
->image
->width(), it
->image
->height()));
107 canvas
->drawImage(it
->image
.get(), it
->point
.x(), it
->point
.y(),
109 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
110 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
111 item
->SetNew(picture
.Pass());
112 if (!it
->transform
.IsIdentity()) {
113 display_list
->CreateAndAppendItem
<EndTransformDisplayItem
>();
117 if (fill_with_nonsolid_color_
) {
118 gfx::Rect draw_rect
= clip
;
120 while (!draw_rect
.IsEmpty()) {
122 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
124 skia::SharePtr(recorder
.beginRecording(gfx::RectToSkRect(draw_rect
)));
125 canvas
->drawIRect(gfx::RectToSkIRect(draw_rect
), paint
);
126 picture
= skia::AdoptRef(recorder
.endRecordingAsPicture());
127 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
128 item
->SetNew(picture
.Pass());
129 draw_rect
.Inset(1, 1);
133 display_list
->CreateAndAppendItem
<EndClipDisplayItem
>();
135 display_list
->Finalize();
139 bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }
141 size_t FakeContentLayerClient::GetApproximateUnsharedMemoryUsage() const {
142 return reported_memory_usage_
;