[Impeller] Implement drawShadow (flutter/engine#35426)

This commit is contained in:
Brandon DeRosier
2022-08-16 17:06:58 -07:00
committed by GitHub
parent c659f04b16
commit f237e36090
4 changed files with 95 additions and 5 deletions

View File

@@ -83,6 +83,10 @@ void Canvas::Concat(const Matrix& xformation) {
xformation_stack_.back().xformation = GetCurrentTransformation() * xformation;
}
void Canvas::PreConcat(const Matrix& xformation) {
xformation_stack_.back().xformation = xformation * GetCurrentTransformation();
}
void Canvas::ResetTransform() {
xformation_stack_.back().xformation = {};
}
@@ -227,8 +231,6 @@ void Canvas::RestoreClip() {
GetCurrentPass().AddEntity(std::move(entity));
}
void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {}
void Canvas::DrawPicture(Picture picture) {
if (!picture.pass) {
return;

View File

@@ -54,6 +54,8 @@ class Canvas {
void Concat(const Matrix& xformation);
void PreConcat(const Matrix& xformation);
void Translate(const Vector3& offset);
void Scale(const Vector2& scale);
@@ -89,8 +91,6 @@ class Canvas {
Path path,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
void DrawShadow(Path path, Color color, Scalar elevation);
void DrawPicture(Picture picture);
void DrawTextFrame(TextFrame text_frame, Point position, Paint paint);

View File

@@ -4,6 +4,7 @@
#include "impeller/display_list/display_list_dispatcher.h"
#include <algorithm>
#include <optional>
#include <unordered_map>
@@ -1061,7 +1062,66 @@ void DisplayListDispatcher::drawShadow(const SkPath& path,
const SkScalar elevation,
bool transparent_occluder,
SkScalar dpr) {
UNIMPLEMENTED;
Color spot_color = ToColor(color);
spot_color.alpha *= 0.25;
// Compute the spot color -- ported from SkShadowUtils::ComputeTonalColors.
{
Scalar max =
std::max(std::max(spot_color.red, spot_color.green), spot_color.blue);
Scalar min =
std::min(std::min(spot_color.red, spot_color.green), spot_color.blue);
Scalar luminance = (min + max) * 0.5;
Scalar alpha_adjust =
(2.6f + (-2.66667f + 1.06667f * spot_color.alpha) * spot_color.alpha) *
spot_color.alpha;
Scalar color_alpha =
(3.544762f + (-4.891428f + 2.3466f * luminance) * luminance) *
luminance;
color_alpha = std::clamp(alpha_adjust * color_alpha, 0.0f, 1.0f);
Scalar greyscale_alpha =
std::clamp(spot_color.alpha * (1 - 0.4f * luminance), 0.0f, 1.0f);
Scalar color_scale = color_alpha * (1 - greyscale_alpha);
Scalar tonal_alpha = color_scale + greyscale_alpha;
Scalar unpremul_scale = color_scale / tonal_alpha;
spot_color = Color(unpremul_scale * spot_color.red,
unpremul_scale * spot_color.green,
unpremul_scale * spot_color.blue, tonal_alpha);
}
Vector3 light_position(0, -1, 1);
Scalar occluder_z = dpr * elevation;
constexpr Scalar kLightRadius = 800 / 600; // Light radius / light height
Paint paint;
paint.style = Paint::Style::kFill;
paint.color = spot_color;
paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{
.style = FilterContents::BlurStyle::kNormal,
.sigma = Radius{kLightRadius * occluder_z /
canvas_.GetCurrentTransformation().GetScale().y},
};
canvas_.Save();
canvas_.PreConcat(
Matrix::MakeTranslation(Vector2(0, -occluder_z * light_position.y)));
SkRect rect;
SkRRect rrect;
if (path.isRect(&rect)) {
canvas_.DrawRect(ToRect(rect), std::move(paint));
} else if (path.isRRect(&rrect) && rrect.isSimple()) {
canvas_.DrawRRect(ToRect(rrect.rect()), rrect.getSimpleRadii().fX,
std::move(paint));
} else {
canvas_.DrawPath(ToPath(path), std::move(paint));
}
canvas_.Restore();
}
Picture DisplayListDispatcher::EndRecordingAsPicture() {

View File

@@ -16,6 +16,7 @@
#include "impeller/display_list/display_list_playground.h"
#include "impeller/geometry/point.h"
#include "impeller/playground/widgets.h"
#include "include/core/SkRRect.h"
#include "third_party/imgui/imgui.h"
#include "third_party/skia/include/core/SkClipOp.h"
#include "third_party/skia/include/core/SkColor.h"
@@ -493,5 +494,32 @@ TEST_P(DisplayListTest, CanDrawZeroLengthLine) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_P(DisplayListTest, CanDrawShadow) {
flutter::DisplayListBuilder builder;
std::array<SkPath, 3> paths = {
SkPath{}.addRect(SkRect::MakeXYWH(0, 0, 200, 100)),
SkPath{}.addRRect(
SkRRect::MakeRectXY(SkRect::MakeXYWH(0, 0, 200, 100), 30, 30)),
SkPath{}.addCircle(100, 50, 50),
};
builder.setColor(flutter::DlColor::kWhite());
builder.drawPaint();
builder.setColor(flutter::DlColor::kCyan());
builder.translate(100, 100);
for (size_t x = 0; x < paths.size(); x++) {
builder.save();
for (size_t y = 0; y < 5; y++) {
builder.drawShadow(paths[x], flutter::DlColor::kBlack(), 3 + y * 5, false,
1);
builder.drawPath(paths[x]);
builder.translate(0, 200);
}
builder.restore();
builder.translate(300, 0);
}
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
} // namespace testing
} // namespace impeller