Flutter Engine
compositor_context.cc
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 #include "flutter/flow/compositor_context.h"
6 
7 #include "flutter/flow/layers/layer_tree.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 
10 namespace flutter {
11 
13  : raster_time_(frame_budget), ui_time_(frame_budget) {}
14 
16 
17 void CompositorContext::BeginFrame(ScopedFrame& frame,
18  bool enable_instrumentation) {
19  if (enable_instrumentation) {
20  frame_count_.Increment();
21  raster_time_.Start();
22  }
23 }
24 
25 void CompositorContext::EndFrame(ScopedFrame& frame,
26  bool enable_instrumentation) {
27  raster_cache_.SweepAfterFrame();
28  if (enable_instrumentation) {
29  raster_time_.Stop();
30  }
31 }
32 
33 std::unique_ptr<CompositorContext::ScopedFrame> CompositorContext::AcquireFrame(
34  GrDirectContext* gr_context,
35  SkCanvas* canvas,
36  ExternalViewEmbedder* view_embedder,
37  const SkMatrix& root_surface_transformation,
38  bool instrumentation_enabled,
39  bool surface_supports_readback,
40  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {
41  return std::make_unique<ScopedFrame>(
42  *this, gr_context, canvas, view_embedder, root_surface_transformation,
43  instrumentation_enabled, surface_supports_readback, raster_thread_merger);
44 }
45 
47  CompositorContext& context,
48  GrDirectContext* gr_context,
49  SkCanvas* canvas,
50  ExternalViewEmbedder* view_embedder,
51  const SkMatrix& root_surface_transformation,
52  bool instrumentation_enabled,
53  bool surface_supports_readback,
54  fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)
55  : context_(context),
56  gr_context_(gr_context),
57  canvas_(canvas),
58  view_embedder_(view_embedder),
59  root_surface_transformation_(root_surface_transformation),
60  instrumentation_enabled_(instrumentation_enabled),
61  surface_supports_readback_(surface_supports_readback),
62  raster_thread_merger_(raster_thread_merger) {
63  context_.BeginFrame(*this, instrumentation_enabled_);
64 }
65 
67  context_.EndFrame(*this, instrumentation_enabled_);
68 }
69 
71  flutter::LayerTree& layer_tree,
72  bool ignore_raster_cache) {
73  TRACE_EVENT0("flutter", "CompositorContext::ScopedFrame::Raster");
74  bool root_needs_readback = layer_tree.Preroll(*this, ignore_raster_cache);
75  bool needs_save_layer = root_needs_readback && !surface_supports_readback();
76  PostPrerollResult post_preroll_result = PostPrerollResult::kSuccess;
77  if (view_embedder_ && raster_thread_merger_) {
78  post_preroll_result =
79  view_embedder_->PostPrerollAction(raster_thread_merger_);
80  }
81 
82  if (post_preroll_result == PostPrerollResult::kResubmitFrame) {
84  }
85  if (post_preroll_result == PostPrerollResult::kSkipAndRetryFrame) {
87  }
88  // Clearing canvas after preroll reduces one render target switch when preroll
89  // paints some raster cache.
90  if (canvas()) {
91  if (needs_save_layer) {
92  FML_LOG(INFO) << "Using SaveLayer to protect non-readback surface";
93  SkRect bounds = SkRect::Make(layer_tree.frame_size());
94  SkPaint paint;
95  paint.setBlendMode(SkBlendMode::kSrc);
96  canvas()->saveLayer(&bounds, &paint);
97  }
98  canvas()->clear(SK_ColorTRANSPARENT);
99  }
100  layer_tree.Paint(*this, ignore_raster_cache);
101  if (canvas() && needs_save_layer) {
102  canvas()->restore();
103  }
104  return RasterStatus::kSuccess;
105 }
106 
108  texture_registry_.OnGrContextCreated();
109  raster_cache_.Clear();
110 }
111 
113  texture_registry_.OnGrContextDestroyed();
114  raster_cache_.Clear();
115 }
116 
117 } // namespace flutter
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
const SkISize & frame_size() const
Definition: layer_tree.h:49
void Increment(size_t count=1)
ScopedFrame(CompositorContext &context, GrDirectContext *gr_context, SkCanvas *canvas, ExternalViewEmbedder *view_embedder, const SkMatrix &root_surface_transformation, bool instrumentation_enabled, bool surface_supports_readback, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger)
#define FML_LOG(severity)
Definition: logging.h:65
virtual PostPrerollResult PostPrerollAction(fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger)
CompositorContext(fml::Milliseconds frame_budget=fml::kDefaultFrameBudget)
bool Preroll(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false)
Definition: layer_tree.cc:32
virtual std::unique_ptr< ScopedFrame > AcquireFrame(GrDirectContext *gr_context, SkCanvas *canvas, ExternalViewEmbedder *view_embedder, const SkMatrix &root_surface_transformation, bool instrumentation_enabled, bool surface_supports_readback, fml::RefPtr< fml::RasterThreadMerger > raster_thread_merger)
void Paint(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false) const
Definition: layer_tree.cc:89
std::chrono::duration< double, std::milli > Milliseconds
Definition: time_delta.h:18
virtual RasterStatus Raster(LayerTree &layer_tree, bool ignore_raster_cache)