data.fColors, data.fPos, data.fCount, tm, mapper);
}
+static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data,
+ SkShader::TileMode tm, SkUnitMapper* mapper) {
+ SkPoint center0, center1;
+ SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10);
+ SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3);
+ center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
+ center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
+ return SkGradientShader::CreateTwoPointConical(center1, radius1,
+ center0, radius0,
+ data.fColors, data.fPos,
+ data.fCount, tm, mapper);
+}
+
typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
SkShader::TileMode tm, SkUnitMapper* mapper);
static const GradMaker gGradMakers[] = {
- MakeLinear, MakeRadial, MakeSweep, Make2Radial
+ MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
};
///////////////////////////////////////////////////////////////////////////////
return SkString("gradients");
}
- virtual SkISize onISize() { return make_isize(640, 510); }
+ virtual SkISize onISize() { return make_isize(640, 615); }
virtual void onDraw(SkCanvas* canvas) {
typedef GM INHERITED;
};
+// Based on the original gradient slide, but with perspective applied to the
+// gradient shaders' local matrices
+class GradientsLocalPerspectiveGM : public GM {
+public:
+ GradientsLocalPerspectiveGM() {
+ this->setBGColor(0xFFDDDDDD);
+ }
+
+protected:
+ SkString onShortName() {
+ return SkString("gradients_local_perspective");
+ }
+
+ virtual SkISize onISize() { return make_isize(640, 615); }
+
+ virtual void onDraw(SkCanvas* canvas) {
+
+ SkPoint pts[2] = {
+ { 0, 0 },
+ { SkIntToScalar(100), SkIntToScalar(100) }
+ };
+ SkShader::TileMode tm = SkShader::kClamp_TileMode;
+ SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
+ SkPaint paint;
+ paint.setAntiAlias(true);
+
+ canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
+ canvas->save();
+ for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
+ SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
+
+ // apply an increasing y perspective as we move to the right
+ SkMatrix perspective;
+ perspective.setIdentity();
+ perspective.setPerspY(SkScalarDiv(SkIntToScalar(i+1),
+ SkIntToScalar(500)));
+ perspective.setSkewX(SkScalarDiv(SkIntToScalar(i+1),
+ SkIntToScalar(10)));
+ shader->setLocalMatrix(perspective);
+
+ paint.setShader(shader);
+ canvas->drawRect(r, paint);
+ shader->unref();
+ canvas->translate(0, SkIntToScalar(120));
+ }
+ canvas->restore();
+ canvas->translate(SkIntToScalar(120), 0);
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+};
+
+// Based on the original gradient slide, but with perspective applied to
+// the view matrix
+class GradientsViewPerspectiveGM : public GradientsGM {
+protected:
+ SkString onShortName() {
+ return SkString("gradients_view_perspective");
+ }
+
+ virtual SkISize onISize() { return make_isize(640, 400); }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ SkMatrix perspective;
+ perspective.setIdentity();
+ perspective.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
+ perspective.setSkewX(SkScalarDiv(SkIntToScalar(8), SkIntToScalar(25)));
+ canvas->setMatrix(perspective);
+ INHERITED::onDraw(canvas);
+ }
+
+private:
+ typedef GradientsGM INHERITED;
+};
+
/*
Inspired by this <canvas> javascript, where we need to detect that we are not
solving a quadratic equation, but must instead solve a linear (since our X^2
static GM* MyFactory4(void*) { return new RadialGradientGM; }
static GMRegistry reg4(MyFactory4);
+
+static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; }
+static GMRegistry reg5(MyFactory5);
+
+static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; }
+static GMRegistry reg6(MyFactory6);
}
+