Model.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2017 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 <memory>
  8. #include "tools/mdbviz/Model.h"
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkPicture.h"
  12. #include "include/core/SkStream.h"
  13. #include "tools/debugger/DebugCanvas.h"
  14. Model::Model() : fCurOp(0) {
  15. SkImageInfo ii = SkImageInfo::MakeN32Premul(1024, 1024);
  16. fBM.allocPixels(ii, 0);
  17. }
  18. Model::~Model() {
  19. this->resetOpList();
  20. }
  21. Model::ErrorCode Model::load(const char* filename) {
  22. std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(filename);
  23. if (!stream) {
  24. return ErrorCode::kCouldntOpenFile;
  25. }
  26. sk_sp<SkPicture> pic(SkPicture::MakeFromStream(stream.get()));
  27. if (!pic) {
  28. return ErrorCode::kCouldntDecodeSKP;
  29. }
  30. {
  31. std::unique_ptr<DebugCanvas> temp(
  32. new DebugCanvas(SkScalarCeilToInt(pic->cullRect().width()),
  33. SkScalarCeilToInt(pic->cullRect().height())));
  34. temp->setPicture(pic.get());
  35. pic->playback(temp.get());
  36. temp->setPicture(nullptr);
  37. this->resetOpList();
  38. temp->detachCommands(&fOps);
  39. }
  40. this->setCurOp(fOps.count()-1);
  41. return ErrorCode::kOK;
  42. }
  43. const char* Model::ErrorString(ErrorCode err) {
  44. static const char* kStrings[] = {
  45. "OK",
  46. "Couldn't read file",
  47. "Couldn't decode picture"
  48. };
  49. return kStrings[(int)err];
  50. }
  51. const char* Model::getOpName(int index) const {
  52. return DrawCommand::GetCommandString(fOps[index]->getType());
  53. }
  54. bool Model::isHierarchyPush(int index) const {
  55. DrawCommand::OpType type = fOps[index]->getType();
  56. return DrawCommand::kSave_OpType == type || DrawCommand::kSaveLayer_OpType == type ||
  57. DrawCommand::kBeginDrawPicture_OpType == type;
  58. }
  59. bool Model::isHierarchyPop(int index) const {
  60. DrawCommand::OpType type = fOps[index]->getType();
  61. return DrawCommand::kRestore_OpType == type || DrawCommand::kEndDrawPicture_OpType == type;
  62. }
  63. void Model::setCurOp(int curOp) {
  64. SkASSERT(curOp < fOps.count());
  65. if (curOp == fCurOp) {
  66. return; // the render state is already up to date
  67. }
  68. fCurOp = curOp;
  69. this->drawTo(fCurOp);
  70. }
  71. void Model::drawTo(int index) {
  72. SkASSERT(index < fOps.count());
  73. SkCanvas canvas(fBM);
  74. int saveCount = canvas.save();
  75. for (int i = 0; i <= index; ++i) {
  76. if (fOps[i]->isVisible()) {
  77. fOps[i]->execute(&canvas);
  78. }
  79. }
  80. canvas.restoreToCount(saveCount);
  81. }
  82. void Model::resetOpList() {
  83. for (int i = 0; i < fOps.count(); ++i) {
  84. delete fOps[i];
  85. }
  86. fOps.reset();
  87. fCurOp = 0;
  88. }