epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkUnitMappers.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { |
| 16 | bm->setConfig(config, w, h); |
| 17 | bm->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 18 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 19 | |
| 20 | SkCanvas canvas(*bm); |
epoger@google.com | b28b5e4 | 2011-05-24 14:51:57 +0000 | [diff] [blame] | 21 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
reed@google.com | 3869007 | 2011-01-26 01:44:18 +0000 | [diff] [blame] | 22 | SkPoint pts[] = { { 0, 0 }, { s, s } }; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 23 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 24 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 25 | SkPaint paint; |
| 26 | |
| 27 | SkUnitMapper* um = NULL; |
| 28 | |
| 29 | um = new SkCosineMapper; |
| 30 | |
| 31 | SkAutoUnref au(um); |
| 32 | |
| 33 | paint.setDither(true); |
| 34 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
| 35 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); |
| 36 | canvas.drawPaint(paint); |
| 37 | } |
| 38 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 39 | static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty, |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 40 | int w, int h) { |
| 41 | static SkBitmap bmp; |
| 42 | if (bmp.isNull()) { |
| 43 | makebm(&bmp, SkBitmap::kARGB_8888_Config, w/2, h/4); |
| 44 | } |
| 45 | return SkShader::CreateBitmapShader(bmp, tx, ty); |
| 46 | } |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | struct GradData { |
| 51 | int fCount; |
| 52 | const SkColor* fColors; |
| 53 | const SkScalar* fPos; |
| 54 | }; |
| 55 | |
| 56 | static const SkColor gColors[] = { |
| 57 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 58 | }; |
| 59 | |
| 60 | static const GradData gGradData[] = { |
| 61 | { 2, gColors, NULL }, |
| 62 | { 5, gColors, NULL }, |
| 63 | }; |
| 64 | |
| 65 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 66 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 67 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 68 | data.fCount, tm, mapper); |
| 69 | } |
| 70 | |
| 71 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 72 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 73 | SkPoint center; |
| 74 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 75 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 76 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 77 | data.fPos, data.fCount, tm, mapper); |
| 78 | } |
| 79 | |
| 80 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
sugoi@google.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 81 | SkShader::TileMode, SkUnitMapper* mapper) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 82 | SkPoint center; |
| 83 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 84 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 85 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 86 | data.fPos, data.fCount, mapper); |
| 87 | } |
| 88 | |
| 89 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 90 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 91 | SkPoint center0, center1; |
| 92 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 93 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 94 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 95 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 96 | return SkGradientShader::CreateTwoPointRadial( |
| 97 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 98 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 99 | data.fColors, data.fPos, data.fCount, tm, mapper); |
| 100 | } |
| 101 | |
| 102 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 103 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 104 | static const GradMaker gGradMakers[] = { |
| 105 | MakeLinear, MakeRadial, MakeSweep, Make2Radial |
| 106 | }; |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | class ShaderTextGM : public GM { |
| 111 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | ShaderTextGM() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 113 | this->setBGColor(0xFFDDDDDD); |
| 114 | } |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 115 | |
| 116 | protected: |
| 117 | |
| 118 | SkString onShortName() { |
| 119 | return SkString("shadertext"); |
| 120 | } |
| 121 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | SkISize onISize() { return make_isize(1450, 500); } |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 123 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 124 | virtual void onDraw(SkCanvas* canvas) { |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 125 | const char text[] = "Shaded Text"; |
| 126 | const int textLen = SK_ARRAY_COUNT(text) - 1; |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 127 | const int pointSize = 36; |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 128 | |
| 129 | int w = pointSize * textLen; |
| 130 | int h = pointSize; |
| 131 | |
| 132 | SkPoint pts[2] = { |
| 133 | { 0, 0 }, |
| 134 | { SkIntToScalar(w), SkIntToScalar(h) } |
| 135 | }; |
| 136 | SkScalar textBase = SkIntToScalar(h/2); |
| 137 | |
| 138 | SkShader::TileMode tileModes[] = { |
| 139 | SkShader::kClamp_TileMode, |
| 140 | SkShader::kRepeat_TileMode, |
| 141 | SkShader::kMirror_TileMode |
| 142 | }; |
| 143 | |
| 144 | static const int gradCount = SK_ARRAY_COUNT(gGradData) * |
| 145 | SK_ARRAY_COUNT(gGradMakers); |
| 146 | static const int bmpCount = SK_ARRAY_COUNT(tileModes) * |
| 147 | SK_ARRAY_COUNT(tileModes); |
| 148 | SkShader* shaders[gradCount + bmpCount]; |
| 149 | |
| 150 | int shdIdx = 0; |
| 151 | for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { |
| 152 | for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { |
| 153 | shaders[shdIdx++] = gGradMakers[m](pts, |
| 154 | gGradData[d], |
| 155 | SkShader::kClamp_TileMode, |
| 156 | NULL); |
| 157 | } |
| 158 | } |
| 159 | for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { |
| 160 | for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { |
| 161 | shaders[shdIdx++] = MakeBitmapShader(tileModes[tx], |
| 162 | tileModes[ty], |
| 163 | w/8, h); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | SkPaint paint; |
| 168 | paint.setDither(true); |
| 169 | paint.setAntiAlias(true); |
| 170 | paint.setTextSize(SkIntToScalar(pointSize)); |
| 171 | |
| 172 | canvas->save(); |
| 173 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
| 174 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 175 | SkPath path; |
bsalomon@google.com | 137c428 | 2011-06-14 17:13:22 +0000 | [diff] [blame] | 176 | path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15), |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 177 | SkIntToScalar(300), SkIntToScalar(90)), |
| 178 | SkIntToScalar(225), SkIntToScalar(90), |
bsalomon@google.com | 137c428 | 2011-06-14 17:13:22 +0000 | [diff] [blame] | 179 | false); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 180 | path.close(); |
| 181 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 182 | static const int testsPerCol = 8; |
| 183 | static const int rowHeight = 60; |
| 184 | static const int colWidth = 300; |
| 185 | canvas->save(); |
| 186 | for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) { |
| 187 | canvas->save(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 188 | int i = 2*s; |
| 189 | canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
| 190 | SkIntToScalar((i % testsPerCol) * rowHeight)); |
| 191 | paint.setShader(shaders[s])->unref(); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 192 | canvas->drawText(text, textLen, 0, textBase, paint); |
| 193 | canvas->restore(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 194 | canvas->save(); |
| 195 | ++i; |
| 196 | canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
| 197 | SkIntToScalar((i % testsPerCol) * rowHeight)); |
| 198 | canvas->drawTextOnPath(text, textLen, path, NULL, paint); |
| 199 | canvas->restore(); |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 200 | } |
| 201 | canvas->restore(); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private: |
| 206 | typedef GM INHERITED; |
| 207 | }; |
| 208 | |
| 209 | /////////////////////////////////////////////////////////////////////////////// |
| 210 | |
borenet@google.com | ceb2a03 | 2012-06-25 21:03:04 +0000 | [diff] [blame] | 211 | #ifndef SK_BUILD_FOR_ANDROID |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 212 | static GM* MyFactory(void*) { return new ShaderTextGM; } |
| 213 | static GMRegistry reg(MyFactory); |
borenet@google.com | ceb2a03 | 2012-06-25 21:03:04 +0000 | [diff] [blame] | 214 | #endif |
bsalomon@google.com | 0fdaa22 | 2011-01-25 18:35:26 +0000 | [diff] [blame] | 215 | } |