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/resources/clip_display_item.h"
8 #include "cc/resources/drawing_display_item.h"
9 #include "cc/resources/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), last_canvas_(NULL
) {
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
->drawRectCoords(draw_rect
.x(),
57 for (BitmapVector::const_iterator it
= draw_bitmaps_
.begin();
58 it
!= draw_bitmaps_
.end(); ++it
) {
59 canvas
->drawBitmap(it
->bitmap
, it
->point
.x(), it
->point
.y(), &it
->paint
);
62 if (fill_with_nonsolid_color_
) {
63 gfx::RectF draw_rect
= paint_rect
;
65 while (!draw_rect
.IsEmpty()) {
67 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
68 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
69 draw_rect
.Inset(1, 1);
74 scoped_refptr
<DisplayItemList
>
75 FakeContentLayerClient::PaintContentsToDisplayList(
76 const gfx::Rect
& clip
,
77 PaintingControlSetting painting_control
) {
78 SkPictureRecorder recorder
;
79 skia::RefPtr
<SkCanvas
> canvas
;
80 skia::RefPtr
<SkPicture
> picture
;
81 scoped_refptr
<DisplayItemList
> list
= DisplayItemList::Create();
82 list
->AppendItem(ClipDisplayItem::Create(clip
, std::vector
<SkRRect
>()));
84 for (RectPaintVector::const_iterator it
= draw_rects_
.begin();
85 it
!= draw_rects_
.end(); ++it
) {
86 const gfx::RectF
& draw_rect
= it
->first
;
87 const SkPaint
& paint
= it
->second
;
89 skia::SharePtr(recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
90 canvas
->drawRectCoords(draw_rect
.x(), draw_rect
.y(), draw_rect
.width(),
91 draw_rect
.height(), paint
);
92 picture
= skia::AdoptRef(recorder
.endRecording());
93 list
->AppendItem(DrawingDisplayItem::Create(picture
));
96 for (BitmapVector::const_iterator it
= draw_bitmaps_
.begin();
97 it
!= draw_bitmaps_
.end(); ++it
) {
98 if (!it
->transform
.IsIdentity()) {
99 list
->AppendItem(TransformDisplayItem::Create(it
->transform
));
101 canvas
= skia::SharePtr(
102 recorder
.beginRecording(it
->bitmap
.width(), it
->bitmap
.height()));
103 canvas
->drawBitmap(it
->bitmap
, it
->point
.x(), it
->point
.y(), &it
->paint
);
104 picture
= skia::AdoptRef(recorder
.endRecording());
105 list
->AppendItem(DrawingDisplayItem::Create(picture
));
106 if (!it
->transform
.IsIdentity()) {
107 list
->AppendItem(EndTransformDisplayItem::Create());
111 if (fill_with_nonsolid_color_
) {
112 gfx::RectF draw_rect
= clip
;
114 while (!draw_rect
.IsEmpty()) {
116 paint
.setColor(red
? SK_ColorRED
: SK_ColorBLUE
);
117 canvas
= skia::SharePtr(
118 recorder
.beginRecording(gfx::RectFToSkRect(draw_rect
)));
119 canvas
->drawRect(gfx::RectFToSkRect(draw_rect
), paint
);
120 picture
= skia::AdoptRef(recorder
.endRecording());
121 list
->AppendItem(DrawingDisplayItem::Create(picture
));
122 draw_rect
.Inset(1, 1);
126 list
->AppendItem(EndClipDisplayItem::Create());
130 bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }