blob: 47786bfa3d7d46e3566d98779cce30b4edd18854 [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
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SampleCode.h"
reed339cdbf2015-02-05 22:02:37 -080010#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkTime.h"
23#include "SkTypeface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040024#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkOSFile.h"
27#include "SkStream.h"
28
29#include "SkGeometry.h" // private include :(
30
reed8a21c9f2016-03-08 18:50:00 -080031static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkBitmap bm;
reed@google.com82065d62011-02-07 15:30:46 +000033
msarettd15750c2016-03-18 15:48:49 -070034// decode_file("/skimages/progressivejpg.jpg", &bm);
35 decode_file("/skimages/logo.png", &bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 size->set(bm.width(), bm.height());
reed8a21c9f2016-03-08 18:50:00 -080037 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
38 SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
reed8a21c9f2016-03-08 18:50:00 -080041static sk_sp<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 };
reed8a21c9f2016-03-08 18:50:00 -080045 return SkGradientShader::MakeLinear(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
Mike Reeddf85c382017-02-14 10:59:19 -050091 SkScalar uv = u * v;
92 SkScalar Uv = (1 - u) * v;
93 SkScalar uV = u * (1 - v);
94 SkScalar UV = (1 - u) * (1 - v);
reed@google.com82065d62011-02-07 15:30:46 +000095
Mike Reeddf85c382017-02-14 10:59:19 -050096 SkScalar x0 = UV * edge[TL].fX + uV * edge[TR].fX + Uv * edge[BL].fX + uv * edge[BR].fX;
97 SkScalar y0 = UV * edge[TL].fY + uV * edge[TR].fY + Uv * edge[BL].fY + uv * edge[BR].fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
Mike Reeddf85c382017-02-14 10:59:19 -050099 SkScalar x = (1 - v) * edge[TL+iu].fX + u * edge[TR+iv].fX +
100 v * edge[BR+nu-iu].fX + (1 - u) * edge[BL+nv-iv].fX - x0;
101 SkScalar y = (1 - v) * edge[TL+iu].fY + u * edge[TR+iv].fY +
102 v * edge[BR+nu-iu].fY + (1 - u) * edge[BL+nv-iv].fY - y0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 pt->set(x, y);
104}
105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106static SkColor make_color(SkScalar s, SkScalar t) {
benjaminwagnere7be3e52016-03-01 13:44:10 -0800107 return SkColorSetARGB(0xFF, SkUnitScalarClampToByte(s), SkUnitScalarClampToByte(t), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108}
109
110void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
111 bool doTextures, bool doColors) {
112 if (nu < 1 || nv < 1) {
113 return;
114 }
115
116 int i, npts = (nu + nv) * 2;
117 SkAutoSTMalloc<16, SkPoint> storage(npts + 1);
118 SkPoint* edge0 = storage.get();
119 SkPoint* edge1 = edge0 + nu;
120 SkPoint* edge2 = edge1 + nv;
121 SkPoint* edge3 = edge2 + nu;
reed@google.com82065d62011-02-07 15:30:46 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 // evaluate the edge points
124 eval_patch_edge(fPts + 0, edge0, nu);
125 eval_patch_edge(fPts + 3, edge1, nv);
126 eval_patch_edge(fPts + 6, edge2, nu);
127 eval_patch_edge(fPts + 9, edge3, nv);
128 edge3[nv] = edge0[0]; // the last shall be first
reed@google.com82065d62011-02-07 15:30:46 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 for (i = 0; i < npts; i++) {
131// canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
132 }
reed@google.com82065d62011-02-07 15:30:46 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 int row, vertCount = (nu + 1) * (nv + 1);
135 SkAutoTMalloc<SkPoint> vertStorage(vertCount);
136 SkPoint* verts = vertStorage.get();
reed@google.com82065d62011-02-07 15:30:46 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 // first row
139 memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
140 // rows
141 SkPoint* r = verts;
142 for (row = 1; row < nv; row++) {
143 r += nu + 1;
144 r[0] = edge3[nv - row];
145 for (int col = 1; col < nu; col++) {
146 eval_sheet(edge0, nu, nv, col, row, &r[col]);
147 }
148 r[nu] = edge1[row];
149 }
150 // last row
151 SkPoint* last = verts + nv * (nu + 1);
152 for (i = 0; i <= nu; i++) {
153 last[i] = edge2[nu - i];
154 }
reed@google.com82065d62011-02-07 15:30:46 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156// canvas->drawPoints(verts, vertCount, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 int stripCount = (nu + 1) * 2;
159 SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2);
160 SkAutoTMalloc<SkColor> colorStorage(stripCount);
161 SkPoint* strip = stripStorage.get();
162 SkPoint* tex = strip + stripCount;
163 SkColor* colors = colorStorage.get();
164 SkScalar t = 0;
165 const SkScalar ds = SK_Scalar1 * fW / nu;
166 const SkScalar dt = SK_Scalar1 * fH / nv;
167 r = verts;
168 for (row = 0; row < nv; row++) {
169 SkPoint* upper = r;
170 SkPoint* lower = r + nu + 1;
171 r = lower;
172 SkScalar s = 0;
173 for (i = 0; i <= nu; i++) {
174 strip[i*2 + 0] = *upper++;
175 strip[i*2 + 1] = *lower++;
176 tex[i*2 + 0].set(s, t);
177 tex[i*2 + 1].set(s, t + dt);
178 colors[i*2 + 0] = make_color(s/fW, t/fH);
179 colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH);
180 s += ds;
181 }
182 t += dt;
Mike Reed887cdf12017-04-03 11:11:09 -0400183 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleStrip_VertexMode, stripCount,
184 strip, doTextures ? tex : nullptr,
185 doColors ? colors : nullptr),
186 SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 }
188}
189
190static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
191 Patch* patch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 SkAutoCanvasRestore ar(canvas, true);
193
reedcdf2db92014-08-06 09:54:19 -0700194 patch->draw(canvas, paint, nu, nv, false, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000195 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700196 patch->draw(canvas, paint, nu, nv, true, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000197 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700198 patch->draw(canvas, paint, nu, nv, false, true);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000199 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700200 patch->draw(canvas, paint, nu, nv, true, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201}
202
reed147476d2014-08-06 07:00:27 -0700203const SkScalar DX = 20;
204const SkScalar DY = 0;
205
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000206class PatchView : public SampleView {
reed339cdbf2015-02-05 22:02:37 -0800207 SkScalar fAngle;
reed8a21c9f2016-03-08 18:50:00 -0800208 sk_sp<SkShader> fShader0;
209 sk_sp<SkShader> fShader1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 SkIPoint fSize0, fSize1;
211 SkPoint fPts[12];
reed@google.com82065d62011-02-07 15:30:46 +0000212
213public:
reed339cdbf2015-02-05 22:02:37 -0800214 PatchView() : fAngle(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 fShader0 = make_shader0(&fSize0);
216 fSize1 = fSize0;
217 if (fSize0.fX == 0 || fSize0.fY == 0) {
218 fSize1.set(2, 2);
219 }
220 fShader1 = make_shader1(fSize1);
221
reed@android.com6b82d1a2009-06-03 02:35:01 +0000222 const SkScalar S = SkIntToScalar(50);
223 const SkScalar T = SkIntToScalar(40);
224 fPts[0].set(S*0, T);
225 fPts[1].set(S*1, T);
226 fPts[2].set(S*2, T);
227 fPts[3].set(S*3, T);
228 fPts[4].set(S*3, T*2);
229 fPts[5].set(S*3, T*3);
230 fPts[6].set(S*3, T*4);
231 fPts[7].set(S*2, T*4);
232 fPts[8].set(S*1, T*4);
233 fPts[9].set(S*0, T*4);
234 fPts[10].set(S*0, T*3);
235 fPts[11].set(S*0, T*2);
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000236
237 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 }
reed@google.com82065d62011-02-07 15:30:46 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240protected:
241 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700242 bool onQuery(SkEvent* evt) override {
tfarina@chromium.org45369a32012-09-30 11:30:01 +0000243 if (SampleCode::TitleQ(*evt)) {
244 SampleCode::TitleR(evt, "Patch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 return true;
246 }
247 return this->INHERITED::onQuery(evt);
248 }
reed@google.com82065d62011-02-07 15:30:46 +0000249
mtkleinf0599002015-07-13 06:18:39 -0700250 void onDrawContent(SkCanvas* canvas) override {
reedcdf2db92014-08-06 09:54:19 -0700251 const int nu = 10;
252 const int nv = 10;
253
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 SkPaint paint;
255 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700256 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +0000257
reed147476d2014-08-06 07:00:27 -0700258 canvas->translate(DX, DY);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 Patch patch;
reed@google.com82065d62011-02-07 15:30:46 +0000261
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 paint.setShader(fShader0);
263 if (fSize0.fX == 0) {
264 fSize0.fX = 1;
265 }
266 if (fSize0.fY == 0) {
267 fSize0.fY = 1;
268 }
269 patch.setBounds(fSize0.fX, fSize0.fY);
reed@google.com82065d62011-02-07 15:30:46 +0000270
271 patch.setPatch(fPts);
reedcdf2db92014-08-06 09:54:19 -0700272 drawpatches(canvas, paint, nu, nv, &patch);
reed@google.com82065d62011-02-07 15:30:46 +0000273
halcanary96fcdcc2015-08-27 07:41:13 -0700274 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 paint.setAntiAlias(true);
276 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.comf2b98d62010-12-20 18:26:13 +0000277 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000278
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 canvas->translate(0, SkIntToScalar(300));
reed@google.com82065d62011-02-07 15:30:46 +0000280
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 paint.setAntiAlias(false);
282 paint.setShader(fShader1);
reedcdf2db92014-08-06 09:54:19 -0700283 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000284 SkMatrix m;
285 m.setSkew(1, 0);
reed150835e2016-03-10 06:36:49 -0800286 paint.setShader(paint.getShader()->makeWithLocalMatrix(m));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000287 }
reedcdf2db92014-08-06 09:54:19 -0700288 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000289 SkMatrix m;
reed339cdbf2015-02-05 22:02:37 -0800290 m.setRotate(fAngle);
reed150835e2016-03-10 06:36:49 -0800291 paint.setShader(paint.getShader()->makeWithLocalMatrix(m));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000292 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 patch.setBounds(fSize1.fX, fSize1.fY);
reedcdf2db92014-08-06 09:54:19 -0700294 drawpatches(canvas, paint, nu, nv, &patch);
reed339cdbf2015-02-05 22:02:37 -0800295 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000296
mtklein36352bf2015-03-25 18:17:31 -0700297 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800298 fAngle = timer.scaled(60, 360);
299 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 }
reed@google.com82065d62011-02-07 15:30:46 +0000301
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 class PtClick : public Click {
303 public:
304 int fIndex;
305 PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
306 };
reed@google.com82065d62011-02-07 15:30:46 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
309 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
310 }
reed@google.com82065d62011-02-07 15:30:46 +0000311
mtkleinf0599002015-07-13 06:18:39 -0700312 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed147476d2014-08-06 07:00:27 -0700313 x -= DX;
314 y -= DY;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000315 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 if (hittest(fPts[i], x, y)) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000317 return new PtClick(this, (int)i);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 }
319 }
reed@google.com4d5c26d2013-01-08 16:17:50 +0000320 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 }
reed@google.com82065d62011-02-07 15:30:46 +0000322
mtkleinf0599002015-07-13 06:18:39 -0700323 bool onClick(Click* click) override {
reed147476d2014-08-06 07:00:27 -0700324 fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY);
halcanary96fcdcc2015-08-27 07:41:13 -0700325 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 return true;
327 }
reed@google.com82065d62011-02-07 15:30:46 +0000328
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000330 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331};
332
333//////////////////////////////////////////////////////////////////////////////
334
335static SkView* MyFactory() { return new PatchView; }
336static SkViewRegister reg(MyFactory);