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