blob: 5d64e74bb07de4922ef856da53906e3f3b5651e7 [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 "
38 "the picture rendered in simple mode.");
junov@chromium.org777442d2012-06-12 14:56:36 +000039
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000040static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000041 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000042 sk_tools::make_filepath(path, dir, name);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000043 // Remove ".skp"
44 path->remove(path->size() - 4, 4);
junov@chromium.org777442d2012-06-12 14:56:36 +000045}
46
scroggo@google.comf8d7d272013-02-22 21:38:35 +000047#include "SkData.h"
48#include "SkLruImageCache.h"
49
50static SkLruImageCache gLruImageCache(1024*1024);
51
52#ifdef SK_BUILD_FOR_ANDROID
53#include "SkAshmemImageCache.h"
54#include "SkImage.h"
55
56static SkImageCache* cache_selector(const SkImage::Info& info) {
57 if (info.fWidth * info.fHeight > 32 * 1024) {
58 return SkAshmemImageCache::GetAshmemImageCache();
59 }
60 return &gLruImageCache;
61}
62
63#endif
64
65static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
66 void* copiedBuffer = sk_malloc_throw(size);
67 memcpy(copiedBuffer, buffer, size);
68 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
69 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
70#ifdef SK_BUILD_FOR_ANDROID
71 factory.setCacheSelector(&cache_selector);
72#else
73 factory.setImageCache(&gLruImageCache);
74#endif
75 return factory.installPixelRef(data, bitmap);
76}
77
borenet@google.com070d3542012-10-26 13:26:55 +000078static bool render_picture(const SkString& inputPath, const SkString* outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +000079 sk_tools::PictureRenderer& renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +000080 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000081 SkString inputFilename;
82 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000083
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000084 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000085 inputStream.setPath(inputPath.c_str());
86 if (!inputStream.isValid()) {
87 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +000088 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000089 }
90
borenet@google.com66bcbd12012-09-17 18:26:06 +000091 bool success = false;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000092 SkPicture* picture;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000093 if (FLAGS_deferImageDecoding) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +000094 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
95 } else {
96 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
97 }
borenet@google.com66bcbd12012-09-17 18:26:06 +000098 if (!success) {
99 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
100 return false;
101 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000102
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000103 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000104 SkPicture* clone = picture->clone();
105 SkDELETE(picture);
106 picture = clone;
107 }
108
109 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +0000110 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000111
edisonn@google.com84f548c2012-12-18 22:24:03 +0000112 renderer.init(picture);
scroggo@google.comb4773b42012-10-01 20:06:09 +0000113 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000114
borenet@google.com070d3542012-10-26 13:26:55 +0000115 SkString* outputPath = NULL;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000116 if (NULL != outputDir && outputDir->size() > 0) {
borenet@google.com070d3542012-10-26 13:26:55 +0000117 outputPath = SkNEW(SkString);
118 make_output_filepath(outputPath, *outputDir, inputFilename);
119 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000120
121 success = renderer.render(outputPath, out);
borenet@google.com070d3542012-10-26 13:26:55 +0000122 if (outputPath) {
123 if (!success) {
124 SkDebugf("Could not write to file %s\n", outputPath->c_str());
125 }
126 SkDELETE(outputPath);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000127 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000128
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000129 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000130
131 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000132 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000133}
134
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000135static inline int getByte(uint32_t value, int index) {
136 SkASSERT(0 <= index && index < 4);
137 return (value >> (index * 8)) & 0xFF;
138}
139
140static int MaxByteDiff(uint32_t v1, uint32_t v2) {
141 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
142 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
143}
144
edisonn@google.com84f548c2012-12-18 22:24:03 +0000145static bool render_picture(const SkString& inputPath, const SkString* outputDir,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000146 sk_tools::PictureRenderer& renderer) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000147 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000148 SkBitmap* bitmap = NULL;
149 bool success = render_picture(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000150 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000151 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000152 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000153
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000154 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000155 SkDebugf("Failed to draw the picture.\n");
156 SkDELETE(bitmap);
157 return false;
158 }
159
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000160 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000161 SkBitmap* referenceBitmap = NULL;
162 sk_tools::SimplePictureRenderer referenceRenderer;
163 success = render_picture(inputPath, NULL, referenceRenderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000164 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000165
166 if (!success || !referenceBitmap) {
167 SkDebugf("Failed to draw the reference picture.\n");
168 SkDELETE(bitmap);
169 SkDELETE(referenceBitmap);
170 return false;
171 }
172
173 if (success && (bitmap->width() != referenceBitmap->width())) {
174 SkDebugf("Expected image width: %i, actual image width %i.\n",
175 referenceBitmap->width(), bitmap->width());
176 SkDELETE(bitmap);
177 SkDELETE(referenceBitmap);
178 return false;
179 }
180 if (success && (bitmap->height() != referenceBitmap->height())) {
181 SkDebugf("Expected image height: %i, actual image height %i",
182 referenceBitmap->height(), bitmap->height());
183 SkDELETE(bitmap);
184 SkDELETE(referenceBitmap);
185 return false;
186 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000187
edisonn@google.com84f548c2012-12-18 22:24:03 +0000188 for (int y = 0; success && y < bitmap->height(); y++) {
189 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000190 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
191 *bitmap->getAddr32(x, y));
192 SkASSERT(diff >= 0 && diff <= 255);
193 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000194
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000195 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000196 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
197 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000198 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000199 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000200 *bitmap->getAddr32(x, y));
201 SkDELETE(bitmap);
202 SkDELETE(referenceBitmap);
203 return false;
204 }
205 }
206 }
207 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000208
209 for (int i = 1; i <= 255; ++i) {
210 if(diffs[i] > 0) {
211 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
212 }
213 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000214 }
215
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000216 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000217 sk_tools::force_all_opaque(*bitmap);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000218 if (NULL != outputDir && FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000219 SkString inputFilename;
220 sk_tools::get_basename(&inputFilename, inputPath);
221 SkString outputPath;
222 make_output_filepath(&outputPath, *outputDir, inputFilename);
223 outputPath.append(".png");
224 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
225 SkImageEncoder::kPNG_Type, 100)) {
226 SkDebugf("Failed to draw the picture.\n");
227 success = false;
228 }
229 }
230 }
231 SkDELETE(bitmap);
232
233 return success;
234}
235
236
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000237static int process_input(const char* input, const SkString* outputDir,
238 sk_tools::PictureRenderer& renderer) {
239 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000240 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000241 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000242 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000243 if (iter.next(&inputFilename)) {
244 do {
245 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000246 SkString inputAsSkString(input);
247 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
248 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000249 ++failures;
250 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000251 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000252 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000253 SkString inputPath(input);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000254 if (!render_picture(inputPath, outputDir, renderer)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000255 ++failures;
256 }
257 } else {
258 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000259 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000260 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000261 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000262 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000263}
264
caryclark@google.com5987f582012-10-02 18:33:14 +0000265int tool_main(int argc, char** argv);
266int tool_main(int argc, char** argv) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000267 SkFlags::SetUsage("Render .skp files.");
268 SkFlags::ParseCommandLine(argc, argv);
269
270 if (FLAGS_r.isEmpty()) {
271 SkDebugf(".skp files or directories are required.\n");
272 exit(-1);
273 }
274
275 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
276 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
277 exit(-1);
278 }
279
280 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
281 SkDebugf("--maxComponentDiff requires --validate\n");
282 exit(-1);
283 }
284
285 if (FLAGS_clone < 0) {
286 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
287 exit(-1);
288 }
289
290 SkString errorString;
291 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
292 kRender_PictureTool));
293 if (errorString.size() > 0) {
294 SkDebugf("%s\n", errorString.c_str());
295 }
296
297 if (renderer.get() == NULL) {
298 exit(-1);
299 }
300
borenet@google.com66bcbd12012-09-17 18:26:06 +0000301 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000302
303 SkString outputDir;
304 if (FLAGS_w.count() == 1) {
305 outputDir.set(FLAGS_w[0]);
306 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000307
borenet@google.com66bcbd12012-09-17 18:26:06 +0000308 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000309 for (int i = 0; i < FLAGS_r.count(); i ++) {
310 failures += process_input(FLAGS_r[i], &outputDir, *renderer.get());
junov@chromium.org777442d2012-06-12 14:56:36 +0000311 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000312 if (failures != 0) {
313 SkDebugf("Failed to render %i pictures.\n", failures);
314 return 1;
315 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000316#if SK_SUPPORT_GPU
317#if GR_CACHE_STATS
318 if (renderer->isUsingGpuDevice()) {
319 GrContext* ctx = renderer->getGrContext();
320
321 ctx->printCacheStats();
322 }
323#endif
324#endif
caryclark@google.com868e1f62012-10-02 20:00:03 +0000325 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000326}
caryclark@google.com5987f582012-10-02 18:33:14 +0000327
328#if !defined SK_BUILD_FOR_IOS
329int main(int argc, char * const argv[]) {
330 return tool_main(argc, (char**) argv);
331}
332#endif