Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkMatrix.h" |
| 12 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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/SkShadowFlags.h" |
| 21 | #include "include/private/SkTArray.h" |
| 22 | #include "include/private/SkTDArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/utils/SkShadowUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | |
| 25 | #include <initializer_list> |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 26 | |
Jim Van Verth | a556684 | 2018-02-22 10:58:34 -0500 | [diff] [blame] | 27 | void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color, |
| 28 | SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 29 | SkScalar ambientAlpha = isAmbient ? .5f : 0.f; |
| 30 | SkScalar spotAlpha = isAmbient ? 0.f : .5f; |
Jim Van Verth | a556684 | 2018-02-22 10:58:34 -0500 | [diff] [blame] | 31 | SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color), |
| 32 | SkColorGetG(color), SkColorGetB(color)); |
| 33 | SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color), |
| 34 | SkColorGetG(color), SkColorGetB(color)); |
| 35 | SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR, |
| 36 | ambientColor, spotColor, flags); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 39 | static constexpr int kW = 800; |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 40 | static constexpr int kH = 960; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 41 | |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 42 | enum ShadowMode { |
| 43 | kDebugColorNoOccluders, |
| 44 | kDebugColorOccluders, |
| 45 | kGrayscale |
| 46 | }; |
| 47 | |
| 48 | void draw_paths(SkCanvas* canvas, ShadowMode mode) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 49 | SkTArray<SkPath> paths; |
| 50 | paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10); |
| 51 | SkRRect oddRRect; |
| 52 | oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16); |
| 53 | paths.push_back().addRRect(oddRRect); |
| 54 | paths.push_back().addRect(SkRect::MakeWH(50, 50)); |
| 55 | paths.push_back().addCircle(25, 25, 25); |
| 56 | paths.push_back().cubicTo(100, 50, 20, 100, 0, 0); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 57 | paths.push_back().addOval(SkRect::MakeWH(20, 60)); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 58 | |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 59 | // star |
| 60 | SkTArray<SkPath> concavePaths; |
| 61 | concavePaths.push_back().moveTo(0.0f, -33.3333f); |
| 62 | concavePaths.back().lineTo(9.62f, -16.6667f); |
| 63 | concavePaths.back().lineTo(28.867f, -16.6667f); |
| 64 | concavePaths.back().lineTo(19.24f, 0.0f); |
| 65 | concavePaths.back().lineTo(28.867f, 16.6667f); |
| 66 | concavePaths.back().lineTo(9.62f, 16.6667f); |
| 67 | concavePaths.back().lineTo(0.0f, 33.3333f); |
| 68 | concavePaths.back().lineTo(-9.62f, 16.6667f); |
| 69 | concavePaths.back().lineTo(-28.867f, 16.6667f); |
| 70 | concavePaths.back().lineTo(-19.24f, 0.0f); |
| 71 | concavePaths.back().lineTo(-28.867f, -16.6667f); |
| 72 | concavePaths.back().lineTo(-9.62f, -16.6667f); |
| 73 | concavePaths.back().close(); |
| 74 | |
| 75 | // dumbbell |
| 76 | concavePaths.push_back().moveTo(50, 0); |
| 77 | concavePaths.back().cubicTo(100, 25, 60, 50, 50, 0); |
| 78 | concavePaths.back().cubicTo(0, -25, 40, -50, 50, 0); |
| 79 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 80 | static constexpr SkScalar kPad = 15.f; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 81 | static constexpr SkScalar kLightR = 100.f; |
| 82 | static constexpr SkScalar kHeight = 50.f; |
Jim Van Verth | 9392f569c | 2017-05-16 15:01:43 -0400 | [diff] [blame] | 83 | |
| 84 | // transform light position relative to canvas to handle tiling |
| 85 | SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400); |
| 86 | SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 }; |
| 87 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 88 | canvas->translate(3 * kPad, 3 * kPad); |
| 89 | canvas->save(); |
| 90 | SkScalar x = 0; |
| 91 | SkScalar dy = 0; |
| 92 | SkTDArray<SkMatrix> matrices; |
| 93 | matrices.push()->reset(); |
| 94 | SkMatrix* m = matrices.push(); |
| 95 | m->setRotate(33.f, 25.f, 25.f); |
| 96 | m->postScale(1.2f, 0.8f, 25.f, 25.f); |
| 97 | for (auto& m : matrices) { |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 98 | for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) { |
Jim Van Verth | eda9a55 | 2019-07-24 14:46:53 -0400 | [diff] [blame] | 99 | int pathCounter = 0; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 100 | for (const auto& path : paths) { |
| 101 | SkRect postMBounds = path.getBounds(); |
| 102 | m.mapRect(&postMBounds); |
| 103 | SkScalar w = postMBounds.width() + kHeight; |
| 104 | SkScalar dx = w + kPad; |
| 105 | if (x + dx > kW - 3 * kPad) { |
| 106 | canvas->restore(); |
| 107 | canvas->translate(0, dy); |
| 108 | canvas->save(); |
| 109 | x = 0; |
| 110 | dy = 0; |
| 111 | } |
| 112 | |
| 113 | canvas->save(); |
| 114 | canvas->concat(m); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 115 | |
Jim Van Verth | eda9a55 | 2019-07-24 14:46:53 -0400 | [diff] [blame] | 116 | // flip a couple of paths to test 180° rotation |
| 117 | if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) { |
| 118 | canvas->save(); |
| 119 | canvas->rotate(180, 25, 25); |
| 120 | } |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 121 | if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) { |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 122 | draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, |
| 123 | true, flags); |
| 124 | draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, |
| 125 | false, flags); |
| 126 | } else if (kGrayscale == mode) { |
Jim Van Verth | a556684 | 2018-02-22 10:58:34 -0500 | [diff] [blame] | 127 | SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0); |
| 128 | SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0); |
| 129 | SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos, |
| 130 | kLightR, ambientColor, spotColor, flags); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 133 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 134 | paint.setAntiAlias(true); |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 135 | if (kDebugColorNoOccluders == mode) { |
| 136 | // Draw the path outline in green on top of the ambient and spot shadows. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 137 | if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) { |
| 138 | paint.setColor(SK_ColorCYAN); |
| 139 | } else { |
| 140 | paint.setColor(SK_ColorGREEN); |
| 141 | } |
| 142 | paint.setStyle(SkPaint::kStroke_Style); |
| 143 | paint.setStrokeWidth(0); |
| 144 | } else { |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 145 | paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE); |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 146 | if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 147 | paint.setAlphaf(0.5f); |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 148 | } |
| 149 | paint.setStyle(SkPaint::kFill_Style); |
| 150 | } |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 151 | canvas->drawPath(path, paint); |
Jim Van Verth | eda9a55 | 2019-07-24 14:46:53 -0400 | [diff] [blame] | 152 | if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) { |
| 153 | canvas->restore(); |
| 154 | } |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 155 | canvas->restore(); |
| 156 | |
| 157 | canvas->translate(dx, 0); |
| 158 | x += dx; |
| 159 | dy = SkTMax(dy, postMBounds.height() + kPad + kHeight); |
Jim Van Verth | eda9a55 | 2019-07-24 14:46:53 -0400 | [diff] [blame] | 160 | ++pathCounter; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 164 | |
| 165 | // concave paths |
| 166 | canvas->restore(); |
| 167 | canvas->translate(kPad, dy); |
| 168 | canvas->save(); |
| 169 | x = kPad; |
| 170 | dy = 0; |
| 171 | for (auto& m : matrices) { |
| 172 | // for the concave paths we are not clipping, so transparent and opaque are the same |
| 173 | for (const auto& path : concavePaths) { |
| 174 | SkRect postMBounds = path.getBounds(); |
| 175 | m.mapRect(&postMBounds); |
| 176 | SkScalar w = postMBounds.width() + kHeight; |
| 177 | SkScalar dx = w + kPad; |
| 178 | |
| 179 | canvas->save(); |
| 180 | canvas->concat(m); |
| 181 | |
| 182 | if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) { |
| 183 | draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, |
| 184 | true, kNone_ShadowFlag); |
| 185 | draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, |
| 186 | false, kNone_ShadowFlag); |
| 187 | } else if (kGrayscale == mode) { |
| 188 | SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0); |
| 189 | SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0); |
| 190 | SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos, |
| 191 | kLightR, ambientColor, spotColor, kNone_ShadowFlag); |
| 192 | } |
| 193 | |
| 194 | SkPaint paint; |
| 195 | paint.setAntiAlias(true); |
| 196 | if (kDebugColorNoOccluders == mode) { |
| 197 | // Draw the path outline in green on top of the ambient and spot shadows. |
| 198 | paint.setColor(SK_ColorGREEN); |
| 199 | paint.setStyle(SkPaint::kStroke_Style); |
| 200 | paint.setStrokeWidth(0); |
| 201 | } else { |
| 202 | paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE); |
| 203 | paint.setStyle(SkPaint::kFill_Style); |
| 204 | } |
| 205 | canvas->drawPath(path, paint); |
| 206 | canvas->restore(); |
| 207 | |
| 208 | canvas->translate(dx, 0); |
| 209 | x += dx; |
| 210 | dy = SkTMax(dy, postMBounds.height() + kPad + kHeight); |
| 211 | } |
| 212 | } |
| 213 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 214 | // Show where the light is in x,y as a circle (specified in device space). |
| 215 | SkMatrix invCanvasM = canvas->getTotalMatrix(); |
| 216 | if (invCanvasM.invert(&invCanvasM)) { |
| 217 | canvas->save(); |
| 218 | canvas->concat(invCanvasM); |
| 219 | SkPaint paint; |
| 220 | paint.setColor(SK_ColorBLACK); |
| 221 | paint.setAntiAlias(true); |
Jim Van Verth | 9392f569c | 2017-05-16 15:01:43 -0400 | [diff] [blame] | 222 | canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 223 | canvas->restore(); |
| 224 | } |
| 225 | } |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 226 | |
| 227 | DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) { |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 228 | draw_paths(canvas, kDebugColorNoOccluders); |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) { |
Jim Van Verth | 744cbb3 | 2017-12-19 13:02:38 -0500 | [diff] [blame] | 232 | draw_paths(canvas, kDebugColorOccluders); |
| 233 | } |
| 234 | |
| 235 | DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) { |
| 236 | draw_paths(canvas, kGrayscale); |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 237 | } |