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