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