2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
14 #include "SkImageDecoder.h"
20 #include "SkXfermode.h"
21 #include "SkColorPriv.h"
22 #include "SkColorFilter.h"
24 #include "SkTypeface.h"
29 #include "SkGeometry.h" // private include :(
31 static SkShader* make_shader0(SkIPoint* size) {
34 // SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm);
35 SkImageDecoder::DecodeFile("/skimages/logo.png", &bm);
36 size->set(bm.width(), bm.height());
37 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
38 SkShader::kClamp_TileMode);
41 static SkShader* make_shader1(const SkIPoint& size) {
42 SkPoint pts[] = { { 0, 0, },
43 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
44 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
45 return SkGradientShader::CreateLinear(pts, colors, NULL,
46 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
49 ///////////////////////////////////////////////////////////////////////////////
53 Patch() { sk_bzero(fPts, sizeof(fPts)); }
56 void setPatch(const SkPoint pts[12]) {
57 memcpy(fPts, pts, 12 * sizeof(SkPoint));
58 fPts[12] = pts[0]; // the last shall be first
60 void setBounds(int w, int h) { fW = w; fH = h; }
62 void draw(SkCanvas*, const SkPaint&, int segsU, int segsV,
63 bool doTextures, bool doColors);
70 static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) {
72 SkScalar dt = SK_Scalar1 / segs;
74 samples[0] = cubic[0];
75 for (int i = 1; i < segs; i++) {
77 SkEvalCubicAt(cubic, t, &samples[i], NULL, NULL);
81 static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv,
85 const int BR = TR + nv;
86 const int BL = BR + nu;
88 SkScalar u = SkIntToScalar(iu) / nu;
89 SkScalar v = SkIntToScalar(iv) / nv;
91 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);
96 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);
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;
112 static int ScalarTo255(SkScalar v) {
113 int scale = SkScalarToFixed(v) >> 8;
116 } else if (scale > 255) {
122 static SkColor make_color(SkScalar s, SkScalar t) {
123 int cs = ScalarTo255(s);
124 int ct = ScalarTo255(t);
125 return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0);
128 void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
129 bool doTextures, bool doColors) {
130 if (nu < 1 || nv < 1) {
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;
141 // 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
148 for (i = 0; i < npts; i++) {
149 // canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
152 int row, vertCount = (nu + 1) * (nv + 1);
153 SkAutoTMalloc<SkPoint> vertStorage(vertCount);
154 SkPoint* verts = vertStorage.get();
157 memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
160 for (row = 1; row < nv; row++) {
162 r[0] = edge3[nv - row];
163 for (int col = 1; col < nu; col++) {
164 eval_sheet(edge0, nu, nv, col, row, &r[col]);
169 SkPoint* last = verts + nv * (nu + 1);
170 for (i = 0; i <= nu; i++) {
171 last[i] = edge2[nu - i];
174 // canvas->drawPoints(verts, vertCount, paint);
176 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();
183 const SkScalar ds = SK_Scalar1 * fW / nu;
184 const SkScalar dt = SK_Scalar1 * fH / nv;
186 for (row = 0; row < nv; row++) {
188 SkPoint* lower = r + nu + 1;
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);
201 canvas->drawVertices(SkCanvas::kTriangleStrip_VertexMode, stripCount,
202 strip, doTextures ? tex : NULL,
203 doColors ? colors : NULL, NULL,
208 static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv,
210 SkAutoCanvasRestore ar(canvas, true);
212 patch->draw(canvas, paint, nu, nv, false, false);
213 canvas->translate(SkIntToScalar(180), 0);
214 patch->draw(canvas, paint, nu, nv, true, false);
215 canvas->translate(SkIntToScalar(180), 0);
216 patch->draw(canvas, paint, nu, nv, false, true);
217 canvas->translate(SkIntToScalar(180), 0);
218 patch->draw(canvas, paint, nu, nv, true, true);
221 const SkScalar DX = 20;
222 const SkScalar DY = 0;
224 class PatchView : public SampleView {
228 SkIPoint fSize0, fSize1;
232 PatchView() : fAngle(0) {
233 fShader0 = make_shader0(&fSize0);
235 if (fSize0.fX == 0 || fSize0.fY == 0) {
238 fShader1 = make_shader1(fSize1);
240 const SkScalar S = SkIntToScalar(50);
241 const SkScalar T = SkIntToScalar(40);
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);
255 this->setBGColor(SK_ColorGRAY);
258 virtual ~PatchView() {
259 SkSafeUnref(fShader0);
260 SkSafeUnref(fShader1);
264 // overrides from SkEventSink
265 virtual bool onQuery(SkEvent* evt) {
266 if (SampleCode::TitleQ(*evt)) {
267 SampleCode::TitleR(evt, "Patch");
270 return this->INHERITED::onQuery(evt);
273 virtual void onDrawContent(SkCanvas* canvas) {
278 paint.setDither(true);
279 paint.setFilterQuality(kLow_SkFilterQuality);
281 canvas->translate(DX, DY);
285 paint.setShader(fShader0);
286 if (fSize0.fX == 0) {
289 if (fSize0.fY == 0) {
292 patch.setBounds(fSize0.fX, fSize0.fY);
294 patch.setPatch(fPts);
295 drawpatches(canvas, paint, nu, nv, &patch);
297 paint.setShader(NULL);
298 paint.setAntiAlias(true);
299 paint.setStrokeWidth(SkIntToScalar(5));
300 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
302 canvas->translate(0, SkIntToScalar(300));
304 paint.setAntiAlias(false);
305 paint.setShader(fShader1);
309 SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m);
310 paint.setShader(s)->unref();
315 SkShader* s = SkShader::CreateLocalMatrixShader(paint.getShader(), m);
316 paint.setShader(s)->unref();
318 patch.setBounds(fSize1.fX, fSize1.fY);
319 drawpatches(canvas, paint, nu, nv, &patch);
322 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
323 fAngle = timer.scaled(60, 360);
327 class PtClick : public Click {
330 PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
333 static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
334 return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
337 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
338 unsigned modi) SK_OVERRIDE {
341 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
342 if (hittest(fPts[i], x, y)) {
343 return new PtClick(this, (int)i);
346 return this->INHERITED::onFindClickHandler(x, y, modi);
349 virtual bool onClick(Click* click) {
350 fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX - DX, click->fCurr.fY - DY);
356 typedef SampleView INHERITED;
359 //////////////////////////////////////////////////////////////////////////////
361 static SkView* MyFactory() { return new PatchView; }
362 static SkViewRegister reg(MyFactory);