commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 1 | /* |
| 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 15 | #include "SkShader.h" |
| 16 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 17 | static struct { |
| 18 | SkShader::TileMode tmx; |
| 19 | SkShader::TileMode tmy; |
| 20 | } kTileConfigs[] = { |
| 21 | { SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode }, |
| 22 | { SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode }, |
| 23 | { SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode }, |
| 24 | }; |
| 25 | |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 26 | class PictureShaderGM : public skiagm::GM { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 27 | public: |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 28 | PictureShaderGM(SkScalar tileSize, SkScalar sceneSize, bool useLocalMatrixWrapper = false) |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 29 | : fTileSize(tileSize) |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 30 | , fSceneSize(sceneSize) |
| 31 | , fUseLocalMatrixWrapper(useLocalMatrixWrapper) {} |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 32 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onOnceBeforeDraw() override { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 35 | // Build the picture. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 36 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 38 | this->drawTile(pictureCanvas); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 39 | fPicture = recorder.finishRecordingAsPicture(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 40 | |
| 41 | // Build a reference bitmap. |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 42 | fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize)); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 43 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
| 44 | SkCanvas bitmapCanvas(fBitmap); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 45 | this->drawTile(&bitmapCanvas); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkString onShortName() override { |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 50 | return SkStringPrintf("pictureshader%s", fUseLocalMatrixWrapper ? "_localwrapper" : ""); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | SkISize onISize() override { |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 54 | return SkISize::Make(1400, 1450); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 58 | this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0); |
| 59 | this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0); |
| 60 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1); |
| 61 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2); |
| 62 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0); |
| 63 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0); |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 64 | |
| 65 | // One last custom row to exercise negative scaling |
| 66 | SkMatrix ctm, localMatrix; |
| 67 | ctm.setTranslate(fSceneSize * 2.1f, fSceneSize * 13.8f); |
| 68 | ctm.preScale(-1, -1); |
| 69 | localMatrix.setScale(2, 2); |
| 70 | this->drawScene(canvas, ctm, localMatrix, 0); |
| 71 | |
| 72 | ctm.setTranslate(fSceneSize * 2.4f, fSceneSize * 12.8f); |
| 73 | localMatrix.setScale(-1, -1); |
| 74 | this->drawScene(canvas, ctm, localMatrix, 0); |
| 75 | |
| 76 | ctm.setTranslate(fSceneSize * 4.8f, fSceneSize * 12.3f); |
| 77 | ctm.preScale(2, 2); |
| 78 | this->drawScene(canvas, ctm, localMatrix, 0); |
| 79 | |
| 80 | ctm.setTranslate(fSceneSize * 13.8f, fSceneSize * 14.3f); |
| 81 | ctm.preScale(-2, -2); |
| 82 | localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 83 | localMatrix.preRotate(45); |
| 84 | localMatrix.preScale(-2, -2); |
| 85 | this->drawScene(canvas, ctm, localMatrix, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
| 89 | void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale, |
| 90 | unsigned tileMode) { |
| 91 | SkMatrix ctm, localMatrix; |
| 92 | |
| 93 | ctm.setTranslate(pos.x(), pos.y()); |
| 94 | ctm.preScale(scale, scale); |
| 95 | localMatrix.setScale(localScale, localScale); |
| 96 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 97 | |
| 98 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale); |
| 99 | ctm.preScale(scale, scale); |
| 100 | localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 101 | localMatrix.preScale(localScale, localScale); |
| 102 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 103 | |
| 104 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale); |
| 105 | ctm.preScale(scale, scale); |
| 106 | localMatrix.setRotate(45); |
| 107 | localMatrix.preScale(localScale, localScale); |
| 108 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 109 | |
| 110 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale); |
| 111 | ctm.preScale(scale, scale); |
| 112 | localMatrix.setSkew(1, 0); |
| 113 | localMatrix.preScale(localScale, localScale); |
| 114 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 115 | |
| 116 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale); |
| 117 | ctm.preScale(scale, scale); |
| 118 | localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 119 | localMatrix.preRotate(45); |
| 120 | localMatrix.preScale(localScale, localScale); |
| 121 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 122 | } |
| 123 | |
| 124 | void drawTile(SkCanvas* canvas) { |
| 125 | SkPaint paint; |
| 126 | paint.setColor(SK_ColorGREEN); |
| 127 | paint.setStyle(SkPaint::kFill_Style); |
| 128 | paint.setAntiAlias(true); |
| 129 | |
| 130 | canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); |
| 131 | canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, |
| 132 | fTileSize / 2, fTileSize / 2), paint); |
| 133 | |
| 134 | paint.setColor(SK_ColorRED); |
| 135 | canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, |
| 136 | fTileSize / 2, fTileSize * 2 / 3, paint); |
| 137 | canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, |
| 138 | fTileSize * 2 / 3, fTileSize / 2, paint); |
| 139 | } |
| 140 | |
| 141 | void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix, |
| 142 | unsigned tileMode) { |
| 143 | SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs)); |
| 144 | |
| 145 | SkPaint paint; |
| 146 | paint.setStyle(SkPaint::kFill_Style); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 147 | paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 148 | |
| 149 | canvas->save(); |
| 150 | canvas->concat(matrix); |
| 151 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 152 | canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint); |
| 153 | |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 154 | auto pictureShader = SkShader::MakePictureShader(fPicture, kTileConfigs[tileMode].tmx, |
| 155 | kTileConfigs[tileMode].tmy, |
| 156 | fUseLocalMatrixWrapper |
| 157 | ? nullptr : &localMatrix, |
| 158 | nullptr); |
| 159 | paint.setShader(fUseLocalMatrixWrapper |
| 160 | ? pictureShader->makeWithLocalMatrix(localMatrix) |
| 161 | : pictureShader); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 162 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 163 | |
| 164 | canvas->translate(fSceneSize * 1.1f, 0); |
| 165 | |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 166 | auto bitmapShader = SkShader::MakeBitmapShader(fBitmap, |
| 167 | kTileConfigs[tileMode].tmx, |
| 168 | kTileConfigs[tileMode].tmy, |
| 169 | fUseLocalMatrixWrapper |
| 170 | ? nullptr : &localMatrix); |
| 171 | paint.setShader(fUseLocalMatrixWrapper |
| 172 | ? bitmapShader->makeWithLocalMatrix(localMatrix) |
| 173 | : bitmapShader); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 174 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 175 | |
| 176 | canvas->restore(); |
| 177 | } |
| 178 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 179 | sk_sp<SkPicture> fPicture; |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 180 | SkBitmap fBitmap; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 181 | |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 182 | SkScalar fTileSize; |
| 183 | SkScalar fSceneSize; |
| 184 | bool fUseLocalMatrixWrapper; |
| 185 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 186 | typedef GM INHERITED; |
| 187 | }; |
| 188 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 189 | DEF_GM(return new PictureShaderGM(50, 100);) |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 190 | DEF_GM(return new PictureShaderGM(50, 100, true);) |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 191 | |
| 192 | DEF_SIMPLE_GM(tiled_picture_shader, canvas, 400, 400) { |
| 193 | // https://code.google.com/p/skia/issues/detail?id=3398 |
| 194 | SkRect tile = SkRect::MakeWH(100, 100); |
| 195 | |
| 196 | SkPictureRecorder recorder; |
| 197 | SkCanvas* c = recorder.beginRecording(tile); |
| 198 | |
| 199 | SkRect r = tile; |
| 200 | r.inset(4, 4); |
| 201 | SkPaint p; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 202 | p.setColor(sk_tool_utils::color_to_565(0xFF303F9F)); // dark blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 203 | c->drawRect(r, p); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 204 | p.setColor(sk_tool_utils::color_to_565(0xFFC5CAE9)); // light blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 205 | p.setStrokeWidth(10); |
| 206 | c->drawLine(20, 20, 80, 80, p); |
| 207 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 208 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 209 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 210 | p.setColor(sk_tool_utils::color_to_565(0xFF8BC34A)); // green |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 211 | canvas->drawPaint(p); |
| 212 | |
| 213 | canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350)); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 214 | p.setColor(sk_tool_utils::color_to_565(0xFFB6B6B6)); // gray |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 215 | canvas->drawPaint(p); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 216 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 217 | p.setShader(SkShader::MakePictureShader(std::move(picture), SkShader::kRepeat_TileMode, |
| 218 | SkShader::kRepeat_TileMode, |
| 219 | nullptr, nullptr)); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 220 | canvas->drawPaint(p); |
| 221 | } |