Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(934)

Side by Side Diff: chrome/test/gpu/gpu_pixel_browsertest.cc

Issue 4723006: Add test fixture for GPU browser tests which do image comparisons.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 <string>
6 #include <vector>
7
8 #include "app/app_switches.h"
9 #include "app/gfx/gl/gl_implementation.h"
10 #include "base/command_line.h"
11 #include "base/file_path.h"
12 #include "base/file_util.h"
13 #include "base/path_service.h"
14 #include "base/string_util.h"
15 #include "base/stringprintf.h"
16 #include "chrome/browser/browser.h"
17 #include "chrome/browser/browser_window.h"
18 #include "chrome/browser/gpu_process_host.h"
19 #include "chrome/browser/renderer_host/render_view_host.h"
20 #include "chrome/browser/tab_contents/tab_contents.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/gpu_info.h"
23 #include "chrome/test/in_process_browser_test.h"
24 #include "chrome/test/test_launcher_utils.h"
25 #include "chrome/test/ui_test_utils.h"
26 #include "gfx/codec/png_codec.h"
27 #include "gfx/size.h"
28 #include "googleurl/src/gurl.h"
29 #include "net/base/net_util.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "third_party/skia/include/core/SkBitmap.h"
32 #include "third_party/skia/include/core/SkColor.h"
33
34 namespace {
35
36 // Command line flag for forcing the machine's GPU to be used instead of OSMesa.
37 const char kUseGpuInTests[] = "use-gpu-in-tests";
38
39 // Reads and decodes a PNG image to a bitmap. Returns true on success. The PNG
40 // should have been encoded using |gfx::PNGCodec::Encode|.
41 bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) {
42 DCHECK(bitmap);
43 std::string png_data;
44 return file_util::ReadFileToString(file_path, &png_data) &&
45 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]),
46 png_data.length(),
47 bitmap);
48 }
49
50 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The
51 // parent directory does not have to exist.
52 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) {
53 std::vector<unsigned char> png_data;
54 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) &&
55 file_util::CreateDirectory(file_path.DirName())) {
56 int bytes_written = file_util::WriteFile(
57 file_path, reinterpret_cast<char*>(&png_data[0]), png_data.size());
58 if (bytes_written == static_cast<int>(png_data.size()))
59 return true;
60 }
61 return false;
62 }
63
64 // Resizes the browser window so that the tab's contents are at a given size.
65 void ResizeTabContainer(Browser* browser, const gfx::Size& desired_size) {
66 gfx::Rect container_rect;
67 browser->GetSelectedTabContents()->GetContainerBounds(&container_rect);
68 // Size cannot be negative, so use a point.
69 gfx::Point correction(desired_size.width() - container_rect.size().width(),
70 desired_size.height() - container_rect.size().height());
71
72 gfx::Rect window_rect = browser->window()->GetRestoredBounds();
73 gfx::Size new_size = window_rect.size();
74 new_size.Enlarge(correction.x(), correction.y());
75 window_rect.set_size(new_size);
76 browser->window()->SetBounds(window_rect);
77 }
78
79 } // namespace
80
81 // Test fixture for GPU image comparison tests.
82 // TODO(kkania): Document how to add to/modify these tests.
83 class GpuPixelBrowserTest : public InProcessBrowserTest {
84 public:
85 GpuPixelBrowserTest() {}
86
87 virtual void SetUpCommandLine(CommandLine* command_line) {
88 // This enables DOM automation for tab contents.
89 EnableDOMAutomation();
90
91 // These tests by default use OSMesa. This can be changed if the |kUseGL|
92 // switch is explicitly set to something else or if |kUseGpuInTests| is
93 // present.
94 if (command_line->HasSwitch(switches::kUseGL)) {
95 using_gpu_ = command_line->GetSwitchValueASCII(switches::kUseGL) !=
96 gfx::kGLImplementationOSMesaName;
97 } else if (command_line->HasSwitch(kUseGpuInTests)) {
98 using_gpu_ = true;
99 } else {
100 // OSMesa will be used by default.
101 EXPECT_TRUE(test_launcher_utils::OverrideGLImplementation(
102 command_line,
103 gfx::kGLImplementationOSMesaName));
104 #if defined(OS_MACOSX)
105 // Accelerated compositing does not work with OSMesa. AcceleratedSurface
106 // assumes GL contexts are native.
107 command_line->AppendSwitch(switches::kDisableAcceleratedCompositing);
108 #endif
109 using_gpu_ = false;
110 }
111 }
112
113 virtual void SetUpInProcessBrowserTestFixture() {
114 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
115 test_data_dir_ = test_data_dir_.AppendASCII("gpu");
116 generated_img_dir_ = test_data_dir_.AppendASCII("generated");
117 if (using_gpu_)
118 reference_img_dir_ = test_data_dir_.AppendASCII("gpu_reference");
119 else
120 reference_img_dir_ = test_data_dir_.AppendASCII("sw_reference");
121
122 test_name_ = testing::UnitTest::GetInstance()->current_test_info()->name();
123 const char* test_status_prefixes[] = {"DISABLED_", "FLAKY_", "FAILS_"};
124 for (size_t i = 0; i < arraysize(test_status_prefixes); ++i) {
125 ReplaceFirstSubstringAfterOffset(
126 &test_name_, 0, test_status_prefixes[i], "");
127 }
128 }
129
130 // Compares the generated bitmap with the appropriate reference image on disk.
131 // The reference image is determined by the name of the test, the |postfix|,
132 // and the vendor and device ID. Returns true iff the images were the same.
133 //
134 // |postfix|, is an optional name that can be appended onto the images name to
135 // distinguish between images from a single test.
136 //
137 // On failure, the image will be written to disk, and can be collected as a
138 // baseline. The image format is:
139 // <test_name>_<postfix>_<os>_<vendor_id>-<device_id>.png
140 // E.g.,
141 // WebGLFishTank_FirstTab_120981-1201.png
142 bool CompareImages(const SkBitmap& gen_bmp, const std::string& postfix) {
143 // Determine the name of the image.
144 std::string img_name = test_name_;
145 if (postfix.length())
146 img_name += "_" + postfix;
147 #if defined(OS_WIN)
148 const char* os_label = "win";
149 #elif defined(OS_MACOSX)
150 const char* os_label = "mac";
151 #elif defined(OS_LINUX)
152 const char* os_label = "linux";
153 #else
154 #error "Not implemented for this platform"
155 #endif
156 if (using_gpu_) {
157 GPUInfo info = GpuProcessHost::Get()->gpu_info();
158 if (!info.initialized()) {
159 LOG(ERROR) << "Could not get gpu info";
160 return false;
161 }
162 img_name = StringPrintf("%s_%s_%x-%x.png",
163 img_name.c_str(), os_label, info.vendor_id(), info.device_id());
164 } else {
165 img_name = StringPrintf("%s_%s_mesa.png", img_name.c_str(), os_label);
166 }
167
168 // Read the reference image and verify the images' dimensions are equal.
169 FilePath ref_img_path = reference_img_dir_.AppendASCII(img_name);
170 SkBitmap ref_bmp;
171 bool should_compare = true;
172 if (!ReadPNGFile(ref_img_path, &ref_bmp)) {
173 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value();
174 should_compare = false;
175 }
176 if (should_compare &&
177 (ref_bmp.width() != gen_bmp.width() ||
178 ref_bmp.height() != gen_bmp.height())) {
179 LOG(ERROR)
180 << "Dimensions do not match (Expected) vs (Actual):"
181 << "(" << ref_bmp.width() << "x" << ref_bmp.height()
182 << ") vs. "
183 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")";
184 should_compare = false;
185 }
186
187 // Compare pixels and create a simple diff image.
188 int diff_pixels_count = 0;
189 SkBitmap diff_bmp;
190 if (should_compare) {
191 diff_bmp.setConfig(SkBitmap::kARGB_8888_Config,
192 gen_bmp.width(), gen_bmp.height());
193 diff_bmp.allocPixels();
194 diff_bmp.eraseColor(SK_ColorWHITE);
195 SkAutoLockPixels lock_bmp(gen_bmp);
196 SkAutoLockPixels lock_ref_bmp(ref_bmp);
197 SkAutoLockPixels lock_diff_bmp(diff_bmp);
198 // The reference images were saved with no alpha channel. Use the mask to
199 // set alpha to 0.
200 uint32_t kAlphaMask = 0x00FFFFFF;
201 for (int x = 0; x < gen_bmp.width(); ++x) {
202 for (int y = 0; y < gen_bmp.height(); ++y) {
203 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) !=
204 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) {
205 ++diff_pixels_count;
206 *diff_bmp.getAddr32(x, y) = 192 << 16; // red
207 }
208 }
209 }
210 }
211
212 // Write the generated and diff images if the comparison failed.
213 if (!should_compare || diff_pixels_count != 0) {
214 FilePath gen_img_path = generated_img_dir_.AppendASCII(img_name);
215 if (WritePNGFile(gen_bmp, gen_img_path)) {
216 // Output the generated image path to the log for easy parsing later.
217 std::cout << "*GEN_IMG=" << gen_img_path.value() << std::endl;
218 } else {
219 LOG(WARNING) << "Could not write generated image: "
220 << gen_img_path.value();
221 }
222 if (should_compare) {
223 WritePNGFile(diff_bmp,
224 generated_img_dir_.AppendASCII("diff-" + img_name));
225 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count;
226 }
227 return false;
228 }
229 return true;
230 }
231
232 protected:
233 FilePath test_data_dir_;
234
235 private:
236 FilePath reference_img_dir_;
237 FilePath generated_img_dir_;
238 // The name of the test, with any special prefixes dropped.
239 std::string test_name_;
240 // Whether the gpu, or OSMesa is being used for rendering.
241 bool using_gpu_;
242
243 DISALLOW_COPY_AND_ASSIGN(GpuPixelBrowserTest);
244 };
245
246 // This test does not run on Linux because the information about the gfx card is
247 // not available yet.
248 #if !defined(OS_LINUX)
249 // Mark this as failing because we don't have the reference images yet. Once the
250 // images are generated on the bots, they will be uploaded and this status will
251 // be changed.
252 #define MAYBE_WebGLTeapot FAILS_WebGLTeapot
253 #else
254 #define MAYBE_WebGLTeapot DISABLED_WebGLTeapot
255 #endif
256 IN_PROC_BROWSER_TEST_F(GpuPixelBrowserTest, MAYBE_WebGLTeapot) {
257 ui_test_utils::DOMMessageQueue message_queue;
258 ui_test_utils::NavigateToURL(
259 browser(),
260 net::FilePathToFileURL(test_data_dir_.AppendASCII("webgl_teapot").
261 AppendASCII("teapot.html")));
262
263 // Wait for message from teapot page indicating the GL calls have been issued.
264 ASSERT_TRUE(message_queue.WaitForMessage(NULL));
265
266 SkBitmap bitmap;
267 gfx::Size container_size(500, 500);
268 ResizeTabContainer(browser(), container_size);
269 ASSERT_TRUE(ui_test_utils::TakeRenderWidgetSnapshot(
270 browser()->GetSelectedTabContents()->render_view_host(),
271 container_size, &bitmap));
272 ASSERT_TRUE(CompareImages(bitmap, ""));
273 }
OLDNEW
« no previous file with comments | « chrome/test/data/gpu/webgl_teapot/teapot_files/teapot-streams.js ('k') | chrome/test/ui_test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698