Skip to content

Commit

Permalink
[canvaskit] Fix Shader program tests (#37644)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Terkelsen authored Dec 3, 2022
1 parent 2c1e5ef commit 8e11659
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
83 changes: 41 additions & 42 deletions lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,47 @@ void _shaderTests() {
),
isNotNull);
});

test('RuntimeEffect', () {
// TODO(hterkelsen): Remove this check when local CanvasKit is default.
if (isRuntimeEffectAvailable) {
const String kSkSlProgram = r'''
half4 main(vec2 fragCoord) {
return vec4(1.0, 0.0, 0.0, 1.0);
}
''';

final SkRuntimeEffect? effect = MakeRuntimeEffect(kSkSlProgram);
expect(effect, isNotNull);

const String kInvalidSkSlProgram = '';

// Invalid SkSL returns null.
final SkRuntimeEffect? invalidEffect = MakeRuntimeEffect(kInvalidSkSlProgram);
expect(invalidEffect, isNull);

final SkShader? shader = effect!.makeShader(<double>[]);
expect(shader, isNotNull);

// mismatched uniforms returns null.
final SkShader? invalidShader = effect.makeShader(<double>[1]);

expect(invalidShader, isNull);

const String kSkSlProgramWithUniforms = r'''
uniform vec4 u_color;
half4 main(vec2 fragCoord) {
return u_color;
}
''';

final SkShader? shaderWithUniform = MakeRuntimeEffect(kSkSlProgramWithUniforms)
!.makeShader(<double>[1.0, 0.0, 0.0, 1.0]);

expect(shaderWithUniform, isNotNull);
}
});
}

SkShader _makeTestShader() {
Expand Down Expand Up @@ -1707,46 +1748,4 @@ void _paragraphTests() {
canvasKit.TextHeightBehavior.DisableAll,
);
});

test('RuntimeEffect', () {
// Is supported..
expect(isRuntimeEffectAvailable, isTrue);

const String kSkSlProgram = r'''
half4 main(vec2 fragCoord) {
return vec4(1.0, 0.0, 0.0, 1.0);
}
''';

final SkRuntimeEffect? effect = MakeRuntimeEffect(kSkSlProgram);
expect(effect, isNotNull);

const String kInvalidSkSlProgram = '';

// Invalid SkSL returns null.
final SkRuntimeEffect? invalidEffect = MakeRuntimeEffect(kInvalidSkSlProgram);
expect(invalidEffect, isNull);

final SkShader? shader = effect!.makeShader(<double>[]);
expect(shader, isNotNull);

// mismatched uniforms returns null.
final SkShader? invalidShader = effect.makeShader(<double>[1]);

expect(invalidShader, isNull);

const String kSkSlProgramWithUniforms = r'''
uniform vec4 u_color;
half4 main(vec2 fragCoord) {
return u_color;
}
''';

final SkShader? shaderWithUniform = MakeRuntimeEffect(kSkSlProgramWithUniforms)
!.makeShader(<double>[1.0, 0.0, 0.0, 1.0]);

expect(shaderWithUniform, isNotNull);
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/115327
}, skip: true);
}
36 changes: 19 additions & 17 deletions lib/web_ui/test/canvaskit/fragment_program_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,29 @@ void testMain() {
});

test('FragmentProgram can be created from JSON IPLR bundle', () async {
final Uint8List data = utf8.encode(kJsonIPLR) as Uint8List;
final CkFragmentProgram program = await CkFragmentProgram.fromBytes('test', data);
// TODO(hterkelsen): Remove this check when local CanvasKit is default.
if (isRuntimeEffectAvailable) {
final Uint8List data = utf8.encode(kJsonIPLR) as Uint8List;
final CkFragmentProgram program = await CkFragmentProgram.fromBytes('test', data);

expect(program.effect, isNotNull);
expect(program.floatCount, 17);
expect(program.textureCount, 0);
expect(program.uniforms, hasLength(17));
expect(program.name, 'test');
expect(program.effect, isNotNull);
expect(program.floatCount, 17);
expect(program.textureCount, 0);
expect(program.uniforms, hasLength(17));
expect(program.name, 'test');

final ui.FragmentShader shader = program.fragmentShader();
final ui.FragmentShader shader = program.fragmentShader();

shader.setFloat(0, 4);
shader.dispose();
shader.setFloat(0, 4);
shader.dispose();

expect(shader.debugDisposed, true);
expect(shader.debugDisposed, true);

final ui.FragmentShader shader2 = program.fragmentShader();
final ui.FragmentShader shader2 = program.fragmentShader();

shader.setFloat(0, 5);
shader2.dispose();
expect(shader2.debugDisposed, true);
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/115327
}, skip: true);
shader.setFloat(0, 5);
shader2.dispose();
expect(shader2.debugDisposed, true);
}
});
}

0 comments on commit 8e11659

Please sign in to comment.