blob: 02b9a57fb4701c66c36fd1b21beffd3b8c04d089 [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()) {
49 bool success;
50 pic = SkNEW_ARGS(SkPicture, (stream.get(), &success,
51 &lazy_decode_bitmap));
52 if (!success) {
53 SkDELETE(pic);
54 pic = NULL;
55 }
56 }
57 return pic;
58}
59
60static SkData* read_into_data(const char file[]) {
61 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
62 if (!stream.get()) {
63 return SkData::NewEmpty();
64 }
65 size_t len = stream->getLength();
66 void* buffer = sk_malloc_throw(len);
67 stream->read(buffer, len);
68 return SkData::NewFromMalloc(buffer, len);
69}
70
reed@google.com2d516772013-05-21 16:05:53 +000071static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
72 const char pictureFile[], const char funcName[]) {
73 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000074 if (!lua_isfunction(L, -1)) {
75 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000076 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000077 lua_settop(L, -2);
78 } else {
79 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000080 lua_pushstring(L, pictureFile);
81 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000082 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
83 }
84 }
85}
86
reed@google.comdff7e112013-05-15 19:34:20 +000087int tool_main(int argc, char** argv);
88int tool_main(int argc, char** argv) {
89 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
90 SkCommandLineFlags::Parse(argc, argv);
91
92 if (FLAGS_skpPath.isEmpty()) {
93 SkDebugf(".skp files or directories are required.\n");
94 exit(-1);
95 }
96 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000097 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000098 exit(-1);
99 }
100
reed@google.come2aad272013-05-31 19:46:02 +0000101 const char* summary = gSummarizeFunc;
102 if (!FLAGS_tailFunc.isEmpty()) {
103 summary = FLAGS_tailFunc[0];
104 }
105
reed@google.comdff7e112013-05-15 19:34:20 +0000106 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +0000107 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000108
109 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
110 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
111 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
reed@google.com3597b732013-05-22 20:12:50 +0000112 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000113 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
114 exit(-1);
115 }
reed@google.comdff7e112013-05-15 19:34:20 +0000116 }
117
reed@google.come2aad272013-05-31 19:46:02 +0000118 if (!FLAGS_headCode.isEmpty()) {
119 L.runCode(FLAGS_headCode[0]);
120 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000121
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000122 int moduloRemainder = -1;
123 int moduloDivisor = -1;
124 SkString moduloStr;
125
126 if (FLAGS_modulo.count() == 2) {
127 moduloRemainder = atoi(FLAGS_modulo[0]);
128 moduloDivisor = atoi(FLAGS_modulo[1]);
129 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
130 SkDebugf("invalid modulo values.\n");
131 return -1;
132 }
133 }
134
reed@google.comdff7e112013-05-15 19:34:20 +0000135 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000136 SkTArray<SkString> paths;
137 if (sk_isdir(FLAGS_skpPath[i])) {
138 // Add all .skp in this directory.
139 const SkString directory(FLAGS_skpPath[i]);
140 SkString filename;
141 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
142 while(iter.next(&filename)) {
143 sk_tools::make_filepath(&paths.push_back(), directory, filename);
144 }
145 } else {
146 // Add this as an .skp itself.
147 paths.push_back() = FLAGS_skpPath[i];
148 }
reed@google.comdff7e112013-05-15 19:34:20 +0000149
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000150 for (int i = 0; i < paths.count(); i++) {
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000151 if (moduloRemainder >= 0) {
152 if ((i % moduloDivisor) != moduloRemainder) {
153 continue;
154 }
155 moduloStr.printf("[%d.%d] ", i, moduloDivisor);
156 }
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000157 const char* path = paths[i].c_str();
commit-bot@chromium.orgaaeb87d2013-06-21 21:45:20 +0000158 SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
reed@google.comdff7e112013-05-15 19:34:20 +0000159
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000160 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000161 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000162 SkAutoTUnref<SkLuaCanvas> canvas(
163 new SkLuaCanvas(pic->width(), pic->height(),
164 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000165
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000166 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000167 canvas->drawPicture(*pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000168 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000169
reed@google.comdff7e112013-05-15 19:34:20 +0000170 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000171 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000172 }
173 }
174 }
175 return 0;
176}
177
178#if !defined SK_BUILD_FOR_IOS
179int main(int argc, char * const argv[]) {
180 return tool_main(argc, (char**) argv);
181}
182#endif