blob: 350a546f0a18b17624bc452e9aaac4e492c41fef [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,
Brian Salomon804e0912017-02-23 09:34:03 -050015 SkScalar lightR, bool isAmbient, uint32_t flags, SkResourceCache* cache) {
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,
Brian Salomon804e0912017-02-23 09:34:03 -050019 color, flags, cache);
Brian Salomon0bd699e2017-02-01 12:23:25 -050020}
21
Brian Salomon8c88a372017-02-02 20:59:09 -050022static constexpr int kW = 700;
Brian Salomon0bd699e2017-02-01 12:23:25 -050023static constexpr int kH = 800;
24
25DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
Brian Salomonbc9956d2017-02-22 13:49:09 -050026 // 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 Salomon804e0912017-02-23 09:34:03 -050028 // 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 Salomonbc9956d2017-02-22 13:49:09 -050032
Brian Salomon0bd699e2017-02-01 12:23:25 -050033 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 Salomon8c88a372017-02-02 20:59:09 -050043 static constexpr SkPoint3 kLightPos = {250, 400, 500};
Brian Salomon0bd699e2017-02-01 12:23:25 -050044 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 Salomon804e0912017-02-23 09:34:03 -050072 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 Salomon0bd699e2017-02-01 12:23:25 -050076
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}