Add fixes & test for isConfigTexturable and isConfigRenderable
[platform/upstream/libSkiaSharp.git] / gm / shadowutils.cpp
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"
11 #include "SkResourceCache.h"
12 #include "SkShadowUtils.h"
13
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,
19                               color, flags);
20 }
21
22 static constexpr int kW = 800;
23 static constexpr int kH = 800;
24
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);
30     SkRRect oddRRect;
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);
42
43     static constexpr SkScalar kPad = 15.f;
44     static constexpr SkScalar kLightR = 100.f;
45     static constexpr SkScalar kHeight = 50.f;
46
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 };
50
51     canvas->translate(3 * kPad, 3 * kPad);
52     canvas->save();
53     SkScalar x = 0;
54     SkScalar dy = 0;
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) {
68                     canvas->restore();
69                     canvas->translate(0, dy);
70                     canvas->save();
71                     x = 0;
72                     dy = 0;
73                 }
74
75                 canvas->save();
76                 canvas->concat(m);
77                 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR, true, flags);
78                 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR, false, flags);
79
80                 // Draw the path outline in green on top of the ambient and spot shadows.
81                 SkPaint paint;
82                 paint.setAntiAlias(true);
83                 if (hideOccluders) {
84                     if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
85                         paint.setColor(SK_ColorCYAN);
86                     } else {
87                         paint.setColor(SK_ColorGREEN);
88                     }
89                     paint.setStyle(SkPaint::kStroke_Style);
90                     paint.setStrokeWidth(0);
91                 } else {
92                     paint.setColor(SK_ColorLTGRAY);
93                     if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
94                         paint.setAlpha(128);
95                     }
96                     paint.setStyle(SkPaint::kFill_Style);
97                 }
98                 canvas->drawPath(path, paint);
99                 canvas->restore();
100
101                 canvas->translate(dx, 0);
102                 x += dx;
103                 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
104             }
105         }
106     }
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)) {
110         canvas->save();
111         canvas->concat(invCanvasM);
112         SkPaint paint;
113         paint.setColor(SK_ColorBLACK);
114         paint.setAntiAlias(true);
115         canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
116         canvas->restore();
117     }
118 }
119
120 DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
121     draw_paths(canvas, true);
122 }
123
124 DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
125     draw_paths(canvas, false);
126 }