images: Fix image animations for images with specific loop count.

For an image with a repetition count of n, it should animate for
n+1 loops. But the compositor only performs n loops. Fix this by
specifying the actual repetition count of PaintImage.

R=chrishtr@chromium.org
TBR=khushalsagar@chromium.org

(cherry picked from commit 644b058c04c5a38e758f07832323f104c12443ad)

Bug: 815038
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.linux:linux_layout_tests_slimming_paint_v2
Change-Id: I450d435ed38a3beba30276fa4dbabdcb5a68a944
Reviewed-on: https://chromium-review.googlesource.com/934968
Reviewed-by: Chris Harrelson <chrishtr@chromium.org>
Commit-Queue: Khushal <khushalsagar@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#539238}
Reviewed-on: https://chromium-review.googlesource.com/939529
Reviewed-by: Khushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#606}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/third_party/WebKit/LayoutTests/images/resources/three-frames_loop-three-times.gif b/third_party/WebKit/LayoutTests/images/resources/three-frames_loop-three-times.gif
new file mode 100644
index 0000000..a474c8e
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/images/resources/three-frames_loop-three-times.gif
Binary files differ
diff --git a/third_party/WebKit/Source/platform/graphics/BitmapImage.cpp b/third_party/WebKit/Source/platform/graphics/BitmapImage.cpp
index 53c91603..c0c4b14 100644
--- a/third_party/WebKit/Source/platform/graphics/BitmapImage.cpp
+++ b/third_party/WebKit/Source/platform/graphics/BitmapImage.cpp
@@ -144,12 +144,21 @@
   auto completion_state = all_data_received_
                               ? PaintImage::CompletionState::DONE
                               : PaintImage::CompletionState::PARTIALLY_DONE;
+
+  // When requesting more than a single loop, repetition count is one less than
+  // the actual number of loops requested.
+  // TODO(khushalsagar): Fix this in RepetitionCount itself when removing code
+  // here for animations.
+  int repetition_count = RepetitionCount();
+  if (repetition_count > 0)
+    repetition_count++;
+
   auto builder =
       CreatePaintImageBuilder()
           .set_paint_image_generator(std::move(generator))
           .set_frame_index(index)
           .set_repetition_count(GetRepetitionCountWithPolicyOverride(
-              repetition_count_, animation_policy_))
+              repetition_count, animation_policy_))
           .set_completion_state(completion_state)
           .set_reset_animation_sequence_id(reset_animation_sequence_id_);
 
diff --git a/third_party/WebKit/Source/platform/graphics/BitmapImageTest.cpp b/third_party/WebKit/Source/platform/graphics/BitmapImageTest.cpp
index 89c576f..5cfea99 100644
--- a/third_party/WebKit/Source/platform/graphics/BitmapImageTest.cpp
+++ b/third_party/WebKit/Source/platform/graphics/BitmapImageTest.cpp
@@ -349,6 +349,13 @@
   EXPECT_EQ(image_->ImageForDefaultFrame(), image_);
 }
 
+TEST_F(BitmapImageTest, GIFRepetitionCount) {
+  LoadImage("/LayoutTests/images/resources/three-frames_loop-three-times.gif");
+  auto paint_image = image_->PaintImageForCurrentFrame();
+  EXPECT_EQ(paint_image.repetition_count(), 3);
+  EXPECT_EQ(paint_image.FrameCount(), 3u);
+}
+
 class BitmapImageTestWithMockDecoder : public BitmapImageTest,
                                        public MockImageDecoderClient {
  public: