epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 7 | #include "Sample.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 10 | #include "SkMaskFilter.h" |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 11 | #include "SkParsePath.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRandom.h" |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 14 | |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 15 | |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 16 | static void test_huge_stroke(SkCanvas* canvas) { |
| 17 | SkRect srcR = { 0, 0, 72000, 54000 }; |
| 18 | SkRect dstR = { 0, 0, 640, 480 }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 20 | SkPath path; |
| 21 | path.moveTo(17600, 8000); |
| 22 | path.lineTo(52800, 8000); |
| 23 | path.lineTo(52800, 41600); |
| 24 | path.lineTo(17600, 41600); |
| 25 | path.close(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setAntiAlias(true); |
| 29 | paint.setStrokeWidth(8000); |
| 30 | paint.setStrokeMiter(10); |
| 31 | paint.setStrokeCap(SkPaint::kButt_Cap); |
| 32 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 33 | paint.setStyle(SkPaint::kStroke_Style); |
| 34 | |
| 35 | SkMatrix matrix; |
| 36 | matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit); |
| 37 | canvas->concat(matrix); |
| 38 | |
| 39 | canvas->drawPath(path, paint); |
| 40 | } |
| 41 | |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 42 | #if 0 |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 43 | static void test_blur() { |
| 44 | uint8_t cell[9]; |
| 45 | memset(cell, 0xFF, sizeof(cell)); |
| 46 | SkMask src; |
| 47 | src.fImage = cell; |
| 48 | src.fFormat = SkMask::kA8_Format; |
| 49 | SkMask dst; |
| 50 | |
| 51 | for (int y = 1; y <= 3; y++) { |
| 52 | for (int x = 1; x <= 3; x++) { |
| 53 | src.fBounds.set(0, 0, x, y); |
| 54 | src.fRowBytes = src.fBounds.width(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 56 | SkScalar radius = 1.f; |
| 57 | |
| 58 | printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop, |
| 59 | src.fBounds.fRight, src.fBounds.fBottom, radius); |
| 60 | |
| 61 | SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style); |
| 62 | uint8_t* dstPtr = dst.fImage; |
| 63 | |
| 64 | for (int y = 0; y < dst.fBounds.height(); y++) { |
| 65 | for (int x = 0; x < dst.fBounds.width(); x++) { |
| 66 | printf(" %02X", dstPtr[x]); |
| 67 | } |
| 68 | printf("\n"); |
| 69 | dstPtr += dst.fRowBytes; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 74 | #endif |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 75 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 76 | static void scale_to_width(SkPath* path, SkScalar dstWidth) { |
| 77 | const SkRect& bounds = path->getBounds(); |
| 78 | SkScalar scale = dstWidth / bounds.width(); |
| 79 | SkMatrix matrix; |
| 80 | |
| 81 | matrix.setScale(scale, scale); |
| 82 | path->transform(matrix); |
| 83 | } |
| 84 | |
| 85 | static const struct { |
| 86 | SkPaint::Style fStyle; |
| 87 | SkPaint::Join fJoin; |
| 88 | int fStrokeWidth; |
| 89 | } gRec[] = { |
| 90 | { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 }, |
| 91 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 }, |
| 92 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 }, |
| 93 | { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 }, |
| 94 | }; |
| 95 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 96 | class StrokePathView : public Sample { |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 97 | SkScalar fWidth; |
| 98 | SkPath fPath; |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 99 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | void onOnceBeforeDraw() override { |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 101 | // test_blur(); |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 102 | fWidth = SkIntToScalar(120); |
| 103 | |
| 104 | #if 0 |
| 105 | const char str[] = |
| 106 | "M 0, 3" |
| 107 | "C 10, -10, 30, -10, 0, 28" |
| 108 | "C -30, -10, -10, -10, 0, 3" |
| 109 | "Z"; |
| 110 | SkParsePath::FromSVGString(str, &fPath); |
| 111 | #else |
| 112 | fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction); |
| 113 | fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction); |
| 114 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 116 | scale_to_width(&fPath, fWidth); |
| 117 | const SkRect& bounds = fPath.getBounds(); |
| 118 | fPath.offset(-bounds.fLeft, -bounds.fTop); |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 119 | |
| 120 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 121 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 123 | bool onQuery(Sample::Event* evt) override { |
| 124 | if (Sample::TitleQ(*evt)) { |
| 125 | Sample::TitleR(evt, "StrokePath"); |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | return this->INHERITED::onQuery(evt); |
| 129 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 131 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 133 | void drawSet(SkCanvas* canvas, SkPaint* paint) { |
| 134 | SkAutoCanvasRestore acr(canvas, true); |
| 135 | |
| 136 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 137 | paint->setStyle(gRec[i].fStyle); |
| 138 | paint->setStrokeJoin(gRec[i].fJoin); |
| 139 | paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth)); |
| 140 | canvas->drawPath(fPath, *paint); |
| 141 | canvas->translate(fWidth * 5 / 4, 0); |
| 142 | } |
| 143 | } |
| 144 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 145 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 146 | test_huge_stroke(canvas); return; |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 147 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 148 | |
| 149 | SkPaint paint; |
| 150 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 152 | if (true) { |
| 153 | canvas->drawColor(SK_ColorBLACK); |
| 154 | |
| 155 | paint.setTextSize(24); |
| 156 | paint.setColor(SK_ColorWHITE); |
| 157 | canvas->translate(10, 30); |
| 158 | |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 159 | static const SkBlurStyle gStyle[] = { |
| 160 | kNormal_SkBlurStyle, |
| 161 | kInner_SkBlurStyle, |
| 162 | kOuter_SkBlurStyle, |
| 163 | kSolid_SkBlurStyle, |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 164 | }; |
| 165 | for (int x = 0; x < 5; x++) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 166 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 167 | for (int y = 0; y < 10; y++) { |
| 168 | if (x) { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 169 | paint.setMaskFilter(SkMaskFilter::MakeBlur(gStyle[x - 1], sigma)); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 170 | } |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 171 | canvas->drawString("Title Bar", x*SkIntToScalar(100), y*SkIntToScalar(30), paint); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 172 | sigma *= 0.75f; |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 173 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 174 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 175 | } |
| 176 | return; |
| 177 | } |
| 178 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 179 | paint.setColor(SK_ColorBLUE); |
| 180 | |
| 181 | #if 1 |
| 182 | SkPath p; |
| 183 | float r = rand.nextUScalar1() + 0.5f; |
| 184 | SkScalar x = 0, y = 0; |
| 185 | p.moveTo(x, y); |
| 186 | #if 0 |
| 187 | p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r); |
| 188 | p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y); |
| 189 | #else |
| 190 | p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r); |
| 191 | p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y); |
| 192 | #endif |
| 193 | p.close(); |
| 194 | fPath = p; |
| 195 | fPath.offset(100, 0); |
| 196 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 197 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 198 | fPath.setFillType(SkPath::kWinding_FillType); |
| 199 | drawSet(canvas, &paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 200 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 201 | canvas->translate(0, fPath.getBounds().height() * 5 / 4); |
| 202 | fPath.setFillType(SkPath::kEvenOdd_FillType); |
| 203 | drawSet(canvas, &paint); |
| 204 | } |
| 205 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 206 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 207 | typedef Sample INHERITED; |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | ////////////////////////////////////////////////////////////////////////////// |
| 211 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame^] | 212 | DEF_SAMPLE( return new StrokePathView(); ) |