1 // Copyright 2013 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_LAYER_TREE_PIXEL_TEST_H_
6 #define CC_TEST_LAYER_TREE_PIXEL_TEST_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "cc/test/layer_tree_test.h"
15 #include "cc/trees/clip_node.h"
16 #include "cc/trees/effect_node.h"
17 #include "cc/trees/scroll_node.h"
18 #include "cc/trees/transform_node.h"
19 #include "components/viz/common/resources/single_release_callback.h"
20 #include "ui/gl/gl_implementation.h"
21 
22 class SkBitmap;
23 
24 namespace base {
25 namespace test {
26 class ScopedFeatureList;
27 }
28 }  // namespace base
29 
30 namespace gfx {
31 class ColorSpace;
32 }
33 
34 namespace viz {
35 class CopyOutputRequest;
36 class CopyOutputResult;
37 }
38 
39 namespace cc {
40 class PixelComparator;
41 class SolidColorLayer;
42 class TextureLayer;
43 
44 class LayerTreePixelTest : public LayerTreeTest {
45  protected:
46   explicit LayerTreePixelTest(viz::RendererType renderer_type);
47   ~LayerTreePixelTest() override;
48 
49   // LayerTreeTest overrides.
50   std::unique_ptr<TestLayerTreeFrameSink> CreateLayerTreeFrameSink(
51       const viz::RendererSettings& renderer_settings,
52       double refresh_rate,
53       scoped_refptr<viz::ContextProvider> compositor_context_provider,
54       scoped_refptr<viz::RasterContextProvider> worker_context_provider)
55       override;
56   std::unique_ptr<viz::DisplayCompositorMemoryAndTaskController>
57   CreateDisplayControllerOnThread() override;
58   std::unique_ptr<viz::SkiaOutputSurface>
59   CreateDisplaySkiaOutputSurfaceOnThread(
60       viz::DisplayCompositorMemoryAndTaskController*) override;
61   std::unique_ptr<viz::OutputSurface> CreateDisplayOutputSurfaceOnThread(
62       scoped_refptr<viz::ContextProvider> compositor_context_provider) override;
63   void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override;
64   void InitializeSettings(LayerTreeSettings* settings) override;
65 
66   virtual std::unique_ptr<viz::CopyOutputRequest> CreateCopyOutputRequest();
67 
68   void ReadbackResult(std::unique_ptr<viz::CopyOutputResult> result);
69 
70   void BeginTest() override;
71   void SetupTree() override;
72   void AfterTest() override;
73   void EndTest() override;
74 
75   void TryEndTest();
76 
77   scoped_refptr<SolidColorLayer> CreateSolidColorLayer(const gfx::Rect& rect,
78                                                        SkColor color);
79   scoped_refptr<SolidColorLayer> CreateSolidColorLayerWithBorder(
80       const gfx::Rect& rect,
81       SkColor color,
82       int border_width,
83       SkColor border_color);
84 
85   void CreateSolidColorLayerPlusBorders(
86       const gfx::Rect& rect,
87       SkColor color,
88       int border_width,
89       SkColor border_color,
90       std::vector<scoped_refptr<SolidColorLayer>>&);
91 
92   void RunPixelTest(scoped_refptr<Layer> content_root,
93                     base::FilePath file_name);
94 
95   void RunPixelTest(scoped_refptr<Layer> content_root,
96                     const SkBitmap& expected_bitmap);
97 
98   void RunPixelTestWithLayerList(base::FilePath file_name);
99 
100   void RunSingleThreadedPixelTest(scoped_refptr<Layer> content_root,
101                                   base::FilePath file_name);
102 
103   void RunPixelTestWithReadbackTarget(scoped_refptr<Layer> content_root,
104                                       Layer* target,
105                                       base::FilePath file_name);
106 
107   SkBitmap CopyMailboxToBitmap(const gfx::Size& size,
108                                const gpu::Mailbox& mailbox,
109                                const gpu::SyncToken& sync_token,
110                                const gfx::ColorSpace& color_space);
111 
112   void Finish();
113 
114   // Allow tests to enlarge the backing texture for a non-root render pass, to
115   // simulate reusing a larger texture from a previous frame for a new
116   // render pass. This should be called before the output surface is bound.
set_enlarge_texture_amount(const gfx::Size & enlarge_texture_amount)117   void set_enlarge_texture_amount(const gfx::Size& enlarge_texture_amount) {
118     enlarge_texture_amount_ = enlarge_texture_amount;
119   }
120 
121   // Gpu rasterization is not used in pixel tests by default, and OOP
122   // rasterization is used by default only for Vulkan and Skia Dawn. Tests may
123   // opt into using a different raster mode.
set_raster_type(TestRasterType raster_type)124   void set_raster_type(TestRasterType raster_type) {
125     raster_type_ = raster_type;
126   }
127 
raster_type()128   TestRasterType raster_type() const { return raster_type_; }
use_accelerated_raster()129   bool use_accelerated_raster() const {
130     return raster_type_ == TestRasterType::kGpu ||
131            raster_type_ == TestRasterType::kOop;
132   }
133 
134   // Common CSS colors defined for tests to use.
135   static const SkColor kCSSOrange = 0xffffa500;
136   static const SkColor kCSSBrown = 0xffa52a2a;
137   static const SkColor kCSSGreen = 0xff008000;
138   static const SkColor kCSSLime = 0xff00ff00;
139   static const SkColor kCSSBlack = 0xff000000;
140 
141   TestRasterType raster_type_;
142   gl::DisableNullDrawGLBindings enable_pixel_output_;
143   std::unique_ptr<PixelComparator> pixel_comparator_;
144   scoped_refptr<Layer> content_root_;  // Not used in layer list mode.
145   Layer* readback_target_;
146   base::FilePath ref_file_;
147   SkBitmap expected_bitmap_;
148   std::unique_ptr<SkBitmap> result_bitmap_;
149   std::vector<scoped_refptr<TextureLayer>> texture_layers_;
150   int pending_texture_mailbox_callbacks_;
151   gfx::Size enlarge_texture_amount_;
152 
153   // Used to create SkiaOutputSurfaceImpl.
154   std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
155 };
156 
157 }  // namespace cc
158 
159 #endif  // CC_TEST_LAYER_TREE_PIXEL_TEST_H_
160