2013-05-15 19:34:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-05-22 20:12:50 +00:00
|
|
|
#include "SkLua.h"
|
2013-05-15 19:34:20 +00:00
|
|
|
#include "SkLuaCanvas.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "picture_utils.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
|
2015-08-26 12:15:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-05-15 19:34:20 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:05:53 +00:00
|
|
|
static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
|
|
|
|
static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
|
|
|
|
static const char gAccumulateFunc[] = "sk_scrape_accumulate";
|
|
|
|
static const char gSummarizeFunc[] = "sk_scrape_summarize";
|
|
|
|
|
2013-06-21 21:45:20 +00:00
|
|
|
// Example usage for the modulo flag:
|
|
|
|
// for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done
|
|
|
|
DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
|
|
|
|
"testIndex %% divisor == remainder.");
|
2013-06-18 18:35:58 +00:00
|
|
|
DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
|
2013-05-15 19:34:20 +00:00
|
|
|
DEFINE_string2(luaFile, l, "", "File containing lua script to run");
|
2013-05-31 19:46:02 +00:00
|
|
|
DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
|
|
|
|
DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
|
2014-03-19 19:23:17 +00:00
|
|
|
DEFINE_bool2(quiet, q, false, "Silence all non-error related output");
|
2013-05-15 19:34:20 +00:00
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
static sk_sp<SkPicture> load_picture(const char path[]) {
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
|
2013-05-15 19:34:20 +00:00
|
|
|
if (stream.get()) {
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkPicture::MakeFromStream(stream.get());
|
2013-05-15 19:34:20 +00:00
|
|
|
}
|
2016-03-18 14:25:55 +00:00
|
|
|
return nullptr;
|
2013-05-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:05:53 +00:00
|
|
|
static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
|
|
|
|
const char pictureFile[], const char funcName[]) {
|
|
|
|
lua_getglobal(L, funcName);
|
2013-05-21 12:20:39 +00:00
|
|
|
if (!lua_isfunction(L, -1)) {
|
|
|
|
int t = lua_type(L, -1);
|
2013-05-21 16:05:53 +00:00
|
|
|
SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
|
2013-05-21 12:20:39 +00:00
|
|
|
lua_settop(L, -2);
|
|
|
|
} else {
|
|
|
|
canvas->pushThis();
|
2013-05-21 16:05:53 +00:00
|
|
|
lua_pushstring(L, pictureFile);
|
|
|
|
if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
|
2013-05-21 12:20:39 +00:00
|
|
|
SkDebugf("lua err: %s\n", lua_tostring(L, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:34:20 +00:00
|
|
|
int tool_main(int argc, char** argv);
|
|
|
|
int tool_main(int argc, char** argv) {
|
|
|
|
SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
|
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
|
|
|
|
if (FLAGS_skpPath.isEmpty()) {
|
|
|
|
SkDebugf(".skp files or directories are required.\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (FLAGS_luaFile.isEmpty()) {
|
2013-05-21 03:24:37 +00:00
|
|
|
SkDebugf("missing luaFile(s)\n");
|
2013-05-15 19:34:20 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2013-05-31 19:46:02 +00:00
|
|
|
const char* summary = gSummarizeFunc;
|
|
|
|
if (!FLAGS_tailFunc.isEmpty()) {
|
|
|
|
summary = FLAGS_tailFunc[0];
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:34:20 +00:00
|
|
|
SkAutoGraphics ag;
|
2013-05-31 19:46:02 +00:00
|
|
|
SkLua L(summary);
|
2013-05-21 03:24:37 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
|
2014-09-12 19:12:27 +00:00
|
|
|
SkAutoDataUnref data(SkData::NewFromFileName(FLAGS_luaFile[i]));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == data.get()) {
|
2014-09-12 19:12:27 +00:00
|
|
|
data.reset(SkData::NewEmpty());
|
|
|
|
}
|
2014-03-19 19:23:17 +00:00
|
|
|
if (!FLAGS_quiet) {
|
|
|
|
SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
|
|
|
|
}
|
2013-05-22 20:12:50 +00:00
|
|
|
if (!L.runCode(data->data(), data->size())) {
|
2013-05-21 03:24:37 +00:00
|
|
|
SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2013-05-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 19:46:02 +00:00
|
|
|
if (!FLAGS_headCode.isEmpty()) {
|
|
|
|
L.runCode(FLAGS_headCode[0]);
|
|
|
|
}
|
2013-06-01 07:01:39 +00:00
|
|
|
|
2013-06-21 21:45:20 +00:00
|
|
|
int moduloRemainder = -1;
|
|
|
|
int moduloDivisor = -1;
|
|
|
|
SkString moduloStr;
|
|
|
|
|
|
|
|
if (FLAGS_modulo.count() == 2) {
|
|
|
|
moduloRemainder = atoi(FLAGS_modulo[0]);
|
|
|
|
moduloDivisor = atoi(FLAGS_modulo[1]);
|
|
|
|
if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
|
|
|
|
SkDebugf("invalid modulo values.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:34:20 +00:00
|
|
|
for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
|
2013-06-18 18:35:58 +00:00
|
|
|
SkTArray<SkString> paths;
|
|
|
|
if (sk_isdir(FLAGS_skpPath[i])) {
|
|
|
|
// Add all .skp in this directory.
|
|
|
|
const SkString directory(FLAGS_skpPath[i]);
|
|
|
|
SkString filename;
|
|
|
|
SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
|
|
|
|
while(iter.next(&filename)) {
|
2014-07-29 02:26:58 +00:00
|
|
|
paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str());
|
2013-06-18 18:35:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add this as an .skp itself.
|
|
|
|
paths.push_back() = FLAGS_skpPath[i];
|
|
|
|
}
|
2013-05-16 07:01:00 +00:00
|
|
|
|
2013-06-18 18:35:58 +00:00
|
|
|
for (int i = 0; i < paths.count(); i++) {
|
2013-06-21 21:45:20 +00:00
|
|
|
if (moduloRemainder >= 0) {
|
|
|
|
if ((i % moduloDivisor) != moduloRemainder) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
moduloStr.printf("[%d.%d] ", i, moduloDivisor);
|
|
|
|
}
|
2013-06-18 18:35:58 +00:00
|
|
|
const char* path = paths[i].c_str();
|
2014-03-19 19:23:17 +00:00
|
|
|
if (!FLAGS_quiet) {
|
|
|
|
SkDebugf("scraping %s %s\n", path, moduloStr.c_str());
|
|
|
|
}
|
2013-05-15 19:34:20 +00:00
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
auto pic(load_picture(path));
|
2013-05-15 19:34:20 +00:00
|
|
|
if (pic.get()) {
|
2013-05-21 16:05:53 +00:00
|
|
|
SkAutoTUnref<SkLuaCanvas> canvas(
|
2014-08-29 15:03:56 +00:00
|
|
|
new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()),
|
|
|
|
SkScalarCeilToInt(pic->cullRect().height()),
|
2013-05-21 16:05:53 +00:00
|
|
|
L.get(), gAccumulateFunc));
|
2013-05-21 12:20:39 +00:00
|
|
|
|
2013-06-18 18:35:58 +00:00
|
|
|
call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
|
2014-06-04 12:40:44 +00:00
|
|
|
canvas->drawPicture(pic);
|
2013-06-18 18:35:58 +00:00
|
|
|
call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
|
2013-05-21 16:05:53 +00:00
|
|
|
|
2013-05-15 19:34:20 +00:00
|
|
|
} else {
|
2013-05-21 01:49:06 +00:00
|
|
|
SkDebugf("failed to load\n");
|
2013-05-15 19:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined SK_BUILD_FOR_IOS
|
|
|
|
int main(int argc, char * const argv[]) {
|
|
|
|
return tool_main(argc, (char**) argv);
|
|
|
|
}
|
|
|
|
#endif
|