Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7dc5e7e

Browse files
author
Jonah Williams
authored
[web] cache sample and stencil params (#38829)
* [web] cache sample and stencil params * test and style * Update canvaskit_api_test.dart * Update canvaskit_api_test.dart
1 parent 9204745 commit 7dc5e7e

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ extension CanvasKitExtension on CanvasKit {
117117
int width,
118118
int height,
119119
ColorSpace colorSpace,
120+
int sampleCount,
121+
int stencil,
120122
);
121123
external SkSurface MakeSWCanvasSurface(DomCanvasElement canvas);
122124

lib/web_ui/lib/src/engine/canvaskit/surface.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class Surface {
9898
DomCanvasElement? htmlCanvas;
9999
int _pixelWidth = -1;
100100
int _pixelHeight = -1;
101+
int _sampleCount = -1;
102+
int _stencilBits = -1;
101103

102104
/// Specify the GPU resource cache limits.
103105
void setSkiaResourceCacheMaxBytes(int bytes) {
@@ -334,6 +336,9 @@ class Surface {
334336
majorVersion: webGLVersion.toDouble(),
335337
),
336338
).toInt();
339+
if (_sampleCount == -1 || _stencilBits == -1) {
340+
_initWebglParams();
341+
}
337342

338343
_glContext = glContext;
339344

@@ -352,6 +357,12 @@ class Surface {
352357
htmlElement.append(htmlCanvas);
353358
}
354359

360+
void _initWebglParams() {
361+
final WebGLContext gl = htmlCanvas!.getGlContext(webGLVersion);
362+
_sampleCount = gl.getParameter(gl.samples);
363+
_stencilBits = gl.getParameter(gl.stencilBits);
364+
}
365+
355366
CkSurface _createNewSurface(ui.Size size) {
356367
assert(htmlCanvas != null);
357368
if (webGLVersion == -1) {
@@ -369,6 +380,8 @@ class Surface {
369380
size.width.ceil(),
370381
size.height.ceil(),
371382
SkColorSpaceSRGB,
383+
_sampleCount,
384+
_stencilBits
372385
);
373386

374387
if (skSurface == null) {

lib/web_ui/lib/src/engine/dom.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,27 @@ extension DomCanvasElementExtension on DomCanvasElement {
624624

625625
DomCanvasRenderingContext2D get context2D =>
626626
getContext('2d')! as DomCanvasRenderingContext2D;
627+
628+
WebGLContext getGlContext(int majorVersion) {
629+
if (majorVersion == 1) {
630+
return getContext('webgl')! as WebGLContext;
631+
}
632+
return getContext('webgl2')! as WebGLContext;
633+
}
634+
}
635+
636+
@JS()
637+
@staticInterop
638+
class WebGLContext {}
639+
640+
extension WebGLContextExtension on WebGLContext {
641+
external int getParameter(int value);
642+
643+
@JS('SAMPLES')
644+
external int get samples;
645+
646+
@JS('STENCIL_BITS')
647+
external int get stencilBits;
627648
}
628649

629650
@JS()

lib/web_ui/test/canvaskit/canvaskit_api_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,4 +1778,33 @@ void _paragraphTests() {
17781778
canvasKit.TextHeightBehavior.DisableAll,
17791779
);
17801780
});
1781+
1782+
test('MakeOnScreenGLSurface test', () {
1783+
final DomCanvasElement canvas = createDomCanvasElement(
1784+
width: 100,
1785+
height: 100,
1786+
);
1787+
final WebGLContext gl = canvas.getGlContext(webGLVersion);
1788+
final int sampleCount = gl.getParameter(gl.samples);
1789+
final int stencilBits = gl.getParameter(gl.stencilBits);
1790+
1791+
final int glContext = canvasKit.GetWebGLContext(
1792+
canvas,
1793+
SkWebGLContextOptions(
1794+
antialias: 0,
1795+
majorVersion: webGLVersion.toDouble(),
1796+
),
1797+
).toInt();
1798+
final SkGrContext grContext = canvasKit.MakeGrContext(glContext);
1799+
final SkSurface? skSurface = canvasKit.MakeOnScreenGLSurface(
1800+
grContext,
1801+
100,
1802+
100,
1803+
SkColorSpaceSRGB,
1804+
sampleCount,
1805+
stencilBits
1806+
);
1807+
1808+
expect(skSurface, isNotNull);
1809+
}, skip: isFirefox); // Intended: Headless firefox has no webgl support https://github.com/flutter/flutter/issues/109265
17811810
}

0 commit comments

Comments
 (0)