commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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 | |
| 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkPicture.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 10 | #include "SkPictureData.h" |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | |
| 13 | DEFINE_string2(input, i, "", "skp on which to report"); |
| 14 | DEFINE_bool2(version, v, true, "version"); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 15 | DEFINE_bool2(cullRect, c, true, "cullRect"); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 16 | DEFINE_bool2(flags, f, true, "flags"); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 17 | DEFINE_bool2(tags, t, true, "tags"); |
| 18 | DEFINE_bool2(quiet, q, false, "quiet"); |
| 19 | |
| 20 | // This tool can print simple information about an SKP but its main use |
| 21 | // is just to check if an SKP has been truncated during the recording |
| 22 | // process. |
| 23 | // return codes: |
| 24 | static const int kSuccess = 0; |
| 25 | static const int kTruncatedFile = 1; |
| 26 | static const int kNotAnSKP = 2; |
| 27 | static const int kInvalidTag = 3; |
| 28 | static const int kMissingInput = 4; |
| 29 | static const int kIOError = 5; |
| 30 | |
| 31 | int tool_main(int argc, char** argv); |
| 32 | int tool_main(int argc, char** argv) { |
| 33 | SkCommandLineFlags::SetUsage("Prints information about an skp file"); |
| 34 | SkCommandLineFlags::Parse(argc, argv); |
| 35 | |
| 36 | if (FLAGS_input.count() != 1) { |
| 37 | if (!FLAGS_quiet) { |
| 38 | SkDebugf("Missing input file\n"); |
| 39 | } |
| 40 | return kMissingInput; |
| 41 | } |
| 42 | |
| 43 | SkFILEStream stream(FLAGS_input[0]); |
| 44 | if (!stream.isValid()) { |
| 45 | if (!FLAGS_quiet) { |
| 46 | SkDebugf("Couldn't open file\n"); |
| 47 | } |
| 48 | return kIOError; |
| 49 | } |
| 50 | |
| 51 | size_t totStreamSize = stream.getLength(); |
| 52 | |
| 53 | SkPictInfo info; |
| 54 | if (!SkPicture::InternalOnly_StreamIsSKP(&stream, &info)) { |
| 55 | return kNotAnSKP; |
| 56 | } |
| 57 | |
| 58 | if (FLAGS_version && !FLAGS_quiet) { |
| 59 | SkDebugf("Version: %d\n", info.fVersion); |
| 60 | } |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 61 | if (FLAGS_cullRect && !FLAGS_quiet) { |
mtklein | 88fd0fb | 2014-12-01 06:56:38 -0800 | [diff] [blame] | 62 | SkDebugf("Cull Rect: %f,%f,%f,%f\n", |
| 63 | info.fCullRect.fLeft, info.fCullRect.fTop, |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 64 | info.fCullRect.fRight, info.fCullRect.fBottom); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 65 | } |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 66 | if (FLAGS_flags && !FLAGS_quiet) { |
| 67 | SkDebugf("Flags: 0x%x\n", info.fFlags); |
| 68 | } |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 69 | |
| 70 | if (!stream.readBool()) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 71 | // If we read true there's a picture playback object flattened |
| 72 | // in the file; if false, there isn't a playback, so we're done |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 73 | // reading the file. |
| 74 | return kSuccess; |
| 75 | } |
| 76 | |
| 77 | for (;;) { |
| 78 | uint32_t tag = stream.readU32(); |
| 79 | if (SK_PICT_EOF_TAG == tag) { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | uint32_t chunkSize = stream.readU32(); |
| 84 | size_t curPos = stream.getPosition(); |
| 85 | |
| 86 | // "move" doesn't error out when seeking beyond the end of file |
| 87 | // so we need a preemptive check here. |
| 88 | if (curPos+chunkSize > totStreamSize) { |
| 89 | if (!FLAGS_quiet) { |
| 90 | SkDebugf("truncated file\n"); |
| 91 | } |
| 92 | return kTruncatedFile; |
| 93 | } |
| 94 | |
| 95 | // Not all the tags store the chunk size (in bytes). Three |
| 96 | // of them store tag-specific size information (e.g., number of |
| 97 | // fonts) instead. This forces us to early exit when those |
| 98 | // chunks are encountered. |
| 99 | switch (tag) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 100 | case SK_PICT_READER_TAG: |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 101 | if (FLAGS_tags && !FLAGS_quiet) { |
| 102 | SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize); |
| 103 | } |
| 104 | break; |
| 105 | case SK_PICT_FACTORY_TAG: |
| 106 | if (FLAGS_tags && !FLAGS_quiet) { |
| 107 | SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 109 | break; |
| 110 | case SK_PICT_TYPEFACE_TAG: |
| 111 | if (FLAGS_tags && !FLAGS_quiet) { |
| 112 | SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize); |
| 113 | SkDebugf("Exiting early due to format limitations\n"); |
| 114 | } |
| 115 | return kSuccess; // TODO: need to store size in bytes |
| 116 | break; |
| 117 | case SK_PICT_PICTURE_TAG: |
| 118 | if (FLAGS_tags && !FLAGS_quiet) { |
| 119 | SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize); |
| 120 | SkDebugf("Exiting early due to format limitations\n"); |
| 121 | } |
| 122 | return kSuccess; // TODO: need to store size in bytes |
| 123 | break; |
| 124 | case SK_PICT_BUFFER_SIZE_TAG: |
| 125 | if (FLAGS_tags && !FLAGS_quiet) { |
| 126 | SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize); |
| 127 | } |
| 128 | break; |
| 129 | default: |
| 130 | if (!FLAGS_quiet) { |
| 131 | SkDebugf("Unknown tag %d\n", chunkSize); |
| 132 | } |
| 133 | return kInvalidTag; |
| 134 | } |
| 135 | |
| 136 | if (!stream.move(chunkSize)) { |
| 137 | if (!FLAGS_quiet) { |
| 138 | SkDebugf("seek error\n"); |
| 139 | } |
| 140 | return kTruncatedFile; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return kSuccess; |
| 145 | } |
| 146 | |
| 147 | #if !defined SK_BUILD_FOR_IOS |
| 148 | int main(int argc, char * const argv[]) { |
| 149 | return tool_main(argc, (char**) argv); |
| 150 | } |
| 151 | #endif |