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 #include "base/file_util.h" 6 #include "cc/output/gl_renderer.h" 7 #include "cc/output/software_renderer.h" 8 #include "cc/quads/render_pass.h" 9 #include "cc/test/pixel_comparator.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "ui/gfx/size.h" 12 13 #ifndef CC_TEST_PIXEL_TEST_H_ 14 #define CC_TEST_PIXEL_TEST_H_ 15 16 namespace cc { 17 class CopyOutputResult; 18 class DirectRenderer; 19 class FakeOutputSurfaceClient; 20 class OutputSurface; 21 class ResourceProvider; 22 class SoftwareRenderer; 23 24 class PixelTest : public testing::Test, RendererClient { 25 protected: 26 PixelTest(); 27 virtual ~PixelTest(); 28 29 enum OffscreenContextOption { 30 NoOffscreenContext, 31 WithOffscreenContext 32 }; 33 34 bool RunPixelTest(RenderPassList* pass_list, 35 OffscreenContextOption provide_offscreen_context, 36 const base::FilePath& ref_file, 37 const PixelComparator& comparator); 38 39 bool RunPixelTestWithReadbackTarget( 40 RenderPassList* pass_list, 41 RenderPass* target, 42 OffscreenContextOption provide_offscreen_context, 43 const base::FilePath& ref_file, 44 const PixelComparator& comparator); 45 46 LayerTreeSettings settings_; 47 gfx::Size device_viewport_size_; 48 bool disable_picture_quad_image_filtering_; 49 class PixelTestRendererClient; 50 scoped_ptr<FakeOutputSurfaceClient> output_surface_client_; 51 scoped_ptr<OutputSurface> output_surface_; 52 scoped_ptr<ResourceProvider> resource_provider_; 53 scoped_ptr<TextureMailboxDeleter> texture_mailbox_deleter_; 54 scoped_ptr<DirectRenderer> renderer_; 55 scoped_ptr<SkBitmap> result_bitmap_; 56 gfx::Vector2d external_device_viewport_offset_; 57 gfx::Rect external_device_clip_rect_; 58 59 void SetUpGLRenderer(bool use_skia_gpu_backend); 60 void SetUpSoftwareRenderer(); 61 62 void ForceExpandedViewport(gfx::Size surface_expansion); 63 void ForceViewportOffset(gfx::Vector2d viewport_offset); 64 void ForceDeviceClip(gfx::Rect clip); 65 void EnableExternalStencilTest(); 66 67 // RendererClient implementation. 68 virtual void SetFullRootLayerDamage() OVERRIDE {} 69 70 private: 71 void ReadbackResult(base::Closure quit_run_loop, 72 scoped_ptr<CopyOutputResult> result); 73 74 bool PixelsMatchReference(const base::FilePath& ref_file, 75 const PixelComparator& comparator); 76 }; 77 78 template<typename RendererType> 79 class RendererPixelTest : public PixelTest { 80 public: 81 RendererType* renderer() { 82 return static_cast<RendererType*>(renderer_.get()); 83 } 84 85 bool UseSkiaGPUBackend() const; 86 bool ExpandedViewport() const; 87 88 protected: 89 virtual void SetUp() OVERRIDE; 90 }; 91 92 // Wrappers to differentiate renderers where the the output surface and viewport 93 // have an externally determined size and offset. 94 class GLRendererWithExpandedViewport : public GLRenderer { 95 public: 96 GLRendererWithExpandedViewport(RendererClient* client, 97 const LayerTreeSettings* settings, 98 OutputSurface* output_surface, 99 ResourceProvider* resource_provider, 100 TextureMailboxDeleter* texture_mailbox_deleter, 101 int highp_threshold_min) 102 : GLRenderer(client, 103 settings, 104 output_surface, 105 resource_provider, 106 texture_mailbox_deleter, 107 highp_threshold_min) {} 108 }; 109 110 class SoftwareRendererWithExpandedViewport : public SoftwareRenderer { 111 public: 112 SoftwareRendererWithExpandedViewport(RendererClient* client, 113 const LayerTreeSettings* settings, 114 OutputSurface* output_surface, 115 ResourceProvider* resource_provider) 116 : SoftwareRenderer(client, settings, output_surface, resource_provider) {} 117 }; 118 119 template<> 120 inline void RendererPixelTest<GLRenderer>::SetUp() { 121 SetUpGLRenderer(false); 122 } 123 124 template<> 125 inline bool RendererPixelTest<GLRenderer>::UseSkiaGPUBackend() const { 126 return false; 127 } 128 129 template<> 130 inline bool RendererPixelTest<GLRenderer>::ExpandedViewport() const { 131 return false; 132 } 133 134 template<> 135 inline void RendererPixelTest<GLRendererWithExpandedViewport>::SetUp() { 136 SetUpGLRenderer(false); 137 ForceExpandedViewport(gfx::Size(50, 50)); 138 ForceViewportOffset(gfx::Vector2d(10, 20)); 139 } 140 141 template <> 142 inline bool 143 RendererPixelTest<GLRendererWithExpandedViewport>::UseSkiaGPUBackend() const { 144 return false; 145 } 146 147 template <> 148 inline bool 149 RendererPixelTest<GLRendererWithExpandedViewport>::ExpandedViewport() const { 150 return true; 151 } 152 153 template<> 154 inline void RendererPixelTest<SoftwareRenderer>::SetUp() { 155 SetUpSoftwareRenderer(); 156 } 157 158 template<> 159 inline bool RendererPixelTest<SoftwareRenderer>::UseSkiaGPUBackend() const { 160 return false; 161 } 162 163 template <> 164 inline bool RendererPixelTest<SoftwareRenderer>::ExpandedViewport() const { 165 return false; 166 } 167 168 template<> 169 inline void RendererPixelTest<SoftwareRendererWithExpandedViewport>::SetUp() { 170 SetUpSoftwareRenderer(); 171 ForceExpandedViewport(gfx::Size(50, 50)); 172 ForceViewportOffset(gfx::Vector2d(10, 20)); 173 } 174 175 template <> 176 inline bool RendererPixelTest< 177 SoftwareRendererWithExpandedViewport>::UseSkiaGPUBackend() const { 178 return false; 179 } 180 181 template <> 182 inline bool RendererPixelTest< 183 SoftwareRendererWithExpandedViewport>::ExpandedViewport() const { 184 return true; 185 } 186 187 typedef RendererPixelTest<GLRenderer> GLRendererPixelTest; 188 typedef RendererPixelTest<SoftwareRenderer> SoftwareRendererPixelTest; 189 190 } // namespace cc 191 192 #endif // CC_TEST_PIXEL_TEST_H_ 193