1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "CopyTilesRenderer.h"
#include "SkBitmap.h"
#include "SkBitmapFactory.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkFlags.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkMath.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTArray.h"
#include "PictureRenderer.h"
#include "PictureRenderingFlags.h"
#include "picture_utils.h"
// Flags used by this file, alphabetically:
DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
DECLARE_bool(deferImageDecoding);
DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
"by more than this amount are considered errors, though all diffs are reported. "
"Requires --validate.");
DECLARE_string(r);
DEFINE_string(w, "", "Directory to write the rendered images.");
DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
"file, instead of an image for each tile.");
DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
"the picture rendered in simple mode.");
static void make_output_filepath(SkString* path, const SkString& dir,
const SkString& name) {
sk_tools::make_filepath(path, dir, name);
// Remove ".skp"
path->remove(path->size() - 4, 4);
}
#include "SkData.h"
#include "SkLruImageCache.h"
static SkLruImageCache gLruImageCache(1024*1024);
#ifdef SK_BUILD_FOR_ANDROID
#include "SkAshmemImageCache.h"
#include "SkImage.h"
static SkImageCache* cache_selector(const SkImage::Info& info) {
if (info.fWidth * info.fHeight > 32 * 1024) {
return SkAshmemImageCache::GetAshmemImageCache();
}
return &gLruImageCache;
}
#endif
static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
void* copiedBuffer = sk_malloc_throw(size);
memcpy(copiedBuffer, buffer, size);
SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
#ifdef SK_BUILD_FOR_ANDROID
factory.setCacheSelector(&cache_selector);
#else
factory.setImageCache(&gLruImageCache);
#endif
return factory.installPixelRef(data, bitmap);
}
static bool render_picture(const SkString& inputPath, const SkString* outputDir,
sk_tools::PictureRenderer& renderer,
SkBitmap** out) {
SkString inputFilename;
sk_tools::get_basename(&inputFilename, inputPath);
SkFILEStream inputStream;
inputStream.setPath(inputPath.c_str());
if (!inputStream.isValid()) {
SkDebugf("Could not open file %s\n", inputPath.c_str());
return false;
}
bool success = false;
SkPicture* picture;
if (FLAGS_deferImageDecoding) {
picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
} else {
picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
}
if (!success) {
SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
return false;
}
for (int i = 0; i < FLAGS_clone; ++i) {
SkPicture* clone = picture->clone();
SkDELETE(picture);
picture = clone;
}
SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
inputPath.c_str());
renderer.init(picture);
renderer.setup();
SkString* outputPath = NULL;
if (NULL != outputDir && outputDir->size() > 0) {
outputPath = SkNEW(SkString);
make_output_filepath(outputPath, *outputDir, inputFilename);
}
success = renderer.render(outputPath, out);
if (outputPath) {
if (!success) {
SkDebugf("Could not write to file %s\n", outputPath->c_str());
}
SkDELETE(outputPath);
}
renderer.end();
SkDELETE(picture);
return success;
}
static inline int getByte(uint32_t value, int index) {
SkASSERT(0 <= index && index < 4);
return (value >> (index * 8)) & 0xFF;
}
static int MaxByteDiff(uint32_t v1, uint32_t v2) {
return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
}
static bool render_picture(const SkString& inputPath, const SkString* outputDir,
sk_tools::PictureRenderer& renderer) {
int diffs[256] = {0};
SkBitmap* bitmap = NULL;
bool success = render_picture(inputPath,
FLAGS_writeWholeImage ? NULL : outputDir,
renderer,
FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
SkDebugf("Failed to draw the picture.\n");
SkDELETE(bitmap);
return false;
}
if (FLAGS_validate) {
SkBitmap* referenceBitmap = NULL;
sk_tools::SimplePictureRenderer referenceRenderer;
success = render_picture(inputPath, NULL, referenceRenderer,
&referenceBitmap);
if (!success || !referenceBitmap) {
SkDebugf("Failed to draw the reference picture.\n");
SkDELETE(bitmap);
SkDELETE(referenceBitmap);
return false;
}
if (success && (bitmap->width() != referenceBitmap->width())) {
SkDebugf("Expected image width: %i, actual image width %i.\n",
referenceBitmap->width(), bitmap->width());
SkDELETE(bitmap);
SkDELETE(referenceBitmap);
return false;
}
if (success && (bitmap->height() != referenceBitmap->height())) {
SkDebugf("Expected image height: %i, actual image height %i",
referenceBitmap->height(), bitmap->height());
SkDELETE(bitmap);
SkDELETE(referenceBitmap);
return false;
}
for (int y = 0; success && y < bitmap->height(); y++) {
for (int x = 0; success && x < bitmap->width(); x++) {
int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
*bitmap->getAddr32(x, y));
SkASSERT(diff >= 0 && diff <= 255);
diffs[diff]++;
if (diff > FLAGS_maxComponentDiff) {
SkDebugf("Expected pixel at (%i %i) exceedds maximum "
"component diff of %i: 0x%x, actual 0x%x\n",
x, y, FLAGS_maxComponentDiff,
*referenceBitmap->getAddr32(x, y),
*bitmap->getAddr32(x, y));
SkDELETE(bitmap);
SkDELETE(referenceBitmap);
return false;
}
}
}
SkDELETE(referenceBitmap);
for (int i = 1; i <= 255; ++i) {
if(diffs[i] > 0) {
SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
}
}
}
if (FLAGS_writeWholeImage) {
sk_tools::force_all_opaque(*bitmap);
if (NULL != outputDir && FLAGS_writeWholeImage) {
SkString inputFilename;
sk_tools::get_basename(&inputFilename, inputPath);
SkString outputPath;
make_output_filepath(&outputPath, *outputDir, inputFilename);
outputPath.append(".png");
if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
SkImageEncoder::kPNG_Type, 100)) {
SkDebugf("Failed to draw the picture.\n");
success = false;
}
}
}
SkDELETE(bitmap);
return success;
}
static int process_input(const char* input, const SkString* outputDir,
sk_tools::PictureRenderer& renderer) {
SkOSFile::Iter iter(input, "skp");
SkString inputFilename;
int failures = 0;
SkDebugf("process_input, %s\n", input);
if (iter.next(&inputFilename)) {
do {
SkString inputPath;
SkString inputAsSkString(input);
sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
if (!render_picture(inputPath, outputDir, renderer)) {
++failures;
}
} while(iter.next(&inputFilename));
} else if (SkStrEndsWith(input, ".skp")) {
SkString inputPath(input);
if (!render_picture(inputPath, outputDir, renderer)) {
++failures;
}
} else {
SkString warning;
warning.printf("Warning: skipping %s\n", input);
SkDebugf(warning.c_str());
}
return failures;
}
int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) {
SkFlags::SetUsage("Render .skp files.");
SkFlags::ParseCommandLine(argc, argv);
if (FLAGS_r.isEmpty()) {
SkDebugf(".skp files or directories are required.\n");
exit(-1);
}
if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
SkDebugf("--maxComponentDiff must be between 0 and 256\n");
exit(-1);
}
if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
SkDebugf("--maxComponentDiff requires --validate\n");
exit(-1);
}
if (FLAGS_clone < 0) {
SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
exit(-1);
}
SkString errorString;
SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
kRender_PictureTool));
if (errorString.size() > 0) {
SkDebugf("%s\n", errorString.c_str());
}
if (renderer.get() == NULL) {
exit(-1);
}
SkAutoGraphics ag;
SkString outputDir;
if (FLAGS_w.count() == 1) {
outputDir.set(FLAGS_w[0]);
}
int failures = 0;
for (int i = 0; i < FLAGS_r.count(); i ++) {
failures += process_input(FLAGS_r[i], &outputDir, *renderer.get());
}
if (failures != 0) {
SkDebugf("Failed to render %i pictures.\n", failures);
return 1;
}
#if SK_SUPPORT_GPU
#if GR_CACHE_STATS
if (renderer->isUsingGpuDevice()) {
GrContext* ctx = renderer->getGrContext();
ctx->printCacheStats();
}
#endif
#endif
return 0;
}
#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
return tool_main(argc, (char**) argv);
}
#endif
|