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