bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 11 | |
| 12 | namespace skiagm { |
| 13 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 14 | static void makebm(SkBitmap* bm, int w, int h) { |
| 15 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 16 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 17 | |
| 18 | SkCanvas canvas(*bm); |
| 19 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
| 20 | static const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; |
| 21 | static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } }; |
| 22 | static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 23 | static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 }; |
| 24 | static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 26 | |
| 27 | SkPaint paint; |
| 28 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 29 | paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 30 | SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref(); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 31 | canvas.drawPaint(paint); |
| 32 | paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos, |
| 33 | SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref(); |
| 34 | canvas.drawPaint(paint); |
| 35 | } |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | struct LabeledMatrix { |
| 40 | SkMatrix fMatrix; |
| 41 | const char* fLabel; |
| 42 | }; |
| 43 | |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 44 | static const char kText[] = "B"; |
| 45 | static const int kTextLen = SK_ARRAY_COUNT(kText) - 1; |
| 46 | static const int kPointSize = 300; |
| 47 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 48 | class ShaderText3GM : public GM { |
| 49 | public: |
| 50 | ShaderText3GM() { |
| 51 | this->setBGColor(0xFFDDDDDD); |
| 52 | } |
| 53 | |
| 54 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 55 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 56 | return kSkipTiled_Flag; |
| 57 | } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 58 | |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 59 | virtual SkString onShortName() SK_OVERRIDE { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 60 | return SkString("shadertext3"); |
| 61 | } |
| 62 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 63 | virtual SkISize onISize() SK_OVERRIDE{ return SkISize::Make(800, 1000); } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 65 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 66 | makebm(&fBmp, kPointSize / 4, kPointSize / 4); |
| 67 | } |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 69 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 70 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 71 | SkPaint bmpPaint; |
| 72 | bmpPaint.setAntiAlias(true); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 73 | bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 74 | bmpPaint.setAlpha(0x80); |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 75 | canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 76 | |
| 77 | SkPaint outlinePaint; |
| 78 | outlinePaint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 79 | sk_tool_utils::set_portable_typeface(&outlinePaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 80 | outlinePaint.setTextSize(SkIntToScalar(kPointSize)); |
| 81 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 82 | outlinePaint.setStrokeWidth(0.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 84 | canvas->translate(15.f, 15.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 85 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 86 | // draw glyphs scaled up |
| 87 | canvas->scale(2.f, 2.f); |
| 88 | |
| 89 | static const SkShader::TileMode kTileModes[] = { |
| 90 | SkShader::kRepeat_TileMode, |
| 91 | SkShader::kMirror_TileMode, |
| 92 | }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 94 | // position the baseline of the first run |
| 95 | canvas->translate(0.f, 0.75f * kPointSize); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 97 | canvas->save(); |
| 98 | int i = 0; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 99 | for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 100 | for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 101 | SkMatrix localM; |
| 102 | localM.setTranslate(5.f, 5.f); |
| 103 | localM.postRotate(20); |
| 104 | localM.postScale(1.15f, .85f); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 105 | |
| 106 | SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(fBmp, |
| 107 | kTileModes[tm0], |
| 108 | kTileModes[tm1], |
| 109 | &localM)); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 110 | |
| 111 | SkPaint fillPaint; |
| 112 | fillPaint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 113 | sk_tool_utils::set_portable_typeface(&fillPaint); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 114 | fillPaint.setTextSize(SkIntToScalar(kPointSize)); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 115 | fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 116 | fillPaint.setShader(shader); |
| 117 | |
| 118 | canvas->drawText(kText, kTextLen, 0, 0, fillPaint); |
| 119 | canvas->drawText(kText, kTextLen, 0, 0, outlinePaint); |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 120 | SkScalar w = fillPaint.measureText(kText, kTextLen); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 121 | canvas->translate(w + 10.f, 0.f); |
| 122 | ++i; |
| 123 | if (!(i % 2)) { |
| 124 | canvas->restore(); |
| 125 | canvas->translate(0, 0.75f * kPointSize); |
| 126 | canvas->save(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | canvas->restore(); |
| 131 | } |
| 132 | |
| 133 | private: |
commit-bot@chromium.org | a54abdc | 2014-03-26 21:21:40 +0000 | [diff] [blame] | 134 | SkBitmap fBmp; |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 135 | typedef GM INHERITED; |
| 136 | }; |
| 137 | |
| 138 | /////////////////////////////////////////////////////////////////////////////// |
| 139 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 140 | static GM* MyFactory(void*) { return new ShaderText3GM; } |
| 141 | static GMRegistry reg(MyFactory); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 142 | } |