• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu_thread_merger.h"
16 #include "flutter/fml/macros.h"
17 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkPictureRecorder.h"
19 
20 namespace flutter {
21 
22 class LayerTree;
23 
24 enum class RasterStatus {
25   // Frame has successfully rasterized.
26   kSuccess,
27   // Frame needs to be resubmitted for rasterization. This is
28   // currently only called when thread configuration change occurs.
29   kResubmit,
30   // Frame has been successfully rasterized, but "there are additional items in
31   // the pipeline waiting to be consumed. This is currently
32   // only called when thread configuration change occurs.
33   kEnqueuePipeline,
34   // Failed to rasterize the frame.
35   kFailed
36 };
37 
38 class CompositorContext {
39  public:
40   class ScopedFrame {
41    public:
42     ScopedFrame(CompositorContext& context,
43                 GrContext* gr_context,
44                 SkCanvas* canvas,
45                 ExternalViewEmbedder* view_embedder,
46                 const SkMatrix& root_surface_transformation,
47                 bool instrumentation_enabled,
48                 fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger);
49 
50     virtual ~ScopedFrame();
51 
canvas()52     SkCanvas* canvas() { return canvas_; }
53 
view_embedder()54     ExternalViewEmbedder* view_embedder() { return view_embedder_; }
55 
context()56     CompositorContext& context() const { return context_; }
57 
root_surface_transformation()58     const SkMatrix& root_surface_transformation() const {
59       return root_surface_transformation_;
60     }
61 
gr_context()62     GrContext* gr_context() const { return gr_context_; }
63 
64     virtual RasterStatus Raster(LayerTree& layer_tree,
65                                 bool ignore_raster_cache);
66 
67    private:
68     CompositorContext& context_;
69     GrContext* gr_context_;
70     SkCanvas* canvas_;
71     ExternalViewEmbedder* view_embedder_;
72     const SkMatrix& root_surface_transformation_;
73     const bool instrumentation_enabled_;
74     fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger_;
75 
76     FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
77   };
78 
79   CompositorContext();
80 
81   virtual ~CompositorContext();
82 
83   virtual std::unique_ptr<ScopedFrame> AcquireFrame(
84       GrContext* gr_context,
85       SkCanvas* canvas,
86       ExternalViewEmbedder* view_embedder,
87       const SkMatrix& root_surface_transformation,
88       bool instrumentation_enabled,
89       fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger);
90 
91   void OnGrContextCreated();
92 
93   void OnGrContextDestroyed();
94 
raster_cache()95   RasterCache& raster_cache() { return raster_cache_; }
96 
texture_registry()97   TextureRegistry& texture_registry() { return texture_registry_; }
98 
frame_count()99   const Counter& frame_count() const { return frame_count_; }
100 
raster_time()101   const Stopwatch& raster_time() const { return raster_time_; }
102 
ui_time()103   Stopwatch& ui_time() { return ui_time_; }
104 
105  private:
106   RasterCache raster_cache_;
107   TextureRegistry texture_registry_;
108   Counter frame_count_;
109   Stopwatch raster_time_;
110   Stopwatch ui_time_;
111 
112   void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);
113 
114   void EndFrame(ScopedFrame& frame, bool enable_instrumentation);
115 
116   FML_DISALLOW_COPY_AND_ASSIGN(CompositorContext);
117 };
118 
119 }  // namespace flutter
120 
121 #endif  // FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
122