blob: 31c48f28968b662b0bc32d542531b29ce1764336 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillipsfac9ceb2015-09-03 08:32:08 -070010#include "SkGradientShader.h"
11#include "SkImage.h"
12#include "SkPath.h"
13#include "SkSurface.h"
Mike Reed46596ae2018-01-02 15:40:29 -050014#include "sk_tool_utils.h"
robertphillipsfac9ceb2015-09-03 08:32:08 -070015
reed9ce9d672016-03-17 10:51:11 -070016static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) {
robertphillipsfac9ceb2015-09-03 08:32:08 -070017 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
Mike Reed46596ae2018-01-02 15:40:29 -050018 auto surface(sk_tool_utils::makeSurface(origCanvas, info));
robertphillipsfac9ceb2015-09-03 08:32:08 -070019 SkCanvas* canvas = surface->getCanvas();
20
21 sk_tool_utils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w/10);
reed9ce9d672016-03-17 10:51:11 -070022 return surface->makeImageSnapshot();
robertphillipsfac9ceb2015-09-03 08:32:08 -070023}
24
robertphillipsfac9ceb2015-09-03 08:32:08 -070025namespace skiagm {
26
27class PerspShadersGM : public GM {
28public:
29 PerspShadersGM(bool doAA) : fDoAA(doAA) { }
30
31protected:
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 {
robertphillips943a4622015-09-03 13:32:33 -070044 fBitmap = sk_tool_utils::create_checkerboard_bitmap(kCellSize, kCellSize,
45 SK_ColorBLUE, SK_ColorYELLOW,
46 kCellSize/10);
robertphillipsfac9ceb2015-09-03 08:32:08 -070047
reed1a9b9642016-03-13 14:13:58 -070048 fBitmapShader = SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode,
49 SkShader::kClamp_TileMode);
robertphillipsfac9ceb2015-09-03 08:32:08 -070050 SkPoint pts1[] = {
51 { 0, 0 },
52 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
53 };
54 SkPoint pts2[] = {
55 { 0, 0 },
56 { 0, SkIntToScalar(kCellSize) }
57 };
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr SkColor colors[] = {
robertphillipsfac9ceb2015-09-03 08:32:08 -070059 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
60 };
mtkleindbfd7ab2016-09-01 11:24:54 -070061 constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
robertphillipsfac9ceb2015-09-03 08:32:08 -070062
reed1a9b9642016-03-13 14:13:58 -070063 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);
robertphillipsfac9ceb2015-09-03 08:32:08 -070067
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);
reed9ce9d672016-03-17 10:51:11 -0700108 canvas->drawImage(fImage.get(), 0, 0, &filterPaint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700109 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 Salomon54268502018-05-24 13:22:01 -0400139 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
reed9ce9d672016-03-17 10:51:11 -0700140 fImage = make_image(canvas, kCellSize, kCellSize);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700141 }
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 }
152private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700153 static constexpr int kCellSize = 50;
154 static constexpr int kNumRows = 4;
155 static constexpr int kNumCols = 6;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700156
reed9ce9d672016-03-17 10:51:11 -0700157 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;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700165
166 typedef GM INHERITED;
167};
168
169//////////////////////////////////////////////////////////////////////////////
170
171DEF_GM(return new PerspShadersGM(true);)
172DEF_GM(return new PerspShadersGM(false);)
173}