blob: 36e8c93bf168caf67425d2b9dfdaaf442d8cc26a [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
8#include "SkLuaCanvas.h"
9#include "SkPicture.h"
10#include "SkCommandLineFlags.h"
11#include "SkGraphics.h"
12#include "SkStream.h"
13#include "SkData.h"
14#include "picture_utils.h"
15#include "SkOSFile.h"
16#include "SkImageDecoder.h"
17
18extern "C" {
19 #include "lua.h"
20 #include "lualib.h"
21 #include "lauxlib.h"
22}
23
24// PictureRenderingFlags.cpp
25extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*);
26
27
28// Flags used by this file, alphabetically:
29DEFINE_string2(skpPath, r, "", "Read .skp files from this dir");
30DEFINE_string2(luaFile, l, "", "File containing lua script to run");
31
32static SkPicture* load_picture(const char path[]) {
33 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
34 SkPicture* pic = NULL;
35 if (stream.get()) {
36 bool success;
37 pic = SkNEW_ARGS(SkPicture, (stream.get(), &success,
38 &lazy_decode_bitmap));
39 if (!success) {
40 SkDELETE(pic);
41 pic = NULL;
42 }
43 }
44 return pic;
45}
46
47static SkData* read_into_data(const char file[]) {
48 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
49 if (!stream.get()) {
50 return SkData::NewEmpty();
51 }
52 size_t len = stream->getLength();
53 void* buffer = sk_malloc_throw(len);
54 stream->read(buffer, len);
55 return SkData::NewFromMalloc(buffer, len);
56}
57
58class SkAutoLua {
59public:
60 SkAutoLua(const char termCode[] = NULL) : fTermCode(termCode) {
61 fL = luaL_newstate();
62 luaL_openlibs(fL);
63 }
64 ~SkAutoLua() {
65 if (fTermCode.size() > 0) {
66 lua_getglobal(fL, fTermCode.c_str());
67 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
68 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
69 }
70 }
71 lua_close(fL);
72 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000073
reed@google.comdff7e112013-05-15 19:34:20 +000074 lua_State* get() const { return fL; }
75 lua_State* operator*() const { return fL; }
76 lua_State* operator->() const { return fL; }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000077
reed@google.comdff7e112013-05-15 19:34:20 +000078 bool load(const char code[]) {
79 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
80 if (err) {
81 SkDebugf("--- lua failed\n");
82 return false;
83 }
84 return true;
85 }
86 bool load(const void* code, size_t size) {
87 SkString str((const char*)code, size);
88 return load(str.c_str());
89 int err = luaL_loadbufferx(fL, (const char*)code, size, NULL, NULL)
90 || lua_pcall(fL, 0, 0, 0);
91 if (err) {
92 SkDebugf("--- lua failed\n");
93 return false;
94 }
95 return true;
96 }
97private:
98 lua_State* fL;
99 SkString fTermCode;
100};
101
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000102static void call_setcanvas(lua_State* L, SkLuaCanvas* canvas) {
103 lua_getglobal(L, "setcanvas");
104 if (!lua_isfunction(L, -1)) {
105 int t = lua_type(L, -1);
106 SkDebugf("--- expected setcanvas function %d\n", t);
107 lua_settop(L, -2);
108 } else {
109 canvas->pushThis();
110 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
111 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
112 }
113 }
114}
115
reed@google.comdff7e112013-05-15 19:34:20 +0000116int tool_main(int argc, char** argv);
117int tool_main(int argc, char** argv) {
118 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
119 SkCommandLineFlags::Parse(argc, argv);
120
121 if (FLAGS_skpPath.isEmpty()) {
122 SkDebugf(".skp files or directories are required.\n");
123 exit(-1);
124 }
125 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000126 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000127 exit(-1);
128 }
129
reed@google.comdff7e112013-05-15 19:34:20 +0000130 SkAutoGraphics ag;
131 SkAutoLua L("summarize");
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000132
133 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
134 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
135 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
136 if (!L.load(data->data(), data->size())) {
137 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
138 exit(-1);
139 }
reed@google.comdff7e112013-05-15 19:34:20 +0000140 }
141
142 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
143 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
144 SkString inputFilename;
145
146 while (iter.next(&inputFilename)) {
147 SkString inputPath;
148 SkString inputAsSkString(FLAGS_skpPath[i]);
149 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
skia.committer@gmail.com539f3642013-05-16 07:01:00 +0000150
reed@google.comdff7e112013-05-15 19:34:20 +0000151 const char* path = inputPath.c_str();
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000152 SkDebugf("scraping %s\n", path);
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()) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000156 SkAutoTUnref<SkLuaCanvas> canvas(new SkLuaCanvas(pic->width(), pic->height(), L.get(), "accumulate"));
157
158 call_setcanvas(L.get(), canvas.get());
159
160 canvas->drawPicture(*pic);
reed@google.comdff7e112013-05-15 19:34:20 +0000161 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000162 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000163 }
164 }
165 }
166 return 0;
167}
168
169#if !defined SK_BUILD_FOR_IOS
170int main(int argc, char * const argv[]) {
171 return tool_main(argc, (char**) argv);
172}
173#endif