OLD | NEW |
(Empty) | |
| 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. |
| 34 SkAutoTUnref<SkPicture> p(SkNEW(SkPicture)); |
| 35 SkCanvas* pictureCanvas = p->beginRecording(SkScalarRoundToInt(tileSize)
, |
| 36 SkScalarRoundToInt(tileSize)
); |
| 37 this->drawTile(pictureCanvas); |
| 38 p->endRecording(); |
| 39 |
| 40 // Build a reference bitmap. |
| 41 SkBitmap bm; |
| 42 bm.allocN32Pixels(SkScalarRoundToInt(tileSize), SkScalarRoundToInt(tileS
ize)); |
| 43 bm.eraseColor(SK_ColorTRANSPARENT); |
| 44 SkCanvas bitmapCanvas(bm); |
| 45 this->drawTile(&bitmapCanvas); |
| 46 |
| 47 for (unsigned i = 0; i < SK_ARRAY_COUNT(kTileConfigs); ++i) { |
| 48 fPictureShaders[i].reset(SkShader::CreatePictureShader(p, |
| 49 kTileConfigs[
i].tmx, |
| 50 kTileConfigs[
i].tmy)); |
| 51 |
| 52 fBitmapShaders[i].reset(SkShader::CreateBitmapShader(bm, |
| 53 kTileConfigs[i]
.tmx, |
| 54 kTileConfigs[i]
.tmy)); |
| 55 } |
| 56 } |
| 57 |
| 58 protected: |
| 59 virtual SkString onShortName() SK_OVERRIDE { |
| 60 return SkString("pictureshader"); |
| 61 } |
| 62 |
| 63 virtual SkISize onISize() SK_OVERRIDE { |
| 64 return SkISize::Make(1400, 1250); |
| 65 } |
| 66 |
| 67 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 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, fSceneSiz
e * 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); |
| 74 } |
| 75 |
| 76 private: |
| 77 void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, S
kScalar localScale, |
| 78 unsigned tileMode) { |
| 79 SkMatrix ctm, localMatrix; |
| 80 |
| 81 ctm.setTranslate(pos.x(), pos.y()); |
| 82 ctm.preScale(scale, scale); |
| 83 localMatrix.setScale(localScale, localScale); |
| 84 this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 85 |
| 86 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale); |
| 87 ctm.preScale(scale, scale); |
| 88 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 89 localMatrix.preScale(localScale, localScale); |
| 90 this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 91 |
| 92 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale); |
| 93 ctm.preScale(scale, scale); |
| 94 localMatrix.setRotate(45); |
| 95 localMatrix.preScale(localScale, localScale); |
| 96 this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 97 |
| 98 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale); |
| 99 ctm.preScale(scale, scale); |
| 100 localMatrix.setSkew(1, 0); |
| 101 localMatrix.preScale(localScale, localScale); |
| 102 this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 103 |
| 104 ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale); |
| 105 ctm.preScale(scale, scale); |
| 106 localMatrix.setTranslate(fTileSize / 4, fTileSize / 4); |
| 107 localMatrix.preRotate(45); |
| 108 localMatrix.preScale(localScale, localScale); |
| 109 this->drawScene(canvas, ctm, localMatrix, tileMode); |
| 110 } |
| 111 |
| 112 void drawTile(SkCanvas* canvas) { |
| 113 SkPaint paint; |
| 114 paint.setColor(SK_ColorGREEN); |
| 115 paint.setStyle(SkPaint::kFill_Style); |
| 116 paint.setAntiAlias(true); |
| 117 |
| 118 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); |
| 119 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, |
| 120 fTileSize / 2, fTileSize / 2), paint); |
| 121 |
| 122 paint.setColor(SK_ColorRED); |
| 123 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, |
| 124 fTileSize / 2, fTileSize * 2 / 3, paint); |
| 125 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, |
| 126 fTileSize * 2 / 3, fTileSize / 2, paint); |
| 127 } |
| 128 |
| 129 void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& loc
alMatrix, |
| 130 unsigned tileMode) { |
| 131 SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs)); |
| 132 |
| 133 SkPaint paint; |
| 134 paint.setStyle(SkPaint::kFill_Style); |
| 135 paint.setColor(SK_ColorLTGRAY); |
| 136 |
| 137 canvas->save(); |
| 138 canvas->concat(matrix); |
| 139 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 140 canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSce
neSize), paint); |
| 141 |
| 142 fPictureShaders[tileMode]->setLocalMatrix(localMatrix); |
| 143 paint.setShader(fPictureShaders[tileMode].get()); |
| 144 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 145 |
| 146 canvas->translate(fSceneSize * 1.1f, 0); |
| 147 |
| 148 fBitmapShaders[tileMode]->setLocalMatrix(localMatrix); |
| 149 paint.setShader(fBitmapShaders[tileMode].get()); |
| 150 canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint); |
| 151 |
| 152 canvas->restore(); |
| 153 } |
| 154 |
| 155 SkScalar fTileSize; |
| 156 SkScalar fSceneSize; |
| 157 |
| 158 SkAutoTUnref<SkShader> fPictureShaders[SK_ARRAY_COUNT(kTileConfigs)]; |
| 159 SkAutoTUnref<SkShader> fBitmapShaders[SK_ARRAY_COUNT(kTileConfigs)]; |
| 160 |
| 161 typedef GM INHERITED; |
| 162 }; |
| 163 |
| 164 DEF_GM( return SkNEW_ARGS(PictureShaderGM, (50, 100)); ) |
| 165 } |
OLD | NEW |