summaryrefslogtreecommitdiffstats
path: root/cc/test/fake_content_layer_client.cc
blob: 789e21e8acf687e7754b2882456332948fc760e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/test/fake_content_layer_client.h"

#include "cc/playback/clip_display_item.h"
#include "cc/playback/drawing_display_item.h"
#include "cc/playback/transform_display_item.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "ui/gfx/skia_util.h"

namespace cc {

FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap& bitmap,
                                               const gfx::Point& point,
                                               const SkPaint& paint)
    : bitmap(bitmap), point(point), paint(paint) {
}

FakeContentLayerClient::BitmapData::BitmapData(const SkBitmap& bitmap,
                                               const gfx::Transform& transform,
                                               const SkPaint& paint)
    : bitmap(bitmap), transform(transform), paint(paint) {
}

FakeContentLayerClient::BitmapData::~BitmapData() {
}

FakeContentLayerClient::FakeContentLayerClient()
    : fill_with_nonsolid_color_(false),
      last_canvas_(nullptr),
      last_painting_control_(PAINTING_BEHAVIOR_NORMAL),
      reported_memory_usage_(0) {}

FakeContentLayerClient::~FakeContentLayerClient() {
}

void FakeContentLayerClient::PaintContents(
    SkCanvas* canvas,
    const gfx::Rect& paint_rect,
    PaintingControlSetting painting_control) {
  last_canvas_ = canvas;
  last_painting_control_ = painting_control;

  canvas->clipRect(gfx::RectToSkRect(paint_rect));
  for (RectPaintVector::const_iterator it = draw_rects_.begin();
      it != draw_rects_.end(); ++it) {
    const gfx::RectF& draw_rect = it->first;
    const SkPaint& paint = it->second;
    canvas->drawRectCoords(draw_rect.x(),
                           draw_rect.y(),
                           draw_rect.right(),
                           draw_rect.bottom(),
                           paint);
  }

  for (BitmapVector::const_iterator it = draw_bitmaps_.begin();
      it != draw_bitmaps_.end(); ++it) {
    canvas->drawBitmap(it->bitmap, it->point.x(), it->point.y(), &it->paint);
  }

  if (fill_with_nonsolid_color_) {
    gfx::RectF draw_rect = paint_rect;
    bool red = true;
    while (!draw_rect.IsEmpty()) {
      SkPaint paint;
      paint.setColor(red ? SK_ColorRED : SK_ColorBLUE);
      canvas->drawRect(gfx::RectFToSkRect(draw_rect), paint);
      draw_rect.Inset(1, 1);
    }
  }
}

scoped_refptr<DisplayItemList>
FakeContentLayerClient::PaintContentsToDisplayList(
    const gfx::Rect& clip,
    PaintingControlSetting painting_control) {
  // Cached picture is used because unit tests expect to be able to
  // use GatherPixelRefs.
  const bool use_cached_picture = true;
  scoped_refptr<DisplayItemList> display_list =
      DisplayItemList::Create(clip, use_cached_picture);
  SkPictureRecorder recorder;
  skia::RefPtr<SkCanvas> canvas;
  skia::RefPtr<SkPicture> picture;
  auto* item = display_list->CreateAndAppendItem<ClipDisplayItem>();
  item->SetNew(clip, std::vector<SkRRect>());

  for (RectPaintVector::const_iterator it = draw_rects_.begin();
       it != draw_rects_.end(); ++it) {
    const gfx::RectF& draw_rect = it->first;
    const SkPaint& paint = it->second;
    canvas =
        skia::SharePtr(recorder.beginRecording(gfx::RectFToSkRect(draw_rect)));
    canvas->drawRectCoords(draw_rect.x(), draw_rect.y(), draw_rect.width(),
                           draw_rect.height(), paint);
    picture = skia::AdoptRef(recorder.endRecordingAsPicture());
    auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
    item->SetNew(picture.Pass());
  }

  for (BitmapVector::const_iterator it = draw_bitmaps_.begin();
       it != draw_bitmaps_.end(); ++it) {
    if (!it->transform.IsIdentity()) {
      auto* item = display_list->CreateAndAppendItem<TransformDisplayItem>();
      item->SetNew(it->transform);
    }
    canvas = skia::SharePtr(
        recorder.beginRecording(it->bitmap.width(), it->bitmap.height()));
    canvas->drawBitmap(it->bitmap, it->point.x(), it->point.y(), &it->paint);
    picture = skia::AdoptRef(recorder.endRecordingAsPicture());
    auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
    item->SetNew(picture.Pass());
    if (!it->transform.IsIdentity()) {
      display_list->CreateAndAppendItem<EndTransformDisplayItem>();
    }
  }

  if (fill_with_nonsolid_color_) {
    gfx::RectF draw_rect = clip;
    bool red = true;
    while (!draw_rect.IsEmpty()) {
      SkPaint paint;
      paint.setColor(red ? SK_ColorRED : SK_ColorBLUE);
      canvas = skia::SharePtr(
          recorder.beginRecording(gfx::RectFToSkRect(draw_rect)));
      canvas->drawRect(gfx::RectFToSkRect(draw_rect), paint);
      picture = skia::AdoptRef(recorder.endRecordingAsPicture());
      auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
      item->SetNew(picture.Pass());
      draw_rect.Inset(1, 1);
    }
  }

  display_list->CreateAndAppendItem<EndClipDisplayItem>();

  display_list->Finalize();
  return display_list;
}

bool FakeContentLayerClient::FillsBoundsCompletely() const { return false; }

size_t FakeContentLayerClient::GetApproximateUnsharedMemoryUsage() const {
  return reported_memory_usage_;
}

}  // namespace cc