blob: edc7804fb4a6afe6ff56652dac35206fb8e507d6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#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"
19#include "SkXfermode.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"
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkOSFile.h"
26#include "SkStream.h"
27
28#include "SkGeometry.h" // private include :(
29
30static void drawtriangle(SkCanvas* canvas, const SkPaint& paint,
31 const SkPoint pts[3]) {
32 SkPath path;
reed@google.com82065d62011-02-07 15:30:46 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 path.moveTo(pts[0]);
35 path.lineTo(pts[1]);
36 path.lineTo(pts[2]);
reed@google.com82065d62011-02-07 15:30:46 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 canvas->drawPath(path, paint);
39}
40
41static SkShader* make_shader0(SkIPoint* size) {
42 SkBitmap bm;
reed@google.com82065d62011-02-07 15:30:46 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044// SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm);
reed@android.comf2b98d62010-12-20 18:26:13 +000045 SkImageDecoder::DecodeFile("/skimages/logo.png", &bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 size->set(bm.width(), bm.height());
47 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
48 SkShader::kClamp_TileMode);
49}
50
51static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000052 SkPoint pts[] = { { 0, 0, },
53 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
55 return SkGradientShader::CreateLinear(pts, colors, NULL,
56 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL);
57}
58
59///////////////////////////////////////////////////////////////////////////////
60
61class Patch {
62public:
reed@android.com4516f472009-06-29 16:25:36 +000063 Patch() { sk_bzero(fPts, sizeof(fPts)); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 ~Patch() {}
reed@google.com82065d62011-02-07 15:30:46 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 void setPatch(const SkPoint pts[12]) {
67 memcpy(fPts, pts, 12 * sizeof(SkPoint));
68 fPts[12] = pts[0]; // the last shall be first
69 }
70 void setBounds(int w, int h) { fW = w; fH = h; }
71
72 void draw(SkCanvas*, const SkPaint&, int segsU, int segsV,
73 bool doTextures, bool doColors);
reed@google.com82065d62011-02-07 15:30:46 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075private:
76 SkPoint fPts[13];
77 int fW, fH;
78};
79
80static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) {
81 SkScalar t = 0;
82 SkScalar dt = SK_Scalar1 / segs;
83
84 samples[0] = cubic[0];
85 for (int i = 1; i < segs; i++) {
86 t += dt;
87 SkEvalCubicAt(cubic, t, &samples[i], NULL, NULL);
88 }
89}
90
91static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv,
92 SkPoint* pt) {
93 const int TL = 0;
94 const int TR = nu;
95 const int BR = TR + nv;
96 const int BL = BR + nu;
97
98 SkScalar u = SkIntToScalar(iu) / nu;
99 SkScalar v = SkIntToScalar(iv) / nv;
reed@google.com82065d62011-02-07 15:30:46 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 SkScalar uv = SkScalarMul(u, v);
102 SkScalar Uv = SkScalarMul(SK_Scalar1 - u, v);
103 SkScalar uV = SkScalarMul(u, SK_Scalar1 - v);
104 SkScalar UV = SkScalarMul(SK_Scalar1 - u, SK_Scalar1 - v);
reed@google.com82065d62011-02-07 15:30:46 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkScalar x0 = SkScalarMul(UV, edge[TL].fX) + SkScalarMul(uV, edge[TR].fX) +
107 SkScalarMul(Uv, edge[BL].fX) + SkScalarMul(uv, edge[BR].fX);
108 SkScalar y0 = SkScalarMul(UV, edge[TL].fY) + SkScalarMul(uV, edge[TR].fY) +
109 SkScalarMul(Uv, edge[BL].fY) + SkScalarMul(uv, edge[BR].fY);
110
111 SkScalar x = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fX) +
112 SkScalarMul(u, edge[TR+iv].fX) +
113 SkScalarMul(v, edge[BR+nu-iu].fX) +
114 SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fX) - x0;
115 SkScalar y = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fY) +
116 SkScalarMul(u, edge[TR+iv].fY) +
117 SkScalarMul(v, edge[BR+nu-iu].fY) +
118 SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fY) - y0;
119 pt->set(x, y);
120}
121
122static int ScalarTo255(SkScalar v) {
123 int scale = SkScalarToFixed(v) >> 8;
124 if (scale < 0) {
125 scale = 0;
126 } else if (scale > 255) {
127 scale = 255;
128 }
129 return scale;
130}
131
132static SkColor make_color(SkScalar s, SkScalar t) {
133 int cs = ScalarTo255(s);
reed@google.com82065d62011-02-07 15:30:46 +0000134 int ct = ScalarTo255(t);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0);
136}
137
138void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
139 bool doTextures, bool doColors) {
140 if (nu < 1 || nv < 1) {
141 return;
142 }
143
144 int i, npts = (nu + nv) * 2;
145 SkAutoSTMalloc<16, SkPoint> storage(npts + 1);
146 SkPoint* edge0 = storage.get();
147 SkPoint* edge1 = edge0 + nu;
148 SkPoint* edge2 = edge1 + nv;
149 SkPoint* edge3 = edge2 + nu;
reed@google.com82065d62011-02-07 15:30:46 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 // evaluate the edge points
152 eval_patch_edge(fPts + 0, edge0, nu);
153 eval_patch_edge(fPts + 3, edge1, nv);
154 eval_patch_edge(fPts + 6, edge2, nu);
155 eval_patch_edge(fPts + 9, edge3, nv);
156 edge3[nv] = edge0[0]; // the last shall be first
reed@google.com82065d62011-02-07 15:30:46 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 for (i = 0; i < npts; i++) {
159// canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
160 }
reed@google.com82065d62011-02-07 15:30:46 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 int row, vertCount = (nu + 1) * (nv + 1);
163 SkAutoTMalloc<SkPoint> vertStorage(vertCount);
164 SkPoint* verts = vertStorage.get();
reed@google.com82065d62011-02-07 15:30:46 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 // first row
167 memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
168 // rows
169 SkPoint* r = verts;
170 for (row = 1; row < nv; row++) {
171 r += nu + 1;
172 r[0] = edge3[nv - row];
173 for (int col = 1; col < nu; col++) {
174 eval_sheet(edge0, nu, nv, col, row, &r[col]);
175 }
176 r[nu] = edge1[row];
177 }
178 // last row
179 SkPoint* last = verts + nv * (nu + 1);
180 for (i = 0; i <= nu; i++) {
181 last[i] = edge2[nu - i];
182 }
reed@google.com82065d62011-02-07 15:30:46 +0000183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184// canvas->drawPoints(verts, vertCount, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 int stripCount = (nu + 1) * 2;
187 SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2);
188 SkAutoTMalloc<SkColor> colorStorage(stripCount);
189 SkPoint* strip = stripStorage.get();
190 SkPoint* tex = strip + stripCount;
191 SkColor* colors = colorStorage.get();
192 SkScalar t = 0;
193 const SkScalar ds = SK_Scalar1 * fW / nu;
194 const SkScalar dt = SK_Scalar1 * fH / nv;
195 r = verts;
196 for (row = 0; row < nv; row++) {
197 SkPoint* upper = r;
198 SkPoint* lower = r + nu + 1;
199 r = lower;
200 SkScalar s = 0;
201 for (i = 0; i <= nu; i++) {
202 strip[i*2 + 0] = *upper++;
203 strip[i*2 + 1] = *lower++;
204 tex[i*2 + 0].set(s, t);
205 tex[i*2 + 1].set(s, t + dt);
206 colors[i*2 + 0] = make_color(s/fW, t/fH);
207 colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH);
208 s += ds;
209 }
210 t += dt;
211 canvas->drawVertices(SkCanvas::kTriangleStrip_VertexMode, stripCount,
212 strip, doTextures ? tex : NULL,
213 doColors ? colors : NULL, NULL,
214 NULL, 0, paint);
215 }
216}
217
218static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
219 Patch* patch) {
220
221 SkAutoCanvasRestore ar(canvas, true);
222
223 patch->draw(canvas, paint, 10, 10, false, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000224 canvas->translate(SkIntToScalar(180), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 patch->draw(canvas, paint, 10, 10, true, false);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000226 canvas->translate(SkIntToScalar(180), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 patch->draw(canvas, paint, 10, 10, false, true);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000228 canvas->translate(SkIntToScalar(180), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 patch->draw(canvas, paint, 10, 10, true, true);
230}
231
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000232class PatchView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 SkShader* fShader0;
234 SkShader* fShader1;
235 SkIPoint fSize0, fSize1;
236 SkPoint fPts[12];
reed@google.com82065d62011-02-07 15:30:46 +0000237
238public:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 PatchView() {
240 fShader0 = make_shader0(&fSize0);
241 fSize1 = fSize0;
242 if (fSize0.fX == 0 || fSize0.fY == 0) {
243 fSize1.set(2, 2);
244 }
245 fShader1 = make_shader1(fSize1);
246
reed@android.com6b82d1a2009-06-03 02:35:01 +0000247 const SkScalar S = SkIntToScalar(50);
248 const SkScalar T = SkIntToScalar(40);
249 fPts[0].set(S*0, T);
250 fPts[1].set(S*1, T);
251 fPts[2].set(S*2, T);
252 fPts[3].set(S*3, T);
253 fPts[4].set(S*3, T*2);
254 fPts[5].set(S*3, T*3);
255 fPts[6].set(S*3, T*4);
256 fPts[7].set(S*2, T*4);
257 fPts[8].set(S*1, T*4);
258 fPts[9].set(S*0, T*4);
259 fPts[10].set(S*0, T*3);
260 fPts[11].set(S*0, T*2);
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000261
262 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 }
reed@google.com82065d62011-02-07 15:30:46 +0000264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 virtual ~PatchView() {
reed@google.com82065d62011-02-07 15:30:46 +0000266 SkSafeUnref(fShader0);
267 SkSafeUnref(fShader1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 }
reed@google.com82065d62011-02-07 15:30:46 +0000269
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270protected:
271 // overrides from SkEventSink
272 virtual bool onQuery(SkEvent* evt) {
273 if (SampleCode::TitleQ(*evt))
274 {
275 SkString str("Patch");
276 SampleCode::TitleR(evt, str.c_str());
277 return true;
278 }
279 return this->INHERITED::onQuery(evt);
280 }
reed@google.com82065d62011-02-07 15:30:46 +0000281
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000282 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 SkPaint paint;
284 paint.setDither(true);
285 paint.setFilterBitmap(true);
reed@google.com82065d62011-02-07 15:30:46 +0000286
reed@android.com6b82d1a2009-06-03 02:35:01 +0000287 canvas->translate(SkIntToScalar(20), 0);
288
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 Patch patch;
reed@google.com82065d62011-02-07 15:30:46 +0000290
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 paint.setShader(fShader0);
292 if (fSize0.fX == 0) {
293 fSize0.fX = 1;
294 }
295 if (fSize0.fY == 0) {
296 fSize0.fY = 1;
297 }
298 patch.setBounds(fSize0.fX, fSize0.fY);
reed@google.com82065d62011-02-07 15:30:46 +0000299
300 patch.setPatch(fPts);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 drawpatches(canvas, paint, 10, 10, &patch);
reed@google.com82065d62011-02-07 15:30:46 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 paint.setShader(NULL);
304 paint.setAntiAlias(true);
305 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.comf2b98d62010-12-20 18:26:13 +0000306 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 canvas->translate(0, SkIntToScalar(300));
reed@google.com82065d62011-02-07 15:30:46 +0000309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 paint.setAntiAlias(false);
311 paint.setShader(fShader1);
312 patch.setBounds(fSize1.fX, fSize1.fY);
313 drawpatches(canvas, paint, 10, 10, &patch);
314 }
reed@google.com82065d62011-02-07 15:30:46 +0000315
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 class PtClick : public Click {
317 public:
318 int fIndex;
319 PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
320 };
reed@google.com82065d62011-02-07 15:30:46 +0000321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
323 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
324 }
reed@google.com82065d62011-02-07 15:30:46 +0000325
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000327 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 if (hittest(fPts[i], x, y)) {
329 return new PtClick(this, i);
330 }
331 }
332 return this->INHERITED::onFindClickHandler(x, y);
333 }
reed@google.com82065d62011-02-07 15:30:46 +0000334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 virtual bool onClick(Click* click) {
336 fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX, click->fCurr.fY);
337 this->inval(NULL);
338 return true;
339 }
reed@google.com82065d62011-02-07 15:30:46 +0000340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000342 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343};
344
345//////////////////////////////////////////////////////////////////////////////
346
347static SkView* MyFactory() { return new PatchView; }
348static SkViewRegister reg(MyFactory);
349