blob: f1bca287ad490dae592c4ed33ef00bf6bbb8053b [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"
17#include "SkImageDecoder.h"
reed@google.comb8b3d902013-06-12 20:07:10 +000018#include "SkForceLinking.h"
19
20__SK_FORCE_IMAGE_DECODER_LINKING;
reed@google.comdff7e112013-05-15 19:34:20 +000021
22extern "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
reed@google.comdff7e112013-05-15 19:34:20 +000033// PictureRenderingFlags.cpp
34extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*);
35
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +000036// Example usage for the modulo flag:
37// for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done
38DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
39 "testIndex %% divisor == remainder.");
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +000040DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
reed@google.comdff7e112013-05-15 19:34:20 +000041DEFINE_string2(luaFile, l, "", "File containing lua script to run");
reed@google.come2aad272013-05-31 19:46:02 +000042DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
43DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
reed@google.comdff7e112013-05-15 19:34:20 +000044
45static SkPicture* load_picture(const char path[]) {
46 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
47 SkPicture* pic = NULL;
48 if (stream.get()) {
scroggo@google.comf1754ec2013-06-28 21:32:00 +000049 pic = SkPicture::CreateFromStream(stream.get(), &lazy_decode_bitmap);
reed@google.comdff7e112013-05-15 19:34:20 +000050 }
51 return pic;
52}
53
54static SkData* read_into_data(const char file[]) {
55 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
56 if (!stream.get()) {
57 return SkData::NewEmpty();
58 }
59 size_t len = stream->getLength();
60 void* buffer = sk_malloc_throw(len);
61 stream->read(buffer, len);
62 return SkData::NewFromMalloc(buffer, len);
63}
64
reed@google.com2d516772013-05-21 16:05:53 +000065static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
66 const char pictureFile[], const char funcName[]) {
67 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000068 if (!lua_isfunction(L, -1)) {
69 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000070 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000071 lua_settop(L, -2);
72 } else {
73 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000074 lua_pushstring(L, pictureFile);
75 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000076 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
77 }
78 }
79}
80
reed@google.comdff7e112013-05-15 19:34:20 +000081int tool_main(int argc, char** argv);
82int tool_main(int argc, char** argv) {
83 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
84 SkCommandLineFlags::Parse(argc, argv);
85
86 if (FLAGS_skpPath.isEmpty()) {
87 SkDebugf(".skp files or directories are required.\n");
88 exit(-1);
89 }
90 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000091 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000092 exit(-1);
93 }
94
reed@google.come2aad272013-05-31 19:46:02 +000095 const char* summary = gSummarizeFunc;
96 if (!FLAGS_tailFunc.isEmpty()) {
97 summary = FLAGS_tailFunc[0];
98 }
99
reed@google.comdff7e112013-05-15 19:34:20 +0000100 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +0000101 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000102
103 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
104 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
105 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
reed@google.com3597b732013-05-22 20:12:50 +0000106 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000107 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
108 exit(-1);
109 }
reed@google.comdff7e112013-05-15 19:34:20 +0000110 }
111
reed@google.come2aad272013-05-31 19:46:02 +0000112 if (!FLAGS_headCode.isEmpty()) {
113 L.runCode(FLAGS_headCode[0]);
114 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000115
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000116 int moduloRemainder = -1;
117 int moduloDivisor = -1;
118 SkString moduloStr;
119
120 if (FLAGS_modulo.count() == 2) {
121 moduloRemainder = atoi(FLAGS_modulo[0]);
122 moduloDivisor = atoi(FLAGS_modulo[1]);
123 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
124 SkDebugf("invalid modulo values.\n");
125 return -1;
126 }
127 }
128
reed@google.comdff7e112013-05-15 19:34:20 +0000129 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000130 SkTArray<SkString> paths;
131 if (sk_isdir(FLAGS_skpPath[i])) {
132 // Add all .skp in this directory.
133 const SkString directory(FLAGS_skpPath[i]);
134 SkString filename;
135 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
136 while(iter.next(&filename)) {
137 sk_tools::make_filepath(&paths.push_back(), directory, filename);
138 }
139 } else {
140 // Add this as an .skp itself.
141 paths.push_back() = FLAGS_skpPath[i];
142 }
reed@google.comdff7e112013-05-15 19:34:20 +0000143
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000144 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000145 if (moduloRemainder >= 0) {
146 if ((i % moduloDivisor) != moduloRemainder) {
147 continue;
148 }
149 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
150 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000151 const char* path = paths[i].c_str();
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000152 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
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);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000161 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