2017-02-01 17:23:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2020-08-26 16:56:51 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkShadowFlags.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkShadowUtils.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2017-02-01 17:23:25 +00:00
|
|
|
|
2018-02-22 15:58:34 +00:00
|
|
|
void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color,
|
|
|
|
SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) {
|
2017-02-01 17:23:25 +00:00
|
|
|
SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
|
|
|
|
SkScalar spotAlpha = isAmbient ? 0.f : .5f;
|
2018-02-22 15:58:34 +00:00
|
|
|
SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color),
|
|
|
|
SkColorGetG(color), SkColorGetB(color));
|
|
|
|
SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color),
|
|
|
|
SkColorGetG(color), SkColorGetB(color));
|
|
|
|
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR,
|
|
|
|
ambientColor, spotColor, flags);
|
2017-02-01 17:23:25 +00:00
|
|
|
}
|
|
|
|
|
Revert "Revert "Create new inset algorithm for spot shadows""
This reverts commit e7c85c45c4c0a97adc6711bb12ecacc36af4ba11.
Reason for revert: <INSERT REASONING HERE>
Original change's description:
> Revert "Create new inset algorithm for spot shadows"
>
> This reverts commit e5f5bf5175e426ebb6aa234f4387831c898f20ad.
>
> Reason for revert: Breaking a bunch of bots. e.g:
>
> https://luci-milo.appspot.com/swarming/task/3519cae0a03c7b10/steps/dm/0/stdout
>
> Original change's description:
> > Create new inset algorithm for spot shadows
> >
> > BUG=skia:
> >
> > Change-Id: If7c67c2a5b9beea28f86d13362a5156b46394d0e
> > Reviewed-on: https://skia-review.googlesource.com/9875
> > Commit-Queue: Ravi Mistry <rmistry@google.com>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
> >
>
> TBR=jvanverth@google.com,bsalomon@google.com,rmistry@google.com,robertphillips@google.com,msarett@google.com,reviews@skia.org
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Change-Id: I3d119ff631dbb1a41f873b9c8753d542ec91254e
> Reviewed-on: https://skia-review.googlesource.com/10112
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>
>
TBR=jvanverth@google.com,bsalomon@google.com,rmistry@google.com,msarett@google.com,robertphillips@google.com,reviews@skia.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Change-Id: Ib3998300606d3a2e2fb3a14b2088cfad48363501
Reviewed-on: https://skia-review.googlesource.com/10113
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2017-03-24 16:07:20 +00:00
|
|
|
static constexpr int kW = 800;
|
2018-02-28 19:51:19 +00:00
|
|
|
static constexpr int kH = 960;
|
2017-02-01 17:23:25 +00:00
|
|
|
|
2017-12-19 18:02:38 +00:00
|
|
|
enum ShadowMode {
|
|
|
|
kDebugColorNoOccluders,
|
|
|
|
kDebugColorOccluders,
|
|
|
|
kGrayscale
|
|
|
|
};
|
|
|
|
|
|
|
|
void draw_paths(SkCanvas* canvas, ShadowMode mode) {
|
2017-02-01 17:23:25 +00:00
|
|
|
SkTArray<SkPath> paths;
|
2021-03-10 15:36:55 +00:00
|
|
|
paths.push_back(SkPath::RRect(SkRect::MakeWH(50, 50), 10, 10.00002f));
|
2017-02-01 17:23:25 +00:00
|
|
|
SkRRect oddRRect;
|
|
|
|
oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
|
2020-08-26 16:56:51 +00:00
|
|
|
paths.push_back(SkPath::RRect(oddRRect));
|
|
|
|
paths.push_back(SkPath::Rect(SkRect::MakeWH(50, 50)));
|
|
|
|
paths.push_back(SkPath::Circle(25, 25, 25));
|
|
|
|
paths.push_back(SkPathBuilder().cubicTo(100, 50, 20, 100, 0, 0).detach());
|
|
|
|
paths.push_back(SkPath::Oval(SkRect::MakeWH(20, 60)));
|
2017-02-01 17:23:25 +00:00
|
|
|
|
2018-02-28 19:51:19 +00:00
|
|
|
// star
|
|
|
|
SkTArray<SkPath> concavePaths;
|
|
|
|
concavePaths.push_back().moveTo(0.0f, -33.3333f);
|
|
|
|
concavePaths.back().lineTo(9.62f, -16.6667f);
|
|
|
|
concavePaths.back().lineTo(28.867f, -16.6667f);
|
|
|
|
concavePaths.back().lineTo(19.24f, 0.0f);
|
|
|
|
concavePaths.back().lineTo(28.867f, 16.6667f);
|
|
|
|
concavePaths.back().lineTo(9.62f, 16.6667f);
|
|
|
|
concavePaths.back().lineTo(0.0f, 33.3333f);
|
|
|
|
concavePaths.back().lineTo(-9.62f, 16.6667f);
|
|
|
|
concavePaths.back().lineTo(-28.867f, 16.6667f);
|
|
|
|
concavePaths.back().lineTo(-19.24f, 0.0f);
|
|
|
|
concavePaths.back().lineTo(-28.867f, -16.6667f);
|
|
|
|
concavePaths.back().lineTo(-9.62f, -16.6667f);
|
|
|
|
concavePaths.back().close();
|
|
|
|
|
|
|
|
// dumbbell
|
|
|
|
concavePaths.push_back().moveTo(50, 0);
|
|
|
|
concavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
|
|
|
|
concavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
|
|
|
|
|
2017-02-01 17:23:25 +00:00
|
|
|
static constexpr SkScalar kPad = 15.f;
|
|
|
|
static constexpr SkScalar kLightR = 100.f;
|
|
|
|
static constexpr SkScalar kHeight = 50.f;
|
2017-05-16 19:01:43 +00:00
|
|
|
|
|
|
|
// transform light position relative to canvas to handle tiling
|
|
|
|
SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
|
|
|
|
SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
|
|
|
|
|
2017-02-01 17:23:25 +00:00
|
|
|
canvas->translate(3 * kPad, 3 * kPad);
|
|
|
|
canvas->save();
|
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar dy = 0;
|
|
|
|
SkTDArray<SkMatrix> matrices;
|
|
|
|
matrices.push()->reset();
|
|
|
|
SkMatrix* m = matrices.push();
|
|
|
|
m->setRotate(33.f, 25.f, 25.f);
|
|
|
|
m->postScale(1.2f, 0.8f, 25.f, 25.f);
|
|
|
|
for (auto& m : matrices) {
|
2017-12-19 18:02:38 +00:00
|
|
|
for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
|
2019-07-24 18:46:53 +00:00
|
|
|
int pathCounter = 0;
|
2017-02-01 17:23:25 +00:00
|
|
|
for (const auto& path : paths) {
|
|
|
|
SkRect postMBounds = path.getBounds();
|
|
|
|
m.mapRect(&postMBounds);
|
|
|
|
SkScalar w = postMBounds.width() + kHeight;
|
|
|
|
SkScalar dx = w + kPad;
|
|
|
|
if (x + dx > kW - 3 * kPad) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dy);
|
|
|
|
canvas->save();
|
|
|
|
x = 0;
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(m);
|
|
|
|
|
2019-07-24 18:46:53 +00:00
|
|
|
// flip a couple of paths to test 180° rotation
|
|
|
|
if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->rotate(180, 25, 25);
|
|
|
|
}
|
2017-12-19 18:02:38 +00:00
|
|
|
if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
|
|
|
|
draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
|
|
|
|
true, flags);
|
|
|
|
draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
|
|
|
|
false, flags);
|
|
|
|
} else if (kGrayscale == mode) {
|
2018-02-22 15:58:34 +00:00
|
|
|
SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
|
|
|
|
SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
|
|
|
|
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor, flags);
|
2017-12-19 18:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 17:23:25 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2017-12-19 18:02:38 +00:00
|
|
|
if (kDebugColorNoOccluders == mode) {
|
|
|
|
// Draw the path outline in green on top of the ambient and spot shadows.
|
2017-05-15 14:44:22 +00:00
|
|
|
if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
} else {
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
}
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
} else {
|
2017-12-19 18:02:38 +00:00
|
|
|
paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
|
2017-05-15 14:44:22 +00:00
|
|
|
if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
|
2019-02-15 21:13:57 +00:00
|
|
|
paint.setAlphaf(0.5f);
|
2017-05-15 14:44:22 +00:00
|
|
|
}
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
}
|
2017-02-01 17:23:25 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2019-07-24 18:46:53 +00:00
|
|
|
if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2017-02-01 17:23:25 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
x += dx;
|
2020-02-07 15:36:46 +00:00
|
|
|
dy = std::max(dy, postMBounds.height() + kPad + kHeight);
|
2019-07-24 18:46:53 +00:00
|
|
|
++pathCounter;
|
2017-02-01 17:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 19:51:19 +00:00
|
|
|
|
|
|
|
// concave paths
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kPad, dy);
|
|
|
|
canvas->save();
|
|
|
|
x = kPad;
|
|
|
|
dy = 0;
|
|
|
|
for (auto& m : matrices) {
|
|
|
|
// for the concave paths we are not clipping, so transparent and opaque are the same
|
|
|
|
for (const auto& path : concavePaths) {
|
|
|
|
SkRect postMBounds = path.getBounds();
|
|
|
|
m.mapRect(&postMBounds);
|
|
|
|
SkScalar w = postMBounds.width() + kHeight;
|
|
|
|
SkScalar dx = w + kPad;
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(m);
|
|
|
|
|
|
|
|
if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
|
|
|
|
draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
|
|
|
|
true, kNone_ShadowFlag);
|
|
|
|
draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
|
|
|
|
false, kNone_ShadowFlag);
|
|
|
|
} else if (kGrayscale == mode) {
|
|
|
|
SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
|
|
|
|
SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
|
|
|
|
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor, kNone_ShadowFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
if (kDebugColorNoOccluders == mode) {
|
|
|
|
// Draw the path outline in green on top of the ambient and spot shadows.
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
} else {
|
|
|
|
paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
}
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
x += dx;
|
2020-02-07 15:36:46 +00:00
|
|
|
dy = std::max(dy, postMBounds.height() + kPad + kHeight);
|
2018-02-28 19:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:23:25 +00:00
|
|
|
// Show where the light is in x,y as a circle (specified in device space).
|
|
|
|
SkMatrix invCanvasM = canvas->getTotalMatrix();
|
|
|
|
if (invCanvasM.invert(&invCanvasM)) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(invCanvasM);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setAntiAlias(true);
|
2017-05-16 19:01:43 +00:00
|
|
|
canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
|
2017-02-01 17:23:25 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 14:44:22 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
|
2017-12-19 18:02:38 +00:00
|
|
|
draw_paths(canvas, kDebugColorNoOccluders);
|
2017-05-15 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
|
2017-12-19 18:02:38 +00:00
|
|
|
draw_paths(canvas, kDebugColorOccluders);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
|
|
|
|
draw_paths(canvas, kGrayscale);
|
2017-05-15 14:44:22 +00:00
|
|
|
}
|
2020-03-27 19:11:10 +00:00
|
|
|
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "src/core/SkColorFilterPriv.h"
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadow_utils_gaussian_colorfilter, canvas, 512, 256) {
|
|
|
|
const SkRect r = SkRect::MakeWH(256, 256);
|
|
|
|
|
|
|
|
const SkColor colors[] = { 0, 0xFF000000 };
|
|
|
|
auto sh = SkGradientShader::MakeRadial({r.centerX(), r.centerY()}, r.width(),
|
|
|
|
colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkTileMode::kClamp);
|
|
|
|
|
|
|
|
SkPaint redPaint;
|
|
|
|
redPaint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(sh);
|
|
|
|
canvas->drawRect(r, redPaint);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
canvas->translate(256, 0);
|
|
|
|
paint.setColorFilter(SkColorFilterPriv::MakeGaussian());
|
|
|
|
canvas->drawRect(r, redPaint);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
2020-12-16 16:56:11 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadow_utils_directional, canvas, 256, 384) {
|
|
|
|
static constexpr SkScalar kLightR = 1.f;
|
|
|
|
static constexpr SkScalar kHeight = 12.f;
|
|
|
|
|
|
|
|
SkPath rrect(SkPath::RRect(SkRect::MakeLTRB(-25, -25, 25, 25), 10, 10));
|
2021-07-15 20:24:17 +00:00
|
|
|
SkPoint3 lightPos = { -45, -45, 77.9422863406f };
|
2020-12-16 16:56:11 +00:00
|
|
|
|
|
|
|
SkColor ambientColor = SkColorSetARGB(0.02f * 255, 0, 0, 0);
|
|
|
|
SkColor spotColor = SkColorSetARGB(0.35f * 255, 0, 0, 0);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
|
|
|
// translation
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(35, 35);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor,
|
|
|
|
kDirectionalLight_ShadowFlag);
|
|
|
|
canvas->drawPath(rrect, paint);
|
|
|
|
canvas->translate(80, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(35 + 80*i, 105);
|
|
|
|
canvas->rotate(20.f*(i + 1));
|
|
|
|
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor,
|
|
|
|
kDirectionalLight_ShadowFlag);
|
|
|
|
|
|
|
|
canvas->drawPath(rrect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
SkScalar scaleFactor = sk_float_pow(2.0, -i);
|
|
|
|
canvas->translate(35 + 80*i, 185);
|
|
|
|
canvas->scale(scaleFactor, scaleFactor);
|
|
|
|
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor,
|
|
|
|
kDirectionalLight_ShadowFlag);
|
|
|
|
|
|
|
|
canvas->drawPath(rrect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// perspective
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
SkMatrix mat;
|
|
|
|
mat.reset();
|
|
|
|
mat[SkMatrix::kMPersp1] = 0.005f;
|
|
|
|
mat[SkMatrix::kMPersp2] = 1.005f;
|
|
|
|
canvas->translate(35 + 80*i, 265);
|
|
|
|
canvas->concat(mat);
|
|
|
|
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
|
|
|
|
kLightR, ambientColor, spotColor,
|
|
|
|
kDirectionalLight_ShadowFlag);
|
|
|
|
|
|
|
|
canvas->drawPath(rrect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|