blob: 85eb316dff6d9b08cd9c14bd49b5028b49ed34a3 [file] [log] [blame]
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +00001/*
2 * Copyright 2014 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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPaint.h"
14#include "include/core/SkPicture.h"
15#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
23#include "include/core/SkTileMode.h"
24#include "include/core/SkTypes.h"
25#include "tools/ToolUtils.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000026
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000027static struct {
Mike Reedfae8fce2019-04-03 10:27:45 -040028 SkTileMode tmx;
29 SkTileMode tmy;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000030} kTileConfigs[] = {
Mike Reedfae8fce2019-04-03 10:27:45 -040031 { SkTileMode::kRepeat, SkTileMode::kRepeat },
32 { SkTileMode::kRepeat, SkTileMode::kClamp },
33 { SkTileMode::kMirror, SkTileMode::kRepeat },
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000034};
35
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000036class PictureShaderGM : public skiagm::GM {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000037public:
Florin Malita18f7e0a2018-02-06 16:49:40 -050038 PictureShaderGM(SkScalar tileSize, SkScalar sceneSize, bool useLocalMatrixWrapper = false)
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000039 : fTileSize(tileSize)
Florin Malita18f7e0a2018-02-06 16:49:40 -050040 , fSceneSize(sceneSize)
41 , fUseLocalMatrixWrapper(useLocalMatrixWrapper) {}
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000042
caryclark63c684a2015-02-25 09:04:04 -080043 protected:
mtklein36352bf2015-03-25 18:17:31 -070044 void onOnceBeforeDraw() override {
caryclark63c684a2015-02-25 09:04:04 -080045 // Build the picture.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000046 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070047 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000048 this->drawTile(pictureCanvas);
reedca2622b2016-03-18 07:25:55 -070049 fPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000050
51 // Build a reference bitmap.
caryclark63c684a2015-02-25 09:04:04 -080052 fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize));
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000053 fBitmap.eraseColor(SK_ColorTRANSPARENT);
54 SkCanvas bitmapCanvas(fBitmap);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000055 this->drawTile(&bitmapCanvas);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000056 }
57
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000058
mtklein36352bf2015-03-25 18:17:31 -070059 SkString onShortName() override {
Florin Malita18f7e0a2018-02-06 16:49:40 -050060 return SkStringPrintf("pictureshader%s", fUseLocalMatrixWrapper ? "_localwrapper" : "");
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000061 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 SkISize onISize() override {
fmalitab0878792015-01-15 10:45:56 -080064 return SkISize::Make(1400, 1450);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000065 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000068 this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0);
69 this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0);
70 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1);
71 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2);
72 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0);
73 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0);
fmalitab0878792015-01-15 10:45:56 -080074
75 // One last custom row to exercise negative scaling
76 SkMatrix ctm, localMatrix;
77 ctm.setTranslate(fSceneSize * 2.1f, fSceneSize * 13.8f);
78 ctm.preScale(-1, -1);
79 localMatrix.setScale(2, 2);
80 this->drawScene(canvas, ctm, localMatrix, 0);
81
82 ctm.setTranslate(fSceneSize * 2.4f, fSceneSize * 12.8f);
83 localMatrix.setScale(-1, -1);
84 this->drawScene(canvas, ctm, localMatrix, 0);
85
86 ctm.setTranslate(fSceneSize * 4.8f, fSceneSize * 12.3f);
87 ctm.preScale(2, 2);
88 this->drawScene(canvas, ctm, localMatrix, 0);
89
90 ctm.setTranslate(fSceneSize * 13.8f, fSceneSize * 14.3f);
91 ctm.preScale(-2, -2);
92 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
93 localMatrix.preRotate(45);
94 localMatrix.preScale(-2, -2);
95 this->drawScene(canvas, ctm, localMatrix, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000096 }
97
98private:
99 void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale,
100 unsigned tileMode) {
101 SkMatrix ctm, localMatrix;
102
103 ctm.setTranslate(pos.x(), pos.y());
104 ctm.preScale(scale, scale);
105 localMatrix.setScale(localScale, localScale);
106 this->drawScene(canvas, ctm, localMatrix, tileMode);
107
108 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale);
109 ctm.preScale(scale, scale);
110 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
111 localMatrix.preScale(localScale, localScale);
112 this->drawScene(canvas, ctm, localMatrix, tileMode);
113
114 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale);
115 ctm.preScale(scale, scale);
116 localMatrix.setRotate(45);
117 localMatrix.preScale(localScale, localScale);
118 this->drawScene(canvas, ctm, localMatrix, tileMode);
119
120 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale);
121 ctm.preScale(scale, scale);
122 localMatrix.setSkew(1, 0);
123 localMatrix.preScale(localScale, localScale);
124 this->drawScene(canvas, ctm, localMatrix, tileMode);
125
126 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale);
127 ctm.preScale(scale, scale);
128 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
129 localMatrix.preRotate(45);
130 localMatrix.preScale(localScale, localScale);
131 this->drawScene(canvas, ctm, localMatrix, tileMode);
132 }
133
134 void drawTile(SkCanvas* canvas) {
135 SkPaint paint;
136 paint.setColor(SK_ColorGREEN);
137 paint.setStyle(SkPaint::kFill_Style);
138 paint.setAntiAlias(true);
139
140 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
141 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
142 fTileSize / 2, fTileSize / 2), paint);
143
144 paint.setColor(SK_ColorRED);
145 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
146 fTileSize / 2, fTileSize * 2 / 3, paint);
147 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
148 fTileSize * 2 / 3, fTileSize / 2, paint);
149 }
150
151 void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix,
152 unsigned tileMode) {
153 SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs));
154
155 SkPaint paint;
156 paint.setStyle(SkPaint::kFill_Style);
Mike Kleind46dce32018-08-16 10:17:03 -0400157 paint.setColor(SK_ColorLTGRAY);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000158
159 canvas->save();
160 canvas->concat(matrix);
161 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
162 canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
163
Mike Reedfae8fce2019-04-03 10:27:45 -0400164 auto pictureShader = fPicture->makeShader(kTileConfigs[tileMode].tmx,
165 kTileConfigs[tileMode].tmy,
166 fUseLocalMatrixWrapper ? nullptr : &localMatrix,
167 nullptr);
Florin Malita18f7e0a2018-02-06 16:49:40 -0500168 paint.setShader(fUseLocalMatrixWrapper
169 ? pictureShader->makeWithLocalMatrix(localMatrix)
170 : pictureShader);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000171 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
172
173 canvas->translate(fSceneSize * 1.1f, 0);
174
Mike Reed50acf8f2019-04-08 13:20:23 -0400175 auto bitmapShader = fBitmap.makeShader(
Florin Malita18f7e0a2018-02-06 16:49:40 -0500176 kTileConfigs[tileMode].tmx,
177 kTileConfigs[tileMode].tmy,
178 fUseLocalMatrixWrapper
179 ? nullptr : &localMatrix);
180 paint.setShader(fUseLocalMatrixWrapper
181 ? bitmapShader->makeWithLocalMatrix(localMatrix)
182 : bitmapShader);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000183 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
184
185 canvas->restore();
186 }
187
reed1a9b9642016-03-13 14:13:58 -0700188 sk_sp<SkPicture> fPicture;
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000189 SkBitmap fBitmap;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000190
Florin Malita18f7e0a2018-02-06 16:49:40 -0500191 SkScalar fTileSize;
192 SkScalar fSceneSize;
193 bool fUseLocalMatrixWrapper;
194
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000195 typedef GM INHERITED;
196};
197
halcanary385fe4d2015-08-26 13:07:48 -0700198DEF_GM(return new PictureShaderGM(50, 100);)
Florin Malita18f7e0a2018-02-06 16:49:40 -0500199DEF_GM(return new PictureShaderGM(50, 100, true);)
halcanary797f58a2015-02-05 10:26:27 -0800200
201DEF_SIMPLE_GM(tiled_picture_shader, canvas, 400, 400) {
202 // https://code.google.com/p/skia/issues/detail?id=3398
203 SkRect tile = SkRect::MakeWH(100, 100);
204
205 SkPictureRecorder recorder;
206 SkCanvas* c = recorder.beginRecording(tile);
207
208 SkRect r = tile;
209 r.inset(4, 4);
210 SkPaint p;
Mike Kleinea3f0142019-03-20 11:12:10 -0500211 p.setColor(ToolUtils::color_to_565(0xFF303F9F)); // dark blue
halcanary797f58a2015-02-05 10:26:27 -0800212 c->drawRect(r, p);
Mike Kleinea3f0142019-03-20 11:12:10 -0500213 p.setColor(ToolUtils::color_to_565(0xFFC5CAE9)); // light blue
halcanary797f58a2015-02-05 10:26:27 -0800214 p.setStrokeWidth(10);
215 c->drawLine(20, 20, 80, 80, p);
216
reedca2622b2016-03-18 07:25:55 -0700217 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
halcanary797f58a2015-02-05 10:26:27 -0800218
Mike Kleinea3f0142019-03-20 11:12:10 -0500219 p.setColor(ToolUtils::color_to_565(0xFF8BC34A)); // green
halcanary797f58a2015-02-05 10:26:27 -0800220 canvas->drawPaint(p);
221
222 canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350));
Mike Kleind46dce32018-08-16 10:17:03 -0400223 p.setColor(0xFFB6B6B6); // gray
halcanary797f58a2015-02-05 10:26:27 -0800224 canvas->drawPaint(p);
halcanary797f58a2015-02-05 10:26:27 -0800225
Mike Reedfae8fce2019-04-03 10:27:45 -0400226 p.setShader(picture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
halcanary797f58a2015-02-05 10:26:27 -0800227 canvas->drawPaint(p);
228}