Skip to content

Commit 78a8ca0

Browse files
authored
Made Picture::toImage happen on the IO thread with no need for an onscreen surface. (flutter#9813)
Made Picture::toImage happen on the IO thread with no need for a surface.
1 parent 74af88b commit 78a8ca0

20 files changed

+173
-170
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ FILE: ../../../flutter/lib/ui/semantics/semantics_update.cc
403403
FILE: ../../../flutter/lib/ui/semantics/semantics_update.h
404404
FILE: ../../../flutter/lib/ui/semantics/semantics_update_builder.cc
405405
FILE: ../../../flutter/lib/ui/semantics/semantics_update_builder.h
406-
FILE: ../../../flutter/lib/ui/snapshot_delegate.h
407406
FILE: ../../../flutter/lib/ui/text.dart
408407
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.cc
409408
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.h

lib/ui/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ source_set("ui") {
7676
"semantics/semantics_update.h",
7777
"semantics/semantics_update_builder.cc",
7878
"semantics/semantics_update_builder.h",
79-
"snapshot_delegate.h",
8079
"text/asset_manager_font_provider.cc",
8180
"text/asset_manager_font_provider.h",
8281
"text/font_collection.cc",

lib/ui/painting/picture.cc

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include "flutter/lib/ui/painting/picture.h"
66

77
#include "flutter/fml/make_copyable.h"
8+
#include "flutter/fml/trace_event.h"
89
#include "flutter/lib/ui/painting/canvas.h"
910
#include "flutter/lib/ui/ui_dart_state.h"
1011
#include "third_party/skia/include/core/SkImage.h"
12+
#include "third_party/skia/include/core/SkSurface.h"
1113
#include "third_party/tonic/converter/dart_converter.h"
1214
#include "third_party/tonic/dart_args.h"
1315
#include "third_party/tonic/dart_binding_macros.h"
@@ -16,6 +18,64 @@
1618
#include "third_party/tonic/logging/dart_invoke.h"
1719

1820
namespace flutter {
21+
namespace {
22+
23+
sk_sp<SkSurface> MakeSnapshotSurface(const SkISize& picture_size,
24+
fml::WeakPtr<GrContext> resource_context) {
25+
SkImageInfo image_info = SkImageInfo::MakeN32Premul(
26+
picture_size.width(), picture_size.height(), SkColorSpace::MakeSRGB());
27+
if (resource_context) {
28+
return SkSurface::MakeRenderTarget(resource_context.get(), // context
29+
SkBudgeted::kNo, // budgeted
30+
image_info // image info
31+
);
32+
} else {
33+
return SkSurface::MakeRaster(image_info);
34+
}
35+
}
36+
37+
/// Makes a RAM backed (Raster) image of a picture.
38+
/// @param[in] picture The picture that will get converted to an image.
39+
/// @param[in] surface The surface tha will be used to render the picture. This
40+
/// will be CPU or GPU based.
41+
/// @todo Currently this creates a RAM backed image regardless of what type of
42+
/// surface is used. In certain instances we may want a GPU backed image
43+
/// from a GPU surface to avoid the conversion.
44+
sk_sp<SkImage> MakeRasterSnapshot(sk_sp<SkPicture> picture,
45+
sk_sp<SkSurface> surface) {
46+
TRACE_EVENT0("flutter", __FUNCTION__);
47+
48+
if (surface == nullptr || surface->getCanvas() == nullptr) {
49+
return nullptr;
50+
}
51+
52+
surface->getCanvas()->drawPicture(picture.get());
53+
54+
surface->getCanvas()->flush();
55+
56+
// Here device could mean GPU or CPU (depending on the supplied surface) and
57+
// host means CPU; this is different from use cases like Flutter driver tests
58+
// where device means mobile devices and host means laptops/desktops.
59+
sk_sp<SkImage> device_snapshot;
60+
{
61+
TRACE_EVENT0("flutter", "MakeDeviceSnpashot");
62+
device_snapshot = surface->makeImageSnapshot();
63+
}
64+
65+
if (device_snapshot == nullptr) {
66+
return nullptr;
67+
}
68+
69+
{
70+
TRACE_EVENT0("flutter", "DeviceHostTransfer");
71+
if (auto raster_image = device_snapshot->makeRasterImage()) {
72+
return raster_image;
73+
}
74+
}
75+
76+
return nullptr;
77+
}
78+
} // namespace
1979

2080
IMPLEMENT_WRAPPERTYPEINFO(ui, Picture);
2181

@@ -75,8 +135,8 @@ Dart_Handle Picture::RasterizeToImage(sk_sp<SkPicture> picture,
75135
new tonic::DartPersistentValue(dart_state, raw_image_callback);
76136
auto unref_queue = dart_state->GetSkiaUnrefQueue();
77137
auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
78-
auto gpu_task_runner = dart_state->GetTaskRunners().GetGPUTaskRunner();
79-
auto snapshot_delegate = dart_state->GetSnapshotDelegate();
138+
auto io_task_runner = dart_state->GetTaskRunners().GetIOTaskRunner();
139+
fml::WeakPtr<GrContext> resource_context = dart_state->GetResourceContext();
80140

81141
// We can't create an image on this task runner because we don't have a
82142
// graphics context. Even if we did, it would be slow anyway. Also, this
@@ -111,17 +171,16 @@ Dart_Handle Picture::RasterizeToImage(sk_sp<SkPicture> picture,
111171
delete image_callback;
112172
});
113173

114-
// Kick things off on the GPU.
115-
fml::TaskRunner::RunNowOrPostTask(
116-
gpu_task_runner,
117-
[ui_task_runner, snapshot_delegate, picture, picture_bounds, ui_task] {
118-
sk_sp<SkImage> raster_image =
119-
snapshot_delegate->MakeRasterSnapshot(picture, picture_bounds);
120-
121-
fml::TaskRunner::RunNowOrPostTask(
122-
ui_task_runner,
123-
[ui_task, raster_image]() { ui_task(raster_image); });
124-
});
174+
fml::TaskRunner::RunNowOrPostTask(io_task_runner, [ui_task_runner, picture,
175+
picture_bounds, ui_task,
176+
resource_context] {
177+
sk_sp<SkSurface> surface =
178+
MakeSnapshotSurface(picture_bounds, resource_context);
179+
sk_sp<SkImage> raster_image = MakeRasterSnapshot(picture, surface);
180+
181+
fml::TaskRunner::RunNowOrPostTask(
182+
ui_task_runner, [ui_task, raster_image]() { ui_task(raster_image); });
183+
});
125184

126185
return Dart_Null();
127186
}

lib/ui/snapshot_delegate.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/ui/ui_dart_state.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ UIDartState::UIDartState(
1717
TaskRunners task_runners,
1818
TaskObserverAdd add_callback,
1919
TaskObserverRemove remove_callback,
20-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
2120
fml::WeakPtr<IOManager> io_manager,
2221
fml::WeakPtr<ImageDecoder> image_decoder,
2322
std::string advisory_script_uri,
@@ -28,7 +27,6 @@ UIDartState::UIDartState(
2827
: task_runners_(std::move(task_runners)),
2928
add_callback_(std::move(add_callback)),
3029
remove_callback_(std::move(remove_callback)),
31-
snapshot_delegate_(std::move(snapshot_delegate)),
3230
io_manager_(std::move(io_manager)),
3331
image_decoder_(std::move(image_decoder)),
3432
advisory_script_uri_(std::move(advisory_script_uri)),
@@ -115,10 +113,6 @@ void UIDartState::AddOrRemoveTaskObserver(bool add) {
115113
}
116114
}
117115

118-
fml::WeakPtr<SnapshotDelegate> UIDartState::GetSnapshotDelegate() const {
119-
return snapshot_delegate_;
120-
}
121-
122116
fml::WeakPtr<GrContext> UIDartState::GetResourceContext() const {
123117
if (!io_manager_) {
124118
return {};

lib/ui/ui_dart_state.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "flutter/lib/ui/io_manager.h"
1818
#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
1919
#include "flutter/lib/ui/painting/image_decoder.h"
20-
#include "flutter/lib/ui/snapshot_delegate.h"
2120
#include "third_party/dart/runtime/include/dart_api.h"
2221
#include "third_party/skia/include/gpu/GrContext.h"
2322
#include "third_party/tonic/dart_microtask_queue.h"
@@ -50,8 +49,6 @@ class UIDartState : public tonic::DartState {
5049

5150
fml::RefPtr<flutter::SkiaUnrefQueue> GetSkiaUnrefQueue() const;
5251

53-
fml::WeakPtr<SnapshotDelegate> GetSnapshotDelegate() const;
54-
5552
fml::WeakPtr<GrContext> GetResourceContext() const;
5653

5754
fml::WeakPtr<ImageDecoder> GetImageDecoder() const;
@@ -78,7 +75,6 @@ class UIDartState : public tonic::DartState {
7875
UIDartState(TaskRunners task_runners,
7976
TaskObserverAdd add_callback,
8077
TaskObserverRemove remove_callback,
81-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
8278
fml::WeakPtr<IOManager> io_manager,
8379
fml::WeakPtr<ImageDecoder> image_decoder,
8480
std::string advisory_script_uri,
@@ -101,7 +97,6 @@ class UIDartState : public tonic::DartState {
10197
const TaskRunners task_runners_;
10298
const TaskObserverAdd add_callback_;
10399
const TaskObserverRemove remove_callback_;
104-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate_;
105100
fml::WeakPtr<IOManager> io_manager_;
106101
fml::WeakPtr<ImageDecoder> image_decoder_;
107102
const std::string advisory_script_uri_;

runtime/dart_isolate.cc

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
3535
fml::RefPtr<const DartSnapshot> shared_snapshot,
3636
TaskRunners task_runners,
3737
std::unique_ptr<Window> window,
38-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
3938
fml::WeakPtr<IOManager> io_manager,
4039
fml::WeakPtr<ImageDecoder> image_decoder,
4140
std::string advisory_script_uri,
@@ -57,18 +56,17 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
5756
// being prepared to run.
5857
auto root_embedder_data = std::make_unique<std::shared_ptr<DartIsolate>>(
5958
std::make_shared<DartIsolate>(
60-
settings, // settings
61-
std::move(isolate_snapshot), // isolate snapshot
62-
std::move(shared_snapshot), // shared snapshot
63-
task_runners, // task runners
64-
std::move(snapshot_delegate), // snapshot delegate
65-
std::move(io_manager), // IO manager
66-
std::move(image_decoder), // Image Decoder
67-
advisory_script_uri, // advisory URI
68-
advisory_script_entrypoint, // advisory entrypoint
69-
nullptr, // child isolate preparer
70-
isolate_create_callback, // isolate create callback
71-
isolate_shutdown_callback // isolate shutdown callback
59+
settings, // settings
60+
std::move(isolate_snapshot), // isolate snapshot
61+
std::move(shared_snapshot), // shared snapshot
62+
task_runners, // task runners
63+
std::move(io_manager), // IO manager
64+
std::move(image_decoder), // Image Decoder
65+
advisory_script_uri, // advisory URI
66+
advisory_script_entrypoint, // advisory entrypoint
67+
nullptr, // child isolate preparer
68+
isolate_create_callback, // isolate create callback
69+
isolate_shutdown_callback // isolate shutdown callback
7270
));
7371

7472
std::tie(vm_isolate, embedder_isolate) = CreateDartVMAndEmbedderObjectPair(
@@ -106,7 +104,6 @@ DartIsolate::DartIsolate(const Settings& settings,
106104
fml::RefPtr<const DartSnapshot> isolate_snapshot,
107105
fml::RefPtr<const DartSnapshot> shared_snapshot,
108106
TaskRunners task_runners,
109-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
110107
fml::WeakPtr<IOManager> io_manager,
111108
fml::WeakPtr<ImageDecoder> image_decoder,
112109
std::string advisory_script_uri,
@@ -117,7 +114,6 @@ DartIsolate::DartIsolate(const Settings& settings,
117114
: UIDartState(std::move(task_runners),
118115
settings.task_observer_add,
119116
settings.task_observer_remove,
120-
std::move(snapshot_delegate),
121117
std::move(io_manager),
122118
std::move(image_decoder),
123119
advisory_script_uri,
@@ -599,7 +595,6 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
599595
vm_data->GetSharedSnapshot(), // shared snapshot
600596
null_task_runners, // task runners
601597
nullptr, // window
602-
{}, // snapshot delegate
603598
{}, // IO Manager
604599
{}, // Image Decoder
605600
DART_VM_SERVICE_ISOLATE_NAME, // script uri
@@ -712,7 +707,6 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair(
712707
(*raw_embedder_isolate)->GetIsolateSnapshot(), // isolate_snapshot
713708
(*raw_embedder_isolate)->GetSharedSnapshot(), // shared_snapshot
714709
null_task_runners, // task_runners
715-
fml::WeakPtr<SnapshotDelegate>{}, // snapshot_delegate
716710
fml::WeakPtr<IOManager>{}, // io_manager
717711
fml::WeakPtr<ImageDecoder>{}, // io_manager
718712
advisory_script_uri, // advisory_script_uri

runtime/dart_isolate.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "flutter/fml/macros.h"
1515
#include "flutter/fml/mapping.h"
1616
#include "flutter/lib/ui/io_manager.h"
17-
#include "flutter/lib/ui/snapshot_delegate.h"
1817
#include "flutter/lib/ui/ui_dart_state.h"
1918
#include "flutter/lib/ui/window/window.h"
2019
#include "flutter/runtime/dart_snapshot.h"
@@ -47,7 +46,6 @@ class DartIsolate : public UIDartState {
4746
fml::RefPtr<const DartSnapshot> shared_snapshot,
4847
TaskRunners task_runners,
4948
std::unique_ptr<Window> window,
50-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
5149
fml::WeakPtr<IOManager> io_manager,
5250
fml::WeakPtr<ImageDecoder> image_decoder,
5351
std::string advisory_script_uri,
@@ -60,7 +58,6 @@ class DartIsolate : public UIDartState {
6058
fml::RefPtr<const DartSnapshot> isolate_snapshot,
6159
fml::RefPtr<const DartSnapshot> shared_snapshot,
6260
TaskRunners task_runners,
63-
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
6461
fml::WeakPtr<IOManager> io_manager,
6562
fml::WeakPtr<ImageDecoder> image_decoder,
6663
std::string advisory_script_uri,

runtime/dart_isolate_unittests.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
4141
vm_data->GetSharedSnapshot(), // shared snapshot
4242
std::move(task_runners), // task runners
4343
nullptr, // window
44-
{}, // snapshot delegate
4544
{}, // io manager
4645
{}, // image decoder
4746
"main.dart", // advisory uri
@@ -75,7 +74,6 @@ TEST_F(DartIsolateTest, IsolateShutdownCallbackIsInIsolateScope) {
7574
vm_data->GetSharedSnapshot(), // shared snapshot
7675
std::move(task_runners), // task runners
7776
nullptr, // window
78-
{}, // snapshot delegate
7977
{}, // io manager
8078
{}, // image decoder
8179
"main.dart", // advisory uri
@@ -186,7 +184,6 @@ static void RunDartCodeInIsolate(DartVMRef& vm_ref,
186184
vm_data->GetSharedSnapshot(), // shared snapshot
187185
std::move(task_runners), // task runners
188186
nullptr, // window
189-
{}, // snapshot delegate
190187
{}, // io manager
191188
{}, // image decoder
192189
"main.dart", // advisory uri

runtime/dart_lifecycle_unittests.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate(
5656
vm.GetSharedSnapshot(), // shared_snapshot
5757
runners, // task_runners
5858
{}, // window
59-
{}, // snapshot_delegate
6059
{}, // io_manager
6160
{}, // image_decoder
6261
"main.dart", // advisory_script_uri

runtime/runtime_controller.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ RuntimeController::RuntimeController(
2020
fml::RefPtr<const DartSnapshot> p_isolate_snapshot,
2121
fml::RefPtr<const DartSnapshot> p_shared_snapshot,
2222
TaskRunners p_task_runners,
23-
fml::WeakPtr<SnapshotDelegate> p_snapshot_delegate,
2423
fml::WeakPtr<IOManager> p_io_manager,
2524
fml::WeakPtr<ImageDecoder> p_image_decoder,
2625
std::string p_advisory_script_uri,
@@ -33,7 +32,6 @@ RuntimeController::RuntimeController(
3332
std::move(p_isolate_snapshot),
3433
std::move(p_shared_snapshot),
3534
std::move(p_task_runners),
36-
std::move(p_snapshot_delegate),
3735
std::move(p_io_manager),
3836
std::move(p_image_decoder),
3937
std::move(p_advisory_script_uri),
@@ -49,7 +47,6 @@ RuntimeController::RuntimeController(
4947
fml::RefPtr<const DartSnapshot> p_isolate_snapshot,
5048
fml::RefPtr<const DartSnapshot> p_shared_snapshot,
5149
TaskRunners p_task_runners,
52-
fml::WeakPtr<SnapshotDelegate> p_snapshot_delegate,
5350
fml::WeakPtr<IOManager> p_io_manager,
5451
fml::WeakPtr<ImageDecoder> p_image_decoder,
5552
std::string p_advisory_script_uri,
@@ -63,7 +60,6 @@ RuntimeController::RuntimeController(
6360
isolate_snapshot_(std::move(p_isolate_snapshot)),
6461
shared_snapshot_(std::move(p_shared_snapshot)),
6562
task_runners_(p_task_runners),
66-
snapshot_delegate_(p_snapshot_delegate),
6763
io_manager_(p_io_manager),
6864
image_decoder_(p_image_decoder),
6965
advisory_script_uri_(p_advisory_script_uri),
@@ -81,7 +77,6 @@ RuntimeController::RuntimeController(
8177
shared_snapshot_, //
8278
task_runners_, //
8379
std::make_unique<Window>(this), //
84-
snapshot_delegate_, //
8580
io_manager_, //
8681
image_decoder_, //
8782
p_advisory_script_uri, //
@@ -142,7 +137,6 @@ std::unique_ptr<RuntimeController> RuntimeController::Clone() const {
142137
isolate_snapshot_, //
143138
shared_snapshot_, //
144139
task_runners_, //
145-
snapshot_delegate_, //
146140
io_manager_, //
147141
image_decoder_, //
148142
advisory_script_uri_, //

0 commit comments

Comments
 (0)