GrTestUtils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2015 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. #ifndef GrTestUtils_DEFINED
  8. #define GrTestUtils_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #if GR_TEST_UTILS
  11. #include "include/core/SkPathEffect.h"
  12. #include "include/core/SkStrokeRec.h"
  13. #include "include/gpu/GrSamplerState.h"
  14. #include "include/private/SkMacros.h"
  15. #include "include/private/SkTemplates.h"
  16. #include "include/utils/SkRandom.h"
  17. #include "src/gpu/GrColor.h"
  18. #include "src/gpu/GrFPArgs.h"
  19. #include "src/shaders/SkShaderBase.h"
  20. class GrColorSpaceInfo;
  21. class GrColorSpaceXform;
  22. struct GrProcessorTestData;
  23. class GrStyle;
  24. class SkMatrix;
  25. class SkPath;
  26. class SkRRect;
  27. struct SkRect;
  28. namespace GrTest {
  29. /**
  30. * Helpers for use in Test functions.
  31. */
  32. const SkMatrix& TestMatrix(SkRandom*);
  33. const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
  34. const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
  35. const SkMatrix& TestMatrixInvertible(SkRandom*);
  36. const SkMatrix& TestMatrixPerspective(SkRandom*);
  37. void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]);
  38. const SkRect& TestRect(SkRandom*);
  39. const SkRect& TestSquare(SkRandom*);
  40. const SkRRect& TestRRectSimple(SkRandom*);
  41. const SkPath& TestPath(SkRandom*);
  42. const SkPath& TestPathConvex(SkRandom*);
  43. SkStrokeRec TestStrokeRec(SkRandom*);
  44. /** Creates styles with dash path effects and null path effects */
  45. void TestStyle(SkRandom*, GrStyle*);
  46. sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
  47. sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
  48. class TestAsFPArgs {
  49. public:
  50. TestAsFPArgs(GrProcessorTestData*);
  51. ~TestAsFPArgs();
  52. const GrFPArgs& args() const { return fArgs; }
  53. private:
  54. SkMatrix fViewMatrixStorage;
  55. std::unique_ptr<GrColorSpaceInfo> fColorSpaceInfoStorage;
  56. GrFPArgs fArgs;
  57. };
  58. // We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
  59. // is in the optional build target effects.
  60. class TestDashPathEffect : public SkPathEffect {
  61. public:
  62. static sk_sp<SkPathEffect> Make(const SkScalar* intervals, int count, SkScalar phase) {
  63. return sk_sp<SkPathEffect>(new TestDashPathEffect(intervals, count, phase));
  64. }
  65. Factory getFactory() const override { return nullptr; }
  66. const char* getTypeName() const override { return nullptr; }
  67. protected:
  68. bool onFilterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
  69. DashType onAsADash(DashInfo* info) const override;
  70. private:
  71. TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase);
  72. int fCount;
  73. SkAutoTArray<SkScalar> fIntervals;
  74. SkScalar fPhase;
  75. SkScalar fInitialDashLength;
  76. int fInitialDashIndex;
  77. SkScalar fIntervalLength;
  78. };
  79. } // namespace GrTest
  80. static inline GrColor GrRandomColor(SkRandom* random) {
  81. // There are only a few cases of random colors which interest us
  82. enum ColorMode {
  83. kAllOnes_ColorMode,
  84. kAllZeros_ColorMode,
  85. kAlphaOne_ColorMode,
  86. kRandom_ColorMode,
  87. kLast_ColorMode = kRandom_ColorMode
  88. };
  89. ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
  90. GrColor color SK_INIT_TO_AVOID_WARNING;
  91. switch (colorMode) {
  92. case kAllOnes_ColorMode:
  93. color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
  94. break;
  95. case kAllZeros_ColorMode:
  96. color = GrColorPackRGBA(0, 0, 0, 0);
  97. break;
  98. case kAlphaOne_ColorMode:
  99. color = GrColorPackRGBA(random->nextULessThan(256),
  100. random->nextULessThan(256),
  101. random->nextULessThan(256),
  102. 0xFF);
  103. break;
  104. case kRandom_ColorMode: {
  105. uint8_t alpha = random->nextULessThan(256);
  106. color = GrColorPackRGBA(random->nextRangeU(0, alpha),
  107. random->nextRangeU(0, alpha),
  108. random->nextRangeU(0, alpha),
  109. alpha);
  110. break;
  111. }
  112. }
  113. return color;
  114. }
  115. static inline uint8_t GrRandomCoverage(SkRandom* random) {
  116. enum CoverageMode {
  117. kZero_CoverageMode,
  118. kAllOnes_CoverageMode,
  119. kRandom_CoverageMode,
  120. kLast_CoverageMode = kRandom_CoverageMode
  121. };
  122. CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
  123. uint8_t coverage SK_INIT_TO_AVOID_WARNING;
  124. switch (colorMode) {
  125. case kZero_CoverageMode:
  126. coverage = 0;
  127. break;
  128. case kAllOnes_CoverageMode:
  129. coverage = 0xff;
  130. break;
  131. case kRandom_CoverageMode:
  132. coverage = random->nextULessThan(256);
  133. break;
  134. }
  135. return coverage;
  136. }
  137. #endif
  138. #endif