drawatlascolor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkRSXform.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkSurface.h"
  22. #include "include/core/SkTypeface.h"
  23. #include "include/core/SkTypes.h"
  24. #include "tools/ToolUtils.h"
  25. // Create a square atlas of:
  26. // opaque white | opaque red
  27. // ------------------------------------
  28. // opaque green | transparent black
  29. //
  30. static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
  31. const int kBlockSize = atlasSize/2;
  32. SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
  33. auto surface(ToolUtils::makeSurface(caller, info));
  34. SkCanvas* canvas = surface->getCanvas();
  35. SkPaint paint;
  36. paint.setBlendMode(SkBlendMode::kSrc);
  37. paint.setColor(SK_ColorWHITE);
  38. SkRect r = SkRect::MakeXYWH(0, 0,
  39. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  40. canvas->drawRect(r, paint);
  41. paint.setColor(SK_ColorRED);
  42. r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
  43. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  44. canvas->drawRect(r, paint);
  45. paint.setColor(SK_ColorGREEN);
  46. r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
  47. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  48. canvas->drawRect(r, paint);
  49. paint.setColor(SK_ColorTRANSPARENT);
  50. r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
  51. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  52. canvas->drawRect(r, paint);
  53. return surface->makeImageSnapshot();
  54. }
  55. // This GM tests the drawAtlas API with colors, different xfer modes
  56. // and transparency in the atlas image
  57. class DrawAtlasColorsGM : public skiagm::GM {
  58. public:
  59. DrawAtlasColorsGM() {
  60. this->setBGColor(0xFFCCCCCC);
  61. }
  62. protected:
  63. SkString onShortName() override {
  64. return SkString("draw-atlas-colors");
  65. }
  66. SkISize onISize() override {
  67. return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
  68. 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
  69. }
  70. void onDraw(SkCanvas* canvas) override {
  71. const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
  72. auto atlas = make_atlas(canvas, kAtlasSize);
  73. const SkBlendMode gModes[] = {
  74. SkBlendMode::kClear,
  75. SkBlendMode::kSrc,
  76. SkBlendMode::kDst,
  77. SkBlendMode::kSrcOver,
  78. SkBlendMode::kDstOver,
  79. SkBlendMode::kSrcIn,
  80. SkBlendMode::kDstIn,
  81. SkBlendMode::kSrcOut,
  82. SkBlendMode::kDstOut,
  83. SkBlendMode::kSrcATop,
  84. SkBlendMode::kDstATop,
  85. SkBlendMode::kXor,
  86. SkBlendMode::kPlus,
  87. SkBlendMode::kModulate,
  88. SkBlendMode::kScreen,
  89. SkBlendMode::kOverlay,
  90. SkBlendMode::kDarken,
  91. SkBlendMode::kLighten,
  92. SkBlendMode::kColorDodge,
  93. SkBlendMode::kColorBurn,
  94. SkBlendMode::kHardLight,
  95. SkBlendMode::kSoftLight,
  96. SkBlendMode::kDifference,
  97. SkBlendMode::kExclusion,
  98. SkBlendMode::kMultiply,
  99. SkBlendMode::kHue,
  100. SkBlendMode::kSaturation,
  101. SkBlendMode::kColor,
  102. SkBlendMode::kLuminosity,
  103. };
  104. SkColor gColors[] = {
  105. SK_ColorWHITE,
  106. SK_ColorRED,
  107. 0x88888888, // transparent grey
  108. 0x88000088 // transparent blue
  109. };
  110. const int numModes = SK_ARRAY_COUNT(gModes);
  111. SkASSERT(numModes == kNumXferModes);
  112. const int numColors = SK_ARRAY_COUNT(gColors);
  113. SkASSERT(numColors == kNumColors);
  114. SkRSXform xforms[numColors];
  115. SkRect rects[numColors];
  116. SkColor quadColors[numColors];
  117. SkPaint paint;
  118. paint.setAntiAlias(true);
  119. for (int i = 0; i < numColors; ++i) {
  120. xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
  121. rects[i] = target;
  122. quadColors[i] = gColors[i];
  123. }
  124. SkFont font(ToolUtils::create_portable_typeface(), kTextPad);
  125. for (int i = 0; i < numModes; ++i) {
  126. const char* label = SkBlendMode_Name(gModes[i]);
  127. canvas->drawString(label, i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
  128. font, paint);
  129. }
  130. for (int i = 0; i < numModes; ++i) {
  131. canvas->save();
  132. canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
  133. SkIntToScalar(kTextPad+kPad));
  134. // w/o a paint
  135. canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
  136. gModes[i], nullptr, nullptr);
  137. canvas->translate(0.0f, numColors*(target.height()+kPad));
  138. // w a paint
  139. canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
  140. gModes[i], nullptr, &paint);
  141. canvas->restore();
  142. }
  143. }
  144. private:
  145. static constexpr int kNumXferModes = 29;
  146. static constexpr int kNumColors = 4;
  147. static constexpr int kAtlasSize = 30;
  148. static constexpr int kPad = 2;
  149. static constexpr int kTextPad = 8;
  150. typedef GM INHERITED;
  151. };
  152. DEF_GM( return new DrawAtlasColorsGM; )