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

cc / slim / frame_sink_impl.h [blame]

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

#ifndef CC_SLIM_FRAME_SINK_IMPL_H_
#define CC_SLIM_FRAME_SINK_IMPL_H_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "cc/resources/ui_resource_bitmap.h"
#include "cc/resources/ui_resource_client.h"
#include "cc/slim/frame_sink.h"
#include "cc/slim/scheduler.h"
#include "components/viz/client/client_resource_provider.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_timing_details_map.h"
#include "components/viz/common/gpu/context_lost_observer.h"
#include "components/viz/common/gpu/raster_context_provider.h"
#include "components/viz/common/hit_test/hit_test_region_list.h"
#include "components/viz/common/surfaces/local_surface_id.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/viz/public/mojom/compositing/compositor_frame_sink.mojom.h"

namespace gpu {
class ClientSharedImage;
}

namespace cc::slim {

class FrameSinkImplClient;
class Scheduler;
class TestFrameSinkImpl;

// Slim implementation of FrameSink.
// * Owns mojo interfaces to viz and responsible for submitting frames and
//   issuing BeginFrame to client.
// * Owns context provider.
// * Listen and respond to context loss or GPU process crashes.
// * Manage uploading UIResource.
class COMPONENT_EXPORT(CC_SLIM) FrameSinkImpl
    : public FrameSink,
      public SchedulerClient,
      public viz::ContextLostObserver,
      public viz::mojom::CompositorFrameSinkClient {
 public:
  ~FrameSinkImpl() override;

  viz::RasterContextProvider* context_provider() const {
    return context_provider_.get();
  }
  void SetLocalSurfaceId(const viz::LocalSurfaceId& local_surface_id);

  // Called by LayerTree. Virtual for testing.
  virtual bool BindToClient(FrameSinkImplClient* client);
  virtual void SetNeedsBeginFrame(bool needs_begin_frame);
  void MaybeCompositeNow();
  void UploadUIResource(cc::UIResourceId resource_id,
                        cc::UIResourceBitmap resource_bitmap);
  void MarkUIResourceForDeletion(cc::UIResourceId resource_id);
  viz::ResourceId GetVizResourceId(cc::UIResourceId id);
  bool IsUIResourceOpaque(cc::UIResourceId resource_id);
  gfx::Size GetUIResourceSize(cc::UIResourceId resource_id);
  viz::ClientResourceProvider* client_resource_provider() {
    return &resource_provider_;
  }
  int GetMaxTextureSize() const;

  // viz::ContextLostObserver
  void OnContextLost() override;

  // mojom::CompositorFrameSinkClient implementation:
  void DidReceiveCompositorFrameAck(
      std::vector<viz::ReturnedResource> resources) override;
  void OnBeginFramePausedChanged(bool paused) override {}
  void ReclaimResources(std::vector<viz::ReturnedResource> resources) override;
  void OnCompositorFrameTransitionDirectiveProcessed(
      uint32_t sequence_id) override {}
  void OnSurfaceEvicted(const viz::LocalSurfaceId& local_surface_id) override {}
  void OnBeginFrame(const viz::BeginFrameArgs& begin_frame_args,
                    const viz::FrameTimingDetailsMap& timing_details,
                    bool frame_ack,
                    std::vector<viz::ReturnedResource> resources) override;

  // SchedulerClient:
  bool DoBeginFrame(const viz::BeginFrameArgs& begin_frame_args) override;
  void SendDidNotProduceFrame(
      const viz::BeginFrameArgs& begin_frame_args) override;

 private:
  friend class FrameSink;
  friend class TestFrameSinkImpl;

  struct UploadedUIResource {
    UploadedUIResource();
    ~UploadedUIResource();
    UploadedUIResource(const UploadedUIResource&);
    UploadedUIResource& operator=(const UploadedUIResource&);

    scoped_refptr<gpu::ClientSharedImage> shared_image;
    gfx::Size size;
    bool is_opaque = true;
    viz::ResourceId viz_resource_id;
  };

  FrameSinkImpl(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
                mojo::PendingAssociatedRemote<viz::mojom::CompositorFrameSink>
                    compositor_frame_sink_associated_remote,
                mojo::PendingReceiver<viz::mojom::CompositorFrameSinkClient>
                    client_receiver,
                scoped_refptr<viz::RasterContextProvider> context_provider,
                base::PlatformThreadId io_thread_id,
                std::unique_ptr<Scheduler> scheduler);

  using UploadedResourceMap =
      base::flat_map<cc::UIResourceId, UploadedUIResource>;
  void UIResourceReleased(cc::UIResourceId ui_resource_id,
                          const gpu::SyncToken& sync_token,
                          bool is_lost);

  const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  const std::unique_ptr<Scheduler> scheduler_;

  mojo::PendingAssociatedRemote<viz::mojom::CompositorFrameSink>
      pending_compositor_frame_sink_associated_remote_;
  mojo::PendingReceiver<viz::mojom::CompositorFrameSinkClient>
      pending_client_receiver_;

  mojo::AssociatedRemote<viz::mojom::CompositorFrameSink> frame_sink_remote_;
  // Separate from AssociatedRemote above for testing.
  raw_ptr<viz::mojom::CompositorFrameSink, DanglingUntriaged> frame_sink_ =
      nullptr;
  mojo::Receiver<viz::mojom::CompositorFrameSinkClient> client_receiver_{this};
  scoped_refptr<viz::RasterContextProvider> context_provider_;
  raw_ptr<FrameSinkImplClient> client_ = nullptr;
  viz::LocalSurfaceId local_surface_id_;

  UploadedResourceMap uploaded_resources_;
  viz::ClientResourceProvider resource_provider_;
  // Last `HitTestRegionList` sent to viz.
  std::optional<viz::HitTestRegionList> hit_test_region_list_;
  base::PlatformThreadId io_thread_id_;

  viz::LocalSurfaceId last_submitted_local_surface_id_;
  float last_submitted_device_scale_factor_ = 1.f;
  gfx::Size last_submitted_size_in_pixels_;

  uint32_t num_unacked_frames_ = 0u;
  bool needs_begin_frame_ = false;
};

}  // namespace cc::slim

#endif  // CC_SLIM_FRAME_SINK_IMPL_H_