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" |
robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 12 | #include "SkFontDescriptor.h" |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 13 | |
| 14 | DEFINE_string2(input, i, "", "skp on which to report"); |
| 15 | DEFINE_bool2(version, v, true, "version"); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 16 | DEFINE_bool2(cullRect, c, true, "cullRect"); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 17 | DEFINE_bool2(flags, f, true, "flags"); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 18 | DEFINE_bool2(tags, t, true, "tags"); |
| 19 | DEFINE_bool2(quiet, q, false, "quiet"); |
| 20 | |
| 21 | // This tool can print simple information about an SKP but its main use |
| 22 | // is just to check if an SKP has been truncated during the recording |
| 23 | // process. |
| 24 | // return codes: |
| 25 | static const int kSuccess = 0; |
| 26 | static const int kTruncatedFile = 1; |
| 27 | static const int kNotAnSKP = 2; |
| 28 | static const int kInvalidTag = 3; |
| 29 | static const int kMissingInput = 4; |
| 30 | static const int kIOError = 5; |
| 31 | |
Mike Klein | be28ee2 | 2017-02-06 12:46:20 -0500 | [diff] [blame^] | 32 | int main(int argc, char** argv) { |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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) { |
Mike Reed | b3f543d | 2016-10-04 15:12:01 -0400 | [diff] [blame] | 59 | SkDebugf("Version: %d\n", info.getVersion()); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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) { |
robertphillips | 4458312 | 2016-04-19 09:29:01 -0700 | [diff] [blame] | 67 | SkDebugf("Flags: "); |
| 68 | bool needsSeparator = false; |
| 69 | if (info.fFlags & SkPictInfo::kCrossProcess_Flag) { |
| 70 | SkDebugf("kCrossProcess"); |
| 71 | needsSeparator = true; |
| 72 | } |
| 73 | if (info.fFlags & SkPictInfo::kScalarIsFloat_Flag) { |
| 74 | if (needsSeparator) { |
| 75 | SkDebugf("|"); |
| 76 | } |
| 77 | SkDebugf("kScalarIsFloat"); |
| 78 | needsSeparator = true; |
| 79 | } |
| 80 | if (info.fFlags & SkPictInfo::kPtrIs64Bit_Flag) { |
| 81 | if (needsSeparator) { |
| 82 | SkDebugf("|"); |
| 83 | } |
| 84 | SkDebugf("kPtrIs64Bit"); |
| 85 | } |
| 86 | SkDebugf("\n"); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 87 | } |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 88 | |
| 89 | if (!stream.readBool()) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 90 | // If we read true there's a picture playback object flattened |
| 91 | // 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] | 92 | // reading the file. |
| 93 | return kSuccess; |
| 94 | } |
| 95 | |
| 96 | for (;;) { |
| 97 | uint32_t tag = stream.readU32(); |
| 98 | if (SK_PICT_EOF_TAG == tag) { |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | uint32_t chunkSize = stream.readU32(); |
| 103 | size_t curPos = stream.getPosition(); |
| 104 | |
| 105 | // "move" doesn't error out when seeking beyond the end of file |
| 106 | // so we need a preemptive check here. |
| 107 | if (curPos+chunkSize > totStreamSize) { |
| 108 | if (!FLAGS_quiet) { |
| 109 | SkDebugf("truncated file\n"); |
| 110 | } |
| 111 | return kTruncatedFile; |
| 112 | } |
| 113 | |
| 114 | // Not all the tags store the chunk size (in bytes). Three |
| 115 | // of them store tag-specific size information (e.g., number of |
| 116 | // fonts) instead. This forces us to early exit when those |
| 117 | // chunks are encountered. |
| 118 | switch (tag) { |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 119 | case SK_PICT_READER_TAG: |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 120 | if (FLAGS_tags && !FLAGS_quiet) { |
| 121 | SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize); |
| 122 | } |
| 123 | break; |
| 124 | case SK_PICT_FACTORY_TAG: |
| 125 | if (FLAGS_tags && !FLAGS_quiet) { |
| 126 | SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 127 | } |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 128 | break; |
robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 129 | case SK_PICT_TYPEFACE_TAG: { |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 130 | if (FLAGS_tags && !FLAGS_quiet) { |
| 131 | SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 132 | } |
robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 133 | |
| 134 | const int count = SkToInt(chunkSize); |
| 135 | for (int i = 0; i < count; i++) { |
| 136 | SkFontDescriptor desc; |
| 137 | if (!SkFontDescriptor::Deserialize(&stream, &desc)) { |
| 138 | if (!FLAGS_quiet) { |
| 139 | SkDebugf("File corruption in SkFontDescriptor\n"); |
| 140 | } |
| 141 | return kInvalidTag; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // clear this since we've consumed all the typefaces |
| 146 | chunkSize = 0; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 147 | break; |
robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 148 | } |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 149 | case SK_PICT_PICTURE_TAG: |
| 150 | if (FLAGS_tags && !FLAGS_quiet) { |
| 151 | SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize); |
| 152 | SkDebugf("Exiting early due to format limitations\n"); |
| 153 | } |
| 154 | return kSuccess; // TODO: need to store size in bytes |
| 155 | break; |
| 156 | case SK_PICT_BUFFER_SIZE_TAG: |
| 157 | if (FLAGS_tags && !FLAGS_quiet) { |
| 158 | SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize); |
| 159 | } |
| 160 | break; |
| 161 | default: |
| 162 | if (!FLAGS_quiet) { |
| 163 | SkDebugf("Unknown tag %d\n", chunkSize); |
| 164 | } |
| 165 | return kInvalidTag; |
| 166 | } |
| 167 | |
| 168 | if (!stream.move(chunkSize)) { |
| 169 | if (!FLAGS_quiet) { |
| 170 | SkDebugf("seek error\n"); |
| 171 | } |
| 172 | return kTruncatedFile; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return kSuccess; |
| 177 | } |