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