robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkImageInfo.h" |
| 15 | #include "include/core/SkMatrix.h" |
| 16 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "include/core/SkTileMode.h" |
| 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/effects/SkGradientShader.h" |
| 29 | #include "tools/ToolUtils.h" |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 30 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 31 | static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 32 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 33 | auto surface(ToolUtils::makeSurface(origCanvas, info)); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 34 | SkCanvas* canvas = surface->getCanvas(); |
| 35 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 36 | ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 37 | return surface->makeImageSnapshot(); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 38 | } |
| 39 | |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 40 | namespace skiagm { |
| 41 | |
| 42 | class PerspShadersGM : public GM { |
| 43 | public: |
| 44 | PerspShadersGM(bool doAA) : fDoAA(doAA) { } |
| 45 | |
| 46 | protected: |
| 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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 59 | fBitmap = ToolUtils::create_checkerboard_bitmap( |
| 60 | kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10); |
Michael Ludwig | b0cee9b | 2020-03-12 10:52:15 -0400 | [diff] [blame] | 61 | fBitmap.setImmutable(); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 62 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 63 | fBitmapShader = fBitmap.makeShader(); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 64 | SkPoint pts1[] = { |
| 65 | { 0, 0 }, |
| 66 | { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } |
| 67 | }; |
| 68 | SkPoint pts2[] = { |
| 69 | { 0, 0 }, |
| 70 | { 0, SkIntToScalar(kCellSize) } |
| 71 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 72 | constexpr SkColor colors[] = { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 73 | SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED |
| 74 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 75 | constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 76 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 77 | fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 78 | SkTileMode::kClamp); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 79 | fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 80 | SkTileMode::kClamp); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 81 | |
| 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); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 122 | canvas->drawImage(fImage.get(), 0, 0, &filterPaint); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 123 | 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 Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame^] | 153 | if (!fImage || !fImage->isValid(canvas->recordingContext())) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 154 | fImage = make_image(canvas, kCellSize, kCellSize); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 155 | } |
| 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 | } |
| 166 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 167 | static constexpr int kCellSize = 50; |
| 168 | static constexpr int kNumRows = 4; |
| 169 | static constexpr int kNumCols = 6; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 170 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 171 | 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; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 179 | |
| 180 | typedef GM INHERITED; |
| 181 | }; |
Mike Reed | b4068ed | 2019-12-05 11:55:52 -0500 | [diff] [blame] | 182 | DEF_GM(return new PerspShadersGM(true);) |
| 183 | DEF_GM(return new PerspShadersGM(false);) |
| 184 | } |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 185 | |
| 186 | ////////////////////////////////////////////////////////////////////////////// |
| 187 | |
Mike Reed | b4068ed | 2019-12-05 11:55:52 -0500 | [diff] [blame] | 188 | #include "tools/Resources.h" |
| 189 | |
| 190 | static SkPath make_path() { |
| 191 | SkRandom rand; |
Mike Reed | 0483b46 | 2019-12-05 17:29:45 -0500 | [diff] [blame] | 192 | auto rand_pt = [&rand]() { |
| 193 | auto x = rand.nextF(); |
| 194 | auto y = rand.nextF(); |
| 195 | return SkPoint{x * 400, y * 400}; |
| 196 | }; |
Mike Reed | b4068ed | 2019-12-05 11:55:52 -0500 | [diff] [blame] | 197 | |
| 198 | SkPath path; |
| 199 | for (int i = 0; i < 4; ++i) { |
Mike Reed | 0483b46 | 2019-12-05 17:29:45 -0500 | [diff] [blame] | 200 | 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 Reed | b4068ed | 2019-12-05 11:55:52 -0500 | [diff] [blame] | 205 | } |
| 206 | return path; |
| 207 | } |
| 208 | |
| 209 | DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) { |
| 210 | SkPath path = make_path(); |
| 211 | auto shader = GetResourceAsImage("images/mandrill_128.png") |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 212 | ->makeShader(SkMatrix::Scale(3, 3)); |
Mike Reed | b4068ed | 2019-12-05 11:55:52 -0500 | [diff] [blame] | 213 | |
| 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); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 232 | } |