blob: d54144f938114c79b6d00460369020a9ae0c1890 [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
8#include "gm.h"
9
10#include "SkBitmap.h"
11#include "SkPaint.h"
12#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000014#include "SkShader.h"
15
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000016static struct {
17 SkShader::TileMode tmx;
18 SkShader::TileMode tmy;
19} kTileConfigs[] = {
20 { SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode },
21 { SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode },
22 { SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode },
23};
24
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000025class PictureShaderGM : public skiagm::GM {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000026public:
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000027 PictureShaderGM(SkScalar tileSize, SkScalar sceneSize)
28 : fTileSize(tileSize)
29 , fSceneSize(sceneSize) {
caryclark63c684a2015-02-25 09:04:04 -080030 }
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000031
caryclark63c684a2015-02-25 09:04:04 -080032 protected:
mtklein36352bf2015-03-25 18:17:31 -070033 void onOnceBeforeDraw() override {
caryclark63c684a2015-02-25 09:04:04 -080034 // Build the picture.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000035 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070036 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000037 this->drawTile(pictureCanvas);
reedca2622b2016-03-18 07:25:55 -070038 fPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000039
40 // Build a reference bitmap.
caryclark63c684a2015-02-25 09:04:04 -080041 fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize));
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000042 fBitmap.eraseColor(SK_ColorTRANSPARENT);
43 SkCanvas bitmapCanvas(fBitmap);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000044 this->drawTile(&bitmapCanvas);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000045 }
46
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000047
mtklein36352bf2015-03-25 18:17:31 -070048 SkString onShortName() override {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000049 return SkString("pictureshader");
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkISize onISize() override {
fmalitab0878792015-01-15 10:45:56 -080053 return SkISize::Make(1400, 1450);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000054 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000057 this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0);
58 this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0);
59 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1);
60 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2);
61 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0);
62 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0);
fmalitab0878792015-01-15 10:45:56 -080063
64 // One last custom row to exercise negative scaling
65 SkMatrix ctm, localMatrix;
66 ctm.setTranslate(fSceneSize * 2.1f, fSceneSize * 13.8f);
67 ctm.preScale(-1, -1);
68 localMatrix.setScale(2, 2);
69 this->drawScene(canvas, ctm, localMatrix, 0);
70
71 ctm.setTranslate(fSceneSize * 2.4f, fSceneSize * 12.8f);
72 localMatrix.setScale(-1, -1);
73 this->drawScene(canvas, ctm, localMatrix, 0);
74
75 ctm.setTranslate(fSceneSize * 4.8f, fSceneSize * 12.3f);
76 ctm.preScale(2, 2);
77 this->drawScene(canvas, ctm, localMatrix, 0);
78
79 ctm.setTranslate(fSceneSize * 13.8f, fSceneSize * 14.3f);
80 ctm.preScale(-2, -2);
81 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
82 localMatrix.preRotate(45);
83 localMatrix.preScale(-2, -2);
84 this->drawScene(canvas, ctm, localMatrix, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000085 }
86
87private:
88 void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale,
89 unsigned tileMode) {
90 SkMatrix ctm, localMatrix;
91
92 ctm.setTranslate(pos.x(), pos.y());
93 ctm.preScale(scale, scale);
94 localMatrix.setScale(localScale, localScale);
95 this->drawScene(canvas, ctm, localMatrix, tileMode);
96
97 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale);
98 ctm.preScale(scale, scale);
99 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
100 localMatrix.preScale(localScale, localScale);
101 this->drawScene(canvas, ctm, localMatrix, tileMode);
102
103 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale);
104 ctm.preScale(scale, scale);
105 localMatrix.setRotate(45);
106 localMatrix.preScale(localScale, localScale);
107 this->drawScene(canvas, ctm, localMatrix, tileMode);
108
109 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale);
110 ctm.preScale(scale, scale);
111 localMatrix.setSkew(1, 0);
112 localMatrix.preScale(localScale, localScale);
113 this->drawScene(canvas, ctm, localMatrix, tileMode);
114
115 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale);
116 ctm.preScale(scale, scale);
117 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
118 localMatrix.preRotate(45);
119 localMatrix.preScale(localScale, localScale);
120 this->drawScene(canvas, ctm, localMatrix, tileMode);
121 }
122
123 void drawTile(SkCanvas* canvas) {
124 SkPaint paint;
125 paint.setColor(SK_ColorGREEN);
126 paint.setStyle(SkPaint::kFill_Style);
127 paint.setAntiAlias(true);
128
129 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
130 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
131 fTileSize / 2, fTileSize / 2), paint);
132
133 paint.setColor(SK_ColorRED);
134 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
135 fTileSize / 2, fTileSize * 2 / 3, paint);
136 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
137 fTileSize * 2 / 3, fTileSize / 2, paint);
138 }
139
140 void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix,
141 unsigned tileMode) {
142 SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs));
143
144 SkPaint paint;
145 paint.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700146 paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000147
148 canvas->save();
149 canvas->concat(matrix);
150 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
151 canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
152
reed1a9b9642016-03-13 14:13:58 -0700153 paint.setShader(SkShader::MakePictureShader(fPicture, kTileConfigs[tileMode].tmx,
154 kTileConfigs[tileMode].tmy, &localMatrix,
155 nullptr));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000156 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
157
158 canvas->translate(fSceneSize * 1.1f, 0);
159
reed1a9b9642016-03-13 14:13:58 -0700160 paint.setShader(SkShader::MakeBitmapShader(fBitmap,
161 kTileConfigs[tileMode].tmx,
162 kTileConfigs[tileMode].tmy,
163 &localMatrix));
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000164 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
165
166 canvas->restore();
167 }
168
169 SkScalar fTileSize;
170 SkScalar fSceneSize;
171
reed1a9b9642016-03-13 14:13:58 -0700172 sk_sp<SkPicture> fPicture;
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000173 SkBitmap fBitmap;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000174
175 typedef GM INHERITED;
176};
177
halcanary385fe4d2015-08-26 13:07:48 -0700178DEF_GM(return new PictureShaderGM(50, 100);)
halcanary797f58a2015-02-05 10:26:27 -0800179
180DEF_SIMPLE_GM(tiled_picture_shader, canvas, 400, 400) {
181 // https://code.google.com/p/skia/issues/detail?id=3398
182 SkRect tile = SkRect::MakeWH(100, 100);
183
184 SkPictureRecorder recorder;
185 SkCanvas* c = recorder.beginRecording(tile);
186
187 SkRect r = tile;
188 r.inset(4, 4);
189 SkPaint p;
caryclarkf597c422015-07-28 10:37:53 -0700190 p.setColor(sk_tool_utils::color_to_565(0xFF303F9F)); // dark blue
halcanary797f58a2015-02-05 10:26:27 -0800191 c->drawRect(r, p);
caryclarkf597c422015-07-28 10:37:53 -0700192 p.setColor(sk_tool_utils::color_to_565(0xFFC5CAE9)); // light blue
halcanary797f58a2015-02-05 10:26:27 -0800193 p.setStrokeWidth(10);
194 c->drawLine(20, 20, 80, 80, p);
195
reedca2622b2016-03-18 07:25:55 -0700196 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
halcanary797f58a2015-02-05 10:26:27 -0800197
caryclarkf597c422015-07-28 10:37:53 -0700198 p.setColor(sk_tool_utils::color_to_565(0xFF8BC34A)); // green
halcanary797f58a2015-02-05 10:26:27 -0800199 canvas->drawPaint(p);
200
201 canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350));
caryclarkf597c422015-07-28 10:37:53 -0700202 p.setColor(sk_tool_utils::color_to_565(0xFFB6B6B6)); // gray
halcanary797f58a2015-02-05 10:26:27 -0800203 canvas->drawPaint(p);
halcanary797f58a2015-02-05 10:26:27 -0800204
reedfe630452016-03-25 09:08:00 -0700205 p.setShader(SkShader::MakePictureShader(std::move(picture), SkShader::kRepeat_TileMode,
206 SkShader::kRepeat_TileMode,
207 nullptr, nullptr));
halcanary797f58a2015-02-05 10:26:27 -0800208 canvas->drawPaint(p);
209}