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" |
| 10 | #include "SkPicturePlayback.h" |
| 11 | #include "SkStream.h" |
| 12 | |
| 13 | DEFINE_string2(input, i, "", "skp on which to report"); |
| 14 | DEFINE_bool2(version, v, true, "version"); |
| 15 | DEFINE_bool2(width, w, true, "width"); |
| 16 | DEFINE_bool2(height, h, true, "height"); |
| 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 | } |
| 61 | if (FLAGS_width && !FLAGS_quiet) { |
| 62 | SkDebugf("Width: %d\n", info.fWidth); |
| 63 | } |
| 64 | if (FLAGS_height && !FLAGS_quiet) { |
| 65 | SkDebugf("Height: %d\n", info.fHeight); |
| 66 | } |
| 67 | |
| 68 | if (!stream.readBool()) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 69 | // If we read true there's a picture playback object flattened |
| 70 | // 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] | 71 | // reading the file. |
| 72 | return kSuccess; |
| 73 | } |
| 74 | |
| 75 | for (;;) { |
| 76 | uint32_t tag = stream.readU32(); |
| 77 | if (SK_PICT_EOF_TAG == tag) { |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | uint32_t chunkSize = stream.readU32(); |
| 82 | size_t curPos = stream.getPosition(); |
| 83 | |
| 84 | // "move" doesn't error out when seeking beyond the end of file |
| 85 | // so we need a preemptive check here. |
| 86 | if (curPos+chunkSize > totStreamSize) { |
| 87 | if (!FLAGS_quiet) { |
| 88 | SkDebugf("truncated file\n"); |
| 89 | } |
| 90 | return kTruncatedFile; |
| 91 | } |
| 92 | |
| 93 | // Not all the tags store the chunk size (in bytes). Three |
| 94 | // of them store tag-specific size information (e.g., number of |
| 95 | // fonts) instead. This forces us to early exit when those |
| 96 | // chunks are encountered. |
| 97 | switch (tag) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 98 | case SK_PICT_READER_TAG: |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 99 | if (FLAGS_tags && !FLAGS_quiet) { |
| 100 | SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize); |
| 101 | } |
| 102 | break; |
| 103 | case SK_PICT_FACTORY_TAG: |
| 104 | if (FLAGS_tags && !FLAGS_quiet) { |
| 105 | SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 106 | } |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 107 | // Remove this code when v21 and below are no longer supported |
| 108 | #ifndef DISABLE_V21_COMPATIBILITY_CODE |
| 109 | if (info.fVersion < 22) { |
| 110 | if (!FLAGS_quiet) { |
| 111 | SkDebugf("Exiting early due to format limitations\n"); |
| 112 | } |
| 113 | return kSuccess; // TODO: need to store size in bytes |
| 114 | } |
| 115 | #endif |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 116 | break; |
| 117 | case SK_PICT_TYPEFACE_TAG: |
| 118 | if (FLAGS_tags && !FLAGS_quiet) { |
| 119 | SkDebugf("SK_PICT_TYPEFACE_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_PICTURE_TAG: |
| 125 | if (FLAGS_tags && !FLAGS_quiet) { |
| 126 | SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize); |
| 127 | SkDebugf("Exiting early due to format limitations\n"); |
| 128 | } |
| 129 | return kSuccess; // TODO: need to store size in bytes |
| 130 | break; |
| 131 | case SK_PICT_BUFFER_SIZE_TAG: |
| 132 | if (FLAGS_tags && !FLAGS_quiet) { |
| 133 | SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize); |
| 134 | } |
| 135 | break; |
| 136 | default: |
| 137 | if (!FLAGS_quiet) { |
| 138 | SkDebugf("Unknown tag %d\n", chunkSize); |
| 139 | } |
| 140 | return kInvalidTag; |
| 141 | } |
| 142 | |
| 143 | if (!stream.move(chunkSize)) { |
| 144 | if (!FLAGS_quiet) { |
| 145 | SkDebugf("seek error\n"); |
| 146 | } |
| 147 | return kTruncatedFile; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return kSuccess; |
| 152 | } |
| 153 | |
| 154 | #if !defined SK_BUILD_FOR_IOS |
| 155 | int main(int argc, char * const argv[]) { |
| 156 | return tool_main(argc, (char**) argv); |
| 157 | } |
| 158 | #endif |