• 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 "include/core/SkData.h"
9 #include "include/core/SkGraphics.h"
10 #include "include/core/SkPicture.h"
11 #include "include/core/SkStream.h"
12 #include "include/utils/SkLua.h"
13 #include "include/utils/SkLuaCanvas.h"
14 #include "src/core/SkOSFile.h"
15 #include "src/utils/SkOSPath.h"
16 #include "tools/flags/CommandLineFlags.h"
17 
18 #include <stdlib.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 static DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
34               "testIndex %% divisor == remainder.");
35 static DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
36 static DEFINE_string2(luaFile, l, "", "File containing lua script to run");
37 static DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
38 static DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
39 static DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
40 
load_picture(const char path[])41 static sk_sp<SkPicture> load_picture(const char path[]) {
42     std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
43     if (stream) {
44         return SkPicture::MakeFromStream(stream.get());
45     }
46     return nullptr;
47 }
48 
call_canvas(lua_State * L,SkLuaCanvas * canvas,const char pictureFile[],const char funcName[])49 static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
50                         const char pictureFile[], const char funcName[]) {
51     lua_getglobal(L, funcName);
52     if (!lua_isfunction(L, -1)) {
53         int t = lua_type(L, -1);
54         SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
55         lua_settop(L, -2);
56     } else {
57         canvas->pushThis();
58         lua_pushstring(L, pictureFile);
59         if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
60             SkDebugf("lua err: %s\n", lua_tostring(L, -1));
61         }
62     }
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66     CommandLineFlags::SetUsage("apply lua script to .skp files.");
67     CommandLineFlags::Parse(argc, argv);
68 
69     if (FLAGS_skpPath.isEmpty()) {
70         SkDebugf(".skp files or directories are required.\n");
71         exit(-1);
72     }
73     if (FLAGS_luaFile.isEmpty()) {
74         SkDebugf("missing luaFile(s)\n");
75         exit(-1);
76     }
77 
78     const char* summary = gSummarizeFunc;
79     if (!FLAGS_tailFunc.isEmpty()) {
80         summary = FLAGS_tailFunc[0];
81     }
82 
83     SkAutoGraphics ag;
84     SkLua L(summary);
85 
86     for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
87         sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_luaFile[i]));
88         if (!data) {
89             data = SkData::MakeEmpty();
90         }
91         if (!FLAGS_quiet) {
92             SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
93         }
94         if (!L.runCode(data->data(), data->size())) {
95             SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
96             exit(-1);
97         }
98     }
99 
100     if (!FLAGS_headCode.isEmpty()) {
101         L.runCode(FLAGS_headCode[0]);
102     }
103 
104     int moduloRemainder = -1;
105     int moduloDivisor = -1;
106     SkString moduloStr;
107 
108     if (FLAGS_modulo.count() == 2) {
109         moduloRemainder = atoi(FLAGS_modulo[0]);
110         moduloDivisor = atoi(FLAGS_modulo[1]);
111         if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
112             SkDebugf("invalid modulo values.\n");
113             return -1;
114         }
115     }
116 
117     for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
118         SkTArray<SkString> paths;
119         if (sk_isdir(FLAGS_skpPath[i])) {
120             // Add all .skp in this directory.
121             const SkString directory(FLAGS_skpPath[i]);
122             SkString filename;
123             SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
124             while(iter.next(&filename)) {
125                 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
126             }
127         } else {
128             // Add this as an .skp itself.
129             paths.push_back() = FLAGS_skpPath[i];
130         }
131 
132         for (int i = 0; i < paths.count(); i++) {
133             if (moduloRemainder >= 0) {
134                 if ((i % moduloDivisor) != moduloRemainder) {
135                     continue;
136                 }
137                 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
138             }
139             const char* path = paths[i].c_str();
140             if (!FLAGS_quiet) {
141                 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
142             }
143 
144             auto pic(load_picture(path));
145             if (pic.get()) {
146                 std::unique_ptr<SkLuaCanvas> canvas(
147                                     new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
148                                                     SkScalarCeilToInt(pic->cullRect().height()),
149                                                     L.get(), gAccumulateFunc));
150 
151                 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
152                 canvas->drawPicture(pic);
153                 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
154 
155             } else {
156                 SkDebugf("failed to load\n");
157             }
158         }
159     }
160     return 0;
161 }
162