| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPathBuilder.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkPoint3.h" |
| 16 |
#include "include/core/SkRRect.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/private/base/SkTArray.h" |
| 21 |
#include "include/private/base/SkTDArray.h" |
| 22 |
#include "include/utils/SkShadowUtils.h" |
| 23 |
|
| 24 |
#include <initializer_list> |
| 25 |
|
| 26 |
using namespace skia_private; |
| 27 |
|
| 28 |
void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color, |
| 29 |
SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) { |
| 30 |
SkScalar ambientAlpha = isAmbient ? .5f : 0.f; |
| 31 |
SkScalar spotAlpha = isAmbient ? 0.f : .5f; |
| 32 |
SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color), |
| 33 |
SkColorGetG(color), SkColorGetB(color)); |
| 34 |
SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color), |
| 35 |
SkColorGetG(color), SkColorGetB(color)); |
| 36 |
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR, |
| 37 |
ambientColor, spotColor, flags); |
| 38 |
} |
| 39 |
|
| 40 |
static constexpr int kW = 800; |
| 41 |
static constexpr int kH = 960; |
| 42 |
|
| 43 |
enum ShadowMode { |
| 44 |
kDebugColorNoOccluders, |
| 45 |
kDebugColorOccluders, |
| 46 |
kGrayscale |
| 47 |
}; |
| 48 |
|
| 49 |
void draw_paths(SkCanvas* canvas, ShadowMode mode) { |
| 50 |
TArray<SkPath> paths; |
| 51 |
paths.push_back(SkPath::RRect(SkRect::MakeWH(50, 50), 10, 10.00002f)); |
| 52 |
SkRRect oddRRect; |
| 53 |
oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16); |
| 54 |
paths.push_back(SkPath::RRect(oddRRect)); |
| 55 |
paths.push_back(SkPath::Rect(SkRect::MakeWH(50, 50))); |
| 56 |
paths.push_back(SkPath::Circle(25, 25, 25)); |
| 57 |
paths.push_back(SkPathBuilder().cubicTo(100, 50, 20, 100, 0, 0).detach()); |
| 58 |
paths.push_back(SkPath::Oval(SkRect::MakeWH(20, 60))); |
| 59 |
|
| 60 |
// star |
| 61 |
TArray<SkPath> concavePaths; |
| 62 |
concavePaths.push_back(SkPathBuilder() |
| 63 |
.moveTo(0.0f, -33.3333f) |
| 64 |
.lineTo(9.62f, -16.6667f) |
| 65 |
.lineTo(28.867f, -16.6667f) |
| 66 |
.lineTo(19.24f, 0.0f) |
| 67 |
.lineTo(28.867f, 16.6667f) |
| 68 |
.lineTo(9.62f, 16.6667f) |
| 69 |
.lineTo(0.0f, 33.3333f) |
| 70 |
.lineTo(-9.62f, 16.6667f) |
| 71 |
.lineTo(-28.867f, 16.6667f) |
| 72 |
.lineTo(-19.24f, 0.0f) |
| 73 |
.lineTo(-28.867f, -16.6667f) |
| 74 |
.lineTo(-9.62f, -16.6667f) |
| 75 |
.close() |
| 76 |
.detach()); |
| 77 |
|
| 78 |
// dumbbell |
| 79 |
concavePaths.push_back(SkPathBuilder() |
| 80 |
.moveTo(50, 0) |
| 81 |
.cubicTo(100, 25, 60, 50, 50, 0) |
| 82 |
.cubicTo(0, -25, 40, -50, 50, 0) |
| 83 |
.detach()); |
| 84 |
|
| 85 |
static constexpr SkScalar kPad = 15.f; |
| 86 |
static constexpr SkScalar kLightR = 100.f; |
| 87 |
static constexpr SkScalar kHeight = 50.f; |
| 88 |
|
| 89 |
// transform light position relative to canvas to handle tiling |
| 90 |
SkPoint lightXY = canvas->getTotalMatrix().mapPoint({250, 400}); |
| 91 |
SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 }; |
| 92 |
|
| 93 |
canvas->translate(3 * kPad, 3 * kPad); |
| 94 |
canvas->save(); |
| 95 |
SkScalar x = 0; |
| 96 |
SkScalar dy = 0; |
| 97 |
SkTDArray<SkMatrix> matrices; |
| 98 |
matrices.append()->reset(); |
| 99 |
matrices.append()->setRotate(33.f, 25.f, 25.f).postScale(1.2f, 0.8f, 25.f, 25.f); |
| 100 |
for (auto& m : matrices) { |
| 101 |
for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) { |
| 102 |
int pathCounter = 0; |
| 103 |
for (const auto& path : paths) { |
| 104 |
SkRect postMBounds = path.getBounds(); |
| 105 |
m.mapRect(&postMBounds); |
| 106 |
SkScalar w = postMBounds.width() + kHeight; |
| 107 |
SkScalar dx = w + kPad; |
| 108 |
if (x + dx > kW - 3 * kPad) { |
| 109 |
canvas->restore(); |
| 110 |
canvas->translate(0, dy); |
| 111 |
canvas->save(); |
| 112 |
x = 0; |
| 113 |
dy = 0; |
| 114 |
} |
| 115 |
|
| 116 |
canvas->save(); |
| 117 |
canvas->concat(m); |
| 118 |
|
| 119 |
// flip a couple of paths to test 180° rotation |
| 120 |
if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) { |
| 121 |
canvas->save(); |
| 122 |
canvas->rotate(180, 25, 25); |
| 123 |
} |
| 124 |
if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) { |
| 125 |
draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, |
| 126 |
true, flags); |
| 127 |
draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, |
| 128 |
false, flags); |
| 129 |
} else if (kGrayscale == mode) { |
| 130 |
SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0); |
| 131 |
SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0); |
| 132 |
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos, |
| 133 |
kLightR, ambientColor, spotColor, flags); |
| 134 |
} |
| 135 |
|
| 136 |
SkPaint paint; |
| 137 |
paint.setAntiAlias(true); |
| 138 |
if (kDebugColorNoOccluders == mode) { |
| 139 |
// Draw the path outline in green on top of the ambient and spot shadows. |
| 140 |
if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) { |
| 141 |
paint.setColor(SK_ColorCYAN); |
| 142 |
} else { |
| 143 |
paint.setColor(SK_ColorGREEN); |
| 144 |
} |
| 145 |
paint.setStyle(SkPaint::kStroke_Style); |
| 146 |
paint.setStrokeWidth(0); |
| 147 |
} else { |
| 148 |
paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE); |
| 149 |
if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) { |
| 150 |
paint.setAlphaf(0.5f); |
| 151 |
} |
| 152 |
paint.setStyle(SkPaint::kFill_Style); |
| 153 |
} |
| 154 |
canvas->drawPath(path, paint); |
| 155 |
if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) { |
| 156 |
canvas->restore(); |
| 157 |
} |
| 158 |
canvas->restore(); |
| 159 |
|
| 160 |
canvas->translate(dx, 0); |
| 161 |
x += dx; |
| 162 |
dy = std::max(dy, postMBounds.height() + kPad + kHeight); |
| 163 |
++pathCounter; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// concave paths |
| 169 |
canvas->restore(); |
| 170 |
canvas->translate(kPad, dy); |
| 171 |
canvas->save(); |
| 172 |
x = kPad; |
| 173 |
dy = 0; |
| 174 |
for (auto& m : matrices) { |
| 175 |
// for the concave paths we are not clipping, so transparent and opaque are the same |
| 176 |
for (const auto& path : concavePaths) { |
| 177 |
SkRect postMBounds = path.getBounds(); |
| 178 |
m.mapRect(&postMBounds); |
| 179 |
SkScalar w = postMBounds.width() + kHeight; |
| 180 |
SkScalar dx = w + kPad; |
| 181 |
|
| 182 |
canvas->save(); |
| 183 |
canvas->concat(m); |
| 184 |
|
| 185 |
if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) { |
| 186 |
draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, |
| 187 |
true, kNone_ShadowFlag); |
| 188 |
draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, |
| 189 |
false, kNone_ShadowFlag); |
| 190 |
} else if (kGrayscale == mode) { |
| 191 |
SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0); |
| 192 |
SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0); |
| 193 |
SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 194 |
kLightR, ambientColor, spotColor, kNone_ShadowFlag); |
| 195 |
} |
| 196 |
|
| 197 |
SkPaint paint; |
| 198 |
paint.setAntiAlias(true); |
| 199 |
if (kDebugColorNoOccluders == mode) { |
| 200 |
// Draw the path outline in green on top of the ambient and spot shadows. |
| 201 |
paint.setColor(SK_ColorGREEN); |
| 202 |
paint.setStyle(SkPaint::kStroke_Style); |
| 203 |
paint.setStrokeWidth(0); |
| 204 |
} else { |
| 205 |
paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE); |
| 206 |
paint.setStyle(SkPaint::kFill_Style); |
| 207 |
} |
| 208 |
canvas->drawPath(path, paint); |
| 209 |
canvas->restore(); |
| 210 |
|
| 211 |
canvas->translate(dx, 0); |
| 212 |
x += dx; |
| 213 |
dy = std::max(dy, postMBounds.height() + kPad + kHeight); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Show where the light is in x,y as a circle (specified in device space). |
| 218 |
SkMatrix invCanvasM = canvas->getTotalMatrix(); |
| 219 |
if (invCanvasM.invert(&invCanvasM)) { |
| 220 |
canvas->save(); |
| 221 |
canvas->concat(invCanvasM); |
| 222 |
SkPaint paint; |
| 223 |
paint.setColor(SK_ColorBLACK); |
| 224 |
paint.setAntiAlias(true); |
| 225 |
canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint); |
| 226 |
canvas->restore(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) { |
| 231 |
draw_paths(canvas, kDebugColorNoOccluders); |
| 232 |
} |
| 233 |
|
| 234 |
DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) { |
| 235 |
draw_paths(canvas, kDebugColorOccluders); |
| 236 |
} |
| 237 |
|
| 238 |
DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) { |
| 239 |
draw_paths(canvas, kGrayscale); |
| 240 |
} |
| 241 |
|
| 242 |
#include "include/effects/SkGradient.h" |
| 243 |
#include "src/core/SkColorFilterPriv.h" |
| 244 |
|
| 245 |
DEF_SIMPLE_GM(shadow_utils_gaussian_colorfilter, canvas, 512, 256) { |
| 246 |
const SkRect r = SkRect::MakeWH(256, 256); |
| 247 |
|
| 248 |
const SkColor4f colors[] = { {0,0,0,0}, {0,0,0,1} }; |
| 249 |
auto sh = SkShaders::RadialGradient(r.center(), r.width(), |
| 250 |
{{colors, {}, SkTileMode::kClamp}, {}}); |
| 251 |
|
| 252 |
SkPaint redPaint; |
| 253 |
redPaint.setColor(SK_ColorRED); |
| 254 |
|
| 255 |
SkPaint paint; |
| 256 |
paint.setShader(sh); |
| 257 |
canvas->drawRect(r, redPaint); |
| 258 |
canvas->drawRect(r, paint); |
| 259 |
|
| 260 |
canvas->translate(256, 0); |
| 261 |
paint.setColorFilter(SkColorFilterPriv::MakeGaussian()); |
| 262 |
canvas->drawRect(r, redPaint); |
| 263 |
canvas->drawRect(r, paint); |
| 264 |
} |
| 265 |
|
| 266 |
DEF_SIMPLE_GM(shadow_utils_directional, canvas, 256, 384) { |
| 267 |
static constexpr SkScalar kLightR = 1.f; |
| 268 |
static constexpr SkScalar kHeight = 12.f; |
| 269 |
|
| 270 |
SkPath rrect(SkPath::RRect(SkRect::MakeLTRB(-25, -25, 25, 25), 10, 10)); |
| 271 |
SkPoint3 lightPos = { -45, -45, 77.9422863406f }; |
| 272 |
|
| 273 |
SkColor ambientColor = SkColorSetARGB(0.02f * 255, 0, 0, 0); |
| 274 |
SkColor spotColor = SkColorSetARGB(0.35f * 255, 0, 0, 0); |
| 275 |
|
| 276 |
SkPaint paint; |
| 277 |
paint.setAntiAlias(true); |
| 278 |
paint.setColor(SK_ColorWHITE); |
| 279 |
paint.setStyle(SkPaint::kFill_Style); |
| 280 |
|
| 281 |
// translation |
| 282 |
canvas->save(); |
| 283 |
canvas->translate(35, 35); |
| 284 |
for (int i = 0; i < 3; ++i) { |
| 285 |
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 286 |
kLightR, ambientColor, spotColor, |
| 287 |
kDirectionalLight_ShadowFlag); |
| 288 |
canvas->drawPath(rrect, paint); |
| 289 |
canvas->translate(80, 0); |
| 290 |
} |
| 291 |
canvas->restore(); |
| 292 |
|
| 293 |
// rotation |
| 294 |
for (int i = 0; i < 3; ++i) { |
| 295 |
canvas->save(); |
| 296 |
canvas->translate(35 + 80*i, 105); |
| 297 |
canvas->rotate(20.f*(i + 1)); |
| 298 |
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 299 |
kLightR, ambientColor, spotColor, |
| 300 |
kDirectionalLight_ShadowFlag); |
| 301 |
|
| 302 |
canvas->drawPath(rrect, paint); |
| 303 |
canvas->restore(); |
| 304 |
} |
| 305 |
|
| 306 |
// scale |
| 307 |
for (int i = 0; i < 3; ++i) { |
| 308 |
canvas->save(); |
| 309 |
SkScalar scaleFactor = std::pow(2.0, -i); |
| 310 |
canvas->translate(35 + 80*i, 185); |
| 311 |
canvas->scale(scaleFactor, scaleFactor); |
| 312 |
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 313 |
kLightR, ambientColor, spotColor, |
| 314 |
kDirectionalLight_ShadowFlag); |
| 315 |
|
| 316 |
canvas->drawPath(rrect, paint); |
| 317 |
canvas->restore(); |
| 318 |
} |
| 319 |
|
| 320 |
// perspective |
| 321 |
for (int i = 0; i < 3; ++i) { |
| 322 |
canvas->save(); |
| 323 |
SkMatrix mat; |
| 324 |
mat.reset(); |
| 325 |
mat[SkMatrix::kMPersp1] = 0.005f; |
| 326 |
mat[SkMatrix::kMPersp2] = 1.005f; |
| 327 |
canvas->translate(35 + 80*i, 265); |
| 328 |
canvas->concat(mat); |
| 329 |
SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 330 |
kLightR, ambientColor, spotColor, |
| 331 |
kDirectionalLight_ShadowFlag); |
| 332 |
|
| 333 |
canvas->drawPath(rrect, paint); |
| 334 |
canvas->restore(); |
| 335 |
} |
| 336 |
|
| 337 |
} |