| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/layout/ImageQualityController.h" | 6 #include "core/layout/ImageQualityController.h" |
| 7 | 7 |
| 8 #include "core/layout/LayoutImage.h" | 8 #include "core/layout/LayoutImage.h" |
| 9 #include "core/layout/LayoutTestHelper.h" | 9 #include "core/layout/LayoutTestHelper.h" |
| 10 #include "platform/graphics/GraphicsContext.h" | 10 #include "platform/graphics/GraphicsContext.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 { | 27 { |
| 28 } | 28 } |
| 29 ImageQualityController* m_controller; | 29 ImageQualityController* m_controller; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 TEST_F(ImageQualityControllerTest, RegularImage) | 32 TEST_F(ImageQualityControllerTest, RegularImage) |
| 33 { | 33 { |
| 34 setBodyInnerHTML("<img src='myimage'></img>"); | 34 setBodyInnerHTML("<img src='myimage'></img>"); |
| 35 LayoutObject* obj = document().body()->firstChild()->layoutObject(); | 35 LayoutObject* obj = document().body()->firstChild()->layoutObject(); |
| 36 | 36 |
| 37 EXPECT_EQ(InterpolationDefault, controller()->chooseInterpolationQuality(nul
lptr, obj, nullptr, nullptr, LayoutSize())); | 37 EXPECT_EQ(InterpolationDefault, controller()->chooseInterpolationQuality(*ob
j, nullptr, nullptr, LayoutSize())); |
| 38 } | 38 } |
| 39 | 39 |
| 40 TEST_F(ImageQualityControllerTest, ImageRenderingPixelated) | 40 TEST_F(ImageQualityControllerTest, ImageRenderingPixelated) |
| 41 { | 41 { |
| 42 setBodyInnerHTML("<img src='myimage' style='image-rendering: pixelated'></im
g>"); | 42 setBodyInnerHTML("<img src='myimage' style='image-rendering: pixelated'></im
g>"); |
| 43 LayoutObject* obj = document().body()->firstChild()->layoutObject(); | 43 LayoutObject* obj = document().body()->firstChild()->layoutObject(); |
| 44 | 44 |
| 45 EXPECT_EQ(InterpolationNone, controller()->chooseInterpolationQuality(nullpt
r, obj, nullptr, nullptr, LayoutSize())); | 45 EXPECT_EQ(InterpolationNone, controller()->chooseInterpolationQuality(*obj,
nullptr, nullptr, LayoutSize())); |
| 46 } | 46 } |
| 47 | 47 |
| 48 #if !USE(LOW_QUALITY_IMAGE_INTERPOLATION) | 48 #if !USE(LOW_QUALITY_IMAGE_INTERPOLATION) |
| 49 | 49 |
| 50 class TestImageAnimated : public Image { | 50 class TestImageAnimated : public Image { |
| 51 public: | 51 public: |
| 52 bool maybeAnimated() override { return true; } | 52 bool maybeAnimated() override { return true; } |
| 53 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } | 53 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } |
| 54 IntSize size() const override { return IntSize(); } | 54 IntSize size() const override { return IntSize(); } |
| 55 void destroyDecodedData(bool) override { } | 55 void destroyDecodedData(bool) override { } |
| 56 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } | 56 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } |
| 57 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } | 57 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 TEST_F(ImageQualityControllerTest, ImageMaybeAnimated) | 60 TEST_F(ImageQualityControllerTest, ImageMaybeAnimated) |
| 61 { | 61 { |
| 62 setBodyInnerHTML("<img src='myimage'></img>"); | 62 setBodyInnerHTML("<img src='myimage'></img>"); |
| 63 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 63 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 64 | 64 |
| 65 RefPtr<TestImageAnimated> testImage = adoptRef(new TestImageAnimated); | 65 RefPtr<TestImageAnimated> testImage = adoptRef(new TestImageAnimated); |
| 66 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(null
ptr, img, testImage.get(), nullptr, LayoutSize())); | 66 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), nullptr, LayoutSize())); |
| 67 } | 67 } |
| 68 | 68 |
| 69 class TestImageWithContrast : public Image { | 69 class TestImageWithContrast : public Image { |
| 70 public: | 70 public: |
| 71 bool maybeAnimated() override { return true; } | 71 bool maybeAnimated() override { return true; } |
| 72 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } | 72 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } |
| 73 IntSize size() const override { return IntSize(); } | 73 IntSize size() const override { return IntSize(); } |
| 74 void destroyDecodedData(bool) override { } | 74 void destroyDecodedData(bool) override { } |
| 75 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } | 75 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } |
| 76 | 76 |
| 77 bool isBitmapImage() const override { return true; } | 77 bool isBitmapImage() const override { return true; } |
| 78 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } | 78 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 TEST_F(ImageQualityControllerTest, LowQualityFilterForContrast) | 81 TEST_F(ImageQualityControllerTest, LowQualityFilterForContrast) |
| 82 { | 82 { |
| 83 setBodyInnerHTML("<img src='myimage' style='image-rendering: -webkit-optimiz
e-contrast'></img>"); | 83 setBodyInnerHTML("<img src='myimage' style='image-rendering: -webkit-optimiz
e-contrast'></img>"); |
| 84 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 84 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 85 | 85 |
| 86 RefPtr<TestImageWithContrast> testImage = adoptRef(new TestImageWithContrast
); | 86 RefPtr<TestImageWithContrast> testImage = adoptRef(new TestImageWithContrast
); |
| 87 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(nullptr
, img, testImage.get(), testImage.get(), LayoutSize())); | 87 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize())); |
| 88 } | 88 } |
| 89 | 89 |
| 90 class TestImageLowQuality : public Image { | 90 class TestImageLowQuality : public Image { |
| 91 public: | 91 public: |
| 92 bool maybeAnimated() override { return true; } | 92 bool maybeAnimated() override { return true; } |
| 93 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } | 93 bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override
{ return false; } |
| 94 IntSize size() const override { return IntSize(1, 1); } | 94 IntSize size() const override { return IntSize(1, 1); } |
| 95 void destroyDecodedData(bool) override { } | 95 void destroyDecodedData(bool) override { } |
| 96 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } | 96 void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRe
ct& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { } |
| 97 | 97 |
| 98 bool isBitmapImage() const override { return true; } | 98 bool isBitmapImage() const override { return true; } |
| 99 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } | 99 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage) | 102 TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage) |
| 103 { | 103 { |
| 104 setBodyInnerHTML("<img src='myimage'></img>"); | 104 setBodyInnerHTML("<img src='myimage'></img>"); |
| 105 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 105 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 106 | 106 |
| 107 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 107 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
| 108 OwnPtr<PaintController> paintController = PaintController::create(); | 108 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(1, 1))); |
| 109 GraphicsContext context(*paintController); | |
| 110 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(1, 1))); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 class MockTimer : public Timer<ImageQualityController> { | 111 class MockTimer : public Timer<ImageQualityController> { |
| 114 typedef void (ImageQualityController::*TimerFiredFunction)(Timer*); | 112 typedef void (ImageQualityController::*TimerFiredFunction)(Timer*); |
| 115 public: | 113 public: |
| 116 MockTimer(ImageQualityController* o, TimerFiredFunction f) | 114 MockTimer(ImageQualityController* o, TimerFiredFunction f) |
| 117 : Timer<ImageQualityController>(o, f) | 115 : Timer<ImageQualityController>(o, f) |
| 118 { | 116 { |
| 119 } | 117 } |
| 120 | 118 |
| 121 void fire() | 119 void fire() |
| 122 { | 120 { |
| 123 this->Timer<ImageQualityController>::fired(); | 121 this->Timer<ImageQualityController>::fired(); |
| 124 stop(); | 122 stop(); |
| 125 } | 123 } |
| 126 }; | 124 }; |
| 127 | 125 |
| 128 TEST_F(ImageQualityControllerTest, LowQualityFilterForLiveResize) | 126 TEST_F(ImageQualityControllerTest, LowQualityFilterForLiveResize) |
| 129 { | 127 { |
| 130 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 128 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
| 131 controller()->setTimer(mockTimer); | 129 controller()->setTimer(mockTimer); |
| 132 setBodyInnerHTML("<img src='myimage'></img>"); | 130 setBodyInnerHTML("<img src='myimage'></img>"); |
| 133 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 131 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 134 | 132 |
| 135 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 133 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
| 136 OwnPtr<PaintController> paintController = PaintController::create(); | |
| 137 GraphicsContext context(*paintController); | |
| 138 | 134 |
| 139 // Start a resize | 135 // Start a resize |
| 140 document().frame()->view()->willStartLiveResize(); | 136 document().frame()->view()->willStartLiveResize(); |
| 141 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 137 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(2, 2))); |
| 142 | 138 |
| 143 document().frame()->view()->willEndLiveResize(); | 139 document().frame()->view()->willEndLiveResize(); |
| 144 | 140 |
| 145 // End of live resize, but timer has not fired. Therefore paint at non-low q
uality. | 141 // End of live resize, but timer has not fired. Therefore paint at non-low q
uality. |
| 146 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 142 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(3, 3))); |
| 147 | 143 |
| 148 // Start another resize | 144 // Start another resize |
| 149 document().frame()->view()->willStartLiveResize(); | 145 document().frame()->view()->willStartLiveResize(); |
| 150 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 146 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(3, 3))); |
| 151 | 147 |
| 152 // While still in resize, expire the timer. | 148 // While still in resize, expire the timer. |
| 153 document().frame()->view()->willEndLiveResize(); | 149 document().frame()->view()->willEndLiveResize(); |
| 154 | 150 |
| 155 mockTimer->fire(); | 151 mockTimer->fire(); |
| 156 // End of live resize, and timer has fired. Therefore paint at non-low quali
ty, even though the size has changed. | 152 // End of live resize, and timer has fired. Therefore paint at non-low quali
ty, even though the size has changed. |
| 157 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 153 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
| 158 } | 154 } |
| 159 | 155 |
| 160 TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage) | 156 TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage) |
| 161 { | 157 { |
| 162 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 158 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
| 163 controller()->setTimer(mockTimer); | 159 controller()->setTimer(mockTimer); |
| 164 setBodyInnerHTML("<img src='myimage'></img>"); | 160 setBodyInnerHTML("<img src='myimage'></img>"); |
| 165 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 161 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 166 | 162 |
| 167 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 163 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
| 168 OwnPtr<PaintController> paintController = PaintController::create(); | 164 OwnPtr<PaintController> paintController = PaintController::create(); |
| 169 GraphicsContext context(*paintController); | 165 GraphicsContext context(*paintController); |
| 170 | 166 |
| 171 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. | 167 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. |
| 172 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 168 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(2, 2))); |
| 173 | 169 |
| 174 // Go into low-quality mode now that the size changed. | 170 // Go into low-quality mode now that the size changed. |
| 175 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 171 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(3, 3))); |
| 176 | 172 |
| 177 // Stay in low-quality mode since the size changed again. | 173 // Stay in low-quality mode since the size changed again. |
| 178 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 174 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(4, 4))); |
| 179 | 175 |
| 180 mockTimer->fire(); | 176 mockTimer->fire(); |
| 181 // The timer fired before painting at another size, so this doesn't count as
animation. Therefore not painting at low quality. | 177 // The timer fired before painting at another size, so this doesn't count as
animation. Therefore not painting at low quality. |
| 182 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 178 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
| 183 } | 179 } |
| 184 | 180 |
| 185 TEST_F(ImageQualityControllerTest, DontKickTheAnimationTimerWhenPaintingAtTheSam
eSize) | 181 TEST_F(ImageQualityControllerTest, DontKickTheAnimationTimerWhenPaintingAtTheSam
eSize) |
| 186 { | 182 { |
| 187 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 183 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
| 188 controller()->setTimer(mockTimer); | 184 controller()->setTimer(mockTimer); |
| 189 setBodyInnerHTML("<img src='myimage'></img>"); | 185 setBodyInnerHTML("<img src='myimage'></img>"); |
| 190 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 186 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
| 191 | 187 |
| 192 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 188 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
| 193 OwnPtr<PaintController> paintController = PaintController::create(); | |
| 194 GraphicsContext context(*paintController); | |
| 195 | 189 |
| 196 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. | 190 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. |
| 197 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 191 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img
, testImage.get(), testImage.get(), LayoutSize(2, 2))); |
| 198 | 192 |
| 199 // Go into low-quality mode now that the size changed. | 193 // Go into low-quality mode now that the size changed. |
| 200 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 194 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(3, 3))); |
| 201 | 195 |
| 202 // Stay in low-quality mode since the size changed again. | 196 // Stay in low-quality mode since the size changed again. |
| 203 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 197 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(4, 4))); |
| 204 | 198 |
| 205 mockTimer->stop(); | 199 mockTimer->stop(); |
| 206 EXPECT_FALSE(mockTimer->isActive()); | 200 EXPECT_FALSE(mockTimer->isActive()); |
| 207 // Painted at the same size, so even though timer is still executing, don't
go to low quality. | 201 // Painted at the same size, so even though timer is still executing, don't
go to low quality. |
| 208 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 202 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, t
estImage.get(), testImage.get(), LayoutSize(4, 4))); |
| 209 // Check that the timer was not kicked. It should not have been, since the i
mage was painted at the same size as last time. | 203 // Check that the timer was not kicked. It should not have been, since the i
mage was painted at the same size as last time. |
| 210 EXPECT_FALSE(mockTimer->isActive()); | 204 EXPECT_FALSE(mockTimer->isActive()); |
| 211 } | 205 } |
| 212 | 206 |
| 213 #endif | 207 #endif |
| 214 | 208 |
| 215 } // namespace blink | 209 } // namespace blink |
| OLD | NEW |