SampleStrokePath.cpp revision 36352bf5e38f45a70ee4f4fc132a38048d38206d
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 */ 8#include "SampleCode.h" 9#include "SkBlurMask.h" 10#include "SkBlurMaskFilter.h" 11#include "SkCanvas.h" 12#include "SkParsePath.h" 13#include "SkPath.h" 14#include "SkRandom.h" 15#include "SkView.h" 16 17 18static void test_huge_stroke(SkCanvas* canvas) { 19 SkRect srcR = { 0, 0, 72000, 54000 }; 20 SkRect dstR = { 0, 0, 640, 480 }; 21 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(); 28 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 44#if 0 45static 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(); 57 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} 76#endif 77 78static 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 87static 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 98class StrokePathView : public SampleView { 99 SkScalar fWidth; 100 SkPath fPath; 101protected: 102 void onOnceBeforeDraw() override { 103// test_blur(); 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 117 118 scale_to_width(&fPath, fWidth); 119 const SkRect& bounds = fPath.getBounds(); 120 fPath.offset(-bounds.fLeft, -bounds.fTop); 121 122 this->setBGColor(0xFFDDDDDD); 123 } 124 125 // overrides from SkEventSink 126 bool onQuery(SkEvent* evt) override { 127 if (SampleCode::TitleQ(*evt)) { 128 SampleCode::TitleR(evt, "StrokePath"); 129 return true; 130 } 131 return this->INHERITED::onQuery(evt); 132 } 133 134 SkRandom rand; 135 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 148 void onDrawContent(SkCanvas* canvas) override { 149 test_huge_stroke(canvas); return; 150 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); 151 152 SkPaint paint; 153 paint.setAntiAlias(true); 154 155 if (true) { 156 canvas->drawColor(SK_ColorBLACK); 157 158 paint.setTextSize(24); 159 paint.setColor(SK_ColorWHITE); 160 canvas->translate(10, 30); 161 162 static const SkBlurStyle gStyle[] = { 163 kNormal_SkBlurStyle, 164 kInner_SkBlurStyle, 165 kOuter_SkBlurStyle, 166 kSolid_SkBlurStyle, 167 }; 168 for (int x = 0; x < 5; x++) { 169 SkMaskFilter* mf; 170 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); 171 for (int y = 0; y < 10; y++) { 172 if (x) { 173 mf = SkBlurMaskFilter::Create(gStyle[x - 1], sigma); 174 paint.setMaskFilter(mf)->unref(); 175 } 176 canvas->drawText("Title Bar", 9, x*SkIntToScalar(100), y*SkIntToScalar(30), paint); 177 sigma *= 0.75f; 178 } 179 180 } 181 return; 182 } 183 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 202 203 fPath.setFillType(SkPath::kWinding_FillType); 204 drawSet(canvas, &paint); 205 206 canvas->translate(0, fPath.getBounds().height() * 5 / 4); 207 fPath.setFillType(SkPath::kEvenOdd_FillType); 208 drawSet(canvas, &paint); 209 } 210 211 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, 212 unsigned modi) override { 213 this->inval(NULL); 214 return this->INHERITED::onFindClickHandler(x, y, modi); 215 } 216private: 217 typedef SampleView INHERITED; 218}; 219 220////////////////////////////////////////////////////////////////////////////// 221 222static SkView* MyFactory() { return new StrokePathView; } 223static SkViewRegister reg(MyFactory); 224