blob: 736e8264d52811fc84b0ff47ee759a35bc6779a9 [file] [log] [blame]
junov@chromium.org777442d2012-06-12 14:56:36 +00001/*
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
scroggo@google.com4a26d9d2012-11-07 18:01:46 +00008#include "CopyTilesRenderer.h"
junov@chromium.org777442d2012-06-12 14:56:36 +00009#include "SkBitmap.h"
keyar@chromium.org472b3792012-07-20 22:34:27 +000010#include "SkDevice.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000011#include "SkCommandLineFlags.h"
borenet@google.com10ef79e2012-09-10 17:19:06 +000012#include "SkGraphics.h"
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000013#include "SkImageDecoder.h"
edisonn@google.com84f548c2012-12-18 22:24:03 +000014#include "SkImageEncoder.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000015#include "SkMath.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000016#include "SkOSFile.h"
17#include "SkPicture.h"
18#include "SkStream.h"
19#include "SkString.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000020#include "PictureRenderer.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000021#include "PictureRenderingFlags.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000022#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000023
scroggo@google.com161e1ba2013-03-04 16:41:06 +000024// Flags used by this file, alphabetically:
25DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
26DECLARE_bool(deferImageDecoding);
27DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
28 "by more than this amount are considered errors, though all diffs are reported. "
29 "Requires --validate.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000030DECLARE_string(readPath);
31DEFINE_string2(writePath, w, "", "Directory to write the rendered images.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000032DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
33 "file, instead of an image for each tile.");
34DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000035 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
36 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000037
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000038static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000039 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000040 sk_tools::make_filepath(path, dir, name);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000041 // Remove ".skp"
42 path->remove(path->size() - 4, 4);
junov@chromium.org777442d2012-06-12 14:56:36 +000043}
44
scroggo@google.combb281f72013-03-18 21:37:39 +000045// Defined in PictureRenderingFlags.cpp
46extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000047
borenet@google.com070d3542012-10-26 13:26:55 +000048static bool render_picture(const SkString& inputPath, const SkString* outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +000049 sk_tools::PictureRenderer& renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +000050 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000051 SkString inputFilename;
52 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000053
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000054 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000055 inputStream.setPath(inputPath.c_str());
56 if (!inputStream.isValid()) {
57 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +000058 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000059 }
60
borenet@google.com66bcbd12012-09-17 18:26:06 +000061 bool success = false;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000062 SkPicture* picture;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000063 if (FLAGS_deferImageDecoding) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +000064 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
65 } else {
66 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
67 }
borenet@google.com66bcbd12012-09-17 18:26:06 +000068 if (!success) {
69 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
70 return false;
71 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000072
scroggo@google.com161e1ba2013-03-04 16:41:06 +000073 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +000074 SkPicture* clone = picture->clone();
75 SkDELETE(picture);
76 picture = clone;
77 }
78
79 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +000080 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +000081
edisonn@google.com84f548c2012-12-18 22:24:03 +000082 renderer.init(picture);
scroggo@google.comb4773b42012-10-01 20:06:09 +000083 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +000084
borenet@google.com070d3542012-10-26 13:26:55 +000085 SkString* outputPath = NULL;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000086 if (NULL != outputDir && outputDir->size() > 0) {
borenet@google.com070d3542012-10-26 13:26:55 +000087 outputPath = SkNEW(SkString);
88 make_output_filepath(outputPath, *outputDir, inputFilename);
89 }
edisonn@google.com84f548c2012-12-18 22:24:03 +000090
91 success = renderer.render(outputPath, out);
borenet@google.com070d3542012-10-26 13:26:55 +000092 if (outputPath) {
93 if (!success) {
94 SkDebugf("Could not write to file %s\n", outputPath->c_str());
95 }
96 SkDELETE(outputPath);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000097 }
scroggo@google.com9a412522012-09-07 15:21:18 +000098
keyar@chromium.org9d696c02012-08-07 17:11:33 +000099 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000100
101 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000102 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000103}
104
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000105static inline int getByte(uint32_t value, int index) {
106 SkASSERT(0 <= index && index < 4);
107 return (value >> (index * 8)) & 0xFF;
108}
109
110static int MaxByteDiff(uint32_t v1, uint32_t v2) {
111 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
112 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
113}
114
junov@chromium.orge286e842013-03-13 17:27:16 +0000115namespace {
116class AutoRestoreBbhType {
117public:
118 AutoRestoreBbhType() {
119 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000120 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000121 }
122
123 void set(sk_tools::PictureRenderer* renderer,
124 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
125 fRenderer = renderer;
126 fSavedBbhType = renderer->getBBoxHierarchyType();
127 renderer->setBBoxHierarchyType(bbhType);
128 }
129
130 ~AutoRestoreBbhType() {
131 if (NULL != fRenderer) {
132 fRenderer->setBBoxHierarchyType(fSavedBbhType);
133 }
134 }
135
136private:
137 sk_tools::PictureRenderer* fRenderer;
138 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
139};
140}
141
edisonn@google.com84f548c2012-12-18 22:24:03 +0000142static bool render_picture(const SkString& inputPath, const SkString* outputDir,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000143 sk_tools::PictureRenderer& renderer) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000144 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000145 SkBitmap* bitmap = NULL;
146 bool success = render_picture(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000147 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000148 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000149 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000150
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000151 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000152 SkDebugf("Failed to draw the picture.\n");
153 SkDELETE(bitmap);
154 return false;
155 }
156
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000157 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000158 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000159 sk_tools::PictureRenderer* referenceRenderer;
160 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000161 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000162 AutoRestoreBbhType arbbh;
163 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
164 renderer.getBBoxHierarchyType()) {
165 referenceRenderer = &renderer;
166 referenceRenderer->ref(); // to match auto unref below
167 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
168 } else {
169 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
170 }
171 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
172
173 success = render_picture(inputPath, NULL, *referenceRenderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000174 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000175
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000176 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000177 SkDebugf("Failed to draw the reference picture.\n");
178 SkDELETE(bitmap);
179 SkDELETE(referenceBitmap);
180 return false;
181 }
182
183 if (success && (bitmap->width() != referenceBitmap->width())) {
184 SkDebugf("Expected image width: %i, actual image width %i.\n",
185 referenceBitmap->width(), bitmap->width());
186 SkDELETE(bitmap);
187 SkDELETE(referenceBitmap);
188 return false;
189 }
190 if (success && (bitmap->height() != referenceBitmap->height())) {
191 SkDebugf("Expected image height: %i, actual image height %i",
192 referenceBitmap->height(), bitmap->height());
193 SkDELETE(bitmap);
194 SkDELETE(referenceBitmap);
195 return false;
196 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000197
edisonn@google.com84f548c2012-12-18 22:24:03 +0000198 for (int y = 0; success && y < bitmap->height(); y++) {
199 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000200 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
201 *bitmap->getAddr32(x, y));
202 SkASSERT(diff >= 0 && diff <= 255);
203 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000204
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000205 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000206 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
207 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000208 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000209 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000210 *bitmap->getAddr32(x, y));
211 SkDELETE(bitmap);
212 SkDELETE(referenceBitmap);
213 return false;
214 }
215 }
216 }
217 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000218
219 for (int i = 1; i <= 255; ++i) {
220 if(diffs[i] > 0) {
221 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
222 }
223 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000224 }
225
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000226 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000227 sk_tools::force_all_opaque(*bitmap);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000228 if (NULL != outputDir && FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000229 SkString inputFilename;
230 sk_tools::get_basename(&inputFilename, inputPath);
231 SkString outputPath;
232 make_output_filepath(&outputPath, *outputDir, inputFilename);
233 outputPath.append(".png");
234 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
235 SkImageEncoder::kPNG_Type, 100)) {
236 SkDebugf("Failed to draw the picture.\n");
237 success = false;
238 }
239 }
240 }
241 SkDELETE(bitmap);
242
243 return success;
244}
245
246
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000247static int process_input(const char* input, const SkString* outputDir,
248 sk_tools::PictureRenderer& renderer) {
249 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000250 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000251 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000252 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000253 if (iter.next(&inputFilename)) {
254 do {
255 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000256 SkString inputAsSkString(input);
257 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
258 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000259 ++failures;
260 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000261 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000262 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000263 SkString inputPath(input);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000264 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000265 ++failures;
266 }
267 } else {
268 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000269 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000270 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000271 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000272 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000273}
274
caryclark@google.com5987f582012-10-02 18:33:14 +0000275int tool_main(int argc, char** argv);
276int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000277 SkCommandLineFlags::SetUsage("Render .skp files.");
278 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000279
scroggo@google.com604e0c22013-04-09 21:25:46 +0000280 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000281 SkDebugf(".skp files or directories are required.\n");
282 exit(-1);
283 }
284
285 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
286 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
287 exit(-1);
288 }
289
290 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
291 SkDebugf("--maxComponentDiff requires --validate\n");
292 exit(-1);
293 }
294
295 if (FLAGS_clone < 0) {
296 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
297 exit(-1);
298 }
299
300 SkString errorString;
301 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
302 kRender_PictureTool));
303 if (errorString.size() > 0) {
304 SkDebugf("%s\n", errorString.c_str());
305 }
306
307 if (renderer.get() == NULL) {
308 exit(-1);
309 }
310
borenet@google.com66bcbd12012-09-17 18:26:06 +0000311 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000312
313 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000314 if (FLAGS_writePath.count() == 1) {
315 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000316 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000317
borenet@google.com66bcbd12012-09-17 18:26:06 +0000318 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000319 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
320 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get());
junov@chromium.org777442d2012-06-12 14:56:36 +0000321 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000322 if (failures != 0) {
323 SkDebugf("Failed to render %i pictures.\n", failures);
324 return 1;
325 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000326#if SK_SUPPORT_GPU
327#if GR_CACHE_STATS
328 if (renderer->isUsingGpuDevice()) {
329 GrContext* ctx = renderer->getGrContext();
330
331 ctx->printCacheStats();
332 }
333#endif
334#endif
caryclark@google.com868e1f62012-10-02 20:00:03 +0000335 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000336}
caryclark@google.com5987f582012-10-02 18:33:14 +0000337
338#if !defined SK_BUILD_FOR_IOS
339int main(int argc, char * const argv[]) {
340 return tool_main(argc, (char**) argv);
341}
342#endif