Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / cc / test / fake_content_layer_client.cc
blob0cbb92a780fa20544ad015e71e29c24a4ae26937
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"
14 namespace cc {
16 FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap& bitmap,
17 const gfx::Point& point,
18 const SkPaint& paint)
19 : bitmap(bitmap), point(point), paint(paint) {
22 FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap& bitmap,
23 const gfx::Transform& transform,
24 const SkPaint& paint)
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(
41 SkCanvas* canvas,
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->drawRect(gfx::RectFToSkRect(draw_rect), paint);
55 for (BitmapVector::const_iterator it = draw_bitmaps_.begin();
56 it != draw_bitmaps_.end(); ++it) {
57 canvas->drawBitmap(it->bitmap, it->point.x(), it->point.y(), &it->paint);
60 if (fill_with_nonsolid_color_) {
61 gfx::Rect draw_rect = paint_rect;
62 bool red = true;
63 while (!draw_rect.IsEmpty()) {
64 SkPaint paint;
65 paint.setColor(red ? SK_ColorRED : SK_ColorBLUE);
66 canvas->drawIRect(gfx::RectToSkIRect(draw_rect), paint);
67 draw_rect.Inset(1, 1);
72 scoped_refptr<DisplayItemList>
73 FakeContentLayerClient::PaintContentsToDisplayList(
74 const gfx::Rect& clip,
75 PaintingControlSetting painting_control) {
76 // Cached picture is used because unit tests expect to be able to
77 // use GatherPixelRefs.
78 const bool use_cached_picture = true;
79 scoped_refptr<DisplayItemList> display_list =
80 DisplayItemList::Create(clip, use_cached_picture);
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;
91 canvas =
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 (BitmapVector::const_iterator it = draw_bitmaps_.begin();
100 it != draw_bitmaps_.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->bitmap.width(), it->bitmap.height()));
107 canvas->drawBitmap(it->bitmap, it->point.x(), it->point.y(), &it->paint);
108 picture = skia::AdoptRef(recorder.endRecordingAsPicture());
109 auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
110 item->SetNew(picture.Pass());
111 if (!it->transform.IsIdentity()) {
112 display_list->CreateAndAppendItem<EndTransformDisplayItem>();
116 if (fill_with_nonsolid_color_) {
117 gfx::Rect draw_rect = clip;
118 bool red = true;
119 while (!draw_rect.IsEmpty()) {
120 SkPaint paint;
121 paint.setColor(red ? SK_ColorRED : SK_ColorBLUE);
122 canvas =
123 skia::SharePtr(recorder.beginRecording(gfx::RectToSkRect(draw_rect)));
124 canvas->drawIRect(gfx::RectToSkIRect(draw_rect), paint);
125 picture = skia::AdoptRef(recorder.endRecordingAsPicture());
126 auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
127 item->SetNew(picture.Pass());
128 draw_rect.Inset(1, 1);
132 display_list->CreateAndAppendItem<EndClipDisplayItem>();
134 display_list->Finalize();
135 return display_list;
138 bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }
140 size_t FakeContentLayerClient::GetApproximateUnsharedMemoryUsage() const {
141 return reported_memory_usage_;
144 } // namespace cc