dump_record.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2014 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/SkBitmap.h"
  8. #include "include/core/SkPicture.h"
  9. #include "include/core/SkPictureRecorder.h"
  10. #include "include/core/SkStream.h"
  11. #include "src/core/SkRecordDraw.h"
  12. #include "src/core/SkRecordOpts.h"
  13. #include "src/core/SkRecorder.h"
  14. #include "tools/DumpRecord.h"
  15. #include "tools/flags/CommandLineFlags.h"
  16. #include <stdio.h>
  17. static DEFINE_string2(skps, r, "", ".SKPs to dump.");
  18. static DEFINE_string(match, "", "The usual filters on file names to dump.");
  19. static DEFINE_bool2(optimize, O, false, "Run SkRecordOptimize before dumping.");
  20. static DEFINE_bool(optimize2, false, "Run SkRecordOptimize2 before dumping.");
  21. static DEFINE_int(tile, 1000000000, "Simulated tile size.");
  22. static DEFINE_bool(timeWithCommand, false,
  23. "If true, print time next to command, else in first column.");
  24. static DEFINE_string2(write, w, "", "Write the (optimized) picture to the named file.");
  25. static void dump(const char* name, int w, int h, const SkRecord& record) {
  26. SkBitmap bitmap;
  27. bitmap.allocN32Pixels(w, h);
  28. SkCanvas canvas(bitmap);
  29. canvas.clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile),
  30. SkIntToScalar(FLAGS_tile)));
  31. printf("%s %s\n", FLAGS_optimize ? "optimized" : "not-optimized", name);
  32. DumpRecord(record, &canvas, FLAGS_timeWithCommand);
  33. }
  34. int main(int argc, char** argv) {
  35. CommandLineFlags::Parse(argc, argv);
  36. for (int i = 0; i < FLAGS_skps.count(); i++) {
  37. if (CommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) {
  38. continue;
  39. }
  40. std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(FLAGS_skps[i]);
  41. if (!stream) {
  42. SkDebugf("Could not read %s.\n", FLAGS_skps[i]);
  43. return 1;
  44. }
  45. sk_sp<SkPicture> src(SkPicture::MakeFromStream(stream.get()));
  46. if (!src) {
  47. SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]);
  48. return 1;
  49. }
  50. const int w = SkScalarCeilToInt(src->cullRect().width());
  51. const int h = SkScalarCeilToInt(src->cullRect().height());
  52. SkRecord record;
  53. SkRecorder canvas(&record, w, h);
  54. src->playback(&canvas);
  55. if (FLAGS_optimize) {
  56. SkRecordOptimize(&record);
  57. }
  58. if (FLAGS_optimize2) {
  59. SkRecordOptimize2(&record);
  60. }
  61. dump(FLAGS_skps[i], w, h, record);
  62. if (FLAGS_write.count() > 0) {
  63. SkPictureRecorder r;
  64. SkRecordDraw(record,
  65. r.beginRecording(SkRect::MakeIWH(w, h)),
  66. nullptr,
  67. nullptr,
  68. 0,
  69. nullptr,
  70. nullptr);
  71. sk_sp<SkPicture> dst(r.finishRecordingAsPicture());
  72. SkFILEWStream ostream(FLAGS_write[0]);
  73. dst->serialize(&ostream);
  74. }
  75. }
  76. return 0;
  77. }