blob: 24073d057c41eac190c3d6c42829bd38e367adf5 [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 Reed8d29ab62021-01-23 18:10:39 -050059 fBitmapImage = ToolUtils::create_checkerboard_image(
Mike Kleinea3f0142019-03-20 11:12:10 -050060 kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10);
robertphillipsfac9ceb2015-09-03 08:32:08 -070061
robertphillipsfac9ceb2015-09-03 08:32:08 -070062 SkPoint pts1[] = {
63 { 0, 0 },
64 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
65 };
66 SkPoint pts2[] = {
67 { 0, 0 },
68 { 0, SkIntToScalar(kCellSize) }
69 };
mtkleindbfd7ab2016-09-01 11:24:54 -070070 constexpr SkColor colors[] = {
robertphillipsfac9ceb2015-09-03 08:32:08 -070071 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
72 };
mtkleindbfd7ab2016-09-01 11:24:54 -070073 constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
robertphillipsfac9ceb2015-09-03 08:32:08 -070074
reed1a9b9642016-03-13 14:13:58 -070075 fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040076 SkTileMode::kClamp);
reed1a9b9642016-03-13 14:13:58 -070077 fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040078 SkTileMode::kClamp);
robertphillipsfac9ceb2015-09-03 08:32:08 -070079
80 fPerspMatrix.reset();
81 fPerspMatrix.setPerspY(SK_Scalar1 / 50);
82
83 fPath.moveTo(0, 0);
84 fPath.lineTo(0, SkIntToScalar(kCellSize));
85 fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f);
86 fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
87 fPath.lineTo(SkIntToScalar(kCellSize), 0);
88 fPath.close();
89 }
90
Mike Reed8d29ab62021-01-23 18:10:39 -050091 void drawRow(SkCanvas* canvas, const SkSamplingOptions& sampling) {
robertphillipsfac9ceb2015-09-03 08:32:08 -070092 SkPaint filterPaint;
robertphillipsfac9ceb2015-09-03 08:32:08 -070093 filterPaint.setAntiAlias(fDoAA);
94
95 SkPaint pathPaint;
Mike Reed8d29ab62021-01-23 18:10:39 -050096 pathPaint.setShader(fBitmapImage->makeShader(sampling));
robertphillipsfac9ceb2015-09-03 08:32:08 -070097 pathPaint.setAntiAlias(fDoAA);
98
99 SkPaint gradPaint1;
100 gradPaint1.setShader(fLinearGrad1);
101 gradPaint1.setAntiAlias(fDoAA);
102 SkPaint gradPaint2;
103 gradPaint2.setShader(fLinearGrad2);
104 gradPaint2.setAntiAlias(fDoAA);
105
106 SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
107
108 canvas->save();
109
110 canvas->save();
111 canvas->concat(fPerspMatrix);
Mike Reed8d29ab62021-01-23 18:10:39 -0500112 canvas->drawImageRect(fBitmapImage, r, sampling, &filterPaint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700113 canvas->restore();
114
115 canvas->translate(SkIntToScalar(kCellSize), 0);
116 canvas->save();
117 canvas->concat(fPerspMatrix);
Mike Reed8d29ab62021-01-23 18:10:39 -0500118 canvas->drawImage(fImage.get(), 0, 0, sampling, &filterPaint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700119 canvas->restore();
120
121 canvas->translate(SkIntToScalar(kCellSize), 0);
122 canvas->save();
123 canvas->concat(fPerspMatrix);
124 canvas->drawRect(r, pathPaint);
125 canvas->restore();
126
127 canvas->translate(SkIntToScalar(kCellSize), 0);
128 canvas->save();
129 canvas->concat(fPerspMatrix);
130 canvas->drawPath(fPath, pathPaint);
131 canvas->restore();
132
133 canvas->translate(SkIntToScalar(kCellSize), 0);
134 canvas->save();
135 canvas->concat(fPerspMatrix);
136 canvas->drawRect(r, gradPaint1);
137 canvas->restore();
138
139 canvas->translate(SkIntToScalar(kCellSize), 0);
140 canvas->save();
141 canvas->concat(fPerspMatrix);
142 canvas->drawPath(fPath, gradPaint2);
143 canvas->restore();
144
145 canvas->restore();
146 }
147
148 void onDraw(SkCanvas* canvas) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400149 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
reed9ce9d672016-03-17 10:51:11 -0700150 fImage = make_image(canvas, kCellSize, kCellSize);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700151 }
152
Mike Reed8d29ab62021-01-23 18:10:39 -0500153 this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kNearest));
robertphillipsfac9ceb2015-09-03 08:32:08 -0700154 canvas->translate(0, SkIntToScalar(kCellSize));
Mike Reed8d29ab62021-01-23 18:10:39 -0500155 this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear));
robertphillipsfac9ceb2015-09-03 08:32:08 -0700156 canvas->translate(0, SkIntToScalar(kCellSize));
Mike Reed8d29ab62021-01-23 18:10:39 -0500157 this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear,
158 SkMipmapMode::kNearest));
robertphillipsfac9ceb2015-09-03 08:32:08 -0700159 canvas->translate(0, SkIntToScalar(kCellSize));
Mike Reedf3ac2af2021-02-05 12:55:38 -0500160 this->drawRow(canvas, SkSamplingOptions(SkCubicResampler::Mitchell()));
robertphillipsfac9ceb2015-09-03 08:32:08 -0700161 canvas->translate(0, SkIntToScalar(kCellSize));
162 }
163private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700164 static constexpr int kCellSize = 50;
165 static constexpr int kNumRows = 4;
166 static constexpr int kNumCols = 6;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700167
reed9ce9d672016-03-17 10:51:11 -0700168 bool fDoAA;
169 SkPath fPath;
reed9ce9d672016-03-17 10:51:11 -0700170 sk_sp<SkShader> fLinearGrad1;
171 sk_sp<SkShader> fLinearGrad2;
172 SkMatrix fPerspMatrix;
173 sk_sp<SkImage> fImage;
Mike Reed8d29ab62021-01-23 18:10:39 -0500174 sk_sp<SkImage> fBitmapImage;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700175
John Stiles7571f9e2020-09-02 22:42:33 -0400176 using INHERITED = GM;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700177};
Mike Reedb4068ed2019-12-05 11:55:52 -0500178DEF_GM(return new PerspShadersGM(true);)
179DEF_GM(return new PerspShadersGM(false);)
John Stilesa6841be2020-08-06 14:11:56 -0400180} // namespace skiagm
robertphillipsfac9ceb2015-09-03 08:32:08 -0700181
182//////////////////////////////////////////////////////////////////////////////
183
Mike Reedb4068ed2019-12-05 11:55:52 -0500184#include "tools/Resources.h"
185
186static SkPath make_path() {
187 SkRandom rand;
Mike Reed0483b462019-12-05 17:29:45 -0500188 auto rand_pt = [&rand]() {
189 auto x = rand.nextF();
190 auto y = rand.nextF();
191 return SkPoint{x * 400, y * 400};
192 };
Mike Reedb4068ed2019-12-05 11:55:52 -0500193
194 SkPath path;
195 for (int i = 0; i < 4; ++i) {
Mike Reed0483b462019-12-05 17:29:45 -0500196 SkPoint pts[6];
197 for (auto& p : pts) {
198 p = rand_pt();
199 }
200 path.moveTo(pts[0]).quadTo(pts[1], pts[2]).quadTo(pts[3], pts[4]).lineTo(pts[5]);
Mike Reedb4068ed2019-12-05 11:55:52 -0500201 }
202 return path;
203}
204
205DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) {
206 SkPath path = make_path();
207 auto shader = GetResourceAsImage("images/mandrill_128.png")
Mike Reedb612b6c2020-12-08 21:58:35 -0500208 ->makeShader(SkSamplingOptions(), SkMatrix::Scale(3, 3));
Mike Reedb4068ed2019-12-05 11:55:52 -0500209
210 SkPaint paint;
211 paint.setColor({0.75, 0.75, 0.75, 1});
212 canvas->drawPath(path, paint);
213
214 // This is a crazy perspective matrix, derived from halfplanes3, to draw a shape where
215 // part of it is "behind" the viewer, hence showing the need for "half-plane" clipping
216 // when in perspective.
217 SkMatrix mx;
218 const SkScalar array[] = {
219 -1.7866f, 1.3357f, 273.0295f,
220 -1.0820f, 1.3186f, 135.5196f,
221 -0.0047f, -0.0015f, 2.1485f,
222 };
223 mx.set9(array);
224
225 paint.setShader(shader);
226 canvas->concat(mx);
227 canvas->drawPath(path, paint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700228}