blob: 85fbd98f601a317c1815e8b6a2b26b38a9a6ecec [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"
fmalitad067ae72016-11-09 09:15:05 -080017#include "SkOSPath.h"
reed@google.comdff7e112013-05-15 19:34:20 +000018
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
Mike Klein38af9432016-11-11 11:39:44 -050034static DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000035 "testIndex %% divisor == remainder.");
Mike Klein38af9432016-11-11 11:39:44 -050036static DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
37static DEFINE_string2(luaFile, l, "", "File containing lua script to run");
38static DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
39static DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
40static DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
reed@google.comdff7e112013-05-15 19:34:20 +000041
reedca2622b2016-03-18 07:25:55 -070042static sk_sp<SkPicture> load_picture(const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -070043 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
44 if (stream) {
reedca2622b2016-03-18 07:25:55 -070045 return SkPicture::MakeFromStream(stream.get());
reed@google.comdff7e112013-05-15 19:34:20 +000046 }
reedca2622b2016-03-18 07:25:55 -070047 return nullptr;
reed@google.comdff7e112013-05-15 19:34:20 +000048}
49
reed@google.com2d516772013-05-21 16:05:53 +000050static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
51 const char pictureFile[], const char funcName[]) {
52 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000053 if (!lua_isfunction(L, -1)) {
54 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000055 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000056 lua_settop(L, -2);
57 } else {
58 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000059 lua_pushstring(L, pictureFile);
60 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000061 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
62 }
63 }
64}
65
Mike Kleinbe28ee22017-02-06 12:46:20 -050066int main(int argc, char** argv) {
reed@google.comdff7e112013-05-15 19:34:20 +000067 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
68 SkCommandLineFlags::Parse(argc, argv);
69
70 if (FLAGS_skpPath.isEmpty()) {
71 SkDebugf(".skp files or directories are required.\n");
72 exit(-1);
73 }
74 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000075 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000076 exit(-1);
77 }
78
reed@google.come2aad272013-05-31 19:46:02 +000079 const char* summary = gSummarizeFunc;
80 if (!FLAGS_tailFunc.isEmpty()) {
81 summary = FLAGS_tailFunc[0];
82 }
83
reed@google.comdff7e112013-05-15 19:34:20 +000084 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000085 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000086
87 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
bungeman38d909e2016-08-02 14:40:46 -070088 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_luaFile[i]));
89 if (!data) {
90 data = SkData::MakeEmpty();
reed9594da12014-09-12 12:12:27 -070091 }
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000092 if (!FLAGS_quiet) {
93 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
94 }
reed@google.com3597b732013-05-22 20:12:50 +000095 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000096 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
97 exit(-1);
98 }
reed@google.comdff7e112013-05-15 19:34:20 +000099 }
100
reed@google.come2aad272013-05-31 19:46:02 +0000101 if (!FLAGS_headCode.isEmpty()) {
102 L.runCode(FLAGS_headCode[0]);
103 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000104
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000105 int moduloRemainder = -1;
106 int moduloDivisor = -1;
107 SkString moduloStr;
108
109 if (FLAGS_modulo.count() == 2) {
110 moduloRemainder = atoi(FLAGS_modulo[0]);
111 moduloDivisor = atoi(FLAGS_modulo[1]);
112 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
113 SkDebugf("invalid modulo values.\n");
114 return -1;
115 }
116 }
117
reed@google.comdff7e112013-05-15 19:34:20 +0000118 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000119 SkTArray<SkString> paths;
120 if (sk_isdir(FLAGS_skpPath[i])) {
121 // Add all .skp in this directory.
122 const SkString directory(FLAGS_skpPath[i]);
123 SkString filename;
124 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
125 while(iter.next(&filename)) {
tfarinaa8e2e152014-07-28 19:26:58 -0700126 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000127 }
128 } else {
129 // Add this as an .skp itself.
130 paths.push_back() = FLAGS_skpPath[i];
131 }
reed@google.comdff7e112013-05-15 19:34:20 +0000132
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000133 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000134 if (moduloRemainder >= 0) {
135 if ((i % moduloDivisor) != moduloRemainder) {
136 continue;
137 }
138 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
139 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000140 const char* path = paths[i].c_str();
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000141 if (!FLAGS_quiet) {
142 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
143 }
reed@google.comdff7e112013-05-15 19:34:20 +0000144
reedca2622b2016-03-18 07:25:55 -0700145 auto pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000146 if (pic.get()) {
Mike Reed5df49342016-11-12 08:06:55 -0600147 std::unique_ptr<SkLuaCanvas> canvas(
halcanary9d524f22016-03-29 09:03:52 -0700148 new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700149 SkScalarCeilToInt(pic->cullRect().height()),
reed@google.com2d516772013-05-21 16:05:53 +0000150 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000151
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000152 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
robertphillips9b14f262014-06-04 05:40:44 -0700153 canvas->drawPicture(pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000154 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000155
reed@google.comdff7e112013-05-15 19:34:20 +0000156 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000157 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000158 }
159 }
160 }
161 return 0;
162}