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.
4 
5 #ifndef CC_TEST_FAKE_CONTENT_LAYER_CLIENT_H_
6 #define CC_TEST_FAKE_CONTENT_LAYER_CLIENT_H_
7 
8 #include <stddef.h>
9 
10 #include <utility>
11 #include <vector>
12 
13 #include "base/compiler_specific.h"
14 #include "cc/layers/content_layer_client.h"
15 #include "cc/paint/paint_flags.h"
16 #include "cc/paint/paint_image_builder.h"
17 #include "third_party/skia/include/core/SkImage.h"
18 #include "third_party/skia/include/core/SkRefCnt.h"
19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/geometry/rect_f.h"
21 #include "ui/gfx/transform.h"
22 
23 namespace cc {
24 
25 class FakeContentLayerClient : public ContentLayerClient {
26  public:
27   struct ImageData {
28     ImageData(PaintImage image,
29               const gfx::Point& point,
30               const PaintFlags& flags);
31     ImageData(PaintImage image,
32               const gfx::Transform& transform,
33               const PaintFlags& flags);
34     ImageData(const ImageData& other);
35     ~ImageData();
36     PaintImage image;
37     gfx::Point point;
38     gfx::Transform transform;
39     PaintFlags flags;
40   };
41 
42   FakeContentLayerClient();
43   ~FakeContentLayerClient() override;
44 
45   gfx::Rect PaintableRegion() override;
46   scoped_refptr<DisplayItemList> PaintContentsToDisplayList() override;
47   bool FillsBoundsCompletely() const override;
48 
set_fill_with_nonsolid_color(bool nonsolid)49   void set_fill_with_nonsolid_color(bool nonsolid) {
50     fill_with_nonsolid_color_ = nonsolid;
51   }
52 
set_contains_slow_paths(bool contains_slow_paths)53   void set_contains_slow_paths(bool contains_slow_paths) {
54     contains_slow_paths_ = contains_slow_paths;
55   }
56 
set_has_non_aa_paint(bool has_non_aa_paint)57   void set_has_non_aa_paint(bool has_non_aa_paint) {
58     has_non_aa_paint_ = has_non_aa_paint;
59   }
60 
set_has_draw_text_op()61   void set_has_draw_text_op() { has_draw_text_op_ = true; }
62 
add_draw_rect(const gfx::Rect & rect,const PaintFlags & flags)63   void add_draw_rect(const gfx::Rect& rect, const PaintFlags& flags) {
64     draw_rects_.push_back(std::make_pair(gfx::RectF(rect), flags));
65   }
66 
add_draw_rectf(const gfx::RectF & rect,const PaintFlags & flags)67   void add_draw_rectf(const gfx::RectF& rect, const PaintFlags& flags) {
68     draw_rects_.push_back(std::make_pair(rect, flags));
69   }
70 
add_draw_image(sk_sp<SkImage> image,const gfx::Point & point,const PaintFlags & flags)71   void add_draw_image(sk_sp<SkImage> image,
72                       const gfx::Point& point,
73                       const PaintFlags& flags) {
74     add_draw_image(
75         PaintImageBuilder::WithDefault()
76             .set_id(PaintImage::GetNextId())
77             .set_image(std::move(image), PaintImage::GetNextContentId())
78             .TakePaintImage(),
79         point, flags);
80   }
add_draw_image(PaintImage image,const gfx::Point & point,const PaintFlags & flags)81   void add_draw_image(PaintImage image,
82                       const gfx::Point& point,
83                       const PaintFlags& flags) {
84     ImageData data(std::move(image), point, flags);
85     draw_images_.push_back(data);
86   }
87 
add_draw_image_with_transform(PaintImage image,const gfx::Transform & transform,const PaintFlags & flags)88   void add_draw_image_with_transform(PaintImage image,
89                                      const gfx::Transform& transform,
90                                      const PaintFlags& flags) {
91     ImageData data(std::move(image), transform, flags);
92     draw_images_.push_back(data);
93   }
94 
last_canvas()95   SkCanvas* last_canvas() const { return last_canvas_; }
96 
set_bounds(gfx::Size bounds)97   void set_bounds(gfx::Size bounds) {
98     bounds_ = bounds;
99     bounds_set_ = true;
100   }
101 
102  private:
103   using RectPaintVector = std::vector<std::pair<gfx::RectF, PaintFlags>>;
104   using ImageVector = std::vector<ImageData>;
105 
106   bool fill_with_nonsolid_color_ = false;
107   RectPaintVector draw_rects_;
108   ImageVector draw_images_;
109   SkCanvas* last_canvas_ = nullptr;
110   gfx::Size bounds_;
111   bool bounds_set_ = false;
112   bool contains_slow_paths_ = false;
113   bool has_non_aa_paint_ = false;
114   bool has_draw_text_op_ = false;
115 };
116 
117 }  // namespace cc
118 
119 #endif  // CC_TEST_FAKE_CONTENT_LAYER_CLIENT_H_
120