bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +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 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkGradientShader.h" |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 10 | |
| 11 | namespace skiagm { |
| 12 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 13 | static void makebm(SkBitmap* bm, int w, int h) { |
| 14 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 15 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 16 | |
| 17 | SkCanvas canvas(*bm); |
| 18 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
| 19 | static const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; |
| 20 | static const SkPoint kPts1[] = { { s, 0 }, { 0, s } }; |
| 21 | static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 22 | static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF }; |
| 23 | static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF }; |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 25 | |
| 26 | SkPaint paint; |
| 27 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 28 | paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 29 | SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref(); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 30 | canvas.drawPaint(paint); |
| 31 | paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos, |
| 32 | SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref(); |
| 33 | canvas.drawPaint(paint); |
| 34 | } |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | struct LabeledMatrix { |
| 39 | SkMatrix fMatrix; |
| 40 | const char* fLabel; |
| 41 | }; |
| 42 | |
| 43 | class ShaderText2GM : public GM { |
| 44 | public: |
| 45 | ShaderText2GM() { |
| 46 | this->setBGColor(0xFFDDDDDD); |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | |
| 51 | SkString onShortName() { |
| 52 | return SkString("shadertext2"); |
| 53 | } |
| 54 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 55 | SkISize onISize() { return SkISize::Make(1800, 900); } |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 56 | |
| 57 | virtual void onDraw(SkCanvas* canvas) { |
| 58 | static const char kText[] = "SKIA"; |
| 59 | static const int kTextLen = SK_ARRAY_COUNT(kText) - 1; |
| 60 | static const int kPointSize = 55; |
| 61 | |
| 62 | SkTDArray<LabeledMatrix> matrices; |
| 63 | matrices.append()->fMatrix.reset(); |
| 64 | matrices.top().fLabel = "Identity"; |
| 65 | matrices.append()->fMatrix.setScale(1.2f, 0.8f); |
| 66 | matrices.top().fLabel = "Scale"; |
| 67 | matrices.append()->fMatrix.setRotate(10.f); |
| 68 | matrices.top().fLabel = "Rotate"; |
| 69 | matrices.append()->fMatrix.reset(); |
| 70 | matrices.top().fMatrix.setPerspX(-0.0015f); |
| 71 | matrices.top().fMatrix.setPerspY(+0.0015f); |
| 72 | matrices.top().fLabel = "Persp"; |
| 73 | |
| 74 | SkTDArray<LabeledMatrix> localMatrices; |
| 75 | localMatrices.append()->fMatrix.reset(); |
| 76 | localMatrices.top().fLabel = "Identity"; |
| 77 | localMatrices.append()->fMatrix.setScale(2.5f, 0.2f); |
| 78 | localMatrices.top().fLabel = "Scale"; |
| 79 | localMatrices.append()->fMatrix.setRotate(45.f); |
| 80 | localMatrices.top().fLabel = "Rotate"; |
| 81 | localMatrices.append()->fMatrix.reset(); |
| 82 | localMatrices.top().fMatrix.setPerspX(-0.007f); |
| 83 | localMatrices.top().fMatrix.setPerspY(+0.008f); |
| 84 | localMatrices.top().fLabel = "Persp"; |
| 85 | |
| 86 | static SkBitmap bmp; |
| 87 | if (bmp.isNull()) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 88 | makebm(&bmp, kPointSize / 2, kPointSize / 2); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 89 | } |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 90 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 91 | SkPaint fillPaint; |
| 92 | fillPaint.setAntiAlias(true); |
| 93 | fillPaint.setTextSize(SkIntToScalar(kPointSize)); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 94 | fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 95 | |
| 96 | SkPaint outlinePaint; |
| 97 | outlinePaint.setAntiAlias(true); |
| 98 | outlinePaint.setTextSize(SkIntToScalar(kPointSize)); |
| 99 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 100 | outlinePaint.setStrokeWidth(0.f); |
| 101 | |
| 102 | SkScalar w = fillPaint.measureText(kText, kTextLen); |
| 103 | static SkScalar kPadY = 0.5f * kPointSize; |
| 104 | static SkScalar kPadX = 1.5f * kPointSize; |
| 105 | |
| 106 | SkPaint strokePaint(fillPaint); |
| 107 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 108 | strokePaint.setStrokeWidth(kPointSize * 0.1f); |
| 109 | |
| 110 | SkPaint labelPaint; |
| 111 | labelPaint.setColor(0xff000000); |
| 112 | labelPaint.setAntiAlias(true); |
| 113 | labelPaint.setTextSize(12.f); |
| 114 | |
| 115 | canvas->translate(15.f, 15.f); |
| 116 | canvas->drawBitmap(bmp, 0, 0); |
| 117 | canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f); |
| 118 | |
| 119 | static const char kLabelLabel[] = "localM / canvasM"; |
| 120 | canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint); |
| 121 | canvas->translate(0, 15.f); |
| 122 | |
| 123 | canvas->save(); |
| 124 | SkScalar maxLabelW = 0; |
| 125 | canvas->translate(0, kPadY / 2 + kPointSize); |
| 126 | for (int lm = 0; lm < localMatrices.count(); ++lm) { |
| 127 | canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel), |
| 128 | 0, labelPaint.getTextSize() - 1, labelPaint); |
| 129 | SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel, |
| 130 | strlen(matrices[lm].fLabel)); |
| 131 | maxLabelW = SkMaxScalar(maxLabelW, labelW); |
| 132 | canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY); |
| 133 | } |
| 134 | canvas->restore(); |
| 135 | |
| 136 | canvas->translate(maxLabelW + kPadX / 2.f, 0.f); |
| 137 | |
| 138 | for (int s = 0; s < 2; ++s) { |
| 139 | SkPaint& paint = s ? strokePaint : fillPaint; |
| 140 | |
commit-bot@chromium.org | 35d4872 | 2014-02-13 18:36:36 +0000 | [diff] [blame] | 141 | SkScalar columnH = 0; |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 142 | for (int m = 0; m < matrices.count(); ++m) { |
| 143 | columnH = 0; |
| 144 | canvas->save(); |
| 145 | canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel), |
| 146 | 0, labelPaint.getTextSize() - 1, labelPaint); |
| 147 | canvas->translate(0, kPadY / 2 + kPointSize); |
| 148 | columnH += kPadY / 2 + kPointSize; |
| 149 | for (int lm = 0; lm < localMatrices.count(); ++lm) { |
commit-bot@chromium.org | bd09045 | 2014-04-29 21:46:59 +0000 | [diff] [blame] | 150 | paint.setShader( |
| 151 | SkShader::CreateBitmapShader(bmp, |
| 152 | SkShader::kMirror_TileMode, |
| 153 | SkShader::kRepeat_TileMode, |
| 154 | &localMatrices[lm].fMatrix))->unref(); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 155 | |
| 156 | canvas->save(); |
| 157 | canvas->concat(matrices[m].fMatrix); |
| 158 | canvas->drawText(kText, kTextLen, 0, 0, paint); |
| 159 | canvas->drawText(kText, kTextLen, 0, 0, outlinePaint); |
| 160 | canvas->restore(); |
| 161 | |
| 162 | SkPath path; |
| 163 | path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f, |
| 164 | 1.2f * w, 2.f * kPointSize), |
| 165 | 225.f, 359.f, |
| 166 | false); |
| 167 | path.close(); |
| 168 | |
| 169 | canvas->translate(0.f, kPointSize + kPadY); |
| 170 | columnH += kPointSize + kPadY; |
| 171 | |
| 172 | canvas->save(); |
| 173 | canvas->concat(matrices[m].fMatrix); |
| 174 | canvas->drawTextOnPath(kText, kTextLen, path, NULL, paint); |
| 175 | canvas->drawTextOnPath(kText, kTextLen, path, NULL, outlinePaint); |
| 176 | canvas->restore(); |
| 177 | SkPaint stroke; |
| 178 | stroke.setStyle(SkPaint::kStroke_Style); |
| 179 | canvas->translate(0.f, kPointSize + kPadY); |
| 180 | columnH += kPointSize + kPadY; |
| 181 | } |
| 182 | canvas->restore(); |
| 183 | canvas->translate(w + kPadX, 0.f); |
| 184 | } |
| 185 | if (0 == s) { |
| 186 | canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint); |
| 187 | canvas->translate(kPadX / 2, 0.f); |
| 188 | static const char kFillLabel[] = "Filled"; |
| 189 | static const char kStrokeLabel[] = "Stroked"; |
| 190 | SkScalar y = columnH + kPadY / 2; |
| 191 | SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX; |
| 192 | SkScalar strokeX = kPadX; |
| 193 | canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint); |
| 194 | canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
reed@google.com | 1b6c73d | 2012-10-10 15:17:24 +0000 | [diff] [blame] | 199 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 200 | // disable 565 for now, til mike fixes the debug assert |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 201 | return kSkip565_Flag | kSkipTiled_Flag; |
reed@google.com | 1b6c73d | 2012-10-10 15:17:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 204 | private: |
| 205 | typedef GM INHERITED; |
| 206 | }; |
| 207 | |
| 208 | /////////////////////////////////////////////////////////////////////////////// |
| 209 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 210 | static GM* MyFactory(void*) { return new ShaderText2GM; } |
| 211 | static GMRegistry reg(MyFactory); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 212 | } |