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) { |
| 30 | |
| 31 | // Build the picture. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 32 | SkPictureRecorder recorder; |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 33 | SkCanvas* pictureCanvas = recorder.beginRecording(tileSize, tileSize, NULL, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 34 | this->drawTile(pictureCanvas); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 35 | fPicture.reset(recorder.endRecording()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 36 | |
| 37 | // Build a reference bitmap. |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 38 | fBitmap.allocN32Pixels(SkScalarCeilToInt(tileSize), SkScalarCeilToInt(tileSize)); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 39 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
| 40 | SkCanvas bitmapCanvas(fBitmap); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 41 | this->drawTile(&bitmapCanvas); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 45 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 46 | SkString onShortName() SK_OVERRIDE { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 47 | return SkString("pictureshader"); |
| 48 | } |
| 49 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 50 | SkISize onISize() SK_OVERRIDE { |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 51 | return SkISize::Make(1400, 1450); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 55 | 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); |
fmalita | b087879 | 2015-01-15 10:45:56 -0800 | [diff] [blame] | 61 | |
| 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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
| 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.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 151 | SkAutoTUnref<SkShader> pictureShader(SkShader::CreatePictureShader( |
| 152 | fPicture, |
| 153 | kTileConfigs[tileMode].tmx, |
| 154 | kTileConfigs[tileMode].tmy, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 155 | &localMatrix, |
| 156 | NULL)); |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 157 | paint.setShader(pictureShader.get()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 158 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 159 | |
| 160 | canvas->translate(fSceneSize * 1.1f, 0); |
| 161 | |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 162 | 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.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 168 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 169 | |
| 170 | canvas->restore(); |
| 171 | } |
| 172 | |
| 173 | SkScalar fTileSize; |
| 174 | SkScalar fSceneSize; |
| 175 | |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 176 | SkAutoTUnref<SkPicture> fPicture; |
| 177 | SkBitmap fBitmap; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 178 | |
| 179 | typedef GM INHERITED; |
| 180 | }; |
| 181 | |
| 182 | DEF_GM( return SkNEW_ARGS(PictureShaderGM, (50, 100)); ) |