2013-06-12 21:33:02 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkCanvas.h"
|
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkSize.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-06-12 21:33:02 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor gColors[] = {
|
2013-06-12 21:33:02 +00:00
|
|
|
SK_ColorRED, SK_ColorYELLOW
|
|
|
|
};
|
|
|
|
|
2013-06-12 23:36:50 +00:00
|
|
|
// These annoying defines are necessary, because the only other alternative
|
2013-11-25 19:44:07 +00:00
|
|
|
// is to use SkIntToScalar(...) everywhere.
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar sZero = 0;
|
|
|
|
constexpr SkScalar sHalf = SK_ScalarHalf;
|
|
|
|
constexpr SkScalar sOne = SK_Scalar1;
|
2013-06-12 23:36:50 +00:00
|
|
|
|
|
|
|
// These arrays define the gradient stop points
|
|
|
|
// as x1, y1, x2, y2 per gradient to draw.
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkPoint linearPts[][2] = {
|
2013-06-12 23:36:50 +00:00
|
|
|
{{sZero, sZero}, {sOne, sZero}},
|
|
|
|
{{sZero, sZero}, {sZero, sOne}},
|
|
|
|
{{sOne, sZero}, {sZero, sZero}},
|
|
|
|
{{sZero, sOne}, {sZero, sZero}},
|
2013-06-12 21:33:02 +00:00
|
|
|
|
2013-06-12 23:36:50 +00:00
|
|
|
{{sZero, sZero}, {sOne, sOne}},
|
|
|
|
{{sOne, sOne}, {sZero, sZero}},
|
|
|
|
{{sOne, sZero}, {sZero, sOne}},
|
|
|
|
{{sZero, sOne}, {sOne, sZero}}
|
2013-06-12 21:33:02 +00:00
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkPoint radialPts[][2] = {
|
2013-06-12 23:36:50 +00:00
|
|
|
{{sZero, sHalf}, {sOne, sHalf}},
|
|
|
|
{{sHalf, sZero}, {sHalf, sOne}},
|
|
|
|
{{sOne, sHalf}, {sZero, sHalf}},
|
|
|
|
{{sHalf, sOne}, {sHalf, sZero}},
|
2013-06-12 21:47:39 +00:00
|
|
|
|
2013-06-12 23:36:50 +00:00
|
|
|
{{sZero, sZero}, {sOne, sOne}},
|
|
|
|
{{sOne, sOne}, {sZero, sZero}},
|
|
|
|
{{sOne, sZero}, {sZero, sOne}},
|
|
|
|
{{sZero, sOne}, {sOne, sZero}}
|
2013-06-12 21:47:39 +00:00
|
|
|
};
|
|
|
|
|
2013-06-12 23:36:50 +00:00
|
|
|
// These define the pixels allocated to each gradient image.
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar TESTGRID_X = SkIntToScalar(200);
|
|
|
|
constexpr SkScalar TESTGRID_Y = SkIntToScalar(200);
|
2013-06-12 21:33:02 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int IMAGES_X = 4; // number of images per row
|
2013-06-12 21:33:02 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
|
|
|
|
return SkGradientShader::MakeLinear(pts, gColors, nullptr, SK_ARRAY_COUNT(gColors),
|
|
|
|
SkShader::kClamp_TileMode, 0, &localMatrix);
|
2013-06-12 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
|
2013-06-12 21:47:39 +00:00
|
|
|
SkPoint center;
|
|
|
|
center.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
|
|
|
float radius = (center - pts[0]).length();
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeRadial(center, radius, gColors, nullptr, SK_ARRAY_COUNT(gColors),
|
|
|
|
SkShader::kClamp_TileMode, 0, &localMatrix);
|
2013-06-12 21:47:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:55:39 +00:00
|
|
|
static void draw_gradients(SkCanvas* canvas,
|
2016-03-13 21:13:58 +00:00
|
|
|
sk_sp<SkShader> (*makeShader)(const SkPoint[2], const SkMatrix&),
|
2013-06-12 21:47:39 +00:00
|
|
|
const SkPoint ptsArray[][2], int numImages) {
|
2013-06-12 21:33:02 +00:00
|
|
|
// Use some nice prime numbers for the rectangle and matrix with
|
|
|
|
// different scaling along the x and y axes (which is the bug this
|
|
|
|
// test addresses, where incorrect order of operations mixed up the axes)
|
2013-06-12 23:36:50 +00:00
|
|
|
SkRect rectGrad = {
|
|
|
|
SkIntToScalar(43), SkIntToScalar(61),
|
|
|
|
SkIntToScalar(181), SkIntToScalar(167) };
|
2013-06-12 21:33:02 +00:00
|
|
|
SkMatrix shaderMat;
|
|
|
|
shaderMat.setScale(rectGrad.width(), rectGrad.height());
|
|
|
|
shaderMat.postTranslate(rectGrad.left(), rectGrad.top());
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
for (int i = 0; i < numImages; i++) {
|
|
|
|
// Advance line downwards if necessary.
|
|
|
|
if (i % IMAGES_X == 0 && i != 0) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, TESTGRID_Y);
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(makeShader(*ptsArray, shaderMat));
|
2013-06-12 21:33:02 +00:00
|
|
|
canvas->drawRect(rectGrad, paint);
|
|
|
|
|
|
|
|
// Advance to next position.
|
|
|
|
canvas->translate(TESTGRID_X, 0);
|
|
|
|
ptsArray++;
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800,
|
|
|
|
sk_tool_utils::color_to_565(0xFFDDDDDD)) {
|
2013-06-12 21:33:02 +00:00
|
|
|
draw_gradients(canvas, &make_linear_gradient,
|
|
|
|
linearPts, SK_ARRAY_COUNT(linearPts));
|
2013-06-12 21:47:39 +00:00
|
|
|
|
|
|
|
canvas->translate(0, TESTGRID_Y);
|
|
|
|
|
|
|
|
draw_gradients(canvas, &make_radial_gradient,
|
|
|
|
radialPts, SK_ARRAY_COUNT(radialPts));
|
2013-06-12 21:33:02 +00:00
|
|
|
}
|