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