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 
22 IMPLEMENT_WRAPPERTYPEINFO(ui, Scene);
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  auto viewport_metrics = UIDartState::Current()
47  ->window()
48  ->viewport_metrics();
49 
50  layer_tree_ = std::make_unique<LayerTree>(
51  SkISize::Make(viewport_metrics.physical_width,
52  viewport_metrics.physical_height),
53  static_cast<float>(viewport_metrics.device_pixel_ratio));
54  layer_tree_->set_root_layer(std::move(rootLayer));
55  layer_tree_->set_rasterizer_tracing_threshold(rasterizerTracingThreshold);
56  layer_tree_->set_checkerboard_raster_cache_images(
57  checkerboardRasterCacheImages);
58  layer_tree_->set_checkerboard_offscreen_layers(checkerboardOffscreenLayers);
59 }
60 
62 
65 }
66 
67 Dart_Handle Scene::toImage(uint32_t width,
68  uint32_t height,
69  Dart_Handle raw_image_callback) {
70  TRACE_EVENT0("flutter", "Scene::toImage");
71 
72  if (!layer_tree_) {
73  return tonic::ToDart("Scene did not contain a layer tree.");
74  }
75 
76  auto picture = layer_tree_->Flatten(SkRect::MakeWH(width, height));
77  if (!picture) {
78  return tonic::ToDart("Could not flatten scene into a layer tree.");
79  }
80 
81  return Picture::RasterizeToImage(picture, width, height, raw_image_callback);
82 }
83 
84 std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree() {
85  return std::move(layer_tree_);
86 }
87 
88 } // namespace flutter
Dart_Handle toImage(uint32_t width, uint32_t height, Dart_Handle image_callback)
Definition: scene.cc:67
void dispose()
Definition: scene.cc:63
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define DART_BIND_ALL(CLASS, FOR_EACH)
~Scene() override
Definition: scene.cc:61
Window * window() const
Retrieves the Window managed by the PlatformConfiguration.
Definition: ref_ptr.h:252
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
int32_t height
void AssociateWithDartWrapper(Dart_Handle wrappable)
int32_t width
PlatformConfiguration * platform_configuration() const
Definition: ui_dart_state.h:48
#define FOR_EACH_BINDING(V)
Definition: scene.cc:24
std::unique_ptr< flutter::LayerTree > takeLayerTree()
Definition: scene.cc:84
Dart_Handle ToDart(const T &object)
const ViewportMetrics & viewport_metrics() const
Definition: window.h:25
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:68