SampleAAClip.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2011 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 "include/core/SkCanvas.h"
  8. #include "include/core/SkPath.h"
  9. #include "samplecode/Sample.h"
  10. #include "src/core/SkAAClip.h"
  11. static void testop(const SkIRect& r0, const SkIRect& r1, SkRegion::Op op,
  12. const SkIRect& expectedR) {
  13. SkAAClip c0, c1, c2;
  14. c0.setRect(r0);
  15. c1.setRect(r1);
  16. c2.op(c0, c1, op);
  17. SkDEBUGCODE(SkIRect r2 = c2.getBounds());
  18. SkASSERT(r2 == expectedR);
  19. }
  20. static const struct {
  21. SkIRect r0;
  22. SkIRect r1;
  23. SkRegion::Op op;
  24. SkIRect expectedR;
  25. } gRec[] = {
  26. {{ 1, 2, 9, 3 }, { -3, 2, 5, 11 }, SkRegion::kDifference_Op, { 5, 2, 9, 3 }},
  27. {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kDifference_Op, { 1, 11, 5, 13 }},
  28. {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kReverseDifference_Op, { 1, 2, 5, 10 }},
  29. };
  30. static void testop() {
  31. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
  32. testop(gRec[i].r0, gRec[i].r1, gRec[i].op, gRec[i].expectedR);
  33. }
  34. }
  35. static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
  36. SkMask mask;
  37. SkBitmap bm;
  38. clip.copyToMask(&mask);
  39. SkAutoMaskFreeImage amfi(mask.fImage);
  40. bm.installMaskPixels(mask);
  41. SkPaint paint;
  42. canvas->drawBitmap(bm,
  43. SK_Scalar1 * mask.fBounds.fLeft,
  44. SK_Scalar1 * mask.fBounds.fTop,
  45. &paint);
  46. }
  47. class AAClipView : public Sample {
  48. SkString name() override { return SkString("AAClip"); }
  49. void onOnceBeforeDraw() override { testop(); }
  50. void onDrawContent(SkCanvas* canvas) override {
  51. #if 1
  52. SkAAClip aaclip;
  53. SkPath path;
  54. SkRect bounds;
  55. bounds.set(0, 0, 20, 20);
  56. bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
  57. // path.addRect(bounds);
  58. // path.addOval(bounds);
  59. path.addRoundRect(bounds, 4, 4);
  60. aaclip.setPath(path);
  61. canvas->translate(30, 30);
  62. drawClip(canvas, aaclip);
  63. SkAAClip aaclip2;
  64. path.offset(10, 10);
  65. aaclip2.setPath(path);
  66. canvas->translate(30, 0);
  67. drawClip(canvas, aaclip2);
  68. SkAAClip aaclip3;
  69. aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op);
  70. canvas->translate(30, 0);
  71. drawClip(canvas, aaclip3);
  72. #endif
  73. #if 0
  74. SkRect r;
  75. r.set(0, 0, this->width(), this->height());
  76. r.inset(20, 20);
  77. canvas->clipRect(r);
  78. SkPath path;
  79. path.addRect(r);
  80. SkPaint paint;
  81. paint.setAntiAlias(true);
  82. paint.setColor(SK_ColorRED);
  83. canvas->drawPath(path, paint);
  84. #endif
  85. }
  86. };
  87. DEF_SAMPLE( return new AAClipView(); )