junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
commit-bot@chromium.org | 56799e2 | 2013-07-16 18:21:46 +0000 | [diff] [blame] | 8 | #include "LazyDecodeBitmap.h" |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 9 | #include "CopyTilesRenderer.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 12 | #include "SkCommandLineFlags.h" |
borenet@google.com | 10ef79e | 2012-09-10 17:19:06 +0000 | [diff] [blame] | 13 | #include "SkGraphics.h" |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 14 | #include "SkImageDecoder.h" |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 16 | #include "SkMath.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 17 | #include "SkOSFile.h" |
| 18 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 19 | #include "SkPictureRecorder.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 20 | #include "SkStream.h" |
| 21 | #include "SkString.h" |
commit-bot@chromium.org | 90c0fbd | 2014-05-09 03:18:41 +0000 | [diff] [blame] | 22 | |
| 23 | #include "image_expectations.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 24 | #include "PictureRenderer.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 25 | #include "PictureRenderingFlags.h" |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 26 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 27 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 28 | // Flags used by this file, alphabetically: |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 29 | DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 30 | DECLARE_bool(deferImageDecoding); |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 31 | DEFINE_string(descriptions, "", "one or more key=value pairs to add to the descriptions section " |
| 32 | "of the JSON summary."); |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame^] | 33 | DEFINE_string(imageBaseGSUrl, "", "The Google Storage image base URL the images are stored in."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 34 | DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ " |
| 35 | "by more than this amount are considered errors, though all diffs are reported. " |
| 36 | "Requires --validate."); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 37 | DEFINE_string(mismatchPath, "", "Write images for tests that failed due to " |
| 38 | "pixel mismatches into this directory."); |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 39 | DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering."); |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 40 | DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from."); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 41 | DECLARE_string(readPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 42 | DEFINE_bool(writeChecksumBasedFilenames, false, |
| 43 | "When writing out images, use checksum-based filenames."); |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 44 | DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a " |
| 45 | "file rather than decoding it. Requires writePath to be set. Skips drawing the full " |
| 46 | "skp to a file. Not compatible with deferImageDecoding."); |
commit-bot@chromium.org | 4610a46 | 2014-04-29 19:39:22 +0000 | [diff] [blame] | 47 | DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to."); |
| 48 | DEFINE_string2(writePath, w, "", "Directory to write the rendered images into."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 49 | DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a " |
| 50 | "file, instead of an image for each tile."); |
| 51 | DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as " |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 52 | "the picture rendered in simple mode. When used in conjunction with --bbh, results " |
| 53 | "are validated against the picture rendered in the same mode, but without the bbh."); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 54 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 55 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | /** |
| 58 | * Table for translating from format of data to a suffix. |
| 59 | */ |
| 60 | struct Format { |
| 61 | SkImageDecoder::Format fFormat; |
| 62 | const char* fSuffix; |
| 63 | }; |
| 64 | static const Format gFormats[] = { |
| 65 | { SkImageDecoder::kBMP_Format, ".bmp" }, |
| 66 | { SkImageDecoder::kGIF_Format, ".gif" }, |
| 67 | { SkImageDecoder::kICO_Format, ".ico" }, |
| 68 | { SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 69 | { SkImageDecoder::kPNG_Format, ".png" }, |
| 70 | { SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 71 | { SkImageDecoder::kWEBP_Format, ".webp" }, |
| 72 | { SkImageDecoder::kUnknown_Format, "" }, |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Get an appropriate suffix for an image format. |
| 77 | */ |
| 78 | static const char* get_suffix_from_format(SkImageDecoder::Format format) { |
| 79 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 80 | if (gFormats[i].fFormat == format) { |
| 81 | return gFormats[i].fSuffix; |
| 82 | } |
| 83 | } |
| 84 | return ""; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Base name for an image file created from the encoded data in an skp. |
| 89 | */ |
| 90 | static SkString gInputFileName; |
| 91 | |
| 92 | /** |
| 93 | * Number to be appended to the image file name so that it is unique. |
| 94 | */ |
| 95 | static uint32_t gImageNo; |
| 96 | |
| 97 | /** |
| 98 | * Set up the name for writing encoded data to a file. |
| 99 | * Sets gInputFileName to name, minus any extension ".*" |
| 100 | * Sets gImageNo to 0, so images from file "X.skp" will |
| 101 | * look like "X_<gImageNo>.<suffix>", beginning with 0 |
| 102 | * for each new skp. |
| 103 | */ |
| 104 | static void reset_image_file_base_name(const SkString& name) { |
| 105 | gImageNo = 0; |
| 106 | // Remove ".skp" |
| 107 | const char* cName = name.c_str(); |
| 108 | const char* dot = strrchr(cName, '.'); |
| 109 | if (dot != NULL) { |
| 110 | gInputFileName.set(cName, dot - cName); |
| 111 | } else { |
| 112 | gInputFileName.set(name); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Write the raw encoded bitmap data to a file. |
| 118 | */ |
| 119 | static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) { |
| 120 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 121 | SkMemoryStream memStream(buffer, size); |
| 122 | SkString outPath; |
| 123 | SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream); |
| 124 | SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++, |
| 125 | get_suffix_from_format(format)); |
| 126 | SkString dir(FLAGS_writePath[0]); |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 127 | outPath = SkOSPath::Join(dir.c_str(), name.c_str()); |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 128 | SkFILEWStream fileStream(outPath.c_str()); |
| 129 | if (!(fileStream.isValid() && fileStream.write(buffer, size))) { |
| 130 | SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str()); |
| 131 | } |
| 132 | // Put in a dummy bitmap. |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 133 | return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType, |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 134 | SkImageDecoder::kDecodeBounds_Mode); |
| 135 | } |
| 136 | |
| 137 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 138 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Called only by render_picture(). |
| 141 | */ |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 142 | static bool render_picture_internal(const SkString& inputPath, const SkString* writePath, |
| 143 | const SkString* mismatchPath, |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 144 | sk_tools::PictureRenderer& renderer, |
| 145 | SkBitmap** out) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 146 | SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 147 | SkString writePathString; |
| 148 | if (NULL != writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) { |
| 149 | writePathString.set(*writePath); |
| 150 | } |
| 151 | SkString mismatchPathString; |
| 152 | if (NULL != mismatchPath && mismatchPath->size() > 0) { |
| 153 | mismatchPathString.set(*mismatchPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 154 | } |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 155 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 156 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 157 | inputStream.setPath(inputPath.c_str()); |
| 158 | if (!inputStream.isValid()) { |
| 159 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 160 | return false; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 161 | } |
| 162 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 163 | SkPicture::InstallPixelRefProc proc; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 164 | if (FLAGS_deferImageDecoding) { |
commit-bot@chromium.org | 56799e2 | 2013-07-16 18:21:46 +0000 | [diff] [blame] | 165 | proc = &sk_tools::LazyDecodeBitmap; |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 166 | } else if (FLAGS_writeEncodedImages) { |
| 167 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 168 | reset_image_file_base_name(inputFilename); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 169 | proc = &write_image_to_file; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 170 | } else { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 171 | proc = &SkImageDecoder::DecodeMemory; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 172 | } |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 173 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 174 | SkDebugf("deserializing... %s\n", inputPath.c_str()); |
| 175 | |
robertphillips | e947075 | 2014-08-12 10:12:40 -0700 | [diff] [blame] | 176 | SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc)); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 177 | |
| 178 | if (NULL == picture) { |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 179 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 180 | return false; |
| 181 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 182 | |
robertphillips | e947075 | 2014-08-12 10:12:40 -0700 | [diff] [blame] | 183 | if (FLAGS_preprocess) { |
| 184 | // Because the GPU preprocessing step relies on the in-memory picture |
| 185 | // statistics we need to rerecord the picture here |
| 186 | SkPictureRecorder recorder; |
| 187 | picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0)); |
| 188 | picture.reset(recorder.endRecording()); |
| 189 | } |
| 190 | |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 191 | while (FLAGS_bench_record) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 192 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 193 | picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 194 | SkAutoTUnref<SkPicture> other(recorder.endRecording()); |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 195 | } |
| 196 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 197 | SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(), |
borenet@google.com | 03fcee8 | 2012-09-10 18:18:38 +0000 | [diff] [blame] | 198 | inputPath.c_str()); |
skia.committer@gmail.com | 1d225f2 | 2012-09-14 02:01:10 +0000 | [diff] [blame] | 199 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 200 | renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename, |
| 201 | FLAGS_writeChecksumBasedFilenames); |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 202 | |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 203 | if (FLAGS_preprocess) { |
| 204 | if (NULL != renderer.getCanvas()) { |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 205 | renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture()); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
scroggo@google.com | b4773b4 | 2012-10-01 20:06:09 +0000 | [diff] [blame] | 209 | renderer.setup(); |
commit-bot@chromium.org | 8991c67 | 2014-05-22 00:36:05 +0000 | [diff] [blame] | 210 | renderer.enableWrites(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 211 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 212 | bool success = renderer.render(out); |
| 213 | if (!success) { |
| 214 | SkDebugf("Failed to render %s\n", inputFilename.c_str()); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 215 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 216 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 217 | renderer.end(); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 218 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 219 | return success; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 220 | } |
| 221 | |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 222 | static inline int getByte(uint32_t value, int index) { |
| 223 | SkASSERT(0 <= index && index < 4); |
| 224 | return (value >> (index * 8)) & 0xFF; |
| 225 | } |
| 226 | |
| 227 | static int MaxByteDiff(uint32_t v1, uint32_t v2) { |
| 228 | return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))), |
| 229 | SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3)))); |
| 230 | } |
| 231 | |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 232 | class AutoRestoreBbhType { |
| 233 | public: |
| 234 | AutoRestoreBbhType() { |
| 235 | fRenderer = NULL; |
junov@chromium.org | d34fda1 | 2013-03-13 19:03:26 +0000 | [diff] [blame] | 236 | fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void set(sk_tools::PictureRenderer* renderer, |
| 240 | sk_tools::PictureRenderer::BBoxHierarchyType bbhType) { |
| 241 | fRenderer = renderer; |
| 242 | fSavedBbhType = renderer->getBBoxHierarchyType(); |
| 243 | renderer->setBBoxHierarchyType(bbhType); |
| 244 | } |
| 245 | |
| 246 | ~AutoRestoreBbhType() { |
| 247 | if (NULL != fRenderer) { |
| 248 | fRenderer->setBBoxHierarchyType(fSavedBbhType); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | sk_tools::PictureRenderer* fRenderer; |
| 254 | sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType; |
| 255 | }; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 256 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 257 | /** |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 258 | * Render the SKP file(s) within inputPath. |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 259 | * |
| 260 | * @param inputPath path to an individual SKP file, or a directory of SKP files |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 261 | * @param writePath if not NULL, write all image(s) generated into this directory |
| 262 | * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 263 | * @param renderer PictureRenderer to use to render the SKPs |
| 264 | * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary |
| 265 | */ |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 266 | static bool render_picture(const SkString& inputPath, const SkString* writePath, |
| 267 | const SkString* mismatchPath, sk_tools::PictureRenderer& renderer, |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 268 | sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 269 | int diffs[256] = {0}; |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 270 | SkBitmap* bitmap = NULL; |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 271 | renderer.setJsonSummaryPtr(jsonSummaryPtr); |
| 272 | bool success = render_picture_internal(inputPath, |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 273 | FLAGS_writeWholeImage ? NULL : writePath, |
| 274 | FLAGS_writeWholeImage ? NULL : mismatchPath, |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 275 | renderer, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 276 | FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 277 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 278 | if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 279 | SkDebugf("Failed to draw the picture.\n"); |
| 280 | SkDELETE(bitmap); |
| 281 | return false; |
| 282 | } |
| 283 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 284 | if (FLAGS_validate) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 285 | SkBitmap* referenceBitmap = NULL; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 286 | sk_tools::PictureRenderer* referenceRenderer; |
| 287 | // If the renderer uses a BBoxHierarchy, then the reference renderer |
skia.committer@gmail.com | 03682be | 2013-03-14 07:02:51 +0000 | [diff] [blame] | 288 | // will be the same renderer, without the bbh. |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 289 | AutoRestoreBbhType arbbh; |
| 290 | if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType != |
| 291 | renderer.getBBoxHierarchyType()) { |
| 292 | referenceRenderer = &renderer; |
| 293 | referenceRenderer->ref(); // to match auto unref below |
| 294 | arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType); |
| 295 | } else { |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 296 | #if SK_SUPPORT_GPU |
| 297 | referenceRenderer = SkNEW_ARGS(sk_tools::SimplePictureRenderer, |
| 298 | (renderer.getGrContextOptions())); |
| 299 | #else |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 300 | referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer); |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 301 | #endif |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 302 | } |
| 303 | SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer); |
| 304 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 305 | success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer, |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 306 | &referenceBitmap); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 307 | |
junov@chromium.org | c19c191 | 2013-03-12 19:56:49 +0000 | [diff] [blame] | 308 | if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 309 | SkDebugf("Failed to draw the reference picture.\n"); |
| 310 | SkDELETE(bitmap); |
| 311 | SkDELETE(referenceBitmap); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | if (success && (bitmap->width() != referenceBitmap->width())) { |
| 316 | SkDebugf("Expected image width: %i, actual image width %i.\n", |
| 317 | referenceBitmap->width(), bitmap->width()); |
| 318 | SkDELETE(bitmap); |
| 319 | SkDELETE(referenceBitmap); |
| 320 | return false; |
| 321 | } |
| 322 | if (success && (bitmap->height() != referenceBitmap->height())) { |
| 323 | SkDebugf("Expected image height: %i, actual image height %i", |
| 324 | referenceBitmap->height(), bitmap->height()); |
| 325 | SkDELETE(bitmap); |
| 326 | SkDELETE(referenceBitmap); |
| 327 | return false; |
| 328 | } |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 329 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 330 | for (int y = 0; success && y < bitmap->height(); y++) { |
| 331 | for (int x = 0; success && x < bitmap->width(); x++) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 332 | int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y), |
| 333 | *bitmap->getAddr32(x, y)); |
| 334 | SkASSERT(diff >= 0 && diff <= 255); |
| 335 | diffs[diff]++; |
skia.committer@gmail.com | 4d28d98 | 2013-01-17 07:06:06 +0000 | [diff] [blame] | 336 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 337 | if (diff > FLAGS_maxComponentDiff) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 338 | SkDebugf("Expected pixel at (%i %i) exceedds maximum " |
| 339 | "component diff of %i: 0x%x, actual 0x%x\n", |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 340 | x, y, FLAGS_maxComponentDiff, |
edisonn@google.com | 01754bf | 2013-01-11 16:08:07 +0000 | [diff] [blame] | 341 | *referenceBitmap->getAddr32(x, y), |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 342 | *bitmap->getAddr32(x, y)); |
| 343 | SkDELETE(bitmap); |
| 344 | SkDELETE(referenceBitmap); |
| 345 | return false; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | SkDELETE(referenceBitmap); |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 350 | |
| 351 | for (int i = 1; i <= 255; ++i) { |
| 352 | if(diffs[i] > 0) { |
| 353 | SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]); |
| 354 | } |
| 355 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 356 | } |
| 357 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 358 | if (FLAGS_writeWholeImage) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 359 | sk_tools::force_all_opaque(*bitmap); |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 360 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 361 | SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 362 | SkString outputFilename(inputFilename); |
| 363 | sk_tools::replace_char(&outputFilename, '.', '_'); |
| 364 | outputFilename.append(".png"); |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 365 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 366 | if (NULL != jsonSummaryPtr) { |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 367 | sk_tools::ImageDigest imageDigest(*bitmap); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 368 | jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest); |
| 369 | if ((NULL != mismatchPath) && !mismatchPath->isEmpty() && |
epoger | 85b438d | 2014-08-21 23:21:32 -0700 | [diff] [blame] | 370 | !jsonSummaryPtr->getExpectation(inputFilename.c_str()).matches(imageDigest)) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 371 | success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL, |
| 372 | outputFilename); |
| 373 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 374 | } |
| 375 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 376 | if ((NULL != writePath) && !writePath->isEmpty()) { |
| 377 | success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | SkDELETE(bitmap); |
| 381 | |
| 382 | return success; |
| 383 | } |
| 384 | |
| 385 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 386 | static int process_input(const char* input, const SkString* writePath, |
| 387 | const SkString* mismatchPath, sk_tools::PictureRenderer& renderer, |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 388 | sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 389 | SkOSFile::Iter iter(input, "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 390 | SkString inputFilename; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 391 | int failures = 0; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 392 | SkDebugf("process_input, %s\n", input); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 393 | if (iter.next(&inputFilename)) { |
| 394 | do { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 395 | SkString inputPath = SkOSPath::Join(input, inputFilename.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 396 | if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 397 | ++failures; |
| 398 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 399 | } while(iter.next(&inputFilename)); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 400 | } else if (SkStrEndsWith(input, ".skp")) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 401 | SkString inputPath(input); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 402 | if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 403 | ++failures; |
| 404 | } |
| 405 | } else { |
| 406 | SkString warning; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 407 | warning.printf("Warning: skipping %s\n", input); |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 408 | SkDebugf(warning.c_str()); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 409 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 410 | return failures; |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 411 | } |
| 412 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 413 | int tool_main(int argc, char** argv); |
| 414 | int tool_main(int argc, char** argv) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 415 | SkCommandLineFlags::SetUsage("Render .skp files."); |
| 416 | SkCommandLineFlags::Parse(argc, argv); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 417 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 418 | if (FLAGS_readPath.isEmpty()) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 419 | SkDebugf(".skp files or directories are required.\n"); |
| 420 | exit(-1); |
| 421 | } |
| 422 | |
| 423 | if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) { |
| 424 | SkDebugf("--maxComponentDiff must be between 0 and 256\n"); |
| 425 | exit(-1); |
| 426 | } |
| 427 | |
| 428 | if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) { |
| 429 | SkDebugf("--maxComponentDiff requires --validate\n"); |
| 430 | exit(-1); |
| 431 | } |
| 432 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 433 | if (FLAGS_writeEncodedImages) { |
| 434 | if (FLAGS_writePath.isEmpty()) { |
| 435 | SkDebugf("--writeEncodedImages requires --writePath\n"); |
| 436 | exit(-1); |
| 437 | } |
| 438 | if (FLAGS_deferImageDecoding) { |
| 439 | SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n"); |
| 440 | exit(-1); |
| 441 | } |
| 442 | } |
| 443 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 444 | SkString errorString; |
| 445 | SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString, |
| 446 | kRender_PictureTool)); |
| 447 | if (errorString.size() > 0) { |
| 448 | SkDebugf("%s\n", errorString.c_str()); |
| 449 | } |
| 450 | |
| 451 | if (renderer.get() == NULL) { |
| 452 | exit(-1); |
| 453 | } |
| 454 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 455 | SkAutoGraphics ag; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 456 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 457 | SkString writePath; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 458 | if (FLAGS_writePath.count() == 1) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 459 | writePath.set(FLAGS_writePath[0]); |
| 460 | } |
| 461 | SkString mismatchPath; |
| 462 | if (FLAGS_mismatchPath.count() == 1) { |
| 463 | mismatchPath.set(FLAGS_mismatchPath[0]); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 464 | } |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 465 | sk_tools::ImageResultsAndExpectations jsonSummary; |
| 466 | sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL; |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 467 | if (FLAGS_writeJsonSummaryPath.count() == 1) { |
| 468 | jsonSummaryPtr = &jsonSummary; |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 469 | if (FLAGS_readJsonSummaryPath.count() == 1) { |
| 470 | SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0])); |
| 471 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 472 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 473 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 474 | int failures = 0; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 475 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 476 | failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(), |
| 477 | jsonSummaryPtr); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 478 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 479 | if (failures != 0) { |
| 480 | SkDebugf("Failed to render %i pictures.\n", failures); |
| 481 | return 1; |
| 482 | } |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 483 | #if SK_SUPPORT_GPU |
| 484 | #if GR_CACHE_STATS |
| 485 | if (renderer->isUsingGpuDevice()) { |
| 486 | GrContext* ctx = renderer->getGrContext(); |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 487 | ctx->printCacheStats(); |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 488 | #ifdef SK_DEVELOPER |
| 489 | ctx->dumpFontCache(); |
| 490 | #endif |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 491 | } |
| 492 | #endif |
| 493 | #endif |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 494 | if (FLAGS_writeJsonSummaryPath.count() == 1) { |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 495 | // If there were any descriptions on the command line, insert them now. |
| 496 | for (int i=0; i<FLAGS_descriptions.count(); i++) { |
| 497 | SkTArray<SkString> tokens; |
| 498 | SkStrSplit(FLAGS_descriptions[i], "=", &tokens); |
| 499 | SkASSERT(tokens.count() == 2); |
| 500 | jsonSummary.addDescription(tokens[0].c_str(), tokens[1].c_str()); |
| 501 | } |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame^] | 502 | if (FLAGS_imageBaseGSUrl.count() == 1) { |
| 503 | jsonSummary.setImageBaseGSUrl(FLAGS_imageBaseGSUrl[0]); |
| 504 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 505 | jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]); |
| 506 | } |
caryclark@google.com | 868e1f6 | 2012-10-02 20:00:03 +0000 | [diff] [blame] | 507 | return 0; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 508 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 509 | |
| 510 | #if !defined SK_BUILD_FOR_IOS |
| 511 | int main(int argc, char * const argv[]) { |
| 512 | return tool_main(argc, (char**) argv); |
| 513 | } |
| 514 | #endif |