blob: b289d39fc7d65208868420515bb7319eac426c31 [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"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000010#include "SkBitmapFactory.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000011#include "SkCanvas.h"
keyar@chromium.org472b3792012-07-20 22:34:27 +000012#include "SkDevice.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000013#include "SkFlags.h"
borenet@google.com10ef79e2012-09-10 17:19:06 +000014#include "SkGraphics.h"
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000015#include "SkImageDecoder.h"
edisonn@google.com84f548c2012-12-18 22:24:03 +000016#include "SkImageEncoder.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000017#include "SkMath.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000018#include "SkOSFile.h"
19#include "SkPicture.h"
20#include "SkStream.h"
21#include "SkString.h"
senorblanco@chromium.org3cbbb542012-07-13 18:55:53 +000022#include "SkTArray.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000023#include "PictureRenderer.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000024#include "PictureRenderingFlags.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000025#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000026
scroggo@google.com161e1ba2013-03-04 16:41:06 +000027// Flags used by this file, alphabetically:
28DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
29DECLARE_bool(deferImageDecoding);
30DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
31 "by more than this amount are considered errors, though all diffs are reported. "
32 "Requires --validate.");
33DECLARE_string(r);
34DEFINE_string(w, "", "Directory to write the rendered images.");
35DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
36 "file, instead of an image for each tile.");
37DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000038 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
39 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000040
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000041static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000042 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000043 sk_tools::make_filepath(path, dir, name);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000044 // Remove ".skp"
45 path->remove(path->size() - 4, 4);
junov@chromium.org777442d2012-06-12 14:56:36 +000046}
47
scroggo@google.comf8d7d272013-02-22 21:38:35 +000048#include "SkData.h"
49#include "SkLruImageCache.h"
50
51static SkLruImageCache gLruImageCache(1024*1024);
52
53#ifdef SK_BUILD_FOR_ANDROID
54#include "SkAshmemImageCache.h"
55#include "SkImage.h"
56
57static SkImageCache* cache_selector(const SkImage::Info& info) {
58 if (info.fWidth * info.fHeight > 32 * 1024) {
59 return SkAshmemImageCache::GetAshmemImageCache();
60 }
61 return &gLruImageCache;
62}
63
64#endif
65
66static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
67 void* copiedBuffer = sk_malloc_throw(size);
68 memcpy(copiedBuffer, buffer, size);
69 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
70 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
71#ifdef SK_BUILD_FOR_ANDROID
72 factory.setCacheSelector(&cache_selector);
73#else
74 factory.setImageCache(&gLruImageCache);
75#endif
76 return factory.installPixelRef(data, bitmap);
77}
78
borenet@google.com070d3542012-10-26 13:26:55 +000079static bool render_picture(const SkString& inputPath, const SkString* outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +000080 sk_tools::PictureRenderer& renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +000081 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000082 SkString inputFilename;
83 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000084
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000085 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000086 inputStream.setPath(inputPath.c_str());
87 if (!inputStream.isValid()) {
88 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +000089 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000090 }
91
borenet@google.com66bcbd12012-09-17 18:26:06 +000092 bool success = false;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000093 SkPicture* picture;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000094 if (FLAGS_deferImageDecoding) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +000095 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
96 } else {
97 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
98 }
borenet@google.com66bcbd12012-09-17 18:26:06 +000099 if (!success) {
100 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
101 return false;
102 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000103
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000104 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000105 SkPicture* clone = picture->clone();
106 SkDELETE(picture);
107 picture = clone;
108 }
109
110 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +0000111 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000112
edisonn@google.com84f548c2012-12-18 22:24:03 +0000113 renderer.init(picture);
scroggo@google.comb4773b42012-10-01 20:06:09 +0000114 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000115
borenet@google.com070d3542012-10-26 13:26:55 +0000116 SkString* outputPath = NULL;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000117 if (NULL != outputDir && outputDir->size() > 0) {
borenet@google.com070d3542012-10-26 13:26:55 +0000118 outputPath = SkNEW(SkString);
119 make_output_filepath(outputPath, *outputDir, inputFilename);
120 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000121
122 success = renderer.render(outputPath, out);
borenet@google.com070d3542012-10-26 13:26:55 +0000123 if (outputPath) {
124 if (!success) {
125 SkDebugf("Could not write to file %s\n", outputPath->c_str());
126 }
127 SkDELETE(outputPath);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000128 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000129
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000130 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000131
132 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000133 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000134}
135
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000136static inline int getByte(uint32_t value, int index) {
137 SkASSERT(0 <= index && index < 4);
138 return (value >> (index * 8)) & 0xFF;
139}
140
141static int MaxByteDiff(uint32_t v1, uint32_t v2) {
142 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
143 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
144}
145
junov@chromium.orge286e842013-03-13 17:27:16 +0000146namespace {
147class AutoRestoreBbhType {
148public:
149 AutoRestoreBbhType() {
150 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000151 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000152 }
153
154 void set(sk_tools::PictureRenderer* renderer,
155 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
156 fRenderer = renderer;
157 fSavedBbhType = renderer->getBBoxHierarchyType();
158 renderer->setBBoxHierarchyType(bbhType);
159 }
160
161 ~AutoRestoreBbhType() {
162 if (NULL != fRenderer) {
163 fRenderer->setBBoxHierarchyType(fSavedBbhType);
164 }
165 }
166
167private:
168 sk_tools::PictureRenderer* fRenderer;
169 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
170};
171}
172
edisonn@google.com84f548c2012-12-18 22:24:03 +0000173static bool render_picture(const SkString& inputPath, const SkString* outputDir,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000174 sk_tools::PictureRenderer& renderer) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000175 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000176 SkBitmap* bitmap = NULL;
177 bool success = render_picture(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000178 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000179 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000180 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000181
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000182 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000183 SkDebugf("Failed to draw the picture.\n");
184 SkDELETE(bitmap);
185 return false;
186 }
187
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000188 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000189 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000190 sk_tools::PictureRenderer* referenceRenderer;
191 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000192 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000193 AutoRestoreBbhType arbbh;
194 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
195 renderer.getBBoxHierarchyType()) {
196 referenceRenderer = &renderer;
197 referenceRenderer->ref(); // to match auto unref below
198 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
199 } else {
200 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
201 }
202 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
203
204 success = render_picture(inputPath, NULL, *referenceRenderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000205 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000206
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000207 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000208 SkDebugf("Failed to draw the reference picture.\n");
209 SkDELETE(bitmap);
210 SkDELETE(referenceBitmap);
211 return false;
212 }
213
214 if (success && (bitmap->width() != referenceBitmap->width())) {
215 SkDebugf("Expected image width: %i, actual image width %i.\n",
216 referenceBitmap->width(), bitmap->width());
217 SkDELETE(bitmap);
218 SkDELETE(referenceBitmap);
219 return false;
220 }
221 if (success && (bitmap->height() != referenceBitmap->height())) {
222 SkDebugf("Expected image height: %i, actual image height %i",
223 referenceBitmap->height(), bitmap->height());
224 SkDELETE(bitmap);
225 SkDELETE(referenceBitmap);
226 return false;
227 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000228
edisonn@google.com84f548c2012-12-18 22:24:03 +0000229 for (int y = 0; success && y < bitmap->height(); y++) {
230 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000231 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
232 *bitmap->getAddr32(x, y));
233 SkASSERT(diff >= 0 && diff <= 255);
234 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000235
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000236 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000237 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
238 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000239 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000240 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000241 *bitmap->getAddr32(x, y));
242 SkDELETE(bitmap);
243 SkDELETE(referenceBitmap);
244 return false;
245 }
246 }
247 }
248 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000249
250 for (int i = 1; i <= 255; ++i) {
251 if(diffs[i] > 0) {
252 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
253 }
254 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000255 }
256
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000257 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000258 sk_tools::force_all_opaque(*bitmap);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000259 if (NULL != outputDir && FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000260 SkString inputFilename;
261 sk_tools::get_basename(&inputFilename, inputPath);
262 SkString outputPath;
263 make_output_filepath(&outputPath, *outputDir, inputFilename);
264 outputPath.append(".png");
265 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
266 SkImageEncoder::kPNG_Type, 100)) {
267 SkDebugf("Failed to draw the picture.\n");
268 success = false;
269 }
270 }
271 }
272 SkDELETE(bitmap);
273
274 return success;
275}
276
277
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000278static int process_input(const char* input, const SkString* outputDir,
279 sk_tools::PictureRenderer& renderer) {
280 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000281 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000282 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000283 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000284 if (iter.next(&inputFilename)) {
285 do {
286 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000287 SkString inputAsSkString(input);
288 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
289 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000290 ++failures;
291 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000292 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000293 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000294 SkString inputPath(input);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000295 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000296 ++failures;
297 }
298 } else {
299 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000300 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000301 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000302 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000303 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000304}
305
caryclark@google.com5987f582012-10-02 18:33:14 +0000306int tool_main(int argc, char** argv);
307int tool_main(int argc, char** argv) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000308 SkFlags::SetUsage("Render .skp files.");
309 SkFlags::ParseCommandLine(argc, argv);
310
311 if (FLAGS_r.isEmpty()) {
312 SkDebugf(".skp files or directories are required.\n");
313 exit(-1);
314 }
315
316 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
317 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
318 exit(-1);
319 }
320
321 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
322 SkDebugf("--maxComponentDiff requires --validate\n");
323 exit(-1);
324 }
325
326 if (FLAGS_clone < 0) {
327 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
328 exit(-1);
329 }
330
331 SkString errorString;
332 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
333 kRender_PictureTool));
334 if (errorString.size() > 0) {
335 SkDebugf("%s\n", errorString.c_str());
336 }
337
338 if (renderer.get() == NULL) {
339 exit(-1);
340 }
341
borenet@google.com66bcbd12012-09-17 18:26:06 +0000342 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000343
344 SkString outputDir;
345 if (FLAGS_w.count() == 1) {
346 outputDir.set(FLAGS_w[0]);
347 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000348
borenet@google.com66bcbd12012-09-17 18:26:06 +0000349 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000350 for (int i = 0; i < FLAGS_r.count(); i ++) {
351 failures += process_input(FLAGS_r[i], &outputDir, *renderer.get());
junov@chromium.org777442d2012-06-12 14:56:36 +0000352 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000353 if (failures != 0) {
354 SkDebugf("Failed to render %i pictures.\n", failures);
355 return 1;
356 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000357#if SK_SUPPORT_GPU
358#if GR_CACHE_STATS
359 if (renderer->isUsingGpuDevice()) {
360 GrContext* ctx = renderer->getGrContext();
361
362 ctx->printCacheStats();
363 }
364#endif
365#endif
caryclark@google.com868e1f62012-10-02 20:00:03 +0000366 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000367}
caryclark@google.com5987f582012-10-02 18:33:14 +0000368
369#if !defined SK_BUILD_FOR_IOS
370int main(int argc, char * const argv[]) {
371 return tool_main(argc, (char**) argv);
372}
373#endif