xform.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef SK_BUILD_FOR_GOOGLE3
  9. #include "experimental/xform/SkShape.h"
  10. #include "experimental/xform/SkXform.h"
  11. #include "include/core/SkCanvas.h"
  12. #include "include/core/SkPaint.h"
  13. #include "tools/timer/TimeUtils.h"
  14. class XformGM : public skiagm::GM {
  15. sk_sp<MatrixXF> fRoot, fRA, fRB, fA, fB;
  16. sk_sp<Shape> fShape;
  17. public:
  18. XformGM() {
  19. fRoot = MatrixXF::Make();
  20. fRA = MatrixXF::Make(fRoot);
  21. fRB = MatrixXF::Make(fRoot);
  22. fA = MatrixXF::Make(fRA);
  23. fB = MatrixXF::Make(fRB);
  24. fRA->setRotate(30);
  25. fA->setTranslate(100, 0);
  26. fRB->setTranslate(100, 0);
  27. fB->setRotate(30);
  28. sk_sp<GroupShape> g = GroupShape::Make();
  29. g->append(GeoShape::Make(fA, {0, 0, 100, 60}, SK_ColorRED));
  30. g->append(GeoShape::Make(fB, {0, 0, 100, 60}, SK_ColorGREEN));
  31. g->append(GeoShape::Make(fRA, {0, 0, 100, 60}, SK_ColorBLUE));
  32. g->append(GeoShape::Make(fRB, {0, 0, 100, 60}, SK_ColorGRAY));
  33. g->append(GeoShape::Make(fRoot, {0, 0, 100, 60}, 0xFFCC8844));
  34. sk_sp<MatrixXF> sub = MatrixXF::Make();
  35. SkMatrix m;
  36. m.setScale(0.5, 0.5);
  37. m.postTranslate(50, 50);
  38. sub->setLocalMatrix(m);
  39. sk_sp<GroupShape> parent = GroupShape::Make();
  40. parent->append(g);
  41. parent->append(GroupShape::Make(sub, g));
  42. fShape = parent;
  43. }
  44. protected:
  45. SkString onShortName() override { return SkString("exp_xform"); }
  46. SkISize onISize() override { return SkISize::Make(520, 520); }
  47. void onDraw(SkCanvas* canvas) override {
  48. auto ctx = XContext::Make(canvas);
  49. if (0) {
  50. canvas->translate(2, 2);
  51. SkRect rect{0, 0, 100, 60};
  52. SkPaint paint; paint.setStyle(SkPaint::kStroke_Style);
  53. canvas->drawRect(rect, paint);
  54. canvas->save(); canvas->translate(10, 10);
  55. paint.setColor(SK_ColorRED); canvas->drawRect(rect, paint); canvas->restore();
  56. canvas->save(); canvas->scale(2, 2);
  57. paint.setColor(SK_ColorBLUE); canvas->drawRect(rect, paint); canvas->restore();
  58. canvas->save(); canvas->scale(2, 2); canvas->translate(10, 10);
  59. paint.setColor(SK_ColorBLACK); canvas->drawRect(rect, paint); canvas->restore();
  60. canvas->save(); canvas->translate(10, 10); canvas->scale(2, 2);
  61. paint.setColor(SK_ColorBLACK); canvas->drawRect(rect, paint); canvas->restore();
  62. auto x0 = MatrixXF::Make();
  63. auto x1 = MatrixXF::Make(x0);
  64. auto x2 = MatrixXF::Make(x1);
  65. x1->setScale(2, 2);
  66. x2->setTranslate(10, 10);
  67. auto sh = GeoShape::Make(x2, {0, 0, 100, 60}, 0x8800FF00);
  68. sh->draw(ctx.get());
  69. return;
  70. }
  71. fShape->draw(ctx.get());
  72. }
  73. bool onAnimate(double nanos) override {
  74. float scale = 3 + sinf(TimeUtils::Scaled(1e-9 * nanos, 1, 0)) * 2;
  75. fRoot->setScale(scale, scale);
  76. fRA->setRotate(TimeUtils::Scaled(1e-9 * nanos, 40, 0));
  77. fB->setRotate(TimeUtils::Scaled(1e-9 * nanos, 40*sqrtf(2), 0));
  78. return true;
  79. }
  80. private:
  81. typedef skiagm::GM INHERITED;
  82. };
  83. DEF_GM( return new XformGM; )
  84. #endif