| 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/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "ui/gfx/image/image_skia_rep.h" | 12 #include "ui/gfx/image/image_skia_rep.h" |
| 12 #include "ui/gfx/image/image_skia_source.h" | 13 #include "ui/gfx/image/image_skia_source.h" |
| 13 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
| 15 #include "ui/gfx/switches.h" |
| 14 | 16 |
| 15 // Duplicated from base/threading/non_thread_safe.h so that we can be | 17 // Duplicated from base/threading/non_thread_safe.h so that we can be |
| 16 // good citizens there and undef the macro. | 18 // good citizens there and undef the macro. |
| 17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 19 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 18 #define ENABLE_NON_THREAD_SAFE 1 | 20 #define ENABLE_NON_THREAD_SAFE 1 |
| 19 #else | 21 #else |
| 20 #define ENABLE_NON_THREAD_SAFE 0 | 22 #define ENABLE_NON_THREAD_SAFE 0 |
| 21 #endif | 23 #endif |
| 22 | 24 |
| 23 namespace gfx { | 25 namespace gfx { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 36 } | 38 } |
| 37 | 39 |
| 38 private: | 40 private: |
| 39 ImageSkiaRep image_; | 41 ImageSkiaRep image_; |
| 40 | 42 |
| 41 DISALLOW_COPY_AND_ASSIGN(FixedSource); | 43 DISALLOW_COPY_AND_ASSIGN(FixedSource); |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 class DynamicSource : public ImageSkiaSource { | 46 class DynamicSource : public ImageSkiaSource { |
| 45 public: | 47 public: |
| 46 DynamicSource(const gfx::Size& size) : size_(size) {} | 48 DynamicSource(const gfx::Size& size) |
| 49 : size_(size), |
| 50 last_requested_scale_(0.0f) {} |
| 47 | 51 |
| 48 virtual ~DynamicSource() { | 52 virtual ~DynamicSource() { |
| 49 } | 53 } |
| 50 | 54 |
| 51 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 55 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
| 56 last_requested_scale_ = scale; |
| 52 return gfx::ImageSkiaRep(size_, scale); | 57 return gfx::ImageSkiaRep(size_, scale); |
| 53 } | 58 } |
| 54 | 59 |
| 60 float GetLastRequestedScaleAndReset() { |
| 61 float result = last_requested_scale_; |
| 62 last_requested_scale_ = 0.0f; |
| 63 return result; |
| 64 } |
| 65 |
| 55 private: | 66 private: |
| 56 gfx::Size size_; | 67 gfx::Size size_; |
| 68 float last_requested_scale_; |
| 57 | 69 |
| 58 DISALLOW_COPY_AND_ASSIGN(DynamicSource); | 70 DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
| 59 }; | 71 }; |
| 60 | 72 |
| 61 class NullSource: public ImageSkiaSource { | 73 class NullSource: public ImageSkiaSource { |
| 62 public: | 74 public: |
| 63 NullSource() { | 75 NullSource() { |
| 64 } | 76 } |
| 65 | 77 |
| 66 virtual ~NullSource() { | 78 virtual ~NullSource() { |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 EXPECT_TRUE(threadsafe_on_thread.can_read()); | 380 EXPECT_TRUE(threadsafe_on_thread.can_read()); |
| 369 EXPECT_FALSE(threadsafe_on_thread.can_modify()); | 381 EXPECT_FALSE(threadsafe_on_thread.can_modify()); |
| 370 EXPECT_TRUE(image.CanRead()); | 382 EXPECT_TRUE(image.CanRead()); |
| 371 EXPECT_FALSE(image.CanModify()); | 383 EXPECT_FALSE(image.CanModify()); |
| 372 } | 384 } |
| 373 #endif // ENABLE_NON_THREAD_SAFE | 385 #endif // ENABLE_NON_THREAD_SAFE |
| 374 | 386 |
| 375 // Just in case we ever get lumped together with other compilation units. | 387 // Just in case we ever get lumped together with other compilation units. |
| 376 #undef ENABLE_NON_THREAD_SAFE | 388 #undef ENABLE_NON_THREAD_SAFE |
| 377 | 389 |
| 390 TEST(ImageSkiaTest, ArbitraryScaleFactor) { |
| 391 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 392 switches::kAllowArbitraryScaleFactorInImageSkia); |
| 393 |
| 394 // source is owned by |image| |
| 395 DynamicSource* source = new DynamicSource(Size(100, 200)); |
| 396 ImageSkia image(source, gfx::Size(100, 200)); |
| 397 |
| 398 image.GetRepresentation(1.5f); |
| 399 EXPECT_EQ(2.0f, source->GetLastRequestedScaleAndReset()); |
| 400 std::vector<ImageSkiaRep> image_reps = image.image_reps(); |
| 401 EXPECT_EQ(2u, image_reps.size()); |
| 402 |
| 403 std::vector<float> scale_factors; |
| 404 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 405 scale_factors.push_back(image_reps[i].scale()); |
| 406 } |
| 407 std::sort(scale_factors.begin(), scale_factors.end()); |
| 408 EXPECT_EQ(1.5f, scale_factors[0]); |
| 409 EXPECT_EQ(2.0f, scale_factors[1]); |
| 410 |
| 411 // Requesting 1.75 scale factor also falls back to 2.0f and rescale. |
| 412 // However, the image already has the 2.0f data, so it won't fetch again. |
| 413 image.GetRepresentation(1.75f); |
| 414 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| 415 image_reps = image.image_reps(); |
| 416 EXPECT_EQ(3u, image_reps.size()); |
| 417 |
| 418 scale_factors.clear(); |
| 419 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 420 scale_factors.push_back(image_reps[i].scale()); |
| 421 } |
| 422 std::sort(scale_factors.begin(), scale_factors.end()); |
| 423 EXPECT_EQ(1.5f, scale_factors[0]); |
| 424 EXPECT_EQ(1.75f, scale_factors[1]); |
| 425 EXPECT_EQ(2.0f, scale_factors[2]); |
| 426 |
| 427 // Scale factor less than 1.0f will be falled back to 1.0f |
| 428 image.GetRepresentation(0.75f); |
| 429 EXPECT_EQ(1.0f, source->GetLastRequestedScaleAndReset()); |
| 430 image_reps = image.image_reps(); |
| 431 EXPECT_EQ(5u, image_reps.size()); |
| 432 |
| 433 scale_factors.clear(); |
| 434 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 435 scale_factors.push_back(image_reps[i].scale()); |
| 436 } |
| 437 std::sort(scale_factors.begin(), scale_factors.end()); |
| 438 EXPECT_EQ(0.75f, scale_factors[0]); |
| 439 EXPECT_EQ(1.0f, scale_factors[1]); |
| 440 EXPECT_EQ(1.5f, scale_factors[2]); |
| 441 EXPECT_EQ(1.75f, scale_factors[3]); |
| 442 EXPECT_EQ(2.0f, scale_factors[4]); |
| 443 |
| 444 // Scale factor greater than 2.0f is falled back to 2.0f for now. |
| 445 image.GetRepresentation(3.0f); |
| 446 image_reps = image.image_reps(); |
| 447 EXPECT_EQ(6u, image_reps.size()); |
| 448 } |
| 449 |
| 378 } // namespace gfx | 450 } // namespace gfx |
| OLD | NEW |