Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/image/image_skia.h" | 5 #include "ui/gfx/image/image_skia.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "ui/gfx/canvas.h" | |
| 13 #include "ui/gfx/geometry/rect.h" | |
| 14 #include "ui/gfx/geometry/size.h" | |
| 12 #include "ui/gfx/image/image_skia_rep.h" | 15 #include "ui/gfx/image/image_skia_rep.h" |
| 13 #include "ui/gfx/image/image_skia_source.h" | 16 #include "ui/gfx/image/image_skia_source.h" |
| 14 #include "ui/gfx/size.h" | |
| 15 #include "ui/gfx/switches.h" | 17 #include "ui/gfx/switches.h" |
| 16 | 18 |
| 17 // Duplicated from base/threading/non_thread_safe.h so that we can be | 19 // Duplicated from base/threading/non_thread_safe.h so that we can be |
| 18 // good citizens there and undef the macro. | 20 // good citizens there and undef the macro. |
| 19 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 21 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 20 #define ENABLE_NON_THREAD_SAFE 1 | 22 #define ENABLE_NON_THREAD_SAFE 1 |
| 21 #else | 23 #else |
| 22 #define ENABLE_NON_THREAD_SAFE 0 | 24 #define ENABLE_NON_THREAD_SAFE 0 |
| 23 #endif | 25 #endif |
| 24 | 26 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 47 public: | 49 public: |
| 48 DynamicSource(const gfx::Size& size) | 50 DynamicSource(const gfx::Size& size) |
| 49 : size_(size), | 51 : size_(size), |
| 50 last_requested_scale_(0.0f) {} | 52 last_requested_scale_(0.0f) {} |
| 51 | 53 |
| 52 virtual ~DynamicSource() { | 54 virtual ~DynamicSource() { |
| 53 } | 55 } |
| 54 | 56 |
| 55 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 57 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
| 56 last_requested_scale_ = scale; | 58 last_requested_scale_ = scale; |
| 57 return gfx::ImageSkiaRep(size_, scale); | 59 // The returned image can be resized in ImageSkiaStorage, so |
|
oshima
2014/05/15 16:14:13
this constructor is used in other tests (and only
Jun Mukai
2014/05/16 01:57:33
Done.
| |
| 60 // empty ImageSkiaRep could cause use-of-uninitialized-value. | |
| 61 gfx::Canvas canvas(size_, scale, true); | |
| 62 canvas.FillRect(Rect(size_), SK_ColorRED); | |
| 63 return canvas.ExtractImageRep(); | |
| 58 } | 64 } |
| 59 | 65 |
| 60 float GetLastRequestedScaleAndReset() { | 66 float GetLastRequestedScaleAndReset() { |
| 61 float result = last_requested_scale_; | 67 float result = last_requested_scale_; |
| 62 last_requested_scale_ = 0.0f; | 68 last_requested_scale_ = 0.0f; |
| 63 return result; | 69 return result; |
| 64 } | 70 } |
| 65 | 71 |
| 66 private: | 72 private: |
| 67 gfx::Size size_; | 73 gfx::Size size_; |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 } | 503 } |
| 498 | 504 |
| 499 TEST_F(ImageSkiaTest, ArbitraryScaleFactorWithMissingResource) { | 505 TEST_F(ImageSkiaTest, ArbitraryScaleFactorWithMissingResource) { |
| 500 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 506 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 501 switches::kAllowArbitraryScaleFactorInImageSkia); | 507 switches::kAllowArbitraryScaleFactorInImageSkia); |
| 502 | 508 |
| 503 // Do not test if the ImageSkia doesn't support arbitrary scale factors. | 509 // Do not test if the ImageSkia doesn't support arbitrary scale factors. |
| 504 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) | 510 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) |
| 505 return; | 511 return; |
| 506 | 512 |
| 507 ImageSkia image(new FixedSource( | 513 Canvas canvas(Size(100, 200), 1.0f, false); |
| 508 ImageSkiaRep(Size(100, 200), 1.0f)), Size(100, 200)); | 514 canvas.FillRect(Rect(0, 0, 100, 200), SK_ColorRED); |
| 515 ImageSkia image(new FixedSource(canvas.ExtractImageRep()), Size(100, 200)); | |
| 509 | 516 |
| 510 // Requesting 1.5f -- falls back to 2.0f, but couldn't find. It should | 517 // Requesting 1.5f -- falls back to 2.0f, but couldn't find. It should |
| 511 // look up 1.0f and then rescale it. | 518 // look up 1.0f and then rescale it. |
| 512 const ImageSkiaRep& rep = image.GetRepresentation(1.5f); | 519 const ImageSkiaRep& rep = image.GetRepresentation(1.5f); |
| 513 EXPECT_EQ(1.5f, rep.scale()); | 520 EXPECT_EQ(1.5f, rep.scale()); |
| 514 EXPECT_EQ(2U, image.image_reps().size()); | 521 EXPECT_EQ(2U, image.image_reps().size()); |
| 515 EXPECT_EQ(1.0f, image.image_reps()[0].scale()); | 522 EXPECT_EQ(1.0f, image.image_reps()[0].scale()); |
| 516 EXPECT_EQ(1.5f, image.image_reps()[1].scale()); | 523 EXPECT_EQ(1.5f, image.image_reps()[1].scale()); |
| 517 } | 524 } |
| 518 | 525 |
| 519 TEST_F(ImageSkiaTest, UnscaledImageForArbitraryScaleFactor) { | 526 TEST_F(ImageSkiaTest, UnscaledImageForArbitraryScaleFactor) { |
| 520 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 527 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 521 switches::kAllowArbitraryScaleFactorInImageSkia); | 528 switches::kAllowArbitraryScaleFactorInImageSkia); |
| 522 | 529 |
| 523 // Do not test if the ImageSkia doesn't support arbitrary scale factors. | 530 // Do not test if the ImageSkia doesn't support arbitrary scale factors. |
| 524 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) | 531 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) |
| 525 return; | 532 return; |
| 526 | 533 |
| 527 // 0.0f means unscaled. | 534 // 0.0f means unscaled. |
| 535 Canvas canvas(Size(100, 200), 1.0f, false); | |
| 536 canvas.FillRect(Rect(0, 0, 100, 200), SK_ColorRED); | |
| 537 ImageSkiaRep origin_rep = canvas.ExtractImageRep(); | |
| 528 ImageSkia image(new FixedSource( | 538 ImageSkia image(new FixedSource( |
| 529 ImageSkiaRep(Size(100, 200), 0.0f)), Size(100, 200)); | 539 ImageSkiaRep(origin_rep.sk_bitmap(), 0.0f)), Size(100, 200)); |
| 530 | 540 |
| 531 // Requesting 2.0f, which should return 1.0f unscaled image. | 541 // Requesting 2.0f, which should return 1.0f unscaled image. |
| 532 const ImageSkiaRep& rep = image.GetRepresentation(2.0f); | 542 const ImageSkiaRep& rep = image.GetRepresentation(2.0f); |
| 533 EXPECT_EQ(1.0f, rep.scale()); | 543 EXPECT_EQ(1.0f, rep.scale()); |
| 534 EXPECT_EQ("100x200", rep.pixel_size().ToString()); | 544 EXPECT_EQ("100x200", rep.pixel_size().ToString()); |
| 535 EXPECT_TRUE(rep.unscaled()); | 545 EXPECT_TRUE(rep.unscaled()); |
| 536 EXPECT_EQ(1U, image.image_reps().size()); | 546 EXPECT_EQ(1U, image.image_reps().size()); |
| 537 | 547 |
| 538 // Same for any other scale factors. | 548 // Same for any other scale factors. |
| 539 const ImageSkiaRep& rep15 = image.GetRepresentation(1.5f); | 549 const ImageSkiaRep& rep15 = image.GetRepresentation(1.5f); |
| 540 EXPECT_EQ(1.0f, rep15.scale()); | 550 EXPECT_EQ(1.0f, rep15.scale()); |
| 541 EXPECT_EQ("100x200", rep15.pixel_size().ToString()); | 551 EXPECT_EQ("100x200", rep15.pixel_size().ToString()); |
| 542 EXPECT_TRUE(rep15.unscaled()); | 552 EXPECT_TRUE(rep15.unscaled()); |
| 543 EXPECT_EQ(1U, image.image_reps().size()); | 553 EXPECT_EQ(1U, image.image_reps().size()); |
| 544 | 554 |
| 545 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f); | 555 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f); |
| 546 EXPECT_EQ(1.0f, rep12.scale()); | 556 EXPECT_EQ(1.0f, rep12.scale()); |
| 547 EXPECT_EQ("100x200", rep12.pixel_size().ToString()); | 557 EXPECT_EQ("100x200", rep12.pixel_size().ToString()); |
| 548 EXPECT_TRUE(rep12.unscaled()); | 558 EXPECT_TRUE(rep12.unscaled()); |
| 549 EXPECT_EQ(1U, image.image_reps().size()); | 559 EXPECT_EQ(1U, image.image_reps().size()); |
| 550 } | 560 } |
| 551 | 561 |
| 552 } // namespace gfx | 562 } // namespace gfx |
| OLD | NEW |