hardstop_gradients.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2016 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. * This GM presents a variety of different gradients with different
  9. * tile modes. Each entry in the table is a rectangle with a linear
  10. * gradient that spans from its left edge to its right edge. The rows
  11. * in the table represent different color/position configurations,
  12. * while the columns in the table represent different tile modes. In
  13. * order to highlight the differences between tile modes, the gradient
  14. * starts and ends at 30 pixel inset from either side of the rectangle.
  15. *
  16. * | Clamp Repeat Mirror
  17. * _____________________________|___________________________________________
  18. * 2-color | rect00 rect01 rect02
  19. * 3-color even | rect10 rect11 rect12
  20. * 3-color texture | rect20 rect21 rect22
  21. * 5-color hard stop | rect30 rect31 rect32
  22. * 4-color hard stop centered | rect40 rect41 rect42
  23. * 3-color hard stop 001 | rect50 rect51 rect52
  24. * 3-color hard stop 011 | rect60 rect61 rect62
  25. * 4-color hard stop off-center | rect70 rect71 rect72
  26. *
  27. * The first three rows are cases covered by pre-hard-stop code; simple
  28. * 2-color gradients, 3-color gradients with the middle color centered,
  29. * and general gradients that are rendered from a texture atlas.
  30. *
  31. * The next four rows all deal with hard stop gradients. The fourth row
  32. * is a generic hard stop gradient, while the three subsequent rows deal
  33. * with special cases of hard stop gradients; centered hard stop gradients
  34. * (with t-values 0, 0.5, 0.5, 1), and two edge cases (with t-values
  35. * 0, 0, 1 and 0, 1, 1). The final row has a single off-center hard stop.
  36. */
  37. #include "gm/gm.h"
  38. #include "include/core/SkCanvas.h"
  39. #include "include/core/SkColor.h"
  40. #include "include/core/SkPaint.h"
  41. #include "include/core/SkPoint.h"
  42. #include "include/core/SkRect.h"
  43. #include "include/core/SkRefCnt.h"
  44. #include "include/core/SkScalar.h"
  45. #include "include/core/SkShader.h"
  46. #include "include/core/SkSize.h"
  47. #include "include/core/SkString.h"
  48. #include "include/core/SkTileMode.h"
  49. #include "include/effects/SkGradientShader.h"
  50. const int WIDTH = 500;
  51. const int HEIGHT = 500;
  52. const int NUM_ROWS = 8;
  53. const int NUM_COLS = 3;
  54. const int CELL_WIDTH = WIDTH / NUM_COLS;
  55. const int CELL_HEIGHT = HEIGHT / NUM_ROWS;
  56. const int PAD_WIDTH = 3;
  57. const int PAD_HEIGHT = 3;
  58. const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH);
  59. const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT);
  60. static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
  61. SkPaint paint;
  62. paint.setShader(shader);
  63. SkRect rect = SkRect::MakeXYWH(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH),
  64. SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT),
  65. SkIntToScalar(RECT_WIDTH),
  66. SkIntToScalar(RECT_HEIGHT));
  67. canvas->drawRect(rect, paint);
  68. }
  69. static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2]) {
  70. const int X_OFFSET = 30;
  71. auto x0 = SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH + X_OFFSET);
  72. auto x1 = SkIntToScalar((cellCol+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET);
  73. auto y = SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2);
  74. points[0] = SkPoint::Make(x0, y);
  75. points[1] = SkPoint::Make(x1, y);
  76. }
  77. class HardstopGradientShaderGM : public skiagm::GM {
  78. public:
  79. HardstopGradientShaderGM() {
  80. }
  81. protected:
  82. SkString onShortName() override {
  83. return SkString("hardstop_gradients");
  84. }
  85. SkISize onISize() override {
  86. return SkISize::Make(512, 512);
  87. }
  88. void onDraw(SkCanvas* canvas) override {
  89. SkPoint points[2];
  90. SkColor colors[] = {
  91. SK_ColorRED,
  92. SK_ColorGREEN,
  93. SK_ColorBLUE,
  94. SK_ColorYELLOW,
  95. SK_ColorMAGENTA,
  96. };
  97. SkScalar row3[] = {0.00f, 0.25f, 1.00f};
  98. SkScalar row4[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f};
  99. SkScalar row5[] = {0.00f, 0.50f, 0.50f, 1.00f};
  100. SkScalar row6[] = {0.00f, 0.00f, 1.00f};
  101. SkScalar row7[] = {0.00f, 1.00f, 1.00f};
  102. SkScalar row8[] = {0.00f, 0.30f, 0.30f, 1.00f};
  103. SkScalar* positions[NUM_ROWS] = {
  104. nullptr,
  105. nullptr,
  106. row3,
  107. row4,
  108. row5,
  109. row6,
  110. row7,
  111. row8,
  112. };
  113. int numGradientColors[NUM_ROWS] = {
  114. 2,
  115. 3,
  116. 3,
  117. 5,
  118. 4,
  119. 3,
  120. 3,
  121. 4,
  122. };
  123. SkTileMode tilemodes[NUM_COLS] = {
  124. SkTileMode::kClamp,
  125. SkTileMode::kRepeat,
  126. SkTileMode::kMirror,
  127. };
  128. for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
  129. for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
  130. create_gradient_points(cellRow, cellCol, points);
  131. auto shader = SkGradientShader::MakeLinear(
  132. points,
  133. colors,
  134. positions[cellRow],
  135. numGradientColors[cellRow],
  136. tilemodes[cellCol],
  137. 0,
  138. nullptr);
  139. shade_rect(canvas, shader, cellRow, cellCol);
  140. }
  141. }
  142. }
  143. private:
  144. typedef skiagm::GM INHERITED;
  145. };
  146. DEF_GM(return new HardstopGradientShaderGM;)