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" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 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 | |
| 26 | class PictureShaderGM : public GM { |
| 27 | public: |
| 28 | |
| 29 | PictureShaderGM(SkScalar tileSize, SkScalar sceneSize) |
| 30 | : fTileSize(tileSize) |
| 31 | , fSceneSize(sceneSize) { |
| 32 | |
| 33 | // Build the picture. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 34 | SkPictureRecorder recorder; |
| 35 | SkCanvas* pictureCanvas = recorder.beginRecording(SkScalarRoundToInt(tileSize), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 36 | SkScalarRoundToInt(tileSize), |
| 37 | NULL, 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 38 | this->drawTile(pictureCanvas); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 39 | SkAutoTUnref<SkPicture> p(recorder.endRecording()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 40 | |
| 41 | // Build a reference bitmap. |
| 42 | SkBitmap bm; |
| 43 | bm.allocN32Pixels(SkScalarRoundToInt(tileSize), SkScalarRoundToInt(tileSize)); |
| 44 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 45 | SkCanvas bitmapCanvas(bm); |
| 46 | this->drawTile(&bitmapCanvas); |
| 47 | |
| 48 | for (unsigned i = 0; i < SK_ARRAY_COUNT(kTileConfigs); ++i) { |
| 49 | fPictureShaders[i].reset(SkShader::CreatePictureShader(p, |
| 50 | kTileConfigs[i].tmx, |
| 51 | kTileConfigs[i].tmy)); |
| 52 | |
| 53 | fBitmapShaders[i].reset(SkShader::CreateBitmapShader(bm, |
| 54 | kTileConfigs[i].tmx, |
| 55 | kTileConfigs[i].tmy)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | virtual SkString onShortName() SK_OVERRIDE { |
| 61 | return SkString("pictureshader"); |
| 62 | } |
| 63 | |
| 64 | virtual SkISize onISize() SK_OVERRIDE { |
| 65 | return SkISize::Make(1400, 1250); |
| 66 | } |
| 67 | |
| 68 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 69 | this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0); |
| 70 | this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0); |
| 71 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1); |
| 72 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2); |
| 73 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0); |
| 74 | this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale, |
| 79 | unsigned tileMode) { |
| 80 | SkMatrix ctm, localMatrix; |
| 81 | |
| 82 | ctm.setTranslate(pos.x(), pos.y()); |
| 83 | ctm.preScale(scale, scale); |
| 84 | localMatrix.setScale(localScale, localScale); |
| 85 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 86 | |
| 87 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale); |
| 88 | ctm.preScale(scale, scale); |
| 89 | localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 90 | localMatrix.preScale(localScale, localScale); |
| 91 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 92 | |
| 93 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale); |
| 94 | ctm.preScale(scale, scale); |
| 95 | localMatrix.setRotate(45); |
| 96 | localMatrix.preScale(localScale, localScale); |
| 97 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 98 | |
| 99 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale); |
| 100 | ctm.preScale(scale, scale); |
| 101 | localMatrix.setSkew(1, 0); |
| 102 | localMatrix.preScale(localScale, localScale); |
| 103 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 104 | |
| 105 | ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale); |
| 106 | ctm.preScale(scale, scale); |
| 107 | localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 108 | localMatrix.preRotate(45); |
| 109 | localMatrix.preScale(localScale, localScale); |
| 110 | this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 111 | } |
| 112 | |
| 113 | void drawTile(SkCanvas* canvas) { |
| 114 | SkPaint paint; |
| 115 | paint.setColor(SK_ColorGREEN); |
| 116 | paint.setStyle(SkPaint::kFill_Style); |
| 117 | paint.setAntiAlias(true); |
| 118 | |
| 119 | canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); |
| 120 | canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, |
| 121 | fTileSize / 2, fTileSize / 2), paint); |
| 122 | |
| 123 | paint.setColor(SK_ColorRED); |
| 124 | canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, |
| 125 | fTileSize / 2, fTileSize * 2 / 3, paint); |
| 126 | canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, |
| 127 | fTileSize * 2 / 3, fTileSize / 2, paint); |
| 128 | } |
| 129 | |
| 130 | void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix, |
| 131 | unsigned tileMode) { |
| 132 | SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs)); |
| 133 | |
| 134 | SkPaint paint; |
| 135 | paint.setStyle(SkPaint::kFill_Style); |
| 136 | paint.setColor(SK_ColorLTGRAY); |
| 137 | |
| 138 | canvas->save(); |
| 139 | canvas->concat(matrix); |
| 140 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 141 | canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint); |
| 142 | |
| 143 | fPictureShaders[tileMode]->setLocalMatrix(localMatrix); |
| 144 | paint.setShader(fPictureShaders[tileMode].get()); |
| 145 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 146 | |
| 147 | canvas->translate(fSceneSize * 1.1f, 0); |
| 148 | |
| 149 | fBitmapShaders[tileMode]->setLocalMatrix(localMatrix); |
| 150 | paint.setShader(fBitmapShaders[tileMode].get()); |
| 151 | canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 152 | |
| 153 | canvas->restore(); |
| 154 | } |
| 155 | |
| 156 | SkScalar fTileSize; |
| 157 | SkScalar fSceneSize; |
| 158 | |
| 159 | SkAutoTUnref<SkShader> fPictureShaders[SK_ARRAY_COUNT(kTileConfigs)]; |
| 160 | SkAutoTUnref<SkShader> fBitmapShaders[SK_ARRAY_COUNT(kTileConfigs)]; |
| 161 | |
| 162 | typedef GM INHERITED; |
| 163 | }; |
| 164 | |
| 165 | DEF_GM( return SkNEW_ARGS(PictureShaderGM, (50, 100)); ) |
| 166 | } |