BisectSlide.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2018 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 "tools/viewer/BisectSlide.h"
  8. #include "include/core/SkPicture.h"
  9. #include "include/core/SkStream.h"
  10. #include "src/utils/SkOSPath.h"
  11. #include <utility>
  12. #ifdef SK_XML
  13. #include "experimental/svg/model/SkSVGDOM.h"
  14. #include "src/xml/SkDOM.h"
  15. #endif
  16. sk_sp<BisectSlide> BisectSlide::Create(const char filepath[]) {
  17. SkFILEStream stream(filepath);
  18. if (!stream.isValid()) {
  19. SkDebugf("BISECT: invalid input file at \"%s\"\n", filepath);
  20. return nullptr;
  21. }
  22. sk_sp<BisectSlide> bisect(new BisectSlide(filepath));
  23. if (bisect->fFilePath.endsWith(".svg")) {
  24. #ifdef SK_XML
  25. SkDOM xml;
  26. if (!xml.build(stream)) {
  27. SkDebugf("BISECT: XML parsing failed: \"%s\"\n", filepath);
  28. return nullptr;
  29. }
  30. sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
  31. if (!svg) {
  32. SkDebugf("BISECT: couldn't load svg at \"%s\"\n", filepath);
  33. return nullptr;
  34. }
  35. svg->setContainerSize(SkSize::Make(bisect->getDimensions()));
  36. svg->render(bisect.get());
  37. #else
  38. return nullptr;
  39. #endif
  40. } else {
  41. sk_sp<SkPicture> skp = SkPicture::MakeFromStream(&stream);
  42. if (!skp) {
  43. SkDebugf("BISECT: couldn't load skp at \"%s\"\n", filepath);
  44. return nullptr;
  45. }
  46. skp->playback(bisect.get());
  47. }
  48. return bisect;
  49. }
  50. BisectSlide::BisectSlide(const char filepath[])
  51. : SkCanvas(4096, 4096, nullptr)
  52. , fFilePath(filepath) {
  53. const char* basename = strrchr(fFilePath.c_str(), SkOSPath::SEPARATOR);
  54. fName.printf("BISECT_%s", basename ? basename + 1 : fFilePath.c_str());
  55. }
  56. // Called through SkPicture::playback only during creation.
  57. void BisectSlide::onDrawPath(const SkPath& path, const SkPaint& paint) {
  58. SkRect bounds;
  59. SkIRect ibounds;
  60. this->getTotalMatrix().mapRect(&bounds, path.getBounds());
  61. bounds.roundOut(&ibounds);
  62. fDrawBounds.join(ibounds);
  63. fFoundPaths.push_back() = {path, paint, this->getTotalMatrix()};
  64. }
  65. bool BisectSlide::onChar(SkUnichar c) {
  66. switch (c) {
  67. case 'X':
  68. if (!fTossedPaths.empty()) {
  69. using std::swap;
  70. swap(fFoundPaths, fTossedPaths);
  71. if ('X' == fTrail.back()) {
  72. fTrail.pop_back();
  73. } else {
  74. fTrail.push_back('X');
  75. }
  76. }
  77. return true;
  78. case 'x':
  79. if (fFoundPaths.count() > 1) {
  80. int midpt = (fFoundPaths.count() + 1) / 2;
  81. fPathHistory.emplace(fFoundPaths, fTossedPaths);
  82. fTossedPaths.reset(fFoundPaths.begin() + midpt, fFoundPaths.count() - midpt);
  83. fFoundPaths.resize_back(midpt);
  84. fTrail.push_back('x');
  85. }
  86. return true;
  87. case 'Z': {
  88. if (!fPathHistory.empty()) {
  89. fFoundPaths = fPathHistory.top().first;
  90. fTossedPaths = fPathHistory.top().second;
  91. fPathHistory.pop();
  92. char ch;
  93. do {
  94. ch = fTrail.back();
  95. fTrail.pop_back();
  96. } while (ch != 'x');
  97. }
  98. return true;
  99. }
  100. case 'D':
  101. SkDebugf("viewer --bisect %s", fFilePath.c_str());
  102. if (!fTrail.empty()) {
  103. SkDebugf(" ");
  104. for (char ch : fTrail) {
  105. SkDebugf("%c", ch);
  106. }
  107. }
  108. SkDebugf("\n");
  109. for (const FoundPath& foundPath : fFoundPaths) {
  110. foundPath.fPath.dump();
  111. }
  112. return true;
  113. }
  114. return false;
  115. }
  116. void BisectSlide::draw(SkCanvas* canvas) {
  117. SkAutoCanvasRestore acr(canvas, true);
  118. canvas->translate(-fDrawBounds.left(), -fDrawBounds.top());
  119. for (const FoundPath& path : fFoundPaths) {
  120. SkAutoCanvasRestore acr(canvas, true);
  121. canvas->concat(path.fViewMatrix);
  122. canvas->drawPath(path.fPath, path.fPaint);
  123. }
  124. }