Skip to content

Commit d3ac5cd

Browse files
authored
[web] Use thread local strike caches in skwasm (flutter#190048)
## Description Multithreaded skwasm is built with `-sWASM_WORKERS` but without `-pthread`, which links the single threaded emscripten system libraries where mutexes are no-ops. Text layout on the main thread and the raster worker share the global `SkStrikeCache`, so under heavy text churn the two threads corrupt the heap and the tab ends up pinned at 100% CPU. The fix is Skia's experimental thread local strike cache flag, enabled from a constructor in `surface.cc`. Each thread gets its own strike cache, so there is no shared state left to race on. Two adjacent fixes: - `context_lost_callback_id_` was allocated on the worker by incrementing `current_callback_id_`, which the main thread also increments. It is now allocated on the main thread in `SetCanvas`. - `surface_setResourceCacheLimitBytes` touched the worker owned `GrDirectContext` from the main thread. It now dispatches to the worker, and the value is stored and reapplied when the render context is recreated. This also fixes a null deref when Dart sets the limit before surface init. ## Tests New regression test `lib/web_ui/test/skwasm/concurrent_text_layout_raster_test.dart`: text layout with never repeating font parameters on the main thread while the worker rasterizes the previous frame, disposing everything per frame and toggling the resource cache limit. Without the flag it fails 2 of 2 runs (one wasm timeout, one hard page freeze); with it, 6 of 6 pass. The context loss suite passes 4 of 4, covering the callback id and cache limit changes. No golden churn is expected. Fixes flutter#190039 Possibly also fixes flutter#184858, which looks like the same corruption from the dispose path. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 01dc54a commit d3ac5cd

5 files changed

Lines changed: 186 additions & 7 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
import 'package:test/bootstrap/browser.dart';
6+
import 'package:test/test.dart';
7+
import 'package:ui/src/engine.dart' show renderer;
8+
import 'package:ui/ui.dart' as ui;
9+
10+
import '../common/rendering.dart';
11+
import '../common/test_initialization.dart';
12+
13+
void main() {
14+
internalBootstrapBrowserTest(() => testMain);
15+
}
16+
17+
const int _maxFrames = 200;
18+
const Duration _maxTestTime = Duration(seconds: 10);
19+
const int _paragraphsPerFrame = 24;
20+
const int _spansPerParagraph = 3;
21+
22+
const List<String> _fontFamilies = <String>['Roboto', 'RobotoVariable', 'Ahem'];
23+
const List<ui.FontWeight> _fontWeights = <ui.FontWeight>[
24+
ui.FontWeight.w100,
25+
ui.FontWeight.w300,
26+
ui.FontWeight.w400,
27+
ui.FontWeight.w500,
28+
ui.FontWeight.w700,
29+
ui.FontWeight.w900,
30+
];
31+
32+
Future<void> testMain() async {
33+
setUpUnitTests(withImplicitView: true, setUpTestViewDimensions: false);
34+
35+
// Advanced by an irrational stride so fractional font sizes never repeat,
36+
// forcing a fresh SkStrike for every span.
37+
double fontSizeSeed = 0;
38+
39+
List<ui.Paragraph> buildAndLayoutParagraphs(int frame) {
40+
final paragraphs = <ui.Paragraph>[];
41+
for (var i = 0; i < _paragraphsPerFrame; i++) {
42+
final builder = ui.ParagraphBuilder(ui.ParagraphStyle());
43+
for (var span = 0; span < _spansPerParagraph; span++) {
44+
fontSizeSeed += 0.6180339887;
45+
builder.pushStyle(
46+
ui.TextStyle(
47+
color: const ui.Color(0xFF000000),
48+
fontFamily: _fontFamilies[(i + span) % _fontFamilies.length],
49+
fontSize: 8.0 + (fontSizeSeed % 32.0),
50+
fontWeight: _fontWeights[(frame + i + span) % _fontWeights.length],
51+
fontStyle: (frame + i + span).isEven ? ui.FontStyle.normal : ui.FontStyle.italic,
52+
),
53+
);
54+
builder.addText('Quick zephyrs blow 0123456789 frame $frame paragraph $i span $span. ');
55+
builder.pop();
56+
}
57+
final ui.Paragraph paragraph = builder.build();
58+
paragraph.layout(const ui.ParagraphConstraints(width: 400));
59+
paragraphs.add(paragraph);
60+
}
61+
return paragraphs;
62+
}
63+
64+
ui.Picture recordPicture(List<ui.Paragraph> paragraphs) {
65+
final recorder = ui.PictureRecorder();
66+
final canvas = ui.Canvas(recorder, const ui.Rect.fromLTWH(0, 0, 500, 500));
67+
canvas.drawColor(const ui.Color(0xFFFFFFFF), ui.BlendMode.src);
68+
var offset = 0.0;
69+
for (final paragraph in paragraphs) {
70+
canvas.drawParagraph(paragraph, ui.Offset(0, offset));
71+
offset += 20.0;
72+
}
73+
return recorder.endRecording();
74+
}
75+
76+
test('concurrent text layout and rasterization does not corrupt the heap', () async {
77+
List<ui.Paragraph> paragraphs = buildAndLayoutParagraphs(0);
78+
final stopwatch = Stopwatch()..start();
79+
var frame = 0;
80+
while (frame < _maxFrames && stopwatch.elapsed < _maxTestTime) {
81+
frame++;
82+
83+
final ui.Picture picture = recordPicture(paragraphs);
84+
final sceneBuilder = ui.SceneBuilder();
85+
sceneBuilder.addPicture(ui.Offset.zero, picture);
86+
87+
// Kick off rasterization unawaited, then yield once so the render
88+
// message reaches the raster thread.
89+
final Future<void> renderFuture = renderScene(sceneBuilder.build());
90+
await Future<void>.delayed(Duration.zero);
91+
92+
// Lay out the next frame's paragraphs while the raster thread is
93+
// rasterizing this one — the concurrent strike cache access under test.
94+
final List<ui.Paragraph> nextParagraphs = buildAndLayoutParagraphs(frame);
95+
96+
await renderFuture;
97+
98+
// Disposing exercises the concurrent teardown path of
99+
// https://github.com/flutter/flutter/issues/184858.
100+
for (final paragraph in paragraphs) {
101+
paragraph.dispose();
102+
}
103+
picture.dispose();
104+
paragraphs = nextParagraphs;
105+
106+
// Shrink and re-grow the GPU resource cache so purging runs against
107+
// in-flight rasterization.
108+
if (frame % 16 == 0) {
109+
renderer.resourceCacheMaxBytes = frame % 32 == 0 ? 1024 * 1024 : 64 * 1024 * 1024;
110+
}
111+
}
112+
113+
for (final paragraph in paragraphs) {
114+
paragraph.dispose();
115+
}
116+
117+
// Completing without a RuntimeError or hang is the pass condition.
118+
expect(frame, greaterThan(0));
119+
}, timeout: const Timeout(Duration(minutes: 2)));
120+
}

‎engine/src/flutter/skwasm/library_skwasm_support.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ mergeInto(LibraryManager.library, {
161161
data.callbackId,
162162
);
163163
return;
164+
case 'setResourceCacheLimit':
165+
_surface_setResourceCacheLimitOnWorker(data.surface, data.bytes);
166+
return;
164167
default:
165168
console.warn(`unrecognized skwasm message: ${skwasmMessage}`);
166169
}
@@ -210,11 +213,10 @@ mergeInto(LibraryManager.library, {
210213
callbackId,
211214
}, [canvas], threadId);
212215
};
213-
_skwasm_reportInitialized = function (surfaceHandle, contextLostCallbackId, callbackId) {
216+
_skwasm_reportInitialized = function (surfaceHandle, callbackId) {
214217
skwasm_postMessage({
215218
skwasmMessage: 'onInitialized',
216219
surface: surfaceHandle,
217-
contextLostCallbackId,
218220
callbackId,
219221
}, []);
220222
};
@@ -290,6 +292,15 @@ mergeInto(LibraryManager.library, {
290292
});
291293
}
292294

295+
// Resource Cache
296+
_skwasm_dispatchSetResourceCacheLimit = function(threadId, surface, bytes) {
297+
skwasm_postMessage({
298+
skwasmMessage: 'setResourceCacheLimit',
299+
surface,
300+
bytes,
301+
}, [], threadId);
302+
};
303+
293304
// Context Loss
294305
_skwasm_dispatchTriggerContextLoss = function (threadId, surfaceHandle, callbackId) {
295306
skwasm_postMessage({
@@ -421,6 +432,8 @@ mergeInto(LibraryManager.library, {
421432
skwasm_createGlTextureFromTextureSource__deps: ['$skwasm_support_setup'],
422433
skwasm_dispatchDisposeSurface: function() {},
423434
skwasm_dispatchDisposeSurface__deps: ['$skwasm_support_setup'],
435+
skwasm_dispatchSetResourceCacheLimit: function() {},
436+
skwasm_dispatchSetResourceCacheLimit__deps: ['$skwasm_support_setup'],
424437
skwasm_dispatchRasterizeImage: function() {},
425438
skwasm_dispatchRasterizeImage__deps: ['$skwasm_support_setup'],
426439
skwasm_postRasterizeResult: function() {},

‎engine/src/flutter/skwasm/skwasm_support.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ extern uint32_t skwasm_getGlContextForCanvas(SkwasmObject canvas,
3939
bool antialias,
4040
Skwasm::Surface* surface);
4141
extern void skwasm_reportInitialized(Skwasm::Surface* surface,
42-
uint32_t callback_id,
43-
uint32_t context_lost_callback_id);
42+
uint32_t callback_id);
4443
extern void skwasm_reportResizeComplete(Skwasm::Surface* surface,
4544
uint32_t callback_id);
4645
extern void skwasm_dispatchResizeSurface(unsigned long thread_id,
@@ -76,6 +75,9 @@ extern void skwasm_dispatchTransferCanvas(unsigned long thread_id,
7675
uint32_t callback_id);
7776
extern void skwasm_dispatchDisposeSurface(unsigned long thread_id,
7877
Skwasm::Surface* surface);
78+
extern void skwasm_dispatchSetResourceCacheLimit(unsigned long thread_id,
79+
Skwasm::Surface* surface,
80+
int bytes);
7981
extern void skwasm_dispatchRasterizeImage(unsigned long thread_id,
8082
Skwasm::Surface* surface,
8183
flutter::DlImage* image,

‎engine/src/flutter/skwasm/surface.cc‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
// the Dart code, which will complete the future that was returned by the
4242
// original Dart method call.
4343

44+
// See https://github.com/flutter/flutter/pull/190048
45+
extern bool
46+
gSkUseThreadLocalStrikeCaches_IAcknowledgeThisIsIncrediblyExperimental;
47+
48+
namespace {
49+
__attribute__((constructor)) void UseThreadLocalStrikeCaches() {
50+
gSkUseThreadLocalStrikeCaches_IAcknowledgeThisIsIncrediblyExperimental = true;
51+
}
52+
} // namespace
53+
4454
unsigned long Skwasm::GetRasterThread() {
4555
static unsigned long thread = []() {
4656
if (skwasm_isSingleThreaded()) {
@@ -86,6 +96,11 @@ void Skwasm::Surface::Dispose() {
8696
uint32_t Skwasm::Surface::SetCanvas(SkwasmObject canvas) {
8797
assert(emscripten_is_main_browser_thread());
8898
uint32_t callback_id = ++current_callback_id_;
99+
100+
// Allocated here instead of on the worker so that current_callback_id_ is
101+
// only ever modified on the main thread.
102+
context_lost_callback_id_ = ++current_callback_id_;
103+
89104
skwasm_dispatchTransferCanvas(GetRasterThread(), this, canvas, callback_id);
90105
return callback_id;
91106
}
@@ -129,9 +144,11 @@ void Skwasm::Surface::ReceiveCanvasOnWorker(SkwasmObject canvas,
129144
render_context_ = Skwasm::RenderContext::Make(sample_count, stencil);
130145
render_context_->Resize(canvas_width_, canvas_height_);
131146

132-
context_lost_callback_id_ = ++current_callback_id_;
147+
if (resource_cache_limit_) {
148+
render_context_->SetResourceCacheLimit(*resource_cache_limit_);
149+
}
133150

134-
skwasm_reportInitialized(this, context_lost_callback_id_, callback_id);
151+
skwasm_reportInitialized(this, callback_id);
135152
}
136153

137154
// Resizing
@@ -288,8 +305,20 @@ void Skwasm::Surface::OnContextLost() {
288305

289306
// Other
290307

308+
// Main thread only
291309
void Skwasm::Surface::SetResourceCacheLimit(int bytes) {
292-
render_context_->SetResourceCacheLimit(bytes);
310+
assert(emscripten_is_main_browser_thread());
311+
skwasm_dispatchSetResourceCacheLimit(GetRasterThread(), this, bytes);
312+
}
313+
314+
// Worker thread only
315+
void Skwasm::Surface::SetResourceCacheLimitOnWorker(int bytes) {
316+
// Always stored so ReceiveCanvasOnWorker can reapply it whenever the
317+
// render context is (re)created.
318+
resource_cache_limit_ = bytes;
319+
if (render_context_) {
320+
render_context_->SetResourceCacheLimit(bytes);
321+
}
293322
}
294323

295324
std::unique_ptr<Skwasm::TextureSourceWrapper>
@@ -410,9 +439,16 @@ SKWASM_EXPORT void surface_dispose(Skwasm::Surface* surface) {
410439

411440
SKWASM_EXPORT void surface_setResourceCacheLimitBytes(Skwasm::Surface* surface,
412441
int bytes) {
442+
// Dispatch to the worker, which owns the render context.
413443
surface->SetResourceCacheLimit(bytes);
414444
}
415445

446+
SKWASM_EXPORT void surface_setResourceCacheLimitOnWorker(
447+
Skwasm::Surface* surface,
448+
int bytes) {
449+
surface->SetResourceCacheLimitOnWorker(bytes);
450+
}
451+
416452
SKWASM_EXPORT uint32_t surface_renderPictures(Skwasm::Surface* surface,
417453
flutter::DisplayList** pictures,
418454
int count) {

‎engine/src/flutter/skwasm/surface.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <emscripten/threading.h>
1313
#include <webgl/webgl1.h>
1414
#include <cassert>
15+
#include <optional>
1516
#include "export.h"
1617
#include "render_context.h"
1718
#include "wrappers.h"
@@ -80,6 +81,7 @@ class Surface {
8081

8182
// Other
8283
void SetResourceCacheLimit(int bytes);
84+
void SetResourceCacheLimitOnWorker(int bytes);
8385
std::unique_ptr<TextureSourceWrapper> CreateTextureSourceWrapper(
8486
SkwasmObject textureSource);
8587

@@ -89,6 +91,8 @@ class Surface {
8991
void RecreateSurface();
9092

9193
CallbackHandler* callback_handler_ = nullptr;
94+
95+
// Main thread only
9296
uint32_t current_callback_id_ = 0;
9397

9498
int canvas_width_ = 0;
@@ -98,6 +102,10 @@ class Surface {
98102
std::unique_ptr<RenderContext> render_context_;
99103
uint32_t context_lost_callback_id_ = 0;
100104

105+
// Worker thread only: the desired resource cache limit for the surface,
106+
// reapplied whenever the render context is (re)created.
107+
std::optional<int> resource_cache_limit_;
108+
101109
bool is_initialized_ = false;
102110
};
103111
} // namespace Skwasm

0 commit comments

Comments
 (0)