blob: cd57c3687f414802a4a1750a0f80ec61a69c12de [file] [log] [blame]
robertphillipsfac9ceb2015-09-03 08:32:08 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/effects/SkGradientShader.h"
29#include "tools/ToolUtils.h"
robertphillipsfac9ceb2015-09-03 08:32:08 -070030
reed9ce9d672016-03-17 10:51:11 -070031static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) {
robertphillipsfac9ceb2015-09-03 08:32:08 -070032 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
Mike Kleinea3f0142019-03-20 11:12:10 -050033 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillipsfac9ceb2015-09-03 08:32:08 -070034 SkCanvas* canvas = surface->getCanvas();
35
Mike Kleinea3f0142019-03-20 11:12:10 -050036 ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10);
reed9ce9d672016-03-17 10:51:11 -070037 return surface->makeImageSnapshot();
robertphillipsfac9ceb2015-09-03 08:32:08 -070038}
39
robertphillipsfac9ceb2015-09-03 08:32:08 -070040namespace skiagm {
41
42class PerspShadersGM : public GM {
43public:
44 PerspShadersGM(bool doAA) : fDoAA(doAA) { }
45
46protected:
47 SkString onShortName() override {
48 SkString name;
49 name.printf("persp_shaders_%s",
50 fDoAA ? "aa" : "bw");
51 return name;
52 }
53
54 SkISize onISize() override {
55 return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows);
56 }
57
58 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050059 fBitmap = ToolUtils::create_checkerboard_bitmap(
60 kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10);
Michael Ludwigb0cee9b2020-03-12 10:52:15 -040061 fBitmap.setImmutable();
robertphillipsfac9ceb2015-09-03 08:32:08 -070062
Mike Reed50acf8f2019-04-08 13:20:23 -040063 fBitmapShader = fBitmap.makeShader();
robertphillipsfac9ceb2015-09-03 08:32:08 -070064 SkPoint pts1[] = {
65 { 0, 0 },
66 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
67 };
68 SkPoint pts2[] = {
69 { 0, 0 },
70 { 0, SkIntToScalar(kCellSize) }
71 };
mtkleindbfd7ab2016-09-01 11:24:54 -070072 constexpr SkColor colors[] = {
robertphillipsfac9ceb2015-09-03 08:32:08 -070073 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
74 };
mtkleindbfd7ab2016-09-01 11:24:54 -070075 constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
robertphillipsfac9ceb2015-09-03 08:32:08 -070076
reed1a9b9642016-03-13 14:13:58 -070077 fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040078 SkTileMode::kClamp);
reed1a9b9642016-03-13 14:13:58 -070079 fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040080 SkTileMode::kClamp);
robertphillipsfac9ceb2015-09-03 08:32:08 -070081
82 fPerspMatrix.reset();
83 fPerspMatrix.setPerspY(SK_Scalar1 / 50);
84
85 fPath.moveTo(0, 0);
86 fPath.lineTo(0, SkIntToScalar(kCellSize));
87 fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f);
88 fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
89 fPath.lineTo(SkIntToScalar(kCellSize), 0);
90 fPath.close();
91 }
92
93 void drawRow(SkCanvas* canvas, SkFilterQuality filterQ) {
94 SkPaint filterPaint;
95 filterPaint.setFilterQuality(filterQ);
96 filterPaint.setAntiAlias(fDoAA);
97
98 SkPaint pathPaint;
99 pathPaint.setShader(fBitmapShader);
100 pathPaint.setFilterQuality(filterQ);
101 pathPaint.setAntiAlias(fDoAA);
102
103 SkPaint gradPaint1;
104 gradPaint1.setShader(fLinearGrad1);
105 gradPaint1.setAntiAlias(fDoAA);
106 SkPaint gradPaint2;
107 gradPaint2.setShader(fLinearGrad2);
108 gradPaint2.setAntiAlias(fDoAA);
109
110 SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
111
112 canvas->save();
113
114 canvas->save();
115 canvas->concat(fPerspMatrix);
116 canvas->drawBitmapRect(fBitmap, r, &filterPaint);
117 canvas->restore();
118
119 canvas->translate(SkIntToScalar(kCellSize), 0);
120 canvas->save();
121 canvas->concat(fPerspMatrix);
reed9ce9d672016-03-17 10:51:11 -0700122 canvas->drawImage(fImage.get(), 0, 0, &filterPaint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700123 canvas->restore();
124
125 canvas->translate(SkIntToScalar(kCellSize), 0);
126 canvas->save();
127 canvas->concat(fPerspMatrix);
128 canvas->drawRect(r, pathPaint);
129 canvas->restore();
130
131 canvas->translate(SkIntToScalar(kCellSize), 0);
132 canvas->save();
133 canvas->concat(fPerspMatrix);
134 canvas->drawPath(fPath, pathPaint);
135 canvas->restore();
136
137 canvas->translate(SkIntToScalar(kCellSize), 0);
138 canvas->save();
139 canvas->concat(fPerspMatrix);
140 canvas->drawRect(r, gradPaint1);
141 canvas->restore();
142
143 canvas->translate(SkIntToScalar(kCellSize), 0);
144 canvas->save();
145 canvas->concat(fPerspMatrix);
146 canvas->drawPath(fPath, gradPaint2);
147 canvas->restore();
148
149 canvas->restore();
150 }
151
152 void onDraw(SkCanvas* canvas) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400153 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
reed9ce9d672016-03-17 10:51:11 -0700154 fImage = make_image(canvas, kCellSize, kCellSize);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700155 }
156
157 this->drawRow(canvas, kNone_SkFilterQuality);
158 canvas->translate(0, SkIntToScalar(kCellSize));
159 this->drawRow(canvas, kLow_SkFilterQuality);
160 canvas->translate(0, SkIntToScalar(kCellSize));
161 this->drawRow(canvas, kMedium_SkFilterQuality);
162 canvas->translate(0, SkIntToScalar(kCellSize));
163 this->drawRow(canvas, kHigh_SkFilterQuality);
164 canvas->translate(0, SkIntToScalar(kCellSize));
165 }
166private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700167 static constexpr int kCellSize = 50;
168 static constexpr int kNumRows = 4;
169 static constexpr int kNumCols = 6;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700170
reed9ce9d672016-03-17 10:51:11 -0700171 bool fDoAA;
172 SkPath fPath;
173 sk_sp<SkShader> fBitmapShader;
174 sk_sp<SkShader> fLinearGrad1;
175 sk_sp<SkShader> fLinearGrad2;
176 SkMatrix fPerspMatrix;
177 sk_sp<SkImage> fImage;
178 SkBitmap fBitmap;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700179
John Stiles7571f9e2020-09-02 22:42:33 -0400180 using INHERITED = GM;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700181};
Mike Reedb4068ed2019-12-05 11:55:52 -0500182DEF_GM(return new PerspShadersGM(true);)
183DEF_GM(return new PerspShadersGM(false);)
John Stilesa6841be2020-08-06 14:11:56 -0400184} // namespace skiagm
robertphillipsfac9ceb2015-09-03 08:32:08 -0700185
186//////////////////////////////////////////////////////////////////////////////
187
Mike Reedb4068ed2019-12-05 11:55:52 -0500188#include "tools/Resources.h"
189
190static SkPath make_path() {
191 SkRandom rand;
Mike Reed0483b462019-12-05 17:29:45 -0500192 auto rand_pt = [&rand]() {
193 auto x = rand.nextF();
194 auto y = rand.nextF();
195 return SkPoint{x * 400, y * 400};
196 };
Mike Reedb4068ed2019-12-05 11:55:52 -0500197
198 SkPath path;
199 for (int i = 0; i < 4; ++i) {
Mike Reed0483b462019-12-05 17:29:45 -0500200 SkPoint pts[6];
201 for (auto& p : pts) {
202 p = rand_pt();
203 }
204 path.moveTo(pts[0]).quadTo(pts[1], pts[2]).quadTo(pts[3], pts[4]).lineTo(pts[5]);
Mike Reedb4068ed2019-12-05 11:55:52 -0500205 }
206 return path;
207}
208
209DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) {
210 SkPath path = make_path();
211 auto shader = GetResourceAsImage("images/mandrill_128.png")
Mike Reedb612b6c2020-12-08 21:58:35 -0500212 ->makeShader(SkSamplingOptions(), SkMatrix::Scale(3, 3));
Mike Reedb4068ed2019-12-05 11:55:52 -0500213
214 SkPaint paint;
215 paint.setColor({0.75, 0.75, 0.75, 1});
216 canvas->drawPath(path, paint);
217
218 // This is a crazy perspective matrix, derived from halfplanes3, to draw a shape where
219 // part of it is "behind" the viewer, hence showing the need for "half-plane" clipping
220 // when in perspective.
221 SkMatrix mx;
222 const SkScalar array[] = {
223 -1.7866f, 1.3357f, 273.0295f,
224 -1.0820f, 1.3186f, 135.5196f,
225 -0.0047f, -0.0015f, 2.1485f,
226 };
227 mx.set9(array);
228
229 paint.setShader(shader);
230 canvas->concat(mx);
231 canvas->drawPath(path, paint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700232}