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
20namespace flutter {
21
22class LayerTree;
23
24enum 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
38class CompositorContext {
39 public:
40 class ScopedFrame {
41 public:
42 ScopedFrame(CompositorContext& context,
43 GrDirectContext* gr_context,
44 SkCanvas* canvas,
45 ExternalViewEmbedder* view_embedder,
46 const SkMatrix& root_surface_transformation,
47 bool instrumentation_enabled,
48 bool surface_supports_readback,
49 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger);
50
51 virtual ~ScopedFrame();
52
53 SkCanvas* canvas() { return canvas_; }
54
55 ExternalViewEmbedder* view_embedder() { return view_embedder_; }
56
57 CompositorContext& context() const { return context_; }
58
59 const SkMatrix& root_surface_transformation() const {
60 return root_surface_transformation_;
61 }
62
63 bool surface_supports_readback() { return surface_supports_readback_; }
64
65 GrDirectContext* gr_context() const { return gr_context_; }
66
67 virtual RasterStatus Raster(LayerTree& layer_tree,
68 bool ignore_raster_cache);
69
70 private:
71 CompositorContext& context_;
72 GrDirectContext* gr_context_;
73 SkCanvas* canvas_;
74 ExternalViewEmbedder* view_embedder_;
75 const SkMatrix& root_surface_transformation_;
76 const bool instrumentation_enabled_;
77 const bool surface_supports_readback_;
78 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
79
80 FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
81 };
82
83 CompositorContext(fml::Milliseconds frame_budget = fml::kDefaultFrameBudget);
84
85 virtual ~CompositorContext();
86
87 virtual std::unique_ptr<ScopedFrame> AcquireFrame(
88 GrDirectContext* gr_context,
89 SkCanvas* canvas,
90 ExternalViewEmbedder* view_embedder,
91 const SkMatrix& root_surface_transformation,
92 bool instrumentation_enabled,
93 bool surface_supports_readback,
94 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger);
95
96 void OnGrContextCreated();
97
98 void OnGrContextDestroyed();
99
100 RasterCache& raster_cache() { return raster_cache_; }
101
102 TextureRegistry& texture_registry() { return texture_registry_; }
103
104 const Counter& frame_count() const { return frame_count_; }
105
106 const Stopwatch& raster_time() const { return raster_time_; }
107
108 Stopwatch& ui_time() { return ui_time_; }
109
110 private:
111 RasterCache raster_cache_;
112 TextureRegistry texture_registry_;
113 Counter frame_count_;
114 Stopwatch raster_time_;
115 Stopwatch ui_time_;
116
117 void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);
118
119 void EndFrame(ScopedFrame& frame, bool enable_instrumentation);
120
121 FML_DISALLOW_COPY_AND_ASSIGN(CompositorContext);
122};
123
124} // namespace flutter
125
126#endif // FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
127