Skip to content

Commit 97a23a8

Browse files
authored
Made a way to turn off the OpenGL operations on the IO thread for backgrounded apps (#13908)
1 parent 0d60e1a commit 97a23a8

15 files changed

Lines changed: 268 additions & 35 deletions

File tree

‎ci/licenses_golden/licenses_flutter‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ FILE: ../../../flutter/fml/synchronization/semaphore_unittest.cc
203203
FILE: ../../../flutter/fml/synchronization/shared_mutex.h
204204
FILE: ../../../flutter/fml/synchronization/shared_mutex_std.cc
205205
FILE: ../../../flutter/fml/synchronization/shared_mutex_std.h
206+
FILE: ../../../flutter/fml/synchronization/sync_switch.cc
207+
FILE: ../../../flutter/fml/synchronization/sync_switch.h
208+
FILE: ../../../flutter/fml/synchronization/sync_switch_unittest.cc
206209
FILE: ../../../flutter/fml/synchronization/waitable_event.cc
207210
FILE: ../../../flutter/fml/synchronization/waitable_event.h
208211
FILE: ../../../flutter/fml/synchronization/waitable_event_unittest.cc

‎flow/skia_gpu_object.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SkiaGPUObject {
5757

5858
SkiaGPUObject(sk_sp<SkiaObjectType> object, fml::RefPtr<SkiaUnrefQueue> queue)
5959
: object_(std::move(object)), queue_(std::move(queue)) {
60-
FML_DCHECK(queue_ && object_);
60+
FML_DCHECK(object_);
6161
}
6262

6363
SkiaGPUObject(SkiaGPUObject&&) = default;
@@ -69,7 +69,7 @@ class SkiaGPUObject {
6969
sk_sp<SkiaObjectType> get() const { return object_; }
7070

7171
void reset() {
72-
if (object_) {
72+
if (object_ && queue_) {
7373
queue_->Unref(object_.release());
7474
}
7575
queue_ = nullptr;

‎fml/BUILD.gn‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ source_set("fml") {
6464
"synchronization/semaphore.cc",
6565
"synchronization/semaphore.h",
6666
"synchronization/shared_mutex.h",
67+
"synchronization/sync_switch.cc",
68+
"synchronization/sync_switch.h",
6769
"synchronization/waitable_event.cc",
6870
"synchronization/waitable_event.h",
6971
"task_runner.cc",
@@ -210,6 +212,7 @@ executable("fml_unittests") {
210212
"paths_unittests.cc",
211213
"platform/darwin/string_range_sanitization_unittests.mm",
212214
"synchronization/semaphore_unittest.cc",
215+
"synchronization/sync_switch_unittest.cc",
213216
"synchronization/waitable_event_unittest.cc",
214217
"thread_local_unittests.cc",
215218
"time/time_delta_unittest.cc",

‎fml/synchronization/sync_switch.cc‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/fml/synchronization/sync_switch.h"
6+
7+
namespace fml {
8+
9+
SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfTrue(
10+
const std::function<void()>& handler) {
11+
true_handler = std::move(handler);
12+
return *this;
13+
}
14+
15+
SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfFalse(
16+
const std::function<void()>& handler) {
17+
false_handler = std::move(handler);
18+
return *this;
19+
}
20+
21+
SyncSwitch::SyncSwitch() : SyncSwitch(false) {}
22+
23+
SyncSwitch::SyncSwitch(bool value) : value_(value) {}
24+
25+
void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) {
26+
std::scoped_lock guard(mutex_);
27+
if (value_) {
28+
handlers.true_handler();
29+
} else {
30+
handlers.false_handler();
31+
}
32+
}
33+
34+
void SyncSwitch::SetSwitch(bool value) {
35+
std::scoped_lock guard(mutex_);
36+
value_ = value;
37+
}
38+
39+
} // namespace fml

‎fml/synchronization/sync_switch.h‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
#ifndef FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
6+
#define FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
7+
8+
#include <forward_list>
9+
#include <functional>
10+
#include <mutex>
11+
12+
#include "flutter/fml/macros.h"
13+
14+
namespace fml {
15+
16+
/// A threadsafe structure that allows you to switch between 2 different
17+
/// execution paths.
18+
///
19+
/// Execution and setting the switch is exclusive, i.e. only one will happen
20+
/// at a time.
21+
class SyncSwitch {
22+
public:
23+
/// Represents the 2 code paths available when calling |SyncSwitch::Execute|.
24+
struct Handlers {
25+
/// Sets the handler that will be executed if the |SyncSwitch| is true.
26+
Handlers& SetIfTrue(const std::function<void()>& handler);
27+
28+
/// Sets the handler that will be executed if the |SyncSwitch| is false.
29+
Handlers& SetIfFalse(const std::function<void()>& handler);
30+
31+
std::function<void()> true_handler = [] {};
32+
std::function<void()> false_handler = [] {};
33+
};
34+
35+
/// Create a |SyncSwitch| with the false value.
36+
SyncSwitch();
37+
38+
/// Create a |SyncSwitch| with the specified value.
39+
///
40+
/// @param[in] value Default value for the |SyncSwitch|.
41+
SyncSwitch(bool value);
42+
43+
/// Diverge execution between true and false values of the SyncSwitch.
44+
///
45+
/// This can be called on any thread. Note that attempting to call
46+
/// |SetSwitch| inside of the handlers will result in a self deadlock.
47+
///
48+
/// @param[in] handlers Called for the correct value of the |SyncSwitch|.
49+
void Execute(const Handlers& handlers);
50+
51+
/// Set the value of the SyncSwitch.
52+
///
53+
/// This can be called on any thread.
54+
///
55+
/// @param[in] value New value for the |SyncSwitch|.
56+
void SetSwitch(bool value);
57+
58+
private:
59+
std::mutex mutex_;
60+
bool value_;
61+
62+
FML_DISALLOW_COPY_AND_ASSIGN(SyncSwitch);
63+
};
64+
65+
} // namespace fml
66+
67+
#endif // FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/fml/synchronization/sync_switch.h"
6+
7+
#include "gtest/gtest.h"
8+
9+
using fml::SyncSwitch;
10+
11+
TEST(SyncSwitchTest, Basic) {
12+
SyncSwitch syncSwitch;
13+
bool switchValue = false;
14+
syncSwitch.Execute(SyncSwitch::Handlers()
15+
.SetIfTrue([&] { switchValue = true; })
16+
.SetIfFalse([&] { switchValue = false; }));
17+
EXPECT_FALSE(switchValue);
18+
syncSwitch.SetSwitch(true);
19+
syncSwitch.Execute(SyncSwitch::Handlers()
20+
.SetIfTrue([&] { switchValue = true; })
21+
.SetIfFalse([&] { switchValue = false; }));
22+
EXPECT_TRUE(switchValue);
23+
}
24+
25+
TEST(SyncSwitchTest, NoopIfUndefined) {
26+
SyncSwitch syncSwitch;
27+
bool switchValue = false;
28+
syncSwitch.Execute(SyncSwitch::Handlers());
29+
EXPECT_FALSE(switchValue);
30+
}

‎lib/ui/io_manager.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "flutter/flow/skia_gpu_object.h"
99
#include "flutter/fml/memory/weak_ptr.h"
10+
#include "flutter/fml/synchronization/sync_switch.h"
1011
#include "third_party/skia/include/gpu/GrContext.h"
1112

1213
namespace flutter {
@@ -22,6 +23,8 @@ class IOManager {
2223
virtual fml::WeakPtr<GrContext> GetResourceContext() const = 0;
2324

2425
virtual fml::RefPtr<flutter::SkiaUnrefQueue> GetSkiaUnrefQueue() const = 0;
26+
27+
virtual std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() = 0;
2528
};
2629

2730
} // namespace flutter

‎lib/ui/painting/image_decoder.cc‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ static sk_sp<SkImage> ImageFromCompressedData(
151151

152152
static SkiaGPUObject<SkImage> UploadRasterImage(
153153
sk_sp<SkImage> image,
154-
fml::WeakPtr<GrContext> context,
155-
fml::RefPtr<flutter::SkiaUnrefQueue> queue,
154+
fml::WeakPtr<IOManager> io_manager,
156155
const fml::tracing::TraceFlow& flow) {
157156
TRACE_EVENT0("flutter", __FUNCTION__);
158157
flow.Step(__FUNCTION__);
@@ -161,7 +160,7 @@ static SkiaGPUObject<SkImage> UploadRasterImage(
161160
// the this method.
162161
FML_DCHECK(!image->isTextureBacked());
163162

164-
if (!context || !queue) {
163+
if (!io_manager->GetResourceContext() || !io_manager->GetSkiaUnrefQueue()) {
165164
FML_LOG(ERROR)
166165
<< "Could not acquire context of release queue for texture upload.";
167166
return {};
@@ -173,19 +172,36 @@ static SkiaGPUObject<SkImage> UploadRasterImage(
173172
return {};
174173
}
175174

176-
auto texture_image =
177-
SkImage::MakeCrossContextFromPixmap(context.get(), // context
178-
pixmap, // pixmap
179-
true, // buildMips,
180-
true // limitToMaxTextureSize
181-
);
182-
183-
if (!texture_image) {
184-
FML_LOG(ERROR) << "Could not make x-context image.";
185-
return {};
186-
}
187-
188-
return {texture_image, queue};
175+
SkiaGPUObject<SkImage> result;
176+
io_manager->GetIsGpuDisabledSyncSwitch()->Execute(
177+
fml::SyncSwitch::Handlers()
178+
.SetIfTrue([&result, &pixmap, &image] {
179+
SkSafeRef(image.get());
180+
sk_sp<SkImage> texture_image = SkImage::MakeFromRaster(
181+
pixmap,
182+
[](const void* pixels, SkImage::ReleaseContext context) {
183+
SkSafeUnref(static_cast<SkImage*>(context));
184+
},
185+
image.get());
186+
result = {texture_image, nullptr};
187+
})
188+
.SetIfFalse([&result, context = io_manager->GetResourceContext(),
189+
&pixmap, queue = io_manager->GetSkiaUnrefQueue()] {
190+
sk_sp<SkImage> texture_image = SkImage::MakeCrossContextFromPixmap(
191+
context.get(), // context
192+
pixmap, // pixmap
193+
true, // buildMips,
194+
true // limitToMaxTextureSize
195+
);
196+
if (!texture_image) {
197+
FML_LOG(ERROR) << "Could not make x-context image.";
198+
result = {};
199+
} else {
200+
result = {texture_image, queue};
201+
}
202+
}));
203+
204+
return result;
189205
}
190206

191207
void ImageDecoder::Decode(ImageDescriptor descriptor,
@@ -265,9 +281,8 @@ void ImageDecoder::Decode(ImageDescriptor descriptor,
265281
return;
266282
}
267283

268-
auto uploaded = UploadRasterImage(
269-
std::move(decompressed), io_manager->GetResourceContext(),
270-
io_manager->GetSkiaUnrefQueue(), flow);
284+
auto uploaded =
285+
UploadRasterImage(std::move(decompressed), io_manager, flow);
271286

272287
if (!uploaded.get()) {
273288
FML_LOG(ERROR) << "Could not upload image to the GPU.";

‎lib/ui/painting/image_decoder_unittests.cc‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class TestIOManager final : public IOManager {
2929
task_runner,
3030
fml::TimeDelta::FromNanoseconds(0))),
3131
runner_(task_runner),
32-
weak_factory_(this) {
32+
weak_factory_(this),
33+
is_gpu_disabled_sync_switch_(std::make_shared<fml::SyncSwitch>()) {
3334
FML_CHECK(task_runner->RunsTasksOnCurrentThread())
3435
<< "The IO manager must be initialized its primary task runner. The "
3536
"test harness may not be setup correctly/safely.";
@@ -62,6 +63,14 @@ class TestIOManager final : public IOManager {
6263
return unref_queue_;
6364
}
6465

66+
// |IOManager|
67+
std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() override {
68+
did_access_is_gpu_disabled_sync_switch_ = true;
69+
return is_gpu_disabled_sync_switch_;
70+
}
71+
72+
bool did_access_is_gpu_disabled_sync_switch_ = false;
73+
6574
private:
6675
TestGLSurface gl_surface_;
6776
sk_sp<GrContext> gl_context_;
@@ -70,6 +79,7 @@ class TestIOManager final : public IOManager {
7079
fml::WeakPtr<TestIOManager> weak_prototype_;
7180
fml::RefPtr<fml::TaskRunner> runner_;
7281
fml::WeakPtrFactory<TestIOManager> weak_factory_;
82+
std::shared_ptr<fml::SyncSwitch> is_gpu_disabled_sync_switch_;
7383

7484
FML_DISALLOW_COPY_AND_ASSIGN(TestIOManager);
7585
};
@@ -167,7 +177,7 @@ TEST_F(ImageDecoderFixtureTest, ValidImageResultsInSuccess) {
167177

168178
fml::AutoResetWaitableEvent latch;
169179

170-
std::unique_ptr<IOManager> io_manager;
180+
std::unique_ptr<TestIOManager> io_manager;
171181

172182
auto release_io_manager = [&]() {
173183
io_manager.reset();
@@ -187,8 +197,10 @@ TEST_F(ImageDecoderFixtureTest, ValidImageResultsInSuccess) {
187197
ImageDecoder::ImageResult callback = [&](SkiaGPUObject<SkImage> image) {
188198
ASSERT_TRUE(runners.GetUITaskRunner()->RunsTasksOnCurrentThread());
189199
ASSERT_TRUE(image.get());
200+
EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_);
190201
runners.GetIOTaskRunner()->PostTask(release_io_manager);
191202
};
203+
EXPECT_FALSE(io_manager->did_access_is_gpu_disabled_sync_switch_);
192204
image_decoder->Decode(std::move(image_descriptor), callback);
193205
};
194206

@@ -198,7 +210,6 @@ TEST_F(ImageDecoderFixtureTest, ValidImageResultsInSuccess) {
198210
};
199211

200212
runners.GetIOTaskRunner()->PostTask(setup_io_manager_and_decode);
201-
202213
latch.Wait();
203214
}
204215

‎shell/common/shell.cc‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread(
104104
// https://github.com/flutter/flutter/issues/42948
105105
fml::TaskRunner::RunNowOrPostTask(
106106
io_task_runner,
107-
[&io_manager_promise, //
108-
&weak_io_manager_promise, //
109-
&unref_queue_promise, //
110-
platform_view = platform_view->GetWeakPtr(), //
111-
io_task_runner //
107+
[&io_manager_promise, //
108+
&weak_io_manager_promise, //
109+
&unref_queue_promise, //
110+
platform_view = platform_view->GetWeakPtr(), //
111+
io_task_runner, //
112+
is_backgrounded_sync_switch = shell->GetIsGpuDisabledSyncSwitch() //
112113
]() {
113114
TRACE_EVENT0("flutter", "ShellSetupIOSubsystem");
114115
auto io_manager = std::make_unique<ShellIOManager>(
115-
platform_view.getUnsafe()->CreateResourceContext(), io_task_runner);
116+
platform_view.getUnsafe()->CreateResourceContext(),
117+
is_backgrounded_sync_switch, io_task_runner);
116118
weak_io_manager_promise.set_value(io_manager->GetWeakPtr());
117119
unref_queue_promise.set_value(io_manager->GetSkiaUnrefQueue());
118120
io_manager_promise.set_value(std::move(io_manager));
@@ -289,6 +291,7 @@ Shell::Shell(DartVMRef vm, TaskRunners task_runners, Settings settings)
289291
: task_runners_(std::move(task_runners)),
290292
settings_(std::move(settings)),
291293
vm_(std::move(vm)),
294+
is_gpu_disabled_sync_switch_(new fml::SyncSwitch()),
292295
weak_factory_(this),
293296
weak_factory_gpu_(nullptr) {
294297
FML_CHECK(vm_) << "Must have access to VM to create a shell.";
@@ -647,7 +650,9 @@ void Shell::OnPlatformViewDestroyed() {
647650
auto io_task = [io_manager = io_manager_.get(), &latch]() {
648651
// Execute any pending Skia object deletions while GPU access is still
649652
// allowed.
650-
io_manager->GetSkiaUnrefQueue()->Drain();
653+
io_manager->GetIsGpuDisabledSyncSwitch()->Execute(
654+
fml::SyncSwitch::Handlers().SetIfFalse(
655+
[&] { io_manager->GetSkiaUnrefQueue()->Drain(); }));
651656
// Step 3: All done. Signal the latch that the platform thread is waiting
652657
// on.
653658
latch.Signal();
@@ -1419,4 +1424,8 @@ bool Shell::ReloadSystemFonts() {
14191424
return true;
14201425
}
14211426

1427+
std::shared_ptr<fml::SyncSwitch> Shell::GetIsGpuDisabledSyncSwitch() const {
1428+
return is_gpu_disabled_sync_switch_;
1429+
}
1430+
14221431
} // namespace flutter

0 commit comments

Comments
 (0)