Index: gm/gradient_matrix.cpp |
diff --git a/gm/gradient_matrix.cpp b/gm/gradient_matrix.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..756e6e6ee0d98e7f667424e8489b6223d653ef18 |
--- /dev/null |
+++ b/gm/gradient_matrix.cpp |
@@ -0,0 +1,107 @@ |
+/* |
+ * Copyright 2013 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+#include "gm.h" |
+#include "SkGradientShader.h" |
+ |
+SkColor gColors[] = { |
+ SK_ColorRED, SK_ColorYELLOW |
+}; |
+ |
+// these arrays define the gradient stop points |
+// as x1, y1, x2, y2 per gradient to draw |
+SkScalar linearPts[] = { |
+ 0, 0, 1, 0, |
+ 0, 0, 0, 1, |
+ 1, 0, 0, 0, |
+ 0, 1, 0, 0, |
+ |
+ 0, 0, 1, 1, |
+ 1, 1, 0, 0, |
+ 1, 0, 0, 1, |
+ 0, 1, 1, 0 |
+}; |
+ |
+const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimension |
+const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimension |
+ |
+const int IMAGES_X = 4; // number of images per row |
+ |
+static SkShader* make_linear_gradient(SkPoint pts[2]) { |
+ return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gColors), |
+ SkShader::kClamp_TileMode, NULL); |
+} |
+ |
+void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(SkPoint[2]), |
+ SkScalar* ptsArray, int numImages) { |
vandebo (ex-Chrome)
2013/06/11 22:11:05
nit: indent
ducky
2013/06/11 23:18:17
Done.
|
+ for (int i = 0; i < numImages; i++) { |
+ // advance line if necessary |
+ if (i % IMAGES_X == 0 && i != 0) { |
+ canvas->restore(); |
+ canvas->translate(0, TESTGRID_Y); |
+ canvas->save(); |
+ } |
+ |
+ // unpack points, setup shader, draw |
+ SkPoint pts[2] = { |
vandebo (ex-Chrome)
2013/06/11 22:11:05
You shouldn't need this bit.
ducky
2013/06/11 23:18:17
Done.
|
+ { *(ptsArray+0), *(ptsArray+1) }, |
+ { *(ptsArray+2), *(ptsArray+3) } |
+ }; |
+ ptsArray += 4; |
+ |
+ SkMatrix shaderMat = SkMatrix(); |
vandebo (ex-Chrome)
2013/06/11 22:11:05
Pull this stuff above the loop.
ducky
2013/06/11 23:18:17
Done.
|
+ shaderMat.setAll(138, 0, 43, |
+ 0, 106, 61, |
+ 0, 0, 1); |
+ SkRect rectGrad = { 43, 61, 181, 167 }; // some nice prime numbers |
+ |
+ SkShader* shader = makeShader(pts); |
vandebo (ex-Chrome)
2013/06/11 22:11:05
use SkAutoTUnref
ducky
2013/06/11 23:18:17
Done.
|
+ shader->setLocalMatrix(shaderMat); |
+ |
+ SkPaint paint; |
+ paint.setShader(shader); |
+ canvas->drawRect(rectGrad, paint); |
+ |
+ shader->unref(); |
+ |
+ // advance to next position |
+ canvas->translate(TESTGRID_X, 0); |
+ } |
+} |
+ |
+namespace skiagm { |
+ |
+class GradientMatrixGM : public GM { |
+public: |
+ GradientMatrixGM() { |
+ this->setBGColor(0xFFDDDDDD); |
+ } |
+ |
+protected: |
+ SkString onShortName() { |
+ return SkString("gradient_matrix"); |
+ } |
+ |
+ virtual SkISize onISize() { |
+ return SkISize::Make(800, 800); |
+ } |
+ |
+ virtual void onDraw(SkCanvas* canvas) { |
+ canvas->save(); |
vandebo (ex-Chrome)
2013/06/11 22:11:05
Do the save restore around the for loop instead of
ducky
2013/06/11 23:18:17
Done.
|
+ |
+ draw_gradients(canvas, &make_linear_gradient, |
+ linearPts, sizeof(linearPts) / sizeof(linearPts[0]) / 4); |
vandebo (ex-Chrome)
2013/06/11 22:11:05
SK_ARRAY_COUNT()
ducky
2013/06/11 23:18:17
Done.
|
+ |
+ canvas->restore(); |
+ } |
+ |
+private: |
+ typedef GM INHERITED; |
+}; |
+ |
+static GM* MyFactory(void*) { return new GradientMatrixGM; } |
+static GMRegistry reg(MyFactory); |
+} |