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

ash / quick_insert / views / quick_insert_gif_view.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/views/quick_insert_gif_view.h"

#include <optional>

#include "ash/public/cpp/image_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/views/background.h"
#include "ui/views/controls/image_view.h"

namespace ash {

namespace {

constexpr int kQuickInsertGifCornerRadius = 8;

// We use a duration of 100ms for frames that specify a duration of <= 10ms.
// This is to follow the behavior of blink (see http://webkit.org/b/36082 for
// more information).
constexpr base::TimeDelta kShortFrameDurationThreshold = base::Milliseconds(10);
constexpr base::TimeDelta kAdjustedDurationForShortFrames =
    base::Milliseconds(100);

}  // namespace

QuickInsertGifView::QuickInsertGifView(
    FramesFetcher frames_fetcher,
    PreviewImageFetcher preview_image_fetcher,
    const gfx::Size& original_dimensions)
    : original_dimensions_(original_dimensions) {
  // Show a placeholder rect while the gif loads.
  views::Builder<QuickInsertGifView>(this)
      .SetBackground(views::CreateThemedRoundedRectBackground(
          cros_tokens::kCrosSysAppBaseShaded, kQuickInsertGifCornerRadius))
      .SetImage(ui::ImageModel::FromImageSkia(
          image_util::CreateEmptyImage(original_dimensions)))
      .BuildChildren();

  fetch_frames_start_time_ = base::TimeTicks::Now();
  std::move(preview_image_fetcher)
      .Run(base::BindOnce(&QuickInsertGifView::OnPreviewImageFetched,
                          weak_factory_.GetWeakPtr()));
  std::move(frames_fetcher)
      .Run(base::BindOnce(&QuickInsertGifView::OnFramesFetched,
                          weak_factory_.GetWeakPtr()));
}

QuickInsertGifView::~QuickInsertGifView() = default;

gfx::Size QuickInsertGifView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  // Calculate the height to retain aspect ratio.
  const int preferred_width =
      views::ImageView::CalculatePreferredSize(available_size).width();
  const int height = original_dimensions_.width() == 0
                         ? 0
                         : (preferred_width * original_dimensions_.height()) /
                               original_dimensions_.width();
  return gfx::Size(preferred_width, height);
}

void QuickInsertGifView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  views::ImageView::OnBoundsChanged(previous_bounds);

  SkPath path;
  path.addRoundRect(gfx::RectToSkRect(GetImageBounds()),
                    SkIntToScalar(kQuickInsertGifCornerRadius),
                    SkIntToScalar(kQuickInsertGifCornerRadius));
  SetClipPath(path);
}

void QuickInsertGifView::UpdateFrame() {
  CHECK(next_frame_index_ < frames_.size());
  SetImage(ui::ImageModel::FromImageSkia(frames_[next_frame_index_].image));

  // Schedule next frame update.
  update_frame_timer_.Start(FROM_HERE, frames_[next_frame_index_].duration,
                            base::BindOnce(&QuickInsertGifView::UpdateFrame,
                                           weak_factory_.GetWeakPtr()));
  next_frame_index_ = (next_frame_index_ + 1) % frames_.size();
}

void QuickInsertGifView::OnFramesFetched(
    std::vector<image_util::AnimationFrame> frames) {
  if (frames.empty()) {
    // TODO: b/316936723 - Handle frames being empty.
    return;
  }

  frames_.reserve(frames.size());
  for (auto& frame : frames) {
    if (frame.duration <= kShortFrameDurationThreshold) {
      frame.duration = kAdjustedDurationForShortFrames;
    }
    frames_.push_back(std::move(frame));
  }

  // Start gif from the first frame.
  next_frame_index_ = 0;
  UpdateFrame();
  RecordFetchFramesTime();
}

void QuickInsertGifView::OnPreviewImageFetched(
    const gfx::ImageSkia& preview_image) {
  // Only show preview image if gif frames have not already been fetched.
  if (frames_.empty()) {
    SetImage(ui::ImageModel::FromImageSkia(preview_image));

    if (!preview_image.isNull()) {
      RecordFetchFramesTime();
    }
  }
}

void QuickInsertGifView::RecordFetchFramesTime() {
  if (fetch_frames_start_time_.has_value()) {
    UmaHistogramCustomTimes("Ash.Picker.TimeToFirstGifFrame",
                            base::TimeTicks::Now() - *fetch_frames_start_time_,
                            base::Milliseconds(0), base::Seconds(1),
                            /*buckets=*/50);
    fetch_frames_start_time_ = std::nullopt;
  }
}

BEGIN_METADATA(QuickInsertGifView)
END_METADATA

}  // namespace ash