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