• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 
8 #include "LazyDecodeBitmap.h"
9 #include "SkLua.h"
10 #include "SkLuaCanvas.h"
11 #include "SkPicture.h"
12 #include "SkCommandLineFlags.h"
13 #include "SkGraphics.h"
14 #include "SkStream.h"
15 #include "SkData.h"
16 #include "picture_utils.h"
17 #include "SkOSFile.h"
18 #include "SkImageDecoder.h"
19 
20 extern "C" {
21     #include "lua.h"
22     #include "lualib.h"
23     #include "lauxlib.h"
24 }
25 
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";
30 
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");
40 
load_picture(const char path[])41 static SkPicture* load_picture(const char path[]) {
42     SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
43     SkPicture* pic = NULL;
44     if (stream.get()) {
45         pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);
46     }
47     return pic;
48 }
49 
call_canvas(lua_State * L,SkLuaCanvas * canvas,const char pictureFile[],const char funcName[])50 static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
51                         const char pictureFile[], const char funcName[]) {
52     lua_getglobal(L, funcName);
53     if (!lua_isfunction(L, -1)) {
54         int t = lua_type(L, -1);
55         SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
56         lua_settop(L, -2);
57     } else {
58         canvas->pushThis();
59         lua_pushstring(L, pictureFile);
60         if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
61             SkDebugf("lua err: %s\n", lua_tostring(L, -1));
62         }
63     }
64 }
65 
66 int tool_main(int argc, char** argv);
tool_main(int argc,char ** argv)67 int tool_main(int argc, char** argv) {
68     SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
69     SkCommandLineFlags::Parse(argc, argv);
70 
71     if (FLAGS_skpPath.isEmpty()) {
72         SkDebugf(".skp files or directories are required.\n");
73         exit(-1);
74     }
75     if (FLAGS_luaFile.isEmpty()) {
76         SkDebugf("missing luaFile(s)\n");
77         exit(-1);
78     }
79 
80     const char* summary = gSummarizeFunc;
81     if (!FLAGS_tailFunc.isEmpty()) {
82         summary = FLAGS_tailFunc[0];
83     }
84 
85     SkAutoGraphics ag;
86     SkLua L(summary);
87 
88     for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
89         SkAutoDataUnref data(SkData::NewFromFileName(FLAGS_luaFile[i]));
90         if (NULL == data.get()) {
91             data.reset(SkData::NewEmpty());
92         }
93         if (!FLAGS_quiet) {
94             SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
95         }
96         if (!L.runCode(data->data(), data->size())) {
97             SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
98             exit(-1);
99         }
100     }
101 
102     if (!FLAGS_headCode.isEmpty()) {
103         L.runCode(FLAGS_headCode[0]);
104     }
105 
106     int moduloRemainder = -1;
107     int moduloDivisor = -1;
108     SkString moduloStr;
109 
110     if (FLAGS_modulo.count() == 2) {
111         moduloRemainder = atoi(FLAGS_modulo[0]);
112         moduloDivisor = atoi(FLAGS_modulo[1]);
113         if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
114             SkDebugf("invalid modulo values.\n");
115             return -1;
116         }
117     }
118 
119     for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
120         SkTArray<SkString> paths;
121         if (sk_isdir(FLAGS_skpPath[i])) {
122             // Add all .skp in this directory.
123             const SkString directory(FLAGS_skpPath[i]);
124             SkString filename;
125             SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
126             while(iter.next(&filename)) {
127                 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
128             }
129         } else {
130             // Add this as an .skp itself.
131             paths.push_back() = FLAGS_skpPath[i];
132         }
133 
134         for (int i = 0; i < paths.count(); i++) {
135             if (moduloRemainder >= 0) {
136                 if ((i % moduloDivisor) != moduloRemainder) {
137                     continue;
138                 }
139                 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
140             }
141             const char* path = paths[i].c_str();
142             if (!FLAGS_quiet) {
143                 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
144             }
145 
146             SkAutoTUnref<SkPicture> pic(load_picture(path));
147             if (pic.get()) {
148                 SkAutoTUnref<SkLuaCanvas> canvas(
149                                     new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
150                                                     SkScalarCeilToInt(pic->cullRect().height()),
151                                                     L.get(), gAccumulateFunc));
152 
153                 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
154                 canvas->drawPicture(pic);
155                 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
156 
157             } else {
158                 SkDebugf("failed to load\n");
159             }
160         }
161     }
162     return 0;
163 }
164 
165 #if !defined SK_BUILD_FOR_IOS
main(int argc,char * const argv[])166 int main(int argc, char * const argv[]) {
167     return tool_main(argc, (char**) argv);
168 }
169 #endif
170