blob: 7b62d9d802184a480a01d0a7264ca4eb9fe5209c [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"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04009#include "Sample.h"
reed339cdbf2015-02-05 22:02:37 -080010#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRandom.h"
16#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040023#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkOSFile.h"
26#include "SkStream.h"
27
28#include "SkGeometry.h" // private include :(
29
reed8a21c9f2016-03-08 18:50:00 -080030static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkBitmap bm;
reed@google.com82065d62011-02-07 15:30:46 +000032
msarettd15750c2016-03-18 15:48:49 -070033// decode_file("/skimages/progressivejpg.jpg", &bm);
34 decode_file("/skimages/logo.png", &bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 size->set(bm.width(), bm.height());
reed8a21c9f2016-03-08 18:50:00 -080036 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
37 SkShader::kClamp_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038}
39
reed8a21c9f2016-03-08 18:50:00 -080040static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000041 SkPoint pts[] = { { 0, 0, },
42 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -080044 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000045 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046}
47
48///////////////////////////////////////////////////////////////////////////////
49
50class Patch {
51public:
reed@android.com4516f472009-06-29 16:25:36 +000052 Patch() { sk_bzero(fPts, sizeof(fPts)); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 ~Patch() {}
reed@google.com82065d62011-02-07 15:30:46 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 void setPatch(const SkPoint pts[12]) {
56 memcpy(fPts, pts, 12 * sizeof(SkPoint));
57 fPts[12] = pts[0]; // the last shall be first
58 }
59 void setBounds(int w, int h) { fW = w; fH = h; }
60
61 void draw(SkCanvas*, const SkPaint&, int segsU, int segsV,
62 bool doTextures, bool doColors);
reed@google.com82065d62011-02-07 15:30:46 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064private:
65 SkPoint fPts[13];
66 int fW, fH;
67};
68
69static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) {
70 SkScalar t = 0;
71 SkScalar dt = SK_Scalar1 / segs;
72
73 samples[0] = cubic[0];
74 for (int i = 1; i < segs; i++) {
75 t += dt;
halcanary96fcdcc2015-08-27 07:41:13 -070076 SkEvalCubicAt(cubic, t, &samples[i], nullptr, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 }
78}
79
80static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv,
81 SkPoint* pt) {
82 const int TL = 0;
83 const int TR = nu;
84 const int BR = TR + nv;
85 const int BL = BR + nu;
86
87 SkScalar u = SkIntToScalar(iu) / nu;
88 SkScalar v = SkIntToScalar(iv) / nv;
reed@google.com82065d62011-02-07 15:30:46 +000089
Mike Reeddf85c382017-02-14 10:59:19 -050090 SkScalar uv = u * v;
91 SkScalar Uv = (1 - u) * v;
92 SkScalar uV = u * (1 - v);
93 SkScalar UV = (1 - u) * (1 - v);
reed@google.com82065d62011-02-07 15:30:46 +000094
Mike Reeddf85c382017-02-14 10:59:19 -050095 SkScalar x0 = UV * edge[TL].fX + uV * edge[TR].fX + Uv * edge[BL].fX + uv * edge[BR].fX;
96 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 +000097
Mike Reeddf85c382017-02-14 10:59:19 -050098 SkScalar x = (1 - v) * edge[TL+iu].fX + u * edge[TR+iv].fX +
99 v * edge[BR+nu-iu].fX + (1 - u) * edge[BL+nv-iv].fX - x0;
100 SkScalar y = (1 - v) * edge[TL+iu].fY + u * edge[TR+iv].fY +
101 v * edge[BR+nu-iu].fY + (1 - u) * edge[BL+nv-iv].fY - y0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 pt->set(x, y);
103}
104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105static SkColor make_color(SkScalar s, SkScalar t) {
benjaminwagnere7be3e52016-03-01 13:44:10 -0800106 return SkColorSetARGB(0xFF, SkUnitScalarClampToByte(s), SkUnitScalarClampToByte(t), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107}
108
109void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
110 bool doTextures, bool doColors) {
111 if (nu < 1 || nv < 1) {
112 return;
113 }
114
115 int i, npts = (nu + nv) * 2;
116 SkAutoSTMalloc<16, SkPoint> storage(npts + 1);
117 SkPoint* edge0 = storage.get();
118 SkPoint* edge1 = edge0 + nu;
119 SkPoint* edge2 = edge1 + nv;
120 SkPoint* edge3 = edge2 + nu;
reed@google.com82065d62011-02-07 15:30:46 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 // evaluate the edge points
123 eval_patch_edge(fPts + 0, edge0, nu);
124 eval_patch_edge(fPts + 3, edge1, nv);
125 eval_patch_edge(fPts + 6, edge2, nu);
126 eval_patch_edge(fPts + 9, edge3, nv);
127 edge3[nv] = edge0[0]; // the last shall be first
reed@google.com82065d62011-02-07 15:30:46 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 for (i = 0; i < npts; i++) {
130// canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
131 }
reed@google.com82065d62011-02-07 15:30:46 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 int row, vertCount = (nu + 1) * (nv + 1);
134 SkAutoTMalloc<SkPoint> vertStorage(vertCount);
135 SkPoint* verts = vertStorage.get();
reed@google.com82065d62011-02-07 15:30:46 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 // first row
138 memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
139 // rows
140 SkPoint* r = verts;
141 for (row = 1; row < nv; row++) {
142 r += nu + 1;
143 r[0] = edge3[nv - row];
144 for (int col = 1; col < nu; col++) {
145 eval_sheet(edge0, nu, nv, col, row, &r[col]);
146 }
147 r[nu] = edge1[row];
148 }
149 // last row
150 SkPoint* last = verts + nv * (nu + 1);
151 for (i = 0; i <= nu; i++) {
152 last[i] = edge2[nu - i];
153 }
reed@google.com82065d62011-02-07 15:30:46 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155// canvas->drawPoints(verts, vertCount, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 int stripCount = (nu + 1) * 2;
158 SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2);
159 SkAutoTMalloc<SkColor> colorStorage(stripCount);
160 SkPoint* strip = stripStorage.get();
161 SkPoint* tex = strip + stripCount;
162 SkColor* colors = colorStorage.get();
163 SkScalar t = 0;
164 const SkScalar ds = SK_Scalar1 * fW / nu;
165 const SkScalar dt = SK_Scalar1 * fH / nv;
166 r = verts;
167 for (row = 0; row < nv; row++) {
168 SkPoint* upper = r;
169 SkPoint* lower = r + nu + 1;
170 r = lower;
171 SkScalar s = 0;
172 for (i = 0; i <= nu; i++) {
173 strip[i*2 + 0] = *upper++;
174 strip[i*2 + 1] = *lower++;
175 tex[i*2 + 0].set(s, t);
176 tex[i*2 + 1].set(s, t + dt);
177 colors[i*2 + 0] = make_color(s/fW, t/fH);
178 colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH);
179 s += ds;
180 }
181 t += dt;
Mike Reed887cdf12017-04-03 11:11:09 -0400182 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleStrip_VertexMode, stripCount,
183 strip, doTextures ? tex : nullptr,
184 doColors ? colors : nullptr),
185 SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 }
187}
188
189static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
190 Patch* patch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 SkAutoCanvasRestore ar(canvas, true);
192
reedcdf2db92014-08-06 09:54:19 -0700193 patch->draw(canvas, paint, nu, nv, false, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000194 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700195 patch->draw(canvas, paint, nu, nv, true, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000196 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700197 patch->draw(canvas, paint, nu, nv, false, true);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000198 canvas->translate(SkIntToScalar(180), 0);
reedcdf2db92014-08-06 09:54:19 -0700199 patch->draw(canvas, paint, nu, nv, true, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200}
201
reed147476d2014-08-06 07:00:27 -0700202const SkScalar DX = 20;
203const SkScalar DY = 0;
204
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400205class PatchView : public Sample {
reed339cdbf2015-02-05 22:02:37 -0800206 SkScalar fAngle;
reed8a21c9f2016-03-08 18:50:00 -0800207 sk_sp<SkShader> fShader0;
208 sk_sp<SkShader> fShader1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 SkIPoint fSize0, fSize1;
210 SkPoint fPts[12];
reed@google.com82065d62011-02-07 15:30:46 +0000211
212public:
reed339cdbf2015-02-05 22:02:37 -0800213 PatchView() : fAngle(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 fShader0 = make_shader0(&fSize0);
215 fSize1 = fSize0;
216 if (fSize0.fX == 0 || fSize0.fY == 0) {
217 fSize1.set(2, 2);
218 }
219 fShader1 = make_shader1(fSize1);
220
reed@android.com6b82d1a2009-06-03 02:35:01 +0000221 const SkScalar S = SkIntToScalar(50);
222 const SkScalar T = SkIntToScalar(40);
223 fPts[0].set(S*0, T);
224 fPts[1].set(S*1, T);
225 fPts[2].set(S*2, T);
226 fPts[3].set(S*3, T);
227 fPts[4].set(S*3, T*2);
228 fPts[5].set(S*3, T*3);
229 fPts[6].set(S*3, T*4);
230 fPts[7].set(S*2, T*4);
231 fPts[8].set(S*1, T*4);
232 fPts[9].set(S*0, T*4);
233 fPts[10].set(S*0, T*3);
234 fPts[11].set(S*0, T*2);
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000235
236 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 }
reed@google.com82065d62011-02-07 15:30:46 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400240 bool onQuery(Sample::Event* evt) override {
241 if (Sample::TitleQ(*evt)) {
242 Sample::TitleR(evt, "Patch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 return true;
244 }
245 return this->INHERITED::onQuery(evt);
246 }
reed@google.com82065d62011-02-07 15:30:46 +0000247
mtkleinf0599002015-07-13 06:18:39 -0700248 void onDrawContent(SkCanvas* canvas) override {
reedcdf2db92014-08-06 09:54:19 -0700249 const int nu = 10;
250 const int nv = 10;
251
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 SkPaint paint;
253 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700254 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +0000255
reed147476d2014-08-06 07:00:27 -0700256 canvas->translate(DX, DY);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 Patch patch;
reed@google.com82065d62011-02-07 15:30:46 +0000259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 paint.setShader(fShader0);
261 if (fSize0.fX == 0) {
262 fSize0.fX = 1;
263 }
264 if (fSize0.fY == 0) {
265 fSize0.fY = 1;
266 }
267 patch.setBounds(fSize0.fX, fSize0.fY);
reed@google.com82065d62011-02-07 15:30:46 +0000268
269 patch.setPatch(fPts);
reedcdf2db92014-08-06 09:54:19 -0700270 drawpatches(canvas, paint, nu, nv, &patch);
reed@google.com82065d62011-02-07 15:30:46 +0000271
halcanary96fcdcc2015-08-27 07:41:13 -0700272 paint.setShader(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 paint.setAntiAlias(true);
274 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.comf2b98d62010-12-20 18:26:13 +0000275 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 canvas->translate(0, SkIntToScalar(300));
reed@google.com82065d62011-02-07 15:30:46 +0000278
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 paint.setAntiAlias(false);
280 paint.setShader(fShader1);
reedcdf2db92014-08-06 09:54:19 -0700281 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000282 SkMatrix m;
283 m.setSkew(1, 0);
reed150835e2016-03-10 06:36:49 -0800284 paint.setShader(paint.getShader()->makeWithLocalMatrix(m));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000285 }
reedcdf2db92014-08-06 09:54:19 -0700286 if (true) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000287 SkMatrix m;
reed339cdbf2015-02-05 22:02:37 -0800288 m.setRotate(fAngle);
reed150835e2016-03-10 06:36:49 -0800289 paint.setShader(paint.getShader()->makeWithLocalMatrix(m));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000290 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 patch.setBounds(fSize1.fX, fSize1.fY);
reedcdf2db92014-08-06 09:54:19 -0700292 drawpatches(canvas, paint, nu, nv, &patch);
reed339cdbf2015-02-05 22:02:37 -0800293 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000294
mtklein36352bf2015-03-25 18:17:31 -0700295 bool onAnimate(const SkAnimTimer& timer) override {
reed339cdbf2015-02-05 22:02:37 -0800296 fAngle = timer.scaled(60, 360);
297 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 }
reed@google.com82065d62011-02-07 15:30:46 +0000299
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 class PtClick : public Click {
301 public:
302 int fIndex;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400303 PtClick(Sample* view, int index) : Click(view), fIndex(index) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 };
reed@google.com82065d62011-02-07 15:30:46 +0000305
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
307 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
308 }
reed@google.com82065d62011-02-07 15:30:46 +0000309
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400310 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed147476d2014-08-06 07:00:27 -0700311 x -= DX;
312 y -= DY;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000313 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 if (hittest(fPts[i], x, y)) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000315 return new PtClick(this, (int)i);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 }
317 }
reed@google.com4d5c26d2013-01-08 16:17:50 +0000318 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 }
reed@google.com82065d62011-02-07 15:30:46 +0000320
mtkleinf0599002015-07-13 06:18:39 -0700321 bool onClick(Click* click) override {
reed147476d2014-08-06 07:00:27 -0700322 fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 return true;
324 }
reed@google.com82065d62011-02-07 15:30:46 +0000325
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400327 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328};
329
330//////////////////////////////////////////////////////////////////////////////
331
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400332DEF_SAMPLE( return new PatchView(); )