blob: d0343a4548ecfb1559d60f94375479eabac45b68 [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
102int tool_main(int argc, char** argv);
103int tool_main(int argc, char** argv) {
104 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
105 SkCommandLineFlags::Parse(argc, argv);
106
107 if (FLAGS_skpPath.isEmpty()) {
108 SkDebugf(".skp files or directories are required.\n");
109 exit(-1);
110 }
111 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000112 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000113 exit(-1);
114 }
115
reed@google.comdff7e112013-05-15 19:34:20 +0000116 SkAutoGraphics ag;
117 SkAutoLua L("summarize");
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000118
119 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
120 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
121 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
122 if (!L.load(data->data(), data->size())) {
123 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
124 exit(-1);
125 }
reed@google.comdff7e112013-05-15 19:34:20 +0000126 }
127
128 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
129 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
130 SkString inputFilename;
131
132 while (iter.next(&inputFilename)) {
133 SkString inputPath;
134 SkString inputAsSkString(FLAGS_skpPath[i]);
135 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
skia.committer@gmail.com539f3642013-05-16 07:01:00 +0000136
reed@google.comdff7e112013-05-15 19:34:20 +0000137 const char* path = inputPath.c_str();
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000138 SkDebugf("scraping %s\n", path);
reed@google.comdff7e112013-05-15 19:34:20 +0000139
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000140 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000141 if (pic.get()) {
reed@google.comdff7e112013-05-15 19:34:20 +0000142 SkLuaCanvas canvas(pic->width(), pic->height(), L.get(), "accumulate");
143 canvas.drawPicture(*pic);
144 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000145 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000146 }
147 }
148 }
149 return 0;
150}
151
152#if !defined SK_BUILD_FOR_IOS
153int main(int argc, char * const argv[]) {
154 return tool_main(argc, (char**) argv);
155}
156#endif