xfermodes2.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/SkBitmap.h"
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorPriv.h"
  13. #include "include/core/SkFont.h"
  14. #include "include/core/SkMatrix.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkShader.h"
  20. #include "include/core/SkSize.h"
  21. #include "include/core/SkString.h"
  22. #include "include/core/SkTileMode.h"
  23. #include "include/core/SkTypeface.h"
  24. #include "include/utils/SkTextUtils.h"
  25. #include "tools/ToolUtils.h"
  26. #include <stdint.h>
  27. #include <string.h>
  28. namespace skiagm {
  29. class Xfermodes2GM : public GM {
  30. public:
  31. Xfermodes2GM() {}
  32. protected:
  33. SkString onShortName() override {
  34. return SkString("xfermodes2");
  35. }
  36. SkISize onISize() override {
  37. return SkISize::Make(455, 475);
  38. }
  39. void onDraw(SkCanvas* canvas) override {
  40. canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
  41. const SkScalar w = SkIntToScalar(kSize);
  42. const SkScalar h = SkIntToScalar(kSize);
  43. SkFont font(ToolUtils::create_portable_typeface());
  44. const int W = 6;
  45. SkScalar x = 0, y = 0;
  46. for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; m++) {
  47. SkBlendMode mode = static_cast<SkBlendMode>(m);
  48. canvas->save();
  49. canvas->translate(x, y);
  50. SkPaint p;
  51. p.setAntiAlias(false);
  52. p.setStyle(SkPaint::kFill_Style);
  53. p.setShader(fBG);
  54. SkRect r = SkRect::MakeWH(w, h);
  55. canvas->drawRect(r, p);
  56. canvas->saveLayer(&r, nullptr);
  57. p.setShader(fDst);
  58. canvas->drawRect(r, p);
  59. p.setShader(fSrc);
  60. p.setBlendMode(mode);
  61. canvas->drawRect(r, p);
  62. canvas->restore();
  63. r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
  64. p.setStyle(SkPaint::kStroke_Style);
  65. p.setShader(nullptr);
  66. p.setBlendMode(SkBlendMode::kSrcOver);
  67. canvas->drawRect(r, p);
  68. canvas->restore();
  69. #if 1
  70. SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode), x + w/2, y - font.getSize()/2, font, SkPaint(),
  71. SkTextUtils::kCenter_Align);
  72. #endif
  73. x += w + SkIntToScalar(10);
  74. if ((m % W) == W - 1) {
  75. x = 0;
  76. y += h + SkIntToScalar(30);
  77. }
  78. }
  79. }
  80. private:
  81. void onOnceBeforeDraw() override {
  82. const uint32_t kCheckData[] = {
  83. SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
  84. SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
  85. SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
  86. SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
  87. };
  88. SkBitmap bg;
  89. bg.allocN32Pixels(2, 2, true);
  90. memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
  91. SkMatrix lm;
  92. lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
  93. fBG = bg.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &lm);
  94. SkBitmap srcBmp;
  95. srcBmp.allocN32Pixels(kSize, kSize);
  96. SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
  97. for (int y = 0; y < kSize; ++y) {
  98. int c = y * (1 << kShift);
  99. SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2);
  100. for (int x = 0; x < kSize; ++x) {
  101. pixels[kSize * y + x] = rowColor;
  102. }
  103. }
  104. fSrc = srcBmp.makeShader();
  105. SkBitmap dstBmp;
  106. dstBmp.allocN32Pixels(kSize, kSize);
  107. pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
  108. for (int x = 0; x < kSize; ++x) {
  109. int c = x * (1 << kShift);
  110. SkPMColor colColor = SkPackARGB32(c, 0, c, c/2);
  111. for (int y = 0; y < kSize; ++y) {
  112. pixels[kSize * y + x] = colColor;
  113. }
  114. }
  115. fDst = dstBmp.makeShader();
  116. }
  117. enum {
  118. kShift = 2,
  119. kSize = 256 >> kShift,
  120. };
  121. sk_sp<SkShader> fBG;
  122. sk_sp<SkShader> fSrc;
  123. sk_sp<SkShader> fDst;
  124. typedef GM INHERITED;
  125. };
  126. //////////////////////////////////////////////////////////////////////////////
  127. DEF_GM( return new Xfermodes2GM; )
  128. }