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; |
| 101 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | StrokePathView() { |
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 | protected: |
| 126 | // overrides from SkEventSink |
| 127 | virtual bool onQuery(SkEvent* evt) { |
| 128 | if (SampleCode::TitleQ(*evt)) { |
| 129 | SampleCode::TitleR(evt, "StrokePath"); |
| 130 | return true; |
| 131 | } |
| 132 | return this->INHERITED::onQuery(evt); |
| 133 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 135 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 137 | void drawSet(SkCanvas* canvas, SkPaint* paint) { |
| 138 | SkAutoCanvasRestore acr(canvas, true); |
| 139 | |
| 140 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 141 | paint->setStyle(gRec[i].fStyle); |
| 142 | paint->setStrokeJoin(gRec[i].fJoin); |
| 143 | paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth)); |
| 144 | canvas->drawPath(fPath, *paint); |
| 145 | canvas->translate(fWidth * 5 / 4, 0); |
| 146 | } |
| 147 | } |
| 148 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 149 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 150 | test_huge_stroke(canvas); return; |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 151 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 152 | |
| 153 | SkPaint paint; |
| 154 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 156 | if (true) { |
| 157 | canvas->drawColor(SK_ColorBLACK); |
| 158 | |
| 159 | paint.setTextSize(24); |
| 160 | paint.setColor(SK_ColorWHITE); |
| 161 | canvas->translate(10, 30); |
| 162 | |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 163 | static const SkBlurStyle gStyle[] = { |
| 164 | kNormal_SkBlurStyle, |
| 165 | kInner_SkBlurStyle, |
| 166 | kOuter_SkBlurStyle, |
| 167 | kSolid_SkBlurStyle, |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 168 | }; |
| 169 | for (int x = 0; x < 5; x++) { |
| 170 | SkMaskFilter* mf; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 171 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 172 | for (int y = 0; y < 10; y++) { |
| 173 | if (x) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 174 | mf = SkBlurMaskFilter::Create(gStyle[x - 1], sigma); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 175 | paint.setMaskFilter(mf)->unref(); |
| 176 | } |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 177 | 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] | 178 | sigma *= 0.75f; |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 179 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 181 | } |
| 182 | return; |
| 183 | } |
| 184 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 185 | paint.setColor(SK_ColorBLUE); |
| 186 | |
| 187 | #if 1 |
| 188 | SkPath p; |
| 189 | float r = rand.nextUScalar1() + 0.5f; |
| 190 | SkScalar x = 0, y = 0; |
| 191 | p.moveTo(x, y); |
| 192 | #if 0 |
| 193 | p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r); |
| 194 | p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y); |
| 195 | #else |
| 196 | p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r); |
| 197 | p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y); |
| 198 | #endif |
| 199 | p.close(); |
| 200 | fPath = p; |
| 201 | fPath.offset(100, 0); |
| 202 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 204 | fPath.setFillType(SkPath::kWinding_FillType); |
| 205 | drawSet(canvas, &paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 206 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 207 | canvas->translate(0, fPath.getBounds().height() * 5 / 4); |
| 208 | fPath.setFillType(SkPath::kEvenOdd_FillType); |
| 209 | drawSet(canvas, &paint); |
| 210 | } |
| 211 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 212 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 213 | unsigned modi) SK_OVERRIDE { |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 214 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 215 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 216 | } |
| 217 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 218 | typedef SampleView INHERITED; |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | ////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | static SkView* MyFactory() { return new StrokePathView; } |
| 224 | static SkViewRegister reg(MyFactory); |