blob: 468d85bf6a862dad625689a3a252dfa4ef6b3d37 [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"
reed@google.comdff7e112013-05-15 19:34:20 +000015#include "SkOSFile.h"
fmalitad067ae72016-11-09 09:15:05 -080016#include "SkOSPath.h"
reed@google.comdff7e112013-05-15 19:34:20 +000017
bungeman60e0fee2015-08-26 05:15:46 -070018#include <stdlib.h>
19
reed@google.comdff7e112013-05-15 19:34:20 +000020extern "C" {
21 #include "lua.h"
22 #include "lualib.h"
23 #include "lauxlib.h"
24}
25
reed@google.com2d516772013-05-21 16:05:53 +000026static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
27static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
28static const char gAccumulateFunc[] = "sk_scrape_accumulate";
29static const char gSummarizeFunc[] = "sk_scrape_summarize";
30
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000031// 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
Mike Klein38af9432016-11-11 11:39:44 -050033static DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000034 "testIndex %% divisor == remainder.");
Mike Klein38af9432016-11-11 11:39:44 -050035static DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
36static DEFINE_string2(luaFile, l, "", "File containing lua script to run");
37static DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
38static DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
39static DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
reed@google.comdff7e112013-05-15 19:34:20 +000040
reedca2622b2016-03-18 07:25:55 -070041static sk_sp<SkPicture> load_picture(const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -070042 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
43 if (stream) {
reedca2622b2016-03-18 07:25:55 -070044 return SkPicture::MakeFromStream(stream.get());
reed@google.comdff7e112013-05-15 19:34:20 +000045 }
reedca2622b2016-03-18 07:25:55 -070046 return nullptr;
reed@google.comdff7e112013-05-15 19:34:20 +000047}
48
reed@google.com2d516772013-05-21 16:05:53 +000049static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
50 const char pictureFile[], const char funcName[]) {
51 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000052 if (!lua_isfunction(L, -1)) {
53 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000054 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000055 lua_settop(L, -2);
56 } else {
57 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000058 lua_pushstring(L, pictureFile);
59 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000060 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
61 }
62 }
63}
64
Mike Kleinbe28ee22017-02-06 12:46:20 -050065int main(int argc, char** argv) {
reed@google.comdff7e112013-05-15 19:34:20 +000066 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
67 SkCommandLineFlags::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()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000074 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000075 exit(-1);
76 }
77
reed@google.come2aad272013-05-31 19:46:02 +000078 const char* summary = gSummarizeFunc;
79 if (!FLAGS_tailFunc.isEmpty()) {
80 summary = FLAGS_tailFunc[0];
81 }
82
reed@google.comdff7e112013-05-15 19:34:20 +000083 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000084 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000085
86 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
bungeman38d909e2016-08-02 14:40:46 -070087 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_luaFile[i]));
88 if (!data) {
89 data = SkData::MakeEmpty();
reed9594da12014-09-12 12:12:27 -070090 }
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000091 if (!FLAGS_quiet) {
92 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
93 }
reed@google.com3597b732013-05-22 20:12:50 +000094 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000095 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
96 exit(-1);
97 }
reed@google.comdff7e112013-05-15 19:34:20 +000098 }
99
reed@google.come2aad272013-05-31 19:46:02 +0000100 if (!FLAGS_headCode.isEmpty()) {
101 L.runCode(FLAGS_headCode[0]);
102 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000103
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000104 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
reed@google.comdff7e112013-05-15 19:34:20 +0000117 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000118 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)) {
tfarinaa8e2e152014-07-28 19:26:58 -0700125 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000126 }
127 } else {
128 // Add this as an .skp itself.
129 paths.push_back() = FLAGS_skpPath[i];
130 }
reed@google.comdff7e112013-05-15 19:34:20 +0000131
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000132 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000133 if (moduloRemainder >= 0) {
134 if ((i % moduloDivisor) != moduloRemainder) {
135 continue;
136 }
137 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
138 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000139 const char* path = paths[i].c_str();
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000140 if (!FLAGS_quiet) {
141 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
142 }
reed@google.comdff7e112013-05-15 19:34:20 +0000143
reedca2622b2016-03-18 07:25:55 -0700144 auto pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000145 if (pic.get()) {
Mike Reed5df49342016-11-12 08:06:55 -0600146 std::unique_ptr<SkLuaCanvas> canvas(
halcanary9d524f22016-03-29 09:03:52 -0700147 new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700148 SkScalarCeilToInt(pic->cullRect().height()),
reed@google.com2d516772013-05-21 16:05:53 +0000149 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000150
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000151 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
robertphillips9b14f262014-06-04 05:40:44 -0700152 canvas->drawPicture(pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000153 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000154
reed@google.comdff7e112013-05-15 19:34:20 +0000155 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000156 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000157 }
158 }
159 }
160 return 0;
161}