blob: f1c5ce920f0c720868a5b33102bc0f0621390a60 [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
commit-bot@chromium.org56799e22013-07-16 18:21:46 +00008#include "LazyDecodeBitmap.h"
reed@google.com3597b732013-05-22 20:12:50 +00009#include "SkLua.h"
reed@google.comdff7e112013-05-15 19:34:20 +000010#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
20extern "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
33DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
34 "testIndex %% divisor == remainder.");
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +000035DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
reed@google.comdff7e112013-05-15 19:34:20 +000036DEFINE_string2(luaFile, l, "", "File containing lua script to run");
reed@google.come2aad272013-05-31 19:46:02 +000037DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
38DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000039DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
reed@google.comdff7e112013-05-15 19:34:20 +000040
41static SkPicture* load_picture(const char path[]) {
42 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
43 SkPicture* pic = NULL;
44 if (stream.get()) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000045 pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);
reed@google.comdff7e112013-05-15 19:34:20 +000046 }
47 return pic;
48}
49
50static SkData* read_into_data(const char file[]) {
51 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
52 if (!stream.get()) {
53 return SkData::NewEmpty();
54 }
55 size_t len = stream->getLength();
56 void* buffer = sk_malloc_throw(len);
57 stream->read(buffer, len);
58 return SkData::NewFromMalloc(buffer, len);
59}
60
reed@google.com2d516772013-05-21 16:05:53 +000061static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
62 const char pictureFile[], const char funcName[]) {
63 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000064 if (!lua_isfunction(L, -1)) {
65 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000066 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000067 lua_settop(L, -2);
68 } else {
69 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000070 lua_pushstring(L, pictureFile);
71 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000072 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
73 }
74 }
75}
76
reed@google.comdff7e112013-05-15 19:34:20 +000077int tool_main(int argc, char** argv);
78int tool_main(int argc, char** argv) {
79 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
80 SkCommandLineFlags::Parse(argc, argv);
81
82 if (FLAGS_skpPath.isEmpty()) {
83 SkDebugf(".skp files or directories are required.\n");
84 exit(-1);
85 }
86 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000087 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000088 exit(-1);
89 }
90
reed@google.come2aad272013-05-31 19:46:02 +000091 const char* summary = gSummarizeFunc;
92 if (!FLAGS_tailFunc.isEmpty()) {
93 summary = FLAGS_tailFunc[0];
94 }
95
reed@google.comdff7e112013-05-15 19:34:20 +000096 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000097 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000098
99 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
100 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000101 if (!FLAGS_quiet) {
102 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
103 }
reed@google.com3597b732013-05-22 20:12:50 +0000104 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000105 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
106 exit(-1);
107 }
reed@google.comdff7e112013-05-15 19:34:20 +0000108 }
109
reed@google.come2aad272013-05-31 19:46:02 +0000110 if (!FLAGS_headCode.isEmpty()) {
111 L.runCode(FLAGS_headCode[0]);
112 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000113
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000114 int moduloRemainder = -1;
115 int moduloDivisor = -1;
116 SkString moduloStr;
117
118 if (FLAGS_modulo.count() == 2) {
119 moduloRemainder = atoi(FLAGS_modulo[0]);
120 moduloDivisor = atoi(FLAGS_modulo[1]);
121 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
122 SkDebugf("invalid modulo values.\n");
123 return -1;
124 }
125 }
126
reed@google.comdff7e112013-05-15 19:34:20 +0000127 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000128 SkTArray<SkString> paths;
129 if (sk_isdir(FLAGS_skpPath[i])) {
130 // Add all .skp in this directory.
131 const SkString directory(FLAGS_skpPath[i]);
132 SkString filename;
133 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
134 while(iter.next(&filename)) {
135 sk_tools::make_filepath(&paths.push_back(), directory, filename);
136 }
137 } else {
138 // Add this as an .skp itself.
139 paths.push_back() = FLAGS_skpPath[i];
140 }
reed@google.comdff7e112013-05-15 19:34:20 +0000141
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000142 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000143 if (moduloRemainder >= 0) {
144 if ((i % moduloDivisor) != moduloRemainder) {
145 continue;
146 }
147 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
148 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000149 const char* path = paths[i].c_str();
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000150 if (!FLAGS_quiet) {
151 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
152 }
reed@google.comdff7e112013-05-15 19:34:20 +0000153
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000154 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000155 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000156 SkAutoTUnref<SkLuaCanvas> canvas(
157 new SkLuaCanvas(pic->width(), pic->height(),
158 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000159
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000160 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
robertphillips9b14f262014-06-04 05:40:44 -0700161 canvas->drawPicture(pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000162 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000163
reed@google.comdff7e112013-05-15 19:34:20 +0000164 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000165 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000166 }
167 }
168 }
169 return 0;
170}
171
172#if !defined SK_BUILD_FOR_IOS
173int main(int argc, char * const argv[]) {
174 return tool_main(argc, (char**) argv);
175}
176#endif