blob: 57b0dcce8ee5f75b575d9615da689e34d3d063e6 [file] [log] [blame]
reed@google.comdff7e112013-05-15 19:34:20 +00001/*
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
reed@google.com3597b732013-05-22 20:12:50 +00008#include "SkLua.h"
reed@google.comdff7e112013-05-15 19:34:20 +00009#include "SkLuaCanvas.h"
10#include "SkPicture.h"
11#include "SkCommandLineFlags.h"
12#include "SkGraphics.h"
13#include "SkStream.h"
14#include "SkData.h"
15#include "picture_utils.h"
16#include "SkOSFile.h"
17#include "SkImageDecoder.h"
18
bungeman60e0fee2015-08-26 05:15:46 -070019#include <stdlib.h>
20
reed@google.comdff7e112013-05-15 19:34:20 +000021extern "C" {
22 #include "lua.h"
23 #include "lualib.h"
24 #include "lauxlib.h"
25}
26
reed@google.com2d516772013-05-21 16:05:53 +000027static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
28static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
29static const char gAccumulateFunc[] = "sk_scrape_accumulate";
30static const char gSummarizeFunc[] = "sk_scrape_summarize";
31
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000032// Example usage for the modulo flag:
33// for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done
34DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
35 "testIndex %% divisor == remainder.");
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +000036DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
reed@google.comdff7e112013-05-15 19:34:20 +000037DEFINE_string2(luaFile, l, "", "File containing lua script to run");
reed@google.come2aad272013-05-31 19:46:02 +000038DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
39DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000040DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
reed@google.comdff7e112013-05-15 19:34:20 +000041
42static SkPicture* load_picture(const char path[]) {
scroggoa1193e42015-01-21 12:09:53 -080043 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070044 SkPicture* pic = nullptr;
reed@google.comdff7e112013-05-15 19:34:20 +000045 if (stream.get()) {
msarett8715d472016-02-17 10:02:29 -080046 pic = SkPicture::CreateFromStream(stream.get());
reed@google.comdff7e112013-05-15 19:34:20 +000047 }
48 return pic;
49}
50
reed@google.com2d516772013-05-21 16:05:53 +000051static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
52 const char pictureFile[], const char funcName[]) {
53 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000054 if (!lua_isfunction(L, -1)) {
55 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000056 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000057 lua_settop(L, -2);
58 } else {
59 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000060 lua_pushstring(L, pictureFile);
61 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000062 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
63 }
64 }
65}
66
reed@google.comdff7e112013-05-15 19:34:20 +000067int tool_main(int argc, char** argv);
68int tool_main(int argc, char** argv) {
69 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
70 SkCommandLineFlags::Parse(argc, argv);
71
72 if (FLAGS_skpPath.isEmpty()) {
73 SkDebugf(".skp files or directories are required.\n");
74 exit(-1);
75 }
76 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000077 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000078 exit(-1);
79 }
80
reed@google.come2aad272013-05-31 19:46:02 +000081 const char* summary = gSummarizeFunc;
82 if (!FLAGS_tailFunc.isEmpty()) {
83 summary = FLAGS_tailFunc[0];
84 }
85
reed@google.comdff7e112013-05-15 19:34:20 +000086 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000087 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000088
89 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
reed9594da12014-09-12 12:12:27 -070090 SkAutoDataUnref data(SkData::NewFromFileName(FLAGS_luaFile[i]));
halcanary96fcdcc2015-08-27 07:41:13 -070091 if (nullptr == data.get()) {
reed9594da12014-09-12 12:12:27 -070092 data.reset(SkData::NewEmpty());
93 }
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000094 if (!FLAGS_quiet) {
95 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
96 }
reed@google.com3597b732013-05-22 20:12:50 +000097 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000098 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
99 exit(-1);
100 }
reed@google.comdff7e112013-05-15 19:34:20 +0000101 }
102
reed@google.come2aad272013-05-31 19:46:02 +0000103 if (!FLAGS_headCode.isEmpty()) {
104 L.runCode(FLAGS_headCode[0]);
105 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000106
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000107 int moduloRemainder = -1;
108 int moduloDivisor = -1;
109 SkString moduloStr;
110
111 if (FLAGS_modulo.count() == 2) {
112 moduloRemainder = atoi(FLAGS_modulo[0]);
113 moduloDivisor = atoi(FLAGS_modulo[1]);
114 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
115 SkDebugf("invalid modulo values.\n");
116 return -1;
117 }
118 }
119
reed@google.comdff7e112013-05-15 19:34:20 +0000120 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000121 SkTArray<SkString> paths;
122 if (sk_isdir(FLAGS_skpPath[i])) {
123 // Add all .skp in this directory.
124 const SkString directory(FLAGS_skpPath[i]);
125 SkString filename;
126 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
127 while(iter.next(&filename)) {
tfarinaa8e2e152014-07-28 19:26:58 -0700128 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000129 }
130 } else {
131 // Add this as an .skp itself.
132 paths.push_back() = FLAGS_skpPath[i];
133 }
reed@google.comdff7e112013-05-15 19:34:20 +0000134
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000135 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000136 if (moduloRemainder >= 0) {
137 if ((i % moduloDivisor) != moduloRemainder) {
138 continue;
139 }
140 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
141 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000142 const char* path = paths[i].c_str();
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000143 if (!FLAGS_quiet) {
144 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
145 }
reed@google.comdff7e112013-05-15 19:34:20 +0000146
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000147 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000148 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000149 SkAutoTUnref<SkLuaCanvas> canvas(
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700150 new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
151 SkScalarCeilToInt(pic->cullRect().height()),
reed@google.com2d516772013-05-21 16:05:53 +0000152 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000153
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000154 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
robertphillips9b14f262014-06-04 05:40:44 -0700155 canvas->drawPicture(pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000156 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000157
reed@google.comdff7e112013-05-15 19:34:20 +0000158 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000159 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000160 }
161 }
162 }
163 return 0;
164}
165
166#if !defined SK_BUILD_FOR_IOS
167int main(int argc, char * const argv[]) {
168 return tool_main(argc, (char**) argv);
169}
170#endif