pathopsinverse.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/pathops/SkPathOps.h"
  18. #include "tools/ToolUtils.h"
  19. namespace skiagm {
  20. class PathOpsInverseGM : public GM {
  21. public:
  22. PathOpsInverseGM() {
  23. }
  24. protected:
  25. void onOnceBeforeDraw() override {
  26. const unsigned oneColor = ToolUtils::color_to_565(0xFF8080FF);
  27. const unsigned twoColor = 0x807F1f1f;
  28. SkColor blendColor = blend(oneColor, twoColor);
  29. makePaint(&fOnePaint, oneColor);
  30. makePaint(&fTwoPaint, twoColor);
  31. makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
  32. makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
  33. makePaint(&fOpPaint[kUnion_SkPathOp], ToolUtils::color_to_565(0xFFc0FFc0));
  34. makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
  35. makePaint(&fOpPaint[kXOR_SkPathOp], ToolUtils::color_to_565(0xFFa0FFe0));
  36. makePaint(&fOutlinePaint, 0xFF000000);
  37. fOutlinePaint.setStyle(SkPaint::kStroke_Style);
  38. }
  39. SkColor blend(SkColor one, SkColor two) {
  40. SkBitmap temp;
  41. temp.allocN32Pixels(1, 1);
  42. SkCanvas canvas(temp);
  43. canvas.drawColor(one);
  44. canvas.drawColor(two);
  45. void* pixels = temp.getPixels();
  46. return *(SkColor*) pixels;
  47. }
  48. void makePaint(SkPaint* paint, SkColor color) {
  49. paint->setAntiAlias(true);
  50. paint->setStyle(SkPaint::kFill_Style);
  51. paint->setColor(color);
  52. }
  53. SkString onShortName() override {
  54. return SkString("pathopsinverse");
  55. }
  56. SkISize onISize() override {
  57. return SkISize::Make(1200, 900);
  58. }
  59. void onDraw(SkCanvas* canvas) override {
  60. SkPath one, two;
  61. int yPos = 0;
  62. for (int oneFill = 0; oneFill <= 1; ++oneFill) {
  63. SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
  64. : SkPath::kEvenOdd_FillType;
  65. for (int twoFill = 0; twoFill <= 1; ++twoFill) {
  66. SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
  67. : SkPath::kEvenOdd_FillType;
  68. one.reset();
  69. one.setFillType(oneF);
  70. one.addRect(10, 10, 70, 70);
  71. two.reset();
  72. two.setFillType(twoF);
  73. two.addRect(40, 40, 100, 100);
  74. canvas->save();
  75. canvas->translate(0, SkIntToScalar(yPos));
  76. canvas->clipRect(SkRect::MakeWH(110, 110), true);
  77. canvas->drawPath(one, fOnePaint);
  78. canvas->drawPath(one, fOutlinePaint);
  79. canvas->drawPath(two, fTwoPaint);
  80. canvas->drawPath(two, fOutlinePaint);
  81. canvas->restore();
  82. int xPos = 150;
  83. for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
  84. SkPath result;
  85. Op(one, two, (SkPathOp) op, &result);
  86. canvas->save();
  87. canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
  88. canvas->clipRect(SkRect::MakeWH(110, 110), true);
  89. canvas->drawPath(result, fOpPaint[op]);
  90. canvas->drawPath(result, fOutlinePaint);
  91. canvas->restore();
  92. xPos += 150;
  93. }
  94. yPos += 150;
  95. }
  96. }
  97. }
  98. private:
  99. SkPaint fOnePaint;
  100. SkPaint fTwoPaint;
  101. SkPaint fOutlinePaint;
  102. SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1];
  103. typedef GM INHERITED;
  104. };
  105. //////////////////////////////////////////////////////////////////////////////
  106. DEF_GM( return new PathOpsInverseGM; )
  107. }