blob: eccf6a62875df213dac79440bea9983f2203c78a [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) {
30
31 // Build the picture.
robertphillips@google.com84b18c72014-04-13 19:09:42 +000032 SkPictureRecorder recorder;
robertphillipsa8d7f0b2014-08-29 08:03:56 -070033 SkCanvas* pictureCanvas = recorder.beginRecording(tileSize, tileSize, NULL, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000034 this->drawTile(pictureCanvas);
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000035 fPicture.reset(recorder.endRecording());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000036
37 // Build a reference bitmap.
robertphillipsa8d7f0b2014-08-29 08:03:56 -070038 fBitmap.allocN32Pixels(SkScalarCeilToInt(tileSize), SkScalarCeilToInt(tileSize));
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +000039 fBitmap.eraseColor(SK_ColorTRANSPARENT);
40 SkCanvas bitmapCanvas(fBitmap);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000041 this->drawTile(&bitmapCanvas);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000042 }
43
44protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000045
mtklein72c9faa2015-01-09 10:06:39 -080046 SkString onShortName() SK_OVERRIDE {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000047 return SkString("pictureshader");
48 }
49
mtklein72c9faa2015-01-09 10:06:39 -080050 SkISize onISize() SK_OVERRIDE {
fmalitab0878792015-01-15 10:45:56 -080051 return SkISize::Make(1400, 1450);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000052 }
53
mtklein72c9faa2015-01-09 10:06:39 -080054 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000055 this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0);
56 this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0);
57 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1);
58 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2);
59 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0);
60 this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0);
fmalitab0878792015-01-15 10:45:56 -080061
62 // One last custom row to exercise negative scaling
63 SkMatrix ctm, localMatrix;
64 ctm.setTranslate(fSceneSize * 2.1f, fSceneSize * 13.8f);
65 ctm.preScale(-1, -1);
66 localMatrix.setScale(2, 2);
67 this->drawScene(canvas, ctm, localMatrix, 0);
68
69 ctm.setTranslate(fSceneSize * 2.4f, fSceneSize * 12.8f);
70 localMatrix.setScale(-1, -1);
71 this->drawScene(canvas, ctm, localMatrix, 0);
72
73 ctm.setTranslate(fSceneSize * 4.8f, fSceneSize * 12.3f);
74 ctm.preScale(2, 2);
75 this->drawScene(canvas, ctm, localMatrix, 0);
76
77 ctm.setTranslate(fSceneSize * 13.8f, fSceneSize * 14.3f);
78 ctm.preScale(-2, -2);
79 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
80 localMatrix.preRotate(45);
81 localMatrix.preScale(-2, -2);
82 this->drawScene(canvas, ctm, localMatrix, 0);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000083 }
84
85private:
86 void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale,
87 unsigned tileMode) {
88 SkMatrix ctm, localMatrix;
89
90 ctm.setTranslate(pos.x(), pos.y());
91 ctm.preScale(scale, scale);
92 localMatrix.setScale(localScale, localScale);
93 this->drawScene(canvas, ctm, localMatrix, tileMode);
94
95 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale);
96 ctm.preScale(scale, scale);
97 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
98 localMatrix.preScale(localScale, localScale);
99 this->drawScene(canvas, ctm, localMatrix, tileMode);
100
101 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale);
102 ctm.preScale(scale, scale);
103 localMatrix.setRotate(45);
104 localMatrix.preScale(localScale, localScale);
105 this->drawScene(canvas, ctm, localMatrix, tileMode);
106
107 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale);
108 ctm.preScale(scale, scale);
109 localMatrix.setSkew(1, 0);
110 localMatrix.preScale(localScale, localScale);
111 this->drawScene(canvas, ctm, localMatrix, tileMode);
112
113 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale);
114 ctm.preScale(scale, scale);
115 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
116 localMatrix.preRotate(45);
117 localMatrix.preScale(localScale, localScale);
118 this->drawScene(canvas, ctm, localMatrix, tileMode);
119 }
120
121 void drawTile(SkCanvas* canvas) {
122 SkPaint paint;
123 paint.setColor(SK_ColorGREEN);
124 paint.setStyle(SkPaint::kFill_Style);
125 paint.setAntiAlias(true);
126
127 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
128 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
129 fTileSize / 2, fTileSize / 2), paint);
130
131 paint.setColor(SK_ColorRED);
132 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
133 fTileSize / 2, fTileSize * 2 / 3, paint);
134 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
135 fTileSize * 2 / 3, fTileSize / 2, paint);
136 }
137
138 void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix,
139 unsigned tileMode) {
140 SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs));
141
142 SkPaint paint;
143 paint.setStyle(SkPaint::kFill_Style);
144 paint.setColor(SK_ColorLTGRAY);
145
146 canvas->save();
147 canvas->concat(matrix);
148 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
149 canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
150
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000151 SkAutoTUnref<SkShader> pictureShader(SkShader::CreatePictureShader(
152 fPicture,
153 kTileConfigs[tileMode].tmx,
154 kTileConfigs[tileMode].tmy,
fmalitab5f78262014-08-06 13:07:15 -0700155 &localMatrix,
156 NULL));
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000157 paint.setShader(pictureShader.get());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000158 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
159
160 canvas->translate(fSceneSize * 1.1f, 0);
161
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000162 SkAutoTUnref<SkShader> bitmapShader(SkShader::CreateBitmapShader(
163 fBitmap,
164 kTileConfigs[tileMode].tmx,
165 kTileConfigs[tileMode].tmy,
166 &localMatrix));
167 paint.setShader(bitmapShader.get());
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000168 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
169
170 canvas->restore();
171 }
172
173 SkScalar fTileSize;
174 SkScalar fSceneSize;
175
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000176 SkAutoTUnref<SkPicture> fPicture;
177 SkBitmap fBitmap;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000178
179 typedef GM INHERITED;
180};
181
182DEF_GM( return SkNEW_ARGS(PictureShaderGM, (50, 100)); )