| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "skia/ext/vector_canvas.h" | 5 #include "skia/ext/vector_canvas.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/gfx/gdi_util.h" | 11 #include "base/gfx/gdi_util.h" |
| 12 #include "base/gfx/png_decoder.h" | 12 #include "base/gfx/png_decoder.h" |
| 13 #include "base/gfx/png_encoder.h" | 13 #include "base/gfx/png_encoder.h" |
| 14 #include "base/gfx/size.h" | 14 #include "base/gfx/size.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/win_util.h" | 17 #include "base/win_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 #include "SkDashPathEffect.h" | 20 #include "SkDashPathEffect.h" |
| 21 | 21 |
| 22 namespace skia { |
| 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 const wchar_t* const kGenerateSwitch = L"vector-canvas-generate"; | 26 const wchar_t* const kGenerateSwitch = L"vector-canvas-generate"; |
| 25 | 27 |
| 26 // Base class for unit test that uses data. It initializes a directory path | 28 // Base class for unit test that uses data. It initializes a directory path |
| 27 // based on the test's name. | 29 // based on the test's name. |
| 28 class DataUnitTest : public testing::Test { | 30 class DataUnitTest : public testing::Test { |
| 29 public: | 31 public: |
| 30 DataUnitTest(const std::wstring& base_path) : base_path_(base_path) { } | 32 DataUnitTest(const std::wstring& base_path) : base_path_(base_path) { } |
| 31 | 33 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 int w; | 133 int w; |
| 132 int h; | 134 int h; |
| 133 EXPECT_TRUE(PNGDecoder::Decode( | 135 EXPECT_TRUE(PNGDecoder::Decode( |
| 134 reinterpret_cast<const unsigned char*>(compressed.c_str()), | 136 reinterpret_cast<const unsigned char*>(compressed.c_str()), |
| 135 compressed.size(), PNGDecoder::FORMAT_BGRA, &data_, &w, &h)); | 137 compressed.size(), PNGDecoder::FORMAT_BGRA, &data_, &w, &h)); |
| 136 size_.SetSize(w, h); | 138 size_.SetSize(w, h); |
| 137 row_length_ = w * sizeof(uint32); | 139 row_length_ = w * sizeof(uint32); |
| 138 } | 140 } |
| 139 | 141 |
| 140 // Loads the image from a canvas. | 142 // Loads the image from a canvas. |
| 141 Image(const gfx::PlatformCanvasWin& canvas) : ignore_alpha_(true) { | 143 Image(const skia::PlatformCanvasWin& canvas) : ignore_alpha_(true) { |
| 142 // Use a different way to access the bitmap. The normal way would be to | 144 // Use a different way to access the bitmap. The normal way would be to |
| 143 // query the SkBitmap. | 145 // query the SkBitmap. |
| 144 HDC context = canvas.getTopPlatformDevice().getBitmapDC(); | 146 HDC context = canvas.getTopPlatformDevice().getBitmapDC(); |
| 145 HGDIOBJ bitmap = GetCurrentObject(context, OBJ_BITMAP); | 147 HGDIOBJ bitmap = GetCurrentObject(context, OBJ_BITMAP); |
| 146 EXPECT_TRUE(bitmap != NULL); | 148 EXPECT_TRUE(bitmap != NULL); |
| 147 // Initialize the clip region to the entire bitmap. | 149 // Initialize the clip region to the entire bitmap. |
| 148 BITMAP bitmap_data; | 150 BITMAP bitmap_data; |
| 149 EXPECT_EQ(GetObject(bitmap, sizeof(BITMAP), &bitmap_data), | 151 EXPECT_EQ(GetObject(bitmap, sizeof(BITMAP), &bitmap_data), |
| 150 sizeof(BITMAP)); | 152 sizeof(BITMAP)); |
| 151 size_.SetSize(bitmap_data.bmWidth, bitmap_data.bmHeight); | 153 size_.SetSize(bitmap_data.bmWidth, bitmap_data.bmHeight); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 if (action_ == GENERATE) { | 272 if (action_ == GENERATE) { |
| 271 // Make sure the directory exist. | 273 // Make sure the directory exist. |
| 272 file_util::CreateDirectory(test_dir()); | 274 file_util::CreateDirectory(test_dir()); |
| 273 } | 275 } |
| 274 } | 276 } |
| 275 | 277 |
| 276 // Compares or saves the bitmap currently loaded in the context, depending on | 278 // Compares or saves the bitmap currently loaded in the context, depending on |
| 277 // kGenerating value. Returns 0 on success or any positive value between ]0, | 279 // kGenerating value. Returns 0 on success or any positive value between ]0, |
| 278 // 100] on failure. The return value is the percentage of difference between | 280 // 100] on failure. The return value is the percentage of difference between |
| 279 // the image in the file and the image in the canvas. | 281 // the image in the file and the image in the canvas. |
| 280 double ProcessCanvas(const gfx::PlatformCanvasWin& canvas, | 282 double ProcessCanvas(const skia::PlatformCanvasWin& canvas, |
| 281 std::wstring filename) const { | 283 std::wstring filename) const { |
| 282 filename += L".png"; | 284 filename += L".png"; |
| 283 switch (action_) { | 285 switch (action_) { |
| 284 case GENERATE: | 286 case GENERATE: |
| 285 SaveImage(canvas, filename); | 287 SaveImage(canvas, filename); |
| 286 return 0.; | 288 return 0.; |
| 287 case COMPARE: | 289 case COMPARE: |
| 288 return CompareImage(canvas, filename); | 290 return CompareImage(canvas, filename); |
| 289 case NOOP: | 291 case NOOP: |
| 290 return 0; | 292 return 0; |
| 291 default: | 293 default: |
| 292 // Invalid state, returns that the image is 100 different. | 294 // Invalid state, returns that the image is 100 different. |
| 293 return 100.; | 295 return 100.; |
| 294 } | 296 } |
| 295 } | 297 } |
| 296 | 298 |
| 297 // Compares the bitmap currently loaded in the context with the file. Returns | 299 // Compares the bitmap currently loaded in the context with the file. Returns |
| 298 // the percentage of pixel difference between both images, between 0 and 100. | 300 // the percentage of pixel difference between both images, between 0 and 100. |
| 299 double CompareImage(const gfx::PlatformCanvasWin& canvas, | 301 double CompareImage(const skia::PlatformCanvasWin& canvas, |
| 300 const std::wstring& filename) const { | 302 const std::wstring& filename) const { |
| 301 Image image1(canvas); | 303 Image image1(canvas); |
| 302 Image image2(test_file(filename)); | 304 Image image2(test_file(filename)); |
| 303 double diff = image1.PercentageDifferent(image2); | 305 double diff = image1.PercentageDifferent(image2); |
| 304 return diff; | 306 return diff; |
| 305 } | 307 } |
| 306 | 308 |
| 307 // Saves the bitmap currently loaded in the context into the file. | 309 // Saves the bitmap currently loaded in the context into the file. |
| 308 void SaveImage(const gfx::PlatformCanvasWin& canvas, | 310 void SaveImage(const skia::PlatformCanvasWin& canvas, |
| 309 const std::wstring& filename) const { | 311 const std::wstring& filename) const { |
| 310 Image(canvas).SaveToFile(test_file(filename)); | 312 Image(canvas).SaveToFile(test_file(filename)); |
| 311 } | 313 } |
| 312 | 314 |
| 313 ProcessAction action_; | 315 ProcessAction action_; |
| 314 | 316 |
| 315 DISALLOW_EVIL_CONSTRUCTORS(ImageTest); | 317 DISALLOW_EVIL_CONSTRUCTORS(ImageTest); |
| 316 }; | 318 }; |
| 317 | 319 |
| 318 // Premultiply the Alpha channel on the R, B and G channels. | 320 // Premultiply the Alpha channel on the R, B and G channels. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 delete context_; | 390 delete context_; |
| 389 context_ = NULL; | 391 context_ = NULL; |
| 390 | 392 |
| 391 parent::TearDown(); | 393 parent::TearDown(); |
| 392 } | 394 } |
| 393 | 395 |
| 394 void Init(int size) { | 396 void Init(int size) { |
| 395 size_ = size; | 397 size_ = size; |
| 396 context_ = new Context(); | 398 context_ = new Context(); |
| 397 bitmap_ = new Bitmap(*context_, size_, size_); | 399 bitmap_ = new Bitmap(*context_, size_, size_); |
| 398 vcanvas_ = new gfx::VectorCanvas(context_->context(), size_, size_); | 400 vcanvas_ = new VectorCanvas(context_->context(), size_, size_); |
| 399 pcanvas_ = new gfx::PlatformCanvasWin(size_, size_, false); | 401 pcanvas_ = new PlatformCanvasWin(size_, size_, false); |
| 400 | 402 |
| 401 // Clear white. | 403 // Clear white. |
| 402 vcanvas_->drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); | 404 vcanvas_->drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); |
| 403 pcanvas_->drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); | 405 pcanvas_->drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); |
| 404 } | 406 } |
| 405 | 407 |
| 406 // Compares both canvas and returns the pixel difference in percentage between | 408 // Compares both canvas and returns the pixel difference in percentage between |
| 407 // both images. 0 on success and ]0, 100] on failure. | 409 // both images. 0 on success and ]0, 100] on failure. |
| 408 double ProcessImage(const std::wstring& filename) { | 410 double ProcessImage(const std::wstring& filename) { |
| 409 std::wstring number(StringPrintf(L"%02d_", number_++)); | 411 std::wstring number(StringPrintf(L"%02d_", number_++)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 430 // Current image number in the current test. Used to number of test files. | 432 // Current image number in the current test. Used to number of test files. |
| 431 int number_; | 433 int number_; |
| 432 | 434 |
| 433 // A temporary HDC to draw into. | 435 // A temporary HDC to draw into. |
| 434 Context* context_; | 436 Context* context_; |
| 435 | 437 |
| 436 // Bitmap created inside context_. | 438 // Bitmap created inside context_. |
| 437 Bitmap* bitmap_; | 439 Bitmap* bitmap_; |
| 438 | 440 |
| 439 // Vector based canvas. | 441 // Vector based canvas. |
| 440 gfx::VectorCanvas* vcanvas_; | 442 VectorCanvas* vcanvas_; |
| 441 | 443 |
| 442 // Pixel based canvas. | 444 // Pixel based canvas. |
| 443 gfx::PlatformCanvasWin* pcanvas_; | 445 PlatformCanvasWin* pcanvas_; |
| 444 | 446 |
| 445 // When true (default), vcanvas_ and pcanvas_ contents are compared and | 447 // When true (default), vcanvas_ and pcanvas_ contents are compared and |
| 446 // verified to be identical. | 448 // verified to be identical. |
| 447 bool compare_canvas_; | 449 bool compare_canvas_; |
| 448 }; | 450 }; |
| 449 | 451 |
| 450 | 452 |
| 451 //////////////////////////////////////////////////////////////////////////////// | 453 //////////////////////////////////////////////////////////////////////////////// |
| 452 // Actual tests | 454 // Actual tests |
| 453 | 455 |
| 454 TEST_F(VectorCanvasTest, Uninitialized) { | 456 TEST_F(VectorCanvasTest, Uninitialized) { |
| 455 // Do a little mubadumba do get uninitialized stuff. | 457 // Do a little mubadumba do get uninitialized stuff. |
| 456 VectorCanvasTest::TearDown(); | 458 VectorCanvasTest::TearDown(); |
| 457 | 459 |
| 458 // The goal is not to verify that have the same uninitialized data. | 460 // The goal is not to verify that have the same uninitialized data. |
| 459 compare_canvas_ = false; | 461 compare_canvas_ = false; |
| 460 | 462 |
| 461 context_ = new Context(); | 463 context_ = new Context(); |
| 462 bitmap_ = new Bitmap(*context_, size_, size_); | 464 bitmap_ = new Bitmap(*context_, size_, size_); |
| 463 vcanvas_ = new gfx::VectorCanvas(context_->context(), size_, size_); | 465 vcanvas_ = new VectorCanvas(context_->context(), size_, size_); |
| 464 pcanvas_ = new gfx::PlatformCanvasWin(size_, size_, false); | 466 pcanvas_ = new PlatformCanvasWin(size_, size_, false); |
| 465 | 467 |
| 466 // VectorCanvas default initialization is black. | 468 // VectorCanvas default initialization is black. |
| 467 // PlatformCanvas default initialization is almost white 0x01FFFEFD (invalid | 469 // PlatformCanvas default initialization is almost white 0x01FFFEFD (invalid |
| 468 // Skia color) in both Debug and Release. See magicTransparencyColor in | 470 // Skia color) in both Debug and Release. See magicTransparencyColor in |
| 469 // platform_device.cc | 471 // platform_device.cc |
| 470 EXPECT_EQ(0., ProcessImage(L"empty")); | 472 EXPECT_EQ(0., ProcessImage(L"empty")); |
| 471 } | 473 } |
| 472 | 474 |
| 473 TEST_F(VectorCanvasTest, BasicDrawing) { | 475 TEST_F(VectorCanvasTest, BasicDrawing) { |
| 474 EXPECT_EQ(Image(*vcanvas_).PercentageDifferent(Image(*pcanvas_)), 0.) | 476 EXPECT_EQ(Image(*vcanvas_).PercentageDifferent(Image(*pcanvas_)), 0.) |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 | 1002 |
| 1001 { | 1003 { |
| 1002 vcanvas_->rotate(67); | 1004 vcanvas_->rotate(67); |
| 1003 pcanvas_->rotate(67); | 1005 pcanvas_->rotate(67); |
| 1004 vcanvas_->drawBitmap(bitmap, 20, -50, NULL); | 1006 vcanvas_->drawBitmap(bitmap, 20, -50, NULL); |
| 1005 pcanvas_->drawBitmap(bitmap, 20, -50, NULL); | 1007 pcanvas_->drawBitmap(bitmap, 20, -50, NULL); |
| 1006 EXPECT_EQ(0., ProcessImage(L"rotate")); | 1008 EXPECT_EQ(0., ProcessImage(L"rotate")); |
| 1007 } | 1009 } |
| 1008 } | 1010 } |
| 1009 | 1011 |
| 1012 } // namespace skia |
| OLD | NEW |