layer_tree_holder.h 1.6 KB
Newer Older
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_COMMON_LAYER_TREE_HOLDER_H_
#define FLUTTER_SHELL_COMMON_LAYER_TREE_HOLDER_H_

#include <memory>

#include "flow/layers/layer_tree.h"

namespace flutter {

/**
 * @brief Holds the next `flutter::LayerTree` that needs to be rasterized. The
 * accesses to `LayerTreeHolder` are thread safe. This is important as this
 * component is accessed from both the UI and the Raster threads.
 *
 * A typical flow of events through this component would be:
 *  1. `flutter::Animator` pushed a layer tree to be rendered during each
 * `Animator::Render` call.
 *  2. `flutter::Rasterizer::Draw` consumes the pushed layer tree via `Pop`.
 *
 * It is important to note that if a layer tree held by this class is yet to be
 * consumed, it can be overriden by a newer layer tree produced by the
 * `Animator`. The newness of the layer tree is determined by the target time.
 */
class LayerTreeHolder {
 public:
  LayerTreeHolder();

  ~LayerTreeHolder();

  /**
   * @brief Checks if a layer tree is currently held.
   *
   * @return true is no layer tree is held.
   * @return false if there is a layer tree waiting to be consumed.
   */
  bool IsEmpty() const;

  [[nodiscard]] std::unique_ptr<LayerTree> Pop();

  void PushIfNewer(std::unique_ptr<LayerTree> proposed_layer_tree);

 private:
  mutable std::mutex layer_tree_mutex;
  std::unique_ptr<LayerTree> layer_tree_;

  FML_DISALLOW_COPY_AND_ASSIGN(LayerTreeHolder);
};

};  // namespace flutter

#endif  // FLUTTER_SHELL_COMMON_LAYER_TREE_HOLDER_H_