Flutter Engine
scene.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/lib/ui/compositing/scene.h"
6 
7 #include "flutter/fml/trace_event.h"
8 #include "flutter/lib/ui/painting/image.h"
9 #include "flutter/lib/ui/painting/picture.h"
10 #include "flutter/lib/ui/ui_dart_state.h"
11 #include "flutter/lib/ui/window/platform_configuration.h"
12 #include "flutter/lib/ui/window/window.h"
13 #include "third_party/skia/include/core/SkImageInfo.h"
14 #include "third_party/skia/include/core/SkSurface.h"
19 
20 namespace flutter {
21 
23 
24 #define FOR_EACH_BINDING(V) \
25  V(Scene, toImage) \
26  V(Scene, dispose)
27 
29 
30 void Scene::create(Dart_Handle scene_handle,
31  std::shared_ptr<flutter::Layer> rootLayer,
32  uint32_t rasterizerTracingThreshold,
33  bool checkerboardRasterCacheImages,
34  bool checkerboardOffscreenLayers) {
35  auto scene = fml::MakeRefCounted<Scene>(
36  std::move(rootLayer), rasterizerTracingThreshold,
37  checkerboardRasterCacheImages, checkerboardOffscreenLayers);
38  scene->AssociateWithDartWrapper(scene_handle);
39 }
40 
41 Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer,
42  uint32_t rasterizerTracingThreshold,
43  bool checkerboardRasterCacheImages,
44  bool checkerboardOffscreenLayers) {
45  // Currently only supports a single window.
46  auto viewport_metrics = UIDartState::Current()
48  ->get_window(0)
49  ->viewport_metrics();
50 
51  layer_tree_ = std::make_unique<LayerTree>(
52  SkISize::Make(viewport_metrics.physical_width,
53  viewport_metrics.physical_height),
54  static_cast<float>(viewport_metrics.device_pixel_ratio));
55  layer_tree_->set_root_layer(std::move(rootLayer));
56  layer_tree_->set_rasterizer_tracing_threshold(rasterizerTracingThreshold);
57  layer_tree_->set_checkerboard_raster_cache_images(
58  checkerboardRasterCacheImages);
59  layer_tree_->set_checkerboard_offscreen_layers(checkerboardOffscreenLayers);
60 }
61 
63 
65  layer_tree_.reset();
67 }
68 
69 Dart_Handle Scene::toImage(uint32_t width,
70  uint32_t height,
71  Dart_Handle raw_image_callback) {
72  TRACE_EVENT0("flutter", "Scene::toImage");
73 
74  if (!layer_tree_) {
75  return tonic::ToDart("Scene did not contain a layer tree.");
76  }
77 
78  auto picture = layer_tree_->Flatten(SkRect::MakeWH(width, height));
79  if (!picture) {
80  return tonic::ToDart("Could not flatten scene into a layer tree.");
81  }
82 
83  return Picture::RasterizeToImage(picture, width, height, raw_image_callback);
84 }
85 
86 std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree() {
87  return std::move(layer_tree_);
88 }
89 
90 } // namespace flutter
Dart_Handle toImage(uint32_t width, uint32_t height, Dart_Handle image_callback)
Definition: scene.cc:69
void dispose()
Definition: scene.cc:64
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
#define DART_BIND_ALL(CLASS, FOR_EACH)
~Scene() override
Definition: scene.cc:62
Definition: ref_ptr.h:252
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
void AssociateWithDartWrapper(Dart_Handle wrappable)
int32_t width
PlatformConfiguration * platform_configuration() const
#define FOR_EACH_BINDING(V)
Definition: scene.cc:24
int32_t height
std::unique_ptr< flutter::LayerTree > takeLayerTree()
Definition: scene.cc:86
Dart_Handle ToDart(const T &object)
Window * get_window(int window_id)
Retrieves the Window with the given ID managed by the PlatformConfiguration.
const ViewportMetrics & viewport_metrics() const
Definition: window.h:29
static UIDartState * Current()
static Dart_Handle RasterizeToImage(sk_sp< SkPicture > picture, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
Definition: picture.cc:91