1 /*
2 * Copyright 2015 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFilterQuality.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPath.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypes.h"
28 #include "include/effects/SkGradientShader.h"
29 #include "tools/ToolUtils.h"
30
make_image(SkCanvas * origCanvas,int w,int h)31 static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) {
32 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
33 auto surface(ToolUtils::makeSurface(origCanvas, info));
34 SkCanvas* canvas = surface->getCanvas();
35
36 ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10);
37 return surface->makeImageSnapshot();
38 }
39
40 namespace skiagm {
41
42 class PerspShadersGM : public GM {
43 public:
PerspShadersGM(bool doAA)44 PerspShadersGM(bool doAA) : fDoAA(doAA) { }
45
46 protected:
onShortName()47 SkString onShortName() override {
48 SkString name;
49 name.printf("persp_shaders_%s",
50 fDoAA ? "aa" : "bw");
51 return name;
52 }
53
onISize()54 SkISize onISize() override {
55 return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows);
56 }
57
onOnceBeforeDraw()58 void onOnceBeforeDraw() override {
59 fBitmap = ToolUtils::create_checkerboard_bitmap(
60 kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10);
61
62 fBitmapShader = fBitmap.makeShader();
63 SkPoint pts1[] = {
64 { 0, 0 },
65 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
66 };
67 SkPoint pts2[] = {
68 { 0, 0 },
69 { 0, SkIntToScalar(kCellSize) }
70 };
71 constexpr SkColor colors[] = {
72 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
73 };
74 constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
75
76 fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors),
77 SkTileMode::kClamp);
78 fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors),
79 SkTileMode::kClamp);
80
81 fPerspMatrix.reset();
82 fPerspMatrix.setPerspY(SK_Scalar1 / 50);
83
84 fPath.moveTo(0, 0);
85 fPath.lineTo(0, SkIntToScalar(kCellSize));
86 fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f);
87 fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
88 fPath.lineTo(SkIntToScalar(kCellSize), 0);
89 fPath.close();
90 }
91
drawRow(SkCanvas * canvas,SkFilterQuality filterQ)92 void drawRow(SkCanvas* canvas, SkFilterQuality filterQ) {
93 SkPaint filterPaint;
94 filterPaint.setFilterQuality(filterQ);
95 filterPaint.setAntiAlias(fDoAA);
96
97 SkPaint pathPaint;
98 pathPaint.setShader(fBitmapShader);
99 pathPaint.setFilterQuality(filterQ);
100 pathPaint.setAntiAlias(fDoAA);
101
102 SkPaint gradPaint1;
103 gradPaint1.setShader(fLinearGrad1);
104 gradPaint1.setAntiAlias(fDoAA);
105 SkPaint gradPaint2;
106 gradPaint2.setShader(fLinearGrad2);
107 gradPaint2.setAntiAlias(fDoAA);
108
109 SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
110
111 canvas->save();
112
113 canvas->save();
114 canvas->concat(fPerspMatrix);
115 canvas->drawBitmapRect(fBitmap, r, &filterPaint);
116 canvas->restore();
117
118 canvas->translate(SkIntToScalar(kCellSize), 0);
119 canvas->save();
120 canvas->concat(fPerspMatrix);
121 canvas->drawImage(fImage.get(), 0, 0, &filterPaint);
122 canvas->restore();
123
124 canvas->translate(SkIntToScalar(kCellSize), 0);
125 canvas->save();
126 canvas->concat(fPerspMatrix);
127 canvas->drawRect(r, pathPaint);
128 canvas->restore();
129
130 canvas->translate(SkIntToScalar(kCellSize), 0);
131 canvas->save();
132 canvas->concat(fPerspMatrix);
133 canvas->drawPath(fPath, pathPaint);
134 canvas->restore();
135
136 canvas->translate(SkIntToScalar(kCellSize), 0);
137 canvas->save();
138 canvas->concat(fPerspMatrix);
139 canvas->drawRect(r, gradPaint1);
140 canvas->restore();
141
142 canvas->translate(SkIntToScalar(kCellSize), 0);
143 canvas->save();
144 canvas->concat(fPerspMatrix);
145 canvas->drawPath(fPath, gradPaint2);
146 canvas->restore();
147
148 canvas->restore();
149 }
150
onDraw(SkCanvas * canvas)151 void onDraw(SkCanvas* canvas) override {
152 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
153 fImage = make_image(canvas, kCellSize, kCellSize);
154 }
155
156 this->drawRow(canvas, kNone_SkFilterQuality);
157 canvas->translate(0, SkIntToScalar(kCellSize));
158 this->drawRow(canvas, kLow_SkFilterQuality);
159 canvas->translate(0, SkIntToScalar(kCellSize));
160 this->drawRow(canvas, kMedium_SkFilterQuality);
161 canvas->translate(0, SkIntToScalar(kCellSize));
162 this->drawRow(canvas, kHigh_SkFilterQuality);
163 canvas->translate(0, SkIntToScalar(kCellSize));
164 }
165 private:
166 static constexpr int kCellSize = 50;
167 static constexpr int kNumRows = 4;
168 static constexpr int kNumCols = 6;
169
170 bool fDoAA;
171 SkPath fPath;
172 sk_sp<SkShader> fBitmapShader;
173 sk_sp<SkShader> fLinearGrad1;
174 sk_sp<SkShader> fLinearGrad2;
175 SkMatrix fPerspMatrix;
176 sk_sp<SkImage> fImage;
177 SkBitmap fBitmap;
178
179 typedef GM INHERITED;
180 };
181 DEF_GM(return new PerspShadersGM(true);)
182 DEF_GM(return new PerspShadersGM(false);)
183 }
184
185 //////////////////////////////////////////////////////////////////////////////
186
187 #include "tools/Resources.h"
188
make_path()189 static SkPath make_path() {
190 SkRandom rand;
191 auto rand_pt = [&rand]() {
192 auto x = rand.nextF();
193 auto y = rand.nextF();
194 return SkPoint{x * 400, y * 400};
195 };
196
197 SkPath path;
198 for (int i = 0; i < 4; ++i) {
199 SkPoint pts[6];
200 for (auto& p : pts) {
201 p = rand_pt();
202 }
203 path.moveTo(pts[0]).quadTo(pts[1], pts[2]).quadTo(pts[3], pts[4]).lineTo(pts[5]);
204 }
205 return path;
206 }
207
208 DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) {
209 SkPath path = make_path();
210 auto shader = GetResourceAsImage("images/mandrill_128.png")
211 ->makeShader(SkMatrix::MakeScale(3, 3));
212
213 SkPaint paint;
214 paint.setColor({0.75, 0.75, 0.75, 1});
215 canvas->drawPath(path, paint);
216
217 // This is a crazy perspective matrix, derived from halfplanes3, to draw a shape where
218 // part of it is "behind" the viewer, hence showing the need for "half-plane" clipping
219 // when in perspective.
220 SkMatrix mx;
221 const SkScalar array[] = {
222 -1.7866f, 1.3357f, 273.0295f,
223 -1.0820f, 1.3186f, 135.5196f,
224 -0.0047f, -0.0015f, 2.1485f,
225 };
226 mx.set9(array);
227
228 paint.setShader(shader);
229 canvas->concat(mx);
230 canvas->drawPath(path, paint);
231 }
232