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

cc / view_transition / view_transition_request.cc [blame]

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

#include "cc/view_transition/view_transition_request.h"

#include <map>
#include <memory>
#include <sstream>
#include <utility>
#include <vector>

#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/ranges/algorithm.h"
#include "components/viz/common/quads/compositor_frame_transition_directive.h"
#include "components/viz/common/quads/compositor_render_pass.h"

namespace cc {
namespace {

std::string TypeToString(viz::CompositorFrameTransitionDirective::Type type) {
  switch (type) {
    case viz::CompositorFrameTransitionDirective::Type::kSave:
      return "kSave";
    case viz::CompositorFrameTransitionDirective::Type::kAnimateRenderer:
      return "kAnimateRenderer";
    case viz::CompositorFrameTransitionDirective::Type::kRelease:
      return "kRelease";
  }
  return "<unknown>";
}

}  // namespace

uint32_t ViewTransitionRequest::s_next_sequence_id_ = 1;

// static
std::unique_ptr<ViewTransitionRequest> ViewTransitionRequest::CreateCapture(
    const blink::ViewTransitionToken& transition_token,
    bool maybe_cross_frame_sink,
    std::vector<viz::ViewTransitionElementResourceId> capture_ids,
    ViewTransitionCaptureCallback commit_callback) {
  return base::WrapUnique(new ViewTransitionRequest(
      Type::kSave, transition_token, maybe_cross_frame_sink,
      std::move(capture_ids), std::move(commit_callback)));
}

// static
std::unique_ptr<ViewTransitionRequest>
ViewTransitionRequest::CreateAnimateRenderer(
    const blink::ViewTransitionToken& transition_token,
    bool maybe_cross_frame_sink) {
  return base::WrapUnique(new ViewTransitionRequest(
      Type::kAnimateRenderer, transition_token, maybe_cross_frame_sink, {},
      ViewTransitionCaptureCallback()));
}

// static
std::unique_ptr<ViewTransitionRequest> ViewTransitionRequest::CreateRelease(
    const blink::ViewTransitionToken& transition_token,
    bool maybe_cross_frame_sink) {
  return base::WrapUnique(new ViewTransitionRequest(
      Type::kRelease, transition_token, maybe_cross_frame_sink, {},
      ViewTransitionCaptureCallback()));
}

ViewTransitionRequest::ViewTransitionRequest(
    Type type,
    const blink::ViewTransitionToken& transition_token,
    bool maybe_cross_frame_sink,
    std::vector<viz::ViewTransitionElementResourceId> capture_ids,
    ViewTransitionCaptureCallback commit_callback)
    : type_(type),
      transition_token_(transition_token),
      maybe_cross_frame_sink_(maybe_cross_frame_sink),
      commit_callback_(std::move(commit_callback)),
      sequence_id_(s_next_sequence_id_++),
      capture_resource_ids_(std::move(capture_ids)) {
  DCHECK(type_ == Type::kSave || !commit_callback_);
}

ViewTransitionRequest::~ViewTransitionRequest() = default;

viz::CompositorFrameTransitionDirective
ViewTransitionRequest::ConstructDirective(
    const ViewTransitionElementMap& shared_element_render_pass_id_map,
    const gfx::DisplayColorSpaces& display_color_spaces) const {
  switch (type_) {
    case Type::kRelease:
      DCHECK(capture_resource_ids_.empty());
      return viz::CompositorFrameTransitionDirective::CreateRelease(
          transition_token_, maybe_cross_frame_sink_, sequence_id_);
    case Type::kAnimateRenderer:
      DCHECK(capture_resource_ids_.empty());
      return viz::CompositorFrameTransitionDirective::CreateAnimate(
          transition_token_, maybe_cross_frame_sink_, sequence_id_);
    case Type::kSave:
      break;
  }

  std::vector<viz::CompositorFrameTransitionDirective::SharedElement>
      shared_elements(capture_resource_ids_.size());

  for (size_t i = 0; i < shared_elements.size(); i++) {
    const auto& capture_resource_id = capture_resource_ids_[i];
    shared_elements[i].view_transition_element_resource_id =
        capture_resource_id;

    auto it = shared_element_render_pass_id_map.find(capture_resource_id);
    if (it != shared_element_render_pass_id_map.end()) {
      shared_elements[i].render_pass_id = it->second;
    }
  }

  return viz::CompositorFrameTransitionDirective::CreateSave(
      transition_token_, maybe_cross_frame_sink_, sequence_id_,
      std::move(shared_elements), display_color_spaces);
}

std::string ViewTransitionRequest::ToString() const {
  std::ostringstream str;
  str << "[type: " << TypeToString(type_) << " sequence_id: " << sequence_id_
      << "]";
  return str.str();
}

}  // namespace cc