modecolorfilters.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2012 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/SkBitmap.h"
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorFilter.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkShader.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTileMode.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkGradientShader.h"
  24. #include "tools/ToolUtils.h"
  25. #define WIDTH 512
  26. #define HEIGHT 1024
  27. namespace skiagm {
  28. // Using gradients because GPU doesn't currently have an implementation of SkColorShader (duh!)
  29. static sk_sp<SkShader> make_color_shader(SkColor color) {
  30. constexpr SkPoint kPts[] = {{0, 0}, {1, 1}};
  31. SkColor colors[] = {color, color};
  32. return SkGradientShader::MakeLinear(kPts, colors, nullptr, 2, SkTileMode::kClamp);
  33. }
  34. static sk_sp<SkShader> make_solid_shader() {
  35. return make_color_shader(SkColorSetARGB(0xFF, 0x42, 0x82, 0x21));
  36. }
  37. static sk_sp<SkShader> make_transparent_shader() {
  38. return make_color_shader(SkColorSetARGB(0x80, 0x10, 0x70, 0x20));
  39. }
  40. static sk_sp<SkShader> make_trans_black_shader() {
  41. return make_color_shader(0x0);
  42. }
  43. // draws a background behind each test rect to see transparency
  44. static sk_sp<SkShader> make_bg_shader(int checkSize) {
  45. SkBitmap bmp;
  46. bmp.allocN32Pixels(2 * checkSize, 2 * checkSize);
  47. SkCanvas canvas(bmp);
  48. canvas.clear(ToolUtils::color_to_565(0xFF800000));
  49. SkPaint paint;
  50. paint.setColor(ToolUtils::color_to_565(0xFF000080));
  51. SkRect rect0 = SkRect::MakeXYWH(0, 0,
  52. SkIntToScalar(checkSize), SkIntToScalar(checkSize));
  53. SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(checkSize), SkIntToScalar(checkSize),
  54. SkIntToScalar(checkSize), SkIntToScalar(checkSize));
  55. canvas.drawRect(rect1, paint);
  56. canvas.drawRect(rect0, paint);
  57. return bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
  58. }
  59. class ModeColorFilterGM : public GM {
  60. public:
  61. ModeColorFilterGM() {
  62. this->setBGColor(0xFF303030);
  63. }
  64. protected:
  65. SkString onShortName() override {
  66. return SkString("modecolorfilters");
  67. }
  68. SkISize onISize() override {
  69. return SkISize::Make(WIDTH, HEIGHT);
  70. }
  71. void onDraw(SkCanvas* canvas) override {
  72. // size of rect for each test case
  73. constexpr int kRectWidth = 20;
  74. constexpr int kRectHeight = 20;
  75. constexpr int kCheckSize = 10;
  76. if (!fBmpShader) {
  77. fBmpShader = make_bg_shader(kCheckSize);
  78. }
  79. SkPaint bgPaint;
  80. bgPaint.setShader(fBmpShader);
  81. bgPaint.setBlendMode(SkBlendMode::kSrc);
  82. sk_sp<SkShader> shaders[] = {
  83. nullptr, // use a paint color instead of a shader
  84. make_solid_shader(),
  85. make_transparent_shader(),
  86. make_trans_black_shader(),
  87. };
  88. // used without shader
  89. SkColor colors[] = {
  90. SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF),
  91. SkColorSetARGB(0xFF, 0x00, 0x00, 0x00),
  92. SkColorSetARGB(0x00, 0x00, 0x00, 0x00),
  93. SkColorSetARGB(0xFF, 0x10, 0x20, 0x42),
  94. SkColorSetARGB(0xA0, 0x20, 0x30, 0x90),
  95. };
  96. // used with shaders
  97. SkColor alphas[] = {0xFFFFFFFF, 0x80808080};
  98. const SkBlendMode modes[] = { // currently just doing the Modes expressible as Coeffs
  99. SkBlendMode::kClear,
  100. SkBlendMode::kSrc,
  101. SkBlendMode::kDst,
  102. SkBlendMode::kSrcOver,
  103. SkBlendMode::kDstOver,
  104. SkBlendMode::kSrcIn,
  105. SkBlendMode::kDstIn,
  106. SkBlendMode::kSrcOut,
  107. SkBlendMode::kDstOut,
  108. SkBlendMode::kSrcATop,
  109. SkBlendMode::kDstATop,
  110. SkBlendMode::kXor,
  111. SkBlendMode::kPlus,
  112. SkBlendMode::kModulate,
  113. };
  114. SkPaint paint;
  115. int idx = 0;
  116. const int kRectsPerRow = SkMax32(this->getISize().fWidth / kRectWidth, 1);
  117. for (size_t cfm = 0; cfm < SK_ARRAY_COUNT(modes); ++cfm) {
  118. for (size_t cfc = 0; cfc < SK_ARRAY_COUNT(colors); ++cfc) {
  119. paint.setColorFilter(SkColorFilters::Blend(colors[cfc], modes[cfm]));
  120. for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); ++s) {
  121. paint.setShader(shaders[s]);
  122. bool hasShader = nullptr == paint.getShader();
  123. int paintColorCnt = hasShader ? SK_ARRAY_COUNT(alphas) : SK_ARRAY_COUNT(colors);
  124. SkColor* paintColors = hasShader ? alphas : colors;
  125. for (int pc = 0; pc < paintColorCnt; ++pc) {
  126. paint.setColor(paintColors[pc]);
  127. SkScalar x = SkIntToScalar(idx % kRectsPerRow);
  128. SkScalar y = SkIntToScalar(idx / kRectsPerRow);
  129. SkRect rect = SkRect::MakeXYWH(x * kRectWidth, y * kRectHeight,
  130. SkIntToScalar(kRectWidth),
  131. SkIntToScalar(kRectHeight));
  132. canvas->saveLayer(&rect, nullptr);
  133. canvas->drawRect(rect, bgPaint);
  134. canvas->drawRect(rect, paint);
  135. canvas->restore();
  136. ++idx;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. private:
  143. sk_sp<SkShader> fBmpShader;
  144. typedef GM INHERITED;
  145. };
  146. //////////////////////////////////////////////////////////////////////////////
  147. DEF_GM( return new ModeColorFilterGM; )
  148. }