2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkResourceCache.h"
12 #include "SkShadowUtils.h"
14 void draw_shadow(SkCanvas* canvas, const SkPath& path, int height, SkColor color, SkPoint3 lightPos,
15 SkScalar lightR, bool isAmbient, uint32_t flags) {
16 SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
17 SkScalar spotAlpha = isAmbient ? 0.f : .5f;
18 SkShadowUtils::DrawShadow(canvas, path, height, lightPos, lightR, ambientAlpha, spotAlpha,
22 static constexpr int kW = 800;
23 static constexpr int kH = 800;
25 void draw_paths(SkCanvas* canvas, bool hideOccluders) {
26 SkTArray<SkPath> paths;
27 paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
28 // disable cache for now until we can figure out how to make it more stable
29 paths.back().setIsVolatile(true);
31 oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
32 paths.push_back().addRRect(oddRRect);
33 paths.back().setIsVolatile(true);
34 paths.push_back().addRect(SkRect::MakeWH(50, 50));
35 paths.back().setIsVolatile(true);
36 paths.push_back().addCircle(25, 25, 25);
37 paths.back().setIsVolatile(true);
38 paths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
39 paths.back().setIsVolatile(true);
40 paths.push_back().addOval(SkRect::MakeWH(20, 60));
41 paths.back().setIsVolatile(true);
43 static constexpr SkScalar kPad = 15.f;
44 static constexpr SkScalar kLightR = 100.f;
45 static constexpr SkScalar kHeight = 50.f;
47 // transform light position relative to canvas to handle tiling
48 SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
49 SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
51 canvas->translate(3 * kPad, 3 * kPad);
55 SkTDArray<SkMatrix> matrices;
56 matrices.push()->reset();
57 SkMatrix* m = matrices.push();
58 m->setRotate(33.f, 25.f, 25.f);
59 m->postScale(1.2f, 0.8f, 25.f, 25.f);
60 for (auto& m : matrices) {
61 for (auto flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
62 for (const auto& path : paths) {
63 SkRect postMBounds = path.getBounds();
64 m.mapRect(&postMBounds);
65 SkScalar w = postMBounds.width() + kHeight;
66 SkScalar dx = w + kPad;
67 if (x + dx > kW - 3 * kPad) {
69 canvas->translate(0, dy);
77 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, true, flags);
78 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, false, flags);
80 // Draw the path outline in green on top of the ambient and spot shadows.
82 paint.setAntiAlias(true);
84 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
85 paint.setColor(SK_ColorCYAN);
87 paint.setColor(SK_ColorGREEN);
89 paint.setStyle(SkPaint::kStroke_Style);
90 paint.setStrokeWidth(0);
92 paint.setColor(SK_ColorLTGRAY);
93 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
96 paint.setStyle(SkPaint::kFill_Style);
98 canvas->drawPath(path, paint);
101 canvas->translate(dx, 0);
103 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
107 // Show where the light is in x,y as a circle (specified in device space).
108 SkMatrix invCanvasM = canvas->getTotalMatrix();
109 if (invCanvasM.invert(&invCanvasM)) {
111 canvas->concat(invCanvasM);
113 paint.setColor(SK_ColorBLACK);
114 paint.setAntiAlias(true);
115 canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
120 DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
121 draw_paths(canvas, true);
124 DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
125 draw_paths(canvas, false);