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); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 61 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 62 | fBitmapShader = fBitmap.makeShader(); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 63 | SkPoint pts1[] = { |
| 64 | { 0, 0 }, |
| 65 | { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } |
| 66 | }; |
| 67 | SkPoint pts2[] = { |
| 68 | { 0, 0 }, |
| 69 | { 0, SkIntToScalar(kCellSize) } |
| 70 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 71 | constexpr SkColor colors[] = { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 72 | SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED |
| 73 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 74 | constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 75 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 77 | SkTileMode::kClamp); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 78 | fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 79 | SkTileMode::kClamp); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 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 | |
| 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); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 121 | canvas->drawImage(fImage.get(), 0, 0, &filterPaint); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 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 | |
| 151 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 5426850 | 2018-05-24 13:22:01 -0400 | [diff] [blame] | 152 | if (!fImage || !fImage->isValid(canvas->getGrContext())) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 153 | fImage = make_image(canvas, kCellSize, kCellSize); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 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: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 166 | static constexpr int kCellSize = 50; |
| 167 | static constexpr int kNumRows = 4; |
| 168 | static constexpr int kNumCols = 6; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 169 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 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; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 178 | |
| 179 | typedef GM INHERITED; |
| 180 | }; |
| 181 | |
| 182 | ////////////////////////////////////////////////////////////////////////////// |
| 183 | |
| 184 | DEF_GM(return new PerspShadersGM(true);) |
| 185 | DEF_GM(return new PerspShadersGM(false);) |
| 186 | } |