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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPicture.h" |
| 15 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #include "include/core/SkPoint.h" |
| 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 27 | static struct { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 28 | SkTileMode tmx; |
| 29 | SkTileMode tmy; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 30 | } kTileConfigs[] = { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 31 | { SkTileMode::kRepeat, SkTileMode::kRepeat }, |
| 32 | { SkTileMode::kRepeat, SkTileMode::kClamp }, |
| 33 | { SkTileMode::kMirror, SkTileMode::kRepeat }, |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 36 | class PictureShaderGM : public skiagm::GM { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 37 | public: |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 38 | PictureShaderGM(SkScalar tileSize, SkScalar sceneSize, bool useLocalMatrixWrapper = false) |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 39 | : fTileSize(tileSize) |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 40 | , fSceneSize(sceneSize) |
| 41 | , fUseLocalMatrixWrapper(useLocalMatrixWrapper) {} |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 42 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 43 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onOnceBeforeDraw() override { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 45 | // Build the picture. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 46 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 48 | this->drawTile(pictureCanvas); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 49 | fPicture = recorder.finishRecordingAsPicture(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 50 | |
| 51 | // Build a reference bitmap. |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 52 | fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize)); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 53 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
| 54 | SkCanvas bitmapCanvas(fBitmap); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 55 | this->drawTile(&bitmapCanvas); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 56 | } |
| 57 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 58 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | SkString onShortName() override { |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 60 | return SkStringPrintf("pictureshader%s", fUseLocalMatrixWrapper ? "_localwrapper" : ""); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 61 | } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | SkISize onISize() override { |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 64 | return SkISize::Make(1400, 1450); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 65 | } |
| 66 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 68 | 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); |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 74 | |
| 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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private: |
| 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 Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 157 | paint.setColor(SK_ColorLTGRAY); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 158 | |
| 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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 164 | auto pictureShader = fPicture->makeShader(kTileConfigs[tileMode].tmx, |
| 165 | kTileConfigs[tileMode].tmy, |
| 166 | fUseLocalMatrixWrapper ? nullptr : &localMatrix, |
| 167 | nullptr); |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 168 | paint.setShader(fUseLocalMatrixWrapper |
| 169 | ? pictureShader->makeWithLocalMatrix(localMatrix) |
| 170 | : pictureShader); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 171 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 172 | |
| 173 | canvas->translate(fSceneSize * 1.1f, 0); |
| 174 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 175 | auto bitmapShader = fBitmap.makeShader( |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 176 | 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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 183 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 184 | |
| 185 | canvas->restore(); |
| 186 | } |
| 187 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 188 | sk_sp<SkPicture> fPicture; |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 189 | SkBitmap fBitmap; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 190 | |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 191 | SkScalar fTileSize; |
| 192 | SkScalar fSceneSize; |
| 193 | bool fUseLocalMatrixWrapper; |
| 194 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 195 | typedef GM INHERITED; |
| 196 | }; |
| 197 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 198 | DEF_GM(return new PictureShaderGM(50, 100);) |
Florin Malita | 18f7e0a | 2018-02-06 16:49:40 -0500 | [diff] [blame] | 199 | DEF_GM(return new PictureShaderGM(50, 100, true);) |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 200 | |
| 201 | DEF_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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 211 | p.setColor(ToolUtils::color_to_565(0xFF303F9F)); // dark blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 212 | c->drawRect(r, p); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 213 | p.setColor(ToolUtils::color_to_565(0xFFC5CAE9)); // light blue |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 214 | p.setStrokeWidth(10); |
| 215 | c->drawLine(20, 20, 80, 80, p); |
| 216 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 217 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 218 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 219 | p.setColor(ToolUtils::color_to_565(0xFF8BC34A)); // green |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 220 | canvas->drawPaint(p); |
| 221 | |
| 222 | canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350)); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 223 | p.setColor(0xFFB6B6B6); // gray |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 224 | canvas->drawPaint(p); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 225 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 226 | p.setShader(picture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat)); |
halcanary | 797f58a | 2015-02-05 10:26:27 -0800 | [diff] [blame] | 227 | canvas->drawPaint(p); |
| 228 | } |