bigrect.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2016 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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkRect.h"
  12. #include "include/core/SkScalar.h"
  13. // Draws big rects with clip (0, 0, 35, 35). The size of the rects is given by big.
  14. static void draw_big_rect(SkCanvas* canvas, SkScalar big, const SkPaint& rectPaint) {
  15. // Looks like this:
  16. // +--+-+----+-+----+
  17. // | | | | | |
  18. // |--+-+----+-+----+
  19. // |--+-+----+-+----+
  20. // | | | | | |
  21. // | | | +-+ |
  22. // +--+-+--+ +--+
  23. // +--+-+--+ +--+
  24. // | | | +-+ |
  25. // | | | | | |
  26. // +--+-+----+-+----+
  27. canvas->clipRect({0, 0, 35, 35});
  28. // Align to pixel boundaries.
  29. canvas->translate(0.5, 0.5);
  30. SkRect horiz = SkRect::MakeLTRB(-big, 5, big, 10);
  31. canvas->drawRect(horiz, rectPaint);
  32. SkRect vert = SkRect::MakeLTRB(5, -big, 10, big);
  33. canvas->drawRect(vert, rectPaint);
  34. SkRect fromLeft = SkRect::MakeLTRB(-big, 20, 17, 25);
  35. canvas->drawRect(fromLeft, rectPaint);
  36. SkRect fromTop = SkRect::MakeLTRB(20, -big, 25, 17);
  37. canvas->drawRect(fromTop, rectPaint);
  38. SkRect fromRight = SkRect::MakeLTRB(28, 20, big, 25);
  39. canvas->drawRect(fromRight, rectPaint);
  40. SkRect fromBottom = SkRect::MakeLTRB(20, 28, 25, big);
  41. canvas->drawRect(fromBottom, rectPaint);
  42. SkRect leftBorder = SkRect::MakeLTRB(-2, -1, 0, 35);
  43. canvas->drawRect(leftBorder, rectPaint);
  44. SkRect topBorder = SkRect::MakeLTRB(-1, -2, 35, 0);
  45. canvas->drawRect(topBorder, rectPaint);
  46. SkRect rightBorder = SkRect::MakeLTRB(34, -1, 36, 35);
  47. canvas->drawRect(rightBorder, rectPaint);
  48. SkRect bottomBorder = SkRect::MakeLTRB(-1, 34, 35, 36);
  49. canvas->drawRect(bottomBorder, rectPaint);
  50. SkPaint outOfBoundsPaint;
  51. outOfBoundsPaint.setColor(SK_ColorRED);
  52. outOfBoundsPaint.setStyle(SkPaint::kStroke_Style);
  53. outOfBoundsPaint.setStrokeWidth(0);
  54. SkRect outOfBounds = SkRect::MakeLTRB(-1, -1, 35, 35);
  55. canvas->drawRect(outOfBounds, outOfBoundsPaint);
  56. }
  57. DEF_SIMPLE_GM(bigrect, canvas, 325, 125) {
  58. // Test with sizes:
  59. // - reasonable size (for comparison),
  60. // - outside the range of int32, and
  61. // - outside the range of SkFixed.
  62. static const SkScalar sizes[] = {SkIntToScalar(100), 5e10f, 1e6f};
  63. for (int i = 0; i < 8; i++) {
  64. for (int j = 0; j < 3; j++) {
  65. canvas->save();
  66. canvas->translate(SkIntToScalar(i*40+5), SkIntToScalar(j*40+5));
  67. SkPaint paint;
  68. paint.setColor(SK_ColorBLUE);
  69. // These are the three parameters that affect the behavior of SkDraw::drawRect.
  70. if (i & 1) {
  71. paint.setStyle(SkPaint::kFill_Style);
  72. } else {
  73. paint.setStyle(SkPaint::kStroke_Style);
  74. }
  75. if (i & 2) {
  76. paint.setStrokeWidth(1);
  77. } else {
  78. paint.setStrokeWidth(0);
  79. }
  80. if (i & 4) {
  81. paint.setAntiAlias(true);
  82. } else {
  83. paint.setAntiAlias(false);
  84. }
  85. const SkScalar big = SkFloatToScalar(sizes[j]);
  86. draw_big_rect(canvas, big, paint);
  87. canvas->restore();
  88. }
  89. }
  90. }