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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame^] | 11 | #include "SkResourceCache.h" |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 12 | #include "SkShadowUtils.h" |
| 13 | |
| 14 | void draw_shadow(SkCanvas* canvas, const SkPath& path, int height, SkColor color, SkPoint3 lightPos, |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame^] | 15 | SkScalar lightR, bool isAmbient, uint32_t flags, SkResourceCache* cache) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 16 | SkScalar ambientAlpha = isAmbient ? .5f : 0.f; |
| 17 | SkScalar spotAlpha = isAmbient ? 0.f : .5f; |
| 18 | SkShadowUtils::DrawShadow(canvas, path, height, lightPos, lightR, ambientAlpha, spotAlpha, |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame^] | 19 | color, flags, cache); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 20 | } |
| 21 | |
Brian Salomon | 8c88a37 | 2017-02-02 20:59:09 -0500 | [diff] [blame] | 22 | static constexpr int kW = 700; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 23 | static constexpr int kH = 800; |
| 24 | |
| 25 | DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) { |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 26 | // SkShadowUtils uses a cache of SkVertices meshes. The vertices are created in a local |
| 27 | // coordinate system and then translated when reused. The coordinate system depends on |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame^] | 28 | // parameters to the generating draw. If other threads are hitting the cache while this GM is |
| 29 | // running then we may have different cache behavior leading to slight rendering differences. |
| 30 | // To avoid that we use our own isolated cache rather than the global cache. |
| 31 | SkResourceCache cache(1 << 20); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 32 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 33 | SkTArray<SkPath> paths; |
| 34 | paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10); |
| 35 | SkRRect oddRRect; |
| 36 | oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16); |
| 37 | paths.push_back().addRRect(oddRRect); |
| 38 | paths.push_back().addRect(SkRect::MakeWH(50, 50)); |
| 39 | paths.push_back().addCircle(25, 25, 25); |
| 40 | paths.push_back().cubicTo(100, 50, 20, 100, 0, 0); |
| 41 | |
| 42 | static constexpr SkScalar kPad = 15.f; |
Brian Salomon | 8c88a37 | 2017-02-02 20:59:09 -0500 | [diff] [blame] | 43 | static constexpr SkPoint3 kLightPos = {250, 400, 500}; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 44 | static constexpr SkScalar kLightR = 100.f; |
| 45 | static constexpr SkScalar kHeight = 50.f; |
| 46 | canvas->translate(3 * kPad, 3 * kPad); |
| 47 | canvas->save(); |
| 48 | SkScalar x = 0; |
| 49 | SkScalar dy = 0; |
| 50 | SkTDArray<SkMatrix> matrices; |
| 51 | matrices.push()->reset(); |
| 52 | SkMatrix* m = matrices.push(); |
| 53 | m->setRotate(33.f, 25.f, 25.f); |
| 54 | m->postScale(1.2f, 0.8f, 25.f, 25.f); |
| 55 | for (auto& m : matrices) { |
| 56 | for (auto flags : {kNone_ShadowFlag, kTransparentOccluder_ShadowFlag}) { |
| 57 | for (const auto& path : paths) { |
| 58 | SkRect postMBounds = path.getBounds(); |
| 59 | m.mapRect(&postMBounds); |
| 60 | SkScalar w = postMBounds.width() + kHeight; |
| 61 | SkScalar dx = w + kPad; |
| 62 | if (x + dx > kW - 3 * kPad) { |
| 63 | canvas->restore(); |
| 64 | canvas->translate(0, dy); |
| 65 | canvas->save(); |
| 66 | x = 0; |
| 67 | dy = 0; |
| 68 | } |
| 69 | |
| 70 | canvas->save(); |
| 71 | canvas->concat(m); |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame^] | 72 | draw_shadow(canvas, path, kHeight, SK_ColorRED, kLightPos, kLightR, true, flags, |
| 73 | &cache); |
| 74 | draw_shadow(canvas, path, kHeight, SK_ColorBLUE, kLightPos, kLightR, false, flags, |
| 75 | &cache); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 76 | |
| 77 | // Draw the path outline in green on top of the ambient and spot shadows. |
| 78 | SkPaint paint; |
| 79 | paint.setColor(SK_ColorGREEN); |
| 80 | paint.setAntiAlias(true); |
| 81 | paint.setStyle(SkPaint::kStroke_Style); |
| 82 | paint.setStrokeWidth(0); |
| 83 | canvas->drawPath(path, paint); |
| 84 | canvas->restore(); |
| 85 | |
| 86 | canvas->translate(dx, 0); |
| 87 | x += dx; |
| 88 | dy = SkTMax(dy, postMBounds.height() + kPad + kHeight); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | // Show where the light is in x,y as a circle (specified in device space). |
| 93 | SkMatrix invCanvasM = canvas->getTotalMatrix(); |
| 94 | if (invCanvasM.invert(&invCanvasM)) { |
| 95 | canvas->save(); |
| 96 | canvas->concat(invCanvasM); |
| 97 | SkPaint paint; |
| 98 | paint.setColor(SK_ColorBLACK); |
| 99 | paint.setAntiAlias(true); |
| 100 | canvas->drawCircle(kLightPos.fX, kLightPos.fY, kLightR / 10.f, paint); |
| 101 | canvas->restore(); |
| 102 | } |
| 103 | } |