mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 9 | #include "SampleCode.h" |
| 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
| 16 | #include "SkImage.h" |
| 17 | #include "SkSurface.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 18 | #include "SkClipOpPriv.h" |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 19 | |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 20 | #define FAT_PIXEL_COLOR SK_ColorBLACK |
| 21 | #define PIXEL_CENTER_SIZE 3 |
| 22 | #define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/ |
| 23 | #define WIRE_FRAME_SIZE 1.5f |
| 24 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 25 | static SkScalar apply_grid(SkScalar x) { |
| 26 | const SkScalar grid = 2; |
| 27 | return SkScalarRoundToScalar(x * grid) / grid; |
| 28 | } |
| 29 | |
| 30 | static void apply_grid(SkPoint pts[], int count) { |
| 31 | for (int i = 0; i < count; ++i) { |
| 32 | pts[i].set(apply_grid(pts[i].fX), apply_grid(pts[i].fY)); |
| 33 | } |
| 34 | } |
| 35 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 36 | static void erase(SkSurface* surface) { |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 37 | surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 40 | class FatBits { |
| 41 | public: |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 42 | FatBits() { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 43 | fAA = false; |
| 44 | fStyle = kHair_Style; |
reed | bb88674 | 2015-08-15 07:42:31 -0700 | [diff] [blame] | 45 | fGrid = false; |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 46 | fShowSkeleton = true; |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 47 | fUseClip = false; |
mike@reedtribe.org | 64494e9 | 2013-03-06 02:18:33 +0000 | [diff] [blame] | 48 | fRectAsOval = false; |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 49 | fUseTriangle = false; |
reed | a5d1207 | 2015-11-16 18:57:18 -0800 | [diff] [blame] | 50 | fStrokeCap = SkPaint::kButt_Cap; |
skia.committer@gmail.com | 1aa90cf | 2012-11-06 13:18:25 +0000 | [diff] [blame] | 51 | |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 52 | fClipRect.set(2, 2, 11, 8 ); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 55 | int getZoom() const { return fZoom; } |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 56 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 57 | bool getAA() const { return fAA; } |
| 58 | void setAA(bool aa) { fAA = aa; } |
| 59 | |
| 60 | bool getGrid() const { return fGrid; } |
| 61 | void setGrid(bool g) { fGrid = g; } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 62 | |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 63 | bool getShowSkeleton() const { return fShowSkeleton; } |
| 64 | void setShowSkeleton(bool ss) { fShowSkeleton = ss; } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 65 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 66 | bool getTriangle() const { return fUseTriangle; } |
| 67 | void setTriangle(bool ut) { fUseTriangle = ut; } |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 68 | |
mike@reedtribe.org | 64494e9 | 2013-03-06 02:18:33 +0000 | [diff] [blame] | 69 | void toggleRectAsOval() { fRectAsOval = !fRectAsOval; } |
| 70 | |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 71 | void togglePixelColors() { |
| 72 | if (fShader == fShader0) { |
| 73 | fShader = fShader1; |
| 74 | } else { |
| 75 | fShader = fShader0; |
| 76 | } |
| 77 | } |
| 78 | |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 79 | bool getUseClip() const { return fUseClip; } |
| 80 | void setUseClip(bool uc) { fUseClip = uc; } |
| 81 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 82 | enum Style { |
| 83 | kHair_Style, |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 84 | kStroke_Style, |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 85 | }; |
| 86 | Style getStyle() const { return fStyle; } |
| 87 | void setStyle(Style s) { fStyle = s; } |
| 88 | |
| 89 | void setWHZ(int width, int height, int zoom) { |
| 90 | fW = width; |
| 91 | fH = height; |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 92 | fZoom = zoom; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 93 | fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom)); |
| 94 | fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom)); |
| 95 | fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom); |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 96 | fShader0 = sk_tool_utils::create_checkerboard_shader(0xFFDDDDDD, 0xFFFFFFFF, zoom); |
| 97 | fShader1 = SkShader::MakeColorShader(SK_ColorWHITE); |
| 98 | fShader = fShader0; |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 100 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 101 | fMinSurface = SkSurface::MakeRaster(info); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 102 | info = info.makeWH(width * zoom, height * zoom); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 103 | fMaxSurface = SkSurface::MakeRaster(info); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void drawBG(SkCanvas*); |
| 107 | void drawFG(SkCanvas*); |
| 108 | void drawLine(SkCanvas*, SkPoint pts[2]); |
| 109 | void drawRect(SkCanvas* canvas, SkPoint pts[2]); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 110 | void drawTriangle(SkCanvas* canvas, SkPoint pts[3]); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 111 | |
reed | a5d1207 | 2015-11-16 18:57:18 -0800 | [diff] [blame] | 112 | SkPaint::Cap fStrokeCap; |
| 113 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 114 | private: |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 115 | bool fAA, fGrid, fShowSkeleton, fUseClip, fRectAsOval, fUseTriangle; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 116 | Style fStyle; |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 117 | int fW, fH, fZoom; |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 118 | SkMatrix fMatrix, fInverse; |
| 119 | SkRect fBounds, fClipRect; |
| 120 | sk_sp<SkShader> fShader0; |
| 121 | sk_sp<SkShader> fShader1; |
| 122 | sk_sp<SkShader> fShader; |
| 123 | sk_sp<SkSurface> fMinSurface; |
| 124 | sk_sp<SkSurface> fMaxSurface; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 125 | |
| 126 | void setupPaint(SkPaint* paint) { |
| 127 | bool aa = this->getAA(); |
reed | a5d1207 | 2015-11-16 18:57:18 -0800 | [diff] [blame] | 128 | paint->setStrokeCap(fStrokeCap); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 129 | switch (fStyle) { |
| 130 | case kHair_Style: |
| 131 | paint->setStrokeWidth(0); |
| 132 | break; |
| 133 | case kStroke_Style: |
reed@google.com | cdbcb3e | 2012-10-22 22:10:20 +0000 | [diff] [blame] | 134 | paint->setStrokeWidth(SK_Scalar1); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | paint->setAntiAlias(aa); |
| 138 | } |
| 139 | |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 140 | void setupSkeletonPaint(SkPaint* paint) { |
| 141 | paint->setStyle(SkPaint::kStroke_Style); |
| 142 | paint->setStrokeWidth(WIRE_FRAME_SIZE); |
| 143 | paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0); |
| 144 | paint->setAntiAlias(true); |
| 145 | } |
| 146 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 147 | void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 148 | void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]); |
| 149 | void drawRectSkeleton(SkCanvas* max, const SkRect& r) { |
| 150 | SkPaint paint; |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 151 | this->setupSkeletonPaint(&paint); |
| 152 | SkPath path; |
| 153 | |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 154 | fRectAsOval ? path.addOval(r) : path.addRect(r); |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 155 | max->drawPath(path, paint); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 156 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 157 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 158 | void copyMinToMax() { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 159 | erase(fMaxSurface.get()); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 160 | SkCanvas* canvas = fMaxSurface->getCanvas(); |
| 161 | canvas->save(); |
| 162 | canvas->concat(fMatrix); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | fMinSurface->draw(canvas, 0, 0, nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 164 | canvas->restore(); |
| 165 | |
| 166 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 167 | paint.setBlendMode(SkBlendMode::kClear); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 168 | for (int iy = 1; iy < fH; ++iy) { |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 169 | SkScalar y = SkIntToScalar(iy * fZoom); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 170 | canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint); |
| 171 | } |
| 172 | for (int ix = 1; ix < fW; ++ix) { |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 173 | SkScalar x = SkIntToScalar(ix * fZoom); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 174 | canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint); |
| 175 | } |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | void FatBits::drawBG(SkCanvas* canvas) { |
| 180 | SkPaint paint; |
| 181 | |
| 182 | paint.setShader(fShader); |
| 183 | canvas->drawRect(fBounds, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 184 | paint.setShader(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void FatBits::drawFG(SkCanvas* canvas) { |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 188 | SkPaint inner, outer; |
| 189 | |
| 190 | inner.setAntiAlias(true); |
| 191 | inner.setColor(SK_ColorBLACK); |
| 192 | inner.setStrokeWidth(PIXEL_CENTER_SIZE); |
| 193 | |
| 194 | outer.setAntiAlias(true); |
| 195 | outer.setColor(SK_ColorWHITE); |
| 196 | outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 197 | |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 198 | SkScalar half = SkIntToScalar(fZoom) / 2; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 199 | for (int iy = 0; iy < fH; ++iy) { |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 200 | SkScalar y = SkIntToScalar(iy * fZoom) + half; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 201 | for (int ix = 0; ix < fW; ++ix) { |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 202 | SkScalar x = SkIntToScalar(ix * fZoom) + half; |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 203 | |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 204 | canvas->drawPoint(x, y, outer); |
| 205 | canvas->drawPoint(x, y, inner); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 208 | |
| 209 | if (fUseClip) { |
| 210 | SkPaint p; |
| 211 | p.setStyle(SkPaint::kStroke_Style); |
| 212 | p.setColor(SK_ColorLTGRAY); |
| 213 | SkRect r = { |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 214 | fClipRect.fLeft * fZoom, |
| 215 | fClipRect.fTop * fZoom, |
| 216 | fClipRect.fRight * fZoom, |
| 217 | fClipRect.fBottom * fZoom |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 218 | }; |
| 219 | canvas->drawRect(r, p); |
| 220 | } |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) { |
| 224 | SkPaint paint; |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 225 | this->setupSkeletonPaint(&paint); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 226 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 227 | SkPath path; |
| 228 | path.moveTo(pts[0]); |
| 229 | path.lineTo(pts[1]); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 230 | |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 231 | if (fStyle == kStroke_Style) { |
| 232 | SkPaint p; |
| 233 | p.setStyle(SkPaint::kStroke_Style); |
| 234 | p.setStrokeWidth(SK_Scalar1 * fZoom); |
| 235 | p.setStrokeCap(fStrokeCap); |
| 236 | SkPath dst; |
| 237 | p.getFillPath(path, &dst); |
| 238 | path = dst; |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 239 | |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 240 | path.moveTo(pts[0]); |
| 241 | path.lineTo(pts[1]); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 242 | } |
| 243 | max->drawPath(path, paint); |
| 244 | } |
| 245 | |
| 246 | void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) { |
| 247 | SkPaint paint; |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 248 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 249 | fInverse.mapPoints(pts, 2); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 250 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 251 | if (fGrid) { |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 252 | apply_grid(pts, 2); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 253 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 254 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 255 | erase(fMinSurface.get()); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 256 | this->setupPaint(&paint); |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 257 | paint.setColor(FAT_PIXEL_COLOR); |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 258 | if (fUseClip) { |
| 259 | fMinSurface->getCanvas()->save(); |
| 260 | SkRect r = fClipRect; |
| 261 | r.inset(SK_Scalar1/3, SK_Scalar1/3); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 262 | fMinSurface->getCanvas()->clipRect(r, kIntersect_SkClipOp, true); |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 263 | } |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 264 | fMinSurface->getCanvas()->drawLine(pts[0], pts[1], paint); |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 265 | if (fUseClip) { |
| 266 | fMinSurface->getCanvas()->restore(); |
| 267 | } |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 268 | this->copyMinToMax(); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 269 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 270 | SkCanvas* max = fMaxSurface->getCanvas(); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 271 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 272 | fMatrix.mapPoints(pts, 2); |
| 273 | this->drawLineSkeleton(max, pts); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 274 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | fMaxSurface->draw(canvas, 0, 0, nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) { |
| 279 | SkPaint paint; |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 280 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 281 | fInverse.mapPoints(pts, 2); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 282 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 283 | if (fGrid) { |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 284 | apply_grid(pts, 2); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 285 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 286 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 287 | SkRect r; |
| 288 | r.set(pts, 2); |
| 289 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 290 | erase(fMinSurface.get()); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 291 | this->setupPaint(&paint); |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 292 | paint.setColor(FAT_PIXEL_COLOR); |
mike@reedtribe.org | 64494e9 | 2013-03-06 02:18:33 +0000 | [diff] [blame] | 293 | { |
| 294 | SkCanvas* c = fMinSurface->getCanvas(); |
| 295 | fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint); |
| 296 | } |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 297 | this->copyMinToMax(); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 298 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 299 | SkCanvas* max = fMaxSurface->getCanvas(); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 300 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 301 | fMatrix.mapPoints(pts, 2); |
| 302 | r.set(pts, 2); |
| 303 | this->drawRectSkeleton(max, r); |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 304 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 305 | fMaxSurface->draw(canvas, 0, 0, nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 306 | } |
| 307 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 308 | void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) { |
| 309 | SkPaint paint; |
| 310 | this->setupSkeletonPaint(&paint); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 311 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 312 | SkPath path; |
| 313 | path.moveTo(pts[0]); |
| 314 | path.lineTo(pts[1]); |
| 315 | path.lineTo(pts[2]); |
| 316 | path.close(); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 317 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 318 | max->drawPath(path, paint); |
| 319 | } |
| 320 | |
| 321 | void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) { |
| 322 | SkPaint paint; |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 323 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 324 | fInverse.mapPoints(pts, 3); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 325 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 326 | if (fGrid) { |
| 327 | apply_grid(pts, 3); |
| 328 | } |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 329 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 330 | SkPath path; |
| 331 | path.moveTo(pts[0]); |
| 332 | path.lineTo(pts[1]); |
| 333 | path.lineTo(pts[2]); |
| 334 | path.close(); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 335 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 336 | erase(fMinSurface.get()); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 337 | this->setupPaint(&paint); |
| 338 | paint.setColor(FAT_PIXEL_COLOR); |
| 339 | fMinSurface->getCanvas()->drawPath(path, paint); |
| 340 | this->copyMinToMax(); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 341 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 342 | SkCanvas* max = fMaxSurface->getCanvas(); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 343 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 344 | fMatrix.mapPoints(pts, 3); |
| 345 | this->drawTriangleSkeleton(max, pts); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 346 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 347 | fMaxSurface->draw(canvas, 0, 0, nullptr); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 348 | } |
| 349 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 350 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 351 | |
| 352 | class IndexClick : public SkView::Click { |
| 353 | int fIndex; |
| 354 | public: |
| 355 | IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {} |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 356 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 357 | static int GetIndex(SkView::Click* click) { |
| 358 | return ((IndexClick*)click)->fIndex; |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | class DrawLineView : public SampleView { |
| 363 | FatBits fFB; |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 364 | SkPoint fPts[3]; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 365 | bool fIsRect; |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 366 | int fZoom = 64; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 367 | public: |
| 368 | DrawLineView() { |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 369 | fFB.setWHZ(24*2, 16*2, fZoom); |
reed | bb88674 | 2015-08-15 07:42:31 -0700 | [diff] [blame] | 370 | fPts[0].set(1, 1); |
| 371 | fPts[1].set(5, 4); |
| 372 | fPts[2].set(2, 6); |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 373 | SkMatrix::MakeScale(SkIntToScalar(fZoom)).mapPoints(fPts, 3); |
reed@google.com | cdbcb3e | 2012-10-22 22:10:20 +0000 | [diff] [blame] | 374 | fIsRect = false; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 375 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 376 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 377 | void setStyle(FatBits::Style s) { |
| 378 | fFB.setStyle(s); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 379 | this->inval(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 383 | bool onQuery(SkEvent* evt) override { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 384 | if (SampleCode::TitleQ(*evt)) { |
| 385 | SampleCode::TitleR(evt, "FatBits"); |
| 386 | return true; |
| 387 | } |
| 388 | SkUnichar uni; |
| 389 | if (SampleCode::CharQ(*evt, &uni)) { |
| 390 | switch (uni) { |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 391 | case 'c': |
| 392 | fFB.setUseClip(!fFB.getUseClip()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 393 | this->inval(nullptr); |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 394 | return true; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 395 | case 'r': |
| 396 | fIsRect = !fIsRect; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 397 | this->inval(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 398 | return true; |
mike@reedtribe.org | 64494e9 | 2013-03-06 02:18:33 +0000 | [diff] [blame] | 399 | case 'o': |
| 400 | fFB.toggleRectAsOval(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 401 | this->inval(nullptr); |
mike@reedtribe.org | 64494e9 | 2013-03-06 02:18:33 +0000 | [diff] [blame] | 402 | return true; |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 403 | case 'x': |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 404 | fFB.setGrid(!fFB.getGrid()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 405 | this->inval(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 406 | return true; |
| 407 | case 's': |
| 408 | if (FatBits::kStroke_Style == fFB.getStyle()) { |
| 409 | this->setStyle(FatBits::kHair_Style); |
| 410 | } else { |
| 411 | this->setStyle(FatBits::kStroke_Style); |
| 412 | } |
| 413 | return true; |
reed | a5d1207 | 2015-11-16 18:57:18 -0800 | [diff] [blame] | 414 | case 'k': { |
| 415 | const SkPaint::Cap caps[] = { |
| 416 | SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap, |
| 417 | }; |
| 418 | fFB.fStrokeCap = caps[(fFB.fStrokeCap + 1) % 3]; |
| 419 | this->inval(nullptr); |
| 420 | return true; |
| 421 | } break; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 422 | case 'a': |
| 423 | fFB.setAA(!fFB.getAA()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 424 | this->inval(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 425 | return true; |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 426 | case 'w': |
| 427 | fFB.setShowSkeleton(!fFB.getShowSkeleton()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 428 | this->inval(nullptr); |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 429 | return true; |
| 430 | case 'g': |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 431 | fFB.togglePixelColors(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 432 | this->inval(nullptr); |
reed@google.com | c83e352 | 2012-10-22 22:00:08 +0000 | [diff] [blame] | 433 | return true; |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 434 | case 't': |
| 435 | fFB.setTriangle(!fFB.getTriangle()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 436 | this->inval(nullptr); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 437 | return true; |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | return this->INHERITED::onQuery(evt); |
| 441 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 442 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 443 | void onDrawContent(SkCanvas* canvas) override { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 444 | fFB.drawBG(canvas); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 445 | if (fFB.getTriangle()) { |
| 446 | fFB.drawTriangle(canvas, fPts); |
| 447 | } |
| 448 | else if (fIsRect) { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 449 | fFB.drawRect(canvas, fPts); |
| 450 | } else { |
| 451 | fFB.drawLine(canvas, fPts); |
| 452 | } |
| 453 | fFB.drawFG(canvas); |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 454 | |
reed@google.com | ffe9d01 | 2012-10-23 15:33:41 +0000 | [diff] [blame] | 455 | { |
| 456 | SkString str; |
reed | 8716a8a | 2016-09-19 07:10:36 -0700 | [diff] [blame] | 457 | str.printf("%s %s %s", |
reed@google.com | ffe9d01 | 2012-10-23 15:33:41 +0000 | [diff] [blame] | 458 | fFB.getAA() ? "AA" : "BW", |
| 459 | FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke", |
reed@google.com | 9c0bef1 | 2012-11-02 19:59:18 +0000 | [diff] [blame] | 460 | fFB.getUseClip() ? "clip" : "noclip"); |
reed@google.com | ffe9d01 | 2012-10-23 15:33:41 +0000 | [diff] [blame] | 461 | SkPaint paint; |
| 462 | paint.setAntiAlias(true); |
| 463 | paint.setTextSize(16); |
| 464 | paint.setColor(SK_ColorBLUE); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 465 | canvas->drawString(str, 10, 16, paint); |
reed@google.com | ffe9d01 | 2012-10-23 15:33:41 +0000 | [diff] [blame] | 466 | } |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 467 | } |
| 468 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 469 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 470 | SkPoint pt = { x, y }; |
| 471 | int index = -1; |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 472 | int count = fFB.getTriangle() ? 3 : 2; |
reed@google.com | cdbcb3e | 2012-10-22 22:10:20 +0000 | [diff] [blame] | 473 | SkScalar tol = 12; |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 474 | |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 475 | for (int i = 0; i < count; ++i) { |
| 476 | if (fPts[i].equalsWithinTolerance(pt, tol)) { |
| 477 | index = i; |
| 478 | break; |
| 479 | } |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 480 | } |
mike@reedtribe.org | c892a15 | 2012-10-23 03:10:46 +0000 | [diff] [blame] | 481 | return new IndexClick(this, index); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 482 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 483 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 484 | bool onClick(Click* click) override { |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 485 | int index = IndexClick::GetIndex(click); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 486 | if (index >= 0 && index <= 2) { |
mike@reedtribe.org | c892a15 | 2012-10-23 03:10:46 +0000 | [diff] [blame] | 487 | fPts[index] = click->fCurr; |
| 488 | } else { |
| 489 | SkScalar dx = click->fCurr.fX - click->fPrev.fX; |
| 490 | SkScalar dy = click->fCurr.fY - click->fPrev.fY; |
| 491 | fPts[0].offset(dx, dy); |
| 492 | fPts[1].offset(dx, dy); |
reed@google.com | bbe66f0 | 2013-10-30 13:41:19 +0000 | [diff] [blame] | 493 | fPts[2].offset(dx, dy); |
mike@reedtribe.org | c892a15 | 2012-10-23 03:10:46 +0000 | [diff] [blame] | 494 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 495 | this->inval(nullptr); |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 496 | return true; |
| 497 | } |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 498 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 499 | private: |
skia.committer@gmail.com | 45ba2f7 | 2012-10-23 02:01:22 +0000 | [diff] [blame] | 500 | |
mike@reedtribe.org | 50e4c72 | 2012-10-22 03:59:34 +0000 | [diff] [blame] | 501 | typedef SampleView INHERITED; |
| 502 | }; |
| 503 | |
| 504 | ////////////////////////////////////////////////////////////////////////////// |
| 505 | |
| 506 | static SkView* MyFactory() { return new DrawLineView; } |
| 507 | static SkViewRegister reg(MyFactory); |