scene_update_context.h 6.2 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_
#define FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_
7

8 9 10 11 12
#include <fuchsia/ui/views/cpp/fidl.h>
#include <lib/ui/scenic/cpp/resources.h>
#include <lib/ui/scenic/cpp/session.h>
#include <lib/ui/scenic/cpp/view_ref_pair.h>

13
#include <cfloat>
14
#include <memory>
15
#include <set>
16 17
#include <vector>

18
#include "flutter/flow/embedded_views.h"
19
#include "flutter/flow/view_holder.h"
20 21
#include "flutter/fml/logging.h"
#include "flutter/fml/macros.h"
22 23 24
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkSurface.h"

25
namespace flutter {
26

27
class Layer;
28

29 30 31 32 33 34 35 36 37
// Scenic currently lacks an API to enable rendering of alpha channel; this only
// happens if there is a OpacityNode higher in the tree with opacity != 1. For
// now, clamp to a infinitesimally smaller value than 1, which does not cause
// visual problems in practice.
constexpr float kOneMinusEpsilon = 1 - FLT_EPSILON;

// How much layers are separated in Scenic z elevation.
constexpr float kScenicZElevationBetweenLayers = 10.f;

38
class SessionWrapper {
39
 public:
40
  virtual ~SessionWrapper() {}
41

42 43 44
  virtual scenic::Session* get() = 0;
  virtual void Present() = 0;
};
45

46 47
class SceneUpdateContext : public flutter::ExternalViewEmbedder {
 public:
48 49
  class Entity {
   public:
50
    Entity(std::shared_ptr<SceneUpdateContext> context);
51
    virtual ~Entity();
52

53
    std::shared_ptr<SceneUpdateContext> context() { return context_; }
54 55
    scenic::EntityNode& entity_node() { return entity_node_; }
    virtual scenic::ContainerNode& embedder_node() { return entity_node_; }
56

57
   private:
58
    std::shared_ptr<SceneUpdateContext> context_;
59
    Entity* const previous_entity_;
60

61
    scenic::EntityNode entity_node_;
62
  };
63

64 65
  class Transform : public Entity {
   public:
66 67 68
    Transform(std::shared_ptr<SceneUpdateContext> context,
              const SkMatrix& transform);
    Transform(std::shared_ptr<SceneUpdateContext> context,
69 70 71
              float scale_x,
              float scale_y,
              float scale_z);
72
    virtual ~Transform();
73 74 75 76

   private:
    float const previous_scale_x_;
    float const previous_scale_y_;
77 78
  };

79
  class Frame : public Entity {
80
   public:
81 82 83
    // When layer is not nullptr, the frame is associated with a layer subtree
    // rooted with that layer. The frame may then create a surface that will be
    // retained for that layer.
84
    Frame(std::shared_ptr<SceneUpdateContext> context,
85 86
          const SkRRect& rrect,
          SkColor color,
87
          SkAlpha opacity,
88
          std::string label);
89
    virtual ~Frame();
90

91
    scenic::ContainerNode& embedder_node() override { return opacity_node_; }
92

93
    void AddPaintLayer(Layer* layer);
94 95

   private:
96 97
    const float previous_elevation_;

98 99 100
    const SkRRect rrect_;
    SkColor const color_;
    SkAlpha const opacity_;
101

102
    scenic::OpacityNodeHACK opacity_node_;
103
    std::vector<Layer*> paint_layers_;
104 105
    SkRect paint_bounds_;
  };
106

107 108
  class Clip : public Entity {
   public:
109 110
    Clip(std::shared_ptr<SceneUpdateContext> context,
         const SkRect& shape_bounds);
111
    ~Clip() = default;
112 113
  };

114 115 116 117 118 119 120 121
  struct PaintTask {
    SkRect paint_bounds;
    SkScalar scale_x;
    SkScalar scale_y;
    SkColor background_color;
    scenic::Material material;
    std::vector<Layer*> layers;
  };
122

123 124 125
  SceneUpdateContext(std::string debug_label,
                     fuchsia::ui::views::ViewToken view_token,
                     scenic::ViewRefPair view_ref_pair,
126 127
                     SessionWrapper& session,
                     bool intercept_all_input = false);
128
  ~SceneUpdateContext() = default;
129

130 131 132 133
  // The cumulative alpha value based on all the parent OpacityLayers.
  void set_alphaf(float alpha) { alpha_ = alpha; }
  float alphaf() { return alpha_; }

134 135
  // Returns all `PaintTask`s generated for the current frame.
  std::vector<PaintTask> GetPaintTasks();
136

137 138
  // Enable/disable wireframe rendering around the root view bounds.
  void EnableWireframe(bool enable);
139

140
  // Reset state for a new frame.
141
  void Reset(const SkISize& frame_size, float device_pixel_ratio);
142

143 144 145 146 147 148 149 150 151
  // |ExternalViewEmbedder|
  SkCanvas* GetRootCanvas() override { return nullptr; }

  // |ExternalViewEmbedder|
  void CancelFrame() override {}

  // |ExternalViewEmbedder|
  void BeginFrame(
      SkISize frame_size,
152
      GrDirectContext* context,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      double device_pixel_ratio,
      fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override {}

  // |ExternalViewEmbedder|
  void PrerollCompositeEmbeddedView(
      int view_id,
      std::unique_ptr<EmbeddedViewParams> params) override {}

  // |ExternalViewEmbedder|
  std::vector<SkCanvas*> GetCurrentCanvases() override {
    return std::vector<SkCanvas*>();
  }

  // |ExternalViewEmbedder|
  virtual SkCanvas* CompositeEmbeddedView(int view_id) override {
    return nullptr;
  }

171 172 173 174 175 176 177
  // View manipulation.
  void CreateView(int64_t view_id,
                  ViewHolder::ViewIdCallback on_view_created,
                  bool hit_testable,
                  bool focusable);
  void DestroyView(int64_t view_id,
                   ViewHolder::ViewIdCallback on_view_destroyed);
178
  void UpdateView(int64_t view_id, bool hit_testable, bool focusable);
179 180 181 182
  void UpdateView(int64_t view_id,
                  const SkPoint& offset,
                  const SkSize& size,
                  std::optional<bool> override_hit_testable = std::nullopt);
183

184
 private:
185
  void CreateFrame(scenic::EntityNode& entity_node,
186 187
                   const SkRRect& rrect,
                   SkColor color,
188
                   SkAlpha opacity,
189
                   const SkRect& paint_bounds,
190
                   std::vector<Layer*> paint_layers);
191

192
  SessionWrapper& session_;
193

194
  scenic::View root_view_;
195 196 197
  scenic::EntityNode metrics_node_;
  scenic::EntityNode layer_tree_node_;
  std::optional<scenic::ShapeNode> input_interceptor_node_;
198

199
  std::vector<PaintTask> paint_tasks_;
200

201 202 203 204
  Entity* top_entity_ = nullptr;
  float top_scale_x_ = 1.f;
  float top_scale_y_ = 1.f;
  float top_elevation_ = 0.f;
205

206 207
  float next_elevation_ = 0.f;
  float alpha_ = 1.f;
208

209
  FML_DISALLOW_COPY_AND_ASSIGN(SceneUpdateContext);
210 211
};

212
}  // namespace flutter
213

214
#endif  // FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_