blob: d9cdfd57683a8f40aabead51f75d95f5941b345b [file] [log] [blame]
Brian Salomon0bd699e2017-02-01 12:23:25 -05001/*
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.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
Brian Salomon804e0912017-02-23 09:34:03 -050011#include "SkResourceCache.h"
Brian Salomon0bd699e2017-02-01 12:23:25 -050012#include "SkShadowUtils.h"
13
14void draw_shadow(SkCanvas* canvas, const SkPath& path, int height, SkColor color, SkPoint3 lightPos,
Jim Van Verth37c5a962017-05-10 14:13:24 -040015 SkScalar lightR, bool isAmbient, uint32_t flags) {
Brian Salomon0bd699e2017-02-01 12:23:25 -050016 SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
17 SkScalar spotAlpha = isAmbient ? 0.f : .5f;
18 SkShadowUtils::DrawShadow(canvas, path, height, lightPos, lightR, ambientAlpha, spotAlpha,
Jim Van Verth37c5a962017-05-10 14:13:24 -040019 color, flags);
Brian Salomon0bd699e2017-02-01 12:23:25 -050020}
21
Brian Salomonab664fa2017-03-24 16:07:20 +000022static constexpr int kW = 800;
Brian Salomon0bd699e2017-02-01 12:23:25 -050023static constexpr int kH = 800;
24
Jim Van Verth744cbb32017-12-19 13:02:38 -050025enum ShadowMode {
26 kDebugColorNoOccluders,
27 kDebugColorOccluders,
28 kGrayscale
29};
30
31void draw_paths(SkCanvas* canvas, ShadowMode mode) {
Brian Salomon0bd699e2017-02-01 12:23:25 -050032 SkTArray<SkPath> paths;
33 paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
34 SkRRect oddRRect;
35 oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
36 paths.push_back().addRRect(oddRRect);
37 paths.push_back().addRect(SkRect::MakeWH(50, 50));
38 paths.push_back().addCircle(25, 25, 25);
39 paths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
Brian Salomonab664fa2017-03-24 16:07:20 +000040 paths.push_back().addOval(SkRect::MakeWH(20, 60));
Brian Salomon0bd699e2017-02-01 12:23:25 -050041
42 static constexpr SkScalar kPad = 15.f;
Brian Salomon0bd699e2017-02-01 12:23:25 -050043 static constexpr SkScalar kLightR = 100.f;
44 static constexpr SkScalar kHeight = 50.f;
Jim Van Verth9392f569c2017-05-16 15:01:43 -040045
46 // transform light position relative to canvas to handle tiling
47 SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
48 SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
49
Brian Salomon0bd699e2017-02-01 12:23:25 -050050 canvas->translate(3 * kPad, 3 * kPad);
51 canvas->save();
52 SkScalar x = 0;
53 SkScalar dy = 0;
54 SkTDArray<SkMatrix> matrices;
55 matrices.push()->reset();
56 SkMatrix* m = matrices.push();
57 m->setRotate(33.f, 25.f, 25.f);
58 m->postScale(1.2f, 0.8f, 25.f, 25.f);
59 for (auto& m : matrices) {
Jim Van Verth744cbb32017-12-19 13:02:38 -050060 for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
Brian Salomon0bd699e2017-02-01 12:23:25 -050061 for (const auto& path : paths) {
62 SkRect postMBounds = path.getBounds();
63 m.mapRect(&postMBounds);
64 SkScalar w = postMBounds.width() + kHeight;
65 SkScalar dx = w + kPad;
66 if (x + dx > kW - 3 * kPad) {
67 canvas->restore();
68 canvas->translate(0, dy);
69 canvas->save();
70 x = 0;
71 dy = 0;
72 }
73
74 canvas->save();
75 canvas->concat(m);
Brian Salomon0bd699e2017-02-01 12:23:25 -050076
Jim Van Verth744cbb32017-12-19 13:02:38 -050077 if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
78 flags |= SkShadowFlags::kDisableTonalColor_ShadowFlag;
79 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
80 true, flags);
81 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
82 false, flags);
83 } else if (kGrayscale == mode) {
84 SkShadowUtils::DrawShadow(canvas, path, kHeight, lightPos, kLightR,
85 0.1f, 0.25f, SK_ColorBLACK, flags);
86 }
87
Brian Salomon0bd699e2017-02-01 12:23:25 -050088 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -050089 paint.setAntiAlias(true);
Jim Van Verth744cbb32017-12-19 13:02:38 -050090 if (kDebugColorNoOccluders == mode) {
91 // Draw the path outline in green on top of the ambient and spot shadows.
Jim Van Verth78c8f302017-05-15 10:44:22 -040092 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
93 paint.setColor(SK_ColorCYAN);
94 } else {
95 paint.setColor(SK_ColorGREEN);
96 }
97 paint.setStyle(SkPaint::kStroke_Style);
98 paint.setStrokeWidth(0);
99 } else {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500100 paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400101 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
102 paint.setAlpha(128);
103 }
104 paint.setStyle(SkPaint::kFill_Style);
105 }
Brian Salomon0bd699e2017-02-01 12:23:25 -0500106 canvas->drawPath(path, paint);
107 canvas->restore();
108
109 canvas->translate(dx, 0);
110 x += dx;
111 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
112 }
113 }
114 }
115 // Show where the light is in x,y as a circle (specified in device space).
116 SkMatrix invCanvasM = canvas->getTotalMatrix();
117 if (invCanvasM.invert(&invCanvasM)) {
118 canvas->save();
119 canvas->concat(invCanvasM);
120 SkPaint paint;
121 paint.setColor(SK_ColorBLACK);
122 paint.setAntiAlias(true);
Jim Van Verth9392f569c2017-05-16 15:01:43 -0400123 canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
Brian Salomon0bd699e2017-02-01 12:23:25 -0500124 canvas->restore();
125 }
126}
Jim Van Verth78c8f302017-05-15 10:44:22 -0400127
128DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500129 draw_paths(canvas, kDebugColorNoOccluders);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400130}
131
132DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500133 draw_paths(canvas, kDebugColorOccluders);
134}
135
136DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
137 draw_paths(canvas, kGrayscale);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400138}