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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkSurface.h" |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 15 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 16 | static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 17 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 18 | auto surface(sk_tool_utils::makeSurface(origCanvas, info)); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 19 | SkCanvas* canvas = surface->getCanvas(); |
| 20 | |
| 21 | sk_tool_utils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w/10); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 22 | return surface->makeImageSnapshot(); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 23 | } |
| 24 | |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | class PerspShadersGM : public GM { |
| 28 | public: |
| 29 | PerspShadersGM(bool doAA) : fDoAA(doAA) { } |
| 30 | |
| 31 | protected: |
| 32 | SkString onShortName() override { |
| 33 | SkString name; |
| 34 | name.printf("persp_shaders_%s", |
| 35 | fDoAA ? "aa" : "bw"); |
| 36 | return name; |
| 37 | } |
| 38 | |
| 39 | SkISize onISize() override { |
| 40 | return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows); |
| 41 | } |
| 42 | |
| 43 | void onOnceBeforeDraw() override { |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 44 | fBitmap = sk_tool_utils::create_checkerboard_bitmap(kCellSize, kCellSize, |
| 45 | SK_ColorBLUE, SK_ColorYELLOW, |
| 46 | kCellSize/10); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 47 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 48 | fBitmapShader = SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode, |
| 49 | SkShader::kClamp_TileMode); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 50 | SkPoint pts1[] = { |
| 51 | { 0, 0 }, |
| 52 | { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } |
| 53 | }; |
| 54 | SkPoint pts2[] = { |
| 55 | { 0, 0 }, |
| 56 | { 0, SkIntToScalar(kCellSize) } |
| 57 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 58 | constexpr SkColor colors[] = { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 59 | SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED |
| 60 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 61 | constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 62 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 63 | fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors), |
| 64 | SkShader::kClamp_TileMode); |
| 65 | fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors), |
| 66 | SkShader::kClamp_TileMode); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 67 | |
| 68 | fPerspMatrix.reset(); |
| 69 | fPerspMatrix.setPerspY(SK_Scalar1 / 50); |
| 70 | |
| 71 | fPath.moveTo(0, 0); |
| 72 | fPath.lineTo(0, SkIntToScalar(kCellSize)); |
| 73 | fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f); |
| 74 | fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)); |
| 75 | fPath.lineTo(SkIntToScalar(kCellSize), 0); |
| 76 | fPath.close(); |
| 77 | } |
| 78 | |
| 79 | void drawRow(SkCanvas* canvas, SkFilterQuality filterQ) { |
| 80 | SkPaint filterPaint; |
| 81 | filterPaint.setFilterQuality(filterQ); |
| 82 | filterPaint.setAntiAlias(fDoAA); |
| 83 | |
| 84 | SkPaint pathPaint; |
| 85 | pathPaint.setShader(fBitmapShader); |
| 86 | pathPaint.setFilterQuality(filterQ); |
| 87 | pathPaint.setAntiAlias(fDoAA); |
| 88 | |
| 89 | SkPaint gradPaint1; |
| 90 | gradPaint1.setShader(fLinearGrad1); |
| 91 | gradPaint1.setAntiAlias(fDoAA); |
| 92 | SkPaint gradPaint2; |
| 93 | gradPaint2.setShader(fLinearGrad2); |
| 94 | gradPaint2.setAntiAlias(fDoAA); |
| 95 | |
| 96 | SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)); |
| 97 | |
| 98 | canvas->save(); |
| 99 | |
| 100 | canvas->save(); |
| 101 | canvas->concat(fPerspMatrix); |
| 102 | canvas->drawBitmapRect(fBitmap, r, &filterPaint); |
| 103 | canvas->restore(); |
| 104 | |
| 105 | canvas->translate(SkIntToScalar(kCellSize), 0); |
| 106 | canvas->save(); |
| 107 | canvas->concat(fPerspMatrix); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 108 | canvas->drawImage(fImage.get(), 0, 0, &filterPaint); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 109 | canvas->restore(); |
| 110 | |
| 111 | canvas->translate(SkIntToScalar(kCellSize), 0); |
| 112 | canvas->save(); |
| 113 | canvas->concat(fPerspMatrix); |
| 114 | canvas->drawRect(r, pathPaint); |
| 115 | canvas->restore(); |
| 116 | |
| 117 | canvas->translate(SkIntToScalar(kCellSize), 0); |
| 118 | canvas->save(); |
| 119 | canvas->concat(fPerspMatrix); |
| 120 | canvas->drawPath(fPath, pathPaint); |
| 121 | canvas->restore(); |
| 122 | |
| 123 | canvas->translate(SkIntToScalar(kCellSize), 0); |
| 124 | canvas->save(); |
| 125 | canvas->concat(fPerspMatrix); |
| 126 | canvas->drawRect(r, gradPaint1); |
| 127 | canvas->restore(); |
| 128 | |
| 129 | canvas->translate(SkIntToScalar(kCellSize), 0); |
| 130 | canvas->save(); |
| 131 | canvas->concat(fPerspMatrix); |
| 132 | canvas->drawPath(fPath, gradPaint2); |
| 133 | canvas->restore(); |
| 134 | |
| 135 | canvas->restore(); |
| 136 | } |
| 137 | |
| 138 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 5426850 | 2018-05-24 13:22:01 -0400 | [diff] [blame] | 139 | if (!fImage || !fImage->isValid(canvas->getGrContext())) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 140 | fImage = make_image(canvas, kCellSize, kCellSize); |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | this->drawRow(canvas, kNone_SkFilterQuality); |
| 144 | canvas->translate(0, SkIntToScalar(kCellSize)); |
| 145 | this->drawRow(canvas, kLow_SkFilterQuality); |
| 146 | canvas->translate(0, SkIntToScalar(kCellSize)); |
| 147 | this->drawRow(canvas, kMedium_SkFilterQuality); |
| 148 | canvas->translate(0, SkIntToScalar(kCellSize)); |
| 149 | this->drawRow(canvas, kHigh_SkFilterQuality); |
| 150 | canvas->translate(0, SkIntToScalar(kCellSize)); |
| 151 | } |
| 152 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 153 | static constexpr int kCellSize = 50; |
| 154 | static constexpr int kNumRows = 4; |
| 155 | static constexpr int kNumCols = 6; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 156 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 157 | bool fDoAA; |
| 158 | SkPath fPath; |
| 159 | sk_sp<SkShader> fBitmapShader; |
| 160 | sk_sp<SkShader> fLinearGrad1; |
| 161 | sk_sp<SkShader> fLinearGrad2; |
| 162 | SkMatrix fPerspMatrix; |
| 163 | sk_sp<SkImage> fImage; |
| 164 | SkBitmap fBitmap; |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 165 | |
| 166 | typedef GM INHERITED; |
| 167 | }; |
| 168 | |
| 169 | ////////////////////////////////////////////////////////////////////////////// |
| 170 | |
| 171 | DEF_GM(return new PerspShadersGM(true);) |
| 172 | DEF_GM(return new PerspShadersGM(false);) |
| 173 | } |