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