blob: c300733131d6fe358381be483791d6263ea89f8e [file] [log] [blame]
mike@reedtribe.org50e4c722012-10-22 03:59:34 +00001/*
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.comc83e3522012-10-22 22:00:08 +000018#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.combbe66f02013-10-30 13:41:19 +000023static SkScalar apply_grid(SkScalar x) {
24 const SkScalar grid = 2;
25 return SkScalarRoundToScalar(x * grid) / grid;
26}
27
28static 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.org50e4c722012-10-22 03:59:34 +000034static void erase(SkSurface* surface) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000035 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000036}
37
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000038static SkShader* createChecker(const SkMatrix& localMatrix) {
reed@google.com9c0bef12012-11-02 19:59:18 +000039// SkColor colors[] = { 0xFFFDFDFD, 0xFFF4F4F4 };
40 SkColor colors[] = { 0xFFFFFFFF, 0xFFFFFFFF };
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000041 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000042 bm.allocN32Pixels(2, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000043 bm.lockPixels();
reed@google.com9c0bef12012-11-02 19:59:18 +000044 *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.org9c9005a2014-04-28 14:55:39 +000046 return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000047 SkShader::kRepeat_TileMode, &localMatrix);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000048}
49
50class FatBits {
51public:
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000052 FatBits() {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000053 fAA = false;
54 fStyle = kHair_Style;
55 fGrid = true;
reed@google.comc83e3522012-10-22 22:00:08 +000056 fShowSkeleton = true;
57 fUseGPU = false;
reed@google.com9c0bef12012-11-02 19:59:18 +000058 fUseClip = false;
mike@reedtribe.org64494e92013-03-06 02:18:33 +000059 fRectAsOval = false;
reed@google.combbe66f02013-10-30 13:41:19 +000060 fUseTriangle = false;
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +000061
reed@google.com9c0bef12012-11-02 19:59:18 +000062 fClipRect.set(2, 2, 11, 8 );
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000063 }
64
robertphillips@google.comca47aae2012-12-12 15:58:25 +000065 int getZoom() const { return fZoom; }
reed@google.comc83e3522012-10-22 22:00:08 +000066
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000067 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.com45ba2f72012-10-23 02:01:22 +000072
reed@google.comc83e3522012-10-22 22:00:08 +000073 bool getShowSkeleton() const { return fShowSkeleton; }
74 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000075
reed@google.comc83e3522012-10-22 22:00:08 +000076 bool getUseGPU() const { return fUseGPU; }
77 void setUseGPU(bool ug) { fUseGPU = ug; }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +000078
reed@google.combbe66f02013-10-30 13:41:19 +000079 bool getTriangle() const { return fUseTriangle; }
80 void setTriangle(bool ut) { fUseTriangle = ut; }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +000081
mike@reedtribe.org64494e92013-03-06 02:18:33 +000082 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
83
reed@google.com9c0bef12012-11-02 19:59:18 +000084 bool getUseClip() const { return fUseClip; }
85 void setUseClip(bool uc) { fUseClip = uc; }
86
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000087 enum Style {
88 kHair_Style,
reed@google.comc83e3522012-10-22 22:00:08 +000089 kStroke_Style,
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000090 };
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.comca47aae2012-12-12 15:58:25 +000097 fZoom = zoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000098 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.org5970f622014-05-12 20:42:21 +0000101 fShader.reset(createChecker(fMatrix));
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000102
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000103 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reed@google.combbe66f02013-10-30 13:41:19 +0000104 fMinSurface.reset(SkSurface::NewRaster(info));
reede5ea5002014-09-03 11:54:58 -0700105 info = info.makeWH(width * zoom, height * zoom);
reed@google.combbe66f02013-10-30 13:41:19 +0000106 fMaxSurface.reset(SkSurface::NewRaster(info));
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000107 }
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.combbe66f02013-10-30 13:41:19 +0000113 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000114
115private:
reed@google.combbe66f02013-10-30 13:41:19 +0000116 bool fAA, fGrid, fShowSkeleton, fUseGPU, fUseClip, fRectAsOval, fUseTriangle;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000117 Style fStyle;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000118 int fW, fH, fZoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000119 SkMatrix fMatrix, fInverse;
reed@google.com9c0bef12012-11-02 19:59:18 +0000120 SkRect fBounds, fClipRect;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000121 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.comcdbcb3e2012-10-22 22:10:20 +0000132 paint->setStrokeWidth(SK_Scalar1);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000133 break;
134 }
135 paint->setAntiAlias(aa);
136 }
137
reed@google.comc83e3522012-10-22 22:00:08 +0000138 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.combbe66f02013-10-30 13:41:19 +0000145 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000146 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
147 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
148 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000149 this->setupSkeletonPaint(&paint);
150 SkPath path;
151
152 if (fUseGPU && fAA) {
153 SkRect rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000154 rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000155 path.addRect(rr);
156 path.moveTo(rr.fLeft, rr.fTop);
157 path.lineTo(rr.fRight, rr.fBottom);
158 rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000159 rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000160 path.addRect(rr);
161 } else {
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000162 fRectAsOval ? path.addOval(r) : path.addRect(r);
reed@google.comc83e3522012-10-22 22:00:08 +0000163 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.org50e4c722012-10-22 03:59:34 +0000169 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000170
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000171 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.comca47aae2012-12-12 15:58:25 +0000182 SkScalar y = SkIntToScalar(iy * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000183 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
184 }
185 for (int ix = 1; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000186 SkScalar x = SkIntToScalar(ix * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000187 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
188 }
189 }
190};
191
192void FatBits::drawBG(SkCanvas* canvas) {
193 SkPaint paint;
194
195 paint.setShader(fShader);
196 canvas->drawRect(fBounds, paint);
197 paint.setShader(NULL);
198}
199
200void FatBits::drawFG(SkCanvas* canvas) {
reed@google.comc83e3522012-10-22 22:00:08 +0000201 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.org50e4c722012-10-22 03:59:34 +0000210
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000211 SkScalar half = SkIntToScalar(fZoom) / 2;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000212 for (int iy = 0; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000213 SkScalar y = SkIntToScalar(iy * fZoom) + half;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000214 for (int ix = 0; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000215 SkScalar x = SkIntToScalar(ix * fZoom) + half;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000216
reed@google.comc83e3522012-10-22 22:00:08 +0000217 canvas->drawPoint(x, y, outer);
218 canvas->drawPoint(x, y, inner);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000219 }
220 }
reed@google.com9c0bef12012-11-02 19:59:18 +0000221
222 if (fUseClip) {
223 SkPaint p;
224 p.setStyle(SkPaint::kStroke_Style);
225 p.setColor(SK_ColorLTGRAY);
226 SkRect r = {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000227 fClipRect.fLeft * fZoom,
228 fClipRect.fTop * fZoom,
229 fClipRect.fRight * fZoom,
230 fClipRect.fBottom * fZoom
reed@google.com9c0bef12012-11-02 19:59:18 +0000231 };
232 canvas->drawRect(r, p);
233 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000234}
235
236void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
237 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000238 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000239
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000240 SkPath path;
241 path.moveTo(pts[0]);
242 path.lineTo(pts[1]);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000243
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000244 switch (fStyle) {
245 case kHair_Style:
reed@google.comc83e3522012-10-22 22:00:08 +0000246 if (fUseGPU) {
247 SkPaint p;
248 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000249 p.setStrokeWidth(SK_Scalar1 * fZoom);
reed@google.comc83e3522012-10-22 22:00:08 +0000250 SkPath dst;
251 p.getFillPath(path, &dst);
252 path.addPath(dst);
253 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000254 break;
255 case kStroke_Style: {
256 SkPaint p;
257 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000258 p.setStrokeWidth(SK_Scalar1 * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000259 SkPath dst;
260 p.getFillPath(path, &dst);
261 path = dst;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000262
reed@google.comc83e3522012-10-22 22:00:08 +0000263 if (fUseGPU) {
264 path.moveTo(dst.getPoint(0));
265 path.lineTo(dst.getPoint(2));
266 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000267 } break;
268 }
269 max->drawPath(path, paint);
270}
271
272void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
273 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000274
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000275 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000276
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000277 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000278 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000279 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000280
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000281 erase(fMinSurface);
282 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000283 paint.setColor(FAT_PIXEL_COLOR);
reed@google.com9c0bef12012-11-02 19:59:18 +0000284 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.org50e4c722012-10-22 03:59:34 +0000290 fMinSurface->getCanvas()->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
reed@google.com9c0bef12012-11-02 19:59:18 +0000291 if (fUseClip) {
292 fMinSurface->getCanvas()->restore();
293 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000294 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000295
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000296 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000297
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000298 fMatrix.mapPoints(pts, 2);
299 this->drawLineSkeleton(max, pts);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000300
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000301 fMaxSurface->draw(canvas, 0, 0, NULL);
302}
303
304void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
305 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000306
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000307 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000308
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000309 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000310 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000311 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000312
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000313 SkRect r;
314 r.set(pts, 2);
315
316 erase(fMinSurface);
317 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000318 paint.setColor(FAT_PIXEL_COLOR);
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000319 {
320 SkCanvas* c = fMinSurface->getCanvas();
321 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
322 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000323 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000324
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000325 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000326
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000327 fMatrix.mapPoints(pts, 2);
328 r.set(pts, 2);
329 this->drawRectSkeleton(max, r);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000330
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000331 fMaxSurface->draw(canvas, 0, 0, NULL);
332}
333
reed@google.combbe66f02013-10-30 13:41:19 +0000334void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) {
335 SkPaint paint;
336 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000337
reed@google.combbe66f02013-10-30 13:41:19 +0000338 SkPath path;
339 path.moveTo(pts[0]);
340 path.lineTo(pts[1]);
341 path.lineTo(pts[2]);
342 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000343
reed@google.combbe66f02013-10-30 13:41:19 +0000344 max->drawPath(path, paint);
345}
346
347void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) {
348 SkPaint paint;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000349
reed@google.combbe66f02013-10-30 13:41:19 +0000350 fInverse.mapPoints(pts, 3);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000351
reed@google.combbe66f02013-10-30 13:41:19 +0000352 if (fGrid) {
353 apply_grid(pts, 3);
354 }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000355
reed@google.combbe66f02013-10-30 13:41:19 +0000356 SkPath path;
357 path.moveTo(pts[0]);
358 path.lineTo(pts[1]);
359 path.lineTo(pts[2]);
360 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000361
reed@google.combbe66f02013-10-30 13:41:19 +0000362 erase(fMinSurface);
363 this->setupPaint(&paint);
364 paint.setColor(FAT_PIXEL_COLOR);
365 fMinSurface->getCanvas()->drawPath(path, paint);
366 this->copyMinToMax();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000367
reed@google.combbe66f02013-10-30 13:41:19 +0000368 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000369
reed@google.combbe66f02013-10-30 13:41:19 +0000370 fMatrix.mapPoints(pts, 3);
371 this->drawTriangleSkeleton(max, pts);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000372
reed@google.combbe66f02013-10-30 13:41:19 +0000373 fMaxSurface->draw(canvas, 0, 0, NULL);
374}
375
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000376///////////////////////////////////////////////////////////////////////////////////////////////////
377
378class IndexClick : public SkView::Click {
379 int fIndex;
380public:
381 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000382
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000383 static int GetIndex(SkView::Click* click) {
384 return ((IndexClick*)click)->fIndex;
385 }
386};
387
388class DrawLineView : public SampleView {
389 FatBits fFB;
reed@google.combbe66f02013-10-30 13:41:19 +0000390 SkPoint fPts[3];
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000391 bool fIsRect;
392public:
393 DrawLineView() {
394 fFB.setWHZ(24, 16, 48);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000395 fPts[0].set(48, 48);
396 fPts[1].set(48 * 5, 48 * 4);
reed@google.combbe66f02013-10-30 13:41:19 +0000397 fPts[2].set(48 * 2, 48 * 6);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000398 fIsRect = false;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000399 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000400
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000401 void setStyle(FatBits::Style s) {
402 fFB.setStyle(s);
403 this->inval(NULL);
404 }
405
406protected:
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.com9c0bef12012-11-02 19:59:18 +0000415 case 'c':
416 fFB.setUseClip(!fFB.getUseClip());
417 this->inval(NULL);
418 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000419 case 'r':
420 fIsRect = !fIsRect;
421 this->inval(NULL);
422 return true;
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000423 case 'o':
424 fFB.toggleRectAsOval();
425 this->inval(NULL);
426 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000427 case 'x':
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000428 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.comc83e3522012-10-22 22:00:08 +0000442 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.combbe66f02013-10-30 13:41:19 +0000450 case 't':
451 fFB.setTriangle(!fFB.getTriangle());
452 this->inval(NULL);
453 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000454 }
455 }
456 return this->INHERITED::onQuery(evt);
457 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000458
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000459 virtual void onDrawContent(SkCanvas* canvas) {
460 fFB.drawBG(canvas);
reed@google.combbe66f02013-10-30 13:41:19 +0000461 if (fFB.getTriangle()) {
462 fFB.drawTriangle(canvas, fPts);
463 }
464 else if (fIsRect) {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000465 fFB.drawRect(canvas, fPts);
466 } else {
467 fFB.drawLine(canvas, fPts);
468 }
469 fFB.drawFG(canvas);
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000470
reed@google.comffe9d012012-10-23 15:33:41 +0000471 {
472 SkString str;
reed@google.com9c0bef12012-11-02 19:59:18 +0000473 str.printf("%s %s %s %s",
reed@google.comffe9d012012-10-23 15:33:41 +0000474 fFB.getAA() ? "AA" : "BW",
475 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
reed@google.com9c0bef12012-11-02 19:59:18 +0000476 fFB.getUseGPU() ? "GPU" : "CPU",
477 fFB.getUseClip() ? "clip" : "noclip");
reed@google.comffe9d012012-10-23 15:33:41 +0000478 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.org50e4c722012-10-22 03:59:34 +0000484 }
485
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000486 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
487 unsigned modi) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000488 SkPoint pt = { x, y };
489 int index = -1;
reed@google.combbe66f02013-10-30 13:41:19 +0000490 int count = fFB.getTriangle() ? 3 : 2;
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000491 SkScalar tol = 12;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000492
reed@google.combbe66f02013-10-30 13:41:19 +0000493 for (int i = 0; i < count; ++i) {
494 if (fPts[i].equalsWithinTolerance(pt, tol)) {
495 index = i;
496 break;
497 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000498 }
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000499 return new IndexClick(this, index);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000500 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000501
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000502 virtual bool onClick(Click* click) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000503 int index = IndexClick::GetIndex(click);
reed@google.combbe66f02013-10-30 13:41:19 +0000504 if (index >= 0 && index <= 2) {
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000505 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.combbe66f02013-10-30 13:41:19 +0000511 fPts[2].offset(dx, dy);
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000512 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000513 this->inval(NULL);
514 return true;
515 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000516
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000517private:
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000518
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000519 typedef SampleView INHERITED;
520};
521
522//////////////////////////////////////////////////////////////////////////////
523
524static SkView* MyFactory() { return new DrawLineView; }
525static SkViewRegister reg(MyFactory);