SampleManyRects.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "include/core/SkCanvas.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/utils/SkRandom.h"
  11. #include "samplecode/Sample.h"
  12. /**
  13. * Animated sample used to develop a predecessor of GrDrawOp combining.
  14. */
  15. class ManyRectsView : public Sample {
  16. private:
  17. enum {
  18. N = 1000,
  19. };
  20. public:
  21. ManyRectsView() {}
  22. protected:
  23. SkString name() override { return SkString("ManyRects"); }
  24. void onDrawContent(SkCanvas* canvas) override {
  25. SkISize dsize = canvas->getBaseLayerSize();
  26. canvas->clear(0xFFF0E0F0);
  27. for (int i = 0; i < N; ++i) {
  28. SkRect rect = SkRect::MakeWH(SkIntToScalar(fRandom.nextRangeU(10, 100)),
  29. SkIntToScalar(fRandom.nextRangeU(10, 100)));
  30. int x = fRandom.nextRangeU(0, dsize.fWidth);
  31. int y = fRandom.nextRangeU(0, dsize.fHeight);
  32. canvas->save();
  33. canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
  34. // Uncomment to test rotated rect draw combining.
  35. if (false) {
  36. SkMatrix rotate;
  37. rotate.setRotate(fRandom.nextUScalar1() * 360,
  38. SkIntToScalar(x) + SkScalarHalf(rect.fRight),
  39. SkIntToScalar(y) + SkScalarHalf(rect.fBottom));
  40. canvas->concat(rotate);
  41. }
  42. SkRect clipRect = rect;
  43. // This clip will always contain the entire rect. It's here to give the GPU op combining
  44. // code a little more challenge.
  45. clipRect.outset(10, 10);
  46. canvas->clipRect(clipRect);
  47. SkPaint paint;
  48. paint.setColor(fRandom.nextU());
  49. canvas->drawRect(rect, paint);
  50. canvas->restore();
  51. }
  52. }
  53. private:
  54. SkRandom fRandom;
  55. typedef Sample INHERITED;
  56. };
  57. //////////////////////////////////////////////////////////////////////////////
  58. DEF_SAMPLE( return new ManyRectsView(); )