2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "LazyDecodeBitmap.h"
10 #include "SkLuaCanvas.h"
11 #include "SkPicture.h"
12 #include "SkCommandLineFlags.h"
13 #include "SkGraphics.h"
16 #include "picture_utils.h"
18 #include "SkImageDecoder.h"
26 static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
27 static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
28 static const char gAccumulateFunc[] = "sk_scrape_accumulate";
29 static const char gSummarizeFunc[] = "sk_scrape_summarize";
31 // Example usage for the modulo flag:
32 // for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done
33 DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
34 "testIndex %% divisor == remainder.");
35 DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
36 DEFINE_string2(luaFile, l, "", "File containing lua script to run");
37 DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
38 DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
39 DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
41 static SkPicture* load_picture(const char path[]) {
42 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
43 SkPicture* pic = NULL;
45 pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);
50 static SkData* read_into_data(const char file[]) {
51 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
53 return SkData::NewEmpty();
55 size_t len = stream->getLength();
56 void* buffer = sk_malloc_throw(len);
57 stream->read(buffer, len);
58 return SkData::NewFromMalloc(buffer, len);
61 static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
62 const char pictureFile[], const char funcName[]) {
63 lua_getglobal(L, funcName);
64 if (!lua_isfunction(L, -1)) {
65 int t = lua_type(L, -1);
66 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
70 lua_pushstring(L, pictureFile);
71 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
72 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
77 int tool_main(int argc, char** argv);
78 int tool_main(int argc, char** argv) {
79 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
80 SkCommandLineFlags::Parse(argc, argv);
82 if (FLAGS_skpPath.isEmpty()) {
83 SkDebugf(".skp files or directories are required.\n");
86 if (FLAGS_luaFile.isEmpty()) {
87 SkDebugf("missing luaFile(s)\n");
91 const char* summary = gSummarizeFunc;
92 if (!FLAGS_tailFunc.isEmpty()) {
93 summary = FLAGS_tailFunc[0];
99 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
100 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
102 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
104 if (!L.runCode(data->data(), data->size())) {
105 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
110 if (!FLAGS_headCode.isEmpty()) {
111 L.runCode(FLAGS_headCode[0]);
114 int moduloRemainder = -1;
115 int moduloDivisor = -1;
118 if (FLAGS_modulo.count() == 2) {
119 moduloRemainder = atoi(FLAGS_modulo[0]);
120 moduloDivisor = atoi(FLAGS_modulo[1]);
121 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
122 SkDebugf("invalid modulo values.\n");
127 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
128 SkTArray<SkString> paths;
129 if (sk_isdir(FLAGS_skpPath[i])) {
130 // Add all .skp in this directory.
131 const SkString directory(FLAGS_skpPath[i]);
133 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
134 while(iter.next(&filename)) {
135 paths.push_back() = SkOSPath::SkPathJoin(directory.c_str(), filename.c_str());
138 // Add this as an .skp itself.
139 paths.push_back() = FLAGS_skpPath[i];
142 for (int i = 0; i < paths.count(); i++) {
143 if (moduloRemainder >= 0) {
144 if ((i % moduloDivisor) != moduloRemainder) {
147 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
149 const char* path = paths[i].c_str();
151 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
154 SkAutoTUnref<SkPicture> pic(load_picture(path));
156 SkAutoTUnref<SkLuaCanvas> canvas(
157 new SkLuaCanvas(pic->width(), pic->height(),
158 L.get(), gAccumulateFunc));
160 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
161 canvas->drawPicture(pic);
162 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
165 SkDebugf("failed to load\n");
172 #if !defined SK_BUILD_FOR_IOS
173 int main(int argc, char * const argv[]) {
174 return tool_main(argc, (char**) argv);