| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkShader.h" |
| 18 |
#include "include/core/SkTileMode.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/effects/SkGradient.h" |
| 21 |
#include "include/private/base/SkFloatingPoint.h" |
| 22 |
|
| 23 |
constexpr SkColor4f gColors[] = { |
| 24 |
SkColors::kRed, SkColors::kYellow |
| 25 |
}; |
| 26 |
|
| 27 |
static SkGradient make_grad(SkTileMode tm) { |
| 28 |
return {{gColors, {}, tm}, {}}; |
| 29 |
} |
| 30 |
|
| 31 |
// These annoying defines are necessary, because the only other alternative |
| 32 |
// is to use SkIntToScalar(...) everywhere. |
| 33 |
constexpr SkScalar sZero = 0; |
| 34 |
constexpr SkScalar sHalf = SK_ScalarHalf; |
| 35 |
constexpr SkScalar sOne = SK_Scalar1; |
| 36 |
|
| 37 |
// These arrays define the gradient stop points |
| 38 |
// as x1, y1, x2, y2 per gradient to draw. |
| 39 |
constexpr SkPoint linearPts[][2] = { |
| 40 |
{{sZero, sZero}, {sOne, sZero}}, |
| 41 |
{{sZero, sZero}, {sZero, sOne}}, |
| 42 |
{{sOne, sZero}, {sZero, sZero}}, |
| 43 |
{{sZero, sOne}, {sZero, sZero}}, |
| 44 |
|
| 45 |
{{sZero, sZero}, {sOne, sOne}}, |
| 46 |
{{sOne, sOne}, {sZero, sZero}}, |
| 47 |
{{sOne, sZero}, {sZero, sOne}}, |
| 48 |
{{sZero, sOne}, {sOne, sZero}} |
| 49 |
}; |
| 50 |
|
| 51 |
constexpr SkPoint radialPts[][2] = { |
| 52 |
{{sZero, sHalf}, {sOne, sHalf}}, |
| 53 |
{{sHalf, sZero}, {sHalf, sOne}}, |
| 54 |
{{sOne, sHalf}, {sZero, sHalf}}, |
| 55 |
{{sHalf, sOne}, {sHalf, sZero}}, |
| 56 |
|
| 57 |
{{sZero, sZero}, {sOne, sOne}}, |
| 58 |
{{sOne, sOne}, {sZero, sZero}}, |
| 59 |
{{sOne, sZero}, {sZero, sOne}}, |
| 60 |
{{sZero, sOne}, {sOne, sZero}} |
| 61 |
}; |
| 62 |
|
| 63 |
// These define the pixels allocated to each gradient image. |
| 64 |
constexpr SkScalar TESTGRID_X = SkIntToScalar(200); |
| 65 |
constexpr SkScalar TESTGRID_Y = SkIntToScalar(200); |
| 66 |
|
| 67 |
constexpr int IMAGES_X = 4; // number of images per row |
| 68 |
|
| 69 |
static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) { |
| 70 |
return SkShaders::LinearGradient(pts, make_grad(SkTileMode::kClamp), &localMatrix); |
| 71 |
} |
| 72 |
|
| 73 |
static sk_sp<SkShader> make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) { |
| 74 |
SkPoint center; |
| 75 |
center.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 76 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 77 |
float radius = (center - pts[0]).length(); |
| 78 |
return SkShaders::RadialGradient(center, radius, make_grad(SkTileMode::kClamp), &localMatrix); |
| 79 |
} |
| 80 |
|
| 81 |
static void draw_gradients(SkCanvas* canvas, |
| 82 |
sk_sp<SkShader> (*makeShader)(const SkPoint[2], const SkMatrix&), |
| 83 |
const SkPoint ptsArray[][2], int numImages) { |
| 84 |
// Use some nice prime numbers for the rectangle and matrix with |
| 85 |
// different scaling along the x and y axes (which is the bug this |
| 86 |
// test addresses, where incorrect order of operations mixed up the axes) |
| 87 |
SkRect rectGrad = { |
| 88 |
SkIntToScalar(43), SkIntToScalar(61), |
| 89 |
SkIntToScalar(181), SkIntToScalar(167) }; |
| 90 |
SkMatrix shaderMat; |
| 91 |
shaderMat.setScale(rectGrad.width(), rectGrad.height()); |
| 92 |
shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); |
| 93 |
|
| 94 |
canvas->save(); |
| 95 |
for (int i = 0; i < numImages; i++) { |
| 96 |
// Advance line downwards if necessary. |
| 97 |
if (i % IMAGES_X == 0 && i != 0) { |
| 98 |
canvas->restore(); |
| 99 |
canvas->translate(0, TESTGRID_Y); |
| 100 |
canvas->save(); |
| 101 |
} |
| 102 |
|
| 103 |
SkPaint paint; |
| 104 |
paint.setShader(makeShader(*ptsArray, shaderMat)); |
| 105 |
canvas->drawRect(rectGrad, paint); |
| 106 |
|
| 107 |
// Advance to next position. |
| 108 |
canvas->translate(TESTGRID_X, 0); |
| 109 |
ptsArray++; |
| 110 |
} |
| 111 |
canvas->restore(); |
| 112 |
} |
| 113 |
|
| 114 |
DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800, 0xFFDDDDDD) { |
| 115 |
draw_gradients(canvas, &make_linear_gradient, |
| 116 |
linearPts, std::size(linearPts)); |
| 117 |
|
| 118 |
canvas->translate(0, TESTGRID_Y); |
| 119 |
|
| 120 |
draw_gradients(canvas, &make_radial_gradient, |
| 121 |
radialPts, std::size(radialPts)); |
| 122 |
} |