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