Implement shadows in the skwasm renderer. (flutter/engine#41056)

Implement shadows in the skwasm renderer.
This commit is contained in:
Jackson Gardner
2023-04-11 15:44:01 -07:00
committed by GitHub
parent 03acaa99c4
commit 4b8536c6e9
6 changed files with 150 additions and 24 deletions

View File

@@ -28,8 +28,8 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void saveLayer(ui.Rect? bounds, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void saveLayer(ui.Rect? bounds, ui.Paint paint) {
paint as SkwasmPaint;
if (bounds != null) {
withStackScope((StackScope s) {
canvasSaveLayer(_handle, s.convertRectToNative(bounds), paint.handle);
@@ -87,8 +87,8 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void clipPath(ui.Path uiPath, {bool doAntiAlias = true}) {
final SkwasmPath path = uiPath as SkwasmPath;
void clipPath(ui.Path path, {bool doAntiAlias = true}) {
path as SkwasmPath;
canvasClipPath(_handle, path.handle, doAntiAlias);
}
@@ -97,20 +97,20 @@ class SkwasmCanvas implements ui.Canvas {
canvasDrawColor(_handle, color.value, blendMode.index);
@override
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
paint as SkwasmPaint;
canvasDrawLine(_handle, p1.dx, p1.dy, p2.dx, p2.dy, paint.handle);
}
@override
void drawPaint(ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawPaint(ui.Paint paint) {
paint as SkwasmPaint;
canvasDrawPaint(_handle, paint.handle);
}
@override
void drawRect(ui.Rect rect, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawRect(ui.Rect rect, ui.Paint paint) {
paint as SkwasmPaint;
withStackScope((StackScope s) {
canvasDrawRect(
_handle,
@@ -121,8 +121,8 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void drawRRect(ui.RRect rrect, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawRRect(ui.RRect rrect, ui.Paint paint) {
paint as SkwasmPaint;
withStackScope((StackScope s) {
canvasDrawRRect(
_handle,
@@ -133,8 +133,8 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
paint as SkwasmPaint;
withStackScope((StackScope s) {
canvasDrawDRRect(
_handle,
@@ -146,23 +146,23 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void drawOval(ui.Rect rect, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawOval(ui.Rect rect, ui.Paint paint) {
paint as SkwasmPaint;
withStackScope((StackScope s) {
canvasDrawOval(_handle, s.convertRectToNative(rect), paint.handle);
});
}
@override
void drawCircle(ui.Offset center, double radius, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
void drawCircle(ui.Offset center, double radius, ui.Paint paint) {
paint as SkwasmPaint;
canvasDrawCircle(_handle, center.dx, center.dy, radius, paint.handle);
}
@override
void drawArc(ui.Rect rect, double startAngle, double sweepAngle,
bool useCenter, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
bool useCenter, ui.Paint paint) {
paint as SkwasmPaint;
withStackScope((StackScope s) {
canvasDrawArc(
_handle,
@@ -176,9 +176,9 @@ class SkwasmCanvas implements ui.Canvas {
}
@override
void drawPath(ui.Path uiPath, ui.Paint uiPaint) {
final SkwasmPaint paint = uiPaint as SkwasmPaint;
final SkwasmPath path = uiPath as SkwasmPath;
void drawPath(ui.Path path, ui.Paint paint) {
paint as SkwasmPaint;
path as SkwasmPath;
canvasDrawPath(_handle, path.handle, paint.handle);
}
@@ -260,7 +260,14 @@ class SkwasmCanvas implements ui.Canvas {
double elevation,
bool transparentOccluder,
) {
// TODO(jacksongardner): implement this
path as SkwasmPath;
canvasDrawShadow(
_handle,
path.handle,
elevation,
ui.window.devicePixelRatio,
color.value,
transparentOccluder);
}
@override

View File

@@ -126,6 +126,17 @@ external void canvasDrawPath(
symbol: 'canvas_drawPicture', isLeaf: true)
external void canvasDrawPicture(CanvasHandle canvas, PictureHandle picture);
@Native<Void Function(CanvasHandle, PathHandle, Float, Float, Int32, Bool)>(
symbol: 'canvas_drawShadow', isLeaf: true)
external void canvasDrawShadow(
CanvasHandle canvas,
PathHandle path,
double elevation,
double devicePixelRatio,
int color,
bool transparentOccluder,
);
@Native<Void Function(CanvasHandle, RawMatrix44)>(
symbol: 'canvas_getTransform', isLeaf: true)
external void canvasGetTransform(CanvasHandle canvas, RawMatrix44 outMatrix);

View File

@@ -238,6 +238,9 @@ class SkwasmRenderer implements Renderer {
}
final SkwasmPicture picture = (scene as SkwasmScene).picture as SkwasmPicture;
await surface.renderPicture(picture);
// TODO(jacksongardner): Remove this hack. See https://github.com/flutter/flutter/issues/124616
await Future<void>.delayed(const Duration(milliseconds: 100));
}
@override

View File

@@ -7,8 +7,26 @@
#include "helpers.h"
#include "wrappers.h"
#include "third_party/skia/include/core/SkPoint3.h"
#include "third_party/skia/include/utils/SkShadowUtils.h"
using namespace Skwasm;
namespace {
// These numbers have been chosen empirically to give a result closest to the
// material spec.
// These values are also used by the CanvasKit renderer and the native engine.
// See:
// flutter/display_list/skia/dl_sk_dispatcher.cc
// flutter/lib/web_ui/lib/src/engine/canvaskit/util.dart
constexpr SkScalar kShadowAmbientAlpha = 0.039;
constexpr SkScalar kShadowSpotAlpha = 0.25;
constexpr SkScalar kShadowLightRadius = 1.1;
constexpr SkScalar kShadowLightHeight = 600.0;
constexpr SkScalar kShadowLightXOffset = 0;
constexpr SkScalar kShadowLightYOffset = -450;
} // namespace
SKWASM_EXPORT void canvas_destroy(CanvasWrapper* wrapper) {
delete wrapper;
}
@@ -162,6 +180,32 @@ SKWASM_EXPORT void canvas_drawPath(CanvasWrapper* wrapper,
wrapper->canvas->drawPath(*path, *paint);
}
SKWASM_EXPORT void canvas_drawShadow(CanvasWrapper* wrapper,
SkPath* path,
SkScalar elevation,
SkScalar devicePixelRatio,
SkColor color,
bool transparentOccluder) {
makeCurrent(wrapper->context);
SkColor inAmbient =
SkColorSetA(color, kShadowAmbientAlpha * SkColorGetA(color));
SkColor inSpot = SkColorSetA(color, kShadowSpotAlpha * SkColorGetA(color));
SkColor outAmbient;
SkColor outSpot;
SkShadowUtils::ComputeTonalColors(inAmbient, inSpot, &outAmbient, &outSpot);
uint32_t flags = transparentOccluder
? SkShadowFlags::kTransparentOccluder_ShadowFlag
: SkShadowFlags::kNone_ShadowFlag;
flags |= SkShadowFlags::kDirectionalLight_ShadowFlag;
SkShadowUtils::DrawShadow(
wrapper->canvas, *path,
SkPoint3::Make(0.0f, 0.0f, elevation * devicePixelRatio),
SkPoint3::Make(kShadowLightXOffset, kShadowLightYOffset,
kShadowLightHeight * devicePixelRatio),
devicePixelRatio * kShadowLightRadius, outAmbient, outSpot, flags);
}
SKWASM_EXPORT void canvas_drawPicture(CanvasWrapper* wrapper,
SkPicture* picture) {
makeCurrent(wrapper->context);

View File

@@ -0,0 +1,54 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/ui.dart' as ui;
import 'package:web_engine_tester/golden_tester.dart';
import 'utils.dart';
void main() {
internalBootstrapBrowserTest(() => testMain);
}
Future<void> testMain() async {
const ui.Rect region = ui.Rect.fromLTWH(0, 0, 300, 300);
setUpUiTest();
test('Test drawing a shadow of an opaque object', () async {
final ui.Picture picture = drawPicture((ui.Canvas canvas) {
final ui.Path path = ui.Path();
path.moveTo(50, 150);
path.cubicTo(100, 50, 200, 250, 250, 150);
canvas.drawShadow(
path,
const ui.Color(0xFF000000),
5,
false);
canvas.drawPath(path, ui.Paint()..color = const ui.Color(0xFFFF00FF));
});
await drawPictureUsingCurrentRenderer(picture);
await matchGoldenFile('shadow_opaque_object.png', region: region);
});
test('Test drawing a shadow of a translucent object', () async {
final ui.Picture picture = drawPicture((ui.Canvas canvas) {
final ui.Path path = ui.Path();
path.moveTo(50, 150);
path.cubicTo(100, 250, 200, 50, 250, 150);
canvas.drawShadow(
path,
const ui.Color(0xFF000000),
5,
true);
canvas.drawPath(path, ui.Paint()..color = const ui.Color(0x8F00FFFF));
});
await drawPictureUsingCurrentRenderer(picture);
await matchGoldenFile('shadow_translucent_object.png', region: region);
});
}

View File

@@ -21,6 +21,13 @@ void setUpUiTest() {
});
}
Picture drawPicture(void Function(Canvas) drawCommands) {
final PictureRecorder recorder = PictureRecorder();
final Canvas canvas = Canvas(recorder);
drawCommands(canvas);
return recorder.endRecording();
}
/// Draws the [Picture]. This is in preparation for a golden test.
Future<void> drawPictureUsingCurrentRenderer(Picture picture) async {
final SceneBuilder sb = SceneBuilder();