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

[web] cache sample and stencil params #38829

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ extension CanvasKitExtension on CanvasKit {
int width,
int height,
ColorSpace colorSpace,
int sampleCount,
int stencil,
);
external SkSurface MakeSWCanvasSurface(DomCanvasElement canvas);

Expand Down
13 changes: 13 additions & 0 deletions lib/web_ui/lib/src/engine/canvaskit/surface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Surface {
DomCanvasElement? htmlCanvas;
int _pixelWidth = -1;
int _pixelHeight = -1;
int _sampleCount = -1;
int _stencilBits = -1;

/// Specify the GPU resource cache limits.
void setSkiaResourceCacheMaxBytes(int bytes) {
Expand Down Expand Up @@ -334,6 +336,9 @@ class Surface {
majorVersion: webGLVersion.toDouble(),
),
).toInt();
if (_sampleCount == -1 || _stencilBits == -1) {
_initWebglParams();
}

_glContext = glContext;

Expand All @@ -352,6 +357,12 @@ class Surface {
htmlElement.append(htmlCanvas);
}

void _initWebglParams() {
final WebGLContext gl = htmlCanvas!.getGlContext(webGLVersion);
_sampleCount = gl.getParameter(gl.samples);
_stencilBits = gl.getParameter(gl.stencilBits);
}

CkSurface _createNewSurface(ui.Size size) {
assert(htmlCanvas != null);
if (webGLVersion == -1) {
Expand All @@ -369,6 +380,8 @@ class Surface {
size.width.ceil(),
size.height.ceil(),
SkColorSpaceSRGB,
_sampleCount,
_stencilBits
);

if (skSurface == null) {
Expand Down
21 changes: 21 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,27 @@ extension DomCanvasElementExtension on DomCanvasElement {

DomCanvasRenderingContext2D get context2D =>
getContext('2d')! as DomCanvasRenderingContext2D;

WebGLContext getGlContext(int majorVersion) {
if (majorVersion == 1) {
return getContext('webgl')! as WebGLContext;
}
return getContext('webgl2')! as WebGLContext;
}
}

@JS()
@staticInterop
class WebGLContext {}

extension WebGLContextExtension on WebGLContext {
external int getParameter(int value);

@JS('SAMPLES')
external int get samples;

@JS('STENCIL_BITS')
external int get stencilBits;
}

@JS()
Expand Down
29 changes: 29 additions & 0 deletions lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1781,4 +1781,33 @@ void _paragraphTests() {
canvasKit.TextHeightBehavior.DisableAll,
);
});

test('MakeOnScreenGLSurface test', () {
final DomCanvasElement canvas = createDomCanvasElement(
width: 100,
height: 100,
);
final WebGLContext gl = canvas.getGlContext(webGLVersion);
final int sampleCount = gl.getParameter(gl.samples);
final int stencilBits = gl.getParameter(gl.stencilBits);

final int glContext = canvasKit.GetWebGLContext(
canvas,
SkWebGLContextOptions(
antialias: 0,
majorVersion: webGLVersion.toDouble(),
),
).toInt();
final SkGrContext grContext = canvasKit.MakeGrContext(glContext);
final SkSurface? skSurface = canvasKit.MakeOnScreenGLSurface(
grContext,
100,
100,
SkColorSpaceSRGB,
sampleCount,
stencilBits
);

expect(skSurface, isNotNull);
}, skip: isFirefox); // Intended: Headless firefox has no webgl support https://github.com/flutter/flutter/issues/109265
}