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

cc / layers / tile_display_layer_impl.h [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.

#ifndef CC_LAYERS_TILE_DISPLAY_LAYER_IMPL_H_
#define CC_LAYERS_TILE_DISPLAY_LAYER_IMPL_H_

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

#include "base/memory/raw_ref.h"
#include "cc/base/tiling_data.h"
#include "cc/cc_export.h"
#include "cc/layers/layer_impl.h"
#include "cc/tiles/tile_index.h"
#include "cc/tiles/tile_priority.h"
#include "cc/tiles/tiling_coverage_iterator.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace cc {

// Viz-side counterpart to a client-side PictureLayerImpl when TreesInViz is
// enabled. Clients push tiling information and tile contents from a picture
// layer down to Viz, and this layer uses that information to draw tile quads.
class CC_EXPORT TileDisplayLayerImpl : public LayerImpl {
 public:
  class CC_EXPORT Client {
   public:
    virtual ~Client() = default;
    virtual void DidAppendQuadsWithResources(
        const std::vector<viz::TransferableResource>& resource) = 0;
  };

  struct NoContents {};

  struct CC_EXPORT TileResource {
    TileResource(const viz::TransferableResource& resource,
                 bool is_premultiplied,
                 bool is_checkered);
    TileResource(const TileResource&);
    TileResource& operator=(const TileResource&);
    ~TileResource();

    viz::TransferableResource resource;
    bool is_premultiplied;
    bool is_checkered;
  };

  using TileContents = absl::variant<NoContents, SkColor4f, TileResource>;

  class CC_EXPORT Tile {
   public:
    Tile();
    explicit Tile(const TileContents& contents);
    Tile(Tile&&);
    Tile& operator=(Tile&&);
    ~Tile();

    const TileContents& contents() const { return contents_; }

    std::optional<SkColor4f> solid_color() const {
      if (absl::holds_alternative<SkColor4f>(contents_)) {
        return absl::get<SkColor4f>(contents_);
      }
      return std::nullopt;
    }

    std::optional<TileResource> resource() const {
      if (absl::holds_alternative<TileResource>(contents_)) {
        return absl::get<TileResource>(contents_);
      }
      return std::nullopt;
    }

    // We only construct Tile objects that are ready to draw.
    bool IsReadyToDraw() const { return true; }

   private:
    TileContents contents_;
  };

  class DisplayTilingCoverageIterator;

  class CC_EXPORT Tiling {
   public:
    using Tile = Tile;
    using TileMap = std::map<TileIndex, std::unique_ptr<Tile>>;
    using CoverageIterator = DisplayTilingCoverageIterator;

    explicit Tiling(TileDisplayLayerImpl& layer, float scale_key);
    ~Tiling();

    Tile* TileAt(const TileIndex& index) const;

    float contents_scale_key() const { return scale_key_; }
    TileResolution resolution() const { return HIGH_RESOLUTION; }
    const TilingData* tiling_data() const { return &tiling_data_; }
    gfx::Size raster_size() const { return layer_->bounds(); }
    const gfx::AxisTransform2d& raster_transform() const {
      return raster_transform_;
    }
    const gfx::Size tile_size() const {
      return tiling_data_.max_texture_size();
    }
    const gfx::Rect tiling_rect() const { return tiling_data_.tiling_rect(); }
    const TileMap& tiles() const { return tiles_; }

    void SetRasterTransform(const gfx::AxisTransform2d& transform);
    void SetTileSize(const gfx::Size& size);
    void SetTilingRect(const gfx::Rect& rect);
    void SetTileContents(const TileIndex& key, const TileContents& contents);

    CoverageIterator Cover(const gfx::Rect& coverage_rect,
                           float coverage_scale) const;

   private:
    const raw_ref<TileDisplayLayerImpl> layer_;
    const float scale_key_;
    gfx::AxisTransform2d raster_transform_;
    TilingData tiling_data_{gfx::Size(), gfx::Rect(), /*border_texels=*/1};
    TileMap tiles_;
  };

  class CC_EXPORT DisplayTilingCoverageIterator
      : public TilingCoverageIterator<Tiling> {
   public:
    using TilingCoverageIterator<Tiling>::TilingCoverageIterator;
  };

  TileDisplayLayerImpl(Client& client, LayerTreeImpl& tree, int id);
  ~TileDisplayLayerImpl() override;

  Tiling& GetOrCreateTilingFromScaleKey(float scale_key);

  // LayerImpl overrides:
  mojom::LayerType GetLayerType() const override;
  std::unique_ptr<LayerImpl> CreateLayerImpl(
      LayerTreeImpl* tree_impl) const override;
  void PushPropertiesTo(LayerImpl* layer) override;
  void AppendQuads(viz::CompositorRenderPass* render_pass,
                   AppendQuadsData* append_quads_data) override;

 private:
  raw_ref<Client> client_;
  std::vector<std::unique_ptr<Tiling>> tilings_;
  std::vector<viz::TransferableResource> discarded_resources_;
};

}  // namespace cc

#endif  // CC_LAYERS_TILE_DISPLAY_LAYER_IMPL_H_