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" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 16 | static 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.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 25 | class PictureShaderGM : public skiagm::GM { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 26 | public: |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 27 | PictureShaderGM(SkScalar tileSize, SkScalar sceneSize) |
| 28 | : fTileSize(tileSize) |
| 29 | , fSceneSize(sceneSize) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 30 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 31 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 32 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | void onOnceBeforeDraw() override { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 34 | // Build the picture. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 35 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 37 | this->drawTile(pictureCanvas); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 38 | fPicture = recorder.finishRecordingAsPicture(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 39 | |
| 40 | // Build a reference bitmap. |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 41 | fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize)); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 42 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
| 43 | SkCanvas bitmapCanvas(fBitmap); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 44 | this->drawTile(&bitmapCanvas); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | SkString onShortName() override { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 49 | return SkString("pictureshader"); |
| 50 | } |
| 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | SkISize onISize() override { |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 53 | return SkISize::Make(1400, 1450); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 57 | 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); |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 63 | |
| 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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 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); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 146 | paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 147 | |
| 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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 153 | paint.setShader(SkShader::MakePictureShader(fPicture, kTileConfigs[tileMode].tmx, |
| 154 | kTileConfigs[tileMode].tmy, &localMatrix, |
| 155 | nullptr)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 156 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 157 | |
| 158 | canvas->translate(fSceneSize * 1.1f, 0); |
| 159 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 160 | paint.setShader(SkShader::MakeBitmapShader(fBitmap, |
| 161 | kTileConfigs[tileMode].tmx, |
| 162 | kTileConfigs[tileMode].tmy, |
| 163 | &localMatrix)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 164 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 165 | |
| 166 | canvas->restore(); |
| 167 | } |
| 168 | |
| 169 | SkScalar fTileSize; |
| 170 | SkScalar fSceneSize; |
| 171 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 172 | sk_sp<SkPicture> fPicture; |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 173 | SkBitmap fBitmap; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 174 | |
| 175 | typedef GM INHERITED; |
| 176 | }; |
| 177 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 178 | DEF_GM(return new PictureShaderGM(50, 100);) |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 179 | |
| 180 | DEF_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; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 190 | p.setColor(sk_tool_utils::color_to_565(0xFF303F9F)); // dark blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 191 | c->drawRect(r, p); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 192 | p.setColor(sk_tool_utils::color_to_565(0xFFC5CAE9)); // light blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 193 | p.setStrokeWidth(10); |
| 194 | c->drawLine(20, 20, 80, 80, p); |
| 195 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 196 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 197 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 198 | p.setColor(sk_tool_utils::color_to_565(0xFF8BC34A)); // green |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 199 | canvas->drawPaint(p); |
| 200 | |
| 201 | canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350)); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 202 | p.setColor(sk_tool_utils::color_to_565(0xFFB6B6B6)); // gray |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 203 | canvas->drawPaint(p); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 204 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 205 | p.setShader(SkShader::MakePictureShader(std::move(picture), SkShader::kRepeat_TileMode, |
| 206 | SkShader::kRepeat_TileMode, |
| 207 | nullptr, nullptr)); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 208 | canvas->drawPaint(p); |
| 209 | } |