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" |
15 | #include "third_party/tonic/converter/dart_converter.h" |
16 | #include "third_party/tonic/dart_args.h" |
17 | #include "third_party/tonic/dart_binding_macros.h" |
18 | #include "third_party/tonic/dart_library_natives.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 | |
28 | DART_BIND_ALL(Scene, FOR_EACH_BINDING) |
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() |
46 | ->platform_configuration() |
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 | |
61 | Scene::~Scene() {} |
62 | |
63 | void Scene::dispose() { |
64 | ClearDartWrapper(); |
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 |
89 | |