blob: 9c59c52c56d7a80cca1a71e6176e83757807330b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#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 Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/effects/SkGradientShader.h"
29#include "tools/ToolUtils.h"
robertphillipsfac9ceb2015-09-03 08:32:08 -070030
reed9ce9d672016-03-17 10:51:11 -070031static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) {
robertphillipsfac9ceb2015-09-03 08:32:08 -070032 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
Mike Kleinea3f0142019-03-20 11:12:10 -050033 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillipsfac9ceb2015-09-03 08:32:08 -070034 SkCanvas* canvas = surface->getCanvas();
35
Mike Kleinea3f0142019-03-20 11:12:10 -050036 ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10);
reed9ce9d672016-03-17 10:51:11 -070037 return surface->makeImageSnapshot();
robertphillipsfac9ceb2015-09-03 08:32:08 -070038}
39
robertphillipsfac9ceb2015-09-03 08:32:08 -070040namespace skiagm {
41
42class PerspShadersGM : public GM {
43public:
44 PerspShadersGM(bool doAA) : fDoAA(doAA) { }
45
46protected:
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 Kleinea3f0142019-03-20 11:12:10 -050059 fBitmap = ToolUtils::create_checkerboard_bitmap(
60 kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10);
robertphillipsfac9ceb2015-09-03 08:32:08 -070061
Mike Reed50acf8f2019-04-08 13:20:23 -040062 fBitmapShader = fBitmap.makeShader();
robertphillipsfac9ceb2015-09-03 08:32:08 -070063 SkPoint pts1[] = {
64 { 0, 0 },
65 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
66 };
67 SkPoint pts2[] = {
68 { 0, 0 },
69 { 0, SkIntToScalar(kCellSize) }
70 };
mtkleindbfd7ab2016-09-01 11:24:54 -070071 constexpr SkColor colors[] = {
robertphillipsfac9ceb2015-09-03 08:32:08 -070072 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
73 };
mtkleindbfd7ab2016-09-01 11:24:54 -070074 constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
robertphillipsfac9ceb2015-09-03 08:32:08 -070075
reed1a9b9642016-03-13 14:13:58 -070076 fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040077 SkTileMode::kClamp);
reed1a9b9642016-03-13 14:13:58 -070078 fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040079 SkTileMode::kClamp);
robertphillipsfac9ceb2015-09-03 08:32:08 -070080
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);
reed9ce9d672016-03-17 10:51:11 -0700121 canvas->drawImage(fImage.get(), 0, 0, &filterPaint);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700122 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 Salomon54268502018-05-24 13:22:01 -0400152 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
reed9ce9d672016-03-17 10:51:11 -0700153 fImage = make_image(canvas, kCellSize, kCellSize);
robertphillipsfac9ceb2015-09-03 08:32:08 -0700154 }
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 }
165private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700166 static constexpr int kCellSize = 50;
167 static constexpr int kNumRows = 4;
168 static constexpr int kNumCols = 6;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700169
reed9ce9d672016-03-17 10:51:11 -0700170 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;
robertphillipsfac9ceb2015-09-03 08:32:08 -0700178
179 typedef GM INHERITED;
180};
181
182//////////////////////////////////////////////////////////////////////////////
183
184DEF_GM(return new PerspShadersGM(true);)
185DEF_GM(return new PerspShadersGM(false);)
186}