File: ImageQualityControllerTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (356 lines) | stat: -rw-r--r-- 14,224 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/layout/ImageQualityController.h"

#include "core/layout/LayoutImage.h"
#include "core/layout/LayoutTestHelper.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/paint/PaintController.h"
#include "platform/scheduler/test/fake_web_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {

class ImageQualityControllerTest : public RenderingTest {
 protected:
  ImageQualityController* controller() { return m_controller; }

 private:
  void SetUp() override {
    m_controller = ImageQualityController::imageQualityController();
    RenderingTest::SetUp();
  }

  ImageQualityController* m_controller;
};

TEST_F(ImageQualityControllerTest, RegularImage) {
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutObject* obj = document().body()->firstChild()->layoutObject();

  EXPECT_EQ(InterpolationDefault, controller()->chooseInterpolationQuality(
                                      *obj, nullptr, nullptr, LayoutSize()));
}

TEST_F(ImageQualityControllerTest, ImageRenderingPixelated) {
  setBodyInnerHTML(
      "<img src='myimage' style='image-rendering: pixelated'></img>");
  LayoutObject* obj = document().body()->firstChild()->layoutObject();

  EXPECT_EQ(InterpolationNone, controller()->chooseInterpolationQuality(
                                   *obj, nullptr, nullptr, LayoutSize()));
}

#if !USE(LOW_QUALITY_IMAGE_INTERPOLATION)

class TestImageAnimated : public Image {
 public:
  bool maybeAnimated() override { return true; }
  bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override {
    return false;
  }
  IntSize size() const override { return IntSize(); }
  void destroyDecodedData() override {}
  void draw(SkCanvas*,
            const SkPaint&,
            const FloatRect& dstRect,
            const FloatRect& srcRect,
            RespectImageOrientationEnum,
            ImageClampingMode,
            const ColorBehavior&) override {}
  sk_sp<SkImage> imageForCurrentFrame(const ColorBehavior&) override {
    return nullptr;
  }
};

TEST_F(ImageQualityControllerTest, ImageMaybeAnimated) {
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageAnimated> testImage = adoptRef(new TestImageAnimated);
  EXPECT_EQ(InterpolationMedium,
            controller()->chooseInterpolationQuality(*img, testImage.get(),
                                                     nullptr, LayoutSize()));
}

class TestImageWithContrast : public Image {
 public:
  bool maybeAnimated() override { return true; }
  bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override {
    return false;
  }
  IntSize size() const override { return IntSize(); }
  void destroyDecodedData() override {}
  void draw(SkCanvas*,
            const SkPaint&,
            const FloatRect& dstRect,
            const FloatRect& srcRect,
            RespectImageOrientationEnum,
            ImageClampingMode,
            const ColorBehavior&) override {}

  bool isBitmapImage() const override { return true; }
  sk_sp<SkImage> imageForCurrentFrame(const ColorBehavior&) override {
    return nullptr;
  }
};

TEST_F(ImageQualityControllerTest, LowQualityFilterForContrast) {
  setBodyInnerHTML(
      "<img src='myimage' style='image-rendering: "
      "-webkit-optimize-contrast'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageWithContrast> testImage = adoptRef(new TestImageWithContrast);
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize()));
}

class TestImageLowQuality : public Image {
 public:
  bool maybeAnimated() override { return true; }
  bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override {
    return false;
  }
  IntSize size() const override { return IntSize(1, 1); }
  void destroyDecodedData() override {}
  void draw(SkCanvas*,
            const SkPaint&,
            const FloatRect& dstRect,
            const FloatRect& srcRect,
            RespectImageOrientationEnum,
            ImageClampingMode,
            const ColorBehavior&) override {}

  bool isBitmapImage() const override { return true; }
  sk_sp<SkImage> imageForCurrentFrame(const ColorBehavior&) override {
    return nullptr;
  }
};

TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage) {
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
  EXPECT_EQ(InterpolationMedium,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(1, 1)));
}

// TODO(alexclarke): Remove this when possible.
class MockTimer : public TaskRunnerTimer<ImageQualityController> {
 public:
  using TimerFiredFunction =
      typename TaskRunnerTimer<ImageQualityController>::TimerFiredFunction;

  static std::unique_ptr<MockTimer> create(ImageQualityController* o,
                                           TimerFiredFunction f) {
    auto taskRunner = adoptRef(new scheduler::FakeWebTaskRunner);
    return WTF::wrapUnique(new MockTimer(std::move(taskRunner), o, f));
  }

  void fire() {
    fired();
    stop();
  }

  void setTime(double newTime) { m_taskRunner->setTime(newTime); }

 private:
  MockTimer(RefPtr<scheduler::FakeWebTaskRunner> taskRunner,
            ImageQualityController* o,
            TimerFiredFunction f)
      : TaskRunnerTimer(taskRunner, o, f),
        m_taskRunner(std::move(taskRunner)) {}

  RefPtr<scheduler::FakeWebTaskRunner> m_taskRunner;

  DISALLOW_COPY_AND_ASSIGN(MockTimer);
};

TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage) {
  MockTimer* mockTimer =
      MockTimer::create(controller(),
                        &ImageQualityController::highQualityRepaintTimerFired)
          .release();
  controller()->setTimer(WTF::wrapUnique(mockTimer));
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
  std::unique_ptr<PaintController> paintController = PaintController::create();
  GraphicsContext context(*paintController);

  // Paint once. This will kick off a timer to see if we resize it during that
  // timer's execution.
  EXPECT_EQ(InterpolationMedium,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(2, 2)));

  // Go into low-quality mode now that the size changed.
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(3, 3)));

  // Stay in low-quality mode since the size changed again.
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(4, 4)));

  mockTimer->fire();
  // The timer fired before painting at another size, so this doesn't count as
  // animation. Therefore not painting at low quality.
  EXPECT_EQ(InterpolationMedium,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(4, 4)));
}

TEST_F(ImageQualityControllerTest,
       MediumQualityFilterForNotAnimatedWhileAnotherAnimates) {
  MockTimer* mockTimer =
      MockTimer::create(controller(),
                        &ImageQualityController::highQualityRepaintTimerFired)
          .release();
  controller()->setTimer(WTF::wrapUnique(mockTimer));
  setBodyInnerHTML(
      "<img id='myAnimatingImage' src='myimage'></img> <img "
      "id='myNonAnimatingImage' src='myimage2'></img>");
  LayoutImage* animatingImage = toLayoutImage(
      document().getElementById("myAnimatingImage")->layoutObject());
  LayoutImage* nonAnimatingImage = toLayoutImage(
      document().getElementById("myNonAnimatingImage")->layoutObject());

  RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
  std::unique_ptr<PaintController> paintController = PaintController::create();
  GraphicsContext context(*paintController);

  // Paint once. This will kick off a timer to see if we resize it during that
  // timer's execution.
  EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(
                                     *animatingImage, testImage.get(),
                                     testImage.get(), LayoutSize(2, 2)));

  // Go into low-quality mode now that the size changed.
  EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(
                                  *animatingImage, testImage.get(),
                                  testImage.get(), LayoutSize(3, 3)));

  // The non-animating image receives a medium-quality filter, even though the
  // other one is animating.
  EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(
                                     *nonAnimatingImage, testImage.get(),
                                     testImage.get(), LayoutSize(4, 4)));

  // Now the second image has animated, so it also gets painted with a
  // low-quality filter.
  EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(
                                  *nonAnimatingImage, testImage.get(),
                                  testImage.get(), LayoutSize(3, 3)));

  mockTimer->fire();
  // The timer fired before painting at another size, so this doesn't count as
  // animation. Therefore not painting at low quality for any image.
  EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(
                                     *animatingImage, testImage.get(),
                                     testImage.get(), LayoutSize(4, 4)));
  EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(
                                     *nonAnimatingImage, testImage.get(),
                                     testImage.get(), LayoutSize(4, 4)));
}

TEST_F(ImageQualityControllerTest,
       DontKickTheAnimationTimerWhenPaintingAtTheSameSize) {
  MockTimer* mockTimer =
      MockTimer::create(controller(),
                        &ImageQualityController::highQualityRepaintTimerFired)
          .release();
  controller()->setTimer(WTF::wrapUnique(mockTimer));
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);

  // Paint once. This will kick off a timer to see if we resize it during that
  // timer's execution.
  EXPECT_EQ(InterpolationMedium,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(2, 2)));

  // Go into low-quality mode now that the size changed.
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(3, 3)));

  // Stay in low-quality mode since the size changed again.
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(4, 4)));

  mockTimer->stop();
  EXPECT_FALSE(mockTimer->isActive());
  // Painted at the same size, so even though timer is still executing, don't go
  // to low quality.
  EXPECT_EQ(InterpolationLow,
            controller()->chooseInterpolationQuality(
                *img, testImage.get(), testImage.get(), LayoutSize(4, 4)));
  // Check that the timer was not kicked. It should not have been, since the
  // image was painted at the same size as last time.
  EXPECT_FALSE(mockTimer->isActive());
}

TEST_F(ImageQualityControllerTest, DontRestartTimerUnlessAdvanced) {
  MockTimer* mockTimer =
      MockTimer::create(controller(),
                        &ImageQualityController::highQualityRepaintTimerFired)
          .release();
  controller()->setTimer(WTF::wrapUnique(mockTimer));
  setBodyInnerHTML("<img src='myimage'></img>");
  LayoutImage* img =
      toLayoutImage(document().body()->firstChild()->layoutObject());

  RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);

  // Paint once. This will kick off a timer to see if we resize it during that
  // timer's execution.
  mockTimer->setTime(0.1);
  EXPECT_FALSE(controller()->shouldPaintAtLowQuality(
      *img, testImage.get(), testImage.get(), LayoutSize(2, 2), 0.1));
  EXPECT_EQ(ImageQualityController::cLowQualityTimeThreshold,
            mockTimer->nextFireInterval());

  // Go into low-quality mode now that the size changed.
  double nextTime = 0.1 + ImageQualityController::cTimerRestartThreshold / 2.0;
  mockTimer->setTime(nextTime);
  EXPECT_EQ(true, controller()->shouldPaintAtLowQuality(
                      *img, testImage.get(), testImage.get(), LayoutSize(3, 3),
                      nextTime));
  // The fire interval has decreased, because we have not restarted the timer.
  EXPECT_EQ(ImageQualityController::cLowQualityTimeThreshold -
                ImageQualityController::cTimerRestartThreshold / 2.0,
            mockTimer->nextFireInterval());

  // This animation is far enough in the future to make the timer restart, since
  // it is half over.
  nextTime = 0.1 + ImageQualityController::cTimerRestartThreshold + 0.01;
  EXPECT_EQ(true, controller()->shouldPaintAtLowQuality(
                      *img, testImage.get(), testImage.get(), LayoutSize(4, 4),
                      nextTime));
  // Now the timer has restarted, leading to a larger fire interval.
  EXPECT_EQ(ImageQualityController::cLowQualityTimeThreshold,
            mockTimer->nextFireInterval());
}

#endif

}  // namespace blink