Flutter Engine
compositor_context.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
6 #define FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "flutter/flow/embedded_views.h"
12 #include "flutter/flow/instrumentation.h"
13 #include "flutter/flow/raster_cache.h"
14 #include "flutter/flow/texture.h"
15 #include "flutter/fml/macros.h"
16 #include "flutter/fml/raster_thread_merger.h"
17 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/gpu/GrDirectContext.h"
19 
20 namespace flutter {
21 
22 class LayerTree;
23 
24 enum class RasterStatus {
25  // Frame has successfully rasterized.
26  kSuccess,
27  // Frame is submitted twice. This is only used on Android when
28  // switching the background surface to FlutterImageView.
29  //
30  // On Android, the first frame doesn't make the image available
31  // to the ImageReader right away. The second frame does.
32  //
33  // TODO(egarciad): https://github.com/flutter/flutter/issues/65652
34  kResubmit,
35  // Frame is dropped and a new frame with the same layer tree is
36  // attempted.
37  //
38  // This is currently used to wait for the thread merger to merge
39  // the raster and platform threads.
40  //
41  // Since the thread merger may be disabled,
43  // Frame has been successfully rasterized, but "there are additional items in
44  // the pipeline waiting to be consumed. This is currently
45  // only used when thread configuration change occurs.
47  // Failed to rasterize the frame.
48  kFailed,
49  // Layer tree was discarded due to LayerTreeDiscardCallback
51 };
52 
54  public:
55  class ScopedFrame {
56  public:
58  GrDirectContext* gr_context,
59  SkCanvas* canvas,
60  ExternalViewEmbedder* view_embedder,
61  const SkMatrix& root_surface_transformation,
62  bool instrumentation_enabled,
63  bool surface_supports_readback,
64  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger);
65 
66  virtual ~ScopedFrame();
67 
68  SkCanvas* canvas() { return canvas_; }
69 
70  ExternalViewEmbedder* view_embedder() { return view_embedder_; }
71 
72  CompositorContext& context() const { return context_; }
73 
74  const SkMatrix& root_surface_transformation() const {
75  return root_surface_transformation_;
76  }
77 
78  bool surface_supports_readback() { return surface_supports_readback_; }
79 
80  GrDirectContext* gr_context() const { return gr_context_; }
81 
82  virtual RasterStatus Raster(LayerTree& layer_tree,
83  bool ignore_raster_cache);
84 
85  private:
86  CompositorContext& context_;
87  GrDirectContext* gr_context_;
88  SkCanvas* canvas_;
89  ExternalViewEmbedder* view_embedder_;
90  const SkMatrix& root_surface_transformation_;
91  const bool instrumentation_enabled_;
92  const bool surface_supports_readback_;
93  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
94 
96  };
97 
99 
100  virtual ~CompositorContext();
101 
102  virtual std::unique_ptr<ScopedFrame> AcquireFrame(
103  GrDirectContext* gr_context,
104  SkCanvas* canvas,
105  ExternalViewEmbedder* view_embedder,
106  const SkMatrix& root_surface_transformation,
107  bool instrumentation_enabled,
108  bool surface_supports_readback,
109  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger);
110 
111  void OnGrContextCreated();
112 
113  void OnGrContextDestroyed();
114 
115  RasterCache& raster_cache() { return raster_cache_; }
116 
117  TextureRegistry& texture_registry() { return texture_registry_; }
118 
119  const Counter& frame_count() const { return frame_count_; }
120 
121  const Stopwatch& raster_time() const { return raster_time_; }
122 
123  Stopwatch& ui_time() { return ui_time_; }
124 
125  private:
126  RasterCache raster_cache_;
127  TextureRegistry texture_registry_;
128  Counter frame_count_;
129  Stopwatch raster_time_;
130  Stopwatch ui_time_;
131 
132  void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);
133 
134  void EndFrame(ScopedFrame& frame, bool enable_instrumentation);
135 
137 };
138 
139 } // namespace flutter
140 
141 #endif // FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
const SkMatrix & root_surface_transformation() const
TextureRegistry & texture_registry()
constexpr Milliseconds kDefaultFrameBudget
Definition: time_delta.h:21
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
const Counter & frame_count() const
const Stopwatch & raster_time() const
std::chrono::duration< double, std::milli > Milliseconds
Definition: time_delta.h:18