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