blob: 355a82352aea28d4a558f1b51a1019f226bcc7ce [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");
reed@google.comdff7e112013-05-15 19:34:20 +000039
40static SkPicture* load_picture(const char path[]) {
41 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
42 SkPicture* pic = NULL;
43 if (stream.get()) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000044 pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap);
reed@google.comdff7e112013-05-15 19:34:20 +000045 }
46 return pic;
47}
48
49static SkData* read_into_data(const char file[]) {
50 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
51 if (!stream.get()) {
52 return SkData::NewEmpty();
53 }
54 size_t len = stream->getLength();
55 void* buffer = sk_malloc_throw(len);
56 stream->read(buffer, len);
57 return SkData::NewFromMalloc(buffer, len);
58}
59
reed@google.com2d516772013-05-21 16:05:53 +000060static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
61 const char pictureFile[], const char funcName[]) {
62 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000063 if (!lua_isfunction(L, -1)) {
64 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000065 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000066 lua_settop(L, -2);
67 } else {
68 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000069 lua_pushstring(L, pictureFile);
70 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000071 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
72 }
73 }
74}
75
reed@google.comdff7e112013-05-15 19:34:20 +000076int tool_main(int argc, char** argv);
77int tool_main(int argc, char** argv) {
78 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
79 SkCommandLineFlags::Parse(argc, argv);
80
81 if (FLAGS_skpPath.isEmpty()) {
82 SkDebugf(".skp files or directories are required.\n");
83 exit(-1);
84 }
85 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000086 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000087 exit(-1);
88 }
89
reed@google.come2aad272013-05-31 19:46:02 +000090 const char* summary = gSummarizeFunc;
91 if (!FLAGS_tailFunc.isEmpty()) {
92 summary = FLAGS_tailFunc[0];
93 }
94
reed@google.comdff7e112013-05-15 19:34:20 +000095 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +000096 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000097
98 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
99 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
100 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
reed@google.com3597b732013-05-22 20:12:50 +0000101 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000102 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
103 exit(-1);
104 }
reed@google.comdff7e112013-05-15 19:34:20 +0000105 }
106
reed@google.come2aad272013-05-31 19:46:02 +0000107 if (!FLAGS_headCode.isEmpty()) {
108 L.runCode(FLAGS_headCode[0]);
109 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000110
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000111 int moduloRemainder = -1;
112 int moduloDivisor = -1;
113 SkString moduloStr;
114
115 if (FLAGS_modulo.count() == 2) {
116 moduloRemainder = atoi(FLAGS_modulo[0]);
117 moduloDivisor = atoi(FLAGS_modulo[1]);
118 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
119 SkDebugf("invalid modulo values.\n");
120 return -1;
121 }
122 }
123
reed@google.comdff7e112013-05-15 19:34:20 +0000124 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000125 SkTArray<SkString> paths;
126 if (sk_isdir(FLAGS_skpPath[i])) {
127 // Add all .skp in this directory.
128 const SkString directory(FLAGS_skpPath[i]);
129 SkString filename;
130 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
131 while(iter.next(&filename)) {
132 sk_tools::make_filepath(&paths.push_back(), directory, filename);
133 }
134 } else {
135 // Add this as an .skp itself.
136 paths.push_back() = FLAGS_skpPath[i];
137 }
reed@google.comdff7e112013-05-15 19:34:20 +0000138
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000139 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000140 if (moduloRemainder >= 0) {
141 if ((i % moduloDivisor) != moduloRemainder) {
142 continue;
143 }
144 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
145 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000146 const char* path = paths[i].c_str();
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000147 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
reed@google.comdff7e112013-05-15 19:34:20 +0000148
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000149 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000150 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000151 SkAutoTUnref<SkLuaCanvas> canvas(
152 new SkLuaCanvas(pic->width(), pic->height(),
153 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);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000156 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