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"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/utils/SkShadowUtils.h"
|
|
|
|
#include "src/core/SkResourceCache.h"
|
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;
|
|
|
|
paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
|
|
|
|
SkRRect oddRRect;
|
|
|
|
oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
|
|
|
|
paths.push_back().addRRect(oddRRect);
|
|
|
|
paths.push_back().addRect(SkRect::MakeWH(50, 50));
|
|
|
|
paths.push_back().addCircle(25, 25, 25);
|
|
|
|
paths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
|
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
|
|
|
paths.push_back().addOval(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 }) {
|
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);
|
|
|
|
|
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);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
x += dx;
|
|
|
|
dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|