blob: 6fa5813d77818685f531ea5afe0da7d298e9987a [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
bungeman60e0fee2015-08-26 05:15:46 -070020#include <stdlib.h>
21
reed@google.comdff7e112013-05-15 19:34:20 +000022extern "C" {
23 #include "lua.h"
24 #include "lualib.h"
25 #include "lauxlib.h"
26}
27
reed@google.com2d516772013-05-21 16:05:53 +000028static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
29static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
30static const char gAccumulateFunc[] = "sk_scrape_accumulate";
31static const char gSummarizeFunc[] = "sk_scrape_summarize";
32
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000033// Example usage for the modulo flag:
34// for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done
35DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
36 "testIndex %% divisor == remainder.");
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +000037DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
reed@google.comdff7e112013-05-15 19:34:20 +000038DEFINE_string2(luaFile, l, "", "File containing lua script to run");
reed@google.come2aad272013-05-31 19:46:02 +000039DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
40DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000041DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
reed@google.comdff7e112013-05-15 19:34:20 +000042
43static SkPicture* load_picture(const char path[]) {
scroggoa1193e42015-01-21 12:09:53 -080044 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070045 SkPicture* pic = nullptr;
reed@google.comdff7e112013-05-15 19:34:20 +000046 if (stream.get()) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000047 pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);
reed@google.comdff7e112013-05-15 19:34:20 +000048 }
49 return pic;
50}
51
reed@google.com2d516772013-05-21 16:05:53 +000052static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
53 const char pictureFile[], const char funcName[]) {
54 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000055 if (!lua_isfunction(L, -1)) {
56 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000057 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000058 lua_settop(L, -2);
59 } else {
60 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000061 lua_pushstring(L, pictureFile);
62 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000063 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
64 }
65 }
66}
67
reed@google.comdff7e112013-05-15 19:34:20 +000068int tool_main(int argc, char** argv);
69int tool_main(int argc, char** argv) {
70 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
71 SkCommandLineFlags::Parse(argc, argv);
72
73 if (FLAGS_skpPath.isEmpty()) {
74 SkDebugf(".skp files or directories are required.\n");
75 exit(-1);
76 }
77 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000078 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000079 exit(-1);
80 }
81
reed@google.come2aad272013-05-31 19:46:02 +000082 const char* summary = gSummarizeFunc;
83 if (!FLAGS_tailFunc.isEmpty()) {
84 summary = FLAGS_tailFunc[0];
85 }
86
reed@google.comdff7e112013-05-15 19:34:20 +000087 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000088 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000089
90 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
reed9594da12014-09-12 12:12:27 -070091 SkAutoDataUnref data(SkData::NewFromFileName(FLAGS_luaFile[i]));
halcanary96fcdcc2015-08-27 07:41:13 -070092 if (nullptr == data.get()) {
reed9594da12014-09-12 12:12:27 -070093 data.reset(SkData::NewEmpty());
94 }
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +000095 if (!FLAGS_quiet) {
96 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
97 }
reed@google.com3597b732013-05-22 20:12:50 +000098 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000099 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
100 exit(-1);
101 }
reed@google.comdff7e112013-05-15 19:34:20 +0000102 }
103
reed@google.come2aad272013-05-31 19:46:02 +0000104 if (!FLAGS_headCode.isEmpty()) {
105 L.runCode(FLAGS_headCode[0]);
106 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000107
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000108 int moduloRemainder = -1;
109 int moduloDivisor = -1;
110 SkString moduloStr;
111
112 if (FLAGS_modulo.count() == 2) {
113 moduloRemainder = atoi(FLAGS_modulo[0]);
114 moduloDivisor = atoi(FLAGS_modulo[1]);
115 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
116 SkDebugf("invalid modulo values.\n");
117 return -1;
118 }
119 }
120
reed@google.comdff7e112013-05-15 19:34:20 +0000121 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000122 SkTArray<SkString> paths;
123 if (sk_isdir(FLAGS_skpPath[i])) {
124 // Add all .skp in this directory.
125 const SkString directory(FLAGS_skpPath[i]);
126 SkString filename;
127 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
128 while(iter.next(&filename)) {
tfarinaa8e2e152014-07-28 19:26:58 -0700129 paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000130 }
131 } else {
132 // Add this as an .skp itself.
133 paths.push_back() = FLAGS_skpPath[i];
134 }
reed@google.comdff7e112013-05-15 19:34:20 +0000135
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000136 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000137 if (moduloRemainder >= 0) {
138 if ((i % moduloDivisor) != moduloRemainder) {
139 continue;
140 }
141 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
142 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000143 const char* path = paths[i].c_str();
commit-bot@chromium.orgbdc772e2014-03-19 19:23:17 +0000144 if (!FLAGS_quiet) {
145 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
146 }
reed@google.comdff7e112013-05-15 19:34:20 +0000147
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000148 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000149 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000150 SkAutoTUnref<SkLuaCanvas> canvas(
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700151 new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
152 SkScalarCeilToInt(pic->cullRect().height()),
reed@google.com2d516772013-05-21 16:05:53 +0000153 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000154
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000155 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
robertphillips9b14f262014-06-04 05:40:44 -0700156 canvas->drawPicture(pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000157 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000158
reed@google.comdff7e112013-05-15 19:34:20 +0000159 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000160 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000161 }
162 }
163 }
164 return 0;
165}
166
167#if !defined SK_BUILD_FOR_IOS
168int main(int argc, char * const argv[]) {
169 return tool_main(argc, (char**) argv);
170}
171#endif