gradient_matrix.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkShader.h"
  17. #include "include/core/SkTileMode.h"
  18. #include "include/core/SkTypes.h"
  19. #include "include/effects/SkGradientShader.h"
  20. constexpr SkColor gColors[] = {
  21. SK_ColorRED, SK_ColorYELLOW
  22. };
  23. // These annoying defines are necessary, because the only other alternative
  24. // is to use SkIntToScalar(...) everywhere.
  25. constexpr SkScalar sZero = 0;
  26. constexpr SkScalar sHalf = SK_ScalarHalf;
  27. constexpr SkScalar sOne = SK_Scalar1;
  28. // These arrays define the gradient stop points
  29. // as x1, y1, x2, y2 per gradient to draw.
  30. constexpr SkPoint linearPts[][2] = {
  31. {{sZero, sZero}, {sOne, sZero}},
  32. {{sZero, sZero}, {sZero, sOne}},
  33. {{sOne, sZero}, {sZero, sZero}},
  34. {{sZero, sOne}, {sZero, sZero}},
  35. {{sZero, sZero}, {sOne, sOne}},
  36. {{sOne, sOne}, {sZero, sZero}},
  37. {{sOne, sZero}, {sZero, sOne}},
  38. {{sZero, sOne}, {sOne, sZero}}
  39. };
  40. constexpr SkPoint radialPts[][2] = {
  41. {{sZero, sHalf}, {sOne, sHalf}},
  42. {{sHalf, sZero}, {sHalf, sOne}},
  43. {{sOne, sHalf}, {sZero, sHalf}},
  44. {{sHalf, sOne}, {sHalf, sZero}},
  45. {{sZero, sZero}, {sOne, sOne}},
  46. {{sOne, sOne}, {sZero, sZero}},
  47. {{sOne, sZero}, {sZero, sOne}},
  48. {{sZero, sOne}, {sOne, sZero}}
  49. };
  50. // These define the pixels allocated to each gradient image.
  51. constexpr SkScalar TESTGRID_X = SkIntToScalar(200);
  52. constexpr SkScalar TESTGRID_Y = SkIntToScalar(200);
  53. constexpr int IMAGES_X = 4; // number of images per row
  54. static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
  55. return SkGradientShader::MakeLinear(pts, gColors, nullptr, SK_ARRAY_COUNT(gColors),
  56. SkTileMode::kClamp, 0, &localMatrix);
  57. }
  58. static sk_sp<SkShader> make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
  59. SkPoint center;
  60. center.set(SkScalarAve(pts[0].fX, pts[1].fX),
  61. SkScalarAve(pts[0].fY, pts[1].fY));
  62. float radius = (center - pts[0]).length();
  63. return SkGradientShader::MakeRadial(center, radius, gColors, nullptr, SK_ARRAY_COUNT(gColors),
  64. SkTileMode::kClamp, 0, &localMatrix);
  65. }
  66. static void draw_gradients(SkCanvas* canvas,
  67. sk_sp<SkShader> (*makeShader)(const SkPoint[2], const SkMatrix&),
  68. const SkPoint ptsArray[][2], int numImages) {
  69. // Use some nice prime numbers for the rectangle and matrix with
  70. // different scaling along the x and y axes (which is the bug this
  71. // test addresses, where incorrect order of operations mixed up the axes)
  72. SkRect rectGrad = {
  73. SkIntToScalar(43), SkIntToScalar(61),
  74. SkIntToScalar(181), SkIntToScalar(167) };
  75. SkMatrix shaderMat;
  76. shaderMat.setScale(rectGrad.width(), rectGrad.height());
  77. shaderMat.postTranslate(rectGrad.left(), rectGrad.top());
  78. canvas->save();
  79. for (int i = 0; i < numImages; i++) {
  80. // Advance line downwards if necessary.
  81. if (i % IMAGES_X == 0 && i != 0) {
  82. canvas->restore();
  83. canvas->translate(0, TESTGRID_Y);
  84. canvas->save();
  85. }
  86. SkPaint paint;
  87. paint.setShader(makeShader(*ptsArray, shaderMat));
  88. canvas->drawRect(rectGrad, paint);
  89. // Advance to next position.
  90. canvas->translate(TESTGRID_X, 0);
  91. ptsArray++;
  92. }
  93. canvas->restore();
  94. }
  95. DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800, 0xFFDDDDDD) {
  96. draw_gradients(canvas, &make_linear_gradient,
  97. linearPts, SK_ARRAY_COUNT(linearPts));
  98. canvas->translate(0, TESTGRID_Y);
  99. draw_gradients(canvas, &make_radial_gradient,
  100. radialPts, SK_ARRAY_COUNT(radialPts));
  101. }