blob: dc3e9df086a4c202e01b1861237eaa8191e1a028 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed339cdbf2015-02-05 22:02:37 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed339cdbf2015-02-05 22:02:37 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRandom.h"
17#include "SkRegion.h"
18#include "SkShader.h"
19#include "SkUtils.h"
20#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkColorPriv.h"
22#include "SkColorFilter.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkOSFile.h"
27#include "SkStream.h"
28
29#include "SkGeometry.h" // private include :(
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031static SkShader* make_shader0(SkIPoint* size) {
32 SkBitmap bm;
reed@google.com82065d62011-02-07 15:30:46 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034// SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm);
reed@android.comf2b98d62010-12-20 18:26:13 +000035 SkImageDecoder::DecodeFile("/skimages/logo.png", &bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 size->set(bm.width(), bm.height());
37 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
38 SkShader::kClamp_TileMode);
39}
40
41static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000042 SkPoint pts[] = { { 0, 0, },
43 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
halcanary96fcdcc2015-08-27 07:41:13 -070045 return SkGradientShader::CreateLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000046 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
49///////////////////////////////////////////////////////////////////////////////
50
51class Patch {
52public:
reed@android.com4516f472009-06-29 16:25:36 +000053 Patch() { sk_bzero(fPts, sizeof(fPts)); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 ~Patch() {}
reed@google.com82065d62011-02-07 15:30:46 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 void setPatch(const SkPoint pts[12]) {
57 memcpy(fPts, pts, 12 * sizeof(SkPoint));
58 fPts[12] = pts[0]; // the last shall be first
59 }
60 void setBounds(int w, int h) { fW = w; fH = h; }
61
62 void draw(SkCanvas*, const SkPaint&, int segsU, int segsV,
63 bool doTextures, bool doColors);
reed@google.com82065d62011-02-07 15:30:46 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065private:
66 SkPoint fPts[13];
67 int fW, fH;
68};
69
70static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) {
71 SkScalar t = 0;
72 SkScalar dt = SK_Scalar1 / segs;
73
74 samples[0] = cubic[0];
75 for (int i = 1; i < segs; i++) {
76 t += dt;
halcanary96fcdcc2015-08-27 07:41:13 -070077 SkEvalCubicAt(cubic, t, &samples[i], nullptr, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 }
79}
80
81static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv,
82 SkPoint* pt) {
83 const int TL = 0;
84 const int TR = nu;
85 const int BR = TR + nv;
86 const int BL = BR + nu;
87
88 SkScalar u = SkIntToScalar(iu) / nu;
89 SkScalar v = SkIntToScalar(iv) / nv;
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkScalar uv = SkScalarMul(u, v);
92 SkScalar Uv = SkScalarMul(SK_Scalar1 - u, v);
93 SkScalar uV = SkScalarMul(u, SK_Scalar1 - v);
94 SkScalar UV = SkScalarMul(SK_Scalar1 - u, SK_Scalar1 - v);
reed@google.com82065d62011-02-07 15:30:46 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 SkScalar x0 = SkScalarMul(UV, edge[TL].fX) + SkScalarMul(uV, edge[TR].fX) +
97 SkScalarMul(Uv, edge[BL].fX) + SkScalarMul(uv, edge[BR].fX);
98 SkScalar y0 = SkScalarMul(UV, edge[TL].fY) + SkScalarMul(uV, edge[TR].fY) +
99 SkScalarMul(Uv, edge[BL].fY) + SkScalarMul(uv, edge[BR].fY);
100
101 SkScalar x = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fX) +
102 SkScalarMul(u, edge[TR+iv].fX) +
103 SkScalarMul(v, edge[BR+nu-iu].fX) +
104 SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fX) - x0;
105 SkScalar y = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fY) +
106 SkScalarMul(u, edge[TR+iv].fY) +
107 SkScalarMul(v, edge[BR+nu-iu].fY) +
108 SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fY) - y0;
109 pt->set(x, y);
110}
111
112static int ScalarTo255(SkScalar v) {
113 int scale = SkScalarToFixed(v) >> 8;
114 if (scale < 0) {
115 scale = 0;
116 } else if (scale > 255) {
117 scale = 255;
118 }
119 return scale;
120}
121
122static SkColor make_color(SkScalar s, SkScalar t) {
123 int cs = ScalarTo255(s);
reed@google.com82065d62011-02-07 15:30:46 +0000124 int ct = ScalarTo255(t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0);
126}
127
128void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
129 bool doTextures, bool doColors) {
130 if (nu < 1 || nv < 1) {
131 return;
132 }
133
134 int i, npts = (nu + nv) * 2;
135 SkAutoSTMalloc<16, SkPoint> storage(npts + 1);
136 SkPoint* edge0 = storage.get();
137 SkPoint* edge1 = edge0 + nu;
138 SkPoint* edge2 = edge1 + nv;
139 SkPoint* edge3 = edge2 + nu;
reed@google.com82065d62011-02-07 15:30:46 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 // evaluate the edge points
142 eval_patch_edge(fPts + 0, edge0, nu);
143 eval_patch_edge(fPts + 3, edge1, nv);
144 eval_patch_edge(fPts + 6, edge2, nu);
145 eval_patch_edge(fPts + 9, edge3, nv);
146 edge3[nv] = edge0[0]; // the last shall be first
reed@google.com82065d62011-02-07 15:30:46 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 for (i = 0; i < npts; i++) {
149// canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
150 }
reed@google.com82065d62011-02-07 15:30:46 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 int row, vertCount = (nu + 1) * (nv + 1);
153 SkAutoTMalloc<SkPoint> vertStorage(vertCount);
154 SkPoint* verts = vertStorage.get();
reed@google.com82065d62011-02-07 15:30:46 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 // first row
157 memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
158 // rows
159 SkPoint* r = verts;
160 for (row = 1; row < nv; row++) {
161 r += nu + 1;
162 r[0] = edge3[nv - row];
163 for (int col = 1; col < nu; col++) {
164 eval_sheet(edge0, nu, nv, col, row, &r[col]);
165 }
166 r[nu] = edge1[row];
167 }
168 // last row
169 SkPoint* last = verts + nv * (nu + 1);
170 for (i = 0; i <= nu; i++) {
171 last[i] = edge2[nu - i];
172 }
reed@google.com82065d62011-02-07 15:30:46 +0000173
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174// canvas->drawPoints(verts, vertCount, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 int stripCount = (nu + 1) * 2;
177 SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2);
178 SkAutoTMalloc<SkColor> colorStorage(stripCount);
179 SkPoint* strip = stripStorage.get();
180 SkPoint* tex = strip + stripCount;
181 SkColor* colors = colorStorage.get();
182 SkScalar t = 0;
183 const SkScalar ds = SK_Scalar1 * fW / nu;
184 const SkScalar dt = SK_Scalar1 * fH / nv;
185 r = verts;
186 for (row = 0; row < nv; row++) {
187 SkPoint* upper = r;
188 SkPoint* lower = r + nu + 1;
189 r = lower;
190 SkScalar s = 0;
191 for (i = 0; i <= nu; i++) {
192 strip[i*2 + 0] = *upper++;
193 strip[i*2 + 1] = *lower++;
194 tex[i*2 + 0].set(s, t);
195 tex[i*2 + 1].set(s, t + dt);
196 colors[i*2 + 0] = make_color(s/fW, t/fH);
197 colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH);
198 s += ds;
199 }
200 t += dt;
201 canvas->drawVertices(SkCanvas::kTriangleStrip_VertexMode, stripCount,
halcanary96fcdcc2015-08-27 07:41:13 -0700202 strip, doTextures ? tex : nullptr,
203 doColors ? colors : nullptr, nullptr,
204 nullptr, 0, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 }
206}
207
208static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
209 Patch* patch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 SkAutoCanvasRestore ar(canvas, true);
211
reedcdf2db92014-08-06 09:54:19 -0700212 patch->draw(canvas, paint, nu, nv, false, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000213 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700214 patch->draw(canvas, paint, nu, nv, true, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000215 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700216 patch->draw(canvas, paint, nu, nv, false, true);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000217 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700218 patch->draw(canvas, paint, nu, nv, true, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219}
220
reed147476d2014-08-06 07:00:27 -0700221const SkScalar DX = 20;
222const SkScalar DY = 0;
223
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000224class PatchView : public SampleView {
reed339cdbf2015-02-05 22:02:37 -0800225 SkScalar fAngle;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 SkShader* fShader0;
227 SkShader* fShader1;
228 SkIPoint fSize0, fSize1;
229 SkPoint fPts[12];
reed@google.com82065d62011-02-07 15:30:46 +0000230
231public:
reed339cdbf2015-02-05 22:02:37 -0800232 PatchView() : fAngle(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 fShader0 = make_shader0(&fSize0);
234 fSize1 = fSize0;
235 if (fSize0.fX == 0 || fSize0.fY == 0) {
236 fSize1.set(2, 2);
237 }
238 fShader1 = make_shader1(fSize1);
239
reed@android.com6b82d1a2009-06-03 02:35:01 +0000240 const SkScalar S = SkIntToScalar(50);
241 const SkScalar T = SkIntToScalar(40);
242 fPts[0].set(S*0, T);
243 fPts[1].set(S*1, T);
244 fPts[2].set(S*2, T);
245 fPts[3].set(S*3, T);
246 fPts[4].set(S*3, T*2);
247 fPts[5].set(S*3, T*3);
248 fPts[6].set(S*3, T*4);
249 fPts[7].set(S*2, T*4);
250 fPts[8].set(S*1, T*4);
251 fPts[9].set(S*0, T*4);
252 fPts[10].set(S*0, T*3);
253 fPts[11].set(S*0, T*2);
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000254
255 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 }
reed@google.com82065d62011-02-07 15:30:46 +0000257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 virtual ~PatchView() {
reed@google.com82065d62011-02-07 15:30:46 +0000259 SkSafeUnref(fShader0);
260 SkSafeUnref(fShader1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 }
reed@google.com82065d62011-02-07 15:30:46 +0000262
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263protected:
264 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700265 bool onQuery(SkEvent* evt) override {
tfarina@chromium.org45369a32012-09-30 11:30:01 +0000266 if (SampleCode::TitleQ(*evt)) {
267 SampleCode::TitleR(evt, "Patch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 return true;
269 }
270 return this->INHERITED::onQuery(evt);
271 }
reed@google.com82065d62011-02-07 15:30:46 +0000272
mtkleinf0599002015-07-13 06:18:39 -0700273 void onDrawContent(SkCanvas* canvas) override {
reedcdf2db92014-08-06 09:54:19 -0700274 const int nu = 10;
275 const int nv = 10;
276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 SkPaint paint;
278 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700279 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +0000280
reed147476d2014-08-06 07:00:27 -0700281 canvas->translate(DX, DY);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000282
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 Patch patch;
reed@google.com82065d62011-02-07 15:30:46 +0000284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 paint.setShader(fShader0);
286 if (fSize0.fX == 0) {
287 fSize0.fX = 1;
288 }
289 if (fSize0.fY == 0) {
290 fSize0.fY = 1;
291 }
292 patch.setBounds(fSize0.fX, fSize0.fY);
reed@google.com82065d62011-02-07 15:30:46 +0000293
294 patch.setPatch(fPts);
reedcdf2db92014-08-06 09:54:19 -0700295 drawpatches(canvas, paint, nu, nv, &patch);
reed@google.com82065d62011-02-07 15:30:46 +0000296
halcanary96fcdcc2015-08-27 07:41:13 -0700297 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 paint.setAntiAlias(true);
299 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.comf2b98d62010-12-20 18:26:13 +0000300 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000301
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 canvas->translate(0, SkIntToScalar(300));
reed@google.com82065d62011-02-07 15:30:46 +0000303
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 paint.setAntiAlias(false);
305 paint.setShader(fShader1);
reedcdf2db92014-08-06 09:54:19 -0700306 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000307 SkMatrix m;
308 m.setSkew(1, 0);
309 SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m);
310 paint.setShader(s)->unref();
311 }
reedcdf2db92014-08-06 09:54:19 -0700312 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000313 SkMatrix m;
reed339cdbf2015-02-05 22:02:37 -0800314 m.setRotate(fAngle);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000315 SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m);
316 paint.setShader(s)->unref();
317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 patch.setBounds(fSize1.fX, fSize1.fY);
reedcdf2db92014-08-06 09:54:19 -0700319 drawpatches(canvas, paint, nu, nv, &patch);
reed339cdbf2015-02-05 22:02:37 -0800320 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000321
mtklein36352bf2015-03-25 18:17:31 -0700322 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800323 fAngle = timer.scaled(60, 360);
324 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 }
reed@google.com82065d62011-02-07 15:30:46 +0000326
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 class PtClick : public Click {
328 public:
329 int fIndex;
330 PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
331 };
reed@google.com82065d62011-02-07 15:30:46 +0000332
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
334 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
335 }
reed@google.com82065d62011-02-07 15:30:46 +0000336
mtkleinf0599002015-07-13 06:18:39 -0700337 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed147476d2014-08-06 07:00:27 -0700338 x -= DX;
339 y -= DY;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000340 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 if (hittest(fPts[i], x, y)) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000342 return new PtClick(this, (int)i);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 }
344 }
reed@google.com4d5c26d2013-01-08 16:17:50 +0000345 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 }
reed@google.com82065d62011-02-07 15:30:46 +0000347
mtkleinf0599002015-07-13 06:18:39 -0700348 bool onClick(Click* click) override {
reed147476d2014-08-06 07:00:27 -0700349 fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY);
halcanary96fcdcc2015-08-27 07:41:13 -0700350 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 return true;
352 }
reed@google.com82065d62011-02-07 15:30:46 +0000353
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000355 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356};
357
358//////////////////////////////////////////////////////////////////////////////
359
360static SkView* MyFactory() { return new PatchView; }
361static SkViewRegister reg(MyFactory);